def run(algorithm, config, software, im_fns, result_dir, configFN=None, configSoftware=None, file_list_file_name=None): if not os.path.isdir(result_dir): os.makedirs(result_dir) config.file_list_file_name = os.path.join(config.result_dir,os.path.basename(file_list_file_name)) savedFileName = lambda name, default: os.path.basename(name) if name else default pyLAR.saveConfiguration(os.path.join(result_dir, savedFileName(configFN, 'Config.txt')), config) pyLAR.saveConfiguration(os.path.join(result_dir, savedFileName(configSoftware, 'Software.txt')), software) pyLAR.writeTxtFromList(os.path.join(result_dir, savedFileName(file_list_file_name, 'listFiles.txt')), im_fns) # Set maximum number of threads used by ITK filters if hasattr(config, "ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS"): os.environ["ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS"] = str(config.ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS) if algorithm == "lr": pyLAR.lr.run(config, software, im_fns) elif algorithm == "uab": pyLAR.uab.run(config, software, im_fns) elif algorithm == "nglra": pyLAR.nglra.run(config, software, im_fns) else: raise Exception("Algorithm selected is not part of pyLAR")
def run(algorithm, config, software, im_fns, result_dir, configFN=None, configSoftware=None, file_list_file_name=None): if not os.path.isdir(result_dir): os.makedirs(result_dir) config.file_list_file_name = os.path.join( config.result_dir, os.path.basename(file_list_file_name)) savedFileName = lambda name, default: os.path.basename( name) if name else default pyLAR.saveConfiguration( os.path.join(result_dir, savedFileName(configFN, 'Config.txt')), config) pyLAR.saveConfiguration( os.path.join(result_dir, savedFileName(configSoftware, 'Software.txt')), software) pyLAR.writeTxtFromList( os.path.join(result_dir, savedFileName(file_list_file_name, 'listFiles.txt')), im_fns) # Set maximum number of threads used by ITK filters if hasattr(config, "ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS"): os.environ["ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS"] = str( config.ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS) if algorithm == "lr": pyLAR.lr.run(config, software, im_fns) elif algorithm == "uab": pyLAR.uab.run(config, software, im_fns) elif algorithm == "nglra": pyLAR.nglra.run(config, software, im_fns) else: raise Exception("Algorithm selected is not part of pyLAR")
def run(config, software, im_fns, check=True): """Low-rank decomposition.""" log = logging.getLogger(__name__) # Checks that all variables are set correctly if check: check_requirements(config, software) # Initialize variables selection = config.selection result_dir = config.result_dir sigma = config.sigma reference_im_fn = config.reference_im_fn num_of_data = len(selection) # Pre-processing: registration and histogram matching s = time.time() if config.registration == 'affine': log.info('Affine registration') pyLAR.affineRegistrationStep(software.EXE_BRAINSFit, im_fns, result_dir, selection, reference_im_fn) elif config.registration == 'rigid': log.info('Rigid registration') pyLAR.rigidRegistrationStep(software.EXE_BRAINSFit, im_fns, result_dir, selection, reference_im_fn) elif config.registration == 'none': pass else: raise Exception('Unknown registration') if config.histogram_matching: pyLAR.histogramMatchingStep(selection, result_dir) e = time.time() l = e - s log.info('Preprocessing - total running time: %f mins' % (l / 60.0)) # Loading images and blurring them if option selected. s = time.time() im_ref = sitk.ReadImage(reference_im_fn) im_ref_array = sitk.GetArrayFromImage(im_ref) z_dim, x_dim, y_dim = im_ref_array.shape vector_length = z_dim * x_dim * y_dim del im_ref, im_ref_array Y = np.zeros((vector_length, num_of_data)) for i in range(num_of_data): if config.registration == 'none': im_file = im_fns[selection[i]] else: im_file = os.path.join(result_dir, 'L0_Iter0_' + str(i) + '.nrrd') log.info("Input File: " + im_file) inIm = sitk.ReadImage(im_file) tmp = sitk.GetArrayFromImage(inIm) if sigma > 0: # blurring log.info("Blurring: " + str(sigma)) outIm = pyLAR.GaussianBlur(inIm, None, sigma) tmp = sitk.GetArrayFromImage(outIm) Y[:, i] = tmp.reshape(-1) del tmp # Low-Rank and sparse decomposition low_rank, sparse, n_iter, rank, sparsity, sum_sparse = pyLAR.rpca( Y, config.lamda) lr_fn = pyLAR.saveImagesFromDM(low_rank, os.path.join(result_dir, 'L' + '_LowRank_'), reference_im_fn) sp_fn = pyLAR.saveImagesFromDM(sparse, os.path.join(result_dir, 'L' + '_Sparse_'), reference_im_fn) pyLAR.writeTxtFromList(os.path.join(result_dir, 'list_outputs.txt'), lr_fn + sp_fn) e = time.time() l = e - s log.info("Rank: " + str(rank)) log.info("Sparsity: " + str(sparsity)) log.info('Processing - total running time: %f mins' % (l / 60.0)) return sparsity, sum_sparse
def run(config, software, im_fns, check=True): """Low-rank decomposition.""" log = logging.getLogger(__name__) # Checks that all variables are set correctly if check: check_requirements(config, software) # Initialize variables selection = config.selection result_dir = config.result_dir sigma = config.sigma reference_im_fn = config.reference_im_fn num_of_data = len(selection) # Pre-processing: registration and histogram matching s = time.time() if config.registration == 'affine': log.info('Affine registration') pyLAR.affineRegistrationStep(software.EXE_BRAINSFit, im_fns, result_dir, selection, reference_im_fn) elif config.registration == 'rigid': log.info('Rigid registration') pyLAR.rigidRegistrationStep(software.EXE_BRAINSFit, im_fns, result_dir, selection, reference_im_fn) elif config.registration == 'none': pass else: raise Exception('Unknown registration') if config.histogram_matching: pyLAR.histogramMatchingStep(selection, result_dir) e = time.time() l = e - s log.info('Preprocessing - total running time: %f mins' % (l / 60.0)) # Loading images and blurring them if option selected. s = time.time() im_ref = sitk.ReadImage(reference_im_fn) im_ref_array = sitk.GetArrayFromImage(im_ref) z_dim, x_dim, y_dim = im_ref_array.shape vector_length = z_dim * x_dim * y_dim del im_ref, im_ref_array Y = np.zeros((vector_length, num_of_data)) for i in range(num_of_data): if config.registration == 'none': im_file = im_fns[selection[i]] else: im_file = os.path.join(result_dir, 'L0_Iter0_' + str(i) + '.nrrd') log.info("Input File: " + im_file) inIm = sitk.ReadImage(im_file) tmp = sitk.GetArrayFromImage(inIm) if sigma > 0: # blurring log.info("Blurring: " + str(sigma)) outIm = pyLAR.GaussianBlur(inIm, None, sigma) tmp = sitk.GetArrayFromImage(outIm) Y[:, i] = tmp.reshape(-1) del tmp # Low-Rank and sparse decomposition low_rank, sparse, n_iter, rank, sparsity, sum_sparse = pyLAR.rpca(Y, config.lamda) lr_fn = pyLAR.saveImagesFromDM(low_rank, os.path.join(result_dir, 'L' + '_LowRank_'), reference_im_fn) sp_fn = pyLAR.saveImagesFromDM(sparse, os.path.join(result_dir, 'L' + '_Sparse_'), reference_im_fn) pyLAR.writeTxtFromList(os.path.join(result_dir,'list_outputs.txt'),lr_fn+sp_fn) e = time.time() l = e - s log.info("Rank: " + str(rank)) log.info("Sparsity: " + str(sparsity)) log.info('Processing - total running time: %f mins' % (l / 60.0)) return sparsity, sum_sparse
def run(config, software, im_fns, check=True): """Unbiased atlas building - Atlas-to-image registration""" log = logging.getLogger(__name__) if check: check_requirements(config, software) reference_im_fn = config.reference_im_fn selection = config.selection result_dir = config.result_dir ants_params = config.ants_params num_of_iterations_per_level = config.num_of_iterations_per_level num_of_levels = config.num_of_levels # multiscale bluring (coarse-to-fine) s = time.time() pyLAR.affineRegistrationStep(software.EXE_BRAINSFit, im_fns, result_dir, selection, reference_im_fn) if config.histogram_matching: pyLAR.histogramMatchingStep(selection, result_dir) num_of_data = len(selection) iterCount = 0 for level in range(0, num_of_levels): for iterCount in range(1, num_of_iterations_per_level + 1): log.info('Level: ' + str(level)) log.info('Iteration ' + str(iterCount)) _runIteration(level, iterCount, ants_params, result_dir, selection, software) gc.collect() # garbage collection # We need to check if num_of_iterations_per_level is set to 0, which leads # to computing an average on the affine registration. if level != num_of_levels - 1: log.warning('No need for multiple levels! TO BE REMOVED!') for i in range(num_of_data): current_file_name = 'L' + str(level) + '_Iter' + str( iterCount) + '_' + str(i) + '.nrrd' current_file_path = os.path.join(result_dir, current_file_name) nextLevelInitIm = os.path.join( result_dir, 'L' + str(level + 1) + '_Iter0_' + str(i) + '.nrrd') shutil.copyfile(current_file_path, nextLevelInitIm) # if num_of_levels > 1: # print 'WARNING: No need for multiple levels! TO BE REMOVED!' # for i in range(num_of_data): # next_prefix = 'L' + str(level+1) + '_Iter0_' # next_path = os.path.join(result_dir, next_prefix) # newLevelInitIm = next_path + str(i) + '.nrrd' current_prefix = 'L' + str(num_of_levels - 1) + '_Iter' + str(num_of_iterations_per_level) current_path = os.path.join(result_dir, current_prefix) atlasIm = current_path + '_atlas.nrrd' listOfImages = [] num_of_data = len(selection) for i in range(num_of_data): lrIm = current_path + '_' + str(i) + '.nrrd' listOfImages.append(lrIm) pyLAR.AverageImages(software.EXE_AverageImages, listOfImages, atlasIm) logging.debug("Saves list outputs:%s" % (os.path.join(result_dir, 'list_outputs.txt'))) pyLAR.writeTxtFromList(os.path.join(result_dir, 'list_outputs.txt'), [atlasIm]) try: import matplotlib.pyplot as plt import SimpleITK as sitk import numpy as np im = sitk.ReadImage(atlasIm) im_array = sitk.GetArrayFromImage(im) z_dim, x_dim, y_dim = im_array.shape plt.figure() plt.imshow(np.flipud(im_array[z_dim / 2, :]), plt.cm.gray) plt.title(current_prefix + ' atlas') plt.savefig(current_path + '.png') except ImportError: pass e = time.time() l = e - s log.info('Total running time: %f mins' % (l / 60.0))
def run(config, software, im_fns, check=True): """Unbiased atlas building - Atlas-to-image registration""" log = logging.getLogger(__name__) if check: check_requirements(config, software) reference_im_fn = config.reference_im_fn selection = config.selection result_dir = config.result_dir ants_params = config.ants_params num_of_iterations_per_level = config.num_of_iterations_per_level num_of_levels = config.num_of_levels # multiscale bluring (coarse-to-fine) s = time.time() pyLAR.affineRegistrationStep(software.EXE_BRAINSFit, im_fns, result_dir, selection, reference_im_fn) if config.histogram_matching: pyLAR.histogramMatchingStep(selection, result_dir) num_of_data = len(selection) iterCount = 0 for level in range(0, num_of_levels): for iterCount in range(1, num_of_iterations_per_level+1): log.info('Level: ' + str(level)) log.info('Iteration ' + str(iterCount)) _runIteration(level, iterCount, ants_params, result_dir, selection, software) gc.collect() # garbage collection # We need to check if num_of_iterations_per_level is set to 0, which leads # to computing an average on the affine registration. if level != num_of_levels - 1: log.warning('No need for multiple levels! TO BE REMOVED!') for i in range(num_of_data): current_file_name = 'L' + str(level) + '_Iter' + str(iterCount) + '_' + str(i) + '.nrrd' current_file_path = os.path.join(result_dir, current_file_name) nextLevelInitIm = os.path.join(result_dir, 'L'+str(level+1)+'_Iter0_' + str(i) + '.nrrd') shutil.copyfile(current_file_path, nextLevelInitIm) # if num_of_levels > 1: # print 'WARNING: No need for multiple levels! TO BE REMOVED!' # for i in range(num_of_data): # next_prefix = 'L' + str(level+1) + '_Iter0_' # next_path = os.path.join(result_dir, next_prefix) # newLevelInitIm = next_path + str(i) + '.nrrd' current_prefix = 'L' + str(num_of_levels-1) + '_Iter' + str(num_of_iterations_per_level) current_path = os.path.join(result_dir, current_prefix) atlasIm = current_path + '_atlas.nrrd' listOfImages = [] num_of_data = len(selection) for i in range(num_of_data): lrIm = current_path + '_' + str(i) + '.nrrd' listOfImages.append(lrIm) pyLAR.AverageImages(software.EXE_AverageImages, listOfImages, atlasIm) logging.debug("Saves list outputs:%s"%(os.path.join(result_dir,'list_outputs.txt'))) pyLAR.writeTxtFromList(os.path.join(result_dir,'list_outputs.txt'),[atlasIm]) try: import matplotlib.pyplot as plt import SimpleITK as sitk import numpy as np im = sitk.ReadImage(atlasIm) im_array = sitk.GetArrayFromImage(im) z_dim, x_dim, y_dim = im_array.shape plt.figure() plt.imshow(np.flipud(im_array[z_dim/2, :]), plt.cm.gray) plt.title(current_prefix + ' atlas') plt.savefig(current_path + '.png') except ImportError: pass e = time.time() l = e - s log.info('Total running time: %f mins' % (l/60.0))
def run(config, software, im_fns, check=True): """unbiased low-rank atlas creation from a selection of images""" log = logging.getLogger(__name__) if check: check_requirements(config, software) reference_im_fn = config.reference_im_fn result_dir = config.result_dir selection = config.selection lamda = config.lamda sigma = config.sigma num_of_iterations_per_level = config.num_of_iterations_per_level num_of_levels = config.num_of_levels # Multi-scale blurring (coarse-to-fine) registration_type = config.registration_type gridSize = [0, 0, 0] if registration_type == 'BSpline': gridSize = config.gridSize s = time.time() pyLAR.showImageMidSlice(reference_im_fn) pyLAR.affineRegistrationStep(software.EXE_BRAINSFit, im_fns, result_dir, selection, reference_im_fn) if config.histogram_matching: pyLAR.histogramMatchingStep(selection, result_dir) im_ref = sitk.ReadImage(reference_im_fn) im_ref_array = sitk.GetArrayFromImage(im_ref) z_dim, x_dim, y_dim = im_ref_array.shape vector_length = z_dim * x_dim * y_dim del im_ref, im_ref_array num_of_data = len(selection) factor = 0.5 # BSpline max displacement constrain, 0.5 refers to half of the grid size iterCount = 0 for level in range(0, num_of_levels): for iterCount in range(1, num_of_iterations_per_level + 1): maxDisp = -1 log.info('Level: ' + str(level)) log.info('Iteration ' + str(iterCount) + ' lamda = %f' % lamda) log.info('Blurring Sigma: ' + str(sigma)) if registration_type == 'BSpline': log.info('Grid size: ' + str(gridSize)) maxDisp = z_dim / gridSize[2] * factor _, _, listOutputImages = _runIteration(vector_length, level, iterCount, config, im_fns, sigma, gridSize, maxDisp, software) # Adjust grid size for finner BSpline Registration if registration_type == 'BSpline' and gridSize[0] < 10: gridSize = np.add(gridSize, [1, 2, 1]) # Reduce the amount of blurring sizes gradually if sigma > 0: sigma = sigma - 0.5 gc.collect() # Garbage collection if level != num_of_levels - 1: log.warning('No need for multiple levels! TO BE REMOVED!') for i in range(num_of_data): current_file_name = 'L' + str(level) + '_Iter' + str(iterCount) + '_' + str(i) + '.nrrd' current_file_path = os.path.join(result_dir, current_file_name) nextLevelInitIm = os.path.join(result_dir, 'L' + str(level + 1) + '_Iter0_' + str(i) + '.nrrd') shutil.copyfile(current_file_path, nextLevelInitIm) if gridSize[0] < 10: gridSize = np.add(gridSize, [1, 2, 1]) if sigma > 0: sigma = sigma - 1 factor = factor * 0.5 # a = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss # print 'Current memory usage :',a/1024.0/1024.0,'GB' # h = hpy() # print h.heap() pyLAR.writeTxtFromList(os.path.join(result_dir,'list_outputs.txt'),listOutputImages) e = time.time() l = e - s log.info('Total running time: %f mins' % (l / 60.0))
def run(config, software, im_fns, check=True): """unbiased low-rank atlas creation from a selection of images""" log = logging.getLogger(__name__) if check: check_requirements(config, software) reference_im_fn = config.reference_im_fn result_dir = config.result_dir selection = config.selection lamda = config.lamda sigma = config.sigma num_of_iterations_per_level = config.num_of_iterations_per_level num_of_levels = config.num_of_levels # Multi-scale blurring (coarse-to-fine) registration_type = config.registration_type gridSize = [0, 0, 0] if registration_type == 'BSpline': gridSize = config.gridSize s = time.time() pyLAR.showImageMidSlice(reference_im_fn) pyLAR.affineRegistrationStep(software.EXE_BRAINSFit, im_fns, result_dir, selection, reference_im_fn) if config.histogram_matching: pyLAR.histogramMatchingStep(selection, result_dir) im_ref = sitk.ReadImage(reference_im_fn) im_ref_array = sitk.GetArrayFromImage(im_ref) z_dim, x_dim, y_dim = im_ref_array.shape vector_length = z_dim * x_dim * y_dim del im_ref, im_ref_array num_of_data = len(selection) factor = 0.5 # BSpline max displacement constrain, 0.5 refers to half of the grid size iterCount = 0 for level in range(0, num_of_levels): for iterCount in range(1, num_of_iterations_per_level + 1): maxDisp = -1 log.info('Level: ' + str(level)) log.info('Iteration ' + str(iterCount) + ' lamda = %f' % lamda) log.info('Blurring Sigma: ' + str(sigma)) if registration_type == 'BSpline': log.info('Grid size: ' + str(gridSize)) maxDisp = z_dim / gridSize[2] * factor _, _, listOutputImages = _runIteration(vector_length, level, iterCount, config, im_fns, sigma, gridSize, maxDisp, software) # Adjust grid size for finner BSpline Registration if registration_type == 'BSpline' and gridSize[0] < 10: gridSize = np.add(gridSize, [1, 2, 1]) # Reduce the amount of blurring sizes gradually if sigma > 0: sigma = sigma - 0.5 gc.collect() # Garbage collection if level != num_of_levels - 1: log.warning('No need for multiple levels! TO BE REMOVED!') for i in range(num_of_data): current_file_name = 'L' + str(level) + '_Iter' + str( iterCount) + '_' + str(i) + '.nrrd' current_file_path = os.path.join(result_dir, current_file_name) nextLevelInitIm = os.path.join( result_dir, 'L' + str(level + 1) + '_Iter0_' + str(i) + '.nrrd') shutil.copyfile(current_file_path, nextLevelInitIm) if gridSize[0] < 10: gridSize = np.add(gridSize, [1, 2, 1]) if sigma > 0: sigma = sigma - 1 factor = factor * 0.5 # a = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss # print 'Current memory usage :',a/1024.0/1024.0,'GB' # h = hpy() # print h.heap() pyLAR.writeTxtFromList(os.path.join(result_dir, 'list_outputs.txt'), listOutputImages) e = time.time() l = e - s log.info('Total running time: %f mins' % (l / 60.0))