Skip to content

Commit

Permalink
test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderVNikitin committed Sep 26, 2023
1 parent 189f496 commit 4bac162
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
12 changes: 12 additions & 0 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


def test_statistics():
eps = 1e-8
ts = np.array([
[[0, 2], [11, -11], [1, 2]],
[[10, 21], [1, -1], [6, 8]]])
Expand All @@ -23,6 +24,12 @@ def test_statistics():

assert (tsgm.metrics.statistics.axis_mode_s(ts, axis=None) == [1]).all()

assert (tsgm.metrics.statistics.axis_mean_s(ts, axis=None) - np.asarray([4.16666667]) < eps).all()
assert (tsgm.metrics.statistics.axis_mean_s(ts, axis=1) - np.asarray([4.83333333, 3.5]) < eps).all()
assert (tsgm.metrics.statistics.axis_mean_s(ts, axis=2) - np.asarray([8.25, 0., 4.25]) < eps).all()

assert (tsgm.metrics.statistics.axis_percentile_s(ts, axis=None, percentile=50) - np.asarray([2]) < eps).all()

# Now, checking with tf.Tensor
ts_tf = tf.convert_to_tensor(ts)

Expand All @@ -36,6 +43,11 @@ def test_statistics():
assert (tsgm.metrics.statistics.axis_min_s(ts_tf, axis=2) == [0, -11, 1]).all()

assert (tsgm.metrics.statistics.axis_mode_s(ts_tf, axis=None) == [1]).all()
assert (tsgm.metrics.statistics.axis_mean_s(ts_tf, axis=None) - np.asarray([4.16666667]) < eps).all()
assert (tsgm.metrics.statistics.axis_mean_s(ts_tf, axis=1) - np.asarray([4.83333333, 3.5]) < eps).all()
assert (tsgm.metrics.statistics.axis_mean_s(ts, axis=2) - np.asarray([8.25, 0., 4.25]) < eps).all()

assert (tsgm.metrics.statistics.axis_percentile_s(ts_tf, axis=None, percentile=50) - np.asarray([2]) < eps).all()


def test_distance_metric():
Expand Down
22 changes: 22 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest

import os
import uuid
import functools
import numpy as np
import random
Expand Down Expand Up @@ -64,6 +66,10 @@ def test_ucr_manager():
X_train, y_train, X_test, y_test = ucr_data_manager.get()
assert X_train.shape == (50, 150) and X_test.shape == (150, 150)

# test y_all is None
ucr_data_manager.y_all = None
assert ucr_data_manager.get_classes_distribution() == {}


def test_sine_vs_const_dataset():
Xs, ys = tsgm.utils.gen_sine_vs_const_dataset(10, 100, 20, max_value=2, const=1)
Expand Down Expand Up @@ -261,3 +267,19 @@ def test_get_physionet2012():

assert val_X.shape == (1765303, 4)
assert val_y.shape == (4000, 6)


def test_download(mocker, caplog):
file_download_mock = mocker.patch("urllib.request.urlretrieve")
resource_name = f"resource_{uuid.uuid4()}"
resource_folder = "./tmp/test_download/"
os.makedirs(resource_folder, exist_ok=True)
resource_path = os.path.join(resource_folder, resource_name)
open(resource_path, 'w')
try:
with pytest.raises(ValueError) as excinfo:
tsgm.utils.download(f"https://pseudourl/{resource_name}", resource_folder, md5=123, max_attempt=1)
assert "Reference md5 value (123) is not equal to the downloaded" in caplog.text
assert "Cannot download dataset" in str(excinfo.value)
finally:
os.remove(resource_path)
15 changes: 10 additions & 5 deletions tests/test_visualizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import tsgm


def test_visualize_dataset():
Xs = np.array([[[1, 2, 3], [3, 4, 5]]])
tsgm.utils.visualize_dataset(Xs)
@pytest.mark.parametrize("ds", [
np.array([[[1, 2, 3], [3, 4, 5]]]), tsgm.dataset.Dataset(np.array([[[1, 2, 3], [3, 4, 5]]]), y=None)
])
def test_visualize_dataset(ds):
tsgm.utils.visualize_dataset(ds)


@pytest.mark.parametrize("feature_averaging", [
Expand All @@ -25,7 +27,10 @@ def test_visualize_tsne_unlabeled(feature_averaging):
tsgm.utils.visualize_tsne_unlabeled(Xs, Xgen, perplexity=2, feature_averaging=feature_averaging)


def test_visualize_tsne():
@pytest.mark.parametrize("feature_averaging", [
True, False
])
def test_visualize_tsne(feature_averaging):
Xs = np.array([
[[1, 2, 3], [3, 4, 5]],
[[1, 2, 3], [3, 4, 5]],
Expand All @@ -36,7 +41,7 @@ def test_visualize_tsne():
X_gen = Xs
ys = np.ones((Xs.shape[0], 1))
y_gen = ys
tsgm.utils.visualize_tsne(X=Xs, y=ys, X_gen=X_gen, y_gen=y_gen, perplexity=2)
tsgm.utils.visualize_tsne(X=Xs, y=ys, X_gen=X_gen, y_gen=y_gen, perplexity=2, feature_averaging=feature_averaging)


def test_visualize_ts():
Expand Down

0 comments on commit 4bac162

Please sign in to comment.