Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with Python 3 #3

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6dd1f6a
The following changes made:
NihanPol Feb 4, 2020
18efd05
Add gitignore and fix typo
NihanPol Feb 4, 2020
b215675
fix incompatibility in makefile
NihanPol Feb 4, 2020
a59b31a
ensure pickle import compatibility between py2 and py3
NihanPol Feb 4, 2020
477412c
add SNR of only detected pulsar to their instance
NihanPol Feb 11, 2020
b194af9
Fix print statements for Py3.xx
NihanPol Feb 14, 2020
f776e63
Fix function definition (line 66; def calc_true) in galacticops.py
NihanPol Feb 14, 2020
29526f6
Fix another function definition in galacticops.py (line 213)
NihanPol Feb 14, 2020
cee53e3
fix print statement in orbitalparams.py
NihanPol Feb 14, 2020
e49a007
fix print statement in dosurvey.py
NihanPol Feb 14, 2020
a481d00
fix compatibility issue with inspect.getargspec at line 156 in popula…
NihanPol Feb 14, 2020
b389783
make change to inspect.getargspec compatible with both py2.xx and py3.xx
NihanPol Feb 14, 2020
3aa8c36
Fix instances where comparison with None fails in Py3.xx in populate.…
NihanPol Feb 14, 2020
69b85c7
Explicitly make all initial strings as byte strings since this is not…
NihanPol Feb 14, 2020
899119c
Fix another instance of explicitly setting strings to dtype=bytes sin…
NihanPol Feb 14, 2020
2b6f172
Fix print statememts in evolve.py
NihanPol Feb 14, 2020
8982511
fix the unicode string --> byte string issue in a slightly different,…
NihanPol Feb 14, 2020
394537b
ensure log-space sampling for uniform sampling in distributions.py
NihanPol Feb 18, 2020
32567b7
Add orbital parameters to the Pulsar object. These include:
nspol Apr 7, 2020
609a3e2
fix gpsfrac == NoneType issue
NihanPol Jun 2, 2020
953e18d
change gpsfrac to -1 from None
NihanPol Jun 2, 2020
f8a328c
Fix gpsfrac issue due to definition in __main__
nspol Jun 2, 2020
e414296
change doublespec from Nonetype to -1 to fix incompatibility in py3
nspol Jun 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.so
*.pyc
*.o
*.model
*.cmd
*ipynb*
2 changes: 1 addition & 1 deletion lib/fortran/make_linux.csh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/tcsh

set gf = /usr/bin/gfortran
set gf=/usr/bin/gfortran

$gf -O2 -fPIC -fno-second-underscore -c -I. -std=legacy *.f

Expand Down
2 changes: 1 addition & 1 deletion lib/fortran/makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include makefile.$(OSTYPE)
include makefile.linux

all: libne2001.so libykarea.so libsla.so libvxyz.so libgamma.so

Expand Down
8 changes: 5 additions & 3 deletions lib/python/dataobj.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/python

import os

import cPickle
import os, sys

if sys.version_info[0] < 3:
import cPickle
else:
import pickle as cPickle

class DataObj:
def __init__(self, name, labelDict, dataDict, size):
Expand Down
8 changes: 6 additions & 2 deletions lib/python/degradation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#!/usr/bin/python

import os

import os, sys
import ctypes as C

if sys.version_info[0] < 3:
import cPickle
else:
import pickle as cPickle

# get the FORTRAN libraries
__dir__ = os.path.dirname(os.path.abspath(__file__))
__libdir__ = os.path.dirname(__dir__)
Expand Down
2 changes: 1 addition & 1 deletion lib/python/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ def draw_double_sided_exp(scale, origin=0.0):

def uniform(low,high):
"""Draw a random number from a uniform distribution between low and high"""
return random.uniform(low,high)
return 10 ** random.uniform(np.log10(low),np.log10(high))
28 changes: 15 additions & 13 deletions lib/python/dosurvey.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import math
import random

import cPickle

from population import Population
from pulsar import Pulsar
from survey import Survey

if sys.version_info[0] < 3:
import cPickle
else:
import pickle as cPickle

