def test_pickle_multiple(tmpdir): m = get_model(StringIO(simplepar)) fakes = [ simulation.make_fake_toas_uniform(55000, 55500, 10, model=m, obs="ao"), simulation.make_fake_toas_uniform(56000, 56500, 10, model=m, obs="gbt"), simulation.make_fake_toas_uniform(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_uniform( 57000, 57001, 20, model_eccentric, freq=1000 * u.MHz, obs="@", dm=10 * u.pc / u.cm**3, ), make_fake_toas_uniform( 57000, 57001, 20, model_eccentric, freq=2000 * u.MHz, 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_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 = simulation.make_fake_toas_uniform(57000, 58000, 1000, model, obs="@") toas_2 = simulation.make_fake_toas_uniform(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_tim_writing_order(): m = get_model(StringIO(simplepar)) fakes = [ simulation.make_fake_toas_uniform(55000, 56000, 5, model=m, obs="ao"), simulation.make_fake_toas_uniform(55000, 56000, 5, model=m, obs="gbt"), simulation.make_fake_toas_uniform(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) toas.write_TOA_file("test.tim", order_by_index=False) obs = [ ln.split()[4] for ln in o.getvalue().split("\n")[1:] if (ln and not ln.startswith("C ")) ] 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 and not ln.startswith("C ")) ] assert obs[0] == obs[3] == obs[6] == obs[9] assert obs[0] != obs[1]
def test_merge_indices(): m = get_model(StringIO(simplepar)) fakes = [ simulation.make_fake_toas_uniform(55000, 55500, 5, model=m, obs="ao"), simulation.make_fake_toas_uniform(56000, 56500, 10, model=m, obs="gbt"), simulation.make_fake_toas_uniform(57000, 57500, 15, model=m, obs="@"), ] toas = toa.merge_TOAs(fakes) check_indices_contiguous(toas)
def test_dm_barycentered(): model = get_model(io.StringIO(par_base)) toas = make_fake_toas_uniform(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_update_model_sets_things(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 = "IF99" model.DILATEFREQ.value = True model.T2CMETHOD.value = "TEMPO" model.use_aliases(reset_to_default=True) toas = make_fake_toas_uniform( 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 *IAU2000B", 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", par_out) assert re.search(r"EPHEM *DE421", par_out) assert re.search(r"DMDATA *N", par_out) assert re.search(r"START *58000.0", par_out) assert re.search(r"FINISH *59000.0", par_out)
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_uniform(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_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_jump_everything_wideband(): model = get_model( io.StringIO("\n".join([par_base, "JUMP TEL barycenter 0"]))) toas = make_fake_toas_uniform(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_gls_chi2_behaviour(): 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 = ["F0", "ELAT", "ELONG"] toas = make_fake_toas_uniform(57000, 59000, 40, model=model, error=1 * u.us) np.random.seed(0) toas.adjust_TOAs(TimeDelta(np.random.randn(len(toas)) * u.us)) f = GLSFitter(toas, model) initial_chi2 = Residuals(toas, model).calc_chi2() fit_chi2 = f.fit_toas() assert fit_chi2 <= initial_chi2 assert f.resids.calc_chi2() <= initial_chi2 assert initial_chi2 == Residuals(toas, model).calc_chi2()
def test_parameter_overrides_model(model): t = make_fake_toas_uniform(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_assumes_dmepoch_equals_pepoch(): m_assume = get_model(io.StringIO("\n".join([par_base, "DM1 1e-5"]))) m_given = get_model(io.StringIO("\n".join([par_base, "DMEPOCH 58000", "DM1 1e-5"]))) t = make_fake_toas_uniform(57000, 59000, 10, m_assume) assert_allclose(m_assume.dm_value(t), m_given.dm_value(t))
def test_many_timfile_jumps(): m = get_model(io.StringIO(par_base)) pairs = 15 toas_per_jump = 3 t = make_fake_toas_uniform(56000, 57000, 5 + toas_per_jump * pairs, model=m) # The following lets us write the fake TOAs to a string as a timfile f = io.StringIO() with redirect_stdout(f): t.write_TOA_file(sys.stdout) s = f.getvalue().splitlines() toalist = ["\n".join(s[:11]) + "\n"] lo = 12 for _ in range(pairs): toalist.append("\n".join(["JUMP"] + s[lo : lo + toas_per_jump] + ["JUMP\n"])) lo += toas_per_jump # read the TOAs tt = get_TOAs(io.StringIO("".join(toalist))) # convert the timfile JUMPs to params m.jump_flags_to_params(tt) assert "PhaseJump" in m.components assert len(m.components["PhaseJump"].jumps) == pairs assert "JUMP1" in m.components["PhaseJump"].jumps assert "JUMP2" in m.components["PhaseJump"].jumps assert "JUMP10" in m.components["PhaseJump"].jumps assert "JUMP15" in m.components["PhaseJump"].jumps
def test_group_survives_plain_pickle(tmpdir): test_model = pint.models.get_model(os.path.join(datadir, "NGC6440E.par")) test_toas = simulation.make_fake_toas_uniform(58000, 59000, 5, model=test_model) new_toas = pickle.loads(pickle.dumps(test_toas)) assert len(test_toas.table.groups.keys) == 1 assert len(new_toas.table.groups.keys) == 1
def test_merge_indices_excised(): m = get_model(StringIO(simplepar)) fakes = [ simulation.make_fake_toas_uniform(55000, 55500, 5, model=m, obs="ao"), simulation.make_fake_toas_uniform(56000, 56500, 10, model=m, obs="gbt"), simulation.make_fake_toas_uniform(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) check_indices_unique(toas_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_renumber_subset_reordered(): m = get_model(StringIO(simplepar)) fakes = [ simulation.make_fake_toas_uniform(55000, 55500, 5, model=m, obs="ao"), simulation.make_fake_toas_uniform(56000, 56500, 10, model=m, obs="gbt"), simulation.make_fake_toas_uniform(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_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_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 wb(): m = get_model(join(datadir, "NGC6440E.par")) t = make_fake_toas_uniform( 55000, 58000, 20, model=m, freq=1400 * u.MHz, dm=10 * pint.dmu ) wb = WidebandTOAFitter(t, m) wb.fit_toas() return wb
def test_load_multiple(tmpdir): m = get_model(StringIO(simplepar)) fakes = [ simulation.make_fake_toas_uniform(55000, 55500, 10, model=m, obs="ao"), simulation.make_fake_toas_uniform(56000, 56500, 10, model=m, obs="gbt"), simulation.make_fake_toas_uniform(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 test_renumber_subset(): m = get_model(StringIO(simplepar)) toas = simulation.make_fake_toas_uniform(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_jump_no_toas(): model = get_model(io.StringIO("\n".join([par_base, "JUMP -fe L_wide 0"]))) toas = make_fake_toas_uniform(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_astropy_group_survives_pickle(tmpdir): p = os.path.join(tmpdir, "test.pickle.gz") test_model = pint.models.get_model(os.path.join(datadir, "NGC6440E.par")) test_toas = simulation.make_fake_toas_uniform(58000, 59000, 5, model=test_model) with open(p, "wb") as f: pickle.dump(test_toas.table, f) with open(p, "rb") as f: new_table = pickle.load(f) assert len(test_toas.table.groups.keys) == 1 assert len(new_table.groups.keys) == 1
def test_group_survives_pickle(tmpdir): p = os.path.join(tmpdir, "test.pickle.gz") test_model = pint.models.get_model(os.path.join(datadir, "NGC6440E.par")) test_toas = simulation.make_fake_toas_uniform(58000, 59000, 5, model=test_model) test_toas.adjust_TOAs( astropy.time.TimeDelta((np.random.uniform(0, 1, size=(5,)) * u.d)) ) pint.toa.save_pickle(test_toas, p) test_toas2 = pint.toa.load_pickle(p) test_toas2.adjust_TOAs( astropy.time.TimeDelta((np.random.uniform(0, 1, size=(5,)) * u.d)) )
def model_eccentric_toas(): g = np.random.default_rng(0) model_eccentric = get_model(io.StringIO(par_eccentric)) toas = make_fake_toas_uniform(57000, 57001, 20, model_eccentric, freq=1400 * u.MHz, obs="@") toas.adjust_TOAs( TimeDelta(g.standard_normal(len(toas)) * toas.table["error"])) return model_eccentric, toas
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_uniform(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_uniform(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_jump_everything(Fitter): model = get_model(io.StringIO("\n".join([par_base, "JUMP TEL barycenter 0"]))) toas = make_fake_toas_uniform( 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 test_renumber_order(): m = get_model(StringIO(simplepar)) toas = simulation.make_fake_toas_uniform(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])