def stacks_for_fiji(): BG_VAL = 244 psf_raw_path = util.ptjoin(util.PSF_DATA_CROP, 'LE01_Sample_4_100nm_aligned_256') psf_out_path = util.ptjoin(util.PSF_DATA_CROP, 'fiji', 'LE01_Sample_4_100nm_aligned_256') sub_path = 'crop_256x256_{0:0>2}_{1}' # 0: 01,02, 1: DL DR TL TR crop_indexes = range(1,8) area_indexes = ['DL','DR','TL','TR'] f_ident = 'PSF_LE01_crop_256x256_{:0>2}_Slice{:0>3}.tif' for cr_ind in crop_indexes: cur_fident = f_ident.format(cr_ind, '{:0>3}') for area in area_indexes: cur_path = util.ptjoin(psf_raw_path, sub_path.format(cr_ind, area)) cur_out_path = util.ptjoin(psf_out_path, sub_path.format(cr_ind, area)) cur_img = sl.read_image_stack(cur_path, cur_fident, 0, 160) cur_psf = np.ones(cur_img.shape, dtype=cur_img.dtype)*BG_VAL #cur_psf = np.zeros(cur_img.shape, dtype= 'float') cur_psf[:,:,79] = cur_img[:,:,79] sl.write_image_stack(cur_psf, cur_out_path, f_ident.format(cr_ind, '{:0>3}'),0)
def show_psf_animation(): psf_path = util.ptjoin(util.PSF_RECON, 'LE01_Sample_4_100nm_aligned_256') sub_path = 'crop_256x256_{0:0>2}_{1}' # 0: 01,02, 1: DL DR TL TR crop_indexes = range(1, 8) area_indexes = ['DL', 'DR', 'TL', 'TR'] f_ident = 'PSF_LE01_crop_256x256_{:0>2}_Slice{:0>3}.tif' cr_ind = 1 area = 'DL' cur_path = util.ptjoin(psf_path, sub_path.format(cr_ind, area)) cur_fident = f_ident.format(cr_ind, '{:0>3}') #cur_path = util.ptjoin(psf_path, 'crop_256x256_stat') #cur_fident = 'PSF_LE01_crop_256x256_STAT_Slice{:0>3}.tif' cur_img = sl.read_image_stack(cur_path, cur_fident, 0, 160) #cur_img[128,128,:] = cur_img.max() return util.imshow3D_ani(cur_img, cmap='gray', useArrayWideMax=False)
def analyze_recon(): recon_path = util.MICRO_RECON sub_paths = ['4082', '4083', '4084', '4085'] f_ident = 'Richardson-Lucy_{:0>4}_{:0>3}.tif' #recon_array = np.array((1350, 1600, 120)) recon_raw = [] for sub in sub_paths: cur_path = util.ptjoin(recon_path, sub) tmp_stack = sl.read_image_stack(cur_path, f_ident.format(sub, '{:0>3}'), 0, 30) print(tmp_stack.shape) recon_raw.append(tmp_stack) recon_array = np.zeros((recon_raw[0].shape[0], recon_raw[0].shape[1], 120)) recon_array[:,:,0:30] = recon_raw[0][:,:,:] recon_array[:,:,30:60] = recon_raw[1][:,:,:] recon_array[:,:,60:90] = recon_raw[2][:,:,:] recon_array[:,:,90:120] = recon_raw[3][:,:,:] util.imshow3D_slice(recon_array[256:512,256:512,:], center_coord=[90,90,80], show_slice_lines=True, cmap='gray')
def recon_from256(): psf_raw_path = util.ptjoin(util.PSF_DATA_CROP, 'LE01_Sample_4_100nm_aligned_256') psf_out_path = util.ptjoin(util.PSF_RECON, 'LE01_Sample_4_100nm_aligned_256') sub_path = 'crop_256x256_{0:0>2}_{1}' # 0: 01,02, 1: DL DR TL TR crop_indexes = range(1, 8) area_indexes = ['DL', 'DR', 'TL', 'TR'] f_ident = 'PSF_LE01_crop_256x256_{:0>2}_Slice{:0>3}.tif' for cr_ind in crop_indexes: cur_fident = f_ident.format(cr_ind, '{:0>3}') for area in area_indexes: cur_path = util.ptjoin(psf_raw_path, sub_path.format(cr_ind, area)) cur_img = sl.read_image_stack(cur_path, cur_fident, 0, 160) #cur_img = cur_img - cur_img.min() cur_img = np.array(cur_img, dtype='float') cur_psf = np.ones(cur_img.shape, dtype=cur_img.dtype) * BG_VAL #cur_psf = np.zeros(cur_img.shape, dtype= 'float') cur_psf[:, :, 79] = cur_img[:, :, 79] print('Recon with Wiener Inverse Filter.') psf_rec = decon.WienerFilter(cur_img, cur_psf, useCpxFFT=False, cutoff_noise=0.005, noiseRelative=True) psf_rec.solve() print('Normalizing') temp_img = psf_rec.out #temp_img = temp_img/temp_img.sum() psf_rec = decon.RichardsonLucy(cur_img, cur_psf, useCpxFFT=False, correctZeros=False, maxIter=600, errTol=0, p=2, normalize=False) psf_rec.curGuess = temp_img psf_rec.debug = True psf_rec.solve() sl.write_image_stack( psf_rec.out.real, util.ptjoin(psf_out_path, sub_path.format(cr_ind, area)), cur_fident, 0)
def psf_test_recon(): test_data_path = util.ptjoin(util.MICRO_DATA, 'nucleus_test') data_f_ident = 'B21_{:0>4}_z_stacks_LE01_S01_R01_Slice{:0>2}_crop.tif' recon_image_data = np.zeros((4, 512, 512, 30), dtype='uint8') #psf_path = util.ptjoin(util.PSF_RECON, 'LE01_Sample_4_100nm_aligned_256','crop_256x256_stat') #psf_ident = 'PSF_LE01_crop_256x256_STAT_Slice{:0>3}.tif' #psf_path = 'O:\\Master FZJ\\PSF_recon\\fiji\\RegInv_TikMiller' #psf_ident = 'TMiller_crop_256x256_01_TL{:0>3}.tif' psf_path = 'C:\\Users\\lukas\\Master FZJ\\PSF_recon\\Sept_2017\\fiji\\Wiener' psf_ident = 'Wiener_PSF_recon_{:0>3}.tif' psf_data = sl.read_image_stack(psf_path, psf_ident, 0, 160) compr = [4, 4, 4] base_shape = [256, 256, 160] compr_shape = [64, 64, 40] psf_data_compr = np.zeros(compr_shape, dtype='float') print('calculating compressed PSF') for z in range(compr_shape[2]): for x in range(compr_shape[0]): for y in range(compr_shape[1]): temp = 0. for indz in range(compr[2]): for indx in range(compr[0]): for indy in range(compr[1]): temp += psf_data[compr[0] * x + indx, compr[1] * y + indy, compr[2] * z + indz] psf_data_compr[x, y, z] = temp print('{} {}'.format(psf_data_compr.max(), psf_data_compr.min())) psf_data_compr = psf_data_compr - psf_data_compr.min() psf_data_compr = (psf_data_compr / psf_data_compr.max()) * 255 print('{} {}'.format(psf_data_compr.max(), psf_data_compr.min())) psf_data_out = np.zeros(compr_shape, dtype='uint8') psf_data_out[:, :, :] = psf_data_compr[:, :, :] print('{} {}'.format(psf_data_out.max(), psf_data_out.min())) sl.write_image_stack(psf_data_out, util.ptjoin(psf_path, 'compressed'), psf_ident, 0) #psf_data_compr = psf_data_compr[:,:, 5:35] #psf_data_compr = psf_data_compr/psf_data_compr.sum() return util.imshow3D_ani(psf_data_compr)
def test_abstract_decon(test_conv_path, test_conv_fident, test_psf_path, test_psf_fident): """""" test_conv = st_l.read_image_stack(test_conv_path, test_conv_fident) test_psf = st_l.read_image_stack(test_psf_path, test_psf_fident) test_dec = abs_decon.AbstractDecon(test_conv, psf=test_psf, sample=None, groundTruth=None, algoName='Abstr', solveFor='sample', constraints=None, isPsfCentered=True, useCpxFFT=False, debugInt=3) test_dec.print_dbg('Test0', 0) test_dec.print_dbg('Test1', 1) test_dec.print_dbg('Test2', 2) test_dec.print_dbg('Test3', 3)
def statistic_psf_recon(): psf_in_path = util.ptjoin(util.PSF_RECON, 'LE01_Sample_4_100nm_aligned_256') psf_out_path = util.ptjoin(util.PSF_RECON, 'LE01_Sample_4_100nm_aligned_256') sub_in_path = 'crop_256x256_{0:0>2}_{1}' # 0: 01,02, 1: DL DR TL TR sub_out_path = 'crop_256x256_stat' crop_indexes = range(1, 8) area_indexes = ['DL', 'DR', 'TL', 'TR'] f_ident = 'PSF_LE01_crop_256x256_{:0>2}_Slice{:0>3}.tif' n_stack = 0 psf_stack = np.zeros((256, 256, 160), dtype='float') for cr_ind in crop_indexes: cur_fident = f_ident.format(cr_ind, '{:0>3}') for area in area_indexes: cur_path = util.ptjoin(psf_in_path, sub_in_path.format(cr_ind, area)) tmp_stack = sl.read_image_stack(cur_path, cur_fident, 0, 160) if tmp_stack.shape == (256, 256, 160): print('Adding to psf_stack. {} {}'.format(cr_ind, area)) psf_stack += tmp_stack n_stack += 1 print('Using {} stacks for statistics...'.format(n_stack)) psf_stack = psf_stack / n_stack psf_stack = psf_stack / psf_stack.sum() sl.write_image_stack(psf_stack, util.ptjoin(psf_out_path, sub_out_path), f_ident.format('STAT', '{:0>3}'), 0)
def test_iterative(test_gr_truth_path, test_gr_truth_fident, test_conv_path, test_conv_fident, test_psf_path, test_psf_fident): """""" test_gr_truth, t_meta = st_l.read_image_stack(test_gr_truth_path, test_gr_truth_fident, meta=True) test_gr_truth = test_gr_truth[0:256, 0:256, 0:128] test_conv, t_meta = st_l.read_image_stack(test_conv_path, test_conv_fident, meta=True) test_psf, t_meta = st_l.read_image_stack(test_psf_path, test_psf_fident, meta=True) #print(test_psf) print('Ground Truth Shape: {}'.format(test_gr_truth.shape)) print('Convoluted Import Shape: {}'.format(test_conv.shape)) print('PSF shape: {}'.format(test_psf.shape)) test_after_conv = cn.Convoluter(test_gr_truth, test_psf, conv_method='cpx_fft', debug_int=3) test_after_conv.convolute() #test_dec = iterative.AbstractIterative(test_after_conv.out, psf=test_psf, sample= None, groundTruth= None, solveFor= 'sample', initialGuess= 'WienerFilter', #algoName= 'AbstractIterative', iterSteps= 1000, #errTol= 1e-5, constraints= None, isPsfCentered= True, #useCpxFFT= False, debugInt= 3, compareWithTruth= False) #test_dec = iterative.Gold(test_after_conv.out, psf=test_psf, initialGuess='WienerFilter', #experimentalMode=False, iterSteps=10, debugInt = 3) test_dec = iterative.Gold(test_after_conv.out, psf=test_psf, groundTruth=test_gr_truth, initialGuess='WienerFilter', iterSteps=10, debugInt=3, compareWithTruth=True, saveIntermediateSteps=2) #test_dec = iterative.JannsonVCittert(test_after_conv.out, psf=test_psf, initialGuess='WienerFilter', #iterSteps=10, errTol= 1e-5, gamma= 1., constraints= None, #isPsfCentered =True, useCpxFFT =False, debugInt=3, #compareWithTruth= False, saveIntermediateSteps= 0) #test_dec = iterative.Landweber(test_after_conv.out, psf=test_psf, initialGuess= 'WienerFilter', #iterSteps=10, errTol= 1e-5, gamma=1., isPsfCentered=True, useCpxFFT=False, #debugInt=3, compareWithTruth=False, saveIntermediateSteps=0) #test_dec = iterative.RichardsonLucy(test_after_conv.out, psf=test_psf, sample =None, groundTruth =None, solveFor ='sample', initialGuess ='orig-array', #iterSteps =10, errTol =1e-5, p =1., constraints =None, #isPsfCentered =True, useCpxFFT =False, debugInt =3, compareWithTruth= False, saveIntermediateSteps= 0) #test_dec = iterative.StarkParker(test_after_conv.out, psf=test_psf, sample= None, groundTruth= None, solveFor= 'sample', initialGuess= 'WienerFilter', #iterSteps=10, errTol= 1e-5, gamma= 1.0, vmin= -1, vmax= -1, #constraints= None, isPsfCentered= True, useCpxFFT= False, debugInt= 3, #compareWithTruth= False, saveIntermediateSteps= 0) #test_dec = iterative.TikhonovMiller(test_after_conv.out, psf=test_psf, sample= None, groundTruth= None, solveFor= 'sample', initialGuess= 'WienerFilter', #iterSteps=10, errTol= 1e-5, gamma= 1., lamb= 1., #constraints= None, isPsfCentered= True, useCpxFFT= False, #debugInt= 3, compareWithTruth= False, saveIntermediateSteps= 0) #test_dec = iterative.ICTMi (test_after_conv.out, ps Int= 3, compareWithTruth= False, saveIntermediateSteps= 0) #test_dec.initSaveParameters(save_path, save_fident, intermediate_paths=['inter[IND]', None], None], None], None], None]) save_path = util.ptjoin(util.SIM_RECON, 'test_decon_iter', 'Gold') f_ident = 'recon_wiener_{:0>3}.tif' util.path_declarations.createAllPaths(save_path) test_dec.initSaveParameters( save_path, f_ident, intermediate_path='inter_[IND]', orig_img_path=[test_conv_path, test_conv_fident], orig_psf_path=[test_psf_path, test_psf_fident], orig_sample_path=[None, None], orig_truth_path=[test_gr_truth_path, test_gr_truth_fident], overwrite=False) test_dec.prepare() test_dec.solve() test_dec.saveSolution()
def test_single_step(test_gr_truth_path, test_gr_truth_fident, test_conv_path, test_conv_fident, test_psf_path, test_psf_fident): """""" test_gr_truth, t_meta = st_l.read_image_stack(test_gr_truth_path, test_gr_truth_fident, meta=True) test_gr_truth = test_gr_truth[0:256, 0:256, 0:128] test_conv, t_meta = st_l.read_image_stack(test_conv_path, test_conv_fident, meta=True) test_psf, t_meta = st_l.read_image_stack(test_psf_path, test_psf_fident, meta=True) #print(test_psf) print('Ground Truth Shape: {}'.format(test_gr_truth.shape)) print('Convoluted Import Shape: {}'.format(test_conv.shape)) print('PSF shape: {}'.format(test_psf.shape)) test_after_conv = cn.Convoluter(test_gr_truth, test_psf, conv_method='cpx_fft', debug_int=3) test_after_conv.convolute() #test_dec = sing_st.InverseFilter(test_after_conv.out, groundTruth= test_gr_truth, psf=test_psf, solveFor= 'sample', isPsfCentered= True, cutoff= 1e-3, #relativeCutoff= True, cutoffInFourierDomain= True, constraints= None, #useCpxFFT= False, debugInt=3, compareWithTruth= True) #test_dec = sing_st.WienerFilter(test_after_conv.out, groundTruth= test_gr_truth, psf=test_psf, noise = 1e-2, #relativeNoise= True, constraints= None, useCpxFFT= False, debugInt= 3, #compareWithTruth= True) test_dec = sing_st.RegularizedTikhonov(test_after_conv.out, psf=test_psf, sample=None, groundTruth=None, solveFor='sample', isPsfCentered=True, lam=1e-3, tolerance=1e-6, constraints=None, useCpxFFT=False, debugInt=3, compareWithTruth=False) test_dec.prepare() test_dec.solve() save_path = util.ptjoin(util.SIM_RECON, 'test_decon_single', 'Tikhonov') f_ident = 'recon_wiener_{:0>3}.tif' util.path_declarations.createAllPaths(save_path) test_dec.initSaveParameters( save_path, f_ident, orig_img_path=[test_conv_path, test_conv_fident], orig_psf_path=[test_psf_path, test_psf_fident], orig_sample_path=[None, None], orig_truth_path=[test_gr_truth_path, test_gr_truth_fident], overwrite=False) test_dec.saveSolution() #conv = np.pad(test_after_conv.out, [[8,8],[8,8],[8,8]], mode='constant', constant_values = 0) #conv = test_after_conv.out.copy() #conv = test_after_conv.out.copy() #psf = np.pad(test_psf, [[120,120],[120,120],[56,56]], mode='constant', constant_values = 0) #psf = np.pad(test_psf, [[0,240],[0,240],[0,112]], mode='constant', constant_values = 0) #psf = np.pad(test_psf, [[128,128],[128,128],[64,64]], mode='constant', constant_values = 0) #psf = np.fft.ifftshift(psf) #fig, ani = util.visu_util.imshow3D_ani(psf) #plt.show() #f_conv = np.fft.fftshift(np.fft.fftn(conv)) #f_psf = np.fft.fftshift(np.fft.fftn(np.fft.ifftshift(psf))) #f_psf = np.fft.fftshift(np.fft.fftn(np.fft.ifftshift(psf))) #mag = f_psf*f_psf.conj() #mag[mag < 1e-6] = 1e-6 #f_recon = f_recon / mag #recon = np.fft.ifftn(np.fft.ifftshift(f_recon)).real #fig, ani = util.visu_util.imshow3D_ani(recon[8:256+9,8:256+9,8:128+9]) #fig, ani = util.visu_util.imshow3D_ani(test_dec.out) plt.show()