<s> import os <EOL> import getopt <EOL> import commands <EOL> import sys <EOL> import matplotlib . pyplot as plt <EOL> try : <EOL> import nibabel <EOL> except ImportError : <EOL> print '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> try : <EOL> import numpy as np <EOL> except ImportError : <EOL> print '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> def main ( ) : <EOL> status , path_sct = commands . getstatusoutput ( '<STR_LIT>' ) <EOL> fname_template_vertebral_level = path_sct + '<STR_LIT>' <EOL> fname_template_vertebral_cord = path_sct + '<STR_LIT>' <EOL> vertebral_level_file = nibabel . load ( fname_template_vertebral_level ) <EOL> vertebral_cord_file = nibabel . load ( fname_template_vertebral_cord ) <EOL> vertebral_level_image = vertebral_level_file . get_data ( ) <EOL> vertebral_cord_image = vertebral_cord_file . get_data ( ) <EOL> hdr_vertebral_level = vertebral_level_file . get_header ( ) <EOL> hdr_vertebral_cord = vertebral_cord_file . get_header ( ) <EOL> z_size_vertebral_level = hdr_vertebral_level [ '<STR_LIT>' ] [ <NUM_LIT:3> ] <EOL> z_size_vertebral_cord = hdr_vertebral_cord [ '<STR_LIT>' ] [ <NUM_LIT:3> ] <EOL> vertebral_levels = find_mid_point_vertebral_level ( vertebral_level_image ) <EOL> vertebral_Mu = np . zeros ( len ( vertebral_levels ) ) <EOL> vertebral_Sigma = np . zeros ( len ( vertebral_levels ) ) <EOL> vertebral_Mu [ <NUM_LIT:0> ] , vertebral_Sigma [ <NUM_LIT:0> ] = <NUM_LIT:0> , <NUM_LIT:0> <EOL> vertebral_Mu [ <NUM_LIT:1> ] , vertebral_Sigma [ <NUM_LIT:1> ] = <NUM_LIT:0> , <NUM_LIT:0> <EOL> vertebral_Mu [ <NUM_LIT:2> ] , vertebral_Sigma [ <NUM_LIT:2> ] = <NUM_LIT> , <NUM_LIT> <EOL> vertebral_Mu [ <NUM_LIT:3> ] , vertebral_Sigma [ <NUM_LIT:3> ] = <NUM_LIT> , <NUM_LIT> <EOL> vertebral_Mu [ <NUM_LIT:4> ] , vertebral_Sigma [ <NUM_LIT:4> ] = <NUM_LIT> , <NUM_LIT> <EOL> vertebral_Mu [ <NUM_LIT:5> ] , vertebral_Sigma [ <NUM_LIT:5> ] = <NUM_LIT> , <NUM_LIT> <EOL> vertebral_Mu [ <NUM_LIT:6> ] , vertebral_Sigma [ <NUM_LIT:6> ] = <NUM_LIT> , <NUM_LIT> <EOL> vertebral_Mu [ <NUM_LIT:7> ] , vertebral_Sigma [ <NUM_LIT:7> ] = <NUM_LIT:0> , <NUM_LIT:0> <EOL> vertebral_Mu [ <NUM_LIT:8> ] , vertebral_Sigma [ <NUM_LIT:8> ] = <NUM_LIT:0> , <NUM_LIT:0> <EOL> vertebral_Mu [ <NUM_LIT:9> ] , vertebral_Sigma [ <NUM_LIT:9> ] = <NUM_LIT:0> , <NUM_LIT:0> <EOL> vertebral_Mu [ <NUM_LIT:10> ] , vertebral_Sigma [ <NUM_LIT:10> ] = <NUM_LIT:0> , <NUM_LIT:0> <EOL> vertebral_Mu [ <NUM_LIT:11> ] , vertebral_Sigma [ <NUM_LIT:11> ] = <NUM_LIT:0> , <NUM_LIT:0> <EOL> vertebral_Mu [ <NUM_LIT:12> ] , vertebral_Sigma [ <NUM_LIT:12> ] = <NUM_LIT:0> , <NUM_LIT:0> <EOL> spinal_Mu = np . zeros ( len ( vertebral_levels ) ) <EOL> spinal_Sigma = np . zeros ( len ( vertebral_levels ) ) <EOL> spinal_Mu [ <NUM_LIT:0> ] , spinal_Sigma [ <NUM_LIT:0> ] = <NUM_LIT:0> , <NUM_LIT:0> <EOL> spinal_Mu [ <NUM_LIT:1> ] , spinal_Sigma [ <NUM_LIT:1> ] = <NUM_LIT:0> , <NUM_LIT:0> <EOL> spinal_Mu [ <NUM_LIT:2> ] , spinal_Sigma [ <NUM_LIT:2> ] = <NUM_LIT> , <NUM_LIT> <EOL> spinal_Mu [ <NUM_LIT:3> ] , spinal_Sigma [ <NUM_LIT:3> ] = <NUM_LIT> , <NUM_LIT> <EOL> spinal_Mu [ <NUM_LIT:4> ] , spinal_Sigma [ <NUM_LIT:4> ] = <NUM_LIT> , <NUM_LIT> <EOL> spinal_Mu [ <NUM_LIT:5> ] , spinal_Sigma [ <NUM_LIT:5> ] = <NUM_LIT> , <NUM_LIT> <EOL> spinal_Mu [ <NUM_LIT:6> ] , spinal_Sigma [ <NUM_LIT:6> ] = <NUM_LIT> , <NUM_LIT> <EOL> spinal_Mu [ <NUM_LIT:7> ] , spinal_Sigma [ <NUM_LIT:7> ] = <NUM_LIT> , <NUM_LIT> <EOL> spinal_Mu [ <NUM_LIT:8> ] , spinal_Sigma [ <NUM_LIT:8> ] = <NUM_LIT:0> , <NUM_LIT:0> <EOL> spinal_Mu [ <NUM_LIT:9> ] , spinal_Sigma [ <NUM_LIT:9> ] = <NUM_LIT:0> , <NUM_LIT:0> <EOL> spinal_Mu [ <NUM_LIT:10> ] , spinal_Sigma [ <NUM_LIT:10> ] = <NUM_LIT:0> , <NUM_LIT:0> <EOL> spinal_Mu [ <NUM_LIT:11> ] , spinal_Sigma [ <NUM_LIT:11> ] = <NUM_LIT:0> , <NUM_LIT:0> <EOL> spinal_Mu [ <NUM_LIT:12> ] , spinal_Sigma [ <NUM_LIT:12> ] = <NUM_LIT:0> , <NUM_LIT:0> <EOL> spinal_vertebral_dist = np . zeros ( <NUM_LIT:5> ) <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> spinal_vertebral_dist [ i ] = ( vertebral_Mu [ i + <NUM_LIT:2> ] - spinal_Mu [ i + <NUM_LIT:2> ] ) <EOL> popt_spinal_vertebral = np . polyfit ( np . arange ( <NUM_LIT:3> , <NUM_LIT:8> ) , spinal_vertebral_dist , <NUM_LIT:1> ) <EOL> P_fit_spinal_vertebral_dist = np . poly1d ( popt_spinal_vertebral ) <EOL> plt . plot ( np . arange ( <NUM_LIT:0> , <NUM_LIT:15> ) , P_fit_spinal_vertebral_dist ( np . arange ( <NUM_LIT:0> , <NUM_LIT:15> ) ) , marker = '<STR_LIT:+>' ) <EOL> plt . plot ( np . arange ( <NUM_LIT:3> , <NUM_LIT:8> ) , spinal_vertebral_dist , marker = '<STR_LIT:o>' , linestyle = '<STR_LIT:None>' ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> plt . show ( ) <EOL> conversion_factor = np . zeros ( <NUM_LIT:4> ) <EOL> for i in range ( <NUM_LIT:2> , <NUM_LIT:6> , <NUM_LIT:1> ) : <EOL> conversion_factor [ i - <NUM_LIT:2> ] = ( vertebral_levels [ i ] - vertebral_levels [ i + <NUM_LIT:1> ] ) / ( vertebral_Mu [ i + <NUM_LIT:1> ] - vertebral_Mu [ i ] ) <EOL> average_conversion_factor = np . mean ( conversion_factor ) <EOL> delta_vertebral_levels = np . zeros ( len ( vertebral_levels ) - <NUM_LIT:1> ) <EOL> for i in range ( len ( vertebral_levels ) - <NUM_LIT:1> ) : <EOL> delta_vertebral_levels [ i ] = ( vertebral_levels [ i ] - vertebral_levels [ i + <NUM_LIT:1> ] ) / average_conversion_factor <EOL> vertebral_Mu [ <NUM_LIT:0> ] = vertebral_Mu [ <NUM_LIT:1> ] - delta_vertebral_levels [ <NUM_LIT:0> ] <EOL> vertebral_Mu [ <NUM_LIT:1> ] = vertebral_Mu [ <NUM_LIT:2> ] - delta_vertebral_levels [ <NUM_LIT:1> ] <EOL> vertebral_Mu [ <NUM_LIT:7> ] = vertebral_Mu [ <NUM_LIT:6> ] + delta_vertebral_levels [ <NUM_LIT:6> ] <EOL> vertebral_Mu [ <NUM_LIT:8> ] = vertebral_Mu [ <NUM_LIT:7> ] + delta_vertebral_levels [ <NUM_LIT:7> ] <EOL> vertebral_Mu [ <NUM_LIT:9> ] = vertebral_Mu [ <NUM_LIT:8> ] + delta_vertebral_levels [ <NUM_LIT:8> ] <EOL> vertebral_Mu [ <NUM_LIT:10> ] = vertebral_Mu [ <NUM_LIT:9> ] + delta_vertebral_levels [ <NUM_LIT:9> ] <EOL> vertebral_Mu [ <NUM_LIT:11> ] = vertebral_Mu [ <NUM_LIT:10> ] + delta_vertebral_levels [ <NUM_LIT:10> ] <EOL> vertebral_Mu [ <NUM_LIT:12> ] = vertebral_Mu [ <NUM_LIT:11> ] + delta_vertebral_levels [ <NUM_LIT:11> ] <EOL> spinal_Mu [ <NUM_LIT:0> ] = vertebral_Mu [ <NUM_LIT:0> ] - P_fit_spinal_vertebral_dist ( <NUM_LIT:1> ) <EOL> spinal_Mu [ <NUM_LIT:1> ] = vertebral_Mu [ <NUM_LIT:1> ] - P_fit_spinal_vertebral_dist ( <NUM_LIT:2> ) <EOL> spinal_Mu [ <NUM_LIT:8> ] = vertebral_Mu [ <NUM_LIT:8> ] - P_fit_spinal_vertebral_dist ( <NUM_LIT:9> ) <EOL> spinal_Mu [ <NUM_LIT:9> ] = vertebral_Mu [ <NUM_LIT:9> ] - P_fit_spinal_vertebral_dist ( <NUM_LIT:10> ) <EOL> spinal_Mu [ <NUM_LIT:10> ] = vertebral_Mu [ <NUM_LIT:10> ] - P_fit_spinal_vertebral_dist ( <NUM_LIT:11> ) <EOL> spinal_Mu [ <NUM_LIT:11> ] = vertebral_Mu [ <NUM_LIT:11> ] - P_fit_spinal_vertebral_dist ( <NUM_LIT:12> ) <EOL> spinal_Mu [ <NUM_LIT:12> ] = vertebral_Mu [ <NUM_LIT:12> ] - P_fit_spinal_vertebral_dist ( <NUM_LIT> ) <EOL> popt_vertebral_sigma = np . polyfit ( np . arange ( <NUM_LIT:3> , <NUM_LIT:8> ) , vertebral_Sigma [ <NUM_LIT:2> : <NUM_LIT:7> ] , <NUM_LIT:1> ) <EOL> P_fit_vertebral_sigma = np . poly1d ( popt_vertebral_sigma ) <EOL> plt . plot ( np . arange ( <NUM_LIT:0> , <NUM_LIT> ) , P_fit_vertebral_sigma ( np . arange ( <NUM_LIT:0> , <NUM_LIT> ) ) , marker = '<STR_LIT:+>' ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> plt . plot ( np . arange ( <NUM_LIT:3> , <NUM_LIT:8> ) , vertebral_Sigma [ <NUM_LIT:2> : <NUM_LIT:7> ] , marker = '<STR_LIT:o>' , linestyle = '<STR_LIT:None>' ) <EOL> plt . show ( ) <EOL> popt_spinal_sigma = np . polyfit ( np . arange ( <NUM_LIT:3> , <NUM_LIT:9> ) , spinal_Sigma [ <NUM_LIT:2> : <NUM_LIT:8> ] , <NUM_LIT:1> ) <EOL> P_fit_spinal_sigma = np . poly1d ( popt_spinal_sigma ) <EOL> plt . plot ( np . arange ( <NUM_LIT:0> , <NUM_LIT> ) , P_fit_spinal_sigma ( np . arange ( <NUM_LIT:0> , <NUM_LIT> ) ) , marker = '<STR_LIT:+>' ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> plt . plot ( np . arange ( <NUM_LIT:3> , <NUM_LIT:9> ) , spinal_Sigma [ <NUM_LIT:2> : <NUM_LIT:8> ] , marker = '<STR_LIT:o>' , linestyle = '<STR_LIT:None>' ) <EOL> plt . show ( ) <EOL> vertebral_Sigma [ <NUM_LIT:1> ] = P_fit_vertebral_sigma ( <NUM_LIT:0> ) <EOL> vertebral_Sigma [ <NUM_LIT:0> ] = P_fit_vertebral_sigma ( <NUM_LIT:1> ) <EOL> vertebral_Sigma [ <NUM_LIT:7> ] = P_fit_vertebral_sigma ( <NUM_LIT:8> ) <EOL> vertebral_Sigma [ <NUM_LIT:8> ] = P_fit_vertebral_sigma ( <NUM_LIT:9> ) <EOL> vertebral_Sigma [ <NUM_LIT:9> ] = P_fit_vertebral_sigma ( <NUM_LIT:10> ) <EOL> vertebral_Sigma [ <NUM_LIT:10> ] = P_fit_vertebral_sigma ( <NUM_LIT:11> ) <EOL> vertebral_Sigma [ <NUM_LIT:11> ] = P_fit_vertebral_sigma ( <NUM_LIT:12> ) <EOL> vertebral_Sigma [ <NUM_LIT:12> ] = P_fit_vertebral_sigma ( <NUM_LIT> ) <EOL> spinal_Sigma [ <NUM_LIT:1> ] = P_fit_spinal_sigma ( <NUM_LIT:2> ) <EOL> spinal_Sigma [ <NUM_LIT:0> ] = P_fit_spinal_sigma ( <NUM_LIT:1> ) <EOL> spinal_Sigma [ <NUM_LIT:8> ] = P_fit_spinal_sigma ( <NUM_LIT:9> ) <EOL> spinal_Sigma [ <NUM_LIT:9> ] = P_fit_spinal_sigma ( <NUM_LIT:10> ) <EOL> spinal_Sigma [ <NUM_LIT:10> ] = P_fit_spinal_sigma ( <NUM_LIT:11> ) <EOL> spinal_Sigma [ <NUM_LIT:11> ] = P_fit_spinal_sigma ( <NUM_LIT:12> ) <EOL> spinal_Sigma [ <NUM_LIT:12> ] = P_fit_spinal_sigma ( <NUM_LIT> ) <EOL> for i in range ( len ( vertebral_levels ) ) : <EOL> y = gaussian ( np . arange ( <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:0.1> ) , spinal_Mu [ i ] , spinal_Sigma [ i ] ) <EOL> plt . plot ( np . arange ( <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:0.1> ) , y ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> plt . show ( ) <EOL> spinal_vertebral_distances = np . zeros ( len ( vertebral_levels ) ) <EOL> for i in range ( len ( vertebral_levels ) ) : <EOL> spinal_vertebral_distances [ i ] = round ( ( vertebral_Mu [ i ] - spinal_Mu [ i ] ) * average_conversion_factor ) <EOL> spinal_levels = np . zeros ( len ( vertebral_levels ) ) <EOL> for i in range ( len ( vertebral_levels ) ) : <EOL> spinal_levels [ i ] = int ( vertebral_levels [ i ] + spinal_vertebral_distances [ i ] ) <EOL> k = <NUM_LIT:0> <EOL> X , Y , Z = np . where ( vertebral_cord_image > <NUM_LIT:0> ) <EOL> for i in spinal_levels [ : ] : <EOL> template_spinal_image = vertebral_cord_image . copy ( ) <EOL> count = <NUM_LIT:0> <EOL> for j in range ( np . amax ( Z ) , np . amin ( Z ) - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> x , y = np . where ( vertebral_cord_image [ : , : , j ] > <NUM_LIT:0> ) <EOL> diff = ( i - j ) / average_conversion_factor <EOL> value = gaussian ( ( spinal_Mu [ k ] + diff ) , spinal_Mu [ k ] , spinal_Sigma [ k ] ) <EOL> template_spinal_image [ x , y , j ] = value <EOL> k = k + <NUM_LIT:1> <EOL> hdr_vertebral_cord . set_data_dtype ( '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> img = nibabel . Nifti1Image ( template_spinal_image , None , hdr_vertebral_cord ) <EOL> if k <= <NUM_LIT:8> : <EOL> file_name = '<STR_LIT>' + str ( k ) + '<STR_LIT>' <EOL> else : <EOL> file_name = '<STR_LIT>' + str ( k - <NUM_LIT:8> ) + '<STR_LIT>' <EOL> nibabel . save ( img , file_name ) <EOL> print '<STR_LIT>' + file_name <EOL> def find_mid_point_vertebral_level ( data ) : <EOL> vertebral_levels = np . zeros ( int ( np . amax ( data ) ) ) <EOL> for i in range ( ( int ( np . amin ( data ) ) + <NUM_LIT:1> ) , ( int ( np . amax ( data ) ) + <NUM_LIT:1> ) ) : <EOL> x , y , z = np . where ( data == i ) <EOL> z = np . sort ( z ) <EOL> vertebral_levels [ i - <NUM_LIT:1> ] = np . amin ( z ) + round ( ( np . amax ( z ) - np . amin ( z ) ) / <NUM_LIT:2> ) <EOL> return vertebral_levels <EOL> def gaussian ( x , mu , sig ) : <EOL> return ( np . exp ( - ( ( x - mu ) * ( x - mu ) ) / ( <NUM_LIT:2> * sig * sig ) ) ) / ( np . sqrt ( <NUM_LIT:2> * ( np . pi ) * sig * sig ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
