class TestCrossspectrum(object): def setup_class(self): tstart = 0.0 tend = 1.0 dt = 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 = Crossspectrum(self.lc1, self.lc2) def test_make_empty_crossspectrum(self): cs = Crossspectrum() 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_init_with_one_lc_none(self): with pytest.raises(TypeError): cs = Crossspectrum(self.lc1) def test_init_with_multiple_gti(self): gti = np.array([[0.0, 0.2], [0.6, 1.0]]) with pytest.raises(TypeError): cs = Crossspectrum(self.lc1, self.lc2, gti=gti) def test_init_with_norm_not_str(self): with pytest.raises(TypeError): cs = Crossspectrum(norm=1) def test_init_with_invalid_norm(self): with pytest.raises(ValueError): cs = Crossspectrum(norm='frabs') def test_init_with_wrong_lc1_instance(self): lc_ = Crossspectrum() with pytest.raises(TypeError): cs = Crossspectrum(lc_, self.lc2) def test_init_with_wrong_lc2_instance(self): lc_ = Crossspectrum() with pytest.raises(TypeError): cs = Crossspectrum(self.lc1, lc_) def test_make_crossspectrum_diff_lc_counts_shape(self): counts = np.array([1]*10001) time = np.linspace(0.0, 1.0001, 10001) lc_ = Lightcurve(time, counts) with pytest.raises(StingrayError): cs = Crossspectrum(self.lc1, lc_) def test_make_crossspectrum_diff_lc_stat(self): lc_ = copy.copy(self.lc1) lc_.err_dist = 'gauss' with pytest.warns(UserWarning) as record: cs = Crossspectrum(self.lc1, lc_) assert np.any(["different statistics" in r.message.args[0] for r in record]) def test_make_crossspectrum_bad_lc_stat(self): lc1 = copy.copy(self.lc1) lc1.err_dist = 'gauss' lc2 = copy.copy(self.lc1) lc2.err_dist = 'gauss' with pytest.warns(UserWarning) as record: cs = Crossspectrum(lc1, lc2) assert np.any(["is not poisson" in r.message.args[0] for r in record]) def test_make_crossspectrum_diff_dt(self): counts = np.array([1]*10000) time = np.linspace(0.0, 2.0, 10000) lc_ = Lightcurve(time, counts) with pytest.raises(StingrayError): cs = Crossspectrum(self.lc1, lc_) def test_rebin_smaller_resolution(self): # Original df is between 0.9 and 1.0 with pytest.raises(ValueError): new_cs = self.cs.rebin(df=0.1) 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_norm_leahy(self): cs = Crossspectrum(self.lc1, self.lc2, norm='leahy') assert len(cs.power) == 4999 assert cs.norm == 'leahy' def test_norm_frac(self): cs = Crossspectrum(self.lc1, self.lc2, norm='frac') assert len(cs.power) == 4999 assert cs.norm == 'frac' def test_norm_abs(self): cs = Crossspectrum(self.lc1, self.lc2, norm='abs') assert len(cs.power) == 4999 assert cs.norm == 'abs' def test_failure_when_normalization_not_recognized(self): with pytest.raises(ValueError): cs = Crossspectrum(self.lc1, self.lc2, norm='wrong') def test_coherence(self): coh = self.cs.coherence() assert len(coh) == 4999 assert np.abs(coh[0]) < 1 def test_timelag(self): time_lag = self.cs.time_lag() assert max(time_lag) <= np.pi assert min(time_lag) >= -np.pi def test_nonzero_err(self): assert np.all(self.cs.power_err > 0) def test_timelag_error(self): class Child(Crossspectrum): def __init__(self): pass obj = Child() with pytest.raises(AttributeError): lag = obj.time_lag()
class TestCrossspectrum(object): def setup_class(self): tstart = 0.0 tend = 1.0 dt = 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.rate1 = 100. # mean count rate (counts/sec) of light curve 1 with pytest.warns(UserWarning) as record: self.cs = Crossspectrum(self.lc1, self.lc2) def test_lc_keyword_deprecation(self): cs1 = Crossspectrum(self.lc1, self.lc2) with pytest.warns(DeprecationWarning) as record: cs2 = Crossspectrum(lc1=self.lc1, lc2=self.lc2) 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 = Crossspectrum() 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_init_with_one_lc_none(self): with pytest.raises(TypeError): cs = Crossspectrum(self.lc1) def test_init_with_multiple_gti(self): gti = np.array([[0.0, 0.2], [0.6, 1.0]]) with pytest.raises(TypeError): cs = Crossspectrum(self.lc1, self.lc2, gti=gti) def test_init_with_norm_not_str(self): with pytest.raises(TypeError): cs = Crossspectrum(norm=1) def test_init_with_invalid_norm(self): with pytest.raises(ValueError): cs = Crossspectrum(norm='frabs') def test_init_with_wrong_lc1_instance(self): lc_ = {"a": 1, "b": 2} with pytest.raises(TypeError): cs = Crossspectrum(lc_, self.lc2) def test_init_with_wrong_lc2_instance(self): lc_ = {"a": 1, "b": 2} with pytest.raises(TypeError): cs = Crossspectrum(self.lc1, lc_) def test_make_crossspectrum_diff_lc_counts_shape(self): counts = np.array([1] * 10001) time = np.linspace(0.0, 1.0001, 10001) lc_ = Lightcurve(time, counts) with pytest.raises(StingrayError): cs = Crossspectrum(self.lc1, lc_) def test_make_crossspectrum_diff_lc_stat(self): lc_ = copy.copy(self.lc1) lc_.err_dist = 'gauss' with pytest.warns(UserWarning) as record: cs = Crossspectrum(self.lc1, lc_) assert np.any( ["different statistics" in r.message.args[0] for r in record]) def test_make_crossspectrum_bad_lc_stat(self): lc1 = copy.copy(self.lc1) lc1.err_dist = 'gauss' lc2 = copy.copy(self.lc1) lc2.err_dist = 'gauss' with pytest.warns(UserWarning) as record: cs = Crossspectrum(lc1, lc2) assert np.any(["is not poisson" in r.message.args[0] for r in record]) def test_make_crossspectrum_diff_dt(self): counts = np.array([1] * 10000) time = np.linspace(0.0, 2.0, 10000) lc_ = Lightcurve(time, counts) with pytest.raises(StingrayError): cs = Crossspectrum(self.lc1, lc_) def test_rebin_smaller_resolution(self): # Original df is between 0.9 and 1.0 with pytest.raises(ValueError): new_cs = self.cs.rebin(df=0.1) 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_norm_abs(self): # Testing for a power spectrum of lc1 cs = Crossspectrum(self.lc1, self.lc1, norm='abs') assert len(cs.power) == 4999 assert cs.norm == 'abs' abs_noise = 2. * self.rate1 # expected Poisson noise level assert np.isclose(np.mean(cs.power[1:]), abs_noise) def test_norm_leahy(self): with pytest.warns(UserWarning) as record: cs = Crossspectrum(self.lc1, self.lc1, norm='leahy') assert len(cs.power) == 4999 assert cs.norm == 'leahy' leahy_noise = 2.0 # expected Poisson noise level assert np.isclose(np.mean(cs.power[1:]), leahy_noise, rtol=0.02) def test_norm_frac(self): with pytest.warns(UserWarning) as record: cs = Crossspectrum(self.lc1, self.lc1, norm='frac') assert len(cs.power) == 4999 assert cs.norm == 'frac' norm = 2. / self.rate1 assert np.isclose(np.mean(cs.power[1:]), norm, rtol=0.2) def test_norm_abs(self): with pytest.warns(UserWarning) as record: cs = Crossspectrum(self.lc1, self.lc2, norm='abs') assert len(cs.power) == 4999 assert cs.norm == 'abs' def test_failure_when_normalization_not_recognized(self): with pytest.raises(ValueError): cs = Crossspectrum(self.lc1, self.lc2, norm='wrong') def test_coherence(self): coh = self.cs.coherence() assert len(coh) == 4999 assert np.abs(coh[0]) < 1 def test_timelag(self): time_lag = self.cs.time_lag() assert np.max(time_lag) <= np.pi assert np.min(time_lag) >= -np.pi def test_nonzero_err(self): assert np.all(self.cs.power_err > 0) def test_timelag_error(self): class Child(Crossspectrum): def __init__(self): pass obj = Child() with pytest.raises(AttributeError): lag = obj.time_lag() def test_plot_simple(self): self.cs.plot() assert plt.fignum_exists('crossspectrum') def test_rebin_error(self): cs = Crossspectrum() with pytest.raises(ValueError): cs.rebin() def test_classical_significances_runs(self): with pytest.warns(UserWarning) as record: cs = Crossspectrum(self.lc1, self.lc2, norm='leahy') cs.classical_significances() def test_classical_significances_fails_in_rms(self): with pytest.warns(UserWarning) as record: cs = Crossspectrum(self.lc1, self.lc2, norm='frac') with pytest.raises(ValueError): cs.classical_significances() def test_classical_significances_threshold(self): with pytest.warns(UserWarning) as record: cs = Crossspectrum(self.lc1, self.lc2, norm='leahy') # change the powers so that just one exceeds the threshold cs.power = np.zeros_like(cs.power) + 2.0 index = 1 cs.power[index] = 10.0 threshold = 0.01 pval = cs.classical_significances(threshold=threshold, trial_correction=False) assert pval[0, 0] < threshold assert pval[1, 0] == index def test_classical_significances_trial_correction(self): with pytest.warns(UserWarning) as record: cs = Crossspectrum(self.lc1, self.lc2, norm='leahy') # change the powers so that just one exceeds the threshold cs.power = np.zeros_like(cs.power) + 2.0 index = 1 cs.power[index] = 10.0 threshold = 0.01 pval = cs.classical_significances(threshold=threshold, trial_correction=True) assert np.size(pval) == 0 def test_classical_significances_with_logbinned_psd(self): with pytest.warns(UserWarning) as record: cs = Crossspectrum(self.lc1, self.lc2, norm='leahy') cs_log = cs.rebin_log() pval = cs_log.classical_significances(threshold=1.1, trial_correction=False) assert len(pval[0]) == len(cs_log.power) def test_pvals_is_numpy_array(self): cs = Crossspectrum(self.lc1, self.lc2, norm='leahy') # change the powers so that just one exceeds the threshold cs.power = np.zeros_like(cs.power) + 2.0 index = 1 cs.power[index] = 10.0 threshold = 1.0 pval = cs.classical_significances(threshold=threshold, trial_correction=True) assert isinstance(pval, np.ndarray) assert pval.shape[0] == 2
class TestCrossspectrum(object): def setup_class(self): tstart = 0.0 tend = 1.0 dt = 0.0001 time = np.linspace(tstart, tend, int((tend - tstart)/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) self.lc2 = Lightcurve(time, counts2) self.cs = Crossspectrum(self.lc1, self.lc2) def test_make_empty_crossspectrum(self): cs = Crossspectrum() 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 def test_init_with_one_lc_none(self): with pytest.raises(TypeError): cs = Crossspectrum(self.lc1) def test_init_with_norm_not_str(self): with pytest.raises(TypeError): cs = Crossspectrum(norm=1) def test_init_with_invalid_norm(self): with pytest.raises(ValueError): cs = Crossspectrum(norm='frabs') def test_init_with_wrong_lc1_instance(self): lc_ = Crossspectrum() with pytest.raises(TypeError): cs = Crossspectrum(lc_, self.lc2) def test_init_with_wrong_lc2_instance(self): lc_ = Crossspectrum() with pytest.raises(TypeError): cs = Crossspectrum(self.lc1, lc_) def test_make_crossspectrum_diff_lc_counts_shape(self): counts = np.array([1]*10001) time = np.linspace(0.0, 1.0001, 10001) lc_ = Lightcurve(time, counts) with pytest.raises(StingrayError): cs = Crossspectrum(self.lc1, lc_) def test_make_crossspectrum_diff_dt(self): counts = np.array([1]*10000) time = np.linspace(0.0, 2.0, 10000) lc_ = Lightcurve(time, counts) with pytest.raises(StingrayError): cs = Crossspectrum(self.lc1, lc_) def test_rebin_smaller_resolution(self): # Original df is between 0.9 and 1.0 with pytest.raises(ValueError): new_cs = self.cs.rebin(df=0.1) def test_rebin(self): new_cs = self.cs.rebin(df=1.5) assert new_cs.df == 1.5 def test_norm_leahy(self): cs = Crossspectrum(self.lc1, self.lc2, norm='leahy') assert len(cs.power) == 4999 assert cs.norm == 'leahy' def test_norm_frac(self): cs = Crossspectrum(self.lc1, self.lc2, norm='frac') assert len(cs.power) == 4999 assert cs.norm == 'frac' def test_norm_abs(self): cs = Crossspectrum(self.lc1, self.lc2, norm='abs') assert len(cs.power) == 4999 assert cs.norm == 'abs' def test_failure_when_normalization_not_recognized(self): with pytest.raises(ValueError): cs = Crossspectrum(self.lc1, self.lc2, norm='wrong') def test_coherence(self): coh = self.cs.coherence() assert len(coh) == 4999 assert np.abs(coh[0]) < 1 def test_timelag(self): time_lag = self.cs.time_lag() assert max(time_lag) <= np.pi assert min(time_lag) >= -np.pi def test_timelag_error(self): class Child(Crossspectrum): def __init__(self): pass obj = Child() with pytest.raises(AttributeError): lag = obj.time_lag()
class TestCrossspectrum(object): def setup_class(self): tstart = 0.0 tend = 1.0 dt = 0.0001 time = np.linspace(tstart, tend, int((tend - tstart)/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) self.lc2 = Lightcurve(time, counts2) self.cs = Crossspectrum(self.lc1, self.lc2) def test_make_empty_crossspectrum(self): cs = Crossspectrum() 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 def test_init_with_one_lc_none(self): with pytest.raises(TypeError): cs = Crossspectrum(self.lc1) def test_init_with_norm_not_str(self): with pytest.raises(TypeError): cs = Crossspectrum(norm=1) def test_init_with_invalid_norm(self): with pytest.raises(ValueError): cs = Crossspectrum(norm='frabs') def test_init_with_wrong_lc1_instance(self): lc_ = Crossspectrum() with pytest.raises(AssertionError): cs = Crossspectrum(lc_, self.lc2) def test_init_with_wrong_lc2_instance(self): lc_ = Crossspectrum() with pytest.raises(AssertionError): cs = Crossspectrum(self.lc1, lc_) def test_make_crossspectrum_diff_lc_counts_shape(self): counts = np.array([1]*10001) time = np.linspace(0.0, 1.0001, 10001) lc_ = Lightcurve(time, counts) with pytest.raises(AssertionError): cs = Crossspectrum(self.lc1, lc_) def test_make_crossspectrum_diff_dt(self): counts = np.array([1]*10000) time = np.linspace(0.0, 2.0, 10000) lc_ = Lightcurve(time, counts) with pytest.raises(AssertionError): cs = Crossspectrum(self.lc1, lc_) def test_rebin_smaller_resolution(self): # Original df is between 0.9 and 1.0 with pytest.raises(AssertionError): new_cs = self.cs.rebin(df=0.1) def test_rebin(self): new_cs = self.cs.rebin(df=1.5) assert new_cs.df == 1.5 def test_norm_leahy(self): cs = Crossspectrum(self.lc1, self.lc2, norm='leahy') assert len(cs.power) == 4999 assert cs.norm == 'leahy' def test_norm_frac(self): cs = Crossspectrum(self.lc1, self.lc2, norm='frac') assert len(cs.power) == 4999 assert cs.norm == 'frac' def test_norm_abs(self): cs = Crossspectrum(self.lc1, self.lc2, norm='abs') assert len(cs.power) == 4999 assert cs.norm == 'abs' def test_failure_when_normalization_not_recognized(self): with pytest.raises(ValueError): cs = Crossspectrum(self.lc1, self.lc2, norm='wrong') def test_coherence(self): coh = self.cs.coherence() assert len(coh) == 4999 assert np.abs(coh[0]) < 1 def test_timelag(self): time_lag = self.cs.time_lag() assert max(time_lag) <= np.pi assert min(time_lag) >= -np.pi def test_timelag_error(self): class Child(Crossspectrum): def __init__(self): pass obj = Child() with pytest.raises(AttributeError): lag = obj.time_lag()