Repository: Parietal-INRIA/fmralign

Issue:
[BUG] Inconsistence between 3D inputs and 4D templates
When dealing with one contrast, input images are usually 3D however the template is initialized as a 1 sample 4D image which raises an error when comparing the shapes during preprocessing.

Discussion:

Patch:
diff --git a/fmralign/preprocessing.py b/fmralign/preprocessing.py
index 63521e8..0cc4a0a 100644
--- a/fmralign/preprocessing.py
+++ b/fmralign/preprocessing.py
@@ -123,6 +123,14 @@ class ParcellationMasker(BaseEstimator, TransformerMixin):
 
         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(


Test patch:
diff --git a/fmralign/tests/test_preprocessing.py b/fmralign/tests/test_preprocessing.py
index 1c39e46..970c008 100644
--- a/fmralign/tests/test_preprocessing.py
+++ b/fmralign/tests/test_preprocessing.py
@@ -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
@@ -198,3 +207,4 @@ def test_get_parcellation():
 
     assert np.allclose(data, labels)
     assert len(np.unique(data)) == n_pieces
+