def test_phase_exposure1(self): start_time = 0 stop_time = 1 period = 1 nbin = 16 expo = phase_exposure(start_time, stop_time, period, nbin) np.testing.assert_array_almost_equal(expo, np.ones(nbin))
def test_phase_exposure4(self): start_time = 0 stop_time = 1 gtis = np.array([[-0.2, 1.2]]) period = 1 nbin = 16 expo = phase_exposure(start_time, stop_time, period, nbin, gtis=gtis) expected = np.ones(nbin) np.testing.assert_array_almost_equal(expo, expected)
def test_phase_exposure2(self): start_time = 0 stop_time = 0.5 period = 1 nbin = 16 expo = phase_exposure(start_time, stop_time, period, nbin) expected = np.ones(nbin) expected[nbin // 2:] = 0 np.testing.assert_array_almost_equal(expo, expected)
def test_phase_exposure2(self): start_time = 0 stop_time = 0.5 period = 1 nbin = 16 expo = phase_exposure(start_time, stop_time, period, nbin) expected = np.ones(nbin) expected[nbin//2:] = 0 np.testing.assert_array_almost_equal(expo, expected)
def test_phase_exposure3(self): start_time = 0 stop_time = 1 gtis = np.array([[0, 0.5]]) period = 1 nbin = 16 expo = phase_exposure(start_time, stop_time, period, nbin, gtis=gtis) expected = np.ones(nbin) expected[nbin // 2:] = 0 np.testing.assert_array_almost_equal(expo, expected)
def test_phase_exposure3(self): start_time = 0 stop_time = 1 gtis = np.array([[0, 0.5]]) period = 1 nbin = 16 expo = phase_exposure(start_time, stop_time, period, nbin, gtis=gtis) expected = np.ones(nbin) expected[nbin//2:] = 0 np.testing.assert_array_almost_equal(expo, expected)
def phase_tag(ev_list, parameter_info, gtis=None, mjdref=0, nbin=10, ref_to_max=False, pepoch=None, expocorr=True, pulse_ref_time=None, plot=True, test=False): """Phase-tag events in a FITS file with a given ephemeris. Parameters ---------- ev_list : float Event times parameter_info : str or array of floats If a string, this is a pulsar parameter file that PINT is able to understand. Otherwise, this is a list of frequency derivatives [F0, F1, F2, ...] Other parameters ---------------- gtis : [[g0_0, g0_1], [g1_0, g1_1], ...] Good time intervals nbin : int Number of nbin in the pulsed profile ref_to_max : bool Automatically refer the TOAs to the maximum of the profile pepoch : float, default None Reference epoch for the timing solution. If None, this is the start of the observation. pulse_ref_time : float Reference time for the pulse. This overrides ref_to_max plot : bool Plot diagnostics expocorr : bool Use exposure correction when calculating the profile """ # ---- in MJD ---- if gtis is None: gtis = np.array([[ev_list[0], ev_list[-1]]]) ev_mjd = ev_list / 86400 + mjdref gtis_mjd = gtis / 86400 + mjdref pepoch = _assign_value_if_none(pepoch, gtis_mjd[0, 0]) # ------ Orbital DEMODULATION -------------------- if is_string(parameter_info): raise NotImplementedError('This part is not yet implemented. Please ' 'use single frequencies and pepoch as ' 'documented') else: frequency_derivatives = parameter_info times = (ev_mjd - pepoch) * 86400 f = frequency_derivatives[0] phase = pulse_phase(times, *frequency_derivatives, to_1=False) gti_phases = pulse_phase((gtis_mjd - pepoch) * 86400, *frequency_derivatives, to_1=False) # ------- now apply period derivatives ------ print("Calculating phases...", end='') ref_phase = 0 ref_time = 0 if pulse_ref_time is not None: ref_time = (pulse_ref_time - pepoch) * 86400 ref_phase = ref_time * f elif ref_to_max: phase_to1 = phase - np.floor(phase) raw_profile, bins = np.histogram(phase_to1, bins=np.linspace(0, 1, nbin + 1)) exposure = phase_exposure(gti_phases[0, 0], gti_phases[-1, 1], 1, nbin=nbin, gtis=gti_phases) profile = raw_profile / exposure profile_err = np.sqrt(raw_profile) / exposure sinpars, bu, bu = fit_profile(profile, profile_err, nperiods=2, baseline=True, debug=test) fine_phases = np.linspace(0, 2, 1000 * 2) fitted_profile = std_fold_fit_func(sinpars, fine_phases) maxp = np.argmax(fitted_profile) ref_phase = fine_phases[maxp] if test: # pragma: no cover # No tests with a pulsed profile yet ref_phase = bins[np.argmax(raw_profile)] ref_time = ref_phase / f phase -= ref_phase gti_phases -= ref_phase phase_to1 = phase - np.floor(phase) raw_profile, bins = np.histogram(phase_to1, bins=np.linspace(0, 1, nbin + 1)) exposure = phase_exposure(gti_phases[0, 0], gti_phases[-1, 1], 1, nbin=nbin, gtis=gti_phases) if np.any(np.logical_or(exposure != exposure, exposure == 0)): warnings.warn('Exposure has NaNs or zeros. Profile is not normalized') expocorr = False if not expocorr: exposure = np.ones_like(raw_profile) profile = raw_profile / exposure profile = np.append(profile, profile) exposure = np.append(exposure, exposure) profile_err = np.sqrt(profile) phs = (bins[1:] + bins[:-1]) / 2 phs = np.append(phs, phs + 1) fig = None if plot: fig = plt.figure() plt.errorbar(phs, profile / exposure, yerr=profile_err / exposure, fmt='none') plt.plot(phs, profile / exposure, 'k-', drawstyle='steps-mid') plt.xlabel("Phase") plt.ylabel("Counts") for i in range(20): plt.axvline(i * 0.1, ls='--', color='b') if not test: # pragma: no cover plt.show() # ------ WRITE RESULTS BACK TO FITS -------------- results = type('results', (object, ), {}) results.ev_list = ev_list results.phase = phase results.frequency_derivatives = frequency_derivatives results.ref_time = ref_time results.figure = fig results.plot_phase = phs results.plot_profile = profile / exposure results.plot_profile_err = profile_err / exposure return results