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, 10.0 / 0.01) 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_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))
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))