def test_pickle_multiple(tmpdir): m = get_model(StringIO(simplepar)) fakes = [ toa.make_fake_toas(55000, 55500, 10, model=m, obs="ao"), toa.make_fake_toas(56000, 56500, 10, model=m, obs="gbt"), toa.make_fake_toas(57000, 57500, 10, model=m, obs="@"), ] filenames = [ os.path.join(tmpdir, f"t{i+1}.tim") for i in range(len(fakes)) ] picklefilename = os.path.join(tmpdir, "t.pickle.gz") for t, f in zip(fakes, filenames): t.write_TOA_file(f, format="tempo2") toa.get_TOAs(filenames, model=m, usepickle=True, picklefilename=picklefilename) assert os.path.exists(picklefilename) assert toa.get_TOAs(filenames, model=m, usepickle=True, picklefilename=picklefilename).was_pickled with open(filenames[-1], "at") as f: f.write("\n") assert not toa.get_TOAs( filenames, model=m, usepickle=True, picklefilename=picklefilename).was_pickled
def model_eccentric_toas_wb(): g = np.random.default_rng(0) model_eccentric = get_model( io.StringIO("\n".join([par_eccentric, "ECORR tel @ 2"]))) toas = merge_TOAs([ make_fake_toas( 57000, 57001, 20, model_eccentric, freq=1000, obs="@", dm=10 * u.pc / u.cm**3, ), make_fake_toas( 57000, 57001, 20, model_eccentric, freq=2000, obs="@", dm=10 * u.pc / u.cm**3, ), ]) toas.adjust_TOAs( TimeDelta(g.standard_normal(len(toas)) * toas.table["error"])) return model_eccentric, toas
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_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_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="@"), ] fakes_excised = [f[1:-1] for f in fakes] toas = toa.merge_TOAs(fakes) toas_excised = toa.merge_TOAs(fakes_excised) for i in range(len(toas_excised)): ix = toas_excised.table["index"][i] match = toas.table[toas.table["index"] == ix] assert len(match) == 1 assert match[0]["tdbld"] == toas_excised.table["tdbld"][i]
def test_ftest_wb(): """Test for wideband fitter class F-test.""" wb_m = tm.get_model(os.path.join(datadir, "J0023+0923_ell1_simple.par")) wb_t = toa.make_fake_toas( 56000.0, 56001.0, 10, wb_m, freq=1400.0, obs="GBT", dm=wb_m.DM.value ) wb_f = fitter.WidebandTOAFitter(wb_t, wb_m) wb_f.fit_toas() # Parallax PX = param.floatParameter( parameter_type="float", name="PX", value=0.0, units=u.mas, frozen=False ) PX_Component = "AstrometryEcliptic" # A1DOT A1DOT = param.floatParameter( parameter_type="float", name="A1DOT", value=0.0, units=ls / u.second, frozen=False, ) A1DOT_Component = "BinaryELL1" # Test adding A1DOT Ftest_dict = wb_f.ftest(A1DOT, A1DOT_Component, remove=False, full_output=True) assert isinstance(Ftest_dict["ft"], float) or isinstance(Ftest_dict["ft"], bool) # Test removing parallax Ftest_dict = wb_f.ftest(PX, PX_Component, remove=True, full_output=True) assert isinstance(Ftest_dict["ft"], float) or isinstance(Ftest_dict["ft"], bool)
def test_ftest_nb(): """Test for narrowband fitter class F-test.""" m = tm.get_model(os.path.join(datadir, "J0023+0923_ell1_simple.par")) t = toa.make_fake_toas(56000.0, 56001.0, 10, m, freq=1400.0, obs="AO") f = fitter.WLSFitter(toas=t, model=m) f.fit_toas() # Test adding parameters F2 = param.prefixParameter( parameter_type="float", name="F2", value=0.0, units=u.Hz / u.s / u.s, frozen=False, ) ft = f.ftest(F2, "Spindown", remove=False) assert isinstance(ft["ft"], float) or isinstance(ft["ft"], bool) # Test return the full output Ftest_dict = f.ftest(F2, "Spindown", remove=False, full_output=True) assert isinstance(Ftest_dict["ft"], float) or isinstance(Ftest_dict["ft"], bool) # Test removing parameter F1 = param.prefixParameter( parameter_type="float", name="F1", value=0.0, units=u.Hz / u.s, frozen=False ) ft = f.ftest(F1, "Spindown", remove=True) assert isinstance(ft["ft"], float) or isinstance(ft["ft"], bool) Ftest_dict = f.ftest(F1, "Spindown", remove=True, full_output=True) assert isinstance(Ftest_dict["ft"], float) or isinstance(Ftest_dict["ft"], bool)
def test_dmx_barycentered(Fitter): model = get_model( io.StringIO( "\n".join( [ par_base, "DMX 15", "DMX_0001 16", "DMXR1_0001 58000", "DMXR2_0001 59000", ] ) ) ) toas = make_fake_toas(58000, 58900, 10, model, obs="@", freq=np.inf) model.free_params = ["F0", "DM", "DMX_0001"] fitter = Fitter(toas, model) with pytest.warns(pint.fitter.DegeneracyWarning, match=r".*degeneracy.*DM\b"): fitter.fit_toas() for p in fitter.model.free_params: assert not np.isnan(fitter.model[p].value) fitter = Fitter(toas, model) with pytest.warns(pint.fitter.DegeneracyWarning, match=r".*degeneracy.*DMX_0001\b"): fitter.fit_toas() for p in fitter.model.free_params: assert not np.isnan(fitter.model[p].value)
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_unused_noise_model_parameter(param, value): model = get_model(io.StringIO("\n".join([par_base, f"{param} TEL ao {value}"]))) toas = make_fake_toas(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_update_model(Fitter): model = get_model( io.StringIO("\n".join([par_base, "JUMP TEL barycenter 0"]))) model.INFO.value = "-f" model.ECL.value = "IERS2010" model.TIMEEPH.value = "FB90" model.T2CMETHOD.value = "IERS2000B" toas = make_fake_toas(58000, 59000, 10, model, obs="barycenter", freq=np.inf) fitter = Fitter(toas, model) fitter.fit_toas() par_out = fitter.model.as_parfile() assert re.search(r"CLOCK *TT\(TAI\)", par_out) assert re.search(r"TIMEEPH *FB90", par_out) assert re.search(r"T2CMETHOD *IERS2000B", par_out) assert re.search(r"NE_SW *0.0", par_out) assert re.search(r"ECL *IERS2010", par_out) assert re.search(r"DILATEFREQ *N", par_out) assert re.search(r"INFO *-f", par_out) assert re.search(r"NTOA *10.0", par_out) assert re.search(r"CHI2 *\d+.\d+", par_out) assert re.search(r"EPHEM *DE421", par_out) assert re.search(r"DMDATA *0.0", par_out) assert re.search(r"START *58000.0", par_out) assert re.search(r"FINISH *59000.0", par_out)
def test_jump_everything_wideband(): model = get_model( io.StringIO("\n".join([par_base, "JUMP TEL barycenter 0"]))) toas = make_fake_toas(58000, 58900, 10, model, obs="barycenter", freq=np.inf) for f in toas.table["flags"]: f["pp_dm"] = 15.0 f["pp_dme"] = 1e-4 model.free_params = ["JUMP1", "F0", "DM"] fitter = pint.fitter.WidebandTOAFitter(toas, model) with pytest.warns(pint.fitter.DegeneracyWarning, match=r".*degeneracy.*Offset\b"): fitter.fit_toas(threshold=1e-14) for p in fitter.model.free_params: assert not np.isnan(fitter.model[p].value) fitter = pint.fitter.WidebandTOAFitter(toas, model) with pytest.warns(pint.fitter.DegeneracyWarning, match=r".*degeneracy.*JUMP1\b"): fitter.fit_toas(threshold=1e-14) for p in fitter.model.free_params: assert not np.isnan(fitter.model[p].value)
def test_parameter_overrides_model(model): t = make_fake_toas(56000, 59000, 10, model, obs="@") t.table["error"] = 1 * u.us delta_f = (1 / (t.last_MJD - t.first_MJD)).to(u.Hz) m_2 = deepcopy(model) m_2.F0.quantity += 2 * delta_f m_2.TRACK.value = "-2" r = Residuals(t, m_2) assert np.amax(r.phase_resids) - np.amin(r.phase_resids) > 1 r = Residuals(t, m_2, track_mode="nearest") assert np.amax(r.phase_resids) - np.amin(r.phase_resids) <= 1 r = Residuals(t, m_2, track_mode="use_pulse_numbers") assert np.amax(r.phase_resids) - np.amin(r.phase_resids) > 1 m_2.TRACK.value = "0" r = Residuals(t, m_2) assert np.amax(r.phase_resids) - np.amin(r.phase_resids) <= 1 r = Residuals(t, m_2, track_mode="nearest") assert np.amax(r.phase_resids) - np.amin(r.phase_resids) <= 1 r = Residuals(t, m_2, track_mode="use_pulse_numbers") assert np.amax(r.phase_resids) - np.amin(r.phase_resids) > 1
def test_random_models(): # Get model and TOAs 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) # 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 = toa.make_fake_toas(54200, 59000, 59000 - 54200, f.model) dphase, mrand = utils.calculate_random_models(f, tnew, Nmodels=100) # 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 = utils.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_load_multiple(tmpdir): m = get_model(StringIO(simplepar)) fakes = [ toa.make_fake_toas(55000, 55500, 10, model=m, obs="ao"), toa.make_fake_toas(56000, 56500, 10, model=m, obs="gbt"), toa.make_fake_toas(57000, 57500, 10, model=m, obs="@"), ] filenames = [os.path.join(tmpdir, f"t{i+1}.tim") for i in range(len(fakes))] for t, f in zip(fakes, filenames): t.write_TOA_file(f, format="tempo2") merged = toa.merge_TOAs([toa.get_TOAs(f, model=m) for f in filenames]) assert merged == toa.get_TOAs(filenames, model=m)
def wb(): m = get_model(join(datadir, "NGC6440E.par")) t = make_fake_toas(55000, 58000, 20, model=m, freq=1400 * u.MHz, dm=10 * pint.dmu) wb = WidebandTOAFitter(t, m) wb.fit_toas() return wb
def test_renumber_subset_reordered(): 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="@"), ] fakes_excised = [f[1:-1] for f in fakes] toas_excised = toa.merge_TOAs(fakes_excised) assert 0 not in toas_excised.table["index"] toas_excised.renumber() assert set(toas_excised.table["index"]) == set(range(len(toas_excised))) toas_excised.renumber(index_order=False) assert np.all(toas_excised.table["index"] == np.arange(len(toas_excised)))
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_tim_writing_order(): m = get_model(StringIO(simplepar)) fakes = [ toa.make_fake_toas(55000, 56000, 5, model=m, obs="ao"), toa.make_fake_toas(55000, 56000, 5, model=m, obs="gbt"), toa.make_fake_toas(55000, 56000, 5, model=m, obs="@"), ] toas = toa.merge_TOAs(fakes) toas.table["index"][np.argsort(toas.table["tdbld"])] = np.arange(len(toas)) o = StringIO() toas.write_TOA_file(o, order_by_index=False) obs = [ln.split()[4] for ln in o.getvalue().split("\n")[1:] if ln] assert obs[0] == obs[1] == obs[2] == obs[3] o = StringIO() toas.write_TOA_file(o, order_by_index=True) obs = [ln.split()[4] for ln in o.getvalue().split("\n")[1:] if ln] assert obs[0] == obs[3] == obs[6] == obs[9] assert obs[0] != obs[1]
def test_renumber_order(): m = get_model(StringIO(simplepar)) toas = toa.make_fake_toas(55000, 55500, 10, model=m, obs="ao") rev = toas[::-1] assert np.all(rev.table["index"] == np.arange(len(rev))[::-1]) rev.renumber() assert np.all(rev.table["index"] == np.arange(len(rev))[::-1]) rev.renumber(index_order=False) assert np.all(rev.table["index"] == np.arange(len(rev))) rev = toas[::-1] rev.renumber(index_order=True) assert np.all(rev.table["index"] == np.arange(len(rev))[::-1])
def test_jump_no_toas(): model = get_model(io.StringIO("\n".join([par_base, "JUMP -fe L_wide 0"]))) toas = make_fake_toas(57000, 57900, 10, model) assert len(model.JUMP1.select_toa_mask(toas)) == 0 model.JUMP1.frozen = True model.validate_toas(toas) model.JUMP1.frozen = False with pytest.raises(ValueError): model.validate_toas(toas) model.JUMP1.frozen = False fitter = pint.fitter.WLSFitter(toas, model) with pytest.raises(ValueError): fitter.fit_toas()
def test_get_highest_density_range(ndays): par_base = """ PSR J1234+5678 F0 1 0 ELAT 0 0 ELONG 0 0 PEPOCH 57000 DM 10 0 SOLARN0 0 """ model = get_model(io.StringIO(par_base)) toas_1 = toa.make_fake_toas(57000, 58000, 1000, model, obs="@") toas_2 = toa.make_fake_toas(57500, 57500 + ndays.value, 100, model, obs="@") merged = toa.merge_TOAs([toas_1, toas_2]) if ndays == 7 * u.d: x1 = merged.get_highest_density_range() x2 = merged.get_highest_density_range(ndays) assert abs(x2[0].value - 57500) <= 1e-5 assert abs(x2[1].value - (57500 + ndays.value)) <= 1e-5 if ndays == 7 * u.d: assert abs(x2[0].value - x1[0].value) <= 1e-5 assert abs(x2[1].value - x1[1].value) <= 1e-5
def test_jump_everything(Fitter): model = get_model(io.StringIO("\n".join([par_base, "JUMP TEL barycenter 0"]))) toas = make_fake_toas(58000, 58900, 10, model, obs="barycenter", freq=np.inf) model.free_params = ["JUMP1", "F0"] fitter = Fitter(toas, model) with pytest.warns(pint.fitter.DegeneracyWarning, match=r".*degeneracy.*Offset\b"): fitter.fit_toas(threshold=1e-14) for p in fitter.model.free_params: assert not np.isnan(fitter.model[p].value) fitter = Fitter(toas, model) with pytest.warns(pint.fitter.DegeneracyWarning, match=r".*degeneracy.*JUMP1\b"): fitter.fit_toas(threshold=1e-14) for p in fitter.model.free_params: assert not np.isnan(fitter.model[p].value)
def model_eccentric_toas(): g = np.random.default_rng(0) model_eccentric = get_model(io.StringIO(par_eccentric)) toas = make_fake_toas(57000, 57001, 20, model_eccentric, freq=1400, obs="@") toas.adjust_TOAs( TimeDelta(g.standard_normal(len(toas)) * toas.table["error"])) return model_eccentric, toas
def test_residuals_wls_chi2(): model = get_model( StringIO(""" PSRJ J1234+5678 ELAT 0 ELONG 0 DM 10 F0 1 PEPOCH 58000 """)) toas = make_fake_toas(57000, 59000, 20, model=model, error=1 * u.us) np.random.seed(0) toas.adjust_TOAs(TimeDelta(np.random.randn(len(toas)) * u.us)) r = Residuals(toas, model) f = WLSFitter(toas, model) assert f.fit_toas() == r.chi2
def test_dmx_no_toas(): model = get_model( io.StringIO("\n".join([ par_base, "DMX 15", "DMX_0001 16 1", "DMXR1_0001 58000", "DMXR2_0001 59000", ]))) toas = make_fake_toas(57000, 57900, 10, model) with pytest.raises(MissingTOAs) as e: model.validate_toas(toas) assert e.value.parameter_names == ["DMX_0001"] fitter = pint.fitter.WLSFitter(toas, model) with pytest.raises(MissingTOAs): fitter.fit_toas()
def test_sun_angle_ecliptic(frac): model = get_model(StringIO(par)) toas = make_fake_toas(march_equinox, march_equinox + 2 * year, 10, model=model, obs="gbt") # Sun longitude, from Astronomical Almanac sun_n = toas.get_mjds().value - 51544.5 sun_L = 280.460 + 0.9856474 * sun_n sun_g = 357.528 + 0.9856003 * sun_n sun_longitude = (sun_L + 1.915 * np.sin(np.deg2rad(sun_g)) + 0.020 * np.sin(2 * np.deg2rad(sun_g))) sun_longitude = (sun_longitude + 180) % 360 - 180 angles = np.rad2deg(model.sun_angle(toas).value) assert_allclose(angles, np.abs(sun_longitude), atol=1)
def test_residuals_scaled_uncertainties(): model = get_model( StringIO(""" PSRJ J1234+5678 ELAT 0 ELONG 0 DM 10 F0 1 PEPOCH 58000 EFAC mjd 57000 58000 2 """)) toas = make_fake_toas(57000, 59000, 20, model=model, error=1 * u.us) r = Residuals(toas, model) e = r.get_data_error(scaled=True) assert np.all(e != 0) assert 0 < np.sum(e > 1.5 * u.us) < len(toas) with pytest.raises(ValueError): model.as_parfile().index("EQUAD")
def wideband_fake(): model = get_model( StringIO(""" PSRJ J1234+5678 ELAT 0 ELONG 0 DM 10 F0 1 PEPOCH 58000 ECORR mjd 57000 58000 2 """)) toas = make_fake_toas(57000, 59000, 40, model=model, error=1 * u.us, dm=10) toas.compute_pulse_numbers(model) np.random.seed(0) toas.adjust_TOAs(TimeDelta(np.random.randn(len(toas)) * u.us)) for f in toas.table["flags"]: f["pp_dm"] += np.random.randn() * f["pp_dme"] return toas, model
def test_gls_chi2_full_cov(): model = get_model( StringIO(""" PSRJ J1234+5678 ELAT 0 ELONG 0 DM 10 F0 1 PEPOCH 58000 TNRedAmp -14.227505410948254 TNRedGam 4.91353 TNRedC 45 """)) model.free_params = ["ELAT", "ELONG"] toas = make_fake_toas(57000, 59000, 100, model=model, error=1 * u.us) np.random.seed(0) toas.adjust_TOAs(TimeDelta(np.random.randn(len(toas)) * u.us)) r = Residuals(toas, model) assert_allclose(r.calc_chi2(full_cov=True), r.calc_chi2(full_cov=False))