<s> class param : <EOL> def __init__ ( self ) : <EOL> self . verbose = <NUM_LIT:1> <EOL> self . output_name = '<STR_LIT>' <EOL> self . smoothness = <NUM_LIT:1> <EOL> import sys <EOL> import getopt <EOL> import sct_utils as sct <EOL> from nibabel import load , Nifti1Image , save <EOL> from numpy import linspace <EOL> import os <EOL> from scipy import interpolate <EOL> def main ( ) : <EOL> fname = '<STR_LIT>' <EOL> verbose = param . verbose <EOL> output_name = param . output_name <EOL> smoothness = param . smoothness <EOL> try : <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , '<STR_LIT>' ) <EOL> except getopt . GetoptError : <EOL> usage ( ) <EOL> for opt , arg in opts : <EOL> if opt == '<STR_LIT>' : <EOL> usage ( ) <EOL> elif opt in ( "<STR_LIT>" ) : <EOL> fname = arg <EOL> elif opt in ( "<STR_LIT>" ) : <EOL> output_name = arg <EOL> elif opt in ( "<STR_LIT>" ) : <EOL> smoothness = arg <EOL> elif opt in ( '<STR_LIT>' ) : <EOL> verbose = int ( arg ) <EOL> if fname == '<STR_LIT>' : <EOL> usage ( ) <EOL> print '<STR_LIT>' <EOL> sct . check_file_exist ( fname ) <EOL> sct . check_if_rpi ( fname ) <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' + fname <EOL> print '<STR_LIT>' + str ( verbose ) <EOL> file = load ( fname ) <EOL> data = file . get_data ( ) <EOL> hdr = file . get_header ( ) <EOL> X , Y , Z = ( data > <NUM_LIT:0> ) . nonzero ( ) <EOL> Z_new = linspace ( min ( Z ) , max ( Z ) , ( max ( Z ) - min ( Z ) + <NUM_LIT:1> ) ) <EOL> X = [ X [ i ] for i in Z [ : ] . argsort ( ) ] <EOL> Y = [ Y [ i ] for i in Z [ : ] . argsort ( ) ] <EOL> Z = [ Z [ i ] for i in Z [ : ] . argsort ( ) ] <EOL> print X , Y , Z <EOL> f1 = interpolate . UnivariateSpline ( Z , X ) <EOL> f2 = interpolate . UnivariateSpline ( Z , Y ) <EOL> X_fit = f1 ( Z_new ) <EOL> Y_fit = f2 ( Z_new ) <EOL> print X_fit <EOL> print Y_fit <EOL> if verbose == <NUM_LIT:2> : <EOL> import matplotlib . pyplot as plt <EOL> plt . figure ( ) <EOL> plt . plot ( Z_new , X_fit ) <EOL> plt . plot ( Z , X , '<STR_LIT:o>' , linestyle = '<STR_LIT:None>' ) <EOL> plt . show ( ) <EOL> plt . figure ( ) <EOL> plt . plot ( Z_new , Y_fit ) <EOL> plt . plot ( Z , Y , '<STR_LIT:o>' , linestyle = '<STR_LIT:None>' ) <EOL> plt . show ( ) <EOL> data = data * <NUM_LIT:0> <EOL> for i in xrange ( len ( X_fit ) ) : <EOL> data [ X_fit [ i ] , Y_fit [ i ] , Z_new [ i ] ] = <NUM_LIT:1> <EOL> print '<STR_LIT>' <EOL> hdr . set_data_dtype ( '<STR_LIT>' ) <EOL> img = Nifti1Image ( data , None , hdr ) <EOL> file_name = output_name <EOL> save ( img , file_name ) <EOL> print '<STR_LIT>' + output_name <EOL> del data <EOL> def usage ( ) : <EOL> print """<STR_LIT:U+0020>""" + os . path . basename ( __file__ ) + """<STR_LIT>""" + os . path . basename ( __file__ ) + """<STR_LIT>""" + str ( param . output_name ) + """<STR_LIT>""" + str ( param . smoothness ) + """<STR_LIT>""" + str ( param . verbose ) + """<STR_LIT>""" + os . path . basename ( __file__ ) + """<STR_LIT>""" <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> param = param ( ) <EOL> main ( ) </s>
