@@ -61,7 +61,6 @@ class PickAtlasOutputSpec(TraitedSpec):
61
61
62
62
63
63
class PickAtlas (BaseInterface ):
64
-
65
64
'''
66
65
Returns ROI masks given an atlas and a list of labels. Supports dilation
67
66
and left right masking (assuming the atlas is properly aligned).
@@ -180,7 +179,6 @@ class ModifyAffineOutputSpec(TraitedSpec):
180
179
181
180
182
181
class ModifyAffine (BaseInterface ):
183
-
184
182
'''
185
183
Left multiplies the affine matrix with a specified values. Saves the volume
186
184
as a nifti file.
@@ -245,7 +243,6 @@ class DistanceOutputSpec(TraitedSpec):
245
243
246
244
247
245
class Distance (BaseInterface ):
248
-
249
246
'''
250
247
Calculates distance between two volumes.
251
248
'''
@@ -281,13 +278,7 @@ def _eucl_min(self, nii1, nii2):
281
278
dist_matrix = cdist (set1_coordinates .T , set2_coordinates .T )
282
279
(point1 , point2 ) = np .unravel_index (
283
280
np .argmin (dist_matrix ), dist_matrix .shape )
284
- return (
285
- euclidean (
286
- set1_coordinates .T [point1 , :],
287
- set2_coordinates .T [point2 , :]
288
- ),
289
- set1_coordinates .T [point1 , :], set2_coordinates .T [point2 , :]
290
- )
281
+ return (euclidean (set1_coordinates .T [point1 , :], set2_coordinates .T [point2 , :]), set1_coordinates .T [point1 , :], set2_coordinates .T [point2 , :])
291
282
292
283
def _eucl_cog (self , nii1 , nii2 ):
293
284
origdata1 = nii1 .get_data ().astype (np .bool )
@@ -415,7 +406,6 @@ class OverlapOutputSpec(TraitedSpec):
415
406
416
407
417
408
class Overlap (BaseInterface ):
418
-
419
409
"""
420
410
Calculates various overlap measures between two maps.
421
411
@@ -478,47 +468,25 @@ def _list_outputs(self):
478
468
479
469
480
470
class FuzzyOverlapInputSpec (BaseInterfaceInputSpec ):
481
- in_ref = InputMultiPath (
482
- File (exists = True ), mandatory = True ,
483
- desc = "Reference image. Requires the same dimensions as in_tst."
484
- )
485
- in_tst = InputMultiPath (
486
- File (exists = True ), mandatory = True ,
487
- desc = "Test image. Requires the same dimensions as in_ref."
488
- )
489
- weighting = traits .Enum (
490
- "none" , "volume" , "squared_vol" ,
491
- desc = '""none": no class-overlap weighting is performed\
492
- "volume": computed class-overlaps are weighted by class volume\
493
- "squared_vol": computed class-overlaps are weighted by the squared\
494
- volume of the class' , usedefault = True
495
- )
496
- out_file = File (
497
- "diff.nii" ,
498
- desc = "alternative name for resulting difference-map" ,
499
- usedefault = True
500
- )
471
+ in_ref = InputMultiPath ( File (exists = True ), mandatory = True ,
472
+ desc = "Reference image. Requires the same dimensions as in_tst." )
473
+ in_tst = InputMultiPath ( File (exists = True ), mandatory = True ,
474
+ desc = "Test image. Requires the same dimensions as in_ref." )
475
+ weighting = traits .Enum ("none" , "volume" , "squared_vol" , desc = '""none": no class-overlap weighting is performed\
476
+ "volume": computed class-overlaps are weighted by class volume\
477
+ "squared_vol": computed class-overlaps are weighted by the squared volume of the class' ,usedefault = True )
478
+ out_file = File ("diff.nii" , desc = "alternative name for resulting difference-map" , usedefault = True )
501
479
502
480
503
481
class FuzzyOverlapOutputSpec (TraitedSpec ):
504
- jaccard = traits .Float (desc = "Fuzzy Jaccard Index (fJI), all the classes" )
505
- dice = traits .Float (desc = "Fuzzy Dice Index (fDI), all the classes" )
506
- diff_file = File (
507
- exists = True , desc = "resulting difference-map of all classes,\
508
- using the chosen weighting"
509
- )
510
- class_fji = traits .List (
511
- traits .Float (),
512
- desc = "Array containing the fJIs of each computed class"
513
- )
514
- class_fdi = traits .List (
515
- traits .Float (),
516
- desc = "Array containing the fDIs of each computed class"
517
- )
482
+ jaccard = traits .Float ( desc = "Fuzzy Jaccard Index (fJI), all the classes" )
483
+ dice = traits .Float ( desc = "Fuzzy Dice Index (fDI), all the classes" )
484
+ diff_file = File (exists = True , desc = "resulting difference-map of all classes, using the chosen weighting" )
485
+ class_fji = traits .List ( traits .Float (), desc = "Array containing the fJIs of each computed class" )
486
+ class_fdi = traits .List ( traits .Float (), desc = "Array containing the fDIs of each computed class" )
518
487
519
488
520
489
class FuzzyOverlap (BaseInterface ):
521
-
522
490
"""
523
491
Calculates various overlap measures between two maps, using the fuzzy
524
492
definition proposed in: Crum et al., Generalized Overlap Measures for
@@ -539,77 +507,76 @@ class FuzzyOverlap(BaseInterface):
539
507
>>> res = overlap.run() # doctest: +SKIP
540
508
"""
541
509
542
- input_spec = FuzzyOverlapInputSpec
510
+ input_spec = FuzzyOverlapInputSpec
543
511
output_spec = FuzzyOverlapOutputSpec
544
512
545
513
def _run_interface (self , runtime ):
546
514
ncomp = len (self .inputs .in_ref )
547
- assert (ncomp == len (self .inputs .in_tst ))
548
- weights = np .ones (shape = ncomp )
515
+ assert ( ncomp == len (self .inputs .in_tst ) )
516
+ weights = np .ones ( shape = ncomp )
517
+
518
+ img_ref = np .array ( [ nb .load ( fname ).get_data () for fname in self .inputs .in_ref ] )
519
+ img_tst = np .array ( [ nb .load ( fname ).get_data () for fname in self .inputs .in_tst ] )
549
520
550
- img_ref = np .array ([nb .load (fname ).get_data ()
551
- for fname in self .inputs .in_ref ])
552
- img_tst = np .array ([nb .load (fname ).get_data ()
553
- for fname in self .inputs .in_tst ])
554
521
555
522
msk = np .sum (img_ref , axis = 0 )
556
- msk [msk > 0 ] = 1.0
523
+ msk [msk > 0 ] = 1.0
557
524
tst_msk = np .sum (img_tst , axis = 0 )
558
- tst_msk [tst_msk > 0 ] = 1.0
525
+ tst_msk [tst_msk > 0 ] = 1.0
526
+
527
+ #check that volumes are normalized
528
+ #img_ref[:][msk>0] = img_ref[:][msk>0] / (np.sum( img_ref, axis=0 ))[msk>0]
529
+ #img_tst[tst_msk>0] = img_tst[tst_msk>0] / np.sum( img_tst, axis=0 )[tst_msk>0]
559
530
560
531
self ._jaccards = []
561
532
volumes = []
562
533
563
- diff_im = np .zeros (img_ref .shape )
534
+ diff_im = np .zeros ( img_ref .shape )
564
535
565
- for ref_comp , tst_comp , diff_comp in zip (img_ref , img_tst , diff_im ):
566
- num = np .minimum (ref_comp , tst_comp )
567
- ddr = np .maximum (ref_comp , tst_comp )
568
- diff_comp [ddr > 0 ] += 1.0 - (num [ddr > 0 ] / ddr [ddr > 0 ])
569
- self ._jaccards .append (np .sum (num ) / np .sum (ddr ) )
570
- volumes .append (np .sum (ref_comp ) )
536
+ for ref_comp , tst_comp , diff_comp in zip ( img_ref , img_tst , diff_im ):
537
+ num = np .minimum ( ref_comp , tst_comp )
538
+ ddr = np .maximum ( ref_comp , tst_comp )
539
+ diff_comp [ddr > 0 ] += 1.0 - (num [ddr > 0 ] / ddr [ddr > 0 ])
540
+ self ._jaccards .append ( np .sum ( num ) / np .sum ( ddr ) )
541
+ volumes .append ( np .sum ( ref_comp ) )
571
542
572
- self ._dices = 2.0 * \
573
- np .array (self ._jaccards ) / (np .array (self ._jaccards ) + 1.0 )
543
+ self ._dices = 2.0 * np .array (self ._jaccards ) / (np .array (self ._jaccards ) + 1.0 )
574
544
575
545
if self .inputs .weighting != "none" :
576
546
weights = 1.0 / np .array (volumes )
577
547
if self .inputs .weighting == "squared_vol" :
578
- weights = weights ** 2
548
+ weights = weights ** 2
579
549
580
- weights = weights / np .sum (weights )
550
+ weights = weights / np .sum ( weights )
581
551
582
- setattr (self , '_jaccard' , np .sum (weights * self ._jaccards ) )
583
- setattr (self , '_dice' , np .sum (weights * self ._dices ) )
552
+ setattr ( self , '_jaccard' , np .sum ( weights * self ._jaccards ) )
553
+ setattr ( self , '_dice' , np .sum ( weights * self ._dices ) )
584
554
585
- diff = np .zeros (diff_im [0 ].shape )
586
555
587
- for w , ch in zip (weights , diff_im ):
588
- ch [msk == 0 ] = 0
589
- diff += w * ch
556
+ diff = np .zeros ( diff_im [0 ].shape )
590
557
591
- nb .save (
592
- nb .Nifti1Image (
593
- diff ,
594
- nb .load (self .inputs .in_ref [0 ]).get_affine (),
595
- nb .load (self .inputs .in_ref [0 ]).get_header ()
596
- ),
597
- self .inputs .out_file
598
- )
558
+ for w ,ch in zip (weights ,diff_im ):
559
+ ch [msk == 0 ] = 0
560
+ diff += w * ch
561
+
562
+ nb .save (nb .Nifti1Image (diff , nb .load ( self .inputs .in_ref [0 ]).get_affine (),
563
+ nb .load ( self .inputs .in_ref [0 ]).get_header ()), self .inputs .out_file )
599
564
565
+
600
566
return runtime
601
567
602
568
def _list_outputs (self ):
603
569
outputs = self ._outputs ().get ()
604
570
for method in ("dice" , "jaccard" ):
605
571
outputs [method ] = getattr (self , '_' + method )
572
+ #outputs['volume_difference'] = self._volume
606
573
outputs ['diff_file' ] = os .path .abspath (self .inputs .out_file )
607
- outputs ['class_fji' ] = np .array (
608
- self ._jaccards ).astype (float ).tolist ()
609
- outputs ['class_fdi' ] = self ._dices .astype (float ).tolist ()
574
+ outputs ['class_fji' ] = np .array (self ._jaccards ).astype (float ).tolist ();
575
+ outputs ['class_fdi' ]= self ._dices .astype (float ).tolist ();
610
576
return outputs
611
577
612
578
579
+
613
580
class CreateNiftiInputSpec (BaseInterfaceInputSpec ):
614
581
data_file = File (exists = True , mandatory = True , desc = "ANALYZE img file" )
615
582
header_file = File (
@@ -664,7 +631,6 @@ class TSNROutputSpec(TraitedSpec):
664
631
665
632
666
633
class TSNR (BaseInterface ):
667
-
668
634
"""Computes the time-course SNR for a time series
669
635
670
636
Typically you want to run this on a realigned time-series.
@@ -742,7 +708,6 @@ class GunzipOutputSpec(TraitedSpec):
742
708
743
709
744
710
class Gunzip (BaseInterface ):
745
-
746
711
"""
747
712
748
713
"""
@@ -784,7 +749,7 @@ def matlab2csv(in_array, name, reshape):
784
749
if reshape :
785
750
if len (np .shape (output_array )) > 1 :
786
751
output_array = np .reshape (output_array , (
787
- np .shape (output_array )[0 ] * np .shape (output_array )[1 ], 1 ))
752
+ np .shape (output_array )[0 ]* np .shape (output_array )[1 ], 1 ))
788
753
iflogger .info (np .shape (output_array ))
789
754
output_name = op .abspath (name + '.csv' )
790
755
np .savetxt (output_name , output_array , delimiter = ',' )
@@ -808,7 +773,6 @@ class Matlab2CSVOutputSpec(TraitedSpec):
808
773
809
774
810
775
class Matlab2CSV (BaseInterface ):
811
-
812
776
"""
813
777
Simple interface to save the components of a MATLAB .mat file as a text
814
778
file with comma-separated values (CSVs).
@@ -841,10 +805,7 @@ def _run_interface(self, runtime):
841
805
if isinstance (in_dict [key ][0 ], np .ndarray ):
842
806
saved_variables .append (key )
843
807
else :
844
- iflogger .info (
845
- 'One of the keys in the input file, {k},\
846
- is not a Numpy array' .format (k = key )
847
- )
808
+ iflogger .info ('One of the keys in the input file, {k}, is not a Numpy array' .format (k = key ))
848
809
849
810
if len (saved_variables ) > 1 :
850
811
iflogger .info (
@@ -910,9 +871,7 @@ def merge_csvs(in_list):
910
871
)
911
872
except ValueError , ex :
912
873
in_array = np .loadtxt (
913
- in_file , delimiter = ',' , skiprows = 1 ,
914
- usecols = range (1 , n_cols - 1 )
915
- )
874
+ in_file , delimiter = ',' , skiprows = 1 , usecols = range (1 , n_cols - 1 ))
916
875
if idx == 0 :
917
876
out_array = in_array
918
877
else :
@@ -930,7 +889,7 @@ def remove_identical_paths(in_files):
930
889
out_names = list ()
931
890
commonprefix = op .commonprefix (in_files )
932
891
lastslash = commonprefix .rfind ('/' )
933
- commonpath = commonprefix [0 :(lastslash + 1 )]
892
+ commonpath = commonprefix [0 :(lastslash + 1 )]
934
893
for fileidx , in_file in enumerate (in_files ):
935
894
path , name , ext = split_filename (in_file )
936
895
in_file = op .join (path , name )
@@ -948,10 +907,10 @@ def maketypelist(rowheadings, shape, extraheadingBool, extraheading):
948
907
if rowheadings :
949
908
typelist .append (('heading' , 'a40' ))
950
909
if len (shape ) > 1 :
951
- for idx in range (1 , (min (shape ) + 1 )):
910
+ for idx in range (1 , (min (shape )+ 1 )):
952
911
typelist .append ((str (idx ), float ))
953
912
else :
954
- for idx in range (1 , (shape [0 ] + 1 )):
913
+ for idx in range (1 , (shape [0 ]+ 1 )):
955
914
typelist .append ((str (idx ), float ))
956
915
if extraheadingBool :
957
916
typelist .append ((extraheading , 'a40' ))
@@ -966,13 +925,13 @@ def makefmtlist(output_array, typelist, rowheadingsBool,
966
925
fmtlist .append ('%s' )
967
926
if len (shape ) > 1 :
968
927
output = np .zeros (max (shape ), typelist )
969
- for idx in range (1 , min (shape ) + 1 ):
970
- output [str (idx )] = output_array [:, idx - 1 ]
928
+ for idx in range (1 , min (shape )+ 1 ):
929
+ output [str (idx )] = output_array [:, idx - 1 ]
971
930
fmtlist .append ('%f' )
972
931
else :
973
932
output = np .zeros (1 , typelist )
974
- for idx in range (1 , len (output_array ) + 1 ):
975
- output [str (idx )] = output_array [idx - 1 ]
933
+ for idx in range (1 , len (output_array )+ 1 ):
934
+ output [str (idx )] = output_array [idx - 1 ]
976
935
fmtlist .append ('%f' )
977
936
if extraheadingBool :
978
937
fmtlist .append ('%s' )
@@ -1007,7 +966,6 @@ class MergeCSVFilesOutputSpec(TraitedSpec):
1007
966
1008
967
1009
968
class MergeCSVFiles (BaseInterface ):
1010
-
1011
969
"""
1012
970
This interface is designed to facilitate data loading in the R environment.
1013
971
It takes input CSV files and merges them into a single CSV file.
@@ -1144,7 +1102,6 @@ class AddCSVColumnOutputSpec(TraitedSpec):
1144
1102
1145
1103
1146
1104
class AddCSVColumn (BaseInterface ):
1147
-
1148
1105
"""
1149
1106
Short interface to add an extra column and field to a text file
1150
1107
@@ -1206,7 +1163,6 @@ class CalculateNormalizedMomentsOutputSpec(TraitedSpec):
1206
1163
1207
1164
1208
1165
class CalculateNormalizedMoments (BaseInterface ):
1209
-
1210
1166
"""
1211
1167
Calculates moments of timeseries.
1212
1168
@@ -1248,4 +1204,4 @@ def calc_moments(timeseries_file, moment):
1248
1204
m2 = stats .moment (timeseries , 2 , axis = 0 )
1249
1205
m3 = stats .moment (timeseries , moment , axis = 0 )
1250
1206
zero = (m2 == 0 )
1251
- return np .where (zero , 0 , m3 / m2 ** (moment / 2.0 ))
1207
+ return np .where (zero , 0 , m3 / m2 ** (moment / 2.0 ))
0 commit comments