8
8
absolute_import )
9
9
from builtins import range
10
10
11
- # Py2 compat: http://python-future.org/compatible_idioms.html#collections-counter-and-ordereddict
12
- from future import standard_library
13
- standard_library .install_aliases ()
14
- from collections import OrderedDict
15
-
16
11
import os
17
12
import os .path as op
13
+ from collections import OrderedDict
18
14
19
15
import nibabel as nb
20
16
import numpy as np
@@ -615,52 +611,48 @@ def _run_interface(self, runtime):
615
611
616
612
save_pre_filter = self .inputs .save_pre_filter
617
613
if save_pre_filter :
618
- if isinstance (save_pre_filter , bool ):
614
+ self ._results ['pre_filter_file' ] = save_pre_filter
615
+ if save_pre_filter is True :
619
616
pre_filter_file = os .path .abspath ('pre_filter.tsv' )
620
- else :
621
- pre_filter_file = save_pre_filter
622
- self ._results ['pre_filter_file' ] = pre_filter_file
623
- if self .inputs .pre_filter and save_pre_filter :
624
- ftype = {
625
- 'polynomial' : 'Legendre' ,
626
- 'cosine' : 'Cosine'
627
- }[self .inputs .pre_filter ]
628
- ncols = filter_basis .shape [1 ] if filter_basis .size > 0 else 0
629
- header = ['{}{:02d}' .format (ftype , i ) for i in range (ncols )]
630
- if skip_vols :
631
- old_basis = filter_basis
632
- # nrows defined above
633
- filter_basis = np .zeros (
634
- (nrows , ncols + skip_vols ), dtype = filter_basis .dtype )
635
- if old_basis .size > 0 :
636
- filter_basis [skip_vols :, :ncols ] = old_basis
637
- filter_basis [:skip_vols , - skip_vols :] = np .eye (skip_vols )
638
- header .extend ([
639
- 'NonSteadyStateOutlier{:02d}' .format (i )
640
- for i in range (skip_vols )
641
- ])
642
- np .savetxt (
643
- pre_filter_file ,
644
- filter_basis ,
645
- fmt = b'%.10f' ,
646
- delimiter = '\t ' ,
647
- header = '\t ' .join (header ),
648
- comments = '' )
649
-
650
- save_metadata = self .inputs .save_metadata
651
- if save_metadata :
652
- if isinstance (save_metadata , bool ):
617
+ if self .inputs .pre_filter :
618
+ ftype = {
619
+ 'polynomial' : 'Legendre' ,
620
+ 'cosine' : 'Cosine'
621
+ }[self .inputs .pre_filter ]
622
+ ncols = filter_basis .shape [1 ] if filter_basis .size > 0 else 0
623
+ header = ['{}{:02d}' .format (ftype , i ) for i in range (ncols )]
624
+ if skip_vols :
625
+ old_basis = filter_basis
626
+ # nrows defined above
627
+ filter_basis = np .zeros (
628
+ (nrows , ncols + skip_vols ), dtype = filter_basis .dtype )
629
+ if old_basis .size > 0 :
630
+ filter_basis [skip_vols :, :ncols ] = old_basis
631
+ filter_basis [:skip_vols , - skip_vols :] = np .eye (skip_vols )
632
+ header .extend ([
633
+ 'NonSteadyStateOutlier{:02d}' .format (i )
634
+ for i in range (skip_vols )
635
+ ])
636
+ np .savetxt (
637
+ self ._results ['pre_filter_file' ],
638
+ filter_basis ,
639
+ fmt = b'%.10f' ,
640
+ delimiter = '\t ' ,
641
+ header = '\t ' .join (header ),
642
+ comments = '' )
643
+
644
+ metadata_file = self .inputs .save_metadata
645
+ if metadata_file :
646
+ self ._results ['metadata_file' ] = metadata_file
647
+ if metadata_file is True :
653
648
metadata_file = os .path .abspath ('component_metadata.tsv' )
654
- else :
655
- metadata_file = save_metadata
656
649
components_names = np .empty (len (metadata ['mask' ]),
657
650
dtype = 'object_' )
658
651
retained = np .where (metadata ['retained' ])
659
652
not_retained = np .where (np .logical_not (metadata ['retained' ]))
660
653
components_names [retained ] = components_header
661
654
components_names [not_retained ] = ([
662
655
'dropped{}' .format (i ) for i in range (len (not_retained [0 ]))])
663
- self ._results ['metadata_file' ] = metadata_file
664
656
with open (metadata_file , 'w' ) as f :
665
657
f .write ('{}\t {}\t {}\t {}\t {}\n ' .format ('component' ,
666
658
* list (metadata .keys ())))
@@ -1200,7 +1192,7 @@ def combine_mask_files(mask_files, mask_method=None, mask_index=None):
1200
1192
def compute_noise_components (imgseries , mask_images , components_criterion = 0.5 ,
1201
1193
filter_type = False , degree = 0 , period_cut = 128 ,
1202
1194
repetition_time = None , failure_mode = 'error' ,
1203
- mask_names = '' ):
1195
+ mask_names = None ):
1204
1196
"""Compute the noise components from the imgseries for each mask
1205
1197
1206
1198
Parameters
@@ -1245,9 +1237,8 @@ def compute_noise_components(imgseries, mask_images, components_criterion=0.5,
1245
1237
basis = np .array ([])
1246
1238
if components_criterion == 'all' :
1247
1239
components_criterion = - 1
1248
- if not mask_names :
1249
- mask_names = range (len (mask_images ))
1250
- for i , img in zip (mask_names , mask_images ):
1240
+ mask_names = mask_names or range (len (mask_images ))
1241
+ for name , img in zip (mask_names , mask_images ):
1251
1242
mask = img .get_data ().astype (np .bool ).squeeze ()
1252
1243
if imgseries .shape [:3 ] != mask .shape :
1253
1244
raise ValueError (
@@ -1267,20 +1258,20 @@ def compute_noise_components(imgseries, mask_images, components_criterion=0.5,
1267
1258
voxel_timecourses , repetition_time , period_cut )
1268
1259
elif filter_type in ('polynomial' , False ):
1269
1260
# from paper:
1270
- # "The constant and linear trends of the columns in the matrix M
1271
- # were removed [prior to ...]"
1261
+ # "The constant and linear trends of the columns in the matrix M were
1262
+ # removed [prior to ...]"
1272
1263
voxel_timecourses , basis = regress_poly (degree , voxel_timecourses )
1273
1264
1274
- # "Voxel time series from the noise ROI (either anatomical or tSTD)
1275
- # were placed in a matrix M of size Nxm, with time along the row
1276
- # dimension and voxels along the column dimension."
1265
+ # "Voxel time series from the noise ROI (either anatomical or tSTD) were
1266
+ # placed in a matrix M of size Nxm, with time along the row dimension
1267
+ # and voxels along the column dimension."
1277
1268
M = voxel_timecourses .T
1278
1269
1279
1270
# "[... were removed] prior to column-wise variance normalization."
1280
1271
M = M / _compute_tSTD (M , 1. )
1281
1272
1282
- # "The covariance matrix C = MMT was constructed and decomposed into
1283
- # its principal components using a singular value decomposition."
1273
+ # "The covariance matrix C = MMT was constructed and decomposed into its
1274
+ # principal components using a singular value decomposition."
1284
1275
try :
1285
1276
u , s , _ = fallback_svd (M , full_matrices = False )
1286
1277
except np .linalg .LinAlgError :
@@ -1308,15 +1299,15 @@ def compute_noise_components(imgseries, mask_images, components_criterion=0.5,
1308
1299
if components is None :
1309
1300
components = u [:, :num_components ]
1310
1301
metadata = OrderedDict ()
1311
- metadata ['mask' ] = [i ] * len (s )
1302
+ metadata ['mask' ] = [name ] * len (s )
1312
1303
metadata ['singular_value' ] = s
1313
1304
metadata ['variance_explained' ] = variance_explained
1314
1305
metadata ['cumulative_variance_explained' ] = (
1315
1306
cumulative_variance_explained )
1316
1307
metadata ['retained' ] = [i < num_components for i in range (len (s ))]
1317
1308
else :
1318
1309
components = np .hstack ((components , u [:, :num_components ]))
1319
- metadata ['mask' ] = metadata ['mask' ] + [i ] * len (s )
1310
+ metadata ['mask' ] = metadata ['mask' ] + [name ] * len (s )
1320
1311
metadata ['singular_value' ] = (
1321
1312
np .hstack ((metadata ['singular_value' ], s )))
1322
1313
metadata ['variance_explained' ] = (
0 commit comments