def test_toa_merge_again(): filenames = ["NGC6440E.tim", "testtimes.tim", "parkes.toa"] toas = [toa.get_TOAs(ff) for ff in filenames] ntoas = sum([tt.ntoas for tt in toas]) nt = toa.merge_TOAs(toas) # The following tests merging with and already merged TOAs other = toa.get_TOAs("test1.tim") nt2 = toa.merge_TOAs([nt, other]) assert len(nt2.filename) == 5 assert nt2.ntoas == ntoas + 9 check_indices_contiguous(nt2)
def test_toa_merge_again(): filenames = ["NGC6440E.tim", "testtimes.tim", "parkes.toa"] toas = [toa.get_TOAs(ff) for ff in filenames] ntoas = sum([tt.ntoas for tt in toas]) other = toa.get_TOAs("test1.tim") # check consecutive merging nt = toa.merge_TOAs(toas[:2]) nt = toa.merge_TOAs([nt, toas[2]]) nt = toa.merge_TOAs([nt, other]) assert len(nt.filename) == 5 assert nt.ntoas == ntoas + 9 check_indices_contiguous(nt) assert nt.check_hashes()
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 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_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_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_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_toa_merge(): filenames = ["NGC6440E.tim", "testtimes.tim", "parkes.toa"] toas = [toa.get_TOAs(ff) for ff in filenames] ntoas = sum([tt.ntoas for tt in toas]) nt = toa.merge_TOAs(toas) assert len(nt.observatories) == 3 assert nt.table.meta["filename"] == filenames assert nt.ntoas == ntoas check_indices_contiguous(nt) assert nt.check_hashes()
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_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 test_toa_merge(): filenames = ["NGC6440E.tim", "testtimes.tim", "parkes.toa"] toas = [toa.get_TOAs(ff) for ff in filenames] ntoas = sum([tt.ntoas for tt in toas]) nt = toa.merge_TOAs(toas) assert len(nt.observatories) == 3 assert nt.table.meta["filename"] == filenames assert nt.ntoas == ntoas # The following tests merging with and already merged TOAs other = toa.get_TOAs("test1.tim") nt2 = toa.merge_TOAs([nt, other]) assert len(nt2.filename) == 5 assert nt2.ntoas == ntoas + 9 # check consecutive merging nt = toa.merge_TOAs(toas[:2]) nt = toa.merge_TOAs([nt, toas[2]]) nt = toa.merge_TOAs([nt, other]) assert len(nt.filename) == 5 assert nt.ntoas == ntoas + 9 # now test a failure if ephems are different toas[0].ephem = "DE436" with pytest.raises(TypeError): nt = toa.merge_TOAs(toas)
def random_models(self, selected): """Compute and plot random models """ log.info("Computing random models based on parameter covariance matrix.") if [p for p in self.postfit_model.free_params if p.startswith("DM")]: log.warning( "Fitting for DM while using random models can cause strange behavior." ) # These are the currently selected TOAs in the fit sim_sel = copy.deepcopy(self.selected_toas) # These are single TOAs from each cluster of TOAs inds = np.zeros(sim_sel.ntoas, dtype=bool) inds[np.unique(sim_sel.get_clusters(), return_index=True)[1]] |= True sim_sel = sim_sel[inds] # Get the range of MJDs we are using in the fit mjds = sim_sel.get_mjds().value minselMJD, maxselMJD = mjds.min(), mjds.max() extra = 0.1 # Fraction beyond TOAs to plot or calculate random models if self.faketoas1 is None: mjds = self.all_toas.get_mjds().value minallMJD, maxallMJD = mjds.min(), mjds.max() spanMJD = maxallMJD - minallMJD # want roughly 1 per day up to 3 years if spanMJD < 1000: Ntoas = min(400, int(spanMJD)) elif spanMJD < 4000: Ntoas = min(750, int(spanMJD) // 2) else: Ntoas = min(1500, int(spanMJD) // 4) log.debug(f"Generating {Ntoas} fake TOAs for the random models") # By default we will use TOAs from the TopoCenter. This gets done only once. self.faketoas1 = make_fake_toas_uniform( minallMJD - extra * spanMJD, maxallMJD + extra * spanMJD, Ntoas, self.postfit_model, obs="coe", freq=1 * u.THz, # effectively infinite frequency include_bipm=sim_sel.clock_corr_info["include_bipm"], include_gps=sim_sel.clock_corr_info["include_gps"], ) self.faketoas1.compute_pulse_numbers(self.postfit_model) # Combine our TOAs toas = merge_TOAs([sim_sel, self.faketoas1]) zero_residuals(toas, self.postfit_model) toas.table.sort("tdbld") # for plotting # Get a selection array to select the non-fake TOAs refs = np.asarray(toas.get_flag_value("name")[0]) != "fake" # Compute the new random timing models rs = calculate_random_models( self.fitter, toas, Nmodels=15, keep_models=False, return_time=True, ) # Get a selection array for the fake TOAs that covers the fit TOAs (plus extra) mjds = toas.get_mjds().value spanMJD = maxselMJD - minselMJD toplot = np.bitwise_and( mjds > (minselMJD - extra * spanMJD), mjds < (maxselMJD + extra * spanMJD) ) # This is the mean of the reference resids for the selected TOAs if selected.sum(): # shorthand for having some selected ref_mean = self.postfit_resids.time_resids[selected][inds].mean() else: ref_mean = self.postfit_resids.time_resids[inds].mean() # This is the means of the corresponding resids from the random models ran_mean = rs[:, refs].mean(axis=1) # Now adjust each set of random resids so that the ran_mean == ref_mean rs -= ran_mean[:, np.newaxis] rs += ref_mean # And store the key things for plotting self.faketoas = toas self.random_resids = rs
def test_toa_merge_different_ephem(): filenames = ["NGC6440E.tim", "testtimes.tim", "parkes.toa"] toas = [toa.get_TOAs(ff) for ff in filenames] toas[0].ephem = "DE436" with pytest.raises(TypeError): nt = toa.merge_TOAs(toas)