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] Handle both 3D and 4D images in the case of one contrast #115

Merged
merged 3 commits into from
Dec 10, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions fmralign/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ def _fit_masker(self, imgs):

if isinstance(imgs, Nifti1Image):
imgs = [imgs]
# If images are 3D, add a fourth dimension
for i, img in enumerate(imgs):
if len(img.shape) == 3:
imgs[i] = Nifti1Image(
np.expand_dims(img.get_fdata(), axis=-1),
img.affine,
img.header,
)
# Assert that all images have the same shape
if len(set([img.shape for img in imgs])) > 1:
raise NotImplementedError(
Expand Down
10 changes: 10 additions & 0 deletions fmralign/tests/test_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ def test_standardization():
assert np.abs(np.std(data_array) - 1.0) < 1e-5


def test_one_contrast():
"""Test that ParcellationMasker handles both 3D and\n
4D images in the case of one contrast"""
img1, _ = random_niimg((8, 7, 6))
img2, _ = random_niimg((8, 7, 6, 1))
pmasker = ParcellationMasker()
pmasker.fit([img1, img2])


def test_get_parcellation():
"""Test that ParcellationMasker returns the parcellation mask"""
n_pieces = 2
Expand All @@ -198,3 +207,4 @@ def test_get_parcellation():

assert np.allclose(data, labels)
assert len(np.unique(data)) == n_pieces

Loading