diff --git a/tests/test_metrics.py b/tests/test_metrics.py index 590aa2d..b49393a 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -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]]]) @@ -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) @@ -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(): diff --git a/tests/test_utils.py b/tests/test_utils.py index 2c2afcb..d8f605e 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,5 +1,7 @@ import pytest +import os +import uuid import functools import numpy as np import random @@ -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) @@ -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) diff --git a/tests/test_visualizations.py b/tests/test_visualizations.py index 36033c2..b686a13 100644 --- a/tests/test_visualizations.py +++ b/tests/test_visualizations.py @@ -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", [ @@ -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]], @@ -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():