def __init__(self, fname_src, fname_transfo, warp_atlas, warp_spinal_levels, folder_out, path_template, verbose, qc): # Initialization self.fname_src = fname_src self.fname_transfo = fname_transfo self.warp_atlas = warp_atlas self.warp_spinal_levels = warp_spinal_levels self.folder_out = folder_out self.path_template = path_template self.folder_template = param.folder_template self.folder_atlas = param.folder_atlas self.folder_spinal_levels = param.folder_spinal_levels self.verbose = verbose self.qc = qc # sct.printv(arguments) sct.printv('\nCheck parameters:') sct.printv(' Working directory ........ ' + os.getcwd()) sct.printv(' Destination image ........ ' + self.fname_src) sct.printv(' Warping field ............ ' + self.fname_transfo) sct.printv(' Path template ............ ' + self.path_template) sct.printv(' Output folder ............ ' + self.folder_out + '\n') # create output folder if not os.path.exists(self.folder_out): os.makedirs(self.folder_out) # Warp template objects sct.printv('\nWARP TEMPLATE:', self.verbose) warp_label(self.path_template, self.folder_template, param.file_info_label, self.fname_src, self.fname_transfo, self.folder_out) # Warp atlas if self.warp_atlas == 1: sct.printv('\nWARP ATLAS OF WHITE MATTER TRACTS:', self.verbose) warp_label(self.path_template, self.folder_atlas, param.file_info_label, self.fname_src, self.fname_transfo, self.folder_out) # Warp spinal levels if self.warp_spinal_levels == 1: sct.printv('\nWARP SPINAL LEVELS:', self.verbose) warp_label(self.path_template, self.folder_spinal_levels, param.file_info_label, self.fname_src, self.fname_transfo, self.folder_out) # to view results sct.printv('\nDone! To view results, type:', self.verbose) sct.printv('fslview ' + self.fname_src + ' ' \ + self.folder_out + self.folder_template + get_file_label(self.folder_out + self.folder_template, 'T2') + ' -b 0,4000 ' \ + self.folder_out + self.folder_template + get_file_label(self.folder_out + self.folder_template, 'vertebral') + ' -l MGH-Cortical -t 0.5 ' \ + self.folder_out + self.folder_template + get_file_label(self.folder_out + self.folder_template, 'gray matter') + ' -l Red-Yellow -b 0.5,1 ' \ + self.folder_out + self.folder_template + get_file_label(self.folder_out + self.folder_template, 'white matter') + ' -l Blue-Lightblue -b 0.5,1 &\n', self.verbose, 'info') if self.qc: from msct_image import Image # output QC image im = Image(self.fname_src) im_wm = Image(self.folder_out + self.folder_template + get_file_label(self.folder_out + self.folder_template, 'white matter')) im.save_quality_control(plane='axial', n_slices=4, seg=im_wm, thr=0.5, cmap_col='blue-cyan', path_output=self.folder_out)
def segment(self): before = time.time() sct.run('mkdir ' + self.tmp_dir) self.segmentation_pipeline() # Generate output files: for res_fname in self.res_names.values(): sct.generate_output_file(self.tmp_dir+res_fname, self.seg_param.output_path+res_fname) if self.ref_gm_seg_fname is not None: sct.generate_output_file(self.tmp_dir+self.dice_name, self.seg_param.output_path+self.dice_name) sct.generate_output_file(self.tmp_dir+self.hausdorff_name, self.seg_param.output_path+self.hausdorff_name) if compute_ratio: sct.generate_output_file(self.tmp_dir+self.ratio_name, self.seg_param.output_path+self.ratio_name) after = time.time() sct.printv('Done! (in ' + str(after-before) + ' sec) \nTo see the result, type :') if self.seg_param.res_type == 'binary': wm_col = 'Red' gm_col = 'Blue' b = '0,1' else: wm_col = 'Blue-Lightblue' gm_col = 'Red-Yellow' b = '0.3,1' sct.printv('fslview ' + self.target_fname + ' '+self.seg_param.output_path+self.res_names['wm_seg']+' -l '+wm_col+' -t 0.4 -b '+b+' '+self.seg_param.output_path+self.res_names['gm_seg']+' -l '+gm_col+' -t 0.4 -b '+b+' &', self.seg_param.verbose, 'info') if self.seg_param.qc: # output QC image im = Image(self.target_fname) im_gmseg = Image(self.seg_param.output_path+self.res_names['gm_seg']) im.save_quality_control(plane='axial', n_slices=5, seg=im_gmseg, thr=float(b.split(',')[0]), cmap_col='red-yellow', path_output=self.seg_param.output_path) if self.seg_param.remove_tmp: sct.printv('Remove temporary folder ...', self.seg_param.verbose, 'normal') sct.run('rm -rf '+self.tmp_dir)
def main(args=None): if args is None: args = sys.argv[1:] # create param objects param_seg = ParamSeg() param_data = ParamData() param_model = ParamModel() param = Param() # get parser parser = get_parser() arguments = parser.parse(args) # set param arguments ad inputted by user param_seg.fname_im = arguments["-i"] param_seg.fname_im_original = arguments["-i"] param_seg.fname_seg = arguments["-s"] if '-vertfile' in arguments: if extract_fname(arguments['-vertfile'])[1].lower() == "none": param_seg.fname_level = None elif os.path.isfile(arguments['-vertfile']): param_seg.fname_level = arguments['-vertfile'] else: param_seg.fname_level = None printv('WARNING: -vertfile input file: "' + arguments['-vertfile'] + '" does not exist.\nSegmenting GM without using vertebral information', 1, 'warning') if '-denoising' in arguments: param_data.denoising = bool(int(arguments['-denoising'])) if '-normalization' in arguments: param_data.normalization = bool(int(arguments['-normalization'])) if '-p' in arguments: param_data.register_param = arguments['-p'] if '-w-levels' in arguments: param_seg.weight_level = arguments['-w-levels'] if '-w-coordi' in arguments: param_seg.weight_coord = arguments['-w-coordi'] if '-thr-sim' in arguments: param_seg.thr_similarity = arguments['-thr-sim'] if '-model' in arguments: param_model.path_model_to_load = os.path.abspath(arguments['-model']) if '-res-type' in arguments: param_seg.type_seg = arguments['-res-type'] if '-ratio' in arguments: param_seg.ratio = arguments['-ratio'] if '-ref' in arguments: param_seg.fname_manual_gmseg = arguments['-ref'] if '-ofolder' in arguments: param_seg.path_results = arguments['-ofolder'] if '-qc' in arguments: param_seg.qc = bool(int(arguments['-qc'])) if '-r' in arguments: param.rm_tmp = bool(int(arguments['-r'])) if '-v' in arguments: param.verbose = arguments['-v'] start_time = time.time() seg_gm = SegmentGM(param_seg=param_seg, param_data=param_data, param_model=param_model, param=param) seg_gm.segment() elapsed_time = time.time() - start_time printv('\nFinished! Elapsed time: ' + str(int(round(elapsed_time))) + 's', param.verbose) # save quality control and sct.printv(info) if param_seg.type_seg == 'bin': wm_col = 'Red' gm_col = 'Blue' b = '0,1' else: wm_col = 'Blue-Lightblue' gm_col = 'Red-Yellow' b = '0.4,1' if param_seg.qc: # output QC image printv('\nSave quality control images...', param.verbose, 'normal') im = Image(seg_gm.tmp_dir + param_seg.fname_im) im.save_quality_control(plane='axial', n_slices=5, seg=seg_gm.im_res_gmseg, thr=float(b.split(',')[0]), cmap_col='red-yellow', path_output=param_seg.path_results) if param.rm_tmp: # remove tmp_dir shutil.rmtree(seg_gm.tmp_dir) printv('\nDone! To view results, type:', param.verbose) printv('fslview ' + param_seg.fname_im_original + ' ' + seg_gm.fname_res_gmseg + ' -b ' + b + ' -l ' + gm_col + ' -t 0.7 ' + seg_gm.fname_res_wmseg + ' -b ' + b + ' -l ' + wm_col + ' -t 0.7 & \n', param.verbose, 'info')
def __init__(self, fname_src, fname_transfo, warp_atlas, warp_spinal_levels, folder_out, path_template, verbose, qc): # Initialization self.fname_src = fname_src self.fname_transfo = fname_transfo self.warp_atlas = warp_atlas self.warp_spinal_levels = warp_spinal_levels self.folder_out = folder_out self.path_template = path_template self.folder_template = param.folder_template self.folder_atlas = param.folder_atlas self.folder_spinal_levels = param.folder_spinal_levels self.verbose = verbose self.qc = qc start_time = time.time() # Check file existence sct.printv('\nCheck file existence...', self.verbose) sct.check_file_exist(self.fname_src) sct.check_file_exist(self.fname_transfo) # add slash at the end of folder name (in case there is no slash) self.path_template = sct.slash_at_the_end(self.path_template, 1) self.folder_out = sct.slash_at_the_end(self.folder_out, 1) self.folder_template = sct.slash_at_the_end(self.folder_template, 1) self.folder_atlas = sct.slash_at_the_end(self.folder_atlas, 1) self.folder_spinal_levels = sct.slash_at_the_end(self.folder_spinal_levels, 1) # print arguments print '\nCheck parameters:' print ' Destination image ........ '+self.fname_src print ' Warping field ............ '+self.fname_transfo print ' Path template ............ '+self.path_template print ' Output folder ............ '+self.folder_out+'\n' # Extract path, file and extension path_src, file_src, ext_src = sct.extract_fname(self.fname_src) # create output folder if os.path.exists(self.folder_out): sct.printv('WARNING: Output folder already exists. Deleting it...', self.verbose, 'warning') sct.run('rm -rf '+self.folder_out) sct.run('mkdir '+self.folder_out) # Warp template objects sct.printv('\nWarp template objects...', self.verbose) warp_label(self.path_template, self.folder_template, param.file_info_label, self.fname_src, self.fname_transfo, self.folder_out) # Warp atlas if self.warp_atlas == 1: sct.printv('\nWarp atlas of white matter tracts...', self.verbose) warp_label(self.path_template, self.folder_atlas, param.file_info_label, self.fname_src, self.fname_transfo, self.folder_out) # Warp spinal levels if self.warp_spinal_levels == 1: sct.printv('\nWarp spinal levels...', self.verbose) warp_label(self.path_template, self.folder_spinal_levels, param.file_info_label, self.fname_src, self.fname_transfo, self.folder_out) # to view results sct.printv('\nDone! To view results, type:', self.verbose) sct.printv('fslview '+self.fname_src+' '+self.folder_out+self.folder_template+'MNI-Poly-AMU_T2.nii.gz -b 0,4000 '+self.folder_out+self.folder_template+'MNI-Poly-AMU_level.nii.gz -l MGH-Cortical -t 0.5 '+self.folder_out+self.folder_template+'MNI-Poly-AMU_GM.nii.gz -l Red-Yellow -b 0.5,1 '+self.folder_out+self.folder_template+'MNI-Poly-AMU_WM.nii.gz -l Blue-Lightblue -b 0.5,1 &\n', self.verbose, 'info') if self.qc: from msct_image import Image # output QC image im = Image(self.fname_src) im_wm = Image(self.folder_out+self.folder_template+'MNI-Poly-AMU_WM.nii.gz') im.save_quality_control(plane='axial', n_slices=4, seg=im_wm, thr=0.5, cmap_col='blue-cyan', path_output=self.folder_out)
def __init__(self, fname_src, fname_transfo, warp_atlas, warp_spinal_levels, folder_out, path_template, verbose, qc): # Initialization self.fname_src = fname_src self.fname_transfo = fname_transfo self.warp_atlas = warp_atlas self.warp_spinal_levels = warp_spinal_levels self.folder_out = folder_out self.path_template = path_template self.folder_template = param.folder_template self.folder_atlas = param.folder_atlas self.folder_spinal_levels = param.folder_spinal_levels self.verbose = verbose self.qc = qc start_time = time.time() # add slash at the end of folder name (in case there is no slash) # self.path_template = sct.slash_at_the_end(self.path_template, 1) # self.folder_out = sct.slash_at_the_end(self.folder_out, 1) # self.folder_template = sct.slash_at_the_end(self.folder_template, 1) # self.folder_atlas = sct.slash_at_the_end(self.folder_atlas, 1) # self.folder_spinal_levels = sct.slash_at_the_end(self.folder_spinal_levels, 1) # print arguments print '\nCheck parameters:' print ' Working directory ........ '+os.getcwd() print ' Destination image ........ '+self.fname_src print ' Warping field ............ '+self.fname_transfo print ' Path template ............ '+self.path_template print ' Output folder ............ '+self.folder_out+'\n' # create output folder if os.path.exists(self.folder_out): sct.printv('WARNING: Output folder already exists. Deleting it...', self.verbose, 'warning') sct.run('rm -rf '+self.folder_out) sct.run('mkdir '+self.folder_out) # Warp template objects sct.printv('\nWARP TEMPLATE:', self.verbose) warp_label(self.path_template, self.folder_template, param.file_info_label, self.fname_src, self.fname_transfo, self.folder_out) # Warp atlas if self.warp_atlas == 1: sct.printv('\nWARP ATLAS OF WHITE MATTER TRACTS:', self.verbose) warp_label(self.path_template, self.folder_atlas, param.file_info_label, self.fname_src, self.fname_transfo, self.folder_out) # Warp spinal levels if self.warp_spinal_levels == 1: sct.printv('\nWARP SPINAL LEVELS:', self.verbose) warp_label(self.path_template, self.folder_spinal_levels, param.file_info_label, self.fname_src, self.fname_transfo, self.folder_out) # to view results sct.printv('\nDone! To view results, type:', self.verbose) sct.printv('fslview '+self.fname_src+' ' \ + self.folder_out + self.folder_template + get_file_label(self.folder_out + self.folder_template, 'T2') + ' -b 0,4000 ' \ + self.folder_out + self.folder_template + get_file_label(self.folder_out + self.folder_template, 'vertebral') + ' -l MGH-Cortical -t 0.5 ' \ + self.folder_out + self.folder_template + get_file_label(self.folder_out + self.folder_template, 'gray matter') + ' -l Red-Yellow -b 0.5,1 ' \ + self.folder_out + self.folder_template + get_file_label(self.folder_out + self.folder_template, 'white matter') + ' -l Blue-Lightblue -b 0.5,1 &\n', self.verbose, 'info') if self.qc: from msct_image import Image # output QC image im = Image(self.fname_src) im_wm = Image(self.folder_out + self.folder_template + get_file_label(self.folder_out + self.folder_template, 'white matter')) im.save_quality_control(plane='axial', n_slices=4, seg=im_wm, thr=0.5, cmap_col='blue-cyan', path_output=self.folder_out)
def __init__(self, fname_src, fname_transfo, warp_atlas, warp_spinal_levels, folder_out, path_template, verbose, qc): # Initialization self.fname_src = fname_src self.fname_transfo = fname_transfo self.warp_atlas = warp_atlas self.warp_spinal_levels = warp_spinal_levels self.folder_out = folder_out self.path_template = path_template self.folder_template = param.folder_template self.folder_atlas = param.folder_atlas self.folder_spinal_levels = param.folder_spinal_levels self.verbose = verbose self.qc = qc start_time = time.time() # add slash at the end of folder name (in case there is no slash) # self.path_template = sct.slash_at_the_end(self.path_template, 1) # self.folder_out = sct.slash_at_the_end(self.folder_out, 1) # self.folder_template = sct.slash_at_the_end(self.folder_template, 1) # self.folder_atlas = sct.slash_at_the_end(self.folder_atlas, 1) # self.folder_spinal_levels = sct.slash_at_the_end(self.folder_spinal_levels, 1) # print arguments print '\nCheck parameters:' print ' Working directory ........ ' + os.getcwd() print ' Destination image ........ ' + self.fname_src print ' Warping field ............ ' + self.fname_transfo print ' Path template ............ ' + self.path_template print ' Output folder ............ ' + self.folder_out + '\n' # create output folder if os.path.exists(self.folder_out): sct.printv('WARNING: Output folder already exists. Deleting it...', self.verbose, 'warning') sct.run('rm -rf ' + self.folder_out) sct.run('mkdir ' + self.folder_out) # Warp template objects sct.printv('\nWARP TEMPLATE:', self.verbose) warp_label(self.path_template, self.folder_template, param.file_info_label, self.fname_src, self.fname_transfo, self.folder_out) # Warp atlas if self.warp_atlas == 1: sct.printv('\nWARP ATLAS OF WHITE MATTER TRACTS:', self.verbose) warp_label(self.path_template, self.folder_atlas, param.file_info_label, self.fname_src, self.fname_transfo, self.folder_out) # Warp spinal levels if self.warp_spinal_levels == 1: sct.printv('\nWARP SPINAL LEVELS:', self.verbose) warp_label(self.path_template, self.folder_spinal_levels, param.file_info_label, self.fname_src, self.fname_transfo, self.folder_out) # to view results sct.printv('\nDone! To view results, type:', self.verbose) sct.printv('fslview ' + self.fname_src + ' ' \ + self.folder_out + self.folder_template + get_file_label(self.folder_out + self.folder_template, 'T2') + ' -b 0,4000 ' \ + self.folder_out + self.folder_template + get_file_label(self.folder_out + self.folder_template, 'vertebral') + ' -l MGH-Cortical -t 0.5 ' \ + self.folder_out + self.folder_template + get_file_label(self.folder_out + self.folder_template, 'gray matter') + ' -l Red-Yellow -b 0.5,1 ' \ + self.folder_out + self.folder_template + get_file_label(self.folder_out + self.folder_template, 'white matter') + ' -l Blue-Lightblue -b 0.5,1 &\n', self.verbose, 'info') if self.qc: from msct_image import Image # output QC image im = Image(self.fname_src) im_wm = Image( self.folder_out + self.folder_template + get_file_label(self.folder_out + self.folder_template, 'white matter')) im.save_quality_control(plane='axial', n_slices=4, seg=im_wm, thr=0.5, cmap_col='blue-cyan', path_output=self.folder_out)
def segment(self): self.copy_data_to_tmp() # go to tmp directory os.chdir(self.tmp_dir) # load model self.model.load_model() self.target_im, self.info_preprocessing = pre_processing(self.param_seg.fname_im, self.param_seg.fname_seg, self.param_seg.fname_level, new_res=self.param_data.axial_res, square_size_size_mm=self.param_data.square_size_size_mm, denoising=self.param_data.denoising, verbose=self.param.verbose, rm_tmp=self.param.rm_tmp) printv('\nRegister target image to model data...', self.param.verbose, 'normal') # register target image to model dictionary space path_warp = self.register_target() printv('\nNormalize intensity of target image...', self.param.verbose, 'normal') self.normalize_target() printv('\nProject target image into the model reduced space...', self.param.verbose, 'normal') self.project_target() printv('\nCompute similarities between target slices and model slices using model reduced space...', self.param.verbose, 'normal') list_dic_indexes_by_slice = self.compute_similarities() printv('\nLabel fusion of model slices most similar to target slices...', self.param.verbose, 'normal') self.label_fusion(list_dic_indexes_by_slice) printv('\nWarp back segmentation into image space...', self.param.verbose, 'normal') self.warp_back_seg(path_warp) printv('\nPost-processing...', self.param.verbose, 'normal') self.im_res_gmseg, self.im_res_wmseg = self.post_processing() if (self.param_seg.path_results != './') and (not os.path.exists('../'+self.param_seg.path_results)): # create output folder printv('\nCreate output folder ...', self.param.verbose, 'normal') os.chdir('..') os.mkdir(self.param_seg.path_results) os.chdir(self.tmp_dir) if self.param_seg.fname_manual_gmseg is not None: # compute validation metrics printv('\nCompute validation metrics...', self.param.verbose, 'normal') self.validation() if self.param_seg.ratio is not '0': printv('\nCompute GM/WM CSA ratio...', self.param.verbose, 'normal') self.compute_ratio() # go back to original directory os.chdir('..') printv('\nSave resulting GM and WM segmentations...', self.param.verbose, 'normal') fname_res_gmseg = self.param_seg.path_results+add_suffix(''.join(extract_fname(self.param_seg.fname_im)[1:]), '_gmseg') fname_res_wmseg = self.param_seg.path_results+add_suffix(''.join(extract_fname(self.param_seg.fname_im)[1:]), '_wmseg') self.im_res_gmseg.setFileName(fname_res_gmseg) self.im_res_wmseg.setFileName(fname_res_wmseg) self.im_res_gmseg.save() self.im_res_wmseg.save() # save quality control and print info if self.param_seg.type_seg == 'bin': wm_col = 'Red' gm_col = 'Blue' b = '0,1' else: wm_col = 'Blue-Lightblue' gm_col = 'Red-Yellow' b = '0.4,1' if self.param_seg.qc: # output QC image printv('\nSave quality control images...', self.param.verbose, 'normal') im = Image(self.tmp_dir+self.param_seg.fname_im) im.save_quality_control(plane='axial', n_slices=5, seg=self.im_res_gmseg, thr=float(b.split(',')[0]), cmap_col='red-yellow', path_output=self.param_seg.path_results) printv('\nDone! To view results, type:', self.param.verbose) printv('fslview '+self.param_seg.fname_im_original+' '+fname_res_gmseg+' -b '+b+' -l '+gm_col+' -t 0.7 '+fname_res_wmseg+' -b '+b+' -l '+wm_col+' -t 0.7 & \n', self.param.verbose, 'info') if self.param.rm_tmp: # remove tmp_dir shutil.rmtree(self.tmp_dir)