Skip to content

Commit b04c9ca

Browse files
committed
@oesteban: limit np array use, clean up conditionals, remove invalid obj
1 parent addb0e9 commit b04c9ca

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

nipype/algorithms/confounds.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ class CompCorInputSpec(BaseInterfaceInputSpec):
400400
'components_file.txt',
401401
usedefault=True,
402402
desc='Filename to store physiological components')
403-
num_components = traits.Either('all', traits.Int,
403+
num_components = traits.Either('all', traits.Range(low=1),
404404
xor=['variance_threshold'],
405405
desc='Number of components to return from the decomposition. If '
406406
'`num_components` is `all`, then all components will be '
@@ -1280,34 +1280,36 @@ def compute_noise_components(imgseries, mask_images, components_criterion=0.5,
12801280
try:
12811281
u, s, _ = fallback_svd(M, full_matrices=False)
12821282
except np.linalg.LinAlgError:
1283-
if self.inputs.failure_mode == 'error':
1283+
if failure_mode == 'error':
12841284
raise
12851285
if components_criterion >= 1:
12861286
u = np.empty((M.shape[0], components_criterion),
12871287
dtype=np.float32) * np.nan
12881288
else:
12891289
continue
12901290

1291-
variance_explained = np.array([value**2/np.sum(s**2) for value in s])
1291+
variance_explained = [value ** 2 / np.sum(s ** 2) for value in s]
12921292
cumulative_variance_explained = np.cumsum(variance_explained)
1293+
1294+
num_components = int(components_criterion)
12931295
if 0 < components_criterion < 1:
12941296
num_components = np.searchsorted(cumulative_variance_explained,
12951297
components_criterion) + 1
12961298
elif components_criterion == -1:
12971299
num_components = len(s)
1298-
else:
1299-
num_components = int(components_criterion)
1300+
1301+
num_components = int(num_components)
1302+
# check whether num_components == 0, break if so
13001303
if components is None:
13011304
components = u[:, :num_components]
13021305
metadata = OrderedDict()
1303-
metadata['mask'] = np.array([i] * len(s))
1306+
metadata['mask'] = [i] * len(s)
13041307
metadata['singular_value'] = s
13051308
metadata['variance_explained'] = variance_explained
13061309
metadata['cumulative_variance_explained'] = (
13071310
cumulative_variance_explained)
1308-
metadata['retained'] = np.array(
1309-
[True if i < num_components
1310-
else False for i in range(len(s))], dtype='bool')
1311+
metadata['retained'] = [
1312+
i < num_components for i in range(len(s))]
13111313
else:
13121314
components = np.hstack((components, u[:, :num_components]))
13131315
metadata['mask'] = np.hstack((metadata['mask'],
@@ -1324,8 +1326,8 @@ def compute_noise_components(imgseries, mask_images, components_criterion=0.5,
13241326
[True if i < num_components
13251327
else False
13261328
for i in range(len(s))]))
1327-
if components is None and num_components != 0:
1328-
if self.inputs.failure_mode == 'error':
1329+
if components is None:
1330+
if failure_mode == 'error':
13291331
raise ValueError('No components found')
13301332
components = np.ones((M.shape[0], num_components),
13311333
dtype=np.float32) * np.nan

0 commit comments

Comments
 (0)