Exemplo n.º 1
0
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 = simulation.make_fake_toas_uniform(54200, 59000, 59000 - 54200,
                                             f.model)
    dphase, mrand = simulation.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 = 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()
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
def test_ftest_nb():
    """Test for narrowband fitter class F-test."""
    m = tm.get_model(os.path.join(datadir, "B1855+09_NANOGrav_9yv1.gls.par"))
    t = toa.get_TOAs(os.path.join(datadir, "B1855+09_NANOGrav_9yv1.tim"))
    f = fitter.WLSFitter(toas=t, model=m)
    f.fit_toas()
    # Test adding parameters
    FD4 = param.prefixParameter(parameter_type="float",
                                name="FD4",
                                value=0.0,
                                units=u.s,
                                frozen=False)
    ft = f.ftest(FD4, "FD", remove=False)
    assert isinstance(ft["ft"], float) or isinstance(ft["ft"], bool)
    # Test return the full output
    Ftest_dict = f.ftest(FD4, "FD", remove=False, full_output=True)
    assert isinstance(Ftest_dict["ft"], float) or isinstance(
        Ftest_dict["ft"], bool)
    # Test removing parameter
    FD3 = param.prefixParameter(parameter_type="float",
                                name="FD3",
                                value=0.0,
                                units=u.s,
                                frozen=False)
    ft = f.ftest(FD3, "FD", remove=True)
    assert isinstance(ft["ft"], float) or isinstance(ft["ft"], bool)
    Ftest_dict = f.ftest(FD3, "FD", remove=True, full_output=True)
    assert isinstance(Ftest_dict["ft"], float) or isinstance(
        Ftest_dict["ft"], bool)
Exemplo n.º 4
0
def test_zero_H3(toasJ0613):
    H3_zero_model = get_model(StringIO(simple_par))
    H3_zero_model.H3.value = 0.0
    H3_zero_model.H3.frozen = False
    assert H3_zero_model.H4.value != 0.0
    H3_zero_model.H4.frozen = False
    test_toas = toasJ0613[::20]
    with pytest.raises(ValueError):
        ff.WLSFitter(test_toas, H3_zero_model)
Exemplo n.º 5
0
def test_zero_H4(toasJ0613):
    H4_zero_model = get_model(StringIO(simple_par))
    H4_zero_model.H4.value = 0.0
    H4_zero_model.H4.frozen = False
    assert H4_zero_model.H3.value != 0.0
    H4_zero_model.H3.frozen = False
    test_toas = toasJ0613[::20]
    f = ff.WLSFitter(test_toas, H4_zero_model)
    f.fit_toas()
Exemplo n.º 6
0
def test_H3_and_H4_non_zero(toasJ0613):
    """Testing if the different H3, H4 combination breaks the fitting. the
    fitting result will not be checked here.
    """
    simple_model = get_model(StringIO(simple_par))

    test_toas = toasJ0613[::20]
    f = ff.WLSFitter(test_toas, simple_model)
    f.fit_toas()
Exemplo n.º 7
0
def test_CHI2():
    """Should be present after fit in PINT, not in TEMPO/TEMPO2"""
    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)
    assert "CHI2" in f.model.as_parfile()
    assert not ("CHI2" in f.model.as_parfile(format="tempo2"))
    assert not ("CHI2" in f.model.as_parfile(format="tempo"))
Exemplo n.º 8
0
def test_no_H3_H4(toasJ0613):
    """Test no H3 and H4 in model."""
    no_H3_H4 = simple_par.replace("H4 2.0262048E-7  1       1.1276173E-7", "")
    no_H3_H4 = no_H3_H4.replace("H3 2.7507208E-7  1       1.5114416E-7", "")
    no_H3_H4_model = get_model(StringIO(no_H3_H4))
    assert no_H3_H4_model.H3.value is None
    assert no_H3_H4_model.H4.value is None
    test_toas = toasJ0613[::20]
    f = ff.WLSFitter(test_toas, no_H3_H4_model)
    f.fit_toas()
