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))
class TestAveragedCrossspectrum(object): def setup_class(self): tstart = 0.0 tend = 1.0 dt = np.longdouble(0.0001) time = np.arange(tstart + 0.5*dt, tend + 0.5*dt, dt) counts1 = np.random.poisson(0.01, size=time.shape[0]) counts2 = np.random.negative_binomial(1, 0.09, size=time.shape[0]) self.lc1 = Lightcurve(time, counts1, gti=[[tstart, tend]], dt=dt) self.lc2 = Lightcurve(time, counts2, gti=[[tstart, tend]], dt=dt) self.cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1) def test_invalid_type_attribute(self): with pytest.raises(ValueError): cs_test = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1) cs_test.type = 'invalid_type' assert AveragedCrossspectrum._make_crossspectrum(cs_test, self.lc1, self.lc2) def test_invalid_type_attribute_with_multiple_lcs(self): acs_test = AveragedCrossspectrum([self.lc1, self.lc2], [self.lc2, self.lc1], segment_size=1) acs_test.type = 'invalid_type' with pytest.raises(ValueError): assert AveragedCrossspectrum._make_crossspectrum(acs_test, lc1=[self.lc1, self.lc2], lc2=[self.lc2, self.lc1]) def test_different_dt(self): time1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] counts1_test = np.random.poisson(0.01, size=len(time1)) test_lc1 = Lightcurve(time1, counts1_test) time2 = [2, 4, 6, 8, 10] counts2_test = np.random.negative_binomial(1, 0.09, size=len(time2)) test_lc2 = Lightcurve(time2, counts2_test) assert test_lc1.tseg == test_lc2.tseg assert test_lc1.dt != test_lc2.dt with pytest.raises(ValueError): assert AveragedCrossspectrum(test_lc1, test_lc2, segment_size=1) def test_different_tseg(self): time2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] counts2_test = np.random.poisson(0.01, size=len(time2)) test_lc2 = Lightcurve(time2, counts2_test) time1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] counts1_test = np.random.negative_binomial(1, 0.09, size=len(time1)) test_lc1 = Lightcurve(time1, counts1_test) assert test_lc2.dt == test_lc1.dt assert test_lc2.tseg != test_lc1.tseg with pytest.raises(ValueError): assert AveragedCrossspectrum(test_lc1, test_lc2, segment_size=1) def test_rebin_with_invalid_type_attribute(self): new_df = 2 aps = AveragedPowerspectrum(lc=self.lc1, segment_size=1, norm='leahy') aps.type = 'invalid_type' with pytest.raises(AttributeError): assert aps.rebin(df=new_df) def test_rebin_with_valid_type_attribute(self): new_df = 2 aps = AveragedPowerspectrum(lc=self.lc1, segment_size=1, norm='leahy') assert aps.rebin(df=new_df) def test_init_with_norm_not_str(self): with pytest.raises(TypeError): cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1, norm=1) def test_init_with_invalid_norm(self): with pytest.raises(ValueError): cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1, norm='frabs') def test_init_with_inifite_segment_size(self): with pytest.raises(ValueError): cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=np.inf) def test_with_iterable_of_lightcurves(self): def iter_lc(lc, n): "Generator of n parts of lc." t0 = int(len(lc) / n) t = t0 i = 0 while(True): lc_seg = lc[i:t] yield lc_seg if t + t0 > len(lc): break else: i, t = t, t + t0 cs = AveragedCrossspectrum(iter_lc(self.lc1, 1), iter_lc(self.lc2, 1), segment_size=1) def test_coherence(self): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") coh = self.cs.coherence() assert len(coh[0]) == 4999 assert len(coh[1]) == 4999 assert issubclass(w[-1].category, UserWarning) def test_failure_when_normalization_not_recognized(self): with pytest.raises(ValueError): self.cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1, norm="wrong") def test_rebin(self): new_cs = self.cs.rebin(df=1.5) assert new_cs.df == 1.5 new_cs.time_lag() def test_rebin_factor(self): new_cs = self.cs.rebin(f=1.5) assert new_cs.df == self.cs.df * 1.5 new_cs.time_lag() def test_rebin_log(self): # For now, just verify that it doesn't crash new_cs = self.cs.rebin_log(f=0.1) assert type(new_cs) == type(self.cs) new_cs.time_lag() def test_timelag(self): from ..simulator.simulator import Simulator dt = 0.1 simulator = Simulator(dt, 10000, rms=0.4, mean=200) test_lc1 = simulator.simulate(2) test_lc2 = Lightcurve(test_lc1.time, np.array(np.roll(test_lc1.counts, 2)), err_dist=test_lc1.err_dist, dt=dt) cs = AveragedCrossspectrum(test_lc1, test_lc2, segment_size=10, norm="none") time_lag, time_lag_err = cs.time_lag() assert np.all(np.abs(time_lag[:10] - 0.1) < 3 * time_lag_err[:10]) def test_errorbars(self): time = np.arange(10000) * 0.1 test_lc1 = Lightcurve(time, np.random.poisson(200, 10000)) test_lc2 = Lightcurve(time, np.random.poisson(200, 10000)) cs = AveragedCrossspectrum(test_lc1, test_lc2, segment_size=10, norm="leahy") assert np.allclose(cs.power_err, np.sqrt(2/cs.m))
class TestAveragedCrossspectrum(object): def setup_class(self): tstart = 0.0 tend = 1.0 dt = np.longdouble(0.0001) time = np.arange(tstart + 0.5 * dt, tend + 0.5 * dt, dt) counts1 = np.random.poisson(0.01, size=time.shape[0]) counts2 = np.random.negative_binomial(1, 0.09, size=time.shape[0]) self.lc1 = Lightcurve(time, counts1, gti=[[tstart, tend]], dt=dt) self.lc2 = Lightcurve(time, counts2, gti=[[tstart, tend]], dt=dt) with pytest.warns(UserWarning) as record: self.cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1) def test_lc_keyword_deprecation(self): cs1 = AveragedCrossspectrum(data1=self.lc1, data2=self.lc2, segment_size=1) with pytest.warns(DeprecationWarning) as record: cs2 = AveragedCrossspectrum(lc1=self.lc1, lc2=self.lc2, segment_size=1) assert np.any(['lcN keywords' in r.message.args[0] for r in record]) assert np.allclose(cs1.power, cs2.power) assert np.allclose(cs1.freq, cs2.freq) def test_make_empty_crossspectrum(self): cs = AveragedCrossspectrum() assert cs.freq is None assert cs.power is None assert cs.df is None assert cs.nphots1 is None assert cs.nphots2 is None assert cs.m == 1 assert cs.n is None assert cs.power_err is None def test_no_counts_warns(self): newlc = copy.deepcopy(self.lc1) newlc.counts[:newlc.counts.size // 2] = \ 0 * newlc.counts[:newlc.counts.size // 2] with pytest.warns(UserWarning) as record: ps = AveragedCrossspectrum(newlc, self.lc2, segment_size=0.2) assert np.any(["No counts in " in r.message.args[0] for r in record]) def test_no_segment_size(self): with pytest.raises(ValueError): cs = AveragedCrossspectrum(self.lc1, self.lc2) def test_invalid_type_attribute(self): with pytest.raises(ValueError): cs_test = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1) cs_test.type = 'invalid_type' assert AveragedCrossspectrum._make_crossspectrum( cs_test, self.lc1, self.lc2) def test_invalid_type_attribute_with_multiple_lcs(self): with pytest.warns(UserWarning) as record: acs_test = AveragedCrossspectrum([self.lc1, self.lc2], [self.lc2, self.lc1], segment_size=1) acs_test.type = 'invalid_type' with pytest.raises(ValueError) as excinfo: assert AveragedCrossspectrum._make_crossspectrum( acs_test, [self.lc1, self.lc2], [self.lc2, self.lc1]) assert "Type of spectrum not recognized" in str(excinfo.value) def test_different_dt(self): time1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] counts1_test = np.random.poisson(0.01, size=len(time1)) test_lc1 = Lightcurve(time1, counts1_test) time2 = [2, 4, 6, 8, 10] counts2_test = np.random.negative_binomial(1, 0.09, size=len(time2)) test_lc2 = Lightcurve(time2, counts2_test) assert test_lc1.tseg == test_lc2.tseg assert test_lc1.dt != test_lc2.dt with pytest.raises(ValueError): assert AveragedCrossspectrum(test_lc1, test_lc2, segment_size=1) def test_different_tseg(self): time2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] counts2_test = np.random.poisson(1000, size=len(time2)) test_lc2 = Lightcurve(time2, counts2_test) time1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] counts1_test = np.random.poisson(1000, size=len(time1)) test_lc1 = Lightcurve(time1, counts1_test) assert test_lc2.dt == test_lc1.dt assert test_lc2.tseg != test_lc1.tseg with pytest.warns(UserWarning) as record: AveragedCrossspectrum(test_lc1, test_lc2, segment_size=5) assert np.any(["same tseg" in r.message.args[0] for r in record]) def test_with_zero_counts(self): nbins = 100 x = np.linspace(0, 10, nbins) ycounts1 = np.random.normal(loc=10, scale=0.5, size=int(0.4 * nbins)) ycounts2 = np.random.normal(loc=10, scale=0.5, size=int(0.4 * nbins)) yzero = np.zeros(int(0.6 * nbins)) y1 = np.hstack([ycounts1, yzero]) y2 = np.hstack([ycounts2, yzero]) lc1 = Lightcurve(x, y1) lc2 = Lightcurve(x, y2) acs = AveragedCrossspectrum(lc1, lc2, segment_size=5.0, norm="leahy") assert acs.m == 1 def test_rebin_with_invalid_type_attribute(self): new_df = 2 with pytest.warns(UserWarning) as record: aps = AveragedCrossspectrum(lc1=self.lc1, lc2=self.lc2, segment_size=1, norm='leahy') aps.type = 'invalid_type' with pytest.raises(ValueError) as excinfo: assert aps.rebin(df=new_df, method=aps.type) assert "Method for summing or averaging not recognized. " in str( excinfo.value) def test_rebin_with_valid_type_attribute(self): new_df = 2 with pytest.warns(UserWarning) as record: aps = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1, norm='leahy') assert aps.rebin(df=new_df) def test_init_with_norm_not_str(self): with pytest.raises(TypeError): cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1, norm=1) def test_init_with_invalid_norm(self): with pytest.raises(ValueError): cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1, norm='frabs') def test_init_with_inifite_segment_size(self): with pytest.raises(ValueError): cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=np.inf) def test_with_iterable_of_lightcurves(self): def iter_lc(lc, n): "Generator of n parts of lc." t0 = int(len(lc) / n) t = t0 i = 0 while (True): lc_seg = lc[i:t] yield lc_seg if t + t0 > len(lc): break else: i, t = t, t + t0 with pytest.warns(UserWarning) as record: cs = AveragedCrossspectrum(iter_lc(self.lc1, 1), iter_lc(self.lc2, 1), segment_size=1) def test_with_multiple_lightcurves_variable_length(self): gti = [[0, 0.05], [0.05, 0.5], [0.555, 1.0]] lc1 = copy.deepcopy(self.lc1) lc1.gti = gti lc2 = copy.deepcopy(self.lc2) lc2.gti = gti lc1_split = lc1.split_by_gti() lc2_split = lc2.split_by_gti() cs = AveragedCrossspectrum(lc1_split, lc2_split, segment_size=0.05, norm="leahy", silent=True) def test_coherence(self): with warnings.catch_warnings(record=True) as w: coh = self.cs.coherence() assert len(coh[0]) == 4999 assert len(coh[1]) == 4999 assert issubclass(w[-1].category, UserWarning) def test_failure_when_normalization_not_recognized(self): with pytest.raises(ValueError): self.cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1, norm="wrong") def test_failure_when_power_type_not_recognized(self): with pytest.raises(ValueError): self.cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1, power_type="wrong") def test_normalize_crossspectrum(self): cs1 = Crossspectrum(self.lc1, self.lc2, norm="leahy") cs2 = Crossspectrum(self.lc1, self.lc2, norm="leahy", power_type="all") cs3 = Crossspectrum(self.lc1, self.lc2, norm="leahy", power_type="real") cs4 = Crossspectrum(self.lc1, self.lc2, norm="leahy", power_type="absolute") assert np.all(cs1.power.real == cs3.power) assert np.all(np.isclose(np.abs(cs2.power), cs4.power, atol=0.0001)) def test_rebin(self): with warnings.catch_warnings(record=True) as w: new_cs = self.cs.rebin(df=1.5) assert new_cs.df == 1.5 new_cs.time_lag() def test_rebin_factor(self): with warnings.catch_warnings(record=True) as w: new_cs = self.cs.rebin(f=1.5) assert new_cs.df == self.cs.df * 1.5 new_cs.time_lag() def test_rebin_log(self): # For now, just verify that it doesn't crash with warnings.catch_warnings(record=True) as w: new_cs = self.cs.rebin_log(f=0.1) assert type(new_cs) == type(self.cs) new_cs.time_lag() def test_rebin_log_returns_complex_values(self): # For now, just verify that it doesn't crash with warnings.catch_warnings(record=True) as w: new_cs = self.cs.rebin_log(f=0.1) assert np.iscomplexobj(new_cs.power[0]) def test_rebin_log_returns_complex_errors(self): # For now, just verify that it doesn't crash with warnings.catch_warnings(record=True) as w: new_cs = self.cs.rebin_log(f=0.1) assert np.iscomplexobj(new_cs.power_err[0]) def test_timelag(self): dt = 0.1 simulator = Simulator(dt, 10000, rms=0.2, mean=1000) test_lc1 = simulator.simulate(2) test_lc1.counts -= np.min(test_lc1.counts) test_lc1 = Lightcurve(test_lc1.time, test_lc1.counts, err_dist=test_lc1.err_dist, dt=dt) test_lc2 = Lightcurve(test_lc1.time, np.array(np.roll(test_lc1.counts, 2)), err_dist=test_lc1.err_dist, dt=dt) with warnings.catch_warnings(record=True) as w: cs = AveragedCrossspectrum(test_lc1, test_lc2, segment_size=5, norm="none") time_lag, time_lag_err = cs.time_lag() assert np.all(np.abs(time_lag[:6] - 0.1) < 3 * time_lag_err[:6]) def test_errorbars(self): time = np.arange(10000) * 0.1 test_lc1 = Lightcurve(time, np.random.poisson(200, 10000)) test_lc2 = Lightcurve(time, np.random.poisson(200, 10000)) with warnings.catch_warnings(record=True) as w: cs = AveragedCrossspectrum(test_lc1, test_lc2, segment_size=10, norm="leahy") assert np.allclose(cs.power_err, np.sqrt(2 / cs.m)) def test_classical_significances(self): time = np.arange(10000) * 0.1 np.random.seed(62) test_lc1 = Lightcurve(time, np.random.poisson(200, 10000)) test_lc2 = Lightcurve(time, np.random.poisson(200, 10000)) with warnings.catch_warnings(record=True) as w: cs = AveragedCrossspectrum(test_lc1, test_lc2, segment_size=10, norm="leahy") maxpower = np.max(cs.power) assert np.all( np.isfinite(cs.classical_significances(threshold=maxpower / 2.)))
class TestAveragedCrossspectrumEvents(object): def setup_class(self): tstart = 0.0 tend = 1.0 self.dt = np.longdouble(0.0001) times1 = np.sort(np.random.uniform(tstart, tend, 1000)) times2 = np.sort(np.random.uniform(tstart, tend, 1000)) gti = np.array([[tstart, tend]]) self.events1 = EventList(times1, gti=gti) self.events2 = EventList(times2, gti=gti) self.cs = Crossspectrum(self.events1, self.events2, dt=self.dt) self.acs = AveragedCrossspectrum(self.events1, self.events2, segment_size=1, dt=self.dt) self.lc1, self.lc2 = self.events1, self.events2 def test_it_works_with_events(self): lc1 = self.events1.to_lc(self.dt) lc2 = self.events2.to_lc(self.dt) lccs = Crossspectrum(lc1, lc2) assert np.allclose(lccs.power, self.cs.power) def test_no_segment_size(self): with pytest.raises(ValueError): cs = AveragedCrossspectrum(self.lc1, self.lc2, dt=self.dt) def test_init_with_norm_not_str(self): with pytest.raises(TypeError): cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1, norm=1, dt=self.dt) def test_init_with_invalid_norm(self): with pytest.raises(ValueError): cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1, norm='frabs', dt=self.dt) def test_init_with_inifite_segment_size(self): with pytest.raises(ValueError): cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=np.inf, dt=self.dt) def test_coherence(self): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") coh = self.acs.coherence() assert len(coh[0]) == 4999 assert len(coh[1]) == 4999 assert issubclass(w[-1].category, UserWarning) def test_failure_when_normalization_not_recognized(self): with pytest.raises(ValueError): cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1, norm="wrong", dt=self.dt) def test_failure_when_power_type_not_recognized(self): with pytest.raises(ValueError): cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1, power_type="wrong", dt=self.dt) def test_rebin(self): new_cs = self.acs.rebin(df=1.5) assert new_cs.df == 1.5 new_cs.time_lag() def test_rebin_factor(self): new_cs = self.acs.rebin(f=1.5) assert new_cs.df == self.acs.df * 1.5 new_cs.time_lag() def test_rebin_log(self): # For now, just verify that it doesn't crash new_cs = self.acs.rebin_log(f=0.1) assert type(new_cs) == type(self.acs) new_cs.time_lag() def test_rebin_log_returns_complex_values(self): # For now, just verify that it doesn't crash new_cs = self.acs.rebin_log(f=0.1) assert np.iscomplexobj(new_cs.power[0]) def test_rebin_log_returns_complex_errors(self): # For now, just verify that it doesn't crash new_cs = self.acs.rebin_log(f=0.1) assert np.iscomplexobj(new_cs.power_err[0])
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))
class TestAveragedCrossspectrum(object): def setup_class(self): tstart = 0.0 tend = 1.0 dt = np.longdouble(0.0001) time = np.arange(tstart + 0.5*dt, tend + 0.5*dt, dt) counts1 = np.random.poisson(0.01, size=time.shape[0]) counts2 = np.random.negative_binomial(1, 0.09, size=time.shape[0]) self.lc1 = Lightcurve(time, counts1, gti=[[tstart, tend]], dt=dt) self.lc2 = Lightcurve(time, counts2, gti=[[tstart, tend]], dt=dt) self.cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1) def test_make_empty_crossspectrum(self): cs = AveragedCrossspectrum() assert cs.freq is None assert cs.power is None assert cs.df is None assert cs.nphots1 is None assert cs.nphots2 is None assert cs.m == 1 assert cs.n is None assert cs.power_err is None def test_no_segment_size(self): with pytest.raises(ValueError): cs = AveragedCrossspectrum(self.lc1, self.lc2) def test_invalid_type_attribute(self): with pytest.raises(ValueError): cs_test = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1) cs_test.type = 'invalid_type' assert AveragedCrossspectrum._make_crossspectrum(cs_test, self.lc1, self.lc2) def test_invalid_type_attribute_with_multiple_lcs(self): acs_test = AveragedCrossspectrum([self.lc1, self.lc2], [self.lc2, self.lc1], segment_size=1) acs_test.type = 'invalid_type' with pytest.raises(ValueError): assert AveragedCrossspectrum._make_crossspectrum(acs_test, lc1=[self.lc1, self.lc2], lc2=[self.lc2, self.lc1]) def test_different_dt(self): time1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] counts1_test = np.random.poisson(0.01, size=len(time1)) test_lc1 = Lightcurve(time1, counts1_test) time2 = [2, 4, 6, 8, 10] counts2_test = np.random.negative_binomial(1, 0.09, size=len(time2)) test_lc2 = Lightcurve(time2, counts2_test) assert test_lc1.tseg == test_lc2.tseg assert test_lc1.dt != test_lc2.dt with pytest.raises(ValueError): assert AveragedCrossspectrum(test_lc1, test_lc2, segment_size=1) def test_different_tseg(self): time2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] counts2_test = np.random.poisson(0.01, size=len(time2)) test_lc2 = Lightcurve(time2, counts2_test) time1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] counts1_test = np.random.negative_binomial(1, 0.09, size=len(time1)) test_lc1 = Lightcurve(time1, counts1_test) assert test_lc2.dt == test_lc1.dt assert test_lc2.tseg != test_lc1.tseg with pytest.raises(ValueError): assert AveragedCrossspectrum(test_lc1, test_lc2, segment_size=1) def test_rebin_with_invalid_type_attribute(self): new_df = 2 aps = AveragedPowerspectrum(lc=self.lc1, segment_size=1, norm='leahy') aps.type = 'invalid_type' with pytest.raises(AttributeError): assert aps.rebin(df=new_df) def test_rebin_with_valid_type_attribute(self): new_df = 2 aps = AveragedPowerspectrum(lc=self.lc1, segment_size=1, norm='leahy') assert aps.rebin(df=new_df) def test_init_with_norm_not_str(self): with pytest.raises(TypeError): cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1, norm=1) def test_init_with_invalid_norm(self): with pytest.raises(ValueError): cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1, norm='frabs') def test_init_with_inifite_segment_size(self): with pytest.raises(ValueError): cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=np.inf) def test_with_iterable_of_lightcurves(self): def iter_lc(lc, n): "Generator of n parts of lc." t0 = int(len(lc) / n) t = t0 i = 0 while(True): lc_seg = lc[i:t] yield lc_seg if t + t0 > len(lc): break else: i, t = t, t + t0 cs = AveragedCrossspectrum(iter_lc(self.lc1, 1), iter_lc(self.lc2, 1), segment_size=1) def test_coherence(self): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") coh = self.cs.coherence() assert len(coh[0]) == 4999 assert len(coh[1]) == 4999 assert issubclass(w[-1].category, UserWarning) def test_failure_when_normalization_not_recognized(self): with pytest.raises(ValueError): self.cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1, norm="wrong") def test_rebin(self): new_cs = self.cs.rebin(df=1.5) assert new_cs.df == 1.5 new_cs.time_lag() def test_rebin_factor(self): new_cs = self.cs.rebin(f=1.5) assert new_cs.df == self.cs.df * 1.5 new_cs.time_lag() def test_rebin_log(self): # For now, just verify that it doesn't crash new_cs = self.cs.rebin_log(f=0.1) assert type(new_cs) == type(self.cs) new_cs.time_lag() def test_timelag(self): from ..simulator.simulator import Simulator dt = 0.1 simulator = Simulator(dt, 10000, rms=0.4, mean=200) test_lc1 = simulator.simulate(2) test_lc2 = Lightcurve(test_lc1.time, np.array(np.roll(test_lc1.counts, 2)), err_dist=test_lc1.err_dist, dt=dt) cs = AveragedCrossspectrum(test_lc1, test_lc2, segment_size=10, norm="none") time_lag, time_lag_err = cs.time_lag() assert np.all(np.abs(time_lag[:10] - 0.1) < 3 * time_lag_err[:10]) def test_errorbars(self): time = np.arange(10000) * 0.1 test_lc1 = Lightcurve(time, np.random.poisson(200, 10000)) test_lc2 = Lightcurve(time, np.random.poisson(200, 10000)) cs = AveragedCrossspectrum(test_lc1, test_lc2, segment_size=10, norm="leahy") assert np.allclose(cs.power_err, np.sqrt(2/cs.m))