Skip to content

Commit fb3c550

Browse files
author
Shoshana Berleant
committed
don't calculate var/stddev twice
1 parent b48395d commit fb3c550

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

nipype/algorithms/confounds.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
class ComputeDVARSInputSpec(BaseInterfaceInputSpec):
3333
in_file = File(exists=True, mandatory=True, desc='functional data, after HMC')
3434
in_mask = File(exists=True, mandatory=True, desc='a brain mask')
35-
remove_zerovariance = traits.Bool(False, usedefault=True,
35+
remove_zerovariance = traits.Bool(True, usedefault=True,
3636
desc='remove voxels with zero variance')
3737
save_std = traits.Bool(True, usedefault=True,
3838
desc='save standardized DVARS')
@@ -626,7 +626,7 @@ def compute_dvars(in_file, in_mask, remove_zerovariance=False):
626626

627627
if remove_zerovariance:
628628
# Remove zero-variance voxels across time axis
629-
mask = zero_variance(func, mask)
629+
mask = zero_remove(func_sd, mask)
630630

631631
idx = np.where(mask > 0)
632632
mfunc = func[idx[0], idx[1], idx[2], :]
@@ -650,35 +650,28 @@ def compute_dvars(in_file, in_mask, remove_zerovariance=False):
650650
# standardization
651651
dvars_stdz = dvars_nstd / diff_sd_mean
652652

653-
654-
with warnings.catch_warnings(): # catch divide by zero errors
653+
with warnings.catch_warnings(): # catch, e.g., divide by zero errors
655654
warnings.filterwarnings('error')
656655

657656
# voxelwise standardization
658-
diff_vx_stdz = func_diff / np.array([diff_sdhat] * func_diff.shape[-1]).T
657+
diff_vx_stdz = func_diff / np.array([diff_sdhat] * func_diff.shape[-1]).T
659658
dvars_vx_stdz = diff_vx_stdz.std(axis=0, ddof=1)
660659

661660
return (dvars_stdz, dvars_nstd, dvars_vx_stdz)
662661

663-
def zero_variance(func, mask):
662+
def zero_remove(data, mask):
664663
"""
665-
Mask out voxels with zero variance across t-axis
664+
Modify inputted mask to also mask out zero values
666665
667-
:param numpy.ndarray func: input fMRI dataset, after motion correction
668-
:param numpy.ndarray mask: 3D brain mask
669-
:return: the 3D mask of voxels with nonzero variance across :math:`t`.
666+
:param numpy.ndarray data: e.g. voxelwise stddev of fMRI dataset, after motion correction
667+
:param numpy.ndarray mask: brain mask (same dimensions as data)
668+
:return: the mask with any additional zero voxels removed (same dimensions as inputs)
670669
:rtype: numpy.ndarray
671670
672671
"""
673-
idx = np.where(mask > 0)
674-
func = func[idx[0], idx[1], idx[2], :]
675-
tvariance = func.var(axis=1)
676-
tv_mask = np.zeros_like(tvariance, dtype=np.uint8)
677-
tv_mask[tvariance > 0] = 1
678-
679-
newmask = np.zeros_like(mask, dtype=np.uint8)
680-
newmask[idx] = tv_mask
681-
return newmask
672+
new_mask = mask.copy()
673+
new_mask[data == 0] = 0
674+
return new_mask
682675

683676
def plot_confound(tseries, figsize, name, units=None,
684677
series_tr=None, normalize=False):

0 commit comments

Comments
 (0)