def test_write(self): # change parameter value for p in self.modelB1855.params: par = getattr(self.modelB1855, p) # Change value for 20% if isinstance(par.value, numbers.Number): ov = par.value if isinstance(par, mp.MJDParameter): continue else: par.value = ov * 0.8 self.res = resids(self.toasB1855, self.modelB1855, False).time_resids.to(u.s) f = open(self.out_parfile, 'w') f.write(self.modelB1855.as_parfile()) f.close() read_model = mb.get_model(self.out_parfile) read_res = resids(self.toasB1855, read_model, False).time_resids.to(u.s) assert np.all(np.abs(read_res.value - self.res.value) < 1e-15 ), 'Output parfile did not produce same residuals.' for pp in self.modelB1855.params: par_ori = getattr(self.modelB1855, pp) par_read = getattr(read_model, pp) if par_ori.uncertainty_value is not None: unc_diff = par_ori.uncertainty_value - par_read.uncertainty_value assert np.abs( unc_diff ) < 1e-15, pp + 'uncertainty does not keep the precision. at' + str( np.abs(unc_diff))
def test_J1853(self): pint_resids_us = resids(self.toasJ1853, self.modelJ1853, False).time_resids.to(u.s) # Due to PINT has higher order of ELL1 model, Tempo2 gives a difference around 3e-8 assert np.all( np.abs(pint_resids_us.value - self.ltres) < 3e-8), 'J1853 residuals test failed.'
def test_DMX(self): print("Testing DMX module.") rs = residuals.resids(self.toas, self.DMXm, False).time_resids.to(u.s).value ltres, _ = np.genfromtxt(self.parf+ '.tempo_test', unpack=True) resDiff = rs-ltres assert np.all(np.abs(resDiff) < 2e-8),\ "PINT and tempo Residual difference is too big."
def test_residuals(self): model = mb.get_model(self.parfile) toas = toa.get_TOAs(self.tim, ephem="DE436", planets=False, include_bipm=True) r = resids(toas, model) assert np.all(np.abs(r.time_resids.to(u.us)) < 800 * u.us), \ "Residuals did not computed correctly for early CHIME data."
def test_B1855(self): pint_resids_us = resids(self.toasB1855, self.modelB1855, False).time_resids.to(u.s) # Due to the gps2utc clock correction. We are at 3e-8 seconds level. assert np.all( np.abs(pint_resids_us.value - self.ltres) < 3e-8), 'B1855 residuals test failed.'
def test_DMX(self): print "Testing DMX module." rs = residuals.resids(self.toas, self.DMXm).time_resids.to(u.us).value psr = lt.tempopulsar(self.parf, self.timf) resDiff = rs-psr.residuals() assert np.all(resDiff) < 1e-7, \ "PINT and tempo Residual difference is too big. "
def make_ideal(toas, model, iterations=2): ''' Takes a pint.toas and pint.model object and effectively zeros out the residuals. ''' for ii in range(iterations): rs=resids(toas, model) toas.adjust_TOAs(TimeDelta(-1.0*rs.time_resids))
def test_K96(self): log= logging.getLogger( "TestJ1713 Switch of K96") self.modelJ1713.K96.value = False res = resids(self.toasJ1713, self.modelJ1713, False).time_resids.to(u.s) delay = self.modelJ1713.delay(self.toasJ1713) testp = tdu.get_derivative_params(self.modelJ1713) for p in testp.keys(): adf = self.modelJ1713.d_phase_d_param(self.toasJ1713, delay, p)
def test_J1713(self): log = logging.getLogger("TestJ1713.test_J1713") pint_resids_us = resids(self.toasJ1713, self.modelJ1713, False).time_resids.to(u.s) diff = pint_resids_us.value - self.ltres log.debug("Max diff %lf" % np.abs(diff - diff.mean()).max()) assert np.all( np.abs(diff - diff.mean()) < 5e-7), 'DDK J1713 TEST FAILED'
def test_K96(self): log = logging.getLogger("TestJ1713 Switch of K96") self.modelJ1713.K96.value = False res = resids(self.toasJ1713, self.modelJ1713, False).time_resids.to(u.s) delay = self.modelJ1713.delay(self.toasJ1713) testp = tdu.get_derivative_params(self.modelJ1713) for p in testp.keys(): adf = self.modelJ1713.d_phase_d_param(self.toasJ1713, delay, p)
def test_J1853(self): pint_resids_us = resids(self.toasJ1853, self.modelJ1853, False).time_resids.to(u.s) # Due to PINT has higher order of ELL1 model, Tempo2 gives a difference around 3e-8 # Changed to 4e-8 since modification to get_PSR_freq() makes this 3.1e-8 log = logging.getLogger("TestJ1853.J1853_residuals") diffs = np.abs(pint_resids_us.value - self.ltres) log.debug('Diffs: %s\nMax: %s' % (diffs, np.max(diffs))) assert np.all(diffs < 4e-8), 'J1853 residuals test failed.'
def formresiduals(self): """Form the residuals""" log.info("Computing residuals...") t0 = time.time() self.resids_us = resids(self.t, self.model).time_resids.to(u.us) time_phase = time.time() - t0 log.info("Computed phases and residuals in %.3f sec" % time_phase) # resids in (approximate) us: log.info("RMS PINT residuals are %.3f us" % self.resids_us.std().value)
def __init__(self, toas, model, sort=True, drop_pintpsr=True, planets=True): self._sort = sort self.planets = planets self.name = model.PSR.value if not drop_pintpsr: self.model = model self.pint_toas = toas self._toas = np.array(toas.table["tdbld"], dtype="float64") * 86400 # saving also stoas (e.g., for DMX comparisons) self._stoas = np.array(toas.get_mjds().value, dtype="float64") * 86400 self._residuals = np.array(resids(toas, model).time_resids.to(u.s), dtype="float64") self._toaerrs = np.array(toas.get_errors().to(u.s), dtype="float64") self._designmatrix = model.designmatrix(toas)[0] self._ssbfreqs = np.array(model.barycentric_radio_freq(toas), dtype="float64") # fitted parameters self.fitpars = ["Offset"] + [par for par in model.params if not getattr(model, par).frozen] # gather DM/DMX information if available self._set_dm(model) # set parameters spars = [par for par in model.params] self.setpars = [sp for sp in spars if sp not in self.fitpars] self._flags = {} for ii, obsflags in enumerate(toas.get_flags()): for jj, flag in enumerate(obsflags): if flag not in list(self._flags.keys()): self._flags[flag] = [""] * toas.ntoas self._flags[flag][ii] = obsflags[flag] # convert flags to arrays # TODO probably better way to do this for key, val in self._flags.items(): if isinstance(val[0], u.quantity.Quantity): self._flags[key] = np.array([v.value for v in val]) else: self._flags[key] = np.array(val) self._pdist = self._get_pdist() self._raj, self._decj = self._get_radec(model) self._pos = self._get_pos() self._planetssb = self._get_planetssb(toas, model) self._sunssb = self._get_sunssb(toas, model) # TODO: pos_t not currently implemented self._pos_t = np.zeros((len(self._toas), 3)) self.sort_data()
def __init__(self, toas, model, sort=True, planets=True): self._sort = sort self.planets = planets self.name = model.PSR.value self._toas = np.array(toas.table['tdbld'], dtype='float64') * 86400 self._residuals = np.array(resids(toas, model).time_resids.to(u.s), dtype='float64') self._toaerrs = np.array(toas.get_errors().to(u.s), dtype='float64') self._designmatrix = model.designmatrix(toas)[0] self._ssbfreqs = np.array(model.barycentric_radio_freq(toas), dtype='float64') # fitted parameters self.fitpars = ['Offset'] + [ par for par in model.params if not getattr(model, par).frozen ] # set parameters spars = [par for par in model.params] self.setpars = [sp for sp in spars if sp not in self.fitpars] self._flags = {} for ii, obsflags in enumerate(toas.get_flags()): for jj, flag in enumerate(obsflags): if flag not in list(self._flags.keys()): self._flags[flag] = [''] * toas.ntoas self._flags[flag][ii] = obsflags[flag] # convert flags to arrays # TODO probably better way to do this for key, val in self._flags.items(): if isinstance(val[0], u.quantity.Quantity): self._flags[key] = np.array([v.value for v in val]) else: self._flags[key] = np.array(val) self._pdist = self._get_pdist() self._raj, self._decj = self._get_radec(model) self._pos = self._get_pos() self._planetssb = self._get_planetssb() # TODO: pos_t not currently implemented self._pos_t = np.zeros((len(self._toas), 3)) self.sort_data()
def test_pulse_number(self): model = pint.models.get_model(parfile) toas = pint.toa.get_TOAs(timfile) #Make sure pn table column was added self.assertTrue('pn' in toas.table.colnames) #Tracking pn should result in runaway residuals track_resids = resids(toas, model).time_resids self.assertFalse(np.max(track_resids) < 0.2 * u.second) #Not tracking pn should keep residuals bounded getattr(model, 'TRACK').value = '0' notrack_resids = resids(toas, model).time_resids self.assertTrue(np.max(notrack_resids) < 0.2 * u.second) #Make sure Exceptions are thrown when trying to track nonexistent pn del toas.table['pn'] getattr(model, 'TRACK').value = '-2' self.assertRaises(Exception, resids, toas, model) #Make sure pn can be added back by using the model self.assertTrue(toas.get_pulse_numbers() is None) toas.compute_pulse_numbers(model) self.assertTrue('pn' in toas.table.colnames)
def test_write(self): # change parameter value for p in self.modelB1855.params: par = getattr(self.modelB1855, p) # Change value for 20% if isinstance(par.value, numbers.Number): ov = par.value if isinstance(par, mp.MJDParameter): continue else: par.value = ov * 0.8 self.res = resids(self.toasB1855, self.modelB1855, False).time_resids.to(u.s) f = open(self.out_parfile, 'w') f.write(self.modelB1855.as_parfile()) f.close() read_model = mb.get_model(self.out_parfile) read_res = resids(self.toasB1855, read_model, False).time_resids.to(u.s) assert np.all(np.abs(read_res.value - self.res.value) < 1e-15), 'Output parfile did not produce same residuals.' for pp in self.modelB1855.params: par_ori = getattr(self.modelB1855, pp) par_read = getattr(read_model, pp) if par_ori.uncertainty_value is not None: unc_diff = par_ori.uncertainty_value - par_read.uncertainty_value assert np.abs(unc_diff) < 1e-15, pp + 'uncertainty does not keep the precision. at' + str(np.abs(unc_diff))
def __init__(self, toas, model, sort=True, planets=True): self._sort = sort self.planets = planets self.name = model.PSR.value self._toas = np.array(toas.table['tdbld'], dtype='float64') * 86400 self._residuals = np.array(resids(toas, model).time_resids.to(u.s), dtype='float64') self._toaerrs = np.array(toas.get_errors().to(u.s), dtype='float64') self._designmatrix = model.designmatrix(toas.table)[0] self._ssbfreqs = np.array(model.barycentric_radio_freq(toas.table), dtype='float64') # fitted parameters self.fitpars = ['Offset'] + [par for par in model.params if not getattr(model, par).frozen] # set parameters spars = [par for par in model.params] self.setpars = [sp for sp in spars if sp not in self.fitpars] self._flags = {} for ii, obsflags in enumerate(toas.get_flags()): for jj, flag in enumerate(obsflags): if flag not in list(self._flags.keys()): self._flags[flag] = [''] * toas.ntoas self._flags[flag][ii] = obsflags[flag] # convert flags to arrays # TODO probably better way to do this for key, val in self._flags.items(): if isinstance(val[0], u.quantity.Quantity): self._flags[key] = np.array([v.value for v in val]) else: self._flags[key] = np.array(val) self._pdist = self._get_pdist() self._raj, self._decj = self._get_radec(model) self._pos = self._get_pos() self._planetssb = self._get_planetssb() # TODO: pos_t not currently implemented self._pos_t = np.zeros((len(self._toas), 3)) self.sort_data()
def test_pint_installed_correctly(self): import pint.toa as toa from pint.residuals import resids import pint.models.model_builder as mb import astropy.units as u parfile = os.path.join(self.datadir, 'example_pint.par') timfile = os.path.join(self.datadir, 'example_pint.tim') toas = toa.get_TOAs(timfile, ephem="DE405", planets=False, include_bipm=False) model = mb.get_model(parfile) pint_resids_us = resids(toas, model, False).time_resids.to(u.s) # Due to the gps2utc clock correction. We are at 3e-8 seconds level. assert np.all(np.abs(pint_resids_us.value) < 3e-6)
except AttributeError: planets = False t0 = time.time() t = toa.get_TOAs(timfile, planets=planets, include_bipm=False, usepickle=False) time_toa = time.time() - t0 if log.level < 25: t.print_summary() log.info("Read/corrected TOAs in %.3f sec" % time_toa) mjds = t.get_mjds() errs = t.get_errors() log.info("Computing residuals...") t0 = time.time() resids_us = resids(t, m, False).time_resids.to(u.us) time_phase = time.time() - t0 log.info("Computed phases and residuals in %.3f sec" % time_phase) # resids in (approximate) us: log.info("RMS PINT residuals are %.3f us" % resids_us.std().value) # Get some general2 stuff log.info("Running TEMPO2...") # Old tempo2 values output # tempo2_vals = tempo2_utils.general2(parfile, timfile, # ['tt2tb', 'roemer', 'post_phase', # 'shapiro', 'shapiroJ']) tempo2_vals = numpy.genfromtxt(parfile + '.tempo2_test', names=True, comments='#',
def test_B1855(self): pint_resids_us = resids(self.toasB1855, self.modelB1855).time_resids.to(u.s) assert np.all(np.abs(pint_resids_us.value - self.ltres) < 1e-7), 'DD B1855 TEST FAILED'
def test_J0023(self): pint_resids_us = resids(self.toasJ0023, self.modelJ0023, False).time_resids.to(u.s) assert np.all(np.abs(pint_resids_us.value - self.ltres) < 1e-8), 'J0023 residuals test failed.'
def test_B1953(self): pint_resids_us = resids(self.toasB1953, self.modelB1953).time_resids.to(u.s) # Due to the gps2utc clock correction. We are at 3e-8 seconds level. assert np.all(np.abs(pint_resids_us.value - self.ltres) < 3e-8), 'B1953 residuals test failed.'
def test_jump(self): presids_s = resids(self.toas, self.JUMPm, False).time_resids.to(u.s) assert np.all(np.abs(presids_s.value - self.ltres['residuals']) < 1e-7), "JUMP test failed."
def test_J0613(self): pint_resids_us = resids(self.toasJ0613, self.modelJ0613).time_resids.to(u.s) # Due to the gps2utc clock correction. We are at 3e-8 seconds level. assert np.all(np.abs(pint_resids_us.value - self.ltres) < 3e-8), 'J0613 residuals test failed.'
# Using Nanograv data B1855 datadir = '../tests/datafile' parfile = os.path.join(datadir, 'B1855+09_NANOGrav_dfg+12_TAI_FB90.par') timfile = os.path.join(datadir, 'B1855+09_NANOGrav_dfg+12.tim') # libstempo calculation print("tempo2 calculation") tempo2_vals = tempo2_utils.general2(parfile, timfile,['pre']) # Build PINT model print("PINT calculation") mdd = mb.get_model(parfile) # Get toas to pint toas = toa.get_TOAs(timfile, planets=False, ephem='DE405', include_bipm=False) # Get residuals t2_resids = tempo2_vals['pre'] presids_us = resids(toas, mdd).time_resids.to(u.us) # Plot residuals plt.errorbar(toas.get_mjds().value, presids_us.value, toas.get_errors().value, fmt='x') print(toas.get_errors().value) print(toas.get_mjds()) print(presids_us.value) plt.title("%s Pre-Fit Timing Residuals" % mdd.PSR.value) plt.xlabel('MJD') plt.ylabel('Residual (us)') plt.grid() plt.show() # Plot tempo difference diff = (presids_us - t2_resids * u.second).to(u.us) plt.plot(toas.get_mjds(high_precision=False), diff, '+')
planets = False t0 = time.time() t = toa.get_TOAs(timfile, planets=planets, include_bipm=False, usepickle=False) time_toa = time.time() - t0 if log.level < 25: t.print_summary() log.info("Read/corrected TOAs in %.3f sec" % time_toa) mjds = t.get_mjds() errs = t.get_errors() log.info("Computing residuals...") t0 = time.time() resids_us = resids(t, m, False).time_resids.to(u.us) time_phase = time.time() - t0 log.info("Computed phases and residuals in %.3f sec" % time_phase) # resids in (approximate) us: log.info("RMS PINT residuals are %.3f us" % resids_us.std().value) # Get some general2 stuff log.info("Running TEMPO2...") # Old tempo2 values output # tempo2_vals = tempo2_utils.general2(parfile, timfile, # ['tt2tb', 'roemer', 'post_phase', # 'shapiro', 'shapiroJ']) tempo2_vals = numpy.genfromtxt(parfile + '.tempo2_test', names=True, comments = '#', dtype = 'float128') t2_resids = tempo2_vals['post_phase'] / float(m.F0.value) * 1e6 * u.us
datadir = '../tests/datafile' parfile = os.path.join(datadir, 'B1855+09_NANOGrav_dfg+12_TAI_FB90.par') timfile = os.path.join(datadir, 'B1855+09_NANOGrav_dfg+12.tim') # libstempo calculation print "tempo2 calculation" tempo2_vals = tempo2_utils.general2(parfile, timfile,['pre']) # Build PINT model print "PINT calculation" mdd = mb.get_model(parfile) # Get toas to pint toas = toa.get_TOAs(timfile, planets=False, ephem='DE405') tt = toas.table # Get residuals t2_resids = tempo2_vals['pre'] presids_us = resids(toas, mdd).time_resids # Plot residuals plt.errorbar(toas.get_mjds(high_precision=False), presids_us.value, toas.get_errors(), fmt='x') plt.title("%s Pre-Fit Timing Residuals" % mdd.PSR.value) plt.xlabel('MJD') plt.ylabel('Residual (us)') plt.grid() plt.show() # Plot tempo difference diff = (presids_us - t2_resids * u.second).to(u.us) plt.plot(toas.get_mjds(high_precision=False), diff, '+') plt.xlabel('Mjd (DAY)') plt.ylabel('residule difference (us)') plt.title('Residule difference between PINT and tempo2') plt.show()
def test_jump(self): presids_s = resids(self.toas, self.JUMPm, False).time_resids.to(u.s) assert np.all( np.abs(presids_s.value - self.ltres['residuals']) < 1e-7), "JUMP test failed."
except AttributeError: planets = False t0 = time.time() t = toa.get_TOAs(timfile, planets=planets,usepickle=False) time_toa = time.time() - t0 if log.level < 25: t.print_summary() log.info("Read/corrected TOAs in %.3f sec" % time_toa) mjds = t.get_mjds() errs = t.get_errors() log.info("Computing residuals...") t0 = time.time() resids_us = resids(t, m).time_resids.to(u.us) time_phase = time.time() - t0 log.info("Computed phases and residuals in %.3f sec" % time_phase) # resids in (approximate) us: log.info("RMS PINT residuals are %.3f us" % resids_us.std().value) # Get some general2 stuff log.info("Running TEMPO2...") tempo2_vals = tempo2_utils.general2(parfile, timfile, ['tt2tb', 'roemer', 'post_phase', 'shapiro', 'shapiroJ']) t2_resids = tempo2_vals['post_phase'] / float(m.F0.num_value) * 1e6 * u.us diff_t2 = (resids_us - t2_resids).to(u.ns) diff_t2 -= diff_t2.mean() log.info("Max resid diff between PINT and T2: %.2f ns" % numpy.fabs(diff_t2).max().value)
def test_J1713(self): log= logging.getLogger( "TestJ1713.test_J1713") pint_resids_us = resids(self.toasJ1713, self.modelJ1713, False).time_resids.to(u.s) diff = pint_resids_us.value - self.ltres log.debug("Max diff %lf" % np.abs(diff - diff.mean()).max()) assert np.all(np.abs(diff - diff.mean()) < 5e-7), 'DDK J1713 TEST FAILED'
def test_J0023(self): pint_resids_us = resids(self.toasJ0023, self.modelJ0023, False).time_resids.to(u.s) assert np.all( np.abs(pint_resids_us.value - self.ltres) < 1e-8), 'J0023 residuals test failed.'
datapath = os.path.join(os.environ['PINT'],'tests','datafile') # Using Nanograv data B1855 parfile = os.path.join(datapath, 'B1855+09_NANOGrav_dfg+12_modified.par') timfile = os.path.join(datapath, 'B1855+09_NANOGrav_dfg+12.tim') # libstempo calculation print "libstempo calculation" psr = lt.tempopulsar(parfile, timfile) # Build PINT model print "PINT calculation" mdd = mb.get_model(parfile) # Get toas to pint toas = toa.get_TOAs(timfile, planets=True) tt = toas.table # Run tempo2 general2 pluging tempo2_vals = tempo2_utils.general2(parfile, timfile, ['tt2tb', 'roemer', 'post_phase', 'shapiro', 'shapiroJ','bat','clock0', 'clock1','clock2','clock3','clock4','sat', 'tropo']) # compute residules t2_resids = tempo2_vals['post_phase'] / float(mdd.F0.value) * 1e6 * u.us presids_us = resids(toas, mdd).time_resids.to(u.us) toas = psr.toas() toas.sort() plt.plot(toas,presids_us-t2_resids) plt.xlabel('Mjd (DAY)') plt.ylabel('residule (us)') plt.title('Residule difference between PINT and tempo2') plt.show()
datadir = '../tests/datafile' parfile = os.path.join(datadir, 'B1855+09_NANOGrav_dfg+12_TAI_FB90.par') timfile = os.path.join(datadir, 'B1855+09_NANOGrav_dfg+12.tim') # libstempo calculation print "tempo2 calculation" tempo2_vals = tempo2_utils.general2(parfile, timfile, ['pre']) # Build PINT model print "PINT calculation" mdd = mb.get_model(parfile) # Get toas to pint toas = toa.get_TOAs(timfile, planets=False, ephem='DE405') tt = toas.table # Get residuals t2_resids = tempo2_vals['pre'] presids_us = resids(toas, mdd).time_resids # Plot residuals plt.errorbar(toas.get_mjds(high_precision=False), presids_us.value, toas.get_errors(), fmt='x') plt.title("%s Pre-Fit Timing Residuals" % mdd.PSR.value) plt.xlabel('MJD') plt.ylabel('Residual (us)') plt.grid() plt.show() # Plot tempo difference diff = (presids_us - t2_resids * u.second).to(u.us) plt.plot(toas.get_mjds(high_precision=False), diff, '+') plt.xlabel('Mjd (DAY)') plt.ylabel('residule difference (us)')
except AttributeError: planets = False t0 = time.time() t = toa.get_TOAs(timfile, planets=planets, usepickle=False) time_toa = time.time() - t0 if log.level < 25: t.print_summary() log.info("Read/corrected TOAs in %.3f sec" % time_toa) mjds = t.get_mjds() errs = t.get_errors() log.info("Computing residuals...") t0 = time.time() resids_us = resids(t, m).time_resids.to(u.us) time_phase = time.time() - t0 log.info("Computed phases and residuals in %.3f sec" % time_phase) # resids in (approximate) us: log.info("RMS PINT residuals are %.3f us" % resids_us.std().value) # Get some general2 stuff log.info("Running TEMPO2...") # Old tempo2 values output # tempo2_vals = tempo2_utils.general2(parfile, timfile, # ['tt2tb', 'roemer', 'post_phase', # 'shapiro', 'shapiroJ']) tempo2_vals = numpy.genfromtxt(parfile + '.tempo2_test', names=True, comments='#',