def ccf_error(ref_counts, ci_counts_0, cs_res_model, rebin_log_factor, meta, ps_rms, filter_type="optimal"): n_seg = meta['N_SEG'] n_seconds = meta['NSECONDS'] dt = meta['DT'] n_bins = meta['N_BINS'] seg_ref_counts = np.array(np.split(ref_counts, n_seg)) seg_ci_counts = np.array(np.split(ci_counts_0, n_seg)) seg_css = np.array([]) seg_ccfs = np.array([]) seg_times = np.arange(0, n_seconds, dt) # light curve time bins for i in range(n_seg): # for each segment # Creating cross spectrum seg_ci_lc = Lightcurve(seg_times, seg_ci_counts[i], dt=dt) # CoI light curve seg_ref_lc = Lightcurve(seg_times, seg_ref_counts[i], dt=dt) # reference band light curve seg_cs = Crossspectrum(lc2=seg_ci_lc, lc1=seg_ref_lc, norm='leahy', power_type="absolute") # cross spectrum seg_cs = seg_cs.rebin_log(rebin_log_factor) # cross spectrum rebinning # applying filter if filter_type == "optimal": cs_filter = Optimal1D(cs_res_model) filter_freq = cs_filter(seg_cs.freq) filtered_seg_cs_power = filter_freq * np.abs(seg_cs.power) else: cs_filter = Window1D(cs_res_model) filter_freq = cs_filter(seg_cs.freq) filtered_seg_cs_power = filter_freq * np.abs(seg_cs.power) # calculating normalized ccf seg_ccf = ifft(filtered_seg_cs_power) # inverse FFT seg_ccf_real = seg_ccf.real # real part of ccf seg_ccf_real_norm = seg_ccf_real * ( 2 / n_bins / ps_rms) # normalisation if i == 0: seg_css = np.hstack((seg_css, np.array(filtered_seg_cs_power))) else: seg_css = np.vstack((seg_css, np.array(filtered_seg_cs_power))) if i == 0: seg_ccfs = np.hstack((seg_ccfs, np.array(seg_ccf_real_norm))) else: seg_ccfs = np.vstack((seg_ccfs, np.array(seg_ccf_real_norm))) # ccf after taking avg avg_seg_css = np.average(seg_css, axis=0) # average of cross spectrum avg_seg_ccf = ccf(avg_seg_css, ps_rms, n_bins) error = standard_error(seg_ccfs, avg_seg_ccf) return error, avg_seg_ccf
def test_compute_rms(): np.random.seed(150) amplitude_0 = 200.0 amplitude_1 = 100.0 amplitude_2 = 50.0 x_0_0 = 0.5 x_0_1 = 2.0 x_0_2 = 7.5 fwhm_0 = 0.1 fwhm_1 = 1.0 fwhm_2 = 0.5 whitenoise = 100.0 model = models.Lorentz1D(amplitude_0, x_0_0, fwhm_0) + \ models.Lorentz1D(amplitude_1, x_0_1, fwhm_1) + \ models.Lorentz1D(amplitude_2, x_0_2, fwhm_2) + \ models.Const1D(whitenoise) freq = np.linspace(-10.0, 10.0, 1000) p = model(freq) noise = np.random.exponential(size=len(freq)) power = p * noise cs = Crossspectrum() cs.freq = freq cs.power = power cs.df = cs.freq[1] - cs.freq[0] cs.n = len(freq) cs.m = 1 rms = np.sqrt(np.sum(model(cs.freq) * cs.df)).mean() assert rms == spec.compute_rms(cs, model, criteria="all") rms_pos = np.sqrt(np.sum(model(cs.freq[cs.freq > 0]) * cs.df)).mean() assert rms_pos == spec.compute_rms(cs, model, criteria="posfreq") optimal_filter = Window1D(model) optimal_filter_freq = optimal_filter(cs.freq) filtered_cs_power = optimal_filter_freq * np.abs(model(cs.freq)) rms = np.sqrt(np.sum(filtered_cs_power * cs.df)).mean() assert rms == spec.compute_rms(cs, model, criteria="window") with pytest.raises(ValueError): spec.compute_rms(cs, model, criteria="filter")
def test_ccf(self): # to make testing faster, fitting is not done. ref_ps = Powerspectrum(self.ref_lc, norm='abs') ci_counts_0 = self.ci_counts[0] ci_times = np.arange(0, self.n_seconds * self.n_seg, self.dt) ci_lc = Lightcurve(ci_times, ci_counts_0, dt=self.dt) # rebinning factor used in `rebin_log` rebin_log_factor = 0.4 acs = AveragedCrossspectrum(lc1=ci_lc, lc2=self.ref_lc, segment_size=self.n_seconds, norm='leahy', power_type="absolute") acs = acs.rebin_log(rebin_log_factor) # parest, res = fit_crossspectrum(acs, self.model, fitmethod="CG") acs_result_model = self.model # using optimal filter optimal_filter = Optimal1D(acs_result_model) optimal_filter_freq = optimal_filter(acs.freq) filtered_acs_power = optimal_filter_freq * np.abs(acs.power) # rebinning power spectrum new_df = spec.get_new_df(ref_ps, self.n_bins) ref_ps_rebinned = ref_ps.rebin(df=new_df) # parest, res = fit_powerspectrum(ref_ps_rebinned, self.model) ref_ps_rebinned_result_model = self.model # calculating rms from power spectrum ref_ps_rebinned_rms = spec.compute_rms(ref_ps_rebinned, ref_ps_rebinned_result_model, criteria="optimal") # calculating normalized ccf ccf_norm = spec.ccf(filtered_acs_power, ref_ps_rebinned_rms, self.n_bins) # calculating ccf error meta = { 'N_SEG': self.n_seg, 'NSECONDS': self.n_seconds, 'DT': self.dt, 'N_BINS': self.n_bins } error_ccf, avg_seg_ccf = spec.ccf_error(self.ref_counts, ci_counts_0, acs_result_model, rebin_log_factor, meta, ref_ps_rebinned_rms, filter_type="optimal") assert np.all(np.isclose(ccf_norm, avg_seg_ccf, atol=0.01)) assert np.all( np.isclose(error_ccf, np.zeros(shape=error_ccf.shape), atol=0.01)) # using window function tophat_filter = Window1D(acs_result_model) tophat_filter_freq = tophat_filter(acs.freq) filtered_acs_power = tophat_filter_freq * np.abs(acs.power) ref_ps_rebinned_rms = spec.compute_rms(ref_ps_rebinned, ref_ps_rebinned_result_model, criteria="window") ccf_norm = spec.ccf(filtered_acs_power, ref_ps_rebinned_rms, self.n_bins) error_ccf, avg_seg_ccf = spec.ccf_error(self.ref_counts, ci_counts_0, acs_result_model, rebin_log_factor, meta, ref_ps_rebinned_rms, filter_type="window") assert np.all(np.isclose(ccf_norm, avg_seg_ccf, atol=0.01)) assert np.all( np.isclose(error_ccf, np.zeros(shape=error_ccf.shape), atol=0.01))