Skip to content

Commit 9c50f2a

Browse files
committed
FIX: Improved mask estimation
1 parent fb54146 commit 9c50f2a

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

nipype/algorithms/confounds.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,19 +1280,21 @@ def combine_mask_files(mask_files, mask_method=None, mask_index=None):
12801280
mask = None
12811281
for filename in mask_files:
12821282
img = nb.load(filename)
1283+
img_as_mask = np.asanyarray(img.dataobj).astype("int32") > 0
12831284
if mask is None:
1284-
mask = img.get_fdata() > 0
1285-
np.logical_or(mask, img.get_fdata() > 0, mask)
1285+
mask = img_as_mask
1286+
np.logical_or(mask, img_as_mask, mask)
12861287
img = nb.Nifti1Image(mask, img.affine, header=img.header)
12871288
return [img]
12881289

12891290
if mask_method == "intersect":
12901291
mask = None
12911292
for filename in mask_files:
12921293
img = nb.load(filename)
1294+
img_as_mask = np.asanyarray(img.dataobj).astype("int32") > 0
12931295
if mask is None:
1294-
mask = img.get_fdata() > 0
1295-
np.logical_and(mask, img.get_fdata() > 0, mask)
1296+
mask = img_as_mask
1297+
np.logical_and(mask, img_as_mask, mask)
12961298
img = nb.Nifti1Image(mask, img.affine, header=img.header)
12971299
return [img]
12981300

nipype/algorithms/metrics.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ def _eucl_cog(self, nii1, nii2):
112112
from scipy.ndimage.measurements import center_of_mass, label
113113

114114
origdata1 = np.asanyarray(nii1.dataobj)
115-
origdata1 = (origdata1 != 0) & ~np.isnan(origdata1)
116-
cog_t = np.array(center_of_mass(origdata1.copy())).reshape(-1, 1)
115+
origdata1 = (np.rint(origdata1) != 0) & ~np.isnan(origdata1)
116+
cog_t = np.array(center_of_mass(origdata1)).reshape(-1, 1)
117117
cog_t = np.vstack((cog_t, np.array([1])))
118118
cog_t_coor = np.dot(nii1.affine, cog_t)[:3, :]
119119

120120
origdata2 = np.asanyarray(nii2.dataobj)
121-
origdata2 = (origdata2 != 0) & ~np.isnan(origdata2)
121+
origdata2 = (np.rint(origdata2) != 0) & ~np.isnan(origdata2)
122122
(labeled_data, n_labels) = label(origdata2)
123123

124124
cogs = np.ones((4, n_labels))
@@ -165,13 +165,13 @@ def _eucl_max(self, nii1, nii2):
165165
from scipy.spatial.distance import cdist
166166

167167
origdata1 = np.asanyarray(nii1.dataobj)
168-
origdata1 = np.logical_not(np.logical_or(origdata1 == 0, np.isnan(origdata1)))
168+
origdata1 = (np.rint(origdata1) != 0) & ~np.isnan(origdata1)
169169
origdata2 = np.asanyarray(nii2.dataobj)
170-
origdata2 = np.logical_not(np.logical_or(origdata2 == 0, np.isnan(origdata2)))
170+
origdata2 = (np.rint(origdata2) != 0) & ~np.isnan(origdata2)
171171

172172
if isdefined(self.inputs.mask_volume):
173173
maskdata = np.asanyarray(nb.load(self.inputs.mask_volume).dataobj)
174-
maskdata = np.logical_not(np.logical_or(maskdata == 0, np.isnan(maskdata)))
174+
maskdata = (np.rint(maskdata) != 0) & ~np.isnan(maskdata)
175175
origdata1 = np.logical_and(maskdata, origdata1)
176176
origdata2 = np.logical_and(maskdata, origdata2)
177177

0 commit comments

Comments
 (0)