コード例 #1
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)
コード例 #2
0
ファイル: test_time_dataset.py プロジェクト: nubiofs/pyseries
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_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])
コード例 #4
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])
コード例 #5
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)
コード例 #6
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)
コード例 #7
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)
コード例 #8
0
ファイル: test_time_dataset.py プロジェクト: nubiofs/pyseries
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]])
コード例 #9
0
ファイル: test_time_dataset.py プロジェクト: nubiofs/pyseries
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()
コード例 #10
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])
コード例 #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 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))