@@ -55,6 +55,7 @@ class PickAtlasOutputSpec(TraitedSpec):
55
55
56
56
57
57
class PickAtlas (BaseInterface ):
58
+
58
59
'''
59
60
Returns ROI masks given an atlas and a list of labels. Supports dilation
60
61
and left right masking (assuming the atlas is properly aligned).
@@ -88,9 +89,9 @@ def _get_brodmann_area(self):
88
89
for lab in labels :
89
90
newdata [origdata == lab ] = 1
90
91
if self .inputs .hemi == 'right' :
91
- newdata [floor (float (origdata .shape [0 ]) / 2 ):, :, :] = 0
92
+ newdata [floor (float (origdata .shape [0 ]) / 2 ):, :,:] = 0
92
93
elif self .inputs .hemi == 'left' :
93
- newdata [:ceil (float (origdata .shape [0 ]) / 2 ), :, :] = 0
94
+ newdata [:ceil (float (origdata .shape [0 ]) / 2 ), :,:] = 0
94
95
95
96
if self .inputs .dilation_size != 0 :
96
97
newdata = grey_dilation (
@@ -162,6 +163,7 @@ class ModifyAffineOutputSpec(TraitedSpec):
162
163
163
164
164
165
class ModifyAffine (BaseInterface ):
166
+
165
167
'''
166
168
Left multiplies the affine matrix with a specified values. Saves the volume as a nifti file.
167
169
'''
@@ -216,6 +218,7 @@ class DistanceOutputSpec(TraitedSpec):
216
218
217
219
218
220
class Distance (BaseInterface ):
221
+
219
222
'''
220
223
Calculates distance between two volumes.
221
224
'''
@@ -231,7 +234,7 @@ def _find_border(self, data):
231
234
232
235
def _get_coordinates (self , data , affine ):
233
236
if len (data .shape ) == 4 :
234
- data = data [:, :, :, 0 ]
237
+ data = data [:, :,:, 0 ]
235
238
indices = np .vstack (np .nonzero (data ))
236
239
indices = np .vstack ((indices , np .ones (indices .shape [1 ])))
237
240
coordinates = np .dot (affine , indices )
@@ -251,7 +254,7 @@ def _eucl_min(self, nii1, nii2):
251
254
dist_matrix = cdist (set1_coordinates .T , set2_coordinates .T )
252
255
(point1 , point2 ) = np .unravel_index (
253
256
np .argmin (dist_matrix ), dist_matrix .shape )
254
- return (euclidean (set1_coordinates .T [point1 , :], set2_coordinates .T [point2 , :]), set1_coordinates .T [point1 , :], set2_coordinates .T [point2 , :])
257
+ return (euclidean (set1_coordinates .T [point1 , :], set2_coordinates .T [point2 ,:]), set1_coordinates .T [point1 ,:], set2_coordinates .T [point2 ,:])
255
258
256
259
def _eucl_cog (self , nii1 , nii2 ):
257
260
origdata1 = nii1 .get_data ().astype (np .bool )
@@ -376,6 +379,7 @@ class OverlapOutputSpec(TraitedSpec):
376
379
377
380
378
381
class Overlap (BaseInterface ):
382
+
379
383
"""
380
384
Calculates various overlap measures between two maps.
381
385
@@ -438,25 +442,30 @@ def _list_outputs(self):
438
442
439
443
440
444
class FuzzyOverlapInputSpec (BaseInterfaceInputSpec ):
441
- in_ref = InputMultiPath ( File (exists = True ), mandatory = True ,
445
+ in_ref = InputMultiPath (File (exists = True ), mandatory = True ,
442
446
desc = "Reference image. Requires the same dimensions as in_tst." )
443
- in_tst = InputMultiPath ( File (exists = True ), mandatory = True ,
447
+ in_tst = InputMultiPath (File (exists = True ), mandatory = True ,
444
448
desc = "Test image. Requires the same dimensions as in_ref." )
445
449
weighting = traits .Enum ("none" , "volume" , "squared_vol" , desc = '""none": no class-overlap weighting is performed\
446
450
"volume": computed class-overlaps are weighted by class volume\
447
- "squared_vol": computed class-overlaps are weighted by the squared volume of the class' ,usedefault = True )
448
- out_file = File ("diff.nii" , desc = "alternative name for resulting difference-map" , usedefault = True )
451
+ "squared_vol": computed class-overlaps are weighted by the squared volume of the class' , usedefault = True )
452
+ out_file = File (
453
+ "diff.nii" , desc = "alternative name for resulting difference-map" , usedefault = True )
449
454
450
455
451
456
class FuzzyOverlapOutputSpec (TraitedSpec ):
452
- jaccard = traits .Float ( desc = "Fuzzy Jaccard Index (fJI), all the classes" )
453
- dice = traits .Float ( desc = "Fuzzy Dice Index (fDI), all the classes" )
454
- diff_file = File (exists = True , desc = "resulting difference-map of all classes, using the chosen weighting" )
455
- class_fji = traits .List ( traits .Float (), desc = "Array containing the fJIs of each computed class" )
456
- class_fdi = traits .List ( traits .Float (), desc = "Array containing the fDIs of each computed class" )
457
+ jaccard = traits .Float (desc = "Fuzzy Jaccard Index (fJI), all the classes" )
458
+ dice = traits .Float (desc = "Fuzzy Dice Index (fDI), all the classes" )
459
+ diff_file = File (
460
+ exists = True , desc = "resulting difference-map of all classes, using the chosen weighting" )
461
+ class_fji = traits .List (
462
+ traits .Float (), desc = "Array containing the fJIs of each computed class" )
463
+ class_fdi = traits .List (
464
+ traits .Float (), desc = "Array containing the fDIs of each computed class" )
457
465
458
466
459
467
class FuzzyOverlap (BaseInterface ):
468
+
460
469
"""
461
470
Calculates various overlap measures between two maps, using the fuzzy definition
462
471
proposed in: Crum et al., Generalized Overlap Measures for Evaluation and Validation
@@ -475,76 +484,72 @@ class FuzzyOverlap(BaseInterface):
475
484
>>> res = overlap.run() # doctest: +SKIP
476
485
"""
477
486
478
- input_spec = FuzzyOverlapInputSpec
487
+ input_spec = FuzzyOverlapInputSpec
479
488
output_spec = FuzzyOverlapOutputSpec
480
489
481
490
def _run_interface (self , runtime ):
482
491
ncomp = len (self .inputs .in_ref )
483
- assert ( ncomp == len (self .inputs .in_tst ) )
484
- weights = np .ones ( shape = ncomp )
485
-
486
- img_ref = np .array ( [ nb .load ( fname ).get_data () for fname in self .inputs .in_ref ] )
487
- img_tst = np .array ( [ nb .load ( fname ).get_data () for fname in self .inputs .in_tst ] )
492
+ assert (ncomp == len (self .inputs .in_tst ))
493
+ weights = np .ones (shape = ncomp )
488
494
495
+ img_ref = np .array ([nb .load (fname ).get_data ()
496
+ for fname in self .inputs .in_ref ])
497
+ img_tst = np .array ([nb .load (fname ).get_data ()
498
+ for fname in self .inputs .in_tst ])
489
499
490
500
msk = np .sum (img_ref , axis = 0 )
491
- msk [msk > 0 ] = 1.0
501
+ msk [msk > 0 ] = 1.0
492
502
tst_msk = np .sum (img_tst , axis = 0 )
493
- tst_msk [tst_msk > 0 ] = 1.0
494
-
495
- #check that volumes are normalized
496
- #img_ref[:][msk>0] = img_ref[:][msk>0] / (np.sum( img_ref, axis=0 ))[msk>0]
497
- #img_tst[tst_msk>0] = img_tst[tst_msk>0] / np.sum( img_tst, axis=0 )[tst_msk>0]
503
+ tst_msk [tst_msk > 0 ] = 1.0
498
504
499
505
self ._jaccards = []
500
506
volumes = []
501
507
502
- diff_im = np .zeros ( img_ref .shape )
508
+ diff_im = np .zeros (img_ref .shape )
503
509
504
- for ref_comp , tst_comp , diff_comp in zip ( img_ref , img_tst , diff_im ):
505
- num = np .minimum ( ref_comp , tst_comp )
506
- ddr = np .maximum ( ref_comp , tst_comp )
507
- diff_comp [ddr > 0 ] += 1.0 - (num [ddr > 0 ] / ddr [ddr > 0 ])
508
- self ._jaccards .append ( np .sum ( num ) / np .sum ( ddr ) )
509
- volumes .append ( np .sum ( ref_comp ) )
510
+ for ref_comp , tst_comp , diff_comp in zip (img_ref , img_tst , diff_im ):
511
+ num = np .minimum (ref_comp , tst_comp )
512
+ ddr = np .maximum (ref_comp , tst_comp )
513
+ diff_comp [ddr > 0 ] += 1.0 - (num [ddr > 0 ] / ddr [ddr > 0 ])
514
+ self ._jaccards .append (np .sum (num ) / np .sum (ddr ) )
515
+ volumes .append (np .sum (ref_comp ) )
510
516
511
- self ._dices = 2.0 * np .array (self ._jaccards ) / (np .array (self ._jaccards ) + 1.0 )
517
+ self ._dices = 2.0 * \
518
+ np .array (self ._jaccards ) / (np .array (self ._jaccards ) + 1.0 )
512
519
513
520
if self .inputs .weighting != "none" :
514
521
weights = 1.0 / np .array (volumes )
515
522
if self .inputs .weighting == "squared_vol" :
516
- weights = weights ** 2
523
+ weights = weights ** 2
517
524
518
- weights = weights / np .sum ( weights )
525
+ weights = weights / np .sum (weights )
519
526
520
- setattr ( self , '_jaccard' , np .sum ( weights * self ._jaccards ) )
521
- setattr ( self , '_dice' , np .sum ( weights * self ._dices ) )
527
+ setattr (self , '_jaccard' , np .sum (weights * self ._jaccards ) )
528
+ setattr (self , '_dice' , np .sum (weights * self ._dices ) )
522
529
530
+ diff = np .zeros (diff_im [0 ].shape )
523
531
524
- diff = np .zeros ( diff_im [0 ].shape )
532
+ for w , ch in zip (weights , diff_im ):
533
+ ch [msk == 0 ] = 0
534
+ diff += w * ch
525
535
526
- for w ,ch in zip (weights ,diff_im ):
527
- ch [msk == 0 ] = 0
528
- diff += w * ch
529
-
530
- nb .save (nb .Nifti1Image (diff , nb .load ( self .inputs .in_ref [0 ]).get_affine (),
531
- nb .load ( self .inputs .in_ref [0 ]).get_header ()), self .inputs .out_file )
536
+ nb .save (
537
+ nb .Nifti1Image (diff , nb .load (self .inputs .in_ref [0 ]).get_affine (),
538
+ nb .load (self .inputs .in_ref [0 ]).get_header ()), self .inputs .out_file )
532
539
533
-
534
540
return runtime
535
541
536
542
def _list_outputs (self ):
537
543
outputs = self ._outputs ().get ()
538
544
for method in ("dice" , "jaccard" ):
539
545
outputs [method ] = getattr (self , '_' + method )
540
- #outputs['volume_difference'] = self._volume
541
546
outputs ['diff_file' ] = os .path .abspath (self .inputs .out_file )
542
- outputs ['class_fji' ] = np .array (self ._jaccards ).astype (float ).tolist ();
543
- outputs ['class_fdi' ]= self ._dices .astype (float ).tolist ();
547
+ outputs ['class_fji' ] = np .array (
548
+ self ._jaccards ).astype (float ).tolist ();
549
+ outputs ['class_fdi' ] = self ._dices .astype (float ).tolist ();
544
550
return outputs
545
551
546
552
547
-
548
553
class CreateNiftiInputSpec (BaseInterfaceInputSpec ):
549
554
data_file = File (exists = True , mandatory = True , desc = "ANALYZE img file" )
550
555
header_file = File (
@@ -599,6 +604,7 @@ class TSNROutputSpec(TraitedSpec):
599
604
600
605
601
606
class TSNR (BaseInterface ):
607
+
602
608
"""Computes the time-course SNR for a time series
603
609
604
610
Typically you want to run this on a realigned time-series.
@@ -641,7 +647,7 @@ def _run_interface(self, runtime):
641
647
betas = np .dot (np .linalg .pinv (X ), np .rollaxis (data , 3 , 2 ))
642
648
datahat = np .rollaxis (np .dot (X [:, 1 :],
643
649
np .rollaxis (
644
- betas [1 :, :, :, :], 0 , 3 )),
650
+ betas [1 :, :,:, :], 0 , 3 )),
645
651
0 , 4 )
646
652
data = data - datahat
647
653
img = nb .Nifti1Image (data , img .get_affine (), header )
@@ -676,6 +682,7 @@ class GunzipOutputSpec(TraitedSpec):
676
682
677
683
678
684
class Gunzip (BaseInterface ):
685
+
679
686
"""
680
687
681
688
"""
@@ -717,7 +724,7 @@ def matlab2csv(in_array, name, reshape):
717
724
if reshape == True :
718
725
if len (np .shape (output_array )) > 1 :
719
726
output_array = np .reshape (output_array , (
720
- np .shape (output_array )[0 ]* np .shape (output_array )[1 ], 1 ))
727
+ np .shape (output_array )[0 ] * np .shape (output_array )[1 ], 1 ))
721
728
iflogger .info (np .shape (output_array ))
722
729
output_name = op .abspath (name + '.csv' )
723
730
np .savetxt (output_name , output_array , delimiter = ',' )
@@ -736,6 +743,7 @@ class Matlab2CSVOutputSpec(TraitedSpec):
736
743
737
744
738
745
class Matlab2CSV (BaseInterface ):
746
+
739
747
"""
740
748
Simple interface to save the components of a MATLAB .mat file as a text file with comma-separated values (CSVs).
741
749
@@ -766,7 +774,8 @@ def _run_interface(self, runtime):
766
774
if isinstance (in_dict [key ][0 ], np .ndarray ):
767
775
saved_variables .append (key )
768
776
else :
769
- iflogger .info ('One of the keys in the input file, {k}, is not a Numpy array' .format (k = key ))
777
+ iflogger .info (
778
+ 'One of the keys in the input file, {k}, is not a Numpy array' .format (k = key ))
770
779
771
780
if len (saved_variables ) > 1 :
772
781
iflogger .info (
@@ -828,7 +837,7 @@ def merge_csvs(in_list):
828
837
in_file , delimiter = ',' , skiprows = 1 , usecols = range (1 , n_cols ))
829
838
except ValueError , ex :
830
839
in_array = np .loadtxt (
831
- in_file , delimiter = ',' , skiprows = 1 , usecols = range (1 , n_cols - 1 ))
840
+ in_file , delimiter = ',' , skiprows = 1 , usecols = range (1 , n_cols - 1 ))
832
841
if idx == 0 :
833
842
out_array = in_array
834
843
else :
@@ -846,7 +855,7 @@ def remove_identical_paths(in_files):
846
855
out_names = list ()
847
856
commonprefix = op .commonprefix (in_files )
848
857
lastslash = commonprefix .rfind ('/' )
849
- commonpath = commonprefix [0 :(lastslash + 1 )]
858
+ commonpath = commonprefix [0 :(lastslash + 1 )]
850
859
for fileidx , in_file in enumerate (in_files ):
851
860
path , name , ext = split_filename (in_file )
852
861
in_file = op .join (path , name )
@@ -864,10 +873,10 @@ def maketypelist(rowheadings, shape, extraheadingBool, extraheading):
864
873
if rowheadings :
865
874
typelist .append (('heading' , 'a40' ))
866
875
if len (shape ) > 1 :
867
- for idx in range (1 , (min (shape )+ 1 )):
876
+ for idx in range (1 , (min (shape ) + 1 )):
868
877
typelist .append ((str (idx ), float ))
869
878
else :
870
- for idx in range (1 , (shape [0 ]+ 1 )):
879
+ for idx in range (1 , (shape [0 ] + 1 )):
871
880
typelist .append ((str (idx ), float ))
872
881
if extraheadingBool :
873
882
typelist .append ((extraheading , 'a40' ))
@@ -881,13 +890,13 @@ def makefmtlist(output_array, typelist, rowheadingsBool, shape, extraheadingBool
881
890
fmtlist .append ('%s' )
882
891
if len (shape ) > 1 :
883
892
output = np .zeros (max (shape ), typelist )
884
- for idx in range (1 , min (shape )+ 1 ):
885
- output [str (idx )] = output_array [:, idx - 1 ]
893
+ for idx in range (1 , min (shape ) + 1 ):
894
+ output [str (idx )] = output_array [:, idx - 1 ]
886
895
fmtlist .append ('%f' )
887
896
else :
888
897
output = np .zeros (1 , typelist )
889
- for idx in range (1 , len (output_array )+ 1 ):
890
- output [str (idx )] = output_array [idx - 1 ]
898
+ for idx in range (1 , len (output_array ) + 1 ):
899
+ output [str (idx )] = output_array [idx - 1 ]
891
900
fmtlist .append ('%f' )
892
901
if extraheadingBool :
893
902
fmtlist .append ('%s' )
@@ -917,6 +926,7 @@ class MergeCSVFilesOutputSpec(TraitedSpec):
917
926
918
927
919
928
class MergeCSVFiles (BaseInterface ):
929
+
920
930
"""
921
931
This interface is designed to facilitate data loading in the R environment.
922
932
It takes input CSV files and merges them into a single CSV file.
@@ -1051,6 +1061,7 @@ class AddCSVColumnOutputSpec(TraitedSpec):
1051
1061
1052
1062
1053
1063
class AddCSVColumn (BaseInterface ):
1064
+
1054
1065
"""
1055
1066
Short interface to add an extra column and field to a text file
1056
1067
@@ -1108,6 +1119,7 @@ class CalculateNormalizedMomentsOutputSpec(TraitedSpec):
1108
1119
1109
1120
1110
1121
class CalculateNormalizedMoments (BaseInterface ):
1122
+
1111
1123
"""
1112
1124
Calculates moments of timeseries.
1113
1125
@@ -1147,4 +1159,4 @@ def calc_moments(timeseries_file, moment):
1147
1159
m2 = stats .moment (timeseries , 2 , axis = 0 )
1148
1160
m3 = stats .moment (timeseries , moment , axis = 0 )
1149
1161
zero = (m2 == 0 )
1150
- return np .where (zero , 0 , m3 / m2 ** (moment / 2.0 ))
1162
+ return np .where (zero , 0 , m3 / m2 ** (moment / 2.0 ))
0 commit comments