Skip to content

Commit 01158b5

Browse files
committed
RF: Use np.nanmean
1 parent 9b857fa commit 01158b5

File tree

1 file changed

+6
-20
lines changed

1 file changed

+6
-20
lines changed

nipype/algorithms/rapidart.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,6 @@ def _calc_norm(mc, use_differences, source, brain_pts=None):
136136
return normdata, displacement
137137

138138

139-
def _nanmean(a, axis=None):
140-
"""Return the mean excluding items that are nan
141-
142-
>>> a = [1, 2, np.nan]
143-
>>> _nanmean(a)
144-
1.5
145-
146-
"""
147-
if axis:
148-
return np.nansum(a, axis) / np.sum(1 - np.isnan(a), axis)
149-
else:
150-
return np.nansum(a) / np.sum(1 - np.isnan(a))
151-
152-
153139
class ArtifactDetectInputSpec(BaseInterfaceInputSpec):
154140
realigned_files = InputMultiPath(File(exists=True),
155141
desc="Names of realigned functional data files",
@@ -376,11 +362,11 @@ def _detect_outliers_core(self, imgfile, motionfile, runidx, cwd=None):
376362
vol = data[:, :, :, t0]
377363
# Use an SPM like approach
378364
mask_tmp = vol > \
379-
(_nanmean(vol) / self.inputs.global_threshold)
365+
(np.nanmean(vol) / self.inputs.global_threshold)
380366
mask = mask * mask_tmp
381367
for t0 in range(timepoints):
382368
vol = data[:, :, :, t0]
383-
g[t0] = _nanmean(vol[mask])
369+
g[t0] = np.nanmean(vol[mask])
384370
if len(find_indices(mask)) < (np.prod((x, y, z)) / 10):
385371
intersect_mask = False
386372
g = np.zeros((timepoints, 1))
@@ -390,7 +376,7 @@ def _detect_outliers_core(self, imgfile, motionfile, runidx, cwd=None):
390376
for t0 in range(timepoints):
391377
vol = data[:, :, :, t0]
392378
mask_tmp = vol > \
393-
(_nanmean(vol) / self.inputs.global_threshold)
379+
(np.nanmean(vol) / self.inputs.global_threshold)
394380
mask[:, :, :, t0] = mask_tmp
395381
g[t0] = np.nansum(vol * mask_tmp) / np.nansum(mask_tmp)
396382
elif masktype == 'file': # uses a mask image to determine intensity
@@ -400,15 +386,15 @@ def _detect_outliers_core(self, imgfile, motionfile, runidx, cwd=None):
400386
mask = mask > 0.5
401387
for t0 in range(timepoints):
402388
vol = data[:, :, :, t0]
403-
g[t0] = _nanmean(vol[mask])
389+
g[t0] = np.nanmean(vol[mask])
404390
elif masktype == 'thresh': # uses a fixed signal threshold
405391
for t0 in range(timepoints):
406392
vol = data[:, :, :, t0]
407393
mask = vol > self.inputs.mask_threshold
408-
g[t0] = _nanmean(vol[mask])
394+
g[t0] = np.nanmean(vol[mask])
409395
else:
410396
mask = np.ones((x, y, z))
411-
g = _nanmean(data[mask > 0, :], 1)
397+
g = np.nanmean(data[mask > 0, :], 1)
412398

413399
# compute normalized intensity values
414400
gz = signal.detrend(g, axis=0) # detrend the signal

0 commit comments

Comments
 (0)