Exemplo n.º 9
0
def test_fitter_basic():
    m = tm.get_model(os.path.join(datadir, "NGC6440E.par"))
    m.fit_params = ["F0", "F1"]
    e = 1 * u.us
    t = simulation.make_fake_toas_uniform(56000, 59000, 16, m, error=e)

    T = (t.last_MJD - t.first_MJD).to(u.s)

    dF0 = (e * m.F0.quantity / T).to(u.Hz)

    f_1 = fitter.WLSFitter(toas=t, model=m)
    f_1.fit_toas()
    assert abs(f_1.model.F0 - m.F0) < dF0

    m_2 = deepcopy(m)
    m_2.F0.quantity += 2 * dF0
    assert abs(m_2.F0 - m.F0) > dF0
    f_2 = fitter.WLSFitter(toas=t, model=m_2)
    f_2.fit_toas()
    assert abs(f_2.model.F0 - m.F0) < dF0
Exemplo n.º 10
0
def test_zero_H3_H4_fit_H3(toasJ0613):
    H3H4_zero2_model = get_model(StringIO(simple_par))
    H3H4_zero2_model.H3.value = 0.0
    H3H4_zero2_model.H3.frozen = False
    H3H4_zero2_model.H4.value = 0.0
    H3H4_zero2_model.H4.frozen = True
    test_toas = toasJ0613[::20]
    f = ff.WLSFitter(test_toas, H3H4_zero2_model)
    # This should work
    f.fit_toas()
    assert f.model.H3.value > 0.0
Exemplo n.º 11
0
def test_fitsummary_binary():
    """Test fitter print_summary() when an ELL1 binary is fit"""
    par = os.path.join(datadir, "B1855+09_NANOGrav_12yv3.wb.gls.par")
    tim = os.path.join(datadir, "B1855+09_NANOGrav_dfg+12.tim")

    m, t = get_model_and_toas(par, tim)

    f = fitter.WLSFitter(t, m)
    f.model.free_params = ["PB", "A1", "SINI"]
    f.fit_toas()
    f.print_summary()
Exemplo n.º 12
0
def test_dmxparse():
    """Test for dmxparse function."""
    m = tm.get_model(os.path.join(datadir, "B1855+09_NANOGrav_9yv1.gls.par"))
    t = toa.get_TOAs(os.path.join(datadir, "B1855+09_NANOGrav_9yv1.tim"))
    f = fitter.GLSFitter(toas=t, model=m)
    f.fit_toas()
    dmx = dmxparse(f, save=False)
    # Check exception handling
    m = tm.get_model(os.path.join(datadir, "B1855+09_NANOGrav_dfg+12_DMX.par"))
    t = toa.get_TOAs(os.path.join(datadir, "B1855+09_NANOGrav_dfg+12.tim"))
    f = fitter.WLSFitter(toas=t, model=m)
    f.fit_toas()
    dmx = dmxparse(f, save=False)
Exemplo n.º 13
0
def test_fitter():
    # Get model

    m = tm.get_model(os.path.join(datadir, "NGC6440E.par"))

    # Get TOAs
    t = toa.TOAs(os.path.join(datadir, "NGC6440E.tim"))
    t.apply_clock_corrections(include_bipm=False)
    t.compute_TDBs()
    try:
        planet_ephems = m.PLANET_SHAPIRO.value
    except AttributeError:
        planet_ephems = False
    t.compute_posvels(planets=planet_ephems)

    f = fitter.WLSFitter(toas=t, model=m)

    # Print initial chi2
    print("chi^2 is initially %0.2f" % f.resids.chi2)

    # Plot initial residuals
    xt = f.resids.toas.get_mjds().value
    yerr = t.get_errors() * 1e-6
    plt.close()
    p1 = plt.errorbar(
        xt,
        f.resids.toas.get_mjds().value,
        f.resids.time_resids.value,
        yerr.value,
        fmt="bo",
    )

    # Do a 4-parameter fit
    f.set_fitparams("F0", "F1", "RA", "DEC")
    f.fit_toas()
    print("chi^2 is %0.2f after 4-param fit" % f.resids.chi2)
    p2 = plt.errorbar(xt, f.resids.time_resids.value, yerr.value, fmt="go")

    # Now perturb F1 and fit only that. This doesn't work, though tempo2 easily fits
    # it.
    f.model.F1.value = 1.1 * f.model.F1.value
    f.fit_toas()
    print("chi^2 is %0.2f after perturbing F1" % f.resids.chi2)
    p3 = plt.errorbar(xt, f.resids.time_resids.value, yerr.value, fmt="ms")

    f.set_fitparams("F1")
    f.fit_toas()
    print('chi^2 is %0.2f after fitting just F1 with default method="Powell"' %
          f.resids.chi2)
    p4 = plt.errorbar(xt, f.resids.time_resids.value, yerr.value, fmt="k^")
Exemplo n.º 14
0
def test_zero_H3_H4_fit_H3_H4(toasJ0613):
    H3H4_zero_model = get_model(StringIO(simple_par))
    H3H4_zero_model.H3.value = 0.0
    H3H4_zero_model.H4.value = 0.0
    H3H4_zero_model.H3.frozen = False
    H3H4_zero_model.H4.frozen = False
    # Make sure H3's derivative does not return zero.
    test_toas = toasJ0613[::20]
    d_h3 = H3H4_zero_model.d_delay_d_param(test_toas, "H3")
    assert d_h3.mean().value != 0.0
    assert d_h3.std().value != 0.0
    f = ff.WLSFitter(test_toas, H3H4_zero_model)
    f.fit_toas()
    assert f.model.H3.value > 0.0
Exemplo n.º 15
0
            for i in range(MAXIT):
                start = timeit.default_timer()
                toa.get_TOAs(tim,
                             planets=use_planets,
                             ephem=model_ephem,
                             usepickle=False)
                end = timeit.default_timer()
                total = total + (end - start)
            getTOAs_nopickle.append(total / MAXIT)

            t = toa.get_TOAs(tim,
                             planets=use_planets,
                             ephem=model_ephem,
                             usepickle=True)  # to use in timing fitter

            f = fitter.WLSFitter(t, m)

            print("timing fitter...")
            total = 0
            for i in range(MAXIT):
                start = timeit.default_timer()
                f.fit_toas()
                end = timeit.default_timer()
                total = total + (end - start)
            fittimes.append(total / MAXIT)

            # pickle time of get_TOAs
            print("timing get_TOAs w/ pickling...")
            total = 0
            for i in range(MAXIT):
                start = timeit.default_timer()
Exemplo n.º 16
0
           linewidth=0.5,
           color='red')
ax3.vlines(57575,
           np.min(prefit_residuals.time_resids.to(units.us)),
           np.max(prefit_residuals.time_resids.to(units.us)),
           linewidth=0.5,
           color='red')
ax3.set_title("%s Pre-fit Timing Residuals" % model.PSR.value)
ax3.set_xlabel("MJD")
ax3.set_ylabel("Residual ($\mu$s)")
plt.legend()
plt.savefig('Pre_fit_residuals_Graham.pdf')
plt.close()

# Perform the fit
WLS_fit = fitter.WLSFitter(TOAs, model)
WLS_fit.set_fitparams('F0', 'F1', 'RAJ', 'DECJ', 'JUMP1', 'JUMP2', 'JUMP3')
WLS_fit.fit_toas()

fig4 = plt.figure()
ax4 = plt.gca()

# Plot the post-fit residuals
ax4.errorbar(TOAs.get_mjds().value,
             WLS_fit.resids.time_resids.to(units.us),
             yerr=TOAs.get_errors().to(units.us),
             color='black',
             elinewidth=0.05,
             capsize=0,
             marker='o',
             markersize=0.8,
Exemplo n.º 17
0
modelin.WAVE1.quantity = (0.0 * u.s, 0.0 * u.s)
modelin.WAVE2.quantity = (0.0 * u.s, 0.0 * u.s)
modelin.WAVE3.quantity = (0.0 * u.s, 0.0 * u.s)
modelin.WAVE4.quantity = (0.0 * u.s, 0.0 * u.s)
modelin.WAVE5.quantity = (0.0 * u.s, 0.0 * u.s)

modelin.WAVE1.frozen = False
modelin.WAVE2.frozen = False
modelin.WAVE3.frozen = False
modelin.WAVE4.frozen = False
modelin.WAVE5.frozen = False

prefitresids = Residuals(realtoas, modelin)

try:
    f = fit.WLSFitter(realtoas, modelin)
    f.fit_toas()
    fig, ax = plt.subplots(figsize=(16, 9))
    ax.set_title("Residuals using PN from compute_pulse_numbers")
    ax.errorbar(
        prefitresids.toas.get_mjds(),
        prefitresids.time_resids,
        yerr=prefitresids.toas.get_errors(),
        fmt=".",
        label="Prefit",
    )
    ax.errorbar(
        f.resids.toas.get_mjds(),
        f.resids.time_resids,
        yerr=f.resids.toas.get_errors(),
        fmt=".",
Exemplo n.º 18
0
def test_fitter():
    # Get model

    m = tm.get_model(os.path.join(datadir, "NGC6440E.par"))

    # Get TOAs
    t = toa.TOAs(os.path.join(datadir, "NGC6440E.tim"))
    t.apply_clock_corrections(include_bipm=False)
    t.compute_TDBs()
    try:
        planet_ephems = m.PLANET_SHAPIRO.value
    except AttributeError:
        planet_ephems = False
    t.compute_posvels(planets=planet_ephems)

    f = fitter.WLSFitter(toas=t, model=m)

    # Print initial chi2
    print("chi^2 is initially %0.2f" % f.resids.chi2)

    # Plot initial residuals
    xt = f.resids.toas.get_mjds().value
    yerr = t.get_errors() * 1e-6
    plt.close()
    p1 = plt.errorbar(
        xt,
        f.resids.toas.get_mjds().value,
        f.resids.time_resids.value,
        yerr.value,
        fmt="bo",
    )

    # Do a 4-parameter fit
    f.model.free_params = ("F0", "F1", "RAJ", "DECJ")
    f.fit_toas()

    # Check the number of degrees of freedom in the fit.
    # Fitting the 4 params above, plus the 1 implicit global offset = 5 free parameters.
    # NTOA = 62, so DOF = 62 - 5 = 57
    assert f.resids.dof == 57

    print("chi^2 is %0.2f after 4-param fit" % f.resids.chi2)
    p2 = plt.errorbar(xt, f.resids.time_resids.value, yerr.value, fmt="go")

    # Make sure the summary printing works
    f.print_summary()

    # Try a few utils

    # Now perturb F1 and fit only that. This doesn't work, though tempo2 easily fits
    # it.
    f.model.F1.value = 1.1 * f.model.F1.value
    f.fit_toas()
    print("chi^2 is %0.2f after perturbing F1" % f.resids.chi2)
    p3 = plt.errorbar(xt, f.resids.time_resids.value, yerr.value, fmt="ms")

    f.model.free_params = ["F1"]
    f.fit_toas()
    print('chi^2 is %0.2f after fitting just F1 with default method="Powell"' %
          f.resids.chi2)
    p4 = plt.errorbar(xt, f.resids.time_resids.value, yerr.value, fmt="k^")
Exemplo n.º 19
0
    def test_fitting_2comp(self):
        f_1 = fitter.WLSFitter(toas=self.t, model=self.m2)

        f_1.fit_toas()