From 6dd1f6a9398bfeb0b55f2ca828fff55076545ebc Mon Sep 17 00:00:00 2001 From: NihanPol Date: Tue, 4 Feb 2020 15:09:28 -0500 Subject: [PATCH 01/23] The following changes made: 1. Modify pulsar.py to add orbital degradation factor as an attribute to each pulsar object (orb_degfac). 2. Modify survey.py to record the snr for each pulsar object in its pulsar.snr attribute. --- lib/python/pulsar.py | 4 ++++ lib/python/survey.py | 3 +++ 2 files changed, 7 insertions(+) diff --git a/lib/python/pulsar.py b/lib/python/pulsar.py index 6c0b4f3..8a8a375 100644 --- a/lib/python/pulsar.py +++ b/lib/python/pulsar.py @@ -37,6 +37,7 @@ def __init__(self, brokenSI=None, br=None, det_nos=None, + orb_degfac = 1.0 #Add in orbital degradation factor for each pulsar *args, **kwargs): """___init___ function for the Pulsar class""" @@ -90,6 +91,9 @@ def __init__(self, # need to add pulsar dead/alive for evolution code self.dead = False + + #Set the orbital degradation factor: + self.orb_degfac = orb_degfac # methods to calculate derived properties def s_1400(self): diff --git a/lib/python/survey.py b/lib/python/survey.py index 5f3a634..a2c60f3 100644 --- a/lib/python/survey.py +++ b/lib/python/survey.py @@ -524,7 +524,10 @@ def SNRcalc(self, 4) print "gamma harm4 = ", gamma + #Save the S/N to the pulsar object + pulsar.snr = sig_to_noise * degfac # return the S/N accounting for beam offset + return sig_to_noise * degfac def _AA_factor(self, pulsar): From 18efd05a6d4a9b1e1c5512d627a17c6f81732b82 Mon Sep 17 00:00:00 2001 From: NihanPol Date: Tue, 4 Feb 2020 15:20:46 -0500 Subject: [PATCH 02/23] Add gitignore and fix typo --- .gitignore | 2 ++ lib/fortran/make_linux.csh | 2 +- lib/python/pulsar.py | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4cd9921 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.so +*.pyc diff --git a/lib/fortran/make_linux.csh b/lib/fortran/make_linux.csh index 3b116d8..4bbb8b4 100644 --- a/lib/fortran/make_linux.csh +++ b/lib/fortran/make_linux.csh @@ -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 diff --git a/lib/python/pulsar.py b/lib/python/pulsar.py index 8a8a375..121db7f 100644 --- a/lib/python/pulsar.py +++ b/lib/python/pulsar.py @@ -37,7 +37,7 @@ def __init__(self, brokenSI=None, br=None, det_nos=None, - orb_degfac = 1.0 #Add in orbital degradation factor for each pulsar + orb_degfac = 1.0, #Add in orbital degradation factor for each pulsar *args, **kwargs): """___init___ function for the Pulsar class""" From b215675128168de922be64d93f12a1deafe394c3 Mon Sep 17 00:00:00 2001 From: NihanPol Date: Tue, 4 Feb 2020 15:44:17 -0500 Subject: [PATCH 03/23] fix incompatibility in makefile --- .gitignore | 3 +++ lib/fortran/makefile | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4cd9921..f1d9038 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ *.so *.pyc +*.o +*.model +*.cmd diff --git a/lib/fortran/makefile b/lib/fortran/makefile index f966a3f..4bb1a83 100644 --- a/lib/fortran/makefile +++ b/lib/fortran/makefile @@ -1,4 +1,4 @@ -include makefile.$(OSTYPE) +include makefile.linux all: libne2001.so libykarea.so libsla.so libvxyz.so libgamma.so From a59b31a119f4c29af0adfe6eead99d96ed27e6a2 Mon Sep 17 00:00:00 2001 From: NihanPol Date: Tue, 4 Feb 2020 15:51:56 -0500 Subject: [PATCH 04/23] ensure pickle import compatibility between py2 and py3 --- lib/python/dataobj.py | 8 +++++--- lib/python/degradation.py | 8 ++++++-- lib/python/dosurvey.py | 6 ++++-- lib/python/evolve.py | 5 ++++- lib/python/populate.py | 6 +++++- lib/python/population.py | 7 +++++-- 6 files changed, 29 insertions(+), 11 deletions(-) diff --git a/lib/python/dataobj.py b/lib/python/dataobj.py index ee8684e..c9ed9ca 100644 --- a/lib/python/dataobj.py +++ b/lib/python/dataobj.py @@ -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): diff --git a/lib/python/degradation.py b/lib/python/degradation.py index 7a264bf..947d6e3 100644 --- a/lib/python/degradation.py +++ b/lib/python/degradation.py @@ -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__) diff --git a/lib/python/dosurvey.py b/lib/python/dosurvey.py index a414089..6386e84 100755 --- a/lib/python/dosurvey.py +++ b/lib/python/dosurvey.py @@ -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""" diff --git a/lib/python/evolve.py b/lib/python/evolve.py index 07843c7..c189fc2 100755 --- a/lib/python/evolve.py +++ b/lib/python/evolve.py @@ -6,7 +6,6 @@ import random import inspect -import cPickle #import scipy.integrate from scipy import integrate import numpy as np @@ -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 diff --git a/lib/python/populate.py b/lib/python/populate.py index 93184f0..274147b 100755 --- a/lib/python/populate.py +++ b/lib/python/populate.py @@ -6,7 +6,6 @@ import random import inspect -import cPickle import numpy as np import distributions as dists @@ -21,6 +20,11 @@ from progressbar import ProgressBar +if sys.version_info[0] < 3: + import cPickle +else: + import pickle as cPickle + class PopulateException(Exception): pass diff --git a/lib/python/population.py b/lib/python/population.py index 32d6eb6..8554388 100755 --- a/lib/python/population.py +++ b/lib/python/population.py @@ -1,10 +1,13 @@ #!/usr/bin/python -import copy -import cPickle +import copy, sys import numpy as np +if sys.version_info[0] < 3: + import cPickle +else: + import pickle as cPickle class Population: From 477412c6b0aeef37d4a8642f1a09d3ffc2cb144d Mon Sep 17 00:00:00 2001 From: NihanPol Date: Tue, 11 Feb 2020 13:47:11 -0500 Subject: [PATCH 05/23] add SNR of only detected pulsar to their instance --- .gitignore | 1 + lib/python/survey.py | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index f1d9038..d1fbd9e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ *.o *.model *.cmd +*ipynb* diff --git a/lib/python/survey.py b/lib/python/survey.py index a2c60f3..88df488 100644 --- a/lib/python/survey.py +++ b/lib/python/survey.py @@ -524,8 +524,6 @@ def SNRcalc(self, 4) print "gamma harm4 = ", gamma - #Save the S/N to the pulsar object - pulsar.snr = sig_to_noise * degfac # return the S/N accounting for beam offset return sig_to_noise * degfac From b194af9794cff0bc7cc69a082c672d9be4aacf6d Mon Sep 17 00:00:00 2001 From: NihanPol Date: Fri, 14 Feb 2020 13:21:12 -0500 Subject: [PATCH 06/23] Fix print statements for Py3.xx --- lib/python/populate.py | 48 +++++++++++++++++----------------- lib/python/progressbar.py | 14 +++++----- lib/python/survey.py | 20 +++++++------- lib/python/two_hist_plotter.py | 2 +- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/lib/python/populate.py b/lib/python/populate.py index 274147b..8bf08cb 100755 --- a/lib/python/populate.py +++ b/lib/python/populate.py @@ -80,38 +80,38 @@ def generate(ngen, # check that the distribution types are supported.... if 'd_g' in (pDistType,lumDistType,zscaleType,radialDistType,brDistType) and dgf is None: - print "Provide the distribution generation file" + print("Provide the distribution generation file") sys.exit() elif dgf != None: try: f = open(dgf, 'rb') except IOError: - print "Could not open file {0}.".format(dgf) + print("Could not open file {0}.".format(dgf)) sys.exit() dgf_pop_load = cPickle.load(f) f.close() if lumDistType not in ['lnorm', 'pow', 'log_unif', 'd_g', 'log_st']: - print "Unsupported luminosity distribution: {0}".format(lumDistType) + print("Unsupported luminosity distribution: {0}".format(lumDistType)) if brDistType not in ['log_unif', 'd_g']: - print "Unsupported burst rate distribution: {0}".format(brDistType) + print("Unsupported burst rate distribution: {0}".format(brDistType)) if pDistType not in ['lnorm', 'norm', 'cc97', 'lorimer12','unif', 'd_g']: - print "Unsupported period distribution: {0}".format(pDistType) + print("Unsupported period distribution: {0}".format(pDistType)) if radialDistType not in ['lfl06', 'yk04', 'isotropic', 'slab', 'disk','unif' ,'gauss','d_g', 'gamma']: - print "Unsupported radial distribution: {0}".format(radialDistType) + print("Unsupported radial distribution: {0}".format(radialDistType)) if electronModel not in ['ne2001', 'lmt85','ymw16']: - print "Unsupported electron model: {0}".format(electronModel) + print("Unsupported electron model: {0}".format(electronModel)) if pattern not in ['gaussian', 'airy']: - print "Unsupported gain pattern: {0}".format(pattern) + print("Unsupported gain pattern: {0}".format(pattern)) if duty_percent < 0.: - print "Unsupported value of duty cycle: {0}".format(duty_percent) + print("Unsupported value of duty cycle: {0}".format(duty_percent)) # need to use properties in this class so they're get/set-type props pop.pDistType = pDistType @@ -159,7 +159,7 @@ def generate(ngen, pass if not nostdout: - print "\tGenerating pulsars with parameters:" + print("\tGenerating pulsars with parameters:") param_string_list = [] for key, value in key_values: s = ": ".join([key, str(value)]) @@ -167,7 +167,7 @@ def generate(ngen, # join this list of strings, and print it s = "\n\t\t".join(param_string_list) - print "\t\t{0}".format(s) + print("\t\t{0}".format(s)) # set up progress bar for fun :) prog = ProgressBar(min_value=0, @@ -207,7 +207,7 @@ def generate(ngen, elif pop.pDistType == 'cc97': p.period = _cc97() elif pop.pDistType == 'gamma': - print "Gamma function not yet supported" + print("Gamma function not yet supported") sys.exit() elif pop.pDistType == 'd_g': Pbin_num=dists.draw1d(dgf_pop_load['pHist']) @@ -370,7 +370,7 @@ def generate(ngen, # add in orbital parameters if orbits: orbitalparams.test_1802_2124(p) - print p.gb, p.gl + print(p.gb, p.gl) #dither the distance if dither: @@ -419,7 +419,7 @@ def generate(ngen, pop.ndet += 1 if not nostdout: prog.increment_amount() - print prog, '\r', + print(prog, '\r',) sys.stdout.flush() # if surveys are given, check if pulsar detected or not # in ANY of the surveys @@ -478,24 +478,24 @@ def generate(ngen, pop.ndet += 1 if not nostdout: prog.increment_amount() - print prog, '\r', + print(prog, '\r',) sys.stdout.flush() # print info to stdout if not nostdout: - print "\n" - print " Total pulsars = {0}".format(len(pop.population)) - print " Total detected = {0}".format(pop.ndet) + print("\n") + print(" Total pulsars = {0}".format(len(pop.population))) + print(" Total detected = {0}".format(pop.ndet)) # print " Number not beaming = {0}".format(surv.nnb) 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)) if singlepulse: - print " Number didn't burst = {0}".format(surv.nbr) + print(" Number didn't burst = {0}".format(surv.nbr)) return pop diff --git a/lib/python/progressbar.py b/lib/python/progressbar.py index c5dc54c..2755336 100644 --- a/lib/python/progressbar.py +++ b/lib/python/progressbar.py @@ -95,29 +95,29 @@ def __str__(self): def main(): - print + print() limit = 1000000 - print 'Example 1: Fixed Bar' + print('Example 1: Fixed Bar') prog = ProgressBar(0, limit, 77, mode='fixed') oldprog = str(prog) for i in xrange(limit+1): prog.update_amount(i) if oldprog != str(prog): - print prog, "\r", + print(prog, "\r",) sys.stdout.flush() oldprog = str(prog) - print '\n\n' + print('\n\n') - print 'Example 2: Dynamic Bar' + print('Example 2: Dynamic Bar') prog = ProgressBar(0, limit, 77, mode='dynamic', char='-') oldprog = str(prog) for i in xrange(limit+1): prog.increment_amount() if oldprog != str(prog): - print prog, "\r", + print(prog, "\r",) sys.stdout.flush() oldprog = str(prog) - print '\n\n' + print('\n\n') diff --git a/lib/python/survey.py b/lib/python/survey.py index 88df488..e4a1145 100644 --- a/lib/python/survey.py +++ b/lib/python/survey.py @@ -273,7 +273,7 @@ def __init__(self, surveyName, pattern='gaussian'): # turn on AA self.AA = True else: - print "Parameter '", a[1].strip(), "' not recognized!" + print("Parameter '", a[1].strip(), "' not recognized!") f.close() @@ -385,8 +385,8 @@ def SNRcalc(self, # (loops over the list of pointings....) if rratssearch: if pulsar.br is None: - print "Population doesn't have a burst rate" - print "Use populate with --singlepulse" + print("Population doesn't have a burst rate") + print("Use populate with --singlepulse") sys.exit() pulsar.pop_time=np.random.poisson(pulsar.br*self.tobs) @@ -492,37 +492,37 @@ def SNRcalc(self, if pulsar.is_binary: # print "the pulsar is a binary!" if jerksearch: - print "jerk" + #print "jerk" gamma = degradation.gamma3(pulsar, self.tobs, 1) elif accelsearch: - print "accel" + #print "accel" gamma = degaadation.gamma2(pulsar, self.tobs, 1) else: - print "norm" + #print "norm" gamma = degradation.gamma1(pulsar, self.tobs, 1) - print "gamma harm1 = ", gamma + #print "gamma harm1 = ", gamma gamma = degradation.gamma1(pulsar, self.tobs, 2) - print "gamma harm2 = ", gamma + #print "gamma harm2 = ", gamma gamma = degradation.gamma1(pulsar, self.tobs, 3) - print "gamma harm3 = ", gamma + #print "gamma harm3 = ", gamma gamma = degradation.gamma1(pulsar, self.tobs, 4) - print "gamma harm4 = ", gamma + #print "gamma harm4 = ", gamma # return the S/N accounting for beam offset diff --git a/lib/python/two_hist_plotter.py b/lib/python/two_hist_plotter.py index ffe8e1b..e33ff50 100644 --- a/lib/python/two_hist_plotter.py +++ b/lib/python/two_hist_plotter.py @@ -33,7 +33,7 @@ def plot_these(x,y,fit=False,func=None,p0=None,label1=None,label2=None,show=True #print injeted mu, recovered mu, err mu and sigma if print_res is True: - print p0[1],par3[1],err3[1],p0[2],par3[2],err3[2] + print(p0[1],par3[1],err3[1],p0[2],par3[2],err3[2]) except RuntimeError: pass plt.xlabel("Luminosity") From f776e639741534e16709ccb5f3512719ae27fad0 Mon Sep 17 00:00:00 2001 From: NihanPol Date: Fri, 14 Feb 2020 13:29:56 -0500 Subject: [PATCH 07/23] Fix function definition (line 66; def calc_true) in galacticops.py --- lib/python/galacticops.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/python/galacticops.py b/lib/python/galacticops.py index 5035c32..2f1f428 100755 --- a/lib/python/galacticops.py +++ b/lib/python/galacticops.py @@ -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) From 29526f6ed23e1a016b2c963548c9ab58458261dd Mon Sep 17 00:00:00 2001 From: NihanPol Date: Fri, 14 Feb 2020 13:36:14 -0500 Subject: [PATCH 08/23] Fix another function definition in galacticops.py (line 213) --- lib/python/galacticops.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/python/galacticops.py b/lib/python/galacticops.py index 2f1f428..1e07a3e 100755 --- a/lib/python/galacticops.py +++ b/lib/python/galacticops.py @@ -210,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 From cee53e353c103577b074a40cab1e84addf5f7443 Mon Sep 17 00:00:00 2001 From: NihanPol Date: Fri, 14 Feb 2020 13:37:33 -0500 Subject: [PATCH 09/23] fix print statement in orbitalparams.py --- lib/python/orbitalparams.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/python/orbitalparams.py b/lib/python/orbitalparams.py index 70210b1..13952b9 100644 --- a/lib/python/orbitalparams.py +++ b/lib/python/orbitalparams.py @@ -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 From e49a0074299a4bdd28d653f9c6a7f93fb9650e84 Mon Sep 17 00:00:00 2001 From: NihanPol Date: Fri, 14 Feb 2020 13:39:34 -0500 Subject: [PATCH 10/23] fix print statement in dosurvey.py --- lib/python/dosurvey.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/python/dosurvey.py b/lib/python/dosurvey.py index 6386e84..e83c91e 100755 --- a/lib/python/dosurvey.py +++ b/lib/python/dosurvey.py @@ -93,8 +93,8 @@ 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 = [] @@ -102,7 +102,7 @@ def run(pop, 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() @@ -154,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, From a481d00b8cabae14f7b868acdc3d15acf814932d Mon Sep 17 00:00:00 2001 From: NihanPol Date: Fri, 14 Feb 2020 14:53:46 -0500 Subject: [PATCH 11/23] fix compatibility issue with inspect.getargspec at line 156 in populate.py --- lib/python/populate.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/python/populate.py b/lib/python/populate.py index 8bf08cb..672a151 100755 --- a/lib/python/populate.py +++ b/lib/python/populate.py @@ -152,8 +152,10 @@ def generate(ngen, # store the dict of arguments inside the model. Could be useful. try: - argspec = inspect.getargspec(generate) - key_values = [(arg, locals()[arg]) for arg in argspec.args] + argspec = inspect.getfullargspec(generate) + lcl = locals() + key_values = [(arg, lcl[arg]) for arg in argspec.args] + #key_values = [(arg, locals()['argspec'][arg]) for arg in argspec.args] #pop.arguments = {key: value for (key, value) in key_values} except SyntaxError: pass From b389783e35791a9456076b7b22a900693af78f57 Mon Sep 17 00:00:00 2001 From: NihanPol Date: Fri, 14 Feb 2020 14:57:35 -0500 Subject: [PATCH 12/23] make change to inspect.getargspec compatible with both py2.xx and py3.xx --- lib/python/populate.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/python/populate.py b/lib/python/populate.py index 672a151..c4bc783 100755 --- a/lib/python/populate.py +++ b/lib/python/populate.py @@ -152,7 +152,10 @@ def generate(ngen, # store the dict of arguments inside the model. Could be useful. try: - argspec = inspect.getfullargspec(generate) + if sys.version_info[0] > 3: + argspec = inspect.getfullargspec(generate) + else: + argspec = inspect.getargspec(generate) lcl = locals() key_values = [(arg, lcl[arg]) for arg in argspec.args] #key_values = [(arg, locals()['argspec'][arg]) for arg in argspec.args] From 3aa8c3689ea6e462b3bc970033c1fc041f1cf0b4 Mon Sep 17 00:00:00 2001 From: NihanPol Date: Fri, 14 Feb 2020 15:30:19 -0500 Subject: [PATCH 13/23] Fix instances where comparison with None fails in Py3.xx in populate.py and pulsar.py --- lib/python/populate.py | 4 ++-- lib/python/pulsar.py | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/python/populate.py b/lib/python/populate.py index c4bc783..8fecf02 100755 --- a/lib/python/populate.py +++ b/lib/python/populate.py @@ -43,8 +43,8 @@ def generate(ngen, zscale=0.33, duty_percent=6., scindex=-3.86, - gpsArgs=[None, None], - doubleSpec=[None, None], + gpsArgs=[-1, None], + doubleSpec=[-1, None], nostdout=False, pattern='gaussian', orbits=False, diff --git a/lib/python/pulsar.py b/lib/python/pulsar.py index 121db7f..bfca565 100644 --- a/lib/python/pulsar.py +++ b/lib/python/pulsar.py @@ -50,8 +50,9 @@ def __init__(self, self.dm = dm # convert to -180->+180 range - if gl > 180.: - gl -= 360. + if gl: + if gl > 180.: + gl -= 360. self.gl = gl self.gb = gb From 69b85c7832f2bc39cd87fb1c877484b43d29d902 Mon Sep 17 00:00:00 2001 From: NihanPol Date: Fri, 14 Feb 2020 15:51:43 -0500 Subject: [PATCH 14/23] Explicitly make all initial strings as byte strings since this is not done implicitly in Py3.xx --- lib/python/galacticops.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/python/galacticops.py b/lib/python/galacticops.py index 1e07a3e..e138d55 100755 --- a/lib/python/galacticops.py +++ b/lib/python/galacticops.py @@ -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 = bytes(os.path.join(__libdir__, 'fortran').encode("ascii")) +Cpath = bytes(os.path.join(__libdir__, 'C').encode("ascii")) -ne2001lib = C.CDLL(os.path.join(fortranpath, 'libne2001.so')) +ne2001lib = C.CDLL(os.path.join(fortranpath, bytes('libne2001.so'.encode("ascii")))) 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, bytes('libsla.so'.encode("ascii")))) +vxyzlib = C.CDLL(os.path.join(fortranpath, bytes('libvxyz.so'.encode("ascii")))) -yklib = C.CDLL(os.path.join(fortranpath, 'libykarea.so')) +yklib = C.CDLL(os.path.join(fortranpath, bytes('libykarea.so'.encode("ascii")))) yklib.ykr_.restype = C.c_float yklib.llfr_.restype = C.c_float From 899119c222f042def1b962e8044c5796f6479870 Mon Sep 17 00:00:00 2001 From: NihanPol Date: Fri, 14 Feb 2020 15:56:13 -0500 Subject: [PATCH 15/23] Fix another instance of explicitly setting strings to dtype=bytes since Py3.xx does not do this implicitly --- lib/python/galacticops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/python/galacticops.py b/lib/python/galacticops.py index e138d55..ce286cf 100755 --- a/lib/python/galacticops.py +++ b/lib/python/galacticops.py @@ -384,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, bytes('lookuptables/tsky.ascii'.encode("ascii"))) tskylist = [] with open(tskypath) as f: for line in f: From 2b6f17265d0958a472edc2e48bfcc1dbff20cc45 Mon Sep 17 00:00:00 2001 From: NihanPol Date: Fri, 14 Feb 2020 16:02:31 -0500 Subject: [PATCH 16/23] Fix print statememts in evolve.py --- lib/python/evolve.py | 56 ++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/lib/python/evolve.py b/lib/python/evolve.py index c189fc2..d6f9842 100755 --- a/lib/python/evolve.py +++ b/lib/python/evolve.py @@ -98,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, @@ -231,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 @@ -341,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: @@ -351,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! @@ -365,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: From 898251192d961447203ea7796dcad885dadd39df Mon Sep 17 00:00:00 2001 From: NihanPol Date: Fri, 14 Feb 2020 16:11:27 -0500 Subject: [PATCH 17/23] fix the unicode string --> byte string issue in a slightly different, better way --- lib/python/galacticops.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/python/galacticops.py b/lib/python/galacticops.py index ce286cf..f9a2010 100755 --- a/lib/python/galacticops.py +++ b/lib/python/galacticops.py @@ -11,16 +11,16 @@ # get the FORTRAN libraries __dir__ = os.path.dirname(os.path.abspath(__file__)) __libdir__ = os.path.dirname(__dir__) -fortranpath = bytes(os.path.join(__libdir__, 'fortran').encode("ascii")) -Cpath = bytes(os.path.join(__libdir__, 'C').encode("ascii")) +fortranpath = os.path.join(__libdir__, 'fortran').encode() +Cpath = os.path.join(__libdir__, 'C').encode() -ne2001lib = C.CDLL(os.path.join(fortranpath, bytes('libne2001.so'.encode("ascii")))) +ne2001lib = C.CDLL(os.path.join(fortranpath, 'libne2001.so'.encode())) ne2001lib.dm_.restype = C.c_float -slalib = C.CDLL(os.path.join(fortranpath, bytes('libsla.so'.encode("ascii")))) -vxyzlib = C.CDLL(os.path.join(fortranpath, bytes('libvxyz.so'.encode("ascii")))) +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, bytes('libykarea.so'.encode("ascii")))) +yklib = C.CDLL(os.path.join(fortranpath, 'libykarea.so'.encode())) yklib.ykr_.restype = C.c_float yklib.llfr_.restype = C.c_float @@ -384,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, bytes('lookuptables/tsky.ascii'.encode("ascii"))) + tskypath = os.path.join(fortranpath, 'lookuptables/tsky.ascii'.encode()) tskylist = [] with open(tskypath) as f: for line in f: From 394537bbaa05268ac480fc680610575b200867df Mon Sep 17 00:00:00 2001 From: NihanPol Date: Tue, 18 Feb 2020 12:14:32 -0500 Subject: [PATCH 18/23] ensure log-space sampling for uniform sampling in distributions.py --- lib/python/distributions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/python/distributions.py b/lib/python/distributions.py index ccf16a8..a03ee47 100644 --- a/lib/python/distributions.py +++ b/lib/python/distributions.py @@ -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)) From 32567b7df10eec52f0b176b4cb21e02c3a940d83 Mon Sep 17 00:00:00 2001 From: NihanPol Date: Tue, 7 Apr 2020 13:25:11 -0400 Subject: [PATCH 19/23] Add orbital parameters to the Pulsar object. These include: m1 -> mass of psr m2 -> mass of companion om -> angle of periastron passage (omper) inc -> inclination of BNS system ec -> eccentricity of BNS pod -> orbital period of BNS m -> harmonic at which power is computed Consequently, added functionality for Population object to hold the ranges of the BNS orbital parameters. populate.generate() now draws uniformly from the range of orbital parameters supplied by the user (as a dictionary). --- lib/python/populate.py | 35 +++++++++++++++++++++++++++++++---- lib/python/population.py | 8 +++++++- lib/python/pulsar.py | 16 ++++++++++++++++ 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/lib/python/populate.py b/lib/python/populate.py index 8fecf02..7438567 100755 --- a/lib/python/populate.py +++ b/lib/python/populate.py @@ -54,6 +54,8 @@ def generate(ngen, accelsearch=False, jerksearch=False, sig_factor=10.0, + bns = False, + orbparams = {'m': [1, 5], 'm1': [1.0, 2.4], 'm2': [0.2, 1e9], 'om': [0, 360.], 'inc': [0, 90], 'ec': [0., 1.], 'pod': [1e-3, 1e3]}, brDistType='log_unif'): """ @@ -125,6 +127,10 @@ def generate(ngen, pop.gpsFrac, pop.gpsA = gpsArgs pop.brokenFrac, pop.brokenSI = doubleSpec + + #Set whether system is BNS: + pop.bns = bns + pop.orbparams = orbparams if pop.lumDistType == 'lnorm': pop.lummean, pop.lumsigma = \ @@ -371,11 +377,32 @@ def generate(ngen, logl = lmin + (lmax-lmin)*(lbin_num+random.random())/len(dgf_pop_load['lHist']) p.lum_1400 = 10.0**logl p.lum_inj_mu=p.lum_1400 - + # add in orbital parameters - if orbits: - orbitalparams.test_1802_2124(p) - print(p.gb, p.gl) + if pop.bns: + + assert isinstance(orbparams, dict), "Orbital parameter distribution limits should be a dictionary of form {'name of orb_param': [min, max]}" + + #These represents the range in which NN was trained. + #DO NOT GO OUTSIDE THESE BOUNDS!! + default_orbparams = {'m': [1, 5], 'm1': [1.0, 2.4], 'm2': [0.2, 1e9], 'om': [0, 360.], 'inc': [0, 90], 'ec': [0., 1.], 'pod': [1e-3, 1e3]} + + if len(pop.orbparams) == 0: + print("Warning: Supplied orbparams dict is empty; Setting ranges to default") + pop.orbparams = default_orbparams + else: + temp_opd = dict(default_orbparams) + temp_opd.update(orbparams) + pop.orbparams = temp_opd + + #Draw a value for each of the orbital parameters from a uniform distribution + p.m = np.int(np.random.uniform(pop.orbparams['m'][0], pop.orbparams['m'][1], size = 1)) #Should typically fix this to one value! + p.m1 = np.random.uniform(pop.orbparams['m1'][0], pop.orbparams['m1'][1], size = 1) + p.m2 = np.random.uniform(pop.orbparams['m2'][0], pop.orbparams['m2'][1], size = 1) + p.om = np.random.uniform(pop.orbparams['om'][0], pop.orbparams['om'][1], size = 1) + p.inc = np.random.uniform(pop.orbparams['inc'][0], pop.orbparams['inc'][1], size = 1) + p.ec = np.random.uniform(pop.orbparams['ec'][0], pop.orbparams['ec'][1], size = 1) + p.pod = np.random.uniform(pop.orbparams['pod'][0], pop.orbparams['pod'][1], size = 1) #dither the distance if dither: diff --git a/lib/python/population.py b/lib/python/population.py index 8554388..736d0dd 100755 --- a/lib/python/population.py +++ b/lib/python/population.py @@ -29,6 +29,8 @@ def __init__(self, gpsA=None, brokenFrac=None, brokenSI=None, + bns = False, + orbparams = {}, ref_freq=1400.0): """Initialise the population object.""" @@ -55,7 +57,11 @@ def __init__(self, self.zscale = zscale self.ref_freq = ref_freq - + + #Set whether we are looking at BNS systems: + self.bns = bns + self.orbparams = orbparams + # GPS and double SI values self.gpsFrac = gpsFrac self.gpsA = gpsA diff --git a/lib/python/pulsar.py b/lib/python/pulsar.py index bfca565..a230531 100644 --- a/lib/python/pulsar.py +++ b/lib/python/pulsar.py @@ -37,6 +37,13 @@ def __init__(self, brokenSI=None, br=None, det_nos=None, + m = 1, # "n"th harmonic; required for orb_degfac calculation + m1 = 1.4, #Mass of psr; required for orb_degfac calculation + m2 = 1.4, #Mass of companion; required for orb_degfac calculation + om = 90., #angle of periastron passage (omega_per); required for orb_degfac calculation + inc = 90., #inclincation of bns system; required for orb_degfac calculation + ec = 0.01, #eccentricity of bns system; required for orb_degfac calculation + pod = 0.1, #orbital period of bns system; required for orb_degfac calculation orb_degfac = 1.0, #Add in orbital degradation factor for each pulsar *args, **kwargs): @@ -93,6 +100,15 @@ def __init__(self, # need to add pulsar dead/alive for evolution code self.dead = False + #Set the orbital parameters: + self.m = m + self.m1 = m1 + self.m2 = m2 + self.om = om + self.inc = inc + self.ec = ec + self.pod = pod + #Set the orbital degradation factor: self.orb_degfac = orb_degfac From 609a3e23739eb92f52edf464c07692c59ca7ecb0 Mon Sep 17 00:00:00 2001 From: Nihan Pol <33101514+NihanPol@users.noreply.github.com> Date: Tue, 2 Jun 2020 10:33:08 -0400 Subject: [PATCH 20/23] fix gpsfrac == NoneType issue cannot compare float to NoneType in Python3. changed gpsargs to [-1, -1] from [-1, None] so that condition evaluates to true for default case. --- lib/python/populate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/python/populate.py b/lib/python/populate.py index 7438567..78cf666 100755 --- a/lib/python/populate.py +++ b/lib/python/populate.py @@ -43,7 +43,7 @@ def generate(ngen, zscale=0.33, duty_percent=6., scindex=-3.86, - gpsArgs=[-1, None], + gpsArgs=[-1, -1], doubleSpec=[-1, None], nostdout=False, pattern='gaussian', @@ -266,7 +266,7 @@ def generate(ngen, # AND double spectra. But for now I assume only have one or # none of these types. if random.random() > pop.gpsFrac: - # This will evaluate true when gpsArgs[0] is NoneType + # This will evaluate true when gpsArgs[0] is -1 # might have to change in future p.gpsFlag = 0 else: From 953e18d16b0698812ee1915d7fdc6e062813941e Mon Sep 17 00:00:00 2001 From: Nihan Pol <33101514+NihanPol@users.noreply.github.com> Date: Tue, 2 Jun 2020 10:36:16 -0400 Subject: [PATCH 21/23] change gpsfrac to -1 from None cannot compare floats to NoneType in py3. change gpsfrac to -1 so that default behavior is satisfied. --- lib/python/population.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/python/population.py b/lib/python/population.py index 736d0dd..0a3d80b 100755 --- a/lib/python/population.py +++ b/lib/python/population.py @@ -25,7 +25,7 @@ def __init__(self, lumsigma=None, zscale=None, electronModel=None, - gpsFrac=None, + gpsFrac=-1, gpsA=None, brokenFrac=None, brokenSI=None, From f8a328c05c4cfecc128332c6cf43088d7f0fbd72 Mon Sep 17 00:00:00 2001 From: NihanPol Date: Tue, 2 Jun 2020 10:39:47 -0400 Subject: [PATCH 22/23] Fix gpsfrac issue due to definition in __main__ --- lib/python/populate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/python/populate.py b/lib/python/populate.py index 78cf666..a7d3ac1 100755 --- a/lib/python/populate.py +++ b/lib/python/populate.py @@ -712,7 +712,7 @@ def _sindegree(angle): # GPS sources parser.add_argument('-gps', type=float, nargs=2, required=False, - default=[None, None], + default=[-1, -1], help='GPS fraction and "a" value') # double-spectral-index sources From e414296ffab85b02501d74268d1436fbdb999668 Mon Sep 17 00:00:00 2001 From: NihanPol Date: Tue, 2 Jun 2020 10:43:32 -0400 Subject: [PATCH 23/23] change doublespec from Nonetype to -1 to fix incompatibility in py3 --- lib/python/populate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/python/populate.py b/lib/python/populate.py index a7d3ac1..d4a4167 100755 --- a/lib/python/populate.py +++ b/lib/python/populate.py @@ -44,7 +44,7 @@ def generate(ngen, duty_percent=6., scindex=-3.86, gpsArgs=[-1, -1], - doubleSpec=[-1, None], + doubleSpec=[-1, -1], nostdout=False, pattern='gaussian', orbits=False, @@ -717,7 +717,7 @@ def _sindegree(angle): # double-spectral-index sources parser.add_argument('-doublespec', type=float, nargs=2, required=False, - default=[None, None], + default=[-1, -1], help='Dbl spec fraction and alpha value') # dist_gen_file parser.add_argument('-dgf', type=str,metavar='dist_gen_file', required= False,