From a12e50dcd98b6ce30565836957c2432f37c1031a Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Tue, 14 Nov 2023 15:39:06 +0100 Subject: [PATCH] update: improve tests for complexity markers --- .../complexity/tests/test_hurst_exponent.py | 43 ++++++++----------- .../tests/test_multiscale_entropy_auc.py | 41 ++++++++---------- .../complexity/tests/test_perm_entropy.py | 41 ++++++++---------- .../complexity/tests/test_range_entropy.py | 41 ++++++++---------- .../tests/test_range_entropy_auc.py | 42 ++++++++---------- .../complexity/tests/test_sample_entropy.py | 41 ++++++++---------- .../tests/test_weighted_perm_entropy.py | 40 ++++++++--------- 7 files changed, 127 insertions(+), 162 deletions(-) diff --git a/junifer/markers/complexity/tests/test_hurst_exponent.py b/junifer/markers/complexity/tests/test_hurst_exponent.py index 09413cb61e..24ebee564a 100644 --- a/junifer/markers/complexity/tests/test_hurst_exponent.py +++ b/junifer/markers/complexity/tests/test_hurst_exponent.py @@ -1,18 +1,23 @@ """Provide test for Hurst exponent.""" # Authors: Amir Omidvarnia +# Synchon Mandal # License: AGPL -import os from pathlib import Path -from nilearn.maskers import NiftiLabelsMasker +import pytest -from junifer.data import load_parcellation -from junifer.datareader import DefaultDataReader -from junifer.markers import HurstExponent -from junifer.storage import SQLiteFeatureStorage -from junifer.testing.datagrabbers import SPMAuditoryTestingDatagrabber + +pytest.importorskip("neurokit2") + + +from junifer.datareader import DefaultDataReader # noqa: E402 +from junifer.markers.complexity import HurstExponent # noqa: E402 +from junifer.storage import SQLiteFeatureStorage # noqa: E402 +from junifer.testing.datagrabbers import ( # noqa: E402 + SPMAuditoryTestingDataGrabber, +) # Set parcellation @@ -21,7 +26,7 @@ def test_compute() -> None: """Test HurstExponent compute().""" - with SPMAuditoryTestingDatagrabber() as dg: + with SPMAuditoryTestingDataGrabber() as dg: # Fetch element element = dg["sub001"] # Fetch element data @@ -30,17 +35,8 @@ def test_compute() -> None: marker = HurstExponent(parcellation=PARCELLATION) # Compute the marker feature_map = marker.fit_transform(element_data) - - # Load parcellation - test_parcellation, _, _ = load_parcellation(PARCELLATION) - # Compute the NiftiLabelsMasker - test_masker = NiftiLabelsMasker(test_parcellation) - test_ts = test_masker.fit_transform(element_data["BOLD"]["data"]) - _, n_roi = test_ts.shape - - # Assert the dimension of timeseries - _, n_roi2 = feature_map["BOLD"]["data"].shape - assert n_roi == n_roi2 + # Assert the dimension of timeseries + assert feature_map["BOLD"]["data"].ndim == 2 def test_get_output_type() -> None: @@ -58,17 +54,16 @@ def test_store(tmp_path: Path) -> None: The path to the test directory. """ - with SPMAuditoryTestingDatagrabber() as dg: + with SPMAuditoryTestingDataGrabber() as dg: # Fetch element element = dg["sub001"] # Fetch element data element_data = DefaultDataReader().fit_transform(element) # Initialize the marker marker = HurstExponent(parcellation=PARCELLATION) - # Create storage - # tmp_path = "/home/aomidvarnia/tmp" - storage_uri = os.path.join(tmp_path, "test_hurst_exponent.sqlite") - storage = SQLiteFeatureStorage(uri=storage_uri) + storage = SQLiteFeatureStorage( + uri=tmp_path / "test_hurst_exponent.sqlite" + ) # Compute the marker and store marker.fit_transform(input=element_data, storage=storage) diff --git a/junifer/markers/complexity/tests/test_multiscale_entropy_auc.py b/junifer/markers/complexity/tests/test_multiscale_entropy_auc.py index 879d069608..924202d23e 100644 --- a/junifer/markers/complexity/tests/test_multiscale_entropy_auc.py +++ b/junifer/markers/complexity/tests/test_multiscale_entropy_auc.py @@ -1,18 +1,22 @@ """Provide test for the AUC of multiscale entropy.""" # Authors: Amir Omidvarnia +# Synchon Mandal # License: AGPL -import os from pathlib import Path -from nilearn.maskers import NiftiLabelsMasker +import pytest -from junifer.data import load_parcellation -from junifer.datareader import DefaultDataReader -from junifer.markers import MultiscaleEntropyAUC -from junifer.storage import SQLiteFeatureStorage -from junifer.testing.datagrabbers import SPMAuditoryTestingDatagrabber + +pytest.importorskip("neurokit2") + +from junifer.datareader import DefaultDataReader # noqa: E402 +from junifer.markers.complexity import MultiscaleEntropyAUC # noqa: E402 +from junifer.storage import SQLiteFeatureStorage # noqa: E402 +from junifer.testing.datagrabbers import ( # noqa: E402 + SPMAuditoryTestingDataGrabber, +) # Set parcellation @@ -21,7 +25,7 @@ def test_compute() -> None: """Test MultiscaleEntropyAUC compute().""" - with SPMAuditoryTestingDatagrabber() as dg: + with SPMAuditoryTestingDataGrabber() as dg: # Fetch element element = dg["sub001"] # Fetch element data @@ -30,17 +34,8 @@ def test_compute() -> None: marker = MultiscaleEntropyAUC(parcellation=PARCELLATION) # Compute the marker feature_map = marker.fit_transform(element_data) - - # Load parcellation - test_parcellation, _, _ = load_parcellation(PARCELLATION) - # Compute the NiftiLabelsMasker - test_masker = NiftiLabelsMasker(test_parcellation) - test_ts = test_masker.fit_transform(element_data["BOLD"]["data"]) - _, n_roi = test_ts.shape - - # Assert the dimension of timeseries - _, n_roi2 = feature_map["BOLD"]["data"].shape - assert n_roi == n_roi2 + # Assert the dimension of timeseries + assert feature_map["BOLD"]["data"].ndim == 2 def test_get_output_type() -> None: @@ -58,18 +53,16 @@ def test_store(tmp_path: Path) -> None: The path to the test directory. """ - with SPMAuditoryTestingDatagrabber() as dg: + with SPMAuditoryTestingDataGrabber() as dg: # Fetch element element = dg["sub001"] # Fetch element data element_data = DefaultDataReader().fit_transform(element) # Initialize the marker marker = MultiscaleEntropyAUC(parcellation=PARCELLATION) - # Create storage - storage_uri = os.path.join( - tmp_path, "test_multiscale_entropy_auc.sqlite" + storage = SQLiteFeatureStorage( + uri=tmp_path / "test_multiscale_entropy_auc.sqlite" ) - storage = SQLiteFeatureStorage(uri=storage_uri) # Compute the marker and store marker.fit_transform(input=element_data, storage=storage) diff --git a/junifer/markers/complexity/tests/test_perm_entropy.py b/junifer/markers/complexity/tests/test_perm_entropy.py index 1279e130e3..7f3826ddf1 100644 --- a/junifer/markers/complexity/tests/test_perm_entropy.py +++ b/junifer/markers/complexity/tests/test_perm_entropy.py @@ -1,18 +1,22 @@ """Provide test for permutation entropy.""" # Authors: Amir Omidvarnia +# Synchon Mandal # License: AGPL -import os from pathlib import Path -from nilearn.maskers import NiftiLabelsMasker +import pytest -from junifer.data import load_parcellation -from junifer.datareader import DefaultDataReader -from junifer.markers import PermEntropy -from junifer.storage import SQLiteFeatureStorage -from junifer.testing.datagrabbers import SPMAuditoryTestingDatagrabber + +pytest.importorskip("neurokit2") + +from junifer.datareader import DefaultDataReader # noqa: E402 +from junifer.markers.complexity import PermEntropy # noqa: E402 +from junifer.storage import SQLiteFeatureStorage # noqa: E402 +from junifer.testing.datagrabbers import ( # noqa: E402 + SPMAuditoryTestingDataGrabber, +) # Set parcellation @@ -21,7 +25,7 @@ def test_compute() -> None: """Test PermEntropy compute().""" - with SPMAuditoryTestingDatagrabber() as dg: + with SPMAuditoryTestingDataGrabber() as dg: # Fetch element element = dg["sub001"] # Fetch element data @@ -30,17 +34,8 @@ def test_compute() -> None: marker = PermEntropy(parcellation=PARCELLATION) # Compute the marker feature_map = marker.fit_transform(element_data) - - # Load parcellation - test_parcellation, _, _ = load_parcellation(PARCELLATION) - # Compute the NiftiLabelsMasker - test_masker = NiftiLabelsMasker(test_parcellation) - test_ts = test_masker.fit_transform(element_data["BOLD"]["data"]) - _, n_roi = test_ts.shape - - # Assert the dimension of timeseries - _, n_roi2 = feature_map["BOLD"]["data"].shape - assert n_roi == n_roi2 + # Assert the dimension of timeseries + assert feature_map["BOLD"]["data"].ndim == 2 def test_get_output_type() -> None: @@ -58,7 +53,7 @@ def test_store(tmp_path: Path) -> None: The path to the test directory. """ - with SPMAuditoryTestingDatagrabber() as dg: + with SPMAuditoryTestingDataGrabber() as dg: # Fetch element element = dg["sub001"] # Fetch element data @@ -66,8 +61,8 @@ def test_store(tmp_path: Path) -> None: # Initialize the marker marker = PermEntropy(parcellation=PARCELLATION) # Create storage - # Create storage - storage_uri = os.path.join(tmp_path, "test_perm_entropy.sqlite") - storage = SQLiteFeatureStorage(uri=storage_uri) + storage = SQLiteFeatureStorage( + uri=tmp_path / "test_perm_entropy.sqlite" + ) # Compute the marker and store marker.fit_transform(input=element_data, storage=storage) diff --git a/junifer/markers/complexity/tests/test_range_entropy.py b/junifer/markers/complexity/tests/test_range_entropy.py index e6ea96c434..86c83fc5cd 100644 --- a/junifer/markers/complexity/tests/test_range_entropy.py +++ b/junifer/markers/complexity/tests/test_range_entropy.py @@ -1,18 +1,23 @@ """Provide test for range entropy.""" # Authors: Amir Omidvarnia +# Synchon Mandal # License: AGPL -import os from pathlib import Path -from nilearn.maskers import NiftiLabelsMasker +import pytest -from junifer.data import load_parcellation -from junifer.datareader import DefaultDataReader -from junifer.markers import RangeEntropy -from junifer.storage import SQLiteFeatureStorage -from junifer.testing.datagrabbers import SPMAuditoryTestingDatagrabber + +pytest.importorskip("neurokit2") + + +from junifer.datareader import DefaultDataReader # noqa: E402 +from junifer.markers.complexity import RangeEntropy # noqa: E402 +from junifer.storage import SQLiteFeatureStorage # noqa: E402 +from junifer.testing.datagrabbers import ( # noqa: E402 + SPMAuditoryTestingDataGrabber, +) # Set parcellation @@ -21,7 +26,7 @@ def test_compute() -> None: """Test RangeEntropy compute().""" - with SPMAuditoryTestingDatagrabber() as dg: + with SPMAuditoryTestingDataGrabber() as dg: # Fetch element element = dg["sub001"] # Fetch element data @@ -30,17 +35,8 @@ def test_compute() -> None: marker = RangeEntropy(parcellation=PARCELLATION) # Compute the marker feature_map = marker.fit_transform(element_data) - - # Load parcellation - test_parcellation, _, _ = load_parcellation(PARCELLATION) - # Compute the NiftiLabelsMasker - test_masker = NiftiLabelsMasker(test_parcellation) - test_ts = test_masker.fit_transform(element_data["BOLD"]["data"]) - _, n_roi = test_ts.shape - - # Assert the dimension of timeseries - _, n_roi2 = feature_map["BOLD"]["data"].shape - assert n_roi == n_roi2 + # Assert the dimension of timeseries + assert feature_map["BOLD"]["data"].ndim == 2 def test_get_output_type() -> None: @@ -58,7 +54,7 @@ def test_store(tmp_path: Path) -> None: The path to the test directory. """ - with SPMAuditoryTestingDatagrabber() as dg: + with SPMAuditoryTestingDataGrabber() as dg: # Fetch element element = dg["sub001"] # Fetch element data @@ -66,7 +62,8 @@ def test_store(tmp_path: Path) -> None: # Initialize the marker marker = RangeEntropy(parcellation=PARCELLATION) # Create storage - storage_uri = os.path.join(tmp_path, "test_range_entropy.sqlite") - storage = SQLiteFeatureStorage(uri=storage_uri) + storage = SQLiteFeatureStorage( + uri=tmp_path / "test_range_entropy.sqlite" + ) # Compute the marker and store marker.fit_transform(input=element_data, storage=storage) diff --git a/junifer/markers/complexity/tests/test_range_entropy_auc.py b/junifer/markers/complexity/tests/test_range_entropy_auc.py index 7a064ebe15..c8a98f7c63 100644 --- a/junifer/markers/complexity/tests/test_range_entropy_auc.py +++ b/junifer/markers/complexity/tests/test_range_entropy_auc.py @@ -1,18 +1,23 @@ """Provide test for the AUC of range entropy.""" # Authors: Amir Omidvarnia +# Synchon Mandal # License: AGPL -import os from pathlib import Path -from nilearn.maskers import NiftiLabelsMasker +import pytest -from junifer.data import load_parcellation -from junifer.datareader import DefaultDataReader -from junifer.markers import RangeEntropyAUC -from junifer.storage import SQLiteFeatureStorage -from junifer.testing.datagrabbers import SPMAuditoryTestingDatagrabber + +pytest.importorskip("neurokit2") + + +from junifer.datareader import DefaultDataReader # noqa: E402 +from junifer.markers.complexity import RangeEntropyAUC # noqa: E402 +from junifer.storage import SQLiteFeatureStorage # noqa: E402 +from junifer.testing.datagrabbers import ( # noqa: E402 + SPMAuditoryTestingDataGrabber, +) # Set parcellation @@ -21,7 +26,7 @@ def test_compute() -> None: """Test RangeEntropyAUC compute().""" - with SPMAuditoryTestingDatagrabber() as dg: + with SPMAuditoryTestingDataGrabber() as dg: # Fetch element element = dg["sub001"] # Fetch element data @@ -30,17 +35,8 @@ def test_compute() -> None: marker = RangeEntropyAUC(parcellation=PARCELLATION) # Compute the marker feature_map = marker.fit_transform(element_data) - - # Load parcellation - test_parcellation, _, _ = load_parcellation(PARCELLATION) - # Compute the NiftiLabelsMasker - test_masker = NiftiLabelsMasker(test_parcellation) - test_ts = test_masker.fit_transform(element_data["BOLD"]["data"]) - _, n_roi = test_ts.shape - - # Assert the dimension of timeseries - _, n_roi2 = feature_map["BOLD"]["data"].shape - assert n_roi == n_roi2 + # Assert the dimension of timeseries + assert feature_map["BOLD"]["data"].ndim == 2 def test_get_output_type() -> None: @@ -58,16 +54,16 @@ def test_store(tmp_path: Path) -> None: The path to the test directory. """ - with SPMAuditoryTestingDatagrabber() as dg: + with SPMAuditoryTestingDataGrabber() as dg: # Fetch element element = dg["sub001"] # Fetch element data element_data = DefaultDataReader().fit_transform(element) # Initialize the marker marker = RangeEntropyAUC(parcellation=PARCELLATION) - # Create storage - storage_uri = os.path.join(tmp_path, "test_range_entropy_auc.sqlite") - storage = SQLiteFeatureStorage(uri=storage_uri) + storage = SQLiteFeatureStorage( + uri=tmp_path / "test_range_entropy_auc.sqlite" + ) # Compute the marker and store marker.fit_transform(input=element_data, storage=storage) diff --git a/junifer/markers/complexity/tests/test_sample_entropy.py b/junifer/markers/complexity/tests/test_sample_entropy.py index d074475136..cdc39d386f 100644 --- a/junifer/markers/complexity/tests/test_sample_entropy.py +++ b/junifer/markers/complexity/tests/test_sample_entropy.py @@ -1,18 +1,22 @@ """Provide test for sample entropy.""" # Authors: Amir Omidvarnia +# Synchon Mandal # License: AGPL -import os from pathlib import Path -from nilearn.maskers import NiftiLabelsMasker +import pytest -from junifer.data import load_parcellation -from junifer.datareader import DefaultDataReader -from junifer.markers import SampleEntropy -from junifer.storage import SQLiteFeatureStorage -from junifer.testing.datagrabbers import SPMAuditoryTestingDatagrabber + +pytest.importorskip("neurokit2") + +from junifer.datareader import DefaultDataReader # noqa: E402 +from junifer.markers.complexity import SampleEntropy # noqa: E402 +from junifer.storage import SQLiteFeatureStorage # noqa: E402 +from junifer.testing.datagrabbers import ( # noqa: E402 + SPMAuditoryTestingDataGrabber, +) # Set parcellation @@ -21,7 +25,7 @@ def test_compute() -> None: """Test SampleEntropy compute().""" - with SPMAuditoryTestingDatagrabber() as dg: + with SPMAuditoryTestingDataGrabber() as dg: # Fetch element element = dg["sub001"] # Fetch element data @@ -30,17 +34,8 @@ def test_compute() -> None: marker = SampleEntropy(parcellation=PARCELLATION) # Compute the marker feature_map = marker.fit_transform(element_data) - - # Load parcellation - test_parcellation, _, _ = load_parcellation(PARCELLATION) - # Compute the NiftiLabelsMasker - test_masker = NiftiLabelsMasker(test_parcellation) - test_ts = test_masker.fit_transform(element_data["BOLD"]["data"]) - _, n_roi = test_ts.shape - - # Assert the dimension of timeseries - _, n_roi2 = feature_map["BOLD"]["data"].shape - assert n_roi == n_roi2 + # Assert the dimension of timeseries + assert feature_map["BOLD"]["data"].ndim == 2 def test_get_output_type() -> None: @@ -58,7 +53,7 @@ def test_store(tmp_path: Path) -> None: The path to the test directory. """ - with SPMAuditoryTestingDatagrabber() as dg: + with SPMAuditoryTestingDataGrabber() as dg: # Fetch element element = dg["sub001"] # Fetch element data @@ -66,8 +61,8 @@ def test_store(tmp_path: Path) -> None: # Initialize the marker marker = SampleEntropy(parcellation=PARCELLATION) # Create storage - storage_uri = os.path.join(tmp_path, "test_sample_entropy.sqlite") - storage_uri = tmp_path / "test_sample_entropy.sqlite" - storage = SQLiteFeatureStorage(uri=storage_uri) + storage = SQLiteFeatureStorage( + uri=tmp_path / "test_sample_entropy.sqlite" + ) # Compute the marker and store marker.fit_transform(input=element_data, storage=storage) diff --git a/junifer/markers/complexity/tests/test_weighted_perm_entropy.py b/junifer/markers/complexity/tests/test_weighted_perm_entropy.py index 46bc19ab73..188e184bc3 100644 --- a/junifer/markers/complexity/tests/test_weighted_perm_entropy.py +++ b/junifer/markers/complexity/tests/test_weighted_perm_entropy.py @@ -1,18 +1,22 @@ """Provide test for weighted permutation entropy.""" # Authors: Amir Omidvarnia +# Synchon Mandal # License: AGPL -import os from pathlib import Path -from nilearn.maskers import NiftiLabelsMasker +import pytest -from junifer.data import load_parcellation -from junifer.datareader import DefaultDataReader -from junifer.markers import WeightedPermEntropy -from junifer.storage import SQLiteFeatureStorage -from junifer.testing.datagrabbers import SPMAuditoryTestingDatagrabber + +pytest.importorskip("neurokit2") + +from junifer.datareader import DefaultDataReader # noqa: E402 +from junifer.markers.complexity import WeightedPermEntropy # noqa: E402 +from junifer.storage import SQLiteFeatureStorage # noqa: E402 +from junifer.testing.datagrabbers import ( # noqa: E402 + SPMAuditoryTestingDataGrabber, +) # Set parcellation @@ -21,7 +25,7 @@ def test_compute() -> None: """Test WeightedPermEntropy compute().""" - with SPMAuditoryTestingDatagrabber() as dg: + with SPMAuditoryTestingDataGrabber() as dg: # Fetch element element = dg["sub001"] # Fetch element data @@ -30,17 +34,8 @@ def test_compute() -> None: marker = WeightedPermEntropy(parcellation=PARCELLATION) # Compute the marker feature_map = marker.fit_transform(element_data) - - # Load parcellation - test_parcellation, _, _ = load_parcellation(PARCELLATION) - # Compute the NiftiLabelsMasker - test_masker = NiftiLabelsMasker(test_parcellation) - test_ts = test_masker.fit_transform(element_data["BOLD"]["data"]) - _, n_roi = test_ts.shape - - # Assert the dimension of timeseries - _, n_roi2 = feature_map["BOLD"]["data"].shape - assert n_roi == n_roi2 + # Assert the dimension of timeseries + assert feature_map["BOLD"]["data"].ndim == 2 def test_get_output_type() -> None: @@ -58,7 +53,7 @@ def test_store(tmp_path: Path) -> None: The path to the test directory. """ - with SPMAuditoryTestingDatagrabber() as dg: + with SPMAuditoryTestingDataGrabber() as dg: # Fetch element element = dg["sub001"] # Fetch element data @@ -66,9 +61,8 @@ def test_store(tmp_path: Path) -> None: # Initialize the marker marker = WeightedPermEntropy(parcellation=PARCELLATION) # Create storage - storage_uri = os.path.join( - tmp_path, "test_weighted_perm_entropy.sqlite" + storage = SQLiteFeatureStorage( + uri=tmp_path / "test_weighted_perm_entropy.sqlite" ) - storage = SQLiteFeatureStorage(uri=storage_uri) # Compute the marker and store marker.fit_transform(input=element_data, storage=storage)