def initSaveParameters(self, save_path, save_fident, orig_img_path=[None, None], orig_psf_path=[None, None], overwrite=False): """""" self.save_fident = save_fident self.overwrite = overwrite util.createAllPaths(save_path) if not util.isEmptyDir(save_path) and not overwrite: raise ValueError( 'Save destination is not empty and overwriting is disabled.') else: self.save_path = save_path if util.checkAllPaths(orig_img_path[0]): self.orig_img_path = orig_img_path[0] self.orig_img_fident = orig_img_path[1] else: raise ValueError('Given path for original image is not valid.') if util.checkAllPaths(orig_psf_path): self.orig_psf_path = orig_psf_path[0] self.orig_psf_fident = orig_psf_path[1] else: raise ValueError('Given path for original psf is not valid.')
def initSaveParameters(self, save_path, save_fident, overwrite=False): """""" self.save_fident = save_fident self.overwrite = overwrite util.createAllPaths(save_path) if not util.isEmptyDir(save_path) and not overwrite: raise ValueError( 'Save destination is not empty and overwriting is disabled.') else: self.save_path = save_path
def write_image_stack(img_stack, path, f_ident, n_start, meta_data = None, meta_only = False): """""" util.createAllPaths(path) if not img_stack.dtype == 'uint8': raise TypeError if meta_data: if isinstance(meta_data, MetaData): meta_data = meta_data.toList() if not isinstance(meta_data, list): raise TypeError if not meta_only: for ind in range(img_stack.shape[2]): temp_img = img_stack[:,:,ind].copy() _writeImage(temp_img, path, f_ident, ind) if meta_data: print('Writing Meta...') _writeMeta(meta_data, path, f_ident)
def create_sub_crops_overlap(): """""" f_ident = 'cyto_sim_{:0>3}.tif' in_main_path = util.ptjoin(util.SIM_DATA, 'cyto_sim_full') path_full = util.ptjoin(in_main_path, 'full_res_[0.2,0.2,0.2]') path_down = util.ptjoin(in_main_path, 'downsample_res_[0.2,0.2,0.2]') crop_diviser = 3 crop_paths_full = [ 'full_overlap_crop_{}_{}_res_[0.2,0.2,0.2]'.format( i / crop_diviser, i % crop_diviser) for i in range(crop_diviser**2) ] crop_paths_down = [ 'downsampled_overlap_crop_{}_{}_res_[0.2,0.2,0.2]'.format( i / crop_diviser, i % crop_diviser) for i in range(crop_diviser**2) ] crop_paths_full = [util.ptjoin(in_main_path, pt) for pt in crop_paths_full] crop_paths_down = [util.ptjoin(in_main_path, pt) for pt in crop_paths_down] util.createAllPaths(crop_paths_full) util.createAllPaths(crop_paths_down) full_stack = util.stack_loader.read_image_stack(path_full, f_ident, meta=False) full_meta = util.stack_loader.read_meta_data_only(path_full, f_ident) t_reduced_shape = [ sh / crop_diviser if sh % 2 == 0 else 1 + sh / crop_diviser for sh in full_stack.shape ] meta_comment = full_meta.comment for i, crop_path in enumerate(crop_paths_full): x_ind = i / crop_diviser y_ind = i % crop_diviser sh_bool2 = [sh % 2 == 0 for sh in t_reduced_shape] ind_slice = [ slice( x_ind * (t_reduced_shape[0] / 2) if sh_bool2[0] else x_ind * ((t_reduced_shape[0] - 1) / 2), (x_ind + 2) * (t_reduced_shape[0] / 2) if sh_bool2[0] else 1 + (x_ind + 2) * (t_reduced_shape[0] - 1) / 2, 1), slice( y_ind * (t_reduced_shape[1] / 2) if sh_bool2[1] else y_ind * ((t_reduced_shape[1] - 1) / 2), (y_ind + 2) * (t_reduced_shape[1] / 2) if sh_bool2[1] else 1 + (y_ind + 2) * (t_reduced_shape[1] - 1) / 2, 1), slice(None, None, 1) ] cropped_stack = full_stack[ind_slice].copy() full_meta.comment = meta_comment + '; Cropped; Crop_index=[{},{}]'.format( x_ind, y_ind) util.stack_loader.write_image_stack(cropped_stack, crop_path, f_ident, 0, meta_data=full_meta.toList()) down_stack = util.stack_loader.read_image_stack(path_down, f_ident, meta=False) down_meta = util.stack_loader.read_meta_data_only(path_down, f_ident) t_reduced_shape = [ sh / crop_diviser if sh % 2 == 0 else 1 + sh / crop_diviser for sh in down_stack.shape ] meta_comment = down_meta.comment for i, crop_path in enumerate(crop_paths_down): x_ind = i / crop_diviser y_ind = i % crop_diviser sh_bool2 = [sh % 2 == 0 for sh in t_reduced_shape] ind_slice = [ slice( x_ind * (t_reduced_shape[0] / 2) if sh_bool2[0] else x_ind * ((t_reduced_shape[0] - 1) / 2), (x_ind + 2) * (t_reduced_shape[0] / 2) if sh_bool2[0] else 1 + (x_ind + 2) * (t_reduced_shape[0] - 1) / 2, 1), slice( y_ind * (t_reduced_shape[1] / 2) if sh_bool2[1] else y_ind * ((t_reduced_shape[1] - 1) / 2), (y_ind + 2) * (t_reduced_shape[1] / 2) if sh_bool2[1] else 1 + (y_ind + 2) * (t_reduced_shape[1] - 1) / 2, 1), slice(None, None, 1) ] cropped_stack = down_stack[ind_slice].copy() down_meta.comment = meta_comment + '; Cropped; Crop_index=[{},{}]'.format( x_ind, y_ind) util.stack_loader.write_image_stack(cropped_stack, crop_path, f_ident, 0, meta_data=down_meta.toList())
def createSupCrops_forDownsample(): """""" main_path = util.ptjoin(util.SIM_DATA, 'cyto_sim_full') res_path_add = 'downsample_res_[0.2,0.2,0.2]' f_ident = 'cyto_sim_{:0>3}.tif' out_path_add = 'cyto_sim_crop_{}_{}' crop_diviser = 2 path_in = util.ptjoin(main_path, res_path_add) in_stack = util.stack_loader.read_image_stack(path_in, f_ident, meta=False) in_meta = util.stack_loader.read_meta_data_only(path_in, f_ident) red_shape = [sh / crop_diviser for sh in in_stack.shape] meta_comment = in_meta.comment for x_ind in range(crop_diviser): for y_ind in range(crop_diviser): cur_path_out = util.ptjoin(util.SIM_DATA, out_path_add.format(x_ind, y_ind), res_path_add) print('Processing path: {}').format(cur_path_out) util.createAllPaths(cur_path_out) ind_slice = [ slice(x_ind * red_shape[0], (x_ind + 1) * red_shape[0], 1), slice(y_ind * red_shape[1], (y_ind + 1) * red_shape[1], 1), slice(None, None, 1) ] cropped_stack = in_stack[ind_slice].copy() in_meta.comment = meta_comment + '; Cropped; Crop_index=[{},{}]'.format( x_ind, y_ind) util.stack_loader.write_image_stack(cropped_stack, cur_path_out, f_ident, 0, meta_data=in_meta.toList()) out_path_add = 'cyto_sim_crop_overlap_{}_{}' for x_ind in range(crop_diviser + 1): for y_ind in range(crop_diviser + 1): cur_path_out = util.ptjoin(util.SIM_DATA, out_path_add.format(x_ind, y_ind), res_path_add) print('Processing path:{}'.format(cur_path_out)) util.createAllPaths(cur_path_out) ind_slice = [ slice(x_ind * (red_shape[0] / 2), (x_ind + 2) * (red_shape[0] / 2), 1), slice(y_ind * (red_shape[1] / 2), (y_ind + 2) * (red_shape[1] / 2), 1), slice(None, None, 1) ] print(ind_slice) cropped_stack = in_stack[ind_slice].copy() in_meta.comment = meta_comment + '; Cropped; Crop_index=[{},{}]'.format( x_ind, y_ind) util.stack_loader.write_image_stack(cropped_stack, cur_path_out, f_ident, 0, meta_data=in_meta.toList())
def testFISTA(): """""" #gauss_single = [0.05, 0.15, 0.25, 0.5] gauss_single = [0.05, 0.5] gauss_multi = [ #[0.05, 0., 0.15], #[0.05, 0., 0.2], [0.05, 0., 0.25], #[0.05, 0., 0.4], [0.1, 0., 0.15], #[0.1, 0., 0.2], #[0.1, 0., 0.25], #[0.1, 0., 0.4], [0.2, 0., 0.15], #[0.2, 0., 0.2], [0.2, 0., 0.25] ] #[0.2, 0., 0.4]] defocus = [] """ defocus = [[0.3, 2000, 400], #[0.3, 2000, 200], [0.3, 2000, 100], #[0.3, 800, 400], [0.3, 800, 200], #[0.3, 800, 100], [0.1, 2000, 400], #[0.1, 2000, 200], #[0.1, 2000, 100], #[0.1, 800, 400], [0.1, 800, 200], #[0.1, 800, 100], [0.05, 2000, 400], #[0.05, 2000, 200], [0.05, 2000, 100], #[0.05, 800, 400], #[0.05, 800, 200], [0.05, 800, 100]] """ dict_paths = [] orig_path = util.ptjoin(util.SIM_DATA, 'cyto_sim_full', 'full_res_[0.4,0.4,0.4]') orig_fident = 'cyto_sim_{:0>3}.tif' for fl in gauss_single: conv_path = 'cyto_gauss_simple_sig-{:.2f}'.format(fl) conv_f_ident = 'cyto_conv_gauss_simple_{:0>3}.tif' #conv_path = util.ptjoin(util.SIM_CONVOLUTED, path, 'full_res_[0.2,0.2,0.2]') psf_path = 'gauss_simple_sig-{:.2f}'.format(fl) psf_f_ident = 'psf_gauss_simple_{:0>3}.tif' #psf_path = util.ptjoin(util.SIM_PSF, 'odd', psf_path, 'res_[0.2,0.2,0.2]') dict_paths.append({ 'conv_path': conv_path, 'conv_fident': conv_f_ident, 'psf_path': psf_path, 'psf_fident': psf_f_ident }) for lis in gauss_multi: conv_path = 'cyto_gauss_sig0-{0[0]:.2f}_sig1-{0[1]:.2f}_sig2-{0[2]:.2f}'.format( lis) conv_f_ident = 'cyto_conv_gauss_{:0>3}.tif' #conv_path = util.ptjoin(util.SIM_CONVOLUTED, path, 'full_res[0.2,0.2,0.2]') psf_path = 'gauss_sig0-{0[0]:.2f}_sig1-{0[1]:.2f}_sig2-{0[2]:.2f}'.format( lis) psf_f_ident = 'psf_gauss_{:0>3}.tif' #psf_path = util.ptjoin(util.SIM_PSF, 'odd', psf_path, 'res_[0.2,0.2,0.2]') dict_paths.append({ 'conv_path': conv_path, 'conv_fident': conv_f_ident, 'psf_path': psf_path, 'psf_fident': psf_f_ident }) for lis in defocus: conv_path = 'cyto_defocus_sig-{0[0]:.2f}_zi-{0[1]}_K-{0[2]}'.format( lis) conv_f_ident = 'cyto_conv_defocus_{:0>3}.tif' #conv_path = util.ptjoin(util.SIM_CONVOLUTED, path, 'full_res[0.2,0.2,0.2]') psf_path = 'defocussing_sig-{0[0]:.2f}_zi-{0[1]}_K-{0[2]}'.format(lis) psf_f_ident = 'psf_defocus_{:0>3}.tif' #psf_path = util.ptjoin(util.SIM_PSF, 'odd', psf_path, 'res_[0.2,0.2,0.2]') dict_paths.append({ 'conv_path': conv_path, 'conv_fident': conv_f_ident, 'psf_path': psf_path, 'psf_fident': psf_f_ident }) orig_stack, orig_meta = util.stack_loader.read_image_stack(orig_path, orig_fident, meta=True) #lambdas = [0.8, 0.85, 0.87, 0.9, 0.92, 0.94, 0.96, 0.98, 1., 1.02, 1.04, 1.06, 1.08, 1.1, 1.13, 1.15, 1.2] lambdas = [0.87, 0.94, 0.98, 1., 1.02, 1.06, 1.13] for paths in dict_paths: for lam in lambdas: conv_path = util.ptjoin(util.SIM_CONVOLUTED, paths['conv_path'], 'full_res_[0.4,0.4,0.4]') psf_path = util.ptjoin(util.SIM_PSF, 'odd', paths['psf_path'], 'res_[0.4,0.4,0.4]') psf_stack, psf_meta = util.stack_loader.read_image_stack( psf_path, paths['psf_fident'], meta=True) conv_stack, conv_meta = util.stack_loader.read_image_stack( conv_path, paths['conv_fident'], meta=True) recon_path = util.ptjoin(util.SIM_RECON, 'test_FISTA', paths['conv_path'], 'res_[0.4,0.4,0.4]') util.createAllPaths(recon_path) recon_fident = 'recon_{:0>3}.tif' fista = wave.FISTA(conv_stack, psf=psf_stack, groundTruth=orig_stack, iterSteps=200, saveIntermediateSteps=50, compareWithTruth=True, debugInt=3) fista.initSaveParameters(recon_path, recon_fident) fista.prepare() fista.solve() fista.saveSolution()
def sample_noise_additions(): """""" convs = { 'defocus1': [ 'cyto_defocus_sig-0.10_zi-1000_K-200', ['res_[0.2,0.2,0.2]'], 'cyto_conv_{:0>3}.tif', [['cnr', 30.], ['cnr', 10.], ['cnr', 5.]] ], 'defocus2': [ 'cyto_defocus_sig-0.05_zi-2000_K-200', ['res_[0.2,0.2,0.2]'], 'cyto_conv_{:0>3}.tif', [['cnr', 30.], ['cnr', 10.], ['cnr', 5.]] ], 'defocus3': [ 'cyto_defocus_sig-0.05_zi-1000_K-200', ['res_[0.2,0.2,0.2]'], 'cyto_conv_{:0>3}.tif', [['cnr', 30.], ['cnr', 10.], ['cnr', 5.]] ], 'defocus4': [ 'cyto_defocus_sig-0.05_zi-800_K-100', ['res_[0.1,0.1,0.1]', 'res_[0.2,0.2,0.2]'], 'cyto_conv_{:0>3}.tif', [['cnr', 30.], ['cnr', 10.], ['cnr', 5.]] ], 'gauss1': [ 'cyto_gauss_sig0-0.15_sig1-0.00_sig2-0.22', ['res_[0.1,0.1,0.1]', 'res_[0.2,0.2,0.2]'], 'cyto_conv_{:0>3}.tif', [['cnr', 30.], ['cnr', 10.], ['cnr', 5.]] ], 'gauss_simple1': [ 'cyto_gauss_simple_sig-0.15', ['res_[0.1,0.1,0.1]', 'res_[0.2,0.2,0.2]'], 'cyto_conv_{:0>3}.tif', [['cnr', 30.], ['cnr', 10.], ['cnr', 5.]] ], 'gauss_simple2': [ 'cyto_gauss_simple_sig-0.30', ['res_[0.1,0.1,0.1]'], 'cyto_conv_{:0>3}.tif', [['cnr', 30.], ['cnr', 10.], ['cnr', 5.]] ], } main_path = util.SIM_CONVOLUTED for name, parameters in psfs.items(): print('Processing Convoluted Image Stack: {}'.format(name)) add_path = parameters[0] res_folders = parameters[1] f_ident = parameters[2] noise_params = parameters[3] for res in res_folders: for noise in noise_params: noise_free_path = util.ptjoin(main_path, add_path, res) new_path = util.ptjoin( main_path, add_path, '{}_{}-{}'.format(res, noise[0], noise[1])) util.createAllPaths(new_path) temp_stack = util.stack_loader.read_image_stack( noise_free_path, f_ident, meta=True) t_noise_dict = { 'noise_def': convoluter.Noise_Adder.NOISE_DEF[noise[0]], noise[0]: noise[1] } temp_noise = convoluter.Noise_Adder(temp_stack[0], old_meta=temp_stack[1], img_type='sample', noise_type='gaussian', noise_params=t_noise_dict, debug_int=3) temp_noise.addNoise() temp_noise.initSaveParameters( new_path, f_ident, orig_img_path=[noise_free_path, f_ident], overwrite=True) temp_noise.saveSolution()
def PSF_noise_additions_batch(): """""" main_path = util.SIM_PSF #path_adds = ['odd', 'even'] path_adds = ['odd'] #res_list = [[0.1,0.1,0.1], [0.2,0.2,0.2]] #res_list = [[0.4,0.4,0.4]] res_list = [[0.8, 0.8, 0.8]] res_path_def = 'res_[{0[0]:.1f},{0[1]:.1f},{0[2]:.1f}]' #Defocus parameter lists sig_defocus_list = [.05, 0.1, 0.2, 0.3] zi_list = [800, 1000, 2000] K_list = [100, 200, 300, 400] defocus_noise_list = [['snr', 60.], ['snr', 30.], ['snr', 15.], ['snr', 10.], ['snr', 5.], ['snr', 2.]] f_ident_defocus = 'psf_defocus_{:0>3}.tif' path_def_defocus = 'defocussing_sig-{:.2f}_zi-{:.0f}_K-{:.0f}' for oddity in path_adds: for res in res_list: for sig in sig_defocus_list: for zi in zi_list: for K in K_list: for noise_par in defocus_noise_list: t_noise_free_path = util.ptjoin( main_path, oddity, path_def_defocus.format(sig, zi, K), res_path_def.format(res)) t_new_path = util.ptjoin( main_path, oddity, path_def_defocus.format(sig, zi, K), 'res_[{0[0]:.1f},{0[1]:.1f},{0[2]:.1f}]_{1}-{2}' .format(res, noise_par[0], noise_par[1])) util.createAllPaths(t_new_path) temp_stack = util.stack_loader.read_image_stack( t_noise_free_path, f_ident_defocus, meta=True) t_noise_dict = { 'noise_def': convoluter.Noise_Adder.NOISE_DEF[noise_par[0]], noise_par[0]: noise_par[1] } temp_noise = convoluter.Noise_Adder( temp_stack[0], old_meta=temp_stack[1], img_type='psf', noise_type='gaussian', noise_params=t_noise_dict, debug_int=3) temp_noise.addNoise() temp_noise.initSaveParameters( t_new_path, f_ident_defocus, orig_img_path=[ t_noise_free_path, f_ident_defocus ], overwrite=True) temp_noise.saveSolution() #Gaussian parameter lists sig0_list = [0.05, 0.10, 0.15, 0.2, 0.3, 0.4] sig1_list = [0] sig2_list = [0.15, 0.2, 0.25, 0.3, 0.4] gauss_noise_list = [['snr', 60.], ['snr', 30.], ['snr', 15.], ['snr', 10.], ['snr', 5.], ['snr', 2.]] f_ident_gauss = 'psf_gauss_{:0>3}.tif' path_def_gauss = 'gauss_sig0-{:.2f}_sig1-{:.2f}_sig2-{:.2f}' for oddity in path_adds: for res in res_list: for sig0 in sig0_list: for sig1 in sig1_list: for sig2 in sig2_list: for noise_par in gauss_noise_list: t_noise_free_path = util.ptjoin( main_path, oddity, path_def_gauss.format(sig0, sig1, sig2), res_path_def.format(res)) t_new_path = util.ptjoin( main_path, oddity, path_def_gauss.format(sig0, sig1, sig2), 'res_[{0[0]:.1f},{0[1]:.1f},{0[2]:.1f}]_{1}-{2}' .format(res, noise_par[0], noise_par[1])) util.createAllPaths(t_new_path) temp_stack = util.stack_loader.read_image_stack( t_noise_free_path, f_ident_gauss, meta=True) t_noise_dict = { 'noise_def': convoluter.Noise_Adder.NOISE_DEF[noise_par[0]], noise_par[0]: noise_par[1] } temp_noise = convoluter.Noise_Adder( temp_stack[0], old_meta=temp_stack[1], img_type='psf', noise_type='gaussian', noise_params=t_noise_dict, debug_int=3) temp_noise.addNoise() temp_noise.initSaveParameters( t_new_path, f_ident_gauss, orig_img_path=[ t_noise_free_path, f_ident_gauss ], overwrite=True) temp_noise.saveSolution() #Gaussian simple lists sig_gauss_simple_list = [0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.4, 0.5] gauss_simple_noise_list = [['snr', 60.], ['snr', 30.], ['snr', 15.], ['snr', 10.], ['snr', 5.], ['snr', 2.]] f_ident_gauss_simple = 'psf_gauss_simple_{:0>3}.tif' path_def_gauss_simple = 'gauss_simple_sig-{:.2f}' for oddity in path_adds: for res in res_list: for sig in sig_gauss_simple_list: for noise_par in gauss_simple_noise_list: t_noise_free_path = util.ptjoin( main_path, oddity, path_def_gauss_simple.format(sig), res_path_def.format(res)) t_new_path = util.ptjoin( main_path, oddity, path_def_gauss_simple.format(sig), 'res_[{0[0]:.1f},{0[1]:.1f},{0[2]:.1f}]_{1}-{2}'. format(res, noise_par[0], noise_par[1])) util.createAllPaths(t_new_path) temp_stack = util.stack_loader.read_image_stack( t_noise_free_path, f_ident_gauss_simple, meta=True) t_noise_dict = { 'noise_def': convoluter.Noise_Adder.NOISE_DEF[noise_par[0]], noise_par[0]: noise_par[1] } temp_noise = convoluter.Noise_Adder( temp_stack[0], old_meta=temp_stack[1], img_type='psf', noise_type='gaussian', noise_params=t_noise_dict, debug_int=3) temp_noise.addNoise() temp_noise.initSaveParameters(t_new_path, f_ident_gauss_simple, orig_img_path=[ t_noise_free_path, f_ident_gauss_simple ], overwrite=True) temp_noise.saveSolution()