Skip to content

Commit e957e87

Browse files
committed
less np array use; unique names for dropped components
1 parent b04c9ca commit e957e87

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

nipype/algorithms/confounds.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -652,10 +652,13 @@ def _run_interface(self, runtime):
652652
metadata_file = os.path.abspath('component_metadata.tsv')
653653
else:
654654
metadata_file = save_metadata
655-
components_names = np.array(dtype='object_',
656-
object=['dropped' for i in range(len(metadata['mask']))])
657-
components_names[np.where(metadata['retained'])] = (
658-
components_header)
655+
components_names = np.empty(len(metadata['mask']),
656+
dtype='object_')
657+
retained = np.where(metadata['retained'])
658+
not_retained = np.where(np.logical_not(metadata['retained']))
659+
components_names[retained] = components_header
660+
components_names[not_retained] = ([
661+
'dropped{}'.format(i) for i in range(len(not_retained[0]))])
659662
self._results['metadata_file'] = metadata_file
660663
with open(metadata_file, 'w') as f:
661664
f.write('{}\t{}\t{}\t{}\t{}\n'.format('component',
@@ -1288,7 +1291,7 @@ def compute_noise_components(imgseries, mask_images, components_criterion=0.5,
12881291
else:
12891292
continue
12901293

1291-
variance_explained = [value ** 2 / np.sum(s ** 2) for value in s]
1294+
variance_explained = (s ** 2) / np.sum(s ** 2)
12921295
cumulative_variance_explained = np.cumsum(variance_explained)
12931296

12941297
num_components = int(components_criterion)
@@ -1299,7 +1302,8 @@ def compute_noise_components(imgseries, mask_images, components_criterion=0.5,
12991302
num_components = len(s)
13001303

13011304
num_components = int(num_components)
1302-
# check whether num_components == 0, break if so
1305+
if num_components == 0:
1306+
break
13031307
if components is None:
13041308
components = u[:, :num_components]
13051309
metadata = OrderedDict()
@@ -1308,12 +1312,10 @@ def compute_noise_components(imgseries, mask_images, components_criterion=0.5,
13081312
metadata['variance_explained'] = variance_explained
13091313
metadata['cumulative_variance_explained'] = (
13101314
cumulative_variance_explained)
1311-
metadata['retained'] = [
1312-
i < num_components for i in range(len(s))]
1315+
metadata['retained'] = [i < num_components for i in range(len(s))]
13131316
else:
13141317
components = np.hstack((components, u[:, :num_components]))
1315-
metadata['mask'] = np.hstack((metadata['mask'],
1316-
[i] * len(s)))
1318+
metadata['mask'] = metadata['mask'] + [i] * len(s)
13171319
metadata['singular_value'] = (
13181320
np.hstack((metadata['singular_value'], s)))
13191321
metadata['variance_explained'] = (
@@ -1322,15 +1324,13 @@ def compute_noise_components(imgseries, mask_images, components_criterion=0.5,
13221324
metadata['cumulative_variance_explained'] = (
13231325
np.hstack((metadata['cumulative_variance_explained'],
13241326
cumulative_variance_explained)))
1325-
metadata['retained'] = np.hstack((metadata['retained'],
1326-
[True if i < num_components
1327-
else False
1328-
for i in range(len(s))]))
1327+
metadata['retained'] = (metadata['retained']
1328+
+ [i < num_components
1329+
for i in range(len(s))])
13291330
if components is None:
13301331
if failure_mode == 'error':
13311332
raise ValueError('No components found')
1332-
components = np.ones((M.shape[0], num_components),
1333-
dtype=np.float32) * np.nan
1333+
components = np.full((M.shape[0], num_components), np.NaN)
13341334
return components, basis, metadata
13351335

13361336

0 commit comments

Comments
 (0)