<s> import sys , commands <EOL> import numpy as np <EOL> from time import time <EOL> import nibabel as nib <EOL> from dipy . denoise . nlmeans import nlmeans <EOL> status , path_sct = commands . getstatusoutput ( '<STR_LIT>' ) <EOL> sys . path . append ( path_sct + '<STR_LIT>' ) <EOL> from msct_parser import Parser <EOL> import sct_utils as sct <EOL> import matplotlib . pyplot as plt <EOL> class Param : <EOL> def __init__ ( self ) : <EOL> self . debug = <NUM_LIT:0> <EOL> self . verbose = <NUM_LIT:1> <EOL> self . remove_temp_files = <NUM_LIT:1> <EOL> self . parameter = "<STR_LIT>" <EOL> self . file_to_denoise = '<STR_LIT>' <EOL> self . output_file_name = '<STR_LIT>' <EOL> def main ( file_to_denoise , param , output_file_name ) : <EOL> path , file , ext = sct . extract_fname ( file_to_denoise ) <EOL> img = nib . load ( file_to_denoise ) <EOL> hdr_0 = img . get_header ( ) <EOL> data = img . get_data ( ) <EOL> aff = img . get_affine ( ) <EOL> mask = data [ : , : , : ] > noise_threshold <EOL> data = data [ : , : , : ] <EOL> if '<STR_LIT>' in arguments : <EOL> sigma = std_noise <EOL> print '<STR_LIT>' <EOL> if param . parameter == '<STR_LIT>' : <EOL> den = nlmeans ( data , sigma = sigma , mask = None , rician = True ) <EOL> else : den = nlmeans ( data , sigma = sigma , mask = None , rician = False ) <EOL> else : <EOL> sigma = np . std ( data [ ~ mask ] ) <EOL> print ( "<STR_LIT>" , data . shape ) <EOL> print '<STR_LIT>' <EOL> if param . parameter == '<STR_LIT>' : <EOL> den = nlmeans ( data , sigma = sigma , mask = mask , rician = True ) <EOL> else : den = nlmeans ( data , sigma = sigma , mask = mask , rician = False ) <EOL> t = time ( ) <EOL> print ( "<STR_LIT>" , time ( ) - t ) <EOL> print ( "<STR_LIT>" , den . shape ) <EOL> axial_middle = data . shape [ <NUM_LIT:2> ] / <NUM_LIT:2> <EOL> before = data [ : , : , axial_middle ] . T <EOL> after = den [ : , : , axial_middle ] . T <EOL> diff_3d = np . absolute ( den . astype ( '<STR_LIT>' ) - data . astype ( '<STR_LIT>' ) ) <EOL> difference = np . absolute ( after . astype ( '<STR_LIT>' ) - before . astype ( '<STR_LIT>' ) ) <EOL> if '<STR_LIT>' not in arguments : <EOL> difference [ ~ mask [ : , : , axial_middle ] . T ] = <NUM_LIT:0> <EOL> if param . verbose == <NUM_LIT:2> : <EOL> fig , ax = plt . subplots ( <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> ax [ <NUM_LIT:0> ] . imshow ( before , cmap = '<STR_LIT>' , origin = '<STR_LIT>' ) <EOL> ax [ <NUM_LIT:0> ] . set_title ( '<STR_LIT>' ) <EOL> ax [ <NUM_LIT:1> ] . imshow ( after , cmap = '<STR_LIT>' , origin = '<STR_LIT>' ) <EOL> ax [ <NUM_LIT:1> ] . set_title ( '<STR_LIT>' ) <EOL> ax [ <NUM_LIT:2> ] . imshow ( difference , cmap = '<STR_LIT>' , origin = '<STR_LIT>' ) <EOL> ax [ <NUM_LIT:2> ] . set_title ( '<STR_LIT>' ) <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> ax [ i ] . set_axis_off ( ) <EOL> plt . show ( ) <EOL> plt . savefig ( '<STR_LIT>' , bbox_inches = '<STR_LIT>' ) <EOL> img_denoise = nib . Nifti1Image ( den , None , hdr_0 ) <EOL> img_diff = nib . Nifti1Image ( diff_3d , None , hdr_0 ) <EOL> if output_file_name != None : <EOL> output_file_name = output_file_name <EOL> else : output_file_name = file + '<STR_LIT>' + ext <EOL> nib . save ( img_denoise , output_file_name ) <EOL> nib . save ( img_diff , file + '<STR_LIT>' + ext ) <EOL> if __name__ == "<STR_LIT:__main__>" : <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> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT>" , <EOL> description = "<STR_LIT>" , <EOL> mandatory = False , <EOL> example = [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> default_value = "<STR_LIT>" ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT:int>" , <EOL> description = "<STR_LIT>" , <EOL> mandatory = False , <EOL> default_value = "<STR_LIT>" ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT:float>" , <EOL> description = "<STR_LIT>" , <EOL> mandatory = False , <EOL> default_value = "<STR_LIT>" ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT>" , <EOL> description = "<STR_LIT>" , <EOL> mandatory = False ) <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>' ] , <EOL> default_value = "<STR_LIT:1>" ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT>" , <EOL> description = "<STR_LIT>" , <EOL> mandatory = False , <EOL> default_value = '<STR_LIT:0>' , <EOL> example = [ '<STR_LIT:0>' , '<STR_LIT:1>' , '<STR_LIT:2>' ] ) <EOL> arguments = parser . parse ( sys . argv [ <NUM_LIT:1> : ] ) <EOL> parameter = arguments [ "<STR_LIT>" ] <EOL> remove_temp_files = int ( arguments [ "<STR_LIT>" ] ) <EOL> verbose = int ( arguments [ "<STR_LIT>" ] ) <EOL> noise_threshold = int ( arguments [ '<STR_LIT>' ] ) <EOL> std_noise = float ( arguments [ '<STR_LIT>' ] ) <EOL> if "<STR_LIT>" in arguments : <EOL> file_to_denoise = arguments [ "<STR_LIT>" ] <EOL> else : file_to_denoise = None <EOL> if "<STR_LIT>" in arguments : <EOL> output_file_name = arguments [ "<STR_LIT>" ] <EOL> else : output_file_name = None <EOL> param = Param ( ) <EOL> param . verbose = verbose <EOL> param . remove_temp_files = remove_temp_files <EOL> param . parameter = parameter <EOL> main ( file_to_denoise , param , output_file_name ) </s>
