<s> import sys <EOL> import os <EOL> import commands <EOL> import time <EOL> import numpy <EOL> import nibabel <EOL> from scipy import ndimage <EOL> import sct_utils as sct <EOL> from sct_image import get_orientation_3d <EOL> from sct_convert import convert <EOL> from msct_image import Image <EOL> from sct_image import copy_header , concat_data <EOL> from msct_parser import Parser <EOL> class Param : <EOL> def __init__ ( self ) : <EOL> self . debug = <NUM_LIT:0> <EOL> self . fname_data = '<STR_LIT>' <EOL> self . fname_out = '<STR_LIT>' <EOL> self . process_list = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . process = '<STR_LIT>' <EOL> self . shape_list = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . shape = '<STR_LIT>' <EOL> self . size = '<STR_LIT>' <EOL> self . even = <NUM_LIT:0> <EOL> self . file_prefix = '<STR_LIT>' <EOL> self . verbose = <NUM_LIT:1> <EOL> self . remove_tmp_files = <NUM_LIT:1> <EOL> self . offset = '<STR_LIT>' <EOL> def main ( ) : <EOL> if param . debug : <EOL> print '<STR_LIT>' <EOL> status , path_sct_data = commands . getstatusoutput ( '<STR_LIT>' ) <EOL> param . fname_data = path_sct_data + '<STR_LIT>' <EOL> param . process = '<STR_LIT>' + path_sct_data + '<STR_LIT>' <EOL> param . shape = '<STR_LIT>' <EOL> param . size = '<STR_LIT>' <EOL> param . remove_tmp_files = <NUM_LIT:1> <EOL> param . verbose = <NUM_LIT:1> <EOL> else : <EOL> parser = get_parser ( ) <EOL> arguments = parser . parse ( sys . argv [ <NUM_LIT:1> : ] ) <EOL> param . fname_data = arguments [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in arguments : <EOL> param . process = arguments [ '<STR_LIT>' ] <EOL> if param . process [ <NUM_LIT:0> ] not in param . process_list : <EOL> sct . printv ( parser . usage . generate ( error = '<STR_LIT>' + param . process [ <NUM_LIT:0> ] + '<STR_LIT>' ) ) <EOL> if '<STR_LIT>' in arguments : <EOL> param . size = arguments [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in arguments : <EOL> param . shape = arguments [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in arguments : <EOL> param . fname_out = arguments [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in arguments : <EOL> param . remove_tmp_files = int ( arguments [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in arguments : <EOL> param . verbose = int ( arguments [ '<STR_LIT>' ] ) <EOL> create_mask ( ) <EOL> def create_mask ( ) : <EOL> fsloutput = '<STR_LIT>' <EOL> method_type = param . process [ <NUM_LIT:0> ] <EOL> if not method_type == '<STR_LIT>' : <EOL> method_val = param . process [ <NUM_LIT:1> ] <EOL> if method_type == '<STR_LIT>' : <EOL> sct . check_file_exist ( method_val , param . verbose ) <EOL> path_data , file_data , ext_data = sct . extract_fname ( param . fname_data ) <EOL> if param . fname_out == '<STR_LIT>' : <EOL> param . fname_out = param . file_prefix + file_data + ext_data <EOL> sct . printv ( '<STR_LIT>' , param . verbose ) <EOL> path_tmp = sct . slash_at_the_end ( '<STR_LIT>' + time . strftime ( "<STR_LIT>" ) , <NUM_LIT:1> ) <EOL> sct . run ( '<STR_LIT>' + path_tmp , param . verbose ) <EOL> sct . printv ( '<STR_LIT>' , param . verbose ) <EOL> orientation_input = get_orientation_3d ( param . fname_data , filename = True ) <EOL> sct . printv ( '<STR_LIT>' + orientation_input , param . verbose ) <EOL> reorient_coordinates = False <EOL> convert ( param . fname_data , path_tmp + '<STR_LIT>' ) <EOL> if method_type == '<STR_LIT>' : <EOL> convert ( method_val , path_tmp + '<STR_LIT>' ) <EOL> if method_type == '<STR_LIT>' : <EOL> convert ( method_val , path_tmp + '<STR_LIT>' ) <EOL> os . chdir ( path_tmp ) <EOL> sct . printv ( '<STR_LIT>' , param . verbose ) <EOL> sct . run ( '<STR_LIT>' , verbose = False ) <EOL> if method_type == '<STR_LIT>' : <EOL> sct . run ( '<STR_LIT>' , verbose = False ) <EOL> if method_type == '<STR_LIT>' : <EOL> sct . run ( '<STR_LIT>' , verbose = False ) <EOL> sct . printv ( '<STR_LIT>' , param . verbose ) <EOL> nx , ny , nz , nt , px , py , pz , pt = Image ( '<STR_LIT>' ) . dim <EOL> sct . printv ( '<STR_LIT:U+0020>' + str ( nx ) + '<STR_LIT>' + str ( ny ) + '<STR_LIT>' + str ( nz ) + '<STR_LIT>' + str ( nt ) , param . verbose ) <EOL> if nt != <NUM_LIT:1> : <EOL> sct . printv ( '<STR_LIT>' + os . path . basename ( __file__ ) + '<STR_LIT>' , param . verbose , '<STR_LIT>' ) <EOL> nii = Image ( '<STR_LIT>' ) <EOL> data3d = nii . data [ : , : , : , <NUM_LIT:0> ] <EOL> nii . data = data3d <EOL> nii . save ( ) <EOL> if method_type == '<STR_LIT>' : <EOL> coord = map ( int , method_val . split ( '<STR_LIT:x>' ) ) <EOL> if method_type == '<STR_LIT>' : <EOL> fname_point = method_val <EOL> sct . printv ( '<STR_LIT>' , param . verbose ) <EOL> status , output = sct . run ( '<STR_LIT>' , param . verbose ) <EOL> coord = output [ output . find ( '<STR_LIT>' ) + <NUM_LIT:10> : - <NUM_LIT> ] . split ( '<STR_LIT:U+002C>' ) <EOL> if method_type == '<STR_LIT>' : <EOL> coord = round ( float ( nx ) / <NUM_LIT:2> ) , round ( float ( ny ) / <NUM_LIT:2> ) <EOL> if method_type == '<STR_LIT>' : <EOL> fname_centerline = '<STR_LIT>' <EOL> else : <EOL> sct . printv ( '<STR_LIT>' , param . verbose ) <EOL> fname_centerline = create_line ( '<STR_LIT>' , coord , nz ) <EOL> sct . printv ( '<STR_LIT>' , param . verbose ) <EOL> centerline = nibabel . load ( fname_centerline ) <EOL> hdr = centerline . get_header ( ) <EOL> hdr . set_data_dtype ( '<STR_LIT>' ) <EOL> spacing = hdr . structarr [ '<STR_LIT>' ] <EOL> data_centerline = centerline . get_data ( ) <EOL> z_centerline = [ iz for iz in range ( <NUM_LIT:0> , nz , <NUM_LIT:1> ) if data_centerline [ : , : , iz ] . any ( ) ] <EOL> nz = len ( z_centerline ) <EOL> cx = [ <NUM_LIT:0> ] * nz <EOL> cy = [ <NUM_LIT:0> ] * nz <EOL> for iz in range ( <NUM_LIT:0> , nz , <NUM_LIT:1> ) : <EOL> cx [ iz ] , cy [ iz ] = ndimage . measurements . center_of_mass ( numpy . array ( data_centerline [ : , : , z_centerline [ iz ] ] ) ) <EOL> file_mask = '<STR_LIT>' <EOL> for iz in range ( nz ) : <EOL> center = numpy . array ( [ cx [ iz ] , cy [ iz ] ] ) <EOL> mask2d = create_mask2d ( center , param . shape , param . size , nx , ny , even = param . even , spacing = spacing ) <EOL> img = nibabel . Nifti1Image ( mask2d , None , hdr ) <EOL> nibabel . save ( img , ( file_mask + str ( iz ) + '<STR_LIT>' ) ) <EOL> '''<STR_LIT>''' <EOL> fname_list = [ file_mask + str ( iz ) + '<STR_LIT>' for iz in range ( nz ) ] <EOL> im_out = concat_data ( fname_list , dim = <NUM_LIT:2> ) <EOL> im_out . setFileName ( '<STR_LIT>' ) <EOL> im_out . save ( ) <EOL> sct . run ( '<STR_LIT>' + orientation_input , param . verbose ) <EOL> im_dat = Image ( '<STR_LIT>' ) <EOL> im_mask = Image ( '<STR_LIT>' ) <EOL> im_mask = copy_header ( im_dat , im_mask ) <EOL> im_mask . save ( ) <EOL> os . chdir ( '<STR_LIT:..>' ) <EOL> sct . printv ( '<STR_LIT>' , param . verbose ) <EOL> sct . generate_output_file ( path_tmp + '<STR_LIT>' , param . fname_out ) <EOL> if param . remove_tmp_files == <NUM_LIT:1> : <EOL> sct . printv ( '<STR_LIT>' , param . verbose ) <EOL> sct . run ( '<STR_LIT>' + path_tmp , param . verbose , error_exit = '<STR_LIT>' ) <EOL> sct . printv ( '<STR_LIT>' , param . verbose ) <EOL> sct . printv ( '<STR_LIT>' + param . fname_data + '<STR_LIT:U+0020>' + param . fname_out + '<STR_LIT>' , param . verbose , '<STR_LIT:info>' ) <EOL> print <EOL> def create_line ( fname , coord , nz ) : <EOL> sct . run ( '<STR_LIT>' + fname + '<STR_LIT>' , param . verbose ) <EOL> sct . run ( '<STR_LIT>' , param . verbose ) <EOL> cmd = '<STR_LIT>' <EOL> for iz in range ( nz ) : <EOL> if iz == nz - <NUM_LIT:1> : <EOL> cmd += str ( int ( coord [ <NUM_LIT:0> ] ) ) + '<STR_LIT:U+002C>' + str ( int ( coord [ <NUM_LIT:1> ] ) ) + '<STR_LIT:U+002C>' + str ( iz ) + '<STR_LIT>' <EOL> else : <EOL> cmd += str ( int ( coord [ <NUM_LIT:0> ] ) ) + '<STR_LIT:U+002C>' + str ( int ( coord [ <NUM_LIT:1> ] ) ) + '<STR_LIT:U+002C>' + str ( iz ) + '<STR_LIT>' <EOL> sct . run ( cmd , param . verbose ) <EOL> return '<STR_LIT>' <EOL> def create_mask2d ( center , shape , size , nx , ny , even = <NUM_LIT:0> , spacing = None ) : <EOL> offset = param . offset . split ( '<STR_LIT:U+002C>' ) <EOL> offset [ <NUM_LIT:0> ] = int ( offset [ <NUM_LIT:0> ] ) <EOL> offset [ <NUM_LIT:1> ] = int ( offset [ <NUM_LIT:1> ] ) <EOL> xx , yy = numpy . mgrid [ : nx , : ny ] <EOL> mask2d = numpy . zeros ( ( nx , ny ) ) <EOL> xc = center [ <NUM_LIT:0> ] <EOL> yc = center [ <NUM_LIT:1> ] <EOL> if '<STR_LIT>' in size : <EOL> from numpy import ceil <EOL> size = int ( size [ : - <NUM_LIT:2> ] ) <EOL> mean_spacing_xy = ( spacing [ <NUM_LIT:1> ] + spacing [ <NUM_LIT:2> ] ) / <NUM_LIT> <EOL> length = round ( float ( size ) / mean_spacing_xy ) <EOL> radius = ceil ( ( int ( length ) - <NUM_LIT:1> ) / <NUM_LIT> ) <EOL> else : <EOL> from numpy import ceil <EOL> radius = ceil ( ( int ( size ) - <NUM_LIT:1> ) / <NUM_LIT> ) <EOL> if shape == '<STR_LIT>' : <EOL> mask2d [ xc - radius : xc + radius + <NUM_LIT:1> , yc - radius : yc + radius + <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> elif shape == '<STR_LIT>' : <EOL> mask2d = ( ( xx + offset [ <NUM_LIT:0> ] - xc ) ** <NUM_LIT:2> + ( yy + offset [ <NUM_LIT:1> ] - yc ) ** <NUM_LIT:2> <= radius ** <NUM_LIT:2> ) * <NUM_LIT:1> <EOL> elif shape == '<STR_LIT>' : <EOL> sigma = float ( radius ) <EOL> mask2d = numpy . exp ( - ( ( ( xx + offset [ <NUM_LIT:0> ] - xc ) ** <NUM_LIT:2> ) / ( <NUM_LIT:2> * ( sigma ** <NUM_LIT:2> ) ) + ( ( yy + offset [ <NUM_LIT:1> ] - yc ) ** <NUM_LIT:2> ) / ( <NUM_LIT:2> * ( sigma ** <NUM_LIT:2> ) ) ) ) <EOL> return mask2d <EOL> def get_parser ( ) : <EOL> parser = Parser ( __file__ ) <EOL> parser . usage . set_description ( '<STR_LIT>' ) <EOL> parser . add_option ( name = '<STR_LIT>' , <EOL> type_value = '<STR_LIT:file>' , <EOL> description = '<STR_LIT>' , <EOL> mandatory = True , <EOL> example = '<STR_LIT>' ) <EOL> parser . add_option ( name = '<STR_LIT>' , <EOL> type_value = [ [ '<STR_LIT:U+002C>' ] , '<STR_LIT:str>' ] , <EOL> description = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> mandatory = True , <EOL> default_value = param_default . process , <EOL> example = [ '<STR_LIT>' ] ) <EOL> parser . add_option ( name = '<STR_LIT>' , <EOL> type_value = None , <EOL> description = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> mandatory = False , <EOL> deprecated_by = '<STR_LIT>' ) <EOL> parser . add_option ( name = '<STR_LIT>' , <EOL> type_value = '<STR_LIT:str>' , <EOL> description = '<STR_LIT>' , <EOL> mandatory = False , <EOL> default_value = param_default . size , <EOL> example = [ '<STR_LIT>' ] ) <EOL> parser . add_option ( name = '<STR_LIT>' , <EOL> type_value = None , <EOL> description = '<STR_LIT>' , <EOL> mandatory = False , <EOL> deprecated_by = '<STR_LIT>' ) <EOL> parser . add_option ( name = '<STR_LIT>' , <EOL> type_value = '<STR_LIT>' , <EOL> description = '<STR_LIT>' , <EOL> mandatory = False , <EOL> default_value = param_default . shape , <EOL> example = param . shape_list ) <EOL> parser . add_option ( name = '<STR_LIT>' , <EOL> type_value = '<STR_LIT>' , <EOL> description = '<STR_LIT>' , <EOL> mandatory = False , <EOL> example = [ '<STR_LIT>' ] ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT>" , <EOL> description = '<STR_LIT>' , <EOL> mandatory = False , <EOL> default_value = '<STR_LIT:1>' , <EOL> example = [ '<STR_LIT:0>' , '<STR_LIT:1>' ] ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = '<STR_LIT>' , <EOL> description = "<STR_LIT>" , <EOL> mandatory = False , <EOL> example = [ '<STR_LIT:0>' , '<STR_LIT:1>' , '<STR_LIT:2>' ] , <EOL> default_value = '<STR_LIT:1>' ) <EOL> return parser <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> param = Param ( ) <EOL> param_default = Param ( ) <EOL> main ( ) </s>
