Skip to content

Commit 9aa2300

Browse files
author
Shoshana Berleant
committed
make percentile threshold a parameter for tCompCor
1 parent c9aa6d6 commit 9aa2300

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

nipype/algorithms/compcor.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,20 @@ def _add_extras(self, components, extra_regressors):
102102
regressors = np.genfromtxt(self.inputs.extra_regressors)
103103
return np.hstack((components, regressors))
104104

105+
class TCompCorInputSpec(CompCorInputSpec):
106+
# and all the fields in CompCorInputSpec
107+
percentile_threshold = traits.Range(low=0., high=1., value=.02,
108+
exclude_low=True, exclude_high=True,
109+
usedefault=True, desc='the percentile '
110+
'used to select highest-variance '
111+
'voxels. By default the 2% of voxels '
112+
'with the highest variance are used.')
113+
105114
class TCompCor(CompCor):
106115

116+
input_spec = TCompCorInputSpec
117+
output_spec = CompCorOutputSpec
118+
107119
def _run_interface(self, runtime):
108120
imgseries = nb.load(self.inputs.realigned_file).get_data()
109121
time_voxels = imgseries.T
@@ -119,16 +131,16 @@ def _run_interface(self, runtime):
119131
tSTD = self._compute_tSTD(time_voxels, 0)
120132
sortSTD = np.sort(tSTD, axis=None) # flattened sorted matrix
121133

122-
# "... and retained a pre-specified upper fraction of the sorted voxels
123-
# within each slice ... we chose a 2% threshold"
124-
threshold = sortSTD[int(num_voxels * .98)]
125-
mask = tSTD >= threshold
134+
# use percentile_threshold to pick voxels
135+
threshold_index = int(num_voxels * (1. - self.inputs.percentile_threshold))
136+
threshold_std = sortSTD[threshold_index]
137+
mask = tSTD >= threshold_std
126138
mask = mask.astype(int)
127139

128140
# save mask
129141
mask_file = 'mask.nii'
130142
nb.nifti1.save(nb.Nifti1Image(mask, np.eye(4)), mask_file)
131-
self.inputs.mask_file = 'mask.nii'
143+
self.inputs.mask_file = mask_file
132144

133145
super(TCompCor, self)._run_interface(runtime)
134146
return runtime

nipype/algorithms/tests/test_compcor.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ def test_tcompcor(self):
6565
['-0.6048328569'], ['0.2134704201'],
6666
['-0.0355784033']])
6767

68+
def test_tcompcor_with_percentile(self):
69+
ccinterface = TCompCor(realigned_file=self.realigned_file, percentile_threshold=0.2)
70+
ccinterface.run()
71+
72+
mask = nb.load('mask.nii').get_data()
73+
num_nonmasked_voxels = np.count_nonzero(mask)
74+
assert_equal(num_nonmasked_voxels, 2)
75+
6876
def run_cc(self, ccinterface, expected_components):
6977
# run
7078
ccresult = ccinterface.run()

0 commit comments

Comments
 (0)