def test_derived_tuple_fit(): parfile = pint.config.examplefile("NGC6440E.par") timfile = pint.config.examplefile("NGC6440E.tim") m, t = get_model_and_toas(parfile, timfile) f = WLSFitter(t, m) # find the best-fit f.fit_toas() bestfit = f.resids.chi2 F0 = np.linspace( f.model.F0.quantity - 3 * f.model.F0.uncertainty, f.model.F0.quantity + 3 * f.model.F0.uncertainty, 53, ) tau = np.linspace(8.1, 8.3, 53) * 100 * u.Myr parvalues = list(zip(F0, tau)) chi2_tau, params, _ = pint.gridutils.tuple_chisq_derived( f, ("F0", "F1"), (lambda x, y: x, lambda x, y: -x / 2 / y), parvalues, # ncpu=1, ) assert np.isclose(bestfit, chi2_tau.min(), atol=3)
def test_grid_extraparams_singleprocessor(): parfile = pint.config.examplefile("NGC6440E.par") timfile = pint.config.examplefile("NGC6440E.tim") m, t = get_model_and_toas(parfile, timfile) f = WLSFitter(t, m) f.fit_toas() bestfit = f.resids.chi2 F0 = np.linspace( f.model.F0.quantity - 3 * f.model.F0.uncertainty, f.model.F0.quantity + 3 * f.model.F0.uncertainty, 5, ) F1 = np.linspace( f.model.F1.quantity - 3 * f.model.F1.uncertainty, f.model.F1.quantity + 3 * f.model.F1.uncertainty, 7, ) chi2grid, extraparams = pint.gridutils.grid_chisq(f, ("F0", "F1"), (F0, F1), ("DM", ), ncpu=1) assert np.isclose(bestfit, chi2grid.min())
def test_grid_derived_extraparam_existingexecutor(): parfile = pint.config.examplefile("NGC6440E.par") timfile = pint.config.examplefile("NGC6440E.tim") m, t = get_model_and_toas(parfile, timfile) f = WLSFitter(t, m) f.fit_toas() bestfit = f.resids.chi2 F0 = np.linspace( f.model.F0.quantity - 3 * f.model.F0.uncertainty, f.model.F0.quantity + 3 * f.model.F0.uncertainty, 15, ) tau = np.linspace(8.1, 8.3, 13) * 100 * u.Myr with concurrent.futures.ProcessPoolExecutor(max_workers=ncpu) as executor: chi2grid_tau, params, extraparams = pint.gridutils.grid_chisq_derived( f, ("F0", "F1"), (lambda x, y: x, lambda x, y: -x / 2 / y), (F0, tau), ("DM", ), executor=executor, ) assert np.isclose(bestfit, chi2grid_tau.min(), atol=1)
def wls(): m = get_model(join(datadir, "NGC6440E.par")) t = get_TOAs(join(datadir, "NGC6440E.tim"), ephem="DE421") wls = WLSFitter(t, m) wls.fit_toas() return wls
def test_ECL_to_ICRS_uncertainties(): # start with ECL model, fit with both models # compare parameter values and uncertainties model_ECL = get_model(io.StringIO(modelstring_ECL)) toas = pint.simulation.make_fake_toas_uniform(MJDStart, MJDStop, NTOA, model=model_ECL, error=1 * u.us, add_noise=True) fit_ECL = WLSFitter(toas, model_ECL) fit_ICRS = WLSFitter(toas, model_ECL.as_ICRS()) fit_ECL.fit_toas() fit_ICRS.fit_toas() m1 = fit_ECL.model m2 = fit_ICRS.model.as_ECL() for p in ("ELONG", "ELAT", "PMELONG", "PMELAT"): assert np.isclose(m1.__getitem__(p).value, m2.__getitem__(p).value) # do a generous test here since the uncertainties could change assert np.isclose(m1.__getitem__(p).uncertainty, m2.__getitem__(p).uncertainty, rtol=0.5)
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 setUpClass(cls): os.chdir(datadir) cls.par = "B1855+09_NANOGrav_dfg+12_TAI_FB90.par" cls.tim = "B1855+09_NANOGrav_dfg+12.tim" cls.m = get_model(cls.par) cls.t = toa.get_TOAs(cls.tim, ephem="DE405") cls.f = WLSFitter(cls.t, cls.m) # set perturb parameter step cls.per_param = { "A1": 1e-05, "DECJ": 1e-06, "DMX_0003": 120, "ECC": 0.2, "F0": 1e-12, "F1": 0.001, "JUMP3": 10.0, "M2": 10.0, "OM": 1e-06, "PB": 1e-08, "PMDEC": 0.1, "PMRA": 0.1, "PX": 100, "RAJ": 1e-08, "SINI": -0.004075, "T0": 1e-10, }
def test_grid_oneparam(): parfile = pint.config.examplefile("NGC6440E.par") timfile = pint.config.examplefile("NGC6440E.tim") m, t = get_model_and_toas(parfile, timfile) f = WLSFitter(t, m) f.fit_toas() bestfit = f.resids.chi2 F0 = np.linspace( f.model.F0.quantity - 3 * f.model.F0.uncertainty, f.model.F0.quantity + 3 * f.model.F0.uncertainty, 5, ) chi2grid, _ = pint.gridutils.grid_chisq(f, ("F0", ), (F0, ), ncpu=ncpu) assert np.isclose(bestfit, chi2grid.min())
def setUpClass(cls): os.chdir(datadir) cls.par = "NGC6440E.par" cls.tim = "NGC6440E.tim" cls.m = get_model(cls.par) cls.t = toa.get_TOAs(cls.tim, ephem="DE421") cls.wls = WLSFitter(cls.t, cls.m) cls.gls = GLSFitter(cls.t, cls.m) cls.gls_full = GLSFitter(cls.t, cls.m)
def test_grid_3param_prefix_multiprocessor(): parfile = pint.config.examplefile("NGC6440E.par") timfile = pint.config.examplefile("NGC6440E.tim") m, t = get_model_and_toas(parfile, timfile) # add a F2 to the model modelcomponent = m.components["Spindown"] p = param.prefixParameter( parameter_type="float", name="F2", value=0, units=modelcomponent.F_unit(2), uncertainty=0, description=modelcomponent.F_description(2), longdouble=True, frozen=False, ) modelcomponent.add_param(p, setup=True) m.validate() f = WLSFitter(t, m) f.fit_toas() bestfit = f.resids.chi2 F0 = np.linspace( f.model.F0.quantity - 3 * f.model.F0.uncertainty, f.model.F0.quantity + 3 * f.model.F0.uncertainty, 3, ) F1 = np.linspace( f.model.F1.quantity - 3 * f.model.F1.uncertainty, f.model.F1.quantity + 3 * f.model.F1.uncertainty, 7, ) F2 = np.linspace( f.model.F2.quantity - 3 * f.model.F2.uncertainty, f.model.F2.quantity + 3 * f.model.F2.uncertainty, 5, ) chi2grid, _ = pint.gridutils.grid_chisq(f, ("F0", "F1", "F2"), (F0, F1, F2), ncpu=ncpu) assert np.isclose(bestfit, chi2grid.min())
def test_grid_derived_multiprocessor(): parfile = pint.config.examplefile("NGC6440E.par") timfile = pint.config.examplefile("NGC6440E.tim") m, t = get_model_and_toas(parfile, timfile) f = WLSFitter(t, m) f.fit_toas() bestfit = f.resids.chi2 F0 = np.linspace( f.model.F0.quantity - 3 * f.model.F0.uncertainty, f.model.F0.quantity + 3 * f.model.F0.uncertainty, 15, ) tau = np.linspace(8.1, 8.3, 13) * 100 * u.Myr chi2grid_tau, params, _ = pint.gridutils.grid_chisq_derived( f, ("F0", "F1"), (lambda x, y: x, lambda x, y: -x / 2 / y), (F0, tau), ncpu=ncpu) assert np.isclose(bestfit, chi2grid_tau.min(), atol=1)
def test_grid_oneparam_existingexecutor(): parfile = pint.config.examplefile("NGC6440E.par") timfile = pint.config.examplefile("NGC6440E.tim") m, t = get_model_and_toas(parfile, timfile) f = WLSFitter(t, m) f.fit_toas() bestfit = f.resids.chi2 F0 = np.linspace( f.model.F0.quantity - 5 * f.model.F0.uncertainty, f.model.F0.quantity + 5 * f.model.F0.uncertainty, 21, ) with concurrent.futures.ProcessPoolExecutor( max_workers=ncpu, ) as executor: chi2grid, _ = pint.gridutils.grid_chisq(f, ("F0", ), (F0, ), executor=executor) assert np.isclose(bestfit, chi2grid.min())
def test_tuple_fit(): parfile = pint.config.examplefile("NGC6440E.par") timfile = pint.config.examplefile("NGC6440E.tim") m, t = get_model_and_toas(parfile, timfile) f = WLSFitter(t, m) # find the best-fit f.fit_toas() bestfit = f.resids.chi2 F0 = np.linspace( f.model.F0.quantity - 3 * f.model.F0.uncertainty, f.model.F0.quantity + 3 * f.model.F0.uncertainty, 25, ) F1 = np.ones(len(F0)) * f.model.F1.quantity parnames = ("F0", "F1") parvalues = list(zip(F0, F1)) chi2, extra = pint.gridutils.tuple_chisq(f, parnames, parvalues, extraparnames=("DM", )) f.model.F0.quantity = F0[3] f.model.F1.quantity = F1[3] f.model.F0.frozen = True f.model.F1.frozen = True f.fit_toas() assert np.isclose(chi2[3], f.resids.calc_chi2()) assert np.isclose(chi2.min(), bestfit) assert np.isclose(extra["DM"][3], f.model.DM.quantity)
from pint.toa import get_TOAs import matplotlib.pyplot as plt from astropy.visualization import quantity_support quantity_support() import scipy.stats # %matplotlib inline # %% # Load in a basic dataset parfile = pint.config.examplefile("NGC6440E.par") timfile = pint.config.examplefile("NGC6440E.tim") m, t = get_model_and_toas(parfile, timfile) f = WLSFitter(t, m) # find the best-fit f.fit_toas() bestfit = f.resids.chi2 # %% [markdown] # What are the free parameters? # %% f.model.free_params # %% [markdown] # So we were fitting for RA, Dec, $F0$, $F1$, and $DM$. # # We'll do something like 3-sigma around the best-fit value of $F0$, fitting for RA, Dec, $F1$ and $DM$ at each grid point.
) print("Is 'P0' in the timing model: ", "P0" in model.params) print("Is 'P1' in the timing model: ", "P1" in model.params) print("Is 'F0' in the timing model: ", "F0" in model.params) print("Is 'F1' in the timing model: ", "F1" in model.params) # %% [markdown] # ### Load TOAs and prepare for fitting # %% from pint.fitter import WLSFitter from pint.toa import get_TOAs # %% toas = get_TOAs("NGC6440E.tim", ephem="DE421") f = WLSFitter(toas, model) # %% [markdown] # ### Plot the residuals # %% import matplotlib.pyplot as plt # %% [markdown] # ### Plot the prefit residuals. # %% plt.errorbar( toas.get_mjds().value, f.resids_init.time_resids.to_value(u.us), yerr=toas.get_errors().to_value(u.us),