def test_set_params(lines, param, value): model = get_model(io.StringIO("\n".join([par_base] + lines))) with pytest.raises(KeyError) as e: model[param].value = value str(e.value).index(param) # raise exception if not found with pytest.raises(AttributeError) as e: getattr(model, param).value = value str(e.value).index(param) # raise exception if not found
def test_change_dmepoch(): parfile = os.path.join(datadir, "J2229+2643_dm1.par") model = models.get_model(parfile) t0 = Time(56000, scale="tdb", format="mjd") epoch_diff = (t0.mjd_long - model.DMEPOCH.quantity.mjd_long) * u.day DM_at_t0 = model.DM.quantity + model.DM1.quantity * epoch_diff.to(u.s) model.change_dmepoch(t0) assert np.abs(model.DM.quantity - DM_at_t0) < 1e-8 * u.pc / u.cm ** 3
def test_copy_model_object(self): model = get_model("J1614-2230_NANOGrav_12yv3.wb.gls.par") model_copy = copy.deepcopy(model) assert sys.getsizeof(model) == sys.getsizeof(model_copy) assert id(model) != id(model_copy) assert len(model.params) == len(model_copy.params) assert list(model.components.keys()) == list(model_copy.components.keys())
def get_pint_models(psr_name, psr_file_path): """Function that returns pint model given a specific pulsar""" # will need to add section for J1713 T2 file. gls is not file wanted for this specfic pulsar. model_name = "{0}{1}_NANOGrav_11yv1.gls.par".format( psr_file_path, psr_name) par_model = models.get_model(model_name) return par_model
def test_dm_barycentered(): model = get_model(io.StringIO(par_base)) toas = make_fake_toas(57000, 57900, 10, model, obs="@", freq=np.inf) model.free_params = ["F0", "DM"] fitter = pint.fitter.WLSFitter(toas, model) with pytest.warns(pint.fitter.DegeneracyWarning, match=".*degeneracy.*DM.*"): fitter.fit_toas()
def test_A1DOT(): """Should get changed to XDOT for TEMPO/TEMPO2""" m = get_model(os.path.join(datadir, "J1600-3053_test.par")) assert (("A1DOT" in m.as_parfile()) and not ("XDOT" in m.as_parfile()) and not ("A1DOT" in m.as_parfile(format="tempo")) and ("XDOT" in m.as_parfile(format="tempo")) and not ("A1DOT" in m.as_parfile(format="tempo2")) and ("XDOT" in m.as_parfile(format="tempo2")))
def test_MODE1(): """Should start TEMPO2""" m = get_model(os.path.join(datadir, "B1855+09_NANOGrav_dfg+12_TAI.par")) assert (not (m.as_parfile(include_info=False).startswith("MODE 1")) and not (m.as_parfile(include_info=False, format="tempo").startswith("MODE 1")) and (m.as_parfile(include_info=False, format="tempo2").startswith("MODE 1")))
def test_DMDATA_N(): """Should be an integer for TEMPO/TEMPO2""" m = get_model(os.path.join(datadir, "J0030+0451_post.par")) for l in m.as_parfile(format="tempo").split("\n"): if "DMDATA" in l: # this should be a 0 dmdata = int(l.split()[-1]) assert dmdata == 0
def setUpClass(self): self.parfileJ1853 = "J1853+1303_NANOGrav_11yv0.gls.par" self.timfileJ1853 = "J1853+1303_NANOGrav_11yv0.tim" self.toasJ1853 = toa.get_TOAs(self.timfileJ1853, ephem="DE421", planets=False) self.modelJ1853 = model.get_model(self.parfileJ1853) self.ltres, self.ltbindelay = np.genfromtxt(self.parfileJ1853 + \ '.tempo2_test',skip_header=1, unpack=True) self.parfileJ0613 = "J0613-0200_NANOGrav_9yv1_ELL1H.gls.par" self.timfileJ0613 = "J0613-0200_NANOGrav_9yv1.tim" self.modelJ0613 = model.get_model(self.parfileJ0613) self.toasJ0613 = toa.get_TOAs(self.timfileJ0613, ephem="DE421", planets=False) self.parfileJ0613_STIG = "J0613-0200_NANOGrav_9yv1_ELL1H_STIG.gls.par" self.modelJ0613_STIG = model.get_model(self.parfileJ0613_STIG)
def test_unset_other_epochs_dm1(caplog): caplog.set_level(logging.WARNING) with warnings.catch_warnings(): warnings.simplefilter("error") m = models.get_model(io.StringIO("\n".join([test_par, "DM1 1e-10"]))) assert m.PEPOCH.quantity is not None assert m.DMEPOCH.quantity == m.PEPOCH.quantity assert m.POSEPOCH.quantity is None assert not caplog.records
def test_solar_wind_delays_positive(): model = get_model(StringIO("\n".join([par, "NE_SW 1"]))) toas = make_fake_toas_uniform(54000, 54000 + year, 13, model=model, obs="gbt") assert np.all( model.components["SolarWindDispersion"].solar_wind_dm(toas) > 0)
def test_merge_indices(): m = get_model(StringIO(simplepar)) fakes = [ toa.make_fake_toas(55000, 55500, 5, model=m, obs="ao"), toa.make_fake_toas(56000, 56500, 10, model=m, obs="gbt"), toa.make_fake_toas(57000, 57500, 15, model=m, obs="@"), ] toas = toa.merge_TOAs(fakes) assert len(set(toas.table["index"])) == len(toas)
def test_unused_noise_model_parameter(param, value): model = get_model(io.StringIO("\n".join([par_base, f"{param} TEL ao {value}"]))) toas = make_fake_toas_uniform( 58000, 58900, 10, model, obs="barycenter", freq=np.inf ) model.free_params = ["F0"] fitter = pint.fitter.GLSFitter(toas, model) with pytest.warns(UserWarning, match=param): fitter.fit_toas()
def test_H3_and_H4_non_zero(toasJ0613): """Testing if the different H3, H4 combination breaks the fitting. the fitting result will not be checked here. """ simple_model = get_model(StringIO(simple_par)) test_toas = toasJ0613[::20] f = ff.WLSFitter(test_toas, simple_model) f.fit_toas()
def test_unset_other_epochs(caplog, par): caplog.set_level(logging.WARNING) with warnings.catch_warnings(): warnings.simplefilter("error") m = models.get_model(io.StringIO(par)) assert m.PEPOCH.quantity is not None assert m.DMEPOCH.quantity is None assert m.POSEPOCH.quantity is None assert not caplog.records
def test_zero_H4(toasJ0613): H4_zero_model = get_model(StringIO(simple_par)) H4_zero_model.H4.value = 0.0 H4_zero_model.H4.frozen = False assert H4_zero_model.H3.value != 0.0 H4_zero_model.H3.frozen = False test_toas = toasJ0613[::20] f = ff.WLSFitter(test_toas, H4_zero_model) f.fit_toas()
def test_zero_H3(toasJ0613): H3_zero_model = get_model(StringIO(simple_par)) H3_zero_model.H3.value = 0.0 H3_zero_model.H3.frozen = False assert H3_zero_model.H4.value != 0.0 H3_zero_model.H4.frozen = False test_toas = toasJ0613[::20] with pytest.raises(ValueError): ff.WLSFitter(test_toas, H3_zero_model)
def _gen_polyco(self, parfile, MJD_start, segLength = 60.0, ncoeff = 15, \ maxha=12.0, method="TEMPO", numNodes=20, usePINT = True): """ This will be a convenience function to generate polycos and subsequent parameters to replace the values in a PSRFITS file header. The default way to do this will be to use PINT (usePINT = True), with other methods currently unsupported. The input values are: parfile [string] : path to par file used to generate the polycos. The observing frequency, and observatory will come from the par file MJD_start [float] : Start MJD of the polyco. Should start no later than the beginning of the observation segLength [float] : Length in minutes of the range covered by the polycos generated. Default is 60 minutes ncoeff [int] : number of polyco coeffeicients to generate. Default is 15, the same as in the PSRFITS file maxha [float] : max hour angle needed by PINT. Default is 12.0 method [string] : Method PINT uses to generate the polyco. Currently only TEMPO is supported. numNodes [int] : Number of nodes PINT will use to fit the polycos. Must be larger than ncoeff usePINT [bool] : Method used to generate polycos. Currently only PINT is supported. """ if usePINT: # Define dictionary to put parameters into polyco_dict = {'NSPAN': segLength, 'NCOEF': ncoeff} # load parfile to PINT model object m = models.get_model(parfile) # Determine MJD_end based on segLength MJD_end = MJD_start + np.double( make_quant(segLength, 'min').to('day').value) # MJD # Determine obseratory and observing frequency obsFreq = m.TZRFRQ.value # in MHz polyco_dict['REF_FREQ'] = obsFreq obs = m.TZRSITE.value # string polyco_dict['NSITE'] = obs.encode( 'utf-8') # observatory code needs to be in binary # Get pulsar frequency polyco_dict['REF_F0'] = m.F0.value # get the polycos pcs = polycos.Polycos() pcs.generate_polycos(m, MJD_start, MJD_end, obs, segLength, ncoeff, obsFreq, maxha=12.0, method="TEMPO", \ numNodes=20) coeffs = pcs.polycoTable['entry'][-1].coeffs polyco_dict['COEFF'] = coeffs # Now we need to determine the reference MJD, and phase REF_MJD = np.double(pcs.polycoTable['tmid'][-1]) polyco_dict['REF_MJD'] = REF_MJD # Now find the phase difference tmid_toa = toa.get_TOAs_list( [toa.TOA(REF_MJD, obs=obs, freq=obsFreq)]) ref_phase = m.phase(tmid_toa) # Need to force positive value if ref_phase.frac.value[0] < 0.0: ref_frac_phase = 1.0 - abs(ref_phase.frac.value[0]) else: ref_frac_phase = ref_phase.frac.value[0] polyco_dict['REF_PHS'] = ref_frac_phase return polyco_dict else: print("Only PINT is currently supported for generating polycos") raise NotImplementedError()
def test_merge_indices(): m = get_model(StringIO(simplepar)) fakes = [ toa.make_fake_toas(55000, 55500, 5, model=m, obs="ao"), toa.make_fake_toas(56000, 56500, 10, model=m, obs="gbt"), toa.make_fake_toas(57000, 57500, 15, model=m, obs="@"), ] toas = toa.merge_TOAs(fakes) check_indices_contiguous(toas)
def test_renumber_subset(): m = get_model(StringIO(simplepar)) toas = toa.make_fake_toas(55000, 55500, 10, model=m, obs="ao") sub = toas[1:-1] assert 0 not in sub.table["index"] sub.renumber() assert np.all(sub.table["index"] == np.arange(len(sub)))
def test_STIGMA(): """Should get changed to VARSIGMA for TEMPO/TEMPO2""" m = get_model( os.path.join(datadir, "J0613-0200_NANOGrav_9yv1_ELL1H_STIG.gls.par")) assert (("STIGMA" in m.as_parfile()) and not ("VARSIGMA" in m.as_parfile()) and not ("STIGMA" in m.as_parfile(format="tempo")) and ("VARSIGMA" in m.as_parfile(format="tempo")) and not ("STIGMA" in m.as_parfile(format="tempo2")) and ("VARSIGMA" in m.as_parfile(format="tempo2")))
def setup(self): self.model = get_model("J1614-2230_NANOGrav_12yv3.wb.gls.par") self.toas = get_TOAs( "J1614-2230_NANOGrav_12yv3.wb.tim", ephem="DE436", bipm_version="BIPM2015" ) self.fit_data_name = ["toa", "dm"] self.fit_params_lite = ["F0", "F1", "ELONG", "ELAT", "DMJUMP1", "DMX_0022"] self.tempo_res = np.genfromtxt( "J1614-2230_NANOGrav_12yv3.wb.tempo_test", comments="#" )
def setup(self): self.model = get_model("J1614-2230_NANOGrav_12yv3.wb.gls.par") self.toas = get_TOAs("J1614-2230_NANOGrav_12yv3.wb.tim") toa_backends, valid_flags = self.toas.get_flag_value("fe") self.toa_backends = np.array(toa_backends) self.dm_jump_params = [ getattr(self.model, x) for x in self.model.params if (x.startswith("DMJUMP")) ]
def test_no_H3_H4(toasJ0613): """Test no H3 and H4 in model.""" no_H3_H4 = simple_par.replace("H4 2.0262048E-7 1 1.1276173E-7", "") no_H3_H4 = no_H3_H4.replace("H3 2.7507208E-7 1 1.5114416E-7", "") no_H3_H4_model = get_model(StringIO(no_H3_H4)) assert no_H3_H4_model.H3.value is None assert no_H3_H4_model.H4.value is None test_toas = toasJ0613[::20] f = ff.WLSFitter(test_toas, no_H3_H4_model) f.fit_toas()
def main(argv=None): parser = argparse.ArgumentParser( description="PINT tool for command-line parfile format conversions", formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) parser.add_argument("input", help="Input parfile", type=str) parser.add_argument( "-f", "--format", help=("Format for output"), choices=_parfile_formats, default="pint", ) parser.add_argument( "-o", "--out", help=("Output filename [default=stdout]"), default=None, ) parser.add_argument( "--log-level", type=str, choices=("TRACE", "DEBUG", "INFO", "WARNING", "ERROR"), default="WARNING", help="Logging level", dest="loglevel", ) args = parser.parse_args(argv) log.remove() log.add( sys.stderr, level=args.loglevel, colorize=True, format=pint.logging.format, filter=pint.logging.LogFilter(), ) if not os.path.exists(args.input): log.error(f"Cannot open '{args.input}' for reading") return log.info(f"Reading '{args.input}'") model = get_model(args.input) output = model.as_parfile(format=args.format) if args.out is None: # just output to STDOUT print(output) else: with open(args.out, "w") as outfile: outfile.write(output) log.info(f"Wrote to '{args.out}'") return
def test_wave(): parfile = 'J1513-5908_PKS_alldata_white.par' timfile = 'J1744-1134.Rcvr1_2.GASP.8y.x.tim' t = toa.get_TOAs(timfile, usepickle=False) m = tm.get_model(parfile) log.info("model.as_parfile():\n%s"%m.as_parfile()) print(m.as_parfile()) print(m) print(m.delay(t))
def test_zero_H3_H4_fit_H3(toasJ0613): H3H4_zero2_model = get_model(StringIO(simple_par)) H3H4_zero2_model.H3.value = 0.0 H3H4_zero2_model.H3.frozen = False H3H4_zero2_model.H4.value = 0.0 H3H4_zero2_model.H4.frozen = True test_toas = toasJ0613[::20] f = ff.WLSFitter(test_toas, H3H4_zero2_model) # This should work f.fit_toas() assert f.model.H3.value > 0.0
def test_ECL(): """Should be only IERS2003 for TEMPO2""" m = get_model(os.path.join(datadir, "J0613-0200_NANOGrav_9yv1.gls.par")) for l in m.as_parfile().split("\n"): if "ECL" in l: assert l.split()[-1] == "IERS2010" for l in m.as_parfile(format="tempo").split("\n"): if "ECL" in l: assert l.split()[-1] == "IERS2010" for l in m.as_parfile(format="tempo2").split("\n"): if "ECL" in l: assert l.split()[-1] == "IERS2003"
def test_T2CMETHOD(): """Should be commented out in TEMPO2""" m = get_model(os.path.join(datadir, "B1855+09_NANOGrav_dfg+12_TAI.par")) for l in m.as_parfile().split("\n"): if "T2CMETHOD" in l: assert not (l.startswith("#")) for l in m.as_parfile(format="tempo").split("\n"): if "T2CMETHOD" in l: assert not (l.startswith("#")) for l in m.as_parfile(format="tempo2").split("\n"): if "T2CMETHOD" in l: assert l.startswith("#")
def setUpClass(cls): os.chdir(datadir) cls.parfileJ1853 = "J1853+1303_NANOGrav_11yv0.gls.par" cls.timfileJ1853 = "J1853+1303_NANOGrav_11yv0.tim" cls.toasJ1853 = toa.get_TOAs(cls.timfileJ1853, ephem="DE421", planets=False) cls.modelJ1853 = model.get_model(cls.parfileJ1853) cls.ltres, cls.ltbindelay = np.genfromtxt(cls.parfileJ1853 + ".tempo2_test", skip_header=1, unpack=True) cls.parfileJ0613 = "J0613-0200_NANOGrav_9yv1_ELL1H.gls.par" cls.timfileJ0613 = "J0613-0200_NANOGrav_9yv1.tim" cls.modelJ0613 = model.get_model(cls.parfileJ0613) cls.toasJ0613 = toa.get_TOAs(cls.timfileJ0613, ephem="DE421", planets=False) cls.parfileJ0613_STIG = "J0613-0200_NANOGrav_9yv1_ELL1H_STIG.gls.par" cls.modelJ0613_STIG = model.get_model(cls.parfileJ0613_STIG)
def get_orbital_correction_from_ephemeris_file(mjdstart, mjdstop, parfile, ntimes=1000, ephem="DE405", return_pint_model=False): """Get a correction for orbital motion from pulsar parameter file. Parameters ---------- mjdstart, mjdstop : float Start and end of the time interval where we want the orbital solution parfile : str Any parameter file understood by PINT (Tempo or Tempo2 format) Other parameters ---------------- ntimes : int Number of time intervals to use for interpolation. Default 1000 Returns ------- correction_sec : function Function that accepts in input an array of times in seconds and a floating-point MJDref value, and returns the deorbited times correction_mjd : function Function that accepts times in MJDs and returns the deorbited times. """ from scipy.interpolate import interp1d from astropy import units simon("Assuming events are already referred to the solar system " "barycenter (timescale is TDB)") if not HAS_PINT: raise ImportError("You need the optional dependency PINT to use this " "functionality: github.com/nanograv/pint") mjds = np.linspace(mjdstart, mjdstop, ntimes) toalist = _load_and_prepare_TOAs(mjds, ephem=ephem) m = get_model(parfile) delays = m.delay(toalist) correction_mjd = \ interp1d(mjds, (toalist.table['tdbld'] * units.d - delays).to(units.d).value) def correction_sec(times, mjdref): deorb_mjds = correction_mjd(times / 86400 + mjdref) return np.array((deorb_mjds - mjdref) * 86400) retvals = [correction_sec, correction_mjd] if return_pint_model: retvals.append(m) return retvals
def loadparfile(self, parfile): """ Load a parfile with pint :param parfile: Name of the parfile """ self.model = pm.get_model(parfile) log.info("model.as_parfile():\n%s"%self.model.as_parfile()) try: self.planets = self.model.PLANET_SHAPIRO.value except AttributeError: self.planets = False self._readparams()
def __init__(self, parfile=None, timfile=None, testpulsar=False): """ Initialize the pulsar object @param parfile: Filename of par file @param timfile: Filename of tim file @param testpulsar: If true, load J1744 test pulsar """ super(PPulsar, self).__init__() # Create a timing-model self._interface = "pint" #m = pm.StandardTimingModel() #m = pm.DDTimingModel() if testpulsar: # Write a test-pulsar, and open that for testing parfilename = tempfile.mktemp() timfilename = tempfile.mktemp() parfile = open(parfilename, 'w') timfile = open(timfilename, 'w') parfile.write(J1744_parfile_basic) timfile.write(J1744_timfile) parfile.close() timfile.close() elif parfile is not None and timfile is not None: parfilename = parfile timfilename = timfile else: raise ValueError("No valid pulsar to load") # We have a par/tim file. Read them in! m = pm.get_model(parfilename) #m.read_parfile(parfilename) print("model.as_parfile():") print(m.as_parfile()) try: planet_ephems = m.PLANET_SHAPIRO.value except AttributeError: planet_ephems = False #t0 = time.time() t = toa.get_TOAs(timfilename) #time_toa = time.time() - t0 t.print_summary() #sys.stderr.write("Read/corrected TOAs in %.3f sec\n" % time_toa) self._mjds = t.get_mjds() #d_tdbs = np.array([x.tdb.delta_tdb_tt for x in t.table['mjd']]) self._toaerrs = t.get_errors() resids = np.zeros_like(self._mjds) #ss_roemer = np.zeros_like(self._mjds) #ss_shapiro = np.zeros_like(self._mjds) #sys.stderr.write("Computing residuals...\n") #t0 = time.time() phases = m.phase(t.table) resids = phases.frac #for ii, tt in enumerate(t.table): # p = m.phase(tt) # resids[ii] = p.frac # ss_roemer[ii] = m.solar_system_geometric_delay(tt) # ss_shapiro[ii] = m.solar_system_shapiro_delay(tt) #time_phase = time.time() - t0 #sys.stderr.write("Computed phases in %.3f sec\n" % time_phase) # resids in (approximate) us: self._resids_us = resids / float(m.F0.value) * 1e6 sys.stderr.write("RMS PINT residuals are %.3f us\n" % self._resids_us.std()) # Create a dictionary of the fitting parameters self.pardict = OrderedDict() self.pardict['START'] = tempopar('START') self.pardict['FINISH'] = tempopar('FINISH') self.pardict['RAJ'] = tempopar('RAJ') self.pardict['DECJ'] = tempopar('DECJ') self.pardict['PMRA'] = tempopar('PMRA') self.pardict['PMDEC'] = tempopar('PMDEC') self.pardict['F0'] = tempopar('F0') self.pardict['F1'] = tempopar('F1') if testpulsar: os.remove(parfilename) os.remove(timfilename)
#! /usr/bin/env python import sys,os import pint.models as tm from pinttestdata import testdir, datadir datadir = os.path.join(testdir,'datafile') parfile = os.path.join(datadir,'J1744-1134.basic.par') m = tm.get_model(parfile) print("model.param_help():") m.param_help() print() print("calling model.read_parfile():") m.read_parfile(parfile) print() print("print model:") print(m) print() print("model.as_parfile():") print(m.as_parfile()) print()
import pint.models as tm from pint.phase import Phase from pint import toa from pint import fitter #import matplotlib #matplotlib.use('TKAgg') import matplotlib.pyplot as plt import numpy from pinttestdata import testdir, datadir # Get model m = tm.get_model(os.path.join(datadir,'NGC6440E.par')) # Get TOAs t = toa.TOAs(os.path.join(datadir,'NGC6440E.tim')) t.apply_clock_corrections(include_bipm=False) t.compute_TDBs() try: planet_ephems = m.PLANET_SHAPIRO.value except AttributeError: planet_ephems = False t.compute_posvels(planets=planet_ephems) f=fitter.WlsFitter(toas=t,model=m) # Print initial chi2 print('chi^2 is initially %0.2f' % f.resids.chi2)