class Detections:
"""Just a simple object to store survey detection summary"""
Expand Down Expand Up @@ -91,16 +93,16 @@ def run(pop,

# print the population
if not nostdout:
print "Running doSurvey on population..."
print pop
print("Running doSurvey on population...")
print(pop)

# loop over the surveys we want to run on the pop file
surveyPops = []
for surv in surveyList:
s = Survey(surv)
s.discoveries = 0
if not nostdout:
print "\nRunning survey {0}".format(surv)
print("\nRunning survey {0}".format(surv))

# create a new population object to store discovered pulsars in
survpop = Population()
Expand Down Expand Up @@ -152,15 +154,15 @@ def run(pop,

# report the results
if not nostdout:
print "Total pulsars in model = {0}".format(len(pop.population))
print "Number detected by survey {0} = {1}".format(surv, ndet)
print "Of which are discoveries = {0}".format(s.discoveries)
print "Number too faint = {0}".format(ntf)
print "Number smeared = {0}".format(nsmear)
print "Number out = {0}".format(nout)
print("Total pulsars in model = {0}".format(len(pop.population)))
print("Number detected by survey {0} = {1}".format(surv, ndet))
print("Of which are discoveries = {0}".format(s.discoveries))
print("Number too faint = {0}".format(ntf))
print("Number smeared = {0}".format(nsmear))
print("Number out = {0}".format(nout))
if rratssearch:
print "Number didn't burst = {0}".format(nbr)
print "\n"
print("Number didn't burst = {0}".format(nbr))
print("\n")

d = Detections(ndet=ndet,
ntf=ntf,
Expand Down
61 changes: 32 additions & 29 deletions lib/python/evolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import random

import inspect
import cPickle
#import scipy.integrate
from scipy import integrate
import numpy as np
Expand All @@ -22,6 +21,10 @@

from progressbar import ProgressBar

if sys.version_info[0] < 3:
import cPickle
else:
import pickle as cPickle

class EvolveException(Exception):
pass
Expand Down Expand Up @@ -95,30 +98,30 @@ def generate(ngen,
pop.zscale = zscale

if widthModel == 'kj07':
print "\tLoading KJ07 models...."
print("\tLoading KJ07 models....")
kj_p_vals, kj_pdot_vals, kj_dists = beammodels.load_kj2007_models()
print "\tDone\n"
print("\tDone\n")

if not nostdout:
print "\tGenerating evolved pulsars with parameters:"
print "\t\tngen = {0}".format(ngen)
print "\t\tUsing electron distn model {0}".format(
pop.electronModel)
print "\n\t\tPeriod mean, sigma = {0}, {1}".format(
print("\tGenerating evolved pulsars with parameters:")
print("\t\tngen = {0}".format(ngen))
print("\t\tUsing electron distn model {0}".format(
pop.electronModel))
print("\n\t\tPeriod mean, sigma = {0}, {1}".format(
pop.pmean,
pop.psigma)
print "\t\tLuminosity mean, sigma = {0}, {1}".format(
pop.psigma))
print("\t\tLuminosity mean, sigma = {0}, {1}".format(
pop.lummean,
pop.lumsigma)
print "\t\tSpectral index mean, sigma = {0}, {1}".format(
pop.lumsigma))
print("\t\tSpectral index mean, sigma = {0}, {1}".format(
pop.simean,
pop.sisigma)
print "\t\tGalactic z scale height = {0} kpc".format(
pop.zscale)
pop.sisigma))
print("\t\tGalactic z scale height = {0} kpc".format(
pop.zscale))
if widthModel is None:
print "\t\tWidth {0}% ".format(duty)
print("\t\tWidth {0}% ".format(duty))
else:
print "\t\tUsing Karastergiou & Johnston beam width model"
print("\t\tUsing Karastergiou & Johnston beam width model")

# set up progress bar for fun :)
prog = ProgressBar(min_value=0,
Expand Down Expand Up @@ -228,7 +231,7 @@ def generate(ngen,
"""

else:
print "Undefined width model!"
print("Undefined width model!")
sys.exit()
# print width
# print pulsar.period, width, pulsar.pdot
Expand Down Expand Up @@ -338,7 +341,7 @@ def generate(ngen,
# update the counter
if not nostdout:
prog.increment_amount()
print prog, '\r',
print(prog, '\r',)
sys.stdout.flush()

else:
Expand All @@ -348,7 +351,7 @@ def generate(ngen,
# update the counter
if not nostdout:
prog.increment_amount()
print prog, '\r',
print(prog, '\r',)
sys.stdout.flush()

# pulsar isn't dead, add to population!
Expand All @@ -362,20 +365,20 @@ def generate(ngen,
# update the counter
if not nostdout:
prog.increment_amount()
print prog, '\r',
print(prog, '\r',)
sys.stdout.flush()

if not nostdout:
print "\n\n"
print " Total pulsars = {0}".format(len(pop.population))
print " Total detected = {0}".format(pop.ndet)
print("\n\n")
print(" Total pulsars = {0}".format(len(pop.population)))
print(" Total detected = {0}".format(pop.ndet))

for surv in surveys:
print "\n Results for survey '{0}'".format(surv.surveyName)
print " Number detected = {0}".format(surv.ndet)
print " Number too faint = {0}".format(surv.ntf)
print " Number smeared = {0}".format(surv.nsmear)
print " Number outside survey area = {0}".format(surv.nout)
print("\n Results for survey '{0}'".format(surv.surveyName))
print(" Number detected = {0}".format(surv.ndet))
print(" Number too faint = {0}".format(surv.ntf))
print(" Number smeared = {0}".format(surv.nsmear))
print(" Number outside survey area = {0}".format(surv.nout))

# save list of arguments into the pop
#try:
Expand Down
22 changes: 12 additions & 10 deletions lib/python/galacticops.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
# get the FORTRAN libraries
__dir__ = os.path.dirname(os.path.abspath(__file__))
__libdir__ = os.path.dirname(__dir__)
fortranpath = os.path.join(__libdir__, 'fortran')
Cpath = os.path.join(__libdir__, 'C')
fortranpath = os.path.join(__libdir__, 'fortran').encode()
Cpath = os.path.join(__libdir__, 'C').encode()

ne2001lib = C.CDLL(os.path.join(fortranpath, 'libne2001.so'))
ne2001lib = C.CDLL(os.path.join(fortranpath, 'libne2001.so'.encode()))
ne2001lib.dm_.restype = C.c_float

slalib = C.CDLL(os.path.join(fortranpath, 'libsla.so'))
vxyzlib = C.CDLL(os.path.join(fortranpath, 'libvxyz.so'))
slalib = C.CDLL(os.path.join(fortranpath, 'libsla.so'.encode()))
vxyzlib = C.CDLL(os.path.join(fortranpath, 'libvxyz.so'.encode()))

yklib = C.CDLL(os.path.join(fortranpath, 'libykarea.so'))
yklib = C.CDLL(os.path.join(fortranpath, 'libykarea.so'.encode()))
yklib.ykr_.restype = C.c_float
yklib.llfr_.restype = C.c_float

Expand Down Expand Up @@ -63,8 +63,9 @@ def vxyz(pulsar):
pulsar.vz = vz.value


def calc_dtrue((x, y, z)):
"""Calculate true distance to pulsar from the sun."""
def calc_dtrue(cart_pos):
"""Calculate true distance to pulsar from the sun. ip cart_pos should be tuple with (x,y,z)"""
x, y, z = cart_pos
rsun = 8.5 # kpc
return math.sqrt(x*x + (y-rsun)*(y-rsun) + z*z)

Expand Down Expand Up @@ -209,8 +210,9 @@ def radec_to_lb(ra, dec):
return l.value, b.value


def xyz_to_lb((x, y, z)):
def xyz_to_lb(cart_pos):
""" Convert galactic xyz in kpc to l and b in degrees."""
x, y, z = cart_pos
rsun = 8.5 # kpc

# distance to pulsar
Expand Down Expand Up @@ -382,7 +384,7 @@ def _double_sided_exp(scale, origin=0.0):
def readtskyfile():
"""Read in tsky.ascii into a list from which temps can be retrieved"""

tskypath = os.path.join(fortranpath, 'lookuptables/tsky.ascii')
tskypath = os.path.join(fortranpath, 'lookuptables/tsky.ascii'.encode())
tskylist = []
with open(tskypath) as f:
for line in f:
Expand Down
4 changes: 2 additions & 2 deletions lib/python/orbitalparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ def test_1802_2124(pulsar):
pulsar.inclination_degrees = 78.52
pulsar.pulsar_mass_msolar = 1.24

print pulsar.gb, pulsar.gl
print(pulsar.gb, pulsar.gl)
pulsar.gb = 0.61
pulsar.gl = 4.38
print pulsar.gb, pulsar.gl
print(pulsar.gb, pulsar.gl)
pulsar.galcoords = (0.49, 5.2, 0.04)
pulsar.lum_1400 = 8.54
pulsar.t_scatter = 0.0
Expand Down
Loading