Skip to content

Commit 850ed41

Browse files
author
Shoshana Berleant
committed
clearer 4Dification, doc edit
1 parent 9dad02e commit 850ed41

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

nipype/interfaces/nilearn.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
class SignalExtractionInputSpec(BaseInterfaceInputSpec):
2626
in_file = File(exists=True, mandatory=True, desc='4-D fMRI nii file')
2727
label_files = InputMultiPath(File(exists=True), mandatory=True,
28-
desc='a 3-D label image, with 0 denoting '
29-
'background, or a list of 3-D probability '
30-
'maps (one per label) or the equivalent 4D '
31-
'file.')
28+
desc='a 3-D label image, with 0 denoting '
29+
'background, or a list of 3-D probability '
30+
'maps (one per label) or the equivalent 4D '
31+
'file.')
3232
class_labels = traits.List(mandatory=True,
3333
desc='Human-readable labels for each segment '
3434
'in the label file, in order. The length of '
@@ -43,12 +43,12 @@ class SignalExtractionInputSpec(BaseInterfaceInputSpec):
4343
'(True), returns simple time series calculated from each '
4444
'region independently (e.g., for noise regression). If '
4545
'False, returns unique signals for each region, discarding '
46-
'shared variance (e.g., for connectivity)')
46+
'shared variance (e.g., for connectivity. Only has effect '
47+
'with 4D probability maps.')
4748
include_global = traits.Bool(False, usedefault=True, mandatory=False,
4849
desc='If True, include an extra column '
4950
'labeled "global", with values calculated from the entire brain '
50-
'(instead of just regions). Only has effect with 4D probability '
51-
'maps.')
51+
'(instead of just regions).')
5252
detrend = traits.Bool(False, usedefault=True, mandatory=False,
5353
desc='If True, perform detrending using nilearn.')
5454

@@ -105,8 +105,7 @@ def _process_inputs(self):
105105
n_labels = label_data.get_data().shape[3]
106106
if self.inputs.incl_shared_variance: # 4d labels, independent computation
107107
for img in nli.iter_img(label_data):
108-
sortof_4d_img = nb.Nifti1Image(img.get_data()[:, :, :, np.newaxis], np.eye(4))
109-
maskers.append(nl.NiftiMapsMasker(sortof_4d_img))
108+
maskers.append(nl.NiftiMapsMasker(self._4d(img.get_data(), img.affine)))
110109
else: # 4d labels, one computation fitting all
111110
maskers.append(nl.NiftiMapsMasker(label_data))
112111

@@ -115,15 +114,14 @@ def _process_inputs(self):
115114
raise ValueError('The length of class_labels {} does not '
116115
'match the number of regions {} found in '
117116
'label_files {}'.format(self.inputs.class_labels,
118-
n_labels,
119-
self.inputs.label_files))
117+
n_labels,
118+
self.inputs.label_files))
120119

121120
if self.inputs.include_global:
122-
global_label_data = label_data.get_data().sum(axis=3)
123-
global_label_data = np.rint(global_label_data).astype(int).clip(0, 1)
124-
global_label_data = global_label_data[:, :, :, np.newaxis] # add back 4th dimension
125-
global_label_data = nb.Nifti1Image(global_label_data, np.eye(4))
126-
global_masker = nl.NiftiMapsMasker(global_label_data, detrend=self.inputs.detrend)
121+
global_label_data = label_data.get_data().sum(axis=3) # sum across all regions
122+
global_label_data = np.rint(global_label_data).astype(int).clip(0, 1) # binarize
123+
global_label_data = self._4d(global_label_data, label_data.affine)
124+
global_masker = nl.NiftiLabelsMasker(global_label_data, detrend=self.inputs.detrend)
127125
maskers.insert(0, global_masker)
128126
self.inputs.class_labels.insert(0, 'global')
129127

@@ -132,6 +130,11 @@ def _process_inputs(self):
132130

133131
return maskers
134132

133+
def _4d(self, array, affine):
134+
''' takes a 3-dimensional numpy array and an affine,
135+
returns the equivalent 4th dimensional nifti file '''
136+
return nb.Nifti1Image(array[:, :, :, np.newaxis], affine)
137+
135138
def _list_outputs(self):
136139
outputs = self._outputs().get()
137140
outputs['out_file'] = self.inputs.out_file

0 commit comments

Comments
 (0)