def test_fitselector(): # WB par = os.path.join(datadir, "B1855+09_NANOGrav_12yv3.wb.gls.par") tim = os.path.join(datadir, "B1855+09_NANOGrav_12yv3.wb.tim") m, t = get_model_and_toas(par, tim) f = fitter.Fitter.auto(t, m, downhill=True) assert isinstance(f, fitter.WidebandDownhillFitter) f = fitter.Fitter.auto(t, m, downhill=False) assert isinstance(f, fitter.WidebandTOAFitter) # correlated errors m, t = get_model_and_toas( os.path.join(datadir, "J0023+0923_NANOGrav_11yv0.gls.par"), os.path.join(datadir, "J0023+0923_NANOGrav_11yv0.tim"), ) f = fitter.Fitter.auto(t, m, downhill=True) assert isinstance(f, fitter.DownhillGLSFitter) f = fitter.Fitter.auto(t, m, downhill=False) assert isinstance(f, fitter.GLSFitter) # uncorrelated errors m, t = get_model_and_toas(os.path.join(datadir, "NGC6440E.par"), os.path.join(datadir, "NGC6440E.tim")) f = fitter.Fitter.auto(t, m, downhill=True) assert isinstance(f, fitter.DownhillWLSFitter) f = fitter.Fitter.auto(t, m, downhill=False) assert isinstance(f, fitter.WLSFitter)
def test_random_models(fitter): # Get model and TOAs m, t = get_model_and_toas(os.path.join(datadir, "NGC6440E.par"), os.path.join(datadir, "NGC6440E.tim")) f = fitter(toas=t, model=m) # Do a 4-parameter fit f.model.free_params = ("F0", "F1", "RAJ", "DECJ") f.fit_toas() # this contains TOAs up through 54200 # make new ones starting there tnew = simulation.make_fake_toas_uniform(54200, 59000, 59000 - 54200, f.model) dphase, mrand = simulation.calculate_random_models(f, tnew, Nmodels=30) # this is a bit stochastic, but I see typically < 0.14 cycles # for the uncertainty at 59000 assert np.all(dphase.std(axis=0) < 0.2) # redo it with only F0 free dphase_F, mrand_F = simulation.calculate_random_models(f, tnew, Nmodels=100, params=["F0"]) # this should be less than the fully free version assert dphase_F.std(axis=0).max() < dphase.std(axis=0).max()
def test_CHI2(): """Should be present after fit in PINT, not in TEMPO/TEMPO2""" m, t = get_model_and_toas(os.path.join(datadir, "NGC6440E.par"), os.path.join(datadir, "NGC6440E.tim")) f = fitter.WLSFitter(toas=t, model=m) assert "CHI2" in f.model.as_parfile() assert not ("CHI2" in f.model.as_parfile(format="tempo2")) assert not ("CHI2" in f.model.as_parfile(format="tempo"))
def test_fitsummary_binary(): """Test fitter print_summary() when an ELL1 binary is fit""" par = os.path.join(datadir, "B1855+09_NANOGrav_12yv3.wb.gls.par") tim = os.path.join(datadir, "B1855+09_NANOGrav_dfg+12.tim") m, t = get_model_and_toas(par, tim) f = fitter.WLSFitter(t, m) f.model.free_params = ["PB", "A1", "SINI"] f.fit_toas() f.print_summary()
def test_no_planet(self): """Test exception when incorrect par(tim) file given.""" with self.assertRaises(ValueError) as context: model, toas = get_model_and_toas( datadir + "/J0030+0451_NANOGrav_9yv1.gls.par", datadir + "/J0030+0451_NANOGrav_9yv1.tim", planets=False) Pulsar(model, toas, planets=True) msg = "obs_earth_pos is not in toas.table.colnames. Either " msg += "`planet` flag is not True in `toas` or further Pint " msg += "development to add additional planets is needed." self.assertTrue(msg in context.exception)
def Pulsar(*args, **kwargs): ephem = kwargs.get("ephem", None) clk = kwargs.get("clk", None) bipm_version = kwargs.get("bipm_version", None) planets = kwargs.get("planets", True) sort = kwargs.get("sort", True) drop_t2pulsar = kwargs.get("drop_t2pulsar", True) drop_pintpsr = kwargs.get("drop_pintpsr", True) timing_package = kwargs.get("timing_package", "tempo2") if pint is not None: toas = [x for x in args if isinstance(x, TOAs)] model = [x for x in args if isinstance(x, TimingModel)] if t2 is not None: t2pulsar = [x for x in args if isinstance(x, t2.tempopulsar)] parfile = [ x for x in args if isinstance(x, str) and x.split(".")[-1] == "par" ] timfile = [ x for x in args if isinstance(x, str) and x.split(".")[-1] in ["tim", "toa"] ] if pint and toas and model: return PintPulsar(toas[0], model[0], sort=sort, drop_pintpsr=drop_pintpsr, planets=planets) elif t2 and t2pulsar: return Tempo2Pulsar(t2pulsar[0], sort=sort, drop_t2pulsar=drop_t2pulsar, planets=planets) elif parfile and timfile: # Check whether the two files exist if not os.path.isfile(parfile[0]) or not os.path.isfile(timfile[0]): msg = "Cannot find parfile {0} or timfile {1}!".format( parfile[0], timfile[0]) raise IOError(msg) # Obtain the directory name of the timfile, and change to it timfiletup = os.path.split(timfile[0]) dirname = timfiletup[0] or "./" reltimfile = timfiletup[-1] relparfile = os.path.relpath(parfile[0], dirname) # get current directory cwd = os.getcwd() # Change directory to the base directory of the tim-file to deal with # INCLUDE statements in the tim-file os.chdir(dirname) if timing_package.lower() == "pint": if (clk is not None) and (bipm_version is None): bipm_version = clk.split("(")[1][:-1] model, toas = get_model_and_toas(relparfile, reltimfile, ephem=ephem, bipm_version=bipm_version, planets=planets) os.chdir(cwd) return PintPulsar(toas, model, sort=sort, drop_pintpsr=drop_pintpsr, planets=planets) elif timing_package.lower() == "tempo2": # hack to set maxobs maxobs = get_maxobs(reltimfile) + 100 t2pulsar = t2.tempopulsar(relparfile, reltimfile, maxobs=maxobs, ephem=ephem, clk=clk) os.chdir(cwd) return Tempo2Pulsar(t2pulsar, sort=sort, drop_t2pulsar=drop_t2pulsar, planets=planets) raise ValueError("Unknown arguments {}".format(args))
import pint.fitter import pint.residuals import pint.toa from pint.models import get_model, get_model_and_toas # %% import pint.config parfile = pint.config.examplefile("NGC6440E.par") timfile = pint.config.examplefile("NGC6440E.tim") assert os.path.exists(parfile) assert os.path.exists(timfile) # %% # Read the timing model and the TOAs m, t = get_model_and_toas(parfile, timfile) # %% [markdown] # If we wanted to do things separately we could do # ```python # # Define the timing model # m = get_model(parfile) # # Read in the TOAs, using the solar system epemeris and other things from the model # t = pint.toa.get_TOAs(timfile, model=m) # ``` # %% [markdown] # If we wanted to select some subset of the TOAs, there are tools to do that. Most easily # you make a new TOAs object containing the subset you care about (we will make but not use # them):
def test_bipm_default(): m, t = get_model_and_toas(StringIO(simplepar.replace("BIPM2017", "BIPM")), "test1.tim") assert t.clock_corr_info["bipm_version"] == "BIPM"
def test_model_override_override2(): parstr = StringIO(simplepar) m, y = get_model_and_toas(parstr, "test1.tim", ephem="DE405") assert y.ephem == "DE405"
def test_model_override2(): parstr = StringIO(simplepar) m, y = get_model_and_toas(parstr, "test1.tim") assert y.ephem == "DE436" assert y.planets is True assert y.clock_corr_info["bipm_version"] == "BIPM2017"
import astropy.units as u import matplotlib.pyplot as plt from astropy.visualization import quantity_support from pint.fitter import WidebandTOAFitter from pint.models import get_model_and_toas from pint.toa import get_TOAs quantity_support() # %% [markdown] # ## Set up your inputs # %% model, toas = get_model_and_toas("J1614-2230_NANOGrav_12yv3.wb.gls.par", "J1614-2230_NANOGrav_12yv3.wb.tim") # %% [markdown] # The DM and its uncertainty are recorded as flags, `pp_dm` and `pp_dme` on the TOAs that have them, They are not currently available as Columns in the Astropy object. On the other hand, it is not necessary that every observation have a measured DM. # # (The name, `pp_dm`, refers to the fact that they are obtained using "phase portraits", like profiles but in one more dimension.) # %% print(open(toas.filename).readlines()[-1]) # %% toas.table[-1] # %% toas.table["flags"][0]
import matplotlib.pyplot as plt from astropy.visualization import quantity_support from pint.fitter import WidebandTOAFitter from pint.models import get_model_and_toas from pint.toa import get_TOAs import pint.config quantity_support() # %% [markdown] # ## Set up your inputs # %% model, toas = get_model_and_toas( pint.config.examplefile("J1614-2230_NANOGrav_12yv3.wb.gls.par"), pint.config.examplefile("J1614-2230_NANOGrav_12yv3.wb.tim"), ) # %% [markdown] # The DM and its uncertainty are recorded as flags, `pp_dm` and `pp_dme` on the TOAs that have them, They are not currently available as Columns in the Astropy object. On the other hand, it is not necessary that every observation have a measured DM. # # (The name, `pp_dm`, refers to the fact that they are obtained using "phase portraits", like profiles but in one more dimension.) # %% print(open(toas.filename).readlines()[-1]) # %% toas.table[-1] # %% toas.table["flags"][0]