Exemplo n.º 1
0
 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_high_coherence(self):
     t = np.arange(1280)
     a = np.random.poisson(100, len(t))
     lc = Lightcurve(t, a)
     c = AveragedCrossspectrum(lc, lc, 128)
     coh, _ = c.coherence()
     np.testing.assert_almost_equal(np.mean(coh).real, 1.0)
Exemplo n.º 4
0
    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])
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
    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)
Exemplo n.º 8
0
    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])
Exemplo n.º 9
0
    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])
Exemplo n.º 10
0
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])
Exemplo n.º 11
0
def createAvgCspec(lc1, lc2, seg):
    AveragedCrossspectrum(lc1, lc2, seg, silent=True)
Exemplo n.º 12
0
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
Exemplo n.º 13
0
 def test_rebin_with_valid_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')
     assert aps.rebin(df=new_df)
Exemplo n.º 14
0
 def test_failure_when_normalization_not_recognized(self):
     with pytest.raises(ValueError):
         self.cs = AveragedCrossspectrum(self.lc1, self.lc2,
                                         segment_size=1,
                                         norm="wrong")
Exemplo n.º 15
0
 def test_failure_when_normalization_not_recognized(self):
     with pytest.raises(ValueError):
         self.cs = AveragedCrossspectrum(self.lc1, self.lc2,
                                         segment_size=1,
                                         norm="wrong")
Exemplo n.º 16
0
class TestAveragedCrossspectrum(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 = 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 len(w) == 1
            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")
Exemplo n.º 17
0
 def test_init_with_invalid_norm(self):
     with pytest.raises(ValueError):
         cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1,
                                    norm='frabs')
Exemplo n.º 18
0
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))
Exemplo n.º 19
0
 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)
Exemplo n.º 20
0
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.)))
Exemplo n.º 21
0
class TestAveragedCrossspectrum(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 = 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 len(w) == 1
            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")
Exemplo n.º 22
0
 def test_no_segment_size(self):
     with pytest.raises(ValueError):
         cs = AveragedCrossspectrum(self.lc1, self.lc2)
Exemplo n.º 23
0
class TestAveragedCrossspectrum(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 = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1)

    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 len(w) == 1
            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")
Exemplo n.º 24
0
 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")
Exemplo n.º 25
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))
Exemplo n.º 26
0
 def test_init_with_norm_not_str(self):
     with pytest.raises(TypeError):
         cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1,
                                    norm=1)
Exemplo n.º 27
0
 def test_save_all(self):
     cs = AveragedCrossspectrum(self.lc1,
                                self.lc2,
                                segment_size=1,
                                save_all=True)
     assert hasattr(cs, 'cs_all')
Exemplo n.º 28
0
 def test_init_with_inifite_segment_size(self):
     with pytest.raises(ValueError):
         cs = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=np.inf)
Exemplo n.º 29
0
class TestAveragedCrossspectrum(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 = AveragedCrossspectrum(self.lc1, self.lc2, segment_size=1)

    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(AssertionError):
            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 len(w) == 1
            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")
Exemplo n.º 30
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))
Exemplo n.º 31
0
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))