Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] get_clusters_table: ensure input is 3D and accept filenames #2701

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions nilearn/reporting/_get_clusters_table.py
Expand Up @@ -14,6 +14,7 @@

from nilearn.image import get_data
from nilearn.image.resampling import coord_transform
from nilearn._utils import check_niimg_3d

tsalo marked this conversation as resolved.
Show resolved Hide resolved

def _local_max(data, affine, min_distance):
Expand Down Expand Up @@ -159,6 +160,8 @@ def get_clusters_table(stat_img, stat_threshold, cluster_threshold=None,
"""
cols = ['Cluster ID', 'X', 'Y', 'Z', 'Peak Stat', 'Cluster Size (mm3)']

# check that stat_img is niimg-like object and 3D
stat_img = check_niimg_3d(stat_img)
# If cluster threshold is used, there is chance that stat_map will be
# modified, therefore copy is needed
if cluster_threshold is None:
Expand Down
14 changes: 13 additions & 1 deletion nilearn/reporting/tests/test_reporting.py
Expand Up @@ -69,7 +69,7 @@ def test_local_max():
assert np.all(np.isnan(vals))


def test_get_clusters_table():
def test_get_clusters_table(tmp_path):
shape = (9, 10, 11)
data = np.zeros(shape)
data[2:4, 5:7, 6:8] = 5.
Expand All @@ -87,6 +87,18 @@ def test_get_clusters_table():
cluster_table = get_clusters_table(stat_img, 4, 9)
assert len(cluster_table) == 0

# test with filename
fname = str(tmp_path / "stat_img.nii.gz")
stat_img.to_filename(fname)
cluster_table = get_clusters_table(fname, 4, 0)
assert len(cluster_table) == 1

# test with extra dimension
data_extra_dim = data[..., np.newaxis]
stat_img_extra_dim = nib.Nifti1Image(data_extra_dim, np.eye(4))
cluster_table = get_clusters_table(stat_img_extra_dim, 4, 0)
assert len(cluster_table) == 1


def test_get_clusters_table_not_modifying_stat_image():
shape = (9, 10, 11)
Expand Down