예제 #1
0
def test_filter_lower():
    '''Tests the lower filter'''

    idx = np.array([0.0, 1.0, 2, 3, 4], dtype='d')
    data = np.array([1.0, 2, 3, 4, 5], dtype='d')
    ts = TimeSeries(idx, data)

    result = ts.filter_lower(1)
    assert_array_equal(result.timestamps, [0.0])
    assert_array_equal(result.data, [1.0])

    result = ts.filter_lower(0)
    assert_array_equal(result.timestamps, [])
    assert_array_equal(result.data, [])

    result = ts.filter_lower(3)
    assert_array_equal(result.timestamps, [0.0, 1.0, 2.0])
    assert_array_equal(result.data, [1.0, 2.0, 3.0])

    result = ts.filter_lower(-1)
    assert_array_equal(result.timestamps, [])
    assert_array_equal(result.data, [])

    result = ts.filter_lower(8)
    assert_array_equal(result.data, [1.0, 2.0, 3.0, 4.0, 5.0])
    assert_array_equal(result.timestamps, [0.0, 1.0, 2.0, 3.0, 4.0])

    expected = []
    for i, f in enumerate(np.arange(-0.5, 5.51, 1)):
        result = ts.filter_lower(f)
        assert_array_equal(result.timestamps, expected)
        if i < ts.timestamps.shape[0]:
            expected.append(ts.timestamps[i])
예제 #2
0
def test_to_empty():
    '''Valid empty creation test'''

    idx1 = np.array([], dtype='d')
    data1 = np.array([], dtype='d')

    ts1 = TimeSeries(idx1, data1)
    ts2 = TimeSeries(idx1.copy(), data1.copy())

    dataset = TimeSeriesDataset(np.asarray([ts1, ts2]))

    X = dataset.np_like_firstn()
    assert_array_equal(X, [[], []])

    X = dataset.np_like_lastn()
    assert_array_equal(X, [[], []])
예제 #3
0
def test_timeseries_creation_not_ok_shape():
    '''Error because of different shapes'''

    idx = np.array([1.0, 2, 3, 4], dtype='d')
    data = np.array([1.0, 2, 3, 4, 5], dtype='d')

    ts = TimeSeries(idx, data)
예제 #4
0
def test_timeseries_creation_not_ok_inverted():
    '''Error because of inverted idx'''

    idx = np.array([0, 1, 2, 3, 4], dtype='d')
    data = np.array([1.0, 2, 3, 4, 5], dtype='d')

    idx = idx[::-1]
    ts = TimeSeries(idx, data)
예제 #5
0
def test_timeseries_creation_not_ok_repeated():
    '''Error because not unique stamps'''

    idx = np.array([0, 1, 2, 3, 4], dtype='d')
    data = np.array([1.0, 2, 3, 4, 5], dtype='d')

    idx[0] = idx[1]
    ts = TimeSeries(idx, data)
예제 #6
0
def test_timeseries_creation_ok():
    '''Valid creation test'''

    idx = np.array([0, 1, 2, 3, 4], dtype='d')
    data = np.array([1.0, 2, 3, 4, 5], dtype='d')

    #OK creation
    ts = TimeSeries(idx, data)
예제 #7
0
def test_filter_lower():
    '''Tests the lower filter'''

    idx = np.array([0.0, 1.0, 2, 3, 4], dtype='d')
    data = np.array([1.0, 2, 3, 4, 5], dtype='d')
    ts = TimeSeries(idx, data)

    result = ts.filter_lower(1)
    assert_array_equal(result.timestamps, [0.0])
    assert_array_equal(result.data, [1.0])

    result = ts.filter_lower(0)
    assert_array_equal(result.timestamps, [])
    assert_array_equal(result.data, [])

    result = ts.filter_lower(3)
    assert_array_equal(result.timestamps, [0.0, 1.0, 2.0])
    assert_array_equal(result.data, [1.0, 2.0, 3.0])

    result = ts.filter_lower(-1)
    assert_array_equal(result.timestamps, [])
    assert_array_equal(result.data, [])

    result = ts.filter_lower(8)
    assert_array_equal(result.data, [1.0, 2.0, 3.0, 4.0, 5.0])
    assert_array_equal(result.timestamps, [0.0, 1.0, 2.0, 3.0, 4.0])

    expected = []
    for i, f in enumerate(np.arange(-0.5, 5.51, 1)):
        result = ts.filter_lower(f)
        assert_array_equal(result.timestamps, expected)
        if i < ts.timestamps.shape[0]:
            expected.append(ts.timestamps[i])
