22import os
33from glob import glob
44from ...external .due import BibTeX
5+ from ...utils .imagemanip import copy_header as _copy_header
56from ...utils .filemanip import split_filename , copyfile , which , fname_presuffix
67from ..base import TraitedSpec , File , traits , InputMultiPath , OutputMultiPath , isdefined
78from .base import ANTSCommand , ANTSCommandInputSpec
@@ -235,7 +236,6 @@ def _gen_filename(self, name):
235236 _ , name , ext = split_filename (self .inputs .intensity_images [0 ])
236237 output = name + "_labeled" + ext
237238 return output
238- return None
239239
240240 def _list_outputs (self ):
241241 outputs = self ._outputs ().get ()
@@ -538,24 +538,15 @@ def _list_outputs(self):
538538
539539 # Fix headers
540540 if self .inputs .copy_header :
541- self ._copy_header (outputs ["output_image" ])
541+ _copy_header (self .inputs .input_image , outputs ["output_image" ],
542+ keep_dtype = False )
542543
543544 if self ._out_bias_file :
544545 outputs ["bias_image" ] = os .path .abspath (self ._out_bias_file )
545546 if self .inputs .copy_header :
546- self . _copy_header (outputs ["bias_image" ])
547+ _copy_header (self . inputs . input_image , outputs ["bias_image" ])
547548 return outputs
548549
549- def _copy_header (self , fname ):
550- """Copy header from input image to an output image."""
551- import nibabel as nb
552-
553- in_img = nb .load (self .inputs .input_image )
554- out_img = nb .load (fname , mmap = False )
555- new_img = out_img .__class__ (out_img .get_fdata (), in_img .affine , in_img .header )
556- new_img .set_data_dtype (out_img .get_data_dtype ())
557- new_img .to_filename (fname )
558-
559550
560551class CorticalThicknessInputSpec (ANTSCommandInputSpec ):
561552 dimension = traits .Enum (
@@ -1500,55 +1491,44 @@ def _format_arg(self, opt, spec, val):
15001491 self .inputs .exclusion_image [ii ],
15011492 )
15021493 )
1503- retval = " " .join (retval )
1504- elif opt == "patch_radius" :
1505- retval = "-p {0}" .format (self ._format_xarray (val ))
1506- elif opt == "search_radius" :
1507- retval = "-s {0}" .format (self ._format_xarray (val ))
1508- elif opt == "out_label_fusion" :
1509- if isdefined (self .inputs .out_intensity_fusion_name_format ):
1510- if isdefined (self .inputs .out_label_post_prob_name_format ):
1511- if isdefined (self .inputs .out_atlas_voting_weight_name_format ):
1512- retval = "-o [{0}, {1}, {2}, {3}]" .format (
1513- self .inputs .out_label_fusion ,
1514- self .inputs .out_intensity_fusion_name_format ,
1515- self .inputs .out_label_post_prob_name_format ,
1516- self .inputs .out_atlas_voting_weight_name_format ,
1517- )
1518- else :
1519- retval = "-o [{0}, {1}, {2}]" .format (
1520- self .inputs .out_label_fusion ,
1521- self .inputs .out_intensity_fusion_name_format ,
1522- self .inputs .out_label_post_prob_name_format ,
1523- )
1494+ return " " .join (retval )
1495+ if opt == "patch_radius" :
1496+ return "-p {0}" .format (self ._format_xarray (val ))
1497+ if opt == "search_radius" :
1498+ return "-s {0}" .format (self ._format_xarray (val ))
1499+ if opt == "out_label_fusion" :
1500+ args = [self .inputs .out_label_fusion ]
1501+ for option in (
1502+ self .inputs .out_intensity_fusion_name_format ,
1503+ self .inputs .out_label_post_prob_name_format ,
1504+ self .inputs .out_atlas_voting_weight_name_format
1505+ ):
1506+ if isdefined (option ):
1507+ args .append (option )
15241508 else :
1525- retval = "-o [{0}, {1}]" .format (
1526- self .inputs .out_label_fusion ,
1527- self .inputs .out_intensity_fusion_name_format ,
1528- )
1529- else :
1530- retval = "-o {0}" .format (self .inputs .out_label_fusion )
1531- elif opt == "out_intensity_fusion_name_format" :
1532- retval = ""
1509+ break
1510+ if len (args ) == 1 :
1511+ return " " .join (("-o" , args [0 ]))
1512+ return "-o [{}]" .format (", " .join (args ))
1513+ if opt == "out_intensity_fusion_name_format" :
15331514 if not isdefined (self .inputs .out_label_fusion ):
1534- retval = "-o {0}" .format (self .inputs .out_intensity_fusion_name_format )
1535- elif opt == "atlas_image" :
1536- atlas_image_cmd = " " .join (
1515+ return "-o {0}" .format (self .inputs .out_intensity_fusion_name_format )
1516+ return ""
1517+ if opt == "atlas_image" :
1518+ return " " .join (
15371519 [
15381520 "-g [{0}]" .format (", " .join ("'%s'" % fn for fn in ai ))
15391521 for ai in self .inputs .atlas_image
15401522 ]
15411523 )
1542- retval = atlas_image_cmd
1543- elif opt == "target_image" :
1544- target_image_cmd = " " .join (
1524+ if opt == "target_image" :
1525+ return " " .join (
15451526 [
15461527 "-t [{0}]" .format (", " .join ("'%s'" % fn for fn in ai ))
15471528 for ai in self .inputs .target_image
15481529 ]
15491530 )
1550- retval = target_image_cmd
1551- elif opt == "atlas_segmentation_image" :
1531+ if opt == "atlas_segmentation_image" :
15521532 if len (val ) != len (self .inputs .atlas_image ):
15531533 raise ValueError (
15541534 "Number of specified segmentations should be identical to the number "
@@ -1557,14 +1537,10 @@ def _format_arg(self, opt, spec, val):
15571537 )
15581538 )
15591539
1560- atlas_segmentation_image_cmd = " " .join (
1540+ return " " .join (
15611541 ["-l {0}" .format (fn ) for fn in self .inputs .atlas_segmentation_image ]
15621542 )
1563- retval = atlas_segmentation_image_cmd
1564- else :
1565-
1566- return super (AntsJointFusion , self )._format_arg (opt , spec , val )
1567- return retval
1543+ return super (AntsJointFusion , self )._format_arg (opt , spec , val )
15681544
15691545 def _list_outputs (self ):
15701546 outputs = self ._outputs ().get ()
@@ -1805,8 +1781,6 @@ def _gen_filename(self, name):
18051781 output = name + "_warped_white_matter" + ext
18061782 return output
18071783
1808- return None
1809-
18101784 def _format_arg (self , opt , spec , val ):
18111785 if opt == "segmentation_image" :
18121786 newval = "[{0},{1},{2}]" .format (
0 commit comments