def test_correlation_t2(self):
        '''Test for EnsembleSeries.correlation() when the target is an EnsembleSeries with less number of Series
        '''
        nt = 100
        t0, v0 = gen_colored_noise(nt=nt)
        t0, noise = gen_normal(nt=nt)

        ts0 = pyleo.Series(time=t0, value=v0)
        ts1 = pyleo.Series(time=t0, value=v0 + noise)
        ts2 = pyleo.Series(time=t0, value=v0 + 2 * noise)
        ts3 = pyleo.Series(time=t0, value=v0 + 1 / 2 * noise)
        ts4 = pyleo.Series(time=t0, value=v0 + 3 / 2 * noise)

        ts_list1 = [ts0, ts1, ts4]
        ts_list2 = [ts2, ts3]

        ts_ens = pyleo.EnsembleSeries(ts_list1)
        ts_target = pyleo.EnsembleSeries(ts_list2)

        corr_res = ts_ens.correlation(ts_target)
        signif_list = corr_res.signif
        for signif in signif_list:
            assert signif is True

        assert np.size(corr_res.p) == np.size(ts_list1)
Beispiel #2
0
    def test_plot(self):

        #Generate time and value arrays
        t_0, v_0 = gen_normal()
        t_1, v_1 = gen_normal()

        #Create series objects
        ts_0 = pyleo.Series(time=t_0, value=v_0)
        ts_1 = pyleo.Series(time=t_1, value=v_1)

        #Create a list of series objects
        serieslist = [ts_0, ts_1]

        #Turn this list into a multiple series object
        ts_M = pyleo.MultipleSeries(serieslist)

        fig, ax = ts_M.plot()

        lines_0 = ax.lines[0]
        lines_1 = ax.lines[1]

        x_plot_0 = lines_0.get_xdata()
        y_plot_0 = lines_0.get_ydata()

        x_plot_1 = lines_1.get_xdata()
        y_plot_1 = lines_1.get_ydata()

        assert_array_equal(t_0, x_plot_0)
        assert_array_equal(t_1, x_plot_1)
        assert_array_equal(v_0, y_plot_0)
        assert_array_equal(v_1, y_plot_1)
Beispiel #3
0
 def test_xwave_t0(self, xwave_method):
     ''' Test Series.wavelet_coherence() with available methods using default arguments
     Note: this function will expand as more methods become available for testing
     '''
     alpha = 1
     t, v = gen_colored_noise(nt=500, alpha=alpha)
     t1, v1 = gen_colored_noise(nt=500, alpha=alpha)
     ts = pyleo.Series(time=t, value=v)
     ts1 = pyleo.Series(time=t1, value=v1)
     scal = ts.wavelet_coherence(ts1, method=xwave_method)
Beispiel #4
0
 def test_xwave_t1(self):
     ''' Test Series.wavelet_coherence() with WWZ with specified frequency vector passed via `settings`
     '''
     alpha = 1
     t, v = gen_colored_noise(nt=500, alpha=alpha)
     t1, v1 = gen_colored_noise(nt=500, alpha=alpha)
     ts = pyleo.Series(time=t, value=v)
     ts1 = pyleo.Series(time=t1, value=v1)
     freq = np.linspace(1 / 500, 1 / 2, 20)
     scal = ts.wavelet_coherence(ts1, method='wwz', settings={'freq': freq})
Beispiel #5
0
 def test_filter_t1(self):
     ''' Band-pass filtering with Butterworth
     '''
     t = np.linspace(0, 1, 1000)
     sig1 = np.sin(2 * np.pi * 10 * t)
     sig2 = np.sin(2 * np.pi * 20 * t)
     sig = sig1 + sig2
     ts1 = pyleo.Series(time=t, value=sig1)
     ts2 = pyleo.Series(time=t, value=sig2)
     ts = pyleo.Series(time=t, value=sig)
     ts_bp = ts.filter(cutoff_freq=[15, 25])
     val_diff = ts_bp.value - ts2.value
     assert np.mean(val_diff**2) < 0.1
Beispiel #6
0
    def test_causality_t0(self, method, eps=0.1):
        ''' Generate two series from a same basic series and calculate their correlation

        Note: NO assert statements for this test yet
        '''
        alpha = 1
        nt = 100
        t, v = gen_colored_noise(nt=nt, alpha=alpha)
        v1 = v + np.random.normal(loc=0, scale=1, size=nt)
        v2 = v + np.random.normal(loc=0, scale=2, size=nt)

        ts1 = pyleo.Series(time=t, value=v1)
        ts2 = pyleo.Series(time=t, value=v2)

        causal_res = ts1.causality(ts2, method=method)
Beispiel #7
0
    def test_correlation_t1(self, corr_method, eps=0.1):
        ''' Generate two colored noise series calculate their correlation
        '''
        alpha = 1
        nt = 1000
        t = np.arange(nt)
        v1 = np.random.normal(loc=0, scale=1, size=nt)
        v2 = np.random.normal(loc=0, scale=1, size=nt)

        ts1 = pyleo.Series(time=t, value=v1)
        ts2 = pyleo.Series(time=t, value=v2)

        corr_res = ts1.correlation(ts2, settings={'method': corr_method})
        r = corr_res['r']
        assert np.abs(r - 0) < eps
Beispiel #8
0
    def test_correlation_t0(self, corr_method, eps=0.1):
        ''' Generate two series from a same basic series and calculate their correlation
        '''
        alpha = 1
        nt = 100
        t, v = gen_colored_noise(nt=nt, alpha=alpha)
        v1 = v + np.random.normal(loc=0, scale=1, size=nt)
        v2 = v + np.random.normal(loc=0, scale=2, size=nt)

        ts1 = pyleo.Series(time=t, value=v1)
        ts2 = pyleo.Series(time=t, value=v2)

        corr_res = ts1.correlation(ts2, settings={'method': corr_method})
        r = corr_res['r']
        assert np.abs(r - 1) < eps
Beispiel #9
0
 def test_ssa_t3(self):
     '''Test Series.ssa() with Kaiser truncation
     '''
     alpha = 1
     t, v = gen_colored_noise(nt=500, alpha=1.0)
     ts = pyleo.Series(time=t, value=v)
     res = ts.ssa(trunc='kaiser')
Beispiel #10
0
 def test_wave_t0(self, wave_method):
     ''' Test Series.wavelet() with available methods using default arguments
     '''
     alpha = 1
     t, v = gen_colored_noise(nt=500, alpha=alpha)
     ts = pyleo.Series(time=t, value=v)
     scal = ts.wavelet(method=wave_method)
def json_to_Series(filename):
    '''
    Open a JSON file and returns a pyleoclim.Series object

    Parameters
    ----------
    filename : str
        The name of the JSON file containing the Series information

    Returns
    -------
    ts : pyleoclim.Series
        A pyleoclim.Series object

    '''
    with open(filename, 'r') as f:
        t = json.load(f)
    ts = pyleo.Series(time=np.array(t['time']),
                      value=np.array(t['value']),
                      time_name=t['time_name'],
                      time_unit=t['time_unit'],
                      value_name=t['value_name'],
                      value_unit=t['value_unit'],
                      label=t['label'])
    return ts
def json_to_Scalogram(filename):
    with open(filename, 'r') as f:
        t = json.load(f)
    t = list_to_array(t)
    temp = t['timeseries']
    ts = pyleo.Series(time=np.array(temp['time']),
                      value=np.array(temp['value']),
                      time_name=temp['time_name'],
                      time_unit=temp['time_unit'],
                      value_name=temp['value_name'],
                      value_unit=temp['value_unit'],
                      label=t['label'])
    c = None
    if type(t['signif_qs']) is dict:
        c = Scalogram_to_MultipleScalogram(t['signif_qs']['scalogram_list'])
    else:
        c = t['signif_qs']

    scalogram = pyleo.Scalogram(frequency=np.array(t['frequency']),
                                time=np.array(t['time']),
                                amplitude=np.array(t['amplitude']),
                                coi=t['coi'],
                                label=t['label'],
                                timeseries=ts,
                                wave_method=t['wave_method'],
                                wave_args=t['wave_args'],
                                signif_qs=c,
                                signif_method=t['signif_method'])
    return scalogram
 def test_plot_t0(self):
     ''' Test PSD.plot() with default parameters
     '''
     alpha = 1
     t, v = gen_colored_noise(nt=500, alpha=alpha)
     ts = pyleo.Series(time=t, value=v)
     psd = ts.spectral(method='mtm')
     fig, ax = psd.plot(mute=True)
    def test_common_time(self, method):
        t_0, v_0 = gen_colored_noise()
        t_1, v_1 = gen_colored_noise()

        ts_0 = pyleo.Series(time=t_0, value=v_0)
        ts_1 = pyleo.Series(time=t_1, value=v_1)

        serieslist = [ts_0, ts_1]

        ts_M = pyleo.MultipleSeries(serieslist)

        ts_M_ct = ts_M.common_time(method=method)

        x_axis_0 = ts_M_ct.series_list[0].time
        x_axis_1 = ts_M_ct.series_list[1].time

        assert_array_equal(x_axis_0, x_axis_1)
    def test_gkernel(self):
        t_0, v_0 = gen_colored_noise()
        t_1, v_1 = gen_colored_noise()

        ts_0 = pyleo.Series(time=t_0, value=v_0)
        ts_1 = pyleo.Series(time=t_1, value=v_1)

        serieslist = [ts_0, ts_1]

        ts_M = pyleo.MultipleSeries(serieslist)

        ts_M_gkernel = ts_M.gkernel()

        x_axis_0 = ts_M_gkernel.series_list[0].__dict__['time']
        x_axis_1 = ts_M_gkernel.series_list[1].__dict__['time']

        assert_array_equal(x_axis_0, x_axis_1)
Beispiel #16
0
 def test_signif_test_t0(self):
     ''' Test scalogram.signif_test() with default parameters
     '''
     alpha = 1
     t, v = gen_colored_noise(nt=100, alpha=alpha)
     ts = pyleo.Series(time=t, value=v)
     scal = ts.wavelet()
     scal_signif = scal.signif_test(number=1)
 def test_signif_test_t0(self):
     ''' Test PSD.signif_test() with default parameters
     '''
     alpha = 1
     t, v = gen_colored_noise(nt=500, alpha=alpha)
     ts = pyleo.Series(time=t, value=v)
     psd = ts.spectral(method='mtm')
     psd_signif = psd.signif_test(number=10)
Beispiel #18
0
    def test_wave_t1(self, wave_method):
        '''Test Series.spectral() with WWZ/cwt with specified frequency vector passed via `settings`
        '''

        alpha = 1
        t, v = gen_colored_noise(nt=500, alpha=alpha)
        ts = pyleo.Series(time=t, value=v)
        freq = np.linspace(1 / 500, 1 / 2, 20)
        scal = ts.wavelet(method=wave_method, settings={'freq': freq})
Beispiel #19
0
    def test_ssa_t2(self):
        '''Test Series.ssa() with Monte-Carlo truncation
        '''

        alpha = 1
        t, v = gen_colored_noise(nt=500, alpha=1.0)
        ts = pyleo.Series(time=t, value=v)

        res = ts.ssa(M=60, nMC=10, trunc='mc-ssa')
Beispiel #20
0
 def test_xwave_t3(self):
     ''' Test Series.wavelet_coherence() with WWZ on unevenly spaced data
     '''
     alpha = 1
     t, v = gen_colored_noise(nt=550, alpha=alpha)
     t1, v1 = gen_colored_noise(nt=550, alpha=alpha)
     #remove points
     n_del = 50
     deleted_idx = np.random.choice(range(np.size(t)), n_del, replace=False)
     deleted_idx1 = np.random.choice(range(np.size(t1)),
                                     n_del,
                                     replace=False)
     t_unevenly = np.delete(t, deleted_idx)
     v_unevenly = np.delete(v, deleted_idx)
     t1_unevenly = np.delete(t1, deleted_idx1)
     v1_unevenly = np.delete(v1, deleted_idx1)
     ts = pyleo.Series(time=t_unevenly, value=v_unevenly)
     ts1 = pyleo.Series(time=t1_unevenly, value=v1_unevenly)
     scal = ts.wavelet_coherence(ts1, method='wwz')
Beispiel #21
0
    def test_segment_t1(self):
        '''Test that in the case of no segmentation, segment and original time series
        are the some object type'''
        t, v = gen_normal()

        ts = pyleo.Series(time=t, value=v)

        ts_seg = ts.segment()

        assert type(ts_seg) == type(ts)
    def test_correlation_t0(self):
        '''Test for EnsembleSeries.correlation() when the target is a Series
        '''
        nt = 100
        t0, v0 = gen_colored_noise(nt=nt)
        t0, noise = gen_normal(nt=nt)

        ts0 = pyleo.Series(time=t0, value=v0)
        ts1 = pyleo.Series(time=t0, value=v0 + noise)
        ts2 = pyleo.Series(time=t0, value=v0 + 2 * noise)

        ts_list = [ts1, ts2]

        ts_ens = pyleo.EnsembleSeries(ts_list)

        corr_res = ts_ens.correlation(ts0)
        signif_list = corr_res.signif
        for signif in signif_list:
            assert signif is True
Beispiel #23
0
    def test_detrend_t1(self, detrend_method):
        alpha = 1
        t, v = gen_colored_noise(nt=550, alpha=alpha)
        #Trends
        slope = 1e-5
        slope1 = 2e-5
        intercept = -1
        nonlinear_trend = slope * t**2 + intercept
        nonlinear_trend1 = slope1 * t**2 + intercept
        v_trend = v + nonlinear_trend
        v_trend1 = v + nonlinear_trend1

        #create series object
        ts = pyleo.Series(time=t, value=v_trend)
        ts1 = pyleo.Series(time=t, value=v_trend1)

        # Create a multiple series object
        ts_all = pyleo.MultipleSeries([ts, ts1])
        ts_detrend = ts_all.detrend(method=detrend_method)
Beispiel #24
0
    def test_spectral_t0(self, spec_method, eps=0.5):
        ''' Test Series.spectral() with available methods using default arguments

        We will estimate the scaling slope of an ideal colored noise to make sure the result is reasonable.
        '''
        alpha = 1
        t, v = gen_colored_noise(nt=1000, alpha=alpha)
        ts = pyleo.Series(time=t, value=v)
        psd = ts.spectral(method=spec_method)
        beta = psd.beta_est()['beta']
        assert np.abs(beta - alpha) < eps
Beispiel #25
0
    def test_spectral_t1(self, freq_method, eps=0.3):
        ''' Test Series.spectral() with MTM using available `freq_method` options with other arguments being default

        We will estimate the scaling slope of an ideal colored noise to make sure the result is reasonable.
        '''
        alpha = 1
        t, v = gen_colored_noise(nt=500, alpha=alpha)
        ts = pyleo.Series(time=t, value=v)
        psd = ts.spectral(method='mtm', freq_method=freq_method)
        beta = psd.beta_est()['beta']
        assert np.abs(beta - alpha) < eps
Beispiel #26
0
    def test_segment_t0(self):
        '''Test that in the case of segmentation, segment returns a Multiple Series object'''
        t = (1, 2, 3000)
        v = (1, 2, 3)

        ts = pyleo.Series(time=t, value=v)

        ts_seg = ts.segment()

        assert str(
            type(ts_seg)) == "<class 'pyleoclim.core.ui.MultipleSeries'>"
Beispiel #27
0
    def test_slice(self):
        t, v = gen_normal()

        ts = pyleo.Series(time=t, value=v)

        ts_slice = ts.slice(timespan=(10, 50, 80, 90))

        times = ts_slice.__dict__['time']

        assert min(times) == 10
        assert max(times) == 90
Beispiel #28
0
    def test_interp_t2(self):
        ''' Test the gkernel function with specified bandwidth'''

        t, v = gen_colored_noise(nt=550, alpha=1.0)
        # randomly remove some data pts
        n_del = 50
        deleted_idx = np.random.choice(range(np.size(t)), n_del, replace=False)
        t_unevenly = np.delete(t, deleted_idx)
        v_unevenly = np.delete(v, deleted_idx)

        ts = pyleo.Series(time=t_unevenly, value=v_unevenly)
        ts_interp = ts.gkernel(h=15)
Beispiel #29
0
    def test_interp_t3(self, interp_method):
        ''' Test the interp function with default parameter values'''
        alpha = 1
        t, v = gen_colored_noise(nt=550, alpha=alpha)
        # randomly remove some data pts
        n_del = 50
        deleted_idx = np.random.choice(range(np.size(t)), n_del, replace=False)
        t_unevenly = np.delete(t, deleted_idx)
        v_unevenly = np.delete(v, deleted_idx)

        ts = pyleo.Series(time=t_unevenly, value=v_unevenly)
        ts_interp = ts.interp(method=interp_method)
Beispiel #30
0
 def test_detrend_t1(self, detrend_method):
     #Generate data
     alpha = 1
     t, v = gen_colored_noise(nt=550, alpha=alpha)
     #Add a trend
     slope = 1e-5
     intercept = -1
     nonlinear_trend = slope * t**2 + intercept
     v_trend = v + nonlinear_trend
     #create a timeseries object
     ts = pyleo.Series(time=t, value=v_trend)
     ts_detrend = ts.detrend(method=detrend_method)