예제 #8
0
def test_filter_mid():
    '''Filters of a middle section'''

    idx = np.array([0.0, 1.0, 2, 3, 4], dtype='d')
    data = np.array([1.0, 2, 3, 4, 5], dtype='d')
    ts = TimeSeries(idx, data)

    result = ts.filter_mid(1, 3)
    assert_array_equal(result.timestamps, [1.0, 2.0])
    assert_array_equal(result.data, [2.0, 3.0])

    result = ts.filter_mid(0.5, 2.5)
    assert_array_equal(result.timestamps, [1.0, 2])
    assert_array_equal(result.data, [2.0, 3])

    result = ts.filter_mid(1.5, 3.5)
    assert_array_equal(result.timestamps, [2.0, 3.0])
    assert_array_equal(result.data, [3.0, 4.0])
예제 #9
0
def test_filter_mid():
    '''Filters of a middle section'''

    idx = np.array([0.0, 1.0, 2, 3, 4], dtype='d')
    data = np.array([1.0, 2, 3, 4, 5], dtype='d')
    ts = TimeSeries(idx, data)

    result = ts.filter_mid(1, 3)
    assert_array_equal(result.timestamps, [1.0, 2.0])
    assert_array_equal(result.data, [2.0, 3.0])

    result = ts.filter_mid(0.5, 2.5)
    assert_array_equal(result.timestamps, [1.0, 2])
    assert_array_equal(result.data, [2.0, 3])

    result = ts.filter_mid(1.5, 3.5)
    assert_array_equal(result.timestamps, [2.0, 3.0])
    assert_array_equal(result.data, [3.0, 4.0])
예제 #10
0
def test_to_numpy():
    '''Valid creation test'''

    idx1 = np.array([0, 1, 2, 3, 4], dtype='d')
    data1 = np.array([1.0, 2, 3, 4, 5], dtype='d')

    ts1 = TimeSeries(idx1, data1)

    idx2 = np.array([0, 1, 2, 3, 4, 5], dtype='d')
    data2 = np.array([1.0, 2, 3, 4, 8, 6], dtype='d')

    ts2 = TimeSeries(idx2, data2)

    dataset = TimeSeriesDataset(np.asarray([ts1, ts2]))

    X = dataset.np_like_firstn()
    assert_array_equal(X, [[1.0, 2, 3, 4, 5], [1.0, 2, 3, 4, 8]])

    X = dataset.np_like_lastn()
    assert_array_equal(X, [[1.0, 2, 3, 4, 5], [2, 3, 4, 8, 6]])
예제 #11
0
def test_filter_upper():
    '''Tests the upper filter'''

    idx = np.array([0.0, 1.0, 2, 3, 4], dtype='d')
    data = np.array([1.0, 2, 3, 4, 5], dtype='d')
    ts = TimeSeries(idx, data)

    result = ts.filter_upper(1)
    assert_array_equal(result.timestamps, [1.0, 2.0, 3.0, 4.0])
    assert_array_equal(result.data, [2.0, 3.0, 4.0, 5.0])

    result = ts.filter_upper(2.5)
    assert_array_equal(result.timestamps, [3.0, 4.0])
    assert_array_equal(result.data, [4.0, 5.0])

    result = ts.filter_upper(-1)
    assert_array_equal(result.timestamps, [0.0, 1.0, 2.0, 3.0, 4.0])
    assert_array_equal(result.data, [1.0, 2.0, 3.0, 4.0, 5.0])

    result = ts.filter_upper(8)
    assert_array_equal(result.timestamps, [])
    assert_array_equal(result.data, [])
예제 #12
0
def test_iter():
    '''Iterates over the time series'''

    idx1 = np.array([0, 1, 2, 3, 4], dtype='d')
    data1 = np.array([1.0, 2, 3, 4, 5], dtype='d')

    ts1 = TimeSeries(idx1, data1)

    idx2 = np.array([0, 1, 2, 3, 4, 5], dtype='d')
    data2 = np.array([1.0, 2, 3, 4, 8, 6], dtype='d')

    ts2 = TimeSeries(idx2, data2)

    dataset = TimeSeriesDataset(np.asarray([ts1, ts2]))
    for i, timeseries in enumerate(dataset):
        if i == 0:
            assert_equal(5, len(timeseries))
        if i == 1:
            assert_equal(6, len(timeseries))

        if i > 1:
            raise Exception()
예제 #13
0
def from_id_row_mat(matrix_fpath, skip_first_col=True, add_eps=0):
    '''
    Converts a file where each row composes of equally spaced time series
    observations. Moreover, all series have the same number of observations.
    The first row in the id of the time series, which will be ignored.
    
    Parameters
    ----------
    matrix_fpath : str 
        path to the matrix file
    skip_first_col : bool
        indicates that first column is an id which can be ignored
    add_eps : int (default=0)
        eps to add to each observatio
    
    Returns
    -------
    a time series dataset object
    '''
    X = np.genfromtxt(matrix_fpath, dtype='d')
    if X.ndim == 1:
        X = X[None]

    from_ = int(skip_first_col)
    X = X[:, from_:]
    X += add_eps

    n, f = X.shape
    tseries = []
    for i in xrange(n):
        idx = np.arange(f, dtype='d')
        data = X[i]

        ts = TimeSeries(idx, data)
        tseries.append(ts)

    return TimeSeriesDataset(np.asarray(tseries))
예제 #14
0
def test_filter_upper():
    '''Tests the upper filter'''

    idx = np.array([0.0, 1.0, 2, 3, 4], dtype='d')
    data = np.array([1.0, 2, 3, 4, 5], dtype='d')
    ts = TimeSeries(idx, data)

    result = ts.filter_upper(1)
    assert_array_equal(result.timestamps, [1.0, 2.0, 3.0, 4.0])
    assert_array_equal(result.data, [2.0, 3.0, 4.0, 5.0])

    result = ts.filter_upper(2.5)
    assert_array_equal(result.timestamps, [3.0, 4.0])
    assert_array_equal(result.data, [4.0, 5.0])

    result = ts.filter_upper(-1)
    assert_array_equal(result.timestamps, [0.0, 1.0, 2.0, 3.0, 4.0])
    assert_array_equal(result.data, [1.0, 2.0, 3.0, 4.0, 5.0])

    result = ts.filter_upper(8)
    assert_array_equal(result.timestamps, [])
    assert_array_equal(result.data, [])
예제 #15
0
def test_filter_lower_pair():
    '''Tests with pair shape arrays'''

    idx = np.array([], dtype='d')
    data = np.array([], dtype='d')
    ts = TimeSeries(idx, data)

    result = ts.filter_lower(1)
    assert_array_equal(result.data, [])
    assert_array_equal(result.timestamps, [])
    
    idx = np.array([1, 2, 3, 4, 5, 6], dtype='d')
    data = np.array([1, 2, 3, 4, 5, 6], dtype='d')
    ts = TimeSeries(idx, data)

    result = ts.filter_lower(1)
    assert_array_equal(result.data, [])
    assert_array_equal(result.timestamps, [])
    
    result = ts.filter_lower(0)
    assert_array_equal(result.data, [])
    assert_array_equal(result.timestamps, [])
    
    result = ts.filter_lower(2)
    assert_array_equal(result.data, [1])
    assert_array_equal(result.timestamps, [1])
    
    result = ts.filter_lower(2.5)
    assert_array_equal(result.data, [1, 2])
    assert_array_equal(result.timestamps, [1, 2])
    
    result = ts.filter_lower(5)
    assert_array_equal(result.data, [1, 2, 3, 4])
    assert_array_equal(result.timestamps, [1, 2, 3, 4])
    
    result = ts.filter_lower(3.5)
    assert_array_equal(result.data, [1, 2, 3])
    assert_array_equal(result.timestamps, [1, 2, 3])
    
    result = ts.filter_lower(5.5)
    assert_array_equal(result.data, [1, 2, 3, 4, 5])
    assert_array_equal(result.timestamps, [1, 2, 3, 4, 5])
    
    result = ts.filter_lower(6.5)
    assert_array_equal(result.data, [1, 2, 3, 4, 5, 6])
    assert_array_equal(result.timestamps, [1, 2, 3, 4, 5, 6])
예제 #16
0
def test_filter_lower_pair():
    '''Tests with pair shape arrays'''

    idx = np.array([], dtype='d')
    data = np.array([], dtype='d')
    ts = TimeSeries(idx, data)

    result = ts.filter_lower(1)
    assert_array_equal(result.data, [])
    assert_array_equal(result.timestamps, [])

    idx = np.array([1, 2, 3, 4, 5, 6], dtype='d')
    data = np.array([1, 2, 3, 4, 5, 6], dtype='d')
    ts = TimeSeries(idx, data)

    result = ts.filter_lower(1)
    assert_array_equal(result.data, [])
    assert_array_equal(result.timestamps, [])

    result = ts.filter_lower(0)
    assert_array_equal(result.data, [])
    assert_array_equal(result.timestamps, [])

    result = ts.filter_lower(2)
    assert_array_equal(result.data, [1])
    assert_array_equal(result.timestamps, [1])

    result = ts.filter_lower(2.5)
    assert_array_equal(result.data, [1, 2])
    assert_array_equal(result.timestamps, [1, 2])

    result = ts.filter_lower(5)
    assert_array_equal(result.data, [1, 2, 3, 4])
    assert_array_equal(result.timestamps, [1, 2, 3, 4])

    result = ts.filter_lower(3.5)
    assert_array_equal(result.data, [1, 2, 3])
    assert_array_equal(result.timestamps, [1, 2, 3])

    result = ts.filter_lower(5.5)
    assert_array_equal(result.data, [1, 2, 3, 4, 5])
    assert_array_equal(result.timestamps, [1, 2, 3, 4, 5])

    result = ts.filter_lower(6.5)
    assert_array_equal(result.data, [1, 2, 3, 4, 5, 6])
    assert_array_equal(result.timestamps, [1, 2, 3, 4, 5, 6])