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(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 test_dm_barycentered():
    model = get_model(io.StringIO(par_base))
    toas = make_fake_toas(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(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 = "FB90"
    model.T2CMETHOD.value = "IERS2000B"
    toas = make_fake_toas(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 *IERS2000B", 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.0", par_out)
    assert re.search(r"CHI2 *\d+.\d+", par_out)
    assert re.search(r"EPHEM *DE421", par_out)
    assert re.search(r"DMDATA *0.0", par_out)
    assert re.search(r"START *58000.0", par_out)
    assert re.search(r"FINISH *59000.0", par_out)
def test_jump_everything_wideband():
    model = get_model(
        io.StringIO("\n".join([par_base, "JUMP TEL barycenter 0"])))
    toas = make_fake_toas(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)
示例#5
0
    def fit(self, iters=1):
        '''
        Run a fit using the specified fitter
        '''
        if self.fitted:
            self.prefit_model = self.postfit_model
            self.prefit_resids = self.postfit_resids

        if self.fitter == Fitters.POWELL:
            fitter = pint.fitter.PowellFitter(self.toas, self.prefit_model)
        elif self.fitter == Fitters.WLS:
            fitter = pint.fitter.WlsFitter(self.toas, self.prefit_model)
        elif self.fitter == Fitters.GLS:
            fitter = pint.fitter.GLSFitter(self.toas, self.prefit_model)
        chi2 = self.prefit_resids.chi2
        wrms = np.sqrt(chi2 / self.toas.ntoas)
        print('Pre-Fit Chi2:\t\t%.8g us^2' % chi2)
        print('Pre-Fit Weighted RMS:\t%.8g us' % wrms)

        fitter.fit_toas(maxiter=1)
        self.postfit_model = fitter.model
        self.postfit_resids = pint.residuals.resids(self.toas,
                                                    self.postfit_model)
        self.fitted = True
        self.write_fit_summary()
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(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_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_jump_no_toas():
    model = get_model(io.StringIO("\n".join([par_base, "JUMP -fe L_wide 0"])))
    toas = make_fake_toas(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_jump_everything(Fitter):
    model = get_model(io.StringIO("\n".join([par_base, "JUMP TEL barycenter 0"])))
    toas = make_fake_toas(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)
示例#10
0
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(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()
示例#11
0
    def test_START_FINISH_in_par(self):
        """ 
        Check that START/FINISH parameters set up/operate properly when 
        from input file.
        """
        m1 = self.m
        t1 = get_TOAs("B1855+09_NANOGrav_dfg+12.tim")

        start_preval = 53358.726464889485214
        finish_preval = 55108.922917417192366
        start_postval = 53358.7274648895  # from Tempo2
        finish_postval = 55108.9219174172  # from Tempo2

        # check parameter initialization
        assert hasattr(m1, "START")
        assert type(m1.START) == type(MJDParameter())
        assert hasattr(m1, "FINISH")
        assert type(m1.FINISH) == type(MJDParameter())

        self.assertEqual(m1.START.value, start_preval)
        self.assertEqual(m1.FINISH.value, finish_preval)
        self.assertEqual(m1.START.frozen, True)
        self.assertEqual(m1.FINISH.frozen, True)

        # fit toas and compare with expected/Tempo2 (for WLS) values
        fitters = [
            pint.fitter.PowellFitter(toas=t1, model=m1),
            pint.fitter.WLSFitter(toas=t1, model=m1),
            pint.fitter.GLSFitter(toas=t1, model=m1),
        ]
        for fitter in fitters:
            fitter.fit_toas()
            self.assertEqual(m1.START.frozen, True)
            self.assertEqual(m1.FINISH.frozen, True)
            if fitter.method == "weighted_least_square":
                self.assertAlmostEqual(
                    fitter.model.START.value, start_postval, places=9
                )
                self.assertAlmostEqual(
                    fitter.model.FINISH.value, finish_postval, places=9
                )
            self.assertAlmostEqual(
                fitter.model.START.value, fitter.toas.first_MJD.value, places=9
            )
            self.assertAlmostEqual(
                fitter.model.FINISH.value, fitter.toas.last_MJD.value, places=9
            )
def test_null_vector(Fitter):
    model = get_model(io.StringIO("\n".join([par_base])))
    model.free_params = ["ELONG", "ELAT"]
    toas = make_fake_toas(
        58000,
        58900,
        10,
        model,
        obs="barycenter",
        freq=1400.0,
        dm=15.0 * u.pc / u.cm ** 3,
        dm_error=1e-4 * u.pc * u.cm ** -3,
    )
    fitter = Fitter(toas, model)
    with pytest.warns(
        pint.fitter.DegeneracyWarning,
        match=r".*degeneracy.*following parameter.*ELONG\b",
    ):
        fitter.fit_toas(threshold=1e-14)
    for p in fitter.model.free_params:
        assert not np.isnan(fitter.model[p].value)
示例#13
0
    def test_START_FINISH_not_in_par(self):
        """
        Check that START/FINISH parameters are added and set up when not 
        in input file.
        """
        # check initialization after fitting for .par file without START/FINISH
        m = get_model("NGC6440E.par")
        t = get_TOAs("NGC6440E.tim")

        start_postval = 53478.2858714192  # from Tempo2
        finish_postval = 54187.5873241699  # from Tempo2

        self.assertTrue(hasattr(m, "START"))
        self.assertTrue(hasattr(m, "FINISH"))

        # fit toas and compare with expected/Tempo2 (for WLS) values
        fitters = [
            pint.fitter.PowellFitter(toas=t, model=m),
            pint.fitter.WLSFitter(toas=t, model=m),
            pint.fitter.GLSFitter(toas=t, model=m),
        ]
        for fitter in fitters:
            fitter.fit_toas()
            self.assertTrue(hasattr(fitter.model, "START"))
            self.assertTrue(hasattr(fitter.model, "FINISH"))
            self.assertEqual(fitter.model.START.frozen, True)
            self.assertEqual(fitter.model.FINISH.frozen, True)
            if fitter.method == "weighted_least_square":
                self.assertAlmostEqual(
                    fitter.model.START.value, start_postval, places=9
                )
                self.assertAlmostEqual(
                    fitter.model.FINISH.value, finish_postval, places=9
                )
            self.assertAlmostEqual(
                fitter.model.START.value, fitter.toas.first_MJD.value, places=9
            )
            self.assertAlmostEqual(
                fitter.model.FINISH.value, fitter.toas.last_MJD.value, places=9
            )
示例#14
0
文件: pulsar.py 项目: demorest/PINT
    def fit(self, iters=1):
        '''
        Run a fit using the specified fitter
        '''
        if self.fitted:
            self.prefit_model = self.postfit_model
            self.prefit_resids = self.postfit_resids

        if self.fitter == Fitters.POWELL:
            fitter = pint.fitter.PowellFitter(self.toas, self.prefit_model)
        elif self.fitter == Fitters.WLS:
            fitter = pint.fitter.WlsFitter(self.toas, self.prefit_model)
        elif self.fitter == Fitters.GLS:
            fitter = pint.fitter.GLSFitter(self.toas, self.prefit_model)
        chi2 = self.prefit_resids.chi2
        wrms = np.sqrt(chi2 / self.toas.ntoas)
        print('Pre-Fit Chi2:\t\t%.8g us^2' % chi2)
        print('Pre-Fit Weighted RMS:\t%.8g us' % wrms)
        
        fitter.fit_toas(maxiter=1)
        self.postfit_model = fitter.model
        self.postfit_resids = pint.residuals.resids(self.toas, self.postfit_model)
        self.fitted = True
        self.write_fit_summary()
示例#15
0
    def fit(self, selected, iters=1):
        """
        Run a fit using the specified fitter
        """
        # Select all the TOAs if none are explicitly set
        if not any(selected):
            selected = ~selected

        if self.fitted:
            self.prefit_model = self.postfit_model
            self.prefit_resids = self.postfit_resids

        if self.fitter == Fitters.POWELL:
            fitter = pint.fitter.PowellFitter(self.selected_toas, self.prefit_model)
        elif self.fitter == Fitters.WLS:
            fitter = pint.fitter.WLSFitter(self.selected_toas, self.prefit_model)
        elif self.fitter == Fitters.GLS:
            fitter = pint.fitter.GLSFitter(self.selected_toas, self.prefit_model)
        chi2 = self.prefit_resids.chi2
        wrms = self.prefit_resids.rms_weighted()
        print("Pre-Fit Chi2:\t\t%.8g" % chi2)
        print("Pre-Fit Weighted RMS:\t%.8g us" % wrms.to(u.us).value)

        fitter.fit_toas(maxiter=1)
        self.postfit_model = fitter.model
        self.postfit_resids = Residuals(self.all_toas, self.postfit_model)
        self.fitted = True
        self.write_fit_summary()

        # TODO: delta_pulse_numbers need some work. They serve both for PHASE and -padd functions from the TOAs
        # as well as for phase jumps added manually in the GUI. They really should not be zeroed out here because
        # that will wipe out preexisting values
        self.all_toas.table["delta_pulse_numbers"] = np.zeros(self.all_toas.ntoas)
        self.selected_toas.table["delta_pulse_number"] = np.zeros(
            self.selected_toas.ntoas
        )

        # plot the prefit without jumps
        pm_no_jumps = copy.deepcopy(self.prefit_model)
        for param in pm_no_jumps.params:
            if param.startswith("JUMP"):
                getattr(pm_no_jumps, param).value = 0.0
                getattr(pm_no_jumps, param).frozen = True
        self.prefit_resids_no_jumps = Residuals(self.selected_toas, pm_no_jumps)

        f = copy.deepcopy(fitter)
        no_jumps = [
            False if "jump" in dict.keys() else True for dict in f.toas.table["flags"]
        ]
        f.toas.select(no_jumps)

        selectedMJDs = self.selected_toas.get_mjds()
        if all(no_jumps):
            q = list(self.all_toas.get_mjds())
            index = q.index(
                [i for i in self.all_toas.get_mjds() if i > selectedMJDs.min()][0]
            )
            rs_mean = (
                Residuals(self.all_toas, f.model)
                .phase_resids[index : index + len(selectedMJDs)]
                .mean()
            )
        else:
            rs_mean = self.prefit_resids_no_jumps.phase_resids[no_jumps].mean()

        # determines how far on either side fake toas go
        # TODO: hard limit on how far fake toas can go --> can get clkcorr
        # errors if go before GBT existed, etc.
        minMJD, maxMJD = selectedMJDs.min(), selectedMJDs.max()
        spanMJDs = maxMJD - minMJD
        if spanMJDs < 30 * u.d:
            redge = ledge = 4
            npoints = 400
        elif spanMJDs < 90 * u.d:
            redge = ledge = 2
            npoints = 300
        elif spanMJDs < 200 * u.d:
            redge = ledge = 1
            npoints = 300
        elif spanMJDs < 400 * u.d:
            redge = ledge = 0.5
            npoints = 200
        else:
            redge = ledge = 1.0
            npoints = 250
        # Check to see if too recent
        nowish = (Time.now().mjd - 40) * u.d
        if maxMJD + spanMJDs * redge > nowish:
            redge = (nowish - maxMJD) / spanMJDs
            if redge < 0.0:
                redge = 0.0
        f_toas = make_fake_toas_uniform(
            minMJD - spanMJDs * ledge, maxMJD + spanMJDs * redge, npoints, f.model
        )
        rs = calculate_random_models(
            f, f_toas, Nmodels=10, keep_models=False, return_time=True
        )

        # subtract the mean residual of each random model from the respective residual set
        # based ONLY on the mean of the random residuals in the real data range
        start_index = np.where(abs(f_toas.get_mjds() - minMJD) < 1 * u.d)
        end_index = np.where(abs(f_toas.get_mjds() - maxMJD) < 1 * u.d)
        for i in range(len(rs)):
            # use start_index[0][0] since np.where returns np.array([], dtype), extract index from list in array
            rs_mean = rs[i][start_index[0][0] : end_index[0][0]].mean()
            rs[i][:] = [resid - rs_mean for resid in rs[i]]

        self.random_resids = rs
        self.fake_toas = f_toas
示例#16
0
    def fit(self, selected, iters=1):
        """
        Run a fit using the specified fitter
        """
        # Select all the TOAs if none are explicitly set
        if not any(selected):
            selected = ~selected
        """JUMP check, TODO: put in fitter?"""
        if "PhaseJump" in self.prefit_model.components:
            # if attempted fit (selected)
            # A) contains only jumps, don't do the fit and return an error
            # B) excludes a jump, turn that jump off
            # C) partially contains a jump, redefine that jump only with the overlap
            fit_jumps = []
            for param in self.prefit_model.params:
                if getattr(self.prefit_model,
                           param).frozen == False and param.startswith("JUMP"):
                    fit_jumps.append(int(param[4:]))

            numjumps = self.prefit_model.components[
                "PhaseJump"].get_number_of_jumps()
            if numjumps == 0:
                log.warn(
                    "There are no jumps (maskParameter objects) in PhaseJump. Please delete the PhaseJump object and try again. "
                )
                return None
            # boolean array to determine if all selected toas are jumped
            jumps = [
                True if "jump" in dict.keys() and dict["jump"] in fit_jumps
                else False for dict in self.all_toas.table["flags"][selected]
            ]
            # check if par file jumps in PhaseJump object
            if not any(jumps):
                # for every jump, set appropriate flag for TOAs it jumps
                for jump_par in self.prefit_model.components[
                        "PhaseJump"].get_jump_param_objects():
                    # find TOAs jump applies to
                    mask = jump_par.select_toa_mask(self.all_toas)
                    # apply to dictionaries for future use
                    for dict in self.all_toas.table["flags"][mask]:
                        dict["jump"] = jump_par.index
                jumps = [
                    True if "jump" in dict.keys() and dict["jump"] in fit_jumps
                    else False
                    for dict in self.all_toas.table["flags"][selected]
                ]
            if all(jumps):
                log.warn(
                    "toas being fit must not all be jumped. Remove or uncheck at least one jump in the selected toas before fitting."
                )
                return None
            # numerical array of selected jump flags
            sel_jump_nums = [
                dict["jump"] if "jump" in dict.keys() else np.nan
                for dict in self.all_toas.table["flags"][selected]
            ]
            # numerical array of all jump flags
            full_jump_nums = [
                dict["jump"] if "jump" in dict.keys() else np.nan
                for dict in self.all_toas.table["flags"]
            ]
            for num in range(1, numjumps + 1):
                num = int(num)
                if num not in sel_jump_nums:
                    getattr(self.prefit_model, "JUMP" + str(num)).frozen = True
                    continue
                jump_select = [num == jump_num for jump_num in full_jump_nums]
                overlap = [a and b for a, b in zip(jump_select, selected)]
                # remove the jump flags for that num
                for dict in self.all_toas.table["flags"]:
                    if "jump" in dict.keys() and dict["jump"] == num:
                        del dict["jump"]
                # re-add the jump using overlap as 'selected'
                for dict in self.all_toas.table["flags"][overlap]:
                    dict["jump"] = num

        if self.fitted:
            self.prefit_model = self.postfit_model
            self.prefit_resids = self.postfit_resids

        if self.fitter == Fitters.POWELL:
            fitter = pint.fitter.PowellFitter(self.selected_toas,
                                              self.prefit_model)
        elif self.fitter == Fitters.WLS:
            fitter = pint.fitter.WLSFitter(self.selected_toas,
                                           self.prefit_model)
        elif self.fitter == Fitters.GLS:
            fitter = pint.fitter.GLSFitter(self.selected_toas,
                                           self.prefit_model)
        chi2 = self.prefit_resids.chi2
        wrms = np.sqrt(chi2 / self.selected_toas.ntoas)
        print("Pre-Fit Chi2:\t\t%.8g us^2" % chi2)
        print("Pre-Fit Weighted RMS:\t%.8g us" % wrms)

        fitter.fit_toas(maxiter=1)
        self.postfit_model = fitter.model
        self.postfit_resids = Residuals(self.all_toas, self.postfit_model)
        self.fitted = True
        self.write_fit_summary()

        # TODO: delta_pulse_numbers need some work. They serve both for PHASE and -padd functions from the TOAs
        # as well as for phase jumps added manually in the GUI. They really should not be zeroed out here because
        # that will wipe out preexisting values
        self.fulltoas.table["delta_pulse_numbers"] = np.zeros(
            self.fulltoas.ntoas)
        self.selected_toas.table["delta_pulse_number"] = np.zeros(
            self.selected_toas.ntoas)

        # plot the prefit without jumps
        pm_no_jumps = copy.deepcopy(self.postfit_model)
        for param in pm_no_jumps.params:
            if param.startswith("JUMP"):
                getattr(pm_no_jumps, param).value = 0.0
                getattr(pm_no_jumps, param).frozen = True
        self.prefit_resids_no_jumps = Residuals(self.selected_toas,
                                                pm_no_jumps)

        f = copy.deepcopy(fitter)
        no_jumps = [
            False if "jump" in dict.keys() else True
            for dict in f.toas.table["flags"]
        ]
        f.toas.select(no_jumps)

        selectedMJDs = self.selected_toas.get_mjds()
        if all(no_jumps):
            q = list(self.all_toas.get_mjds())
            index = q.index([
                i for i in self.all_toas.get_mjds() if i > selectedMJDs.min()
            ][0])
            rs_mean = (Residuals(
                self.all_toas,
                f.model).phase_resids[index:index + len(selectedMJDs)].mean())
        else:
            rs_mean = self.prefit_resids_no_jumps.phase_resids[no_jumps].mean()

        # determines how far on either side fake toas go
        # TODO: hard limit on how far fake toas can go --> can get clkcorr
        # errors if go before GBT existed, etc.
        minMJD, maxMJD = selectedMJDs.min(), selectedMJDs.max()
        spanMJDs = maxMJD - minMJD
        if spanMJDs < 30 * u.d:
            redge = ledge = 4
            npoints = 400
        elif spanMJDs < 90 * u.d:
            redge = ledge = 2
            npoints = 300
        elif spanMJDs < 200 * u.d:
            redge = ledge = 1
            npoints = 300
        elif spanMJDs < 400 * u.d:
            redge = ledge = 0.5
            npoints = 200
        else:
            redge = ledge = 0.2
            npoints = 150
        # Check to see if too recent
        nowish = (Time.now().mjd - 40) * u.d
        if maxMJD + spanMJDs * redge > nowish:
            redge = (nowish - maxMJD) / spanMJDs
            if redge < 0.0:
                redge = 0.0
        f_toas, rs, mrands = random_models(
            f,
            rs_mean=rs_mean,
            redge_multiplier=redge,
            ledge_multiplier=ledge,
            npoints=npoints,
            iter=10,
        )
        self.random_resids = rs
        self.fake_toas = f_toas
示例#17
0
    def fit(self, selected, iters=1):
        """
        Run a fit using the specified fitter
        """
        # Select all the TOAs if none are explicitly set
        if not any(selected):
            selected = ~selected
        """JUMP check, TODO: put in fitter?"""
        if "PhaseJump" in self.prefit_model.components:
            # if attempted fit (selected)
            # A) contains only jumps, don't do the fit and return an error
            # B) excludes a jump, turn that jump off
            # C) partially contains a jump, redefine that jump only with the overlap
            fit_jumps = []
            for param in self.prefit_model.params:
                if getattr(self.prefit_model,
                           param).frozen == False and param.startswith("JUMP"):
                    fit_jumps.append(int(param[4:]))
            jumps = [
                True if "jump" in dict.keys() and dict["jump"] in fit_jumps
                else False for dict in self.selected_toas.table["flags"]
            ]
            if all(jumps):
                log.warn(
                    "toas being fit must not all be jumped. Remove or uncheck at least one jump in the selected toas before fitting."
                )
                return None
            sel_jump_nums = [
                dict["jump"] if "jump" in dict.keys() else np.nan
                for dict in self.selected_toas.table["flags"]
            ]
            full_jump_nums = [
                dict["jump"] if "jump" in dict.keys() else np.nan
                for dict in self.all_toas.table["flags"]
            ]
            for num in range(1, int(np.nanmax(full_jump_nums) + 1)):
                num = int(num)
                if num not in sel_jump_nums:
                    getattr(self.prefit_model, "JUMP" + str(num)).frozen = True
                    continue
                jump_select = [num == jump_num for jump_num in full_jump_nums]
                overlap = [a and b for a, b in zip(jump_select, selected)]
                # remove the jump flags for that num
                for dict in self.all_toas.table["flags"]:
                    if "jump" in dict.keys() and dict["jump"] == num:
                        del dict["jump"]
                # re-add the jump using overlap as 'selected'
                for dict in self.all_toas.table["flags"][overlap]:
                    dict["jump"] = num

        if self.fitted:
            self.prefit_model = self.postfit_model
            self.prefit_resids = self.postfit_resids

        if self.fitter == Fitters.POWELL:
            fitter = pint.fitter.PowellFitter(self.selected_toas,
                                              self.prefit_model)
        elif self.fitter == Fitters.WLS:
            fitter = pint.fitter.WLSFitter(self.selected_toas,
                                           self.prefit_model)
        elif self.fitter == Fitters.GLS:
            fitter = pint.fitter.GLSFitter(self.selected_toas,
                                           self.prefit_model)
        chi2 = self.prefit_resids.chi2
        wrms = np.sqrt(chi2 / self.selected_toas.ntoas)
        print("Pre-Fit Chi2:\t\t%.8g us^2" % chi2)
        print("Pre-Fit Weighted RMS:\t%.8g us" % wrms)

        fitter.fit_toas(maxiter=1)
        self.postfit_model = fitter.model
        self.postfit_resids = Residuals(self.all_toas,
                                        self.postfit_model,
                                        set_pulse_nums=True)
        self.fitted = True
        self.write_fit_summary()

        # TODO: set pulse nums above not working to reset delta pulse nums, have to force it here
        # self.fulltoas.table['delta_pulse_numbers'] = np.zeros(self.fulltoas.ntoas)
        self.selected_toas.table["delta_pulse_number"] = np.zeros(
            self.selected_toas.ntoas)

        # plot the prefit without jumps
        pm_no_jumps = copy.deepcopy(self.postfit_model)
        for param in pm_no_jumps.params:
            if param.startswith("JUMP"):
                getattr(pm_no_jumps, param).value = 0.0
                getattr(pm_no_jumps, param).frozen = True
        self.prefit_resids_no_jumps = Residuals(self.all_toas,
                                                pm_no_jumps,
                                                set_pulse_nums=True)

        f = copy.deepcopy(fitter)
        no_jumps = [
            False if "jump" in dict.keys() else True
            for dict in f.toas.table["flags"]
        ]
        f.toas.select(no_jumps)

        selectedMJDs = self.selected_toas.get_mjds()
        if all(no_jumps):
            q = list(self.all_toas.get_mjds())
            index = q.index([
                i for i in self.all_toas.get_mjds() if i > selectedMJDs.min()
            ][0])
            rs_mean = (Residuals(
                self.all_toas, f.model,
                set_pulse_nums=True).phase_resids[index:index +
                                                  len(selectedMJDs)].mean())
        else:
            rs_mean = self.prefit_resids_no_jumps.phase_resids[no_jumps].mean()

        # determines how far on either side fake toas go
        # TODO: hard limit on how far fake toas can go --> can get clkcorr
        # errors if go before GBT existed, etc.
        minMJD, maxMJD = selectedMJDs.min(), selectedMJDs.max()
        spanMJDs = maxMJD - minMJD
        if spanMJDs < 30 * u.d:
            redge = ledge = 4
            npoints = 400
        elif spanMJDs < 90 * u.d:
            redge = ledge = 2
            npoints = 300
        elif spanMJDs < 200 * u.d:
            redge = ledge = 1
            npoints = 300
        elif spanMJDs < 400 * u.d:
            redge = ledge = 0.5
            npoints = 200
        else:
            redge = ledge = 0.2
            npoints = 150
        # Check to see if too recent
        nowish = (Time.now().mjd - 40) * u.d
        if maxMJD + spanMJDs * redge > nowish:
            redge = (nowish - maxMJD) / spanMJDs
            if redge < 0.0:
                redge = 0.0
        f_toas, rs = random_models(
            f,
            rs_mean=rs_mean,
            redge_multiplier=redge,
            ledge_multiplier=ledge,
            npoints=npoints,
            iter=10,
        )
        self.random_resids = rs
        self.fake_toas = f_toas