Skip to content

Commit

Permalink
TST: rework and add tests in test_timeseries
Browse files Browse the repository at this point in the history
- subsitute old tests by parametrized test for CrossRecurrencePlot
- add parametrized test for InterSystemRecurrenceNetwork
- testing agains issues pik-copan#128 and pik-copan#145
- add fixture params for fixed recurrence rate mode when pik-copan#188 is solved
  • Loading branch information
fkuehlein committed Jul 27, 2023
1 parent e5dfcad commit 6b96ced
Showing 1 changed file with 52 additions and 35 deletions.
87 changes: 52 additions & 35 deletions tests/test_timeseries/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
Simple tests for the timeseries class.
"""

import pytest
import numpy as np
from numpy.testing import assert_array_almost_equal

from pyunicorn.timeseries import CrossRecurrencePlot, VisibilityGraph, \
Surrogates
from pyunicorn.timeseries import CrossRecurrencePlot, \
InterSystemRecurrenceNetwork, Surrogates, VisibilityGraph
from pyunicorn.core.data import Data
from pyunicorn.core._ext.types import DFIELD

Expand All @@ -35,7 +36,6 @@
def create_test_data():
# Create test time series
tdata = Data.SmallTestData().observable()
n_index, n_times = tdata.shape
# subtract means form the input data
tdata -= np.mean(tdata, axis=1)[:, None]
# normalize the data
Expand All @@ -48,42 +48,59 @@ def create_test_data():
# -----------------------------------------------------------------------------


def testCrossRecurrencePlot():
tdata = create_test_data()
CrossRecurrencePlot(x=tdata, y=tdata, threshold=0.2)


def testDistanceMatrix():
tdata = create_test_data()
crp = CrossRecurrencePlot(x=tdata, y=tdata, threshold=1.0)
crp.distance_matrix(tdata.T, tdata.T, metric='manhattan')

@pytest.mark.parametrize("met", ["supremum", "euclidean", "manhattan"])
@pytest.mark.parametrize(
"thresh, rr", [(.2, None)]) # add (None, .2) when #188 solved
def testCrossRecurrencePlot(thresh, rr, met):
# create two instances of the same test dataset
tdata1 = create_test_data(); x1 = tdata1[:,0]; y1 = tdata1[:,1]
tdata2 = create_test_data(); x2 = tdata2[:,0]; y2 = tdata2[:,1]
# create CrossRecurrencePlot for both
crp1 = CrossRecurrencePlot(
x1, y1, threshold=thresh, recurrence_rate=rr, metric=met)
crp2 = CrossRecurrencePlot(
x2, y2, threshold=thresh, recurrence_rate=rr, metric=met)
# get respective distance matrices
dist_1 = crp1.distance_matrix(crp1.x_embedded, crp1.y_embedded, met)
dist_2 = crp2.distance_matrix(crp2.x_embedded, crp2.y_embedded, met)
# get respective recurrence matrices
CR1 = crp1.recurrence_matrix()
CR2 = crp2.recurrence_matrix()

assert np.allclose(dist_1, dist_2, atol=1e-04)
assert CR1.shape == CR2.shape
assert CR1.shape == (len(x1), len(y1))
assert CR1.dtype == CR2.dtype
assert CR1.dtype == np.int8

def testManhattanDistanceMatrix():
tdata = create_test_data()
n_index, n_times = tdata.shape
crp = CrossRecurrencePlot(x=tdata, y=tdata, threshold=1.0)
manh_dist_1 = crp.manhattan_distance_matrix(tdata.T, tdata.T)
manh_dist_2 = crp.manhattan_distance_matrix(tdata.T, tdata.T)
assert np.allclose(manh_dist_1, manh_dist_2, atol=1e-04)


def testEuclideanDistanceMatrix():
tdata = create_test_data()
n_index, n_times = tdata.shape
crp = CrossRecurrencePlot(x=tdata, y=tdata, threshold=1.0)
eucl_dist_1 = crp.euclidean_distance_matrix(tdata.T, tdata.T)
eucl_dist_2 = crp.euclidean_distance_matrix(tdata.T, tdata.T)
assert np.allclose(eucl_dist_1, eucl_dist_2, atol=1e-04)
# -----------------------------------------------------------------------------
# inter_system_recurrence_network
# -----------------------------------------------------------------------------


def testSupremumDistanceMatrix():
tdata = create_test_data()
n_index, n_times = tdata.shape
crp = CrossRecurrencePlot(x=tdata, y=tdata, threshold=1.0)
supr_dist_1 = crp.supremum_distance_matrix(tdata.T, tdata.T)
supr_dist_2 = crp.supremum_distance_matrix(tdata.T, tdata.T)
assert np.allclose(supr_dist_1, supr_dist_2, atol=1e-04)
@pytest.mark.parametrize("met", ["supremum", "euclidean", "manhattan"])
@pytest.mark.parametrize(
"thresh, rr", [((.2, .3, .2), None)]
) # add (None, (.2, .3, .2) when #188 solved
def testInterSystemRecurrenceNetwork(thresh, rr, met):
# create two instances of the same test dataset
tdata1 = create_test_data(); x1 = tdata1[:,0]; y1 = tdata1[:,1]
tdata2 = create_test_data(); x2 = tdata2[:,0]; y2 = tdata2[:,1]
# create InterSystemRecurrenceNetwork for both
isrn1 = InterSystemRecurrenceNetwork(
x1, y1, threshold=thresh, recurrence_rate=rr, metric=met)
isrn2 = InterSystemRecurrenceNetwork(
x2, y2, threshold=thresh, recurrence_rate=rr, metric=met)
# get respective adjacency matrices
A1 = isrn1.adjacency
A2 = isrn2.adjacency

assert np.array_equal(A1, A2)
assert A1.shape == A2.shape
assert A1.shape == (len(x1)*2, len(y1)*2)
assert A1.dtype == A2.dtype
assert A1.dtype == np.int16


# -----------------------------------------------------------------------------
Expand Down

0 comments on commit 6b96ced

Please sign in to comment.