Example #1
0
    def test_dm_jumps(self):
        # First get the toas for jump
        toa_backends, valid_flags = self.toas.get_flag_value("fe")
        toa_backends = np.array(toa_backends)
        all_backends = list(set(toa_backends))
        dm_jump_value = self.model.jump_dm(self.toas)
        dm_jump_params = [
            getattr(self.model, x) for x in self.model.params
            if (x.startswith("DMJUMP"))
        ]
        dm_jump_map = {}
        for dmj in dm_jump_params:
            dm_jump_map[dmj.key_value[0]] = dmj
        for be in all_backends:
            assert all(
                dm_jump_value[toa_backends == be] == -dm_jump_map[be].quantity)

        r = WidebandTOAResiduals(self.toas,
                                 self.model,
                                 dm_resid_args=dict(subtract_mean=False))

        model2 = deepcopy(self.model)
        for i, be in enumerate(all_backends):
            dm_jump_map[be].value += i + 1

        r2 = WidebandTOAResiduals(self.toas,
                                  model2,
                                  dm_resid_args=dict(subtract_mean=False))

        delta_dm = r2.dm.resids_value - r.dm.resids_value
        delta_dm_intended = np.zeros_like(delta_dm)
        for i, be in enumerate(all_backends):
            delta_dm_intended[toa_backends == be] = -(i + 1)
        assert np.allclose(delta_dm, delta_dm_intended)
Example #2
0
    def test_dm_jumps(self):
        # First get the toas for jump
        all_backends = list(set(self.toa_backends))
        dm_jump_value = self.model.jump_dm(self.toas)
        dm_jump_map = {}
        for dmj in self.dm_jump_params:
            dm_jump_map[dmj.key_value[0]] = dmj
        for be in all_backends:
            assert all(
                dm_jump_value[self.toa_backends == be] == -dm_jump_map[be].quantity
            )

        r = WidebandTOAResiduals(
            self.toas, self.model, dm_resid_args=dict(subtract_mean=False)
        )

        model2 = deepcopy(self.model)
        for i, be in enumerate(all_backends):
            dm_jump_map[be].value += i + 1

        r2 = WidebandTOAResiduals(
            self.toas, model2, dm_resid_args=dict(subtract_mean=False)
        )

        delta_dm = r2.dm.resids_value - r.dm.resids_value
        delta_dm_intended = np.zeros_like(delta_dm)
        for i, be in enumerate(all_backends):
            delta_dm_intended[self.toa_backends == be] = -(i + 1)
        assert np.allclose(delta_dm, delta_dm_intended)
Example #3
0
def test_wideband_chi2_null_updating(wideband_fake):
    toas, model = wideband_fake
    model.free_params = ["F0"]
    f = WidebandTOAFitter(toas, model)
    assert abs(f.fit_toas() - WidebandTOAResiduals(toas, model).chi2) > 1
    c2 = WidebandTOAResiduals(toas, f.model).chi2
    assert_allclose(f.fit_toas(), c2)
    c2 = WidebandTOAResiduals(toas, f.model).chi2
    assert_allclose(f.fit_toas(), c2)
Example #4
0
def test_wideband_residuals_dmjump(wb_model, wb_toas):
    r = WidebandTOAResiduals(wb_toas, wb_model, dm_resid_args=dict(subtract_mean=False))
    model = deepcopy(wb_model)
    assert wb_model.DMJUMP1.value == 0
    model.DMJUMP1.value = 10
    assert model.DMJUMP1.value == 10
    with pytest.raises(AttributeError):
        model.DMJUMP0
    with pytest.raises(AttributeError):
        model.DMJUMP2
    r2 = WidebandTOAResiduals(wb_toas, model, dm_resid_args=dict(subtract_mean=False))
    assert 0 < np.sum(r.dm.resids_value != r2.dm.resids_value) < len(r.dm.resids_value)
Example #5
0
def test_wideband_chi2_updating(wideband_fake):
    toas, model = wideband_fake
    model.free_params = ["F0"]
    model.F0.value += 1e-6
    c2 = WidebandTOAResiduals(
        toas, model, toa_resid_args=dict(track_mode="use_pulse_numbers")
    ).chi2
    f2 = WidebandTOAFitter(
        toas, model, additional_args=dict(toa=dict(track_mode="use_pulse_numbers"))
    )
    ftc2 = f2.fit_toas()
    assert abs(ftc2 - c2) > 100
    assert_allclose(f2.model.F0.value, 1)
    assert 1e-3 > abs(WidebandTOAResiduals(toas, f2.model).chi2 - ftc2) > 1e-5
    ftc2 = f2.fit_toas(maxiter=10)
    assert_allclose(WidebandTOAResiduals(toas, f2.model).chi2, ftc2)
Example #6
0
def test_residuals_wideband_chi2(wideband_fake):
    toas, model = wideband_fake
    r = WidebandTOAResiduals(toas, model)
    rn = Residuals(toas, model)
    f = WidebandTOAFitter(toas, model)
    assert_allclose(f.fit_toas(), r.chi2)
    assert f.fit_toas() >= rn.chi2
Example #7
0
def test_wideband_residuals_dof(wb_model, wb_toas_all):
    wb_model.free_params = ["DMJUMP1"]
    r = WidebandTOAResiduals(
        wb_toas_all, wb_model, dm_resid_args=dict(subtract_mean=False)
    )
    assert r.dof == 12 - 2
    assert_allclose(r.chi2, 2e14)
    assert_allclose(r.reduced_chi2, r.chi2 / r.dof)
Example #8
0
def test_residuals_fake_wideband():
    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, dm=10)
    r = WidebandTOAResiduals(toas, model)
    e = r.toa.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")
Example #9
0
def test_wideband_residuals(wb_model, wb_toas):
    r = WidebandTOAResiduals(wb_toas, wb_model, dm_resid_args=dict(subtract_mean=False))
    assert len(r.toa.time_resids) == len(wb_toas)
    assert len(r.dm.dm_data) < len(wb_toas)
Example #10
0
 def test_wideband_residuals(self):
     wb_res = WidebandTOAResiduals(toas=self.toa, model=self.model)
     assert wb_res.dof == 419
     # Make sure the model object are shared by all individual residual class
     assert wb_res.model is wb_res.toa.model
     assert wb_res.model is wb_res.dm.model