diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 709bb730..c98bd8bb 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -38,7 +38,7 @@ jobs: strategy: matrix: os: ['ubuntu-latest'] - python-version: ['3.8', '3.9', '3.10'] + python-version: ['3.8', '3.9', '3.10', '3.11', '3.12'] steps: - name: Checkout code uses: actions/checkout@v4 diff --git a/neuromaps/images.py b/neuromaps/images.py index e318eb06..f7a2155c 100644 --- a/neuromaps/images.py +++ b/neuromaps/images.py @@ -132,7 +132,7 @@ def load_nifti(img): try: img = nib.load(img) except (TypeError) as err: - if not ("os.PathLike" in str(err) and "not Nifti1Image" in str(err)): + if not ("os.PathLike" in str(err) and "Nifti1Image" in str(err)): raise err return img @@ -164,7 +164,7 @@ def load_gifti(img): # it's not a pre-loaded GiftiImage so error out elif (isinstance(err, TypeError) and not ( - "os.PathLike" in str(err) and "not GiftiImage" in str(err) + "os.PathLike" in str(err) and "GiftiImage" in str(err) ) ): raise err @@ -213,7 +213,7 @@ def load_data(data): if (isinstance(err, AttributeError) or ( "os.PathLike" in str(err) - and "not Nifti1Image" in str(err) + and "Nifti1Image" in str(err) ) ): out = np.stack([load_nifti(img).get_fdata() for img in data], diff --git a/neuromaps/stats.py b/neuromaps/stats.py index 33b06341..1fbb712b 100644 --- a/neuromaps/stats.py +++ b/neuromaps/stats.py @@ -271,7 +271,7 @@ def efficient_pearsonr(a, b, ddof=1, nan_policy='propagate', return_pval=True): if return_pval: # taken from scipy.stats ab = (n_obs / 2) - 1 - prob = 2 * special.btdtr(ab, ab, 0.5 * (1 - np.abs(corr))) + prob = 2 * special.betainc(ab, ab, 0.5 * (1 - np.abs(corr))) return corr, prob diff --git a/neuromaps/tests/conftest.py b/neuromaps/tests/conftest.py index 0963681f..8cee1161 100644 --- a/neuromaps/tests/conftest.py +++ b/neuromaps/tests/conftest.py @@ -6,6 +6,13 @@ import pytest +def pytest_configure(config): + """Add markers for tests.""" + config.addinivalue_line( + "markers", "workbench: mark test to run with Connectome Workbench" + ) + + def pytest_runtest_setup(item): """Skip tests that require workbench if it's not installed.""" markers = set(mark.name for mark in item.iter_markers()) diff --git a/neuromaps/tests/test_images.py b/neuromaps/tests/test_images.py index cf5579d7..43ad7baf 100644 --- a/neuromaps/tests/test_images.py +++ b/neuromaps/tests/test_images.py @@ -30,16 +30,69 @@ def test_fix_coordsys(): assert False -@pytest.mark.xfail -def test_load_nifti(): +@pytest.fixture(scope="session") +def dummy_img(request, tmp_path_factory): + """Return a valid image file.""" + file_type = request.param["file_type"] + return_type = request.param["return_type"] + + rng = np.random.default_rng() + + # create a valid image file + if file_type == "nifti": + data = rng.random((10, 10, 10)) + curr_img = nib.Nifti1Image(data, affine=np.eye(4)) + curr_path = tmp_path_factory.mktemp("nifti") \ + / "valid_nifti_file.nii.gz" + nib.save(curr_img, str(curr_path)) + elif file_type == "gifti": + data = rng.random((10, 10), dtype=np.float32) + curr_img = nib.gifti.GiftiImage() + gifti_data_array = nib.gifti.GiftiDataArray(data) + curr_img.add_gifti_data_array(gifti_data_array) + curr_path = tmp_path_factory.mktemp("gifti") \ + / "valid_gifti_file.gii" + nib.save(curr_img, str(curr_path)) + else: + raise ValueError(f"Invalid file type: {file_type}") + + # return the appropriate file type + if return_type == "str": + return str(curr_path) + elif return_type == "path": + return curr_path + elif return_type == "object": + return curr_img + else: + raise ValueError(f"Invalid return type: {return_type}") + + +@pytest.mark.parametrize( + "dummy_img", [ + pytest.param( + {"file_type": "nifti", "return_type": _}, + id=_ + ) for _ in ["str", "path", "object"] + ], indirect=True +) +def test_load_nifti(dummy_img): """Test loading a NIfTI image.""" - assert False - - -@pytest.mark.xfail -def test_load_gifti(): + res = images.load_nifti(dummy_img) + assert isinstance(res, nib.Nifti1Image) + + +@pytest.mark.parametrize( + "dummy_img", [ + pytest.param( + {"file_type": "gifti", "return_type": _}, + id=_ + ) for _ in ["str", "path", "object"] + ], indirect=True +) +def test_load_gifti(dummy_img): """Test loading a GIFTI image.""" - assert False + res = images.load_gifti(dummy_img) + assert isinstance(res, nib.gifti.GiftiImage) @pytest.mark.xfail