def test_high_coherence(self): import copy t = np.arange(1280) a = np.random.poisson(100, len(t)) lc = Lightcurve(t, a) lc2 = Lightcurve(t, copy.copy(a)) c = AveragedCrossspectrum(lc, lc2, 128) coh, _ = c.coherence() np.testing.assert_almost_equal(np.mean(coh).real, 1.0)
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_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_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_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.)))
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): assert aps.rebin(df=new_df, method=aps.type)
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")
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_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 AvgCspecMain(bench_msg): func_dict = { 'AveragedCrossspectrum': [ 'Time_Init', 'Mem_Init', 'Time_Coher', 'Mem_Coher', 'Time_Tlag', 'Mem_Tlag' ], } wall_time = [[ f'{datetime.utcfromtimestamp(int(time.time())).strftime("%Y-%m-%d %H:%M:%S")}', f'{bench_msg}', ] for i in range(int(sum([len(x) for x in func_dict.values()]) / 2))] mem_use = [[ f'{datetime.utcfromtimestamp(int(time.time())).strftime("%Y-%m-%d %H:%M:%S")}', f'{bench_msg}', ] for i in range(int(sum([len(x) for x in func_dict.values()]) / 2))] for size in [10**i for i in range(5, 8)]: num_func = 0 times = np.arange(size) counts = np.random.rand(size) * 100 lc = Lightcurve(times, counts, dt=1.0, skip_checks=True) lc_other = Lightcurve(times, counts * np.random.rand(size), dt=1.0, skip_checks=True) time1, mem1 = benchCode(createAvgCspec, lc, lc_other, 10000) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 avg_cspec = AveragedCrossspectrum(lc, lc_other, 10000, silent=True) time1, mem1 = benchCode(coherAvgCspec, avg_cspec) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 time1, mem1 = benchCode(TlagAvgCspec, avg_cspec) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 del avg_cspec, lc, lc_other, times, counts, time1, mem1 CSVWriter(f'{os.path.abspath(os.path.join(os.getcwd(), os.pardir))}/data', func_dict, wall_time, mem_use) del func_dict, wall_time, mem_use
def test_load_and_save_averaged_crosssp(self): pds = AveragedCrossspectrum() pds.freq = np.linspace(0, 10, 15) pds.power = np.random.poisson(30, 15) pds.mjdref = 54385.3254923845 pds.gti = np.longdouble([[-0.5, 3.5]]) pds.m = 2 save_to_intermediate_file(pds, self.dum) pds2 = load_from_intermediate_file(self.dum) assert np.allclose(pds.gti, pds2.gti) assert np.allclose(pds.mjdref, pds2.mjdref) assert np.allclose(pds.gti, pds2.gti) assert pds.m == pds2.m
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 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 = AveragedCrossspectrum(self.lc1, self.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_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_timelag(self): dt = 0.1 simulator = Simulator(dt, 10000, rms=0.2, mean=1000) 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) 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_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_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_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 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_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) with pytest.warns(UserWarning) as record: acs = AveragedCrossspectrum(lc1, lc2, segment_size=5.0, norm="leahy") assert acs.m == 1 assert np.any( ["No counts in interval" in r.message.args[0] for r in record])
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_failure_when_normalization_not_recognized(self): with pytest.raises(ValueError): self.cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1, norm="wrong")
def test_init_with_inifite_segment_size(self): with pytest.raises(ValueError): cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=np.inf)
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_norm_not_str(self): with pytest.raises(TypeError): cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1, norm=1)
def createAvgCspec(lc1, lc2, seg): AveragedCrossspectrum(lc1, lc2, seg, silent=True)
def callerFunction(bench_msg): # bench_msg = input("Enter the changes made, if none put a '-': ") func_dict = { 'Lightcurve': [ 'Time_MakeLightcurve', 'Mem_MakeLightcurve', 'Time_InitNoParam', 'Mem_InitNoParam', 'Time_InitParam', 'Mem_InitParam', 'Time_ChangeMJDREF', 'Mem_ChangeMJDREF', 'Time_Rebin_Sum', 'Mem_Rebin_Sum', 'Time_Rebin_Mean_Avg', 'Mem_Rebin_Mean_Avg', 'Time_AddLC', 'Mem_AddLC', 'Time_SubLC', 'MemSubLC', 'Time_EqLC', 'Mem_EqLC', 'Time_NegLC', 'Mem_NegLC', 'Time_Trunc_Index', 'Mem_Trunc_Index', 'Time_Trunc_Time', 'Mem_Trunc_Time', 'Time_SplitLC', 'Mem_SplitLC', 'Time_Sort_Time', 'Mem_Sort_Time', 'Time_Sort_Counts', 'Mem_Sort_Counts', 'Time_Analyze_Chunks', 'Mem_Analyze_Chunks', 'Time_Est_Chunk_Len', 'Mem_Est_Chunk_Len', 'Time_JoinLC', 'Mem_JoinLC' ], 'Crossspectrum': [ 'Time_Init', 'Mem_Init', 'Time_Rebin_Linear', 'Mem_Rebin_Linear', 'Time_Coherence', 'Mem_Coherence', 'Time_Tlag', 'Mem_Tlag' ], 'AveragedCrossspectrum': [ 'Time_Init', 'Mem_Init', 'Time_Coher', 'Mem_Coher', 'Time_Tlag', 'Mem_Tlag' ], 'Powerspectrum': [ 'Time_Init', 'Mem_Init', 'Time_Rebin', 'Mem_Rebin', 'Time_RMS', 'Mem_RMS', 'Time_Class_Sign', 'Mem_Class_Sign' ], 'AveragedPowerspectrum': ['Time_Init', 'Mem_Init'] } wall_time = [[ f'{datetime.utcfromtimestamp(int(time.time())).strftime("%Y-%m-%d %H:%M:%S")}', f'{bench_msg}', ] for i in range(int(sum([len(x) for x in func_dict.values()]) / 2))] mem_use = [[ f'{datetime.utcfromtimestamp(int(time.time())).strftime("%Y-%m-%d %H:%M:%S")}', f'{bench_msg}', ] for i in range(int(sum([len(x) for x in func_dict.values()]) / 2))] for size in [10**i for i in range(5, 7)]: num_func = 0 times = np.arange(size) counts = np.random.rand(size) * 100 time1, mem1 = benchCode(makeLCFunc, times) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 time1, mem1 = benchCode(createLc, times, counts) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 time1, mem1 = benchCode(createLcP, times, counts) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 lc = Lightcurve(times, counts, dt=1.0, skip_checks=True) time1, mem1 = benchCode(lcMJD, lc) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 time1, mem1 = benchCode(rebinSum, lc, 2.0) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 time1, mem1 = benchCode(rebinMean, lc, 2.0) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 lc_other = Lightcurve(times, counts * np.random.rand(size), dt=1.0, skip_checks=True) time1, mem1 = benchCode(addLC, lc, lc_other) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 time1, mem1 = benchCode(subLC, lc, lc_other) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 time1, mem1 = benchCode(eqLC, lc, lc_other) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 del lc_other time1, mem1 = benchCode(negLC, lc) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 time1, mem1 = benchCode(indexTrunc, lc) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 time1, mem1 = benchCode(tTrunc, lc) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 times2 = np.arange(0, size, np.random.randint(4, 9)) counts2 = np.random.rand(len(times)) * 100 lc_temp = Lightcurve(times, counts, dt=1.0, skip_checks=True) time1, mem1 = benchCode(splitLc, lc_temp, 4) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 del times2, counts2, lc_temp time1, mem1 = benchCode(sortLcTime, lc) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 time1, mem1 = benchCode(sortLcCount, lc) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 time1, mem1 = benchCode(chunkAnlyze, lc, 100000, lambda x: np.mean(x)) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 time1, mem1 = benchCode(chunkLen, lc, 10000, 10000) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 lc_other = Lightcurve(times, counts * np.random.rand(size), dt=1.0, skip_checks=True) time1, mem1 = benchCode(joinLc, lc, lc_other) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 time1, mem1 = benchCode(createCspec, lc, lc_other) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 cspec = Crossspectrum(lc, lc_other, dt=1.0) time1, mem1 = benchCode(rebinCspec, cspec) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 time1, mem1 = benchCode(coherCspec, cspec) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 time1, mem1 = benchCode(TlagCspec, cspec) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 del cspec time1, mem1 = benchCode(createAvgCspec, lc, lc_other, 10000) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 avg_cspec = AveragedCrossspectrum(lc, lc_other, 10000, silent=True) time1, mem1 = benchCode(coherAvgCspec, avg_cspec) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 time1, mem1 = benchCode(TlagAvgCspec, avg_cspec) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 del avg_cspec, lc_other time1, mem1 = benchCode(createPspec, lc) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 pspec = Powerspectrum(lc) time1, mem1 = benchCode(rebinPspec, pspec) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 time1, mem1 = benchCode(pspecRMS, pspec) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 temp_pspec = Powerspectrum(lc, norm='leahy') time1, mem1 = benchCode(classSign, temp_pspec) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 del pspec, temp_pspec time1, mem1 = benchCode(createAvgPspec, lc, 10000) wall_time[num_func].append(time1) mem_use[num_func].append(mem1) num_func += 1 del lc, time1, mem1 CSVWriter(func_dict, wall_time, mem_use) del func_dict, wall_time, mem_use
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_no_segment_size(self): with pytest.raises(ValueError): cs = AveragedCrossspectrum(self.lc1, self.lc2)