def preprocess_data(prj, flat, dark, FF_norm=flat_field_norm, remove_rings=remove_rings, FF_drift_corr=flat_field_drift_corr, downsapling=binning): if FF_norm: # dark-flat field correction prj = tomopy.normalize(prj, flat, dark) if FF_drift_corr: # flat field drift correction prj = tomopy.normalize_bg(prj, air=50) prj[prj <= 0] = 1 # check dark<data prj = tomopy.minus_log(prj) # -logarithm if remove_rings: # remove rings prj = tomopy.remove_stripe_fw(prj, level=7, wname='sym16', sigma=1, pad=True) #prj = tomopy.remove_stripe_ti(prj,2) # prj = tomopy.remove_all_stripe(prj) if downsapling > 0: # binning prj = tomopy.downsample(prj, level=binning) prj = tomopy.downsample(prj, level=binning, axis=1) return prj
def reconstruct_sirt(fname, sino_range, theta_st=0, theta_end=PI, n_epochs=200, output_folder=None, downsample=None, center=None): if output_folder is None: output_folder = 'sirt_niter_{}_ds_{}_{}_{}'.format(n_epochs, *downsample) t0 = time.time() print('Reading data...') prj, flt, drk, _ = dxchange.read_aps_32id(fname, sino=sino_range) print('Data reading: {} s'.format(time.time() - t0)) print('Data shape: {}'.format(prj.shape)) prj = tomopy.normalize(prj, flt, drk) prj = preprocess(prj) # scale up to prevent precision issue prj *= 1.e2 if downsample is not None: prj = tomopy.downsample(prj, level=downsample[0], axis=0) prj = tomopy.downsample(prj, level=downsample[1], axis=1) prj = tomopy.downsample(prj, level=downsample[2], axis=2) print('Downsampled shape: {}'.format(prj.shape)) n_theta = prj.shape[0] theta = np.linspace(theta_st, theta_end, n_theta) print('Starting reconstruction...') t0 = time.time() extra_options = {'MinConstraint': 0} options = {'proj_type': 'cuda', 'method': 'SIRT_CUDA', 'num_iter': n_epochs, 'extra_options': extra_options} res = tomopy.recon(prj, theta, center=center, algorithm=tomopy.astra, options=options) dxchange.write_tiff_stack(res, fname=os.path.join(output_folder, 'recon'), dtype='float32', overwrite=True) print('Reconstruction time: {} s'.format(time.time() - t0))
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'): sample_detector_distance = 30 # Propagation distance of the wavefront in cm detector_pixel_size_x = 1.17e-4 # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4) monochromator_energy = 25.74 # Energy of incident wave in keV alpha = 1e-02 # Phase retrieval coeff. zinger_level = 1000 # Zinger level for projections zinger_level_w = 1000 # Zinger level for white miss_angles = [141,226] # Read APS 32-BM raw data. proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino) print (theta) # Manage the missing angles: #proj_size = np.shape(proj) #theta = np.linspace(0,180,proj_size[0]) proj = np.concatenate((proj[0:miss_angles[0],:,:], proj[miss_angles[1]+1:-1,:,:]), axis=0) theta = np.concatenate((theta[0:miss_angles[0]], theta[miss_angles[1]+1:-1])) # zinger_removal #proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0) #flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0) # Flat-field correction of raw data. data = tomopy.normalize(proj, flat, dark, cutoff=0.8) # remove stripes data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True) # phase retrieval # data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True) print("Raw data: ", h5fname) print("Center: ", rot_center) data = tomopy.minus_log(data) data = tomopy.remove_nan(data, val=0.0) data = tomopy.remove_neg(data, val=0.00) data[np.where(data == np.inf)] = 0.00 rot_center = rot_center/np.power(2, float(binning)) data = tomopy.downsample(data, level=binning) data = tomopy.downsample(data, level=binning, axis=1) # Reconstruct object. if algorithm == 'sirtfbp': rec = rec_sirtfbp(data, theta, rot_center) else: rec = tomopy.recon(data, theta, center=rot_center, algorithm=algorithm, filter_name='parzen') print("Algorithm: ", algorithm) # Mask each reconstructed slice with a circle. ##rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) return rec
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'): sample_detector_distance = 3 # Propagation distance of the wavefront in cm detector_pixel_size_x = 1.17e-4 # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4) monochromator_energy = 22.7 # Energy of incident wave in keV alpha = 1e-02 # Phase retrieval coeff. zinger_level = 800 # Zinger level for projections zinger_level_w = 1000 # Zinger level for white # Read APS 32-BM raw data. proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino) # zinger_removal proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0) flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0) # Flat-field correction of raw data. ##data = tomopy.normalize(proj, flat, dark, cutoff=0.8) data = tomopy.normalize(proj, flat, dark) # remove stripes ## data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True) ##data = tomopy.remove_stripe_ti(data, alpha=1.5) data = tomopy.remove_stripe_sf(data, size=150) # phase retrieval ##data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True) print("Raw data: ", h5fname) print("Center: ", rot_center) data = tomopy.minus_log(data) data = tomopy.remove_nan(data, val=0.0) data = tomopy.remove_neg(data, val=0.00) data[np.where(data == np.inf)] = 0.00 rot_center = rot_center / np.power(2, float(binning)) data = tomopy.downsample(data, level=binning) data = tomopy.downsample(data, level=binning, axis=1) # Reconstruct object. if algorithm == 'sirtfbp': rec = rec_sirtfbp(data, theta, rot_center) else: rec = tomopy.recon(data, theta, center=rot_center, algorithm=algorithm, filter_name='parzen') print("Algorithm: ", algorithm) # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) return rec
def preprocess_data(prj, flat, dark, FF_norm=flat_field_norm, remove_rings = remove_rings, medfilt_size=medfilt_size, FF_drift_corr=flat_field_drift_corr, downspling=binning): if FF_norm: # normalize the prj print('\n*** Applying flat field correction:') start_norm_time = time.time() prj = tomopy.normalize(prj, flat, dark) print(' done in %0.3f min' % ((time.time() - start_norm_time)/60)) if FF_drift_corr: print('\n*** Applying flat field drift correction:') start_norm_bg_time = time.time() prj = tomopy.normalize_bg(prj, air=100) print(' done in %0.3f min' % ((time.time() - start_norm_bg_time)/60)) # Applying -log print('\n*** Applying -log:') start_log_time = time.time() prj = tomopy.minus_log(prj) print(' done in %0.3f min' % ((time.time() - start_log_time)/60)) prj = tomopy.misc.corr.remove_neg(prj, val=0.000) prj = tomopy.misc.corr.remove_nan(prj, val=0.000) prj[np.where(prj == np.inf)] = 0.000 # prj[np.where(prj == 0)] = 0.000,2 print('\n*** Min and max val in prj before recon: %0.3f, %0.3f' % (np.min(prj), np.max(prj))) if remove_rings: # remove ring artefacts tmp = prj[-1,:,:] # use to fixe the bug of remove_stripe_ti print('\n*** Applying ring removal algo:') start_ring_time = time.time() # prj = tomopy.remove_stripe_ti(prj, nblock=0, alpha=2) prj = tomopy.remove_stripe_fw(prj) # prj = tomopy.remove_stripe_sf(prj,10); prj = tomopy.misc.corr.remove_neg(prj, val=0.000) # remove the neg values coming from remove_stripe_sf print(' done in %0.3f min' % ((time.time() - start_ring_time)/60)) prj[-1,:,:] = tmp # fixe the bug of remove_stripe_ti # Filtering data with 2D median filter before downsampling and recon if medfilt_size>1: start_filter_time = time.time() print('\n*** Applying median filter') #prj = tomopy.median_filter(prj,size=1) prj = ndimage.median_filter(prj,footprint=np.ones((1, medfilt_size, medfilt_size))) print(' done in %0.3f min' % ((time.time() - start_filter_time)/60)) # Downsampling data: if downspling>0: print('\n** Applying downsampling') start_down_time = time.time() prj = tomopy.downsample(prj, level=binning) prj = tomopy.downsample(prj, level=binning, axis=1) print(' done in %0.3f min' % ((time.time() - start_down_time)/60)) print('\n*** Shape of the data:'+str(np.shape(prj))) print(' Dimension of theta:'+str(np.shape(theta))) return prj
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'): sample_detector_distance = 8 # Propagation distance of the wavefront in cm detector_pixel_size_x = 2.247e-4 # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4) monochromator_energy = 24.9 # Energy of incident wave in keV alpha = 1e-02 # Phase retrieval coeff. zinger_level = 800 # Zinger level for projections zinger_level_w = 1000 # Zinger level for white # Read APS 32-BM raw data. proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino) # zinger_removal # proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0) # flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0) # Flat-field correction of raw data. ##data = tomopy.normalize(proj, flat, dark, cutoff=0.8) data = tomopy.normalize(proj, flat, dark) # remove stripes data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True) # data = tomopy.remove_stripe_ti(data, alpha=1.5) # data = tomopy.remove_stripe_sf(data, size=150) # phase retrieval #data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True) print("Raw data: ", h5fname) print("Center: ", rot_center) data = tomopy.minus_log(data) data = tomopy.remove_nan(data, val=0.0) data = tomopy.remove_neg(data, val=0.00) data[np.where(data == np.inf)] = 0.00 rot_center = rot_center/np.power(2, float(binning)) data = tomopy.downsample(data, level=binning) data = tomopy.downsample(data, level=binning, axis=1) # Reconstruct object. if algorithm == 'sirtfbp': rec = rec_sirtfbp(data, theta, rot_center) elif algorithm == 'astrasirt': extra_options ={'MinConstraint':0} options = {'proj_type':'cuda', 'method':'SIRT_CUDA', 'num_iter':200, 'extra_options':extra_options} rec = tomopy.recon(data, theta, center=rot_center, algorithm=tomopy.astra, options=options) else: rec = tomopy.recon(data, theta, center=rot_center, algorithm=algorithm, filter_name='parzen') print("Algorithm: ", algorithm) # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) return rec
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'): sample_detector_distance = 8 # Propagation distance of the wavefront in cm detector_pixel_size_x = 2.247e-4 # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4) monochromator_energy = 24.9 # Energy of incident wave in keV alpha = 1e-02 # Phase retrieval coeff. zinger_level = 800 # Zinger level for projections zinger_level_w = 1000 # Zinger level for white # h5fname_norm = '/local/data/2019-02/Burke/C47M_0015.h5' h5fname_norm = '/local/data/2019-02/Burke/kc78_Menardii_0003.h5' proj1, flat, dark, theta1 = dxchange.read_aps_32id(h5fname_norm, sino=sino) proj, dummy, dummy1, theta = dxchange.read_aps_32id(h5fname, sino=sino) # zinger_removal proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0) flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0) # Flat-field correction of raw data. ##data = tomopy.normalize(proj, flat, dark, cutoff=0.8) data = tomopy.normalize(proj, flat, dark) # remove stripes data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True) #data = tomopy.remove_stripe_ti(data, alpha=1.5) data = tomopy.remove_stripe_sf(data, size=20) # phase retrieval #data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True) print("Raw data: ", h5fname) print("Center: ", rot_center) data = tomopy.minus_log(data) data = tomopy.remove_nan(data, val=0.0) data = tomopy.remove_neg(data, val=0.00) data[np.where(data == np.inf)] = 0.00 rot_center = rot_center/np.power(2, float(binning)) data = tomopy.downsample(data, level=binning) data = tomopy.downsample(data, level=binning, axis=1) # Reconstruct object. if algorithm == 'sirtfbp': rec = rec_sirtfbp(data, theta, rot_center) else: rec = tomopy.recon(data, theta, center=rot_center, algorithm=algorithm, filter_name='parzen') print("Algorithm: ", algorithm) # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) return rec
def downsample_img(img, ds, axis=0): if isinstance(ds, int): if img.ndim == 3: res = tomopy.downsample(img, level=int(np.log2(ds)), axis=0) else: res = tomopy.downsample(img[:, np.newaxis, :], level=int(np.log2(ds)), axis=0) else: zm = np.ones(img.ndim) zm[axis] = 1. / ds res = zoom(img, zm) return res
def find_center_vo(tomo, ind=None, smin=-50, smax=50, srad=6, step=0.5, ratio=0.5, drop=20): """ Transplanted from TomoPy with minor fixes. Find rotation axis location using Nghia Vo's method. :cite:`Vo:14`. Parameters ---------- tomo : ndarray 3D tomographic data. ind : int, optional Index of the slice to be used for reconstruction. smin, smax : int, optional Coarse search radius. Reference to the horizontal center of the sinogram. srad : float, optional Fine search radius. step : float, optional Step of fine searching. ratio : float, optional The ratio between the FOV of the camera and the size of object. It's used to generate the mask. drop : int, optional Drop lines around vertical center of the mask. Returns ------- float Rotation axis location. """ tomo = dtype.as_float32(tomo) if ind is None: ind = tomo.shape[1] // 2 _tomo = tomo[:, ind, :] # Enable cache for FFTW. pyfftw.interfaces.cache.enable() # Reduce noise by smooth filters. Use different filters for coarse and fine search _tomo_cs = ndimage.filters.gaussian_filter(_tomo, (3, 1)) _tomo_fs = ndimage.filters.median_filter(_tomo, (2, 2)) # Coarse and fine searches for finding the rotation center. if _tomo.shape[0] * _tomo.shape[1] > 4e6: # If data is large (>2kx2k) _tomo_coarse = downsample(np.expand_dims(_tomo_cs, 1), level=2)[:, 0, :] init_cen = _search_coarse(_tomo_coarse, smin / 4, smax / 4, ratio, drop) fine_cen = _search_fine(_tomo_fs, srad, step, init_cen * 4, ratio, drop) else: init_cen = _search_coarse(_tomo_cs, smin, smax, ratio, drop) fine_cen = _search_fine(_tomo_fs, srad, step, init_cen, ratio, drop) logger.debug('Rotation center search finished: %i', fine_cen) return fine_cen
def main(arg): fname = '/local/dataraid/elettra/Oak_16bit_slice343_all_repack.h5' # Read the hdf raw data. sino, sflat, sdark, th = dxchange.read_aps_32id(fname) slider(sino) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(sino.shape[1], ang1=0.0, ang2=180.0) print(sino.shape, sdark.shape, sflat.shape, theta.shape) # Quick normalization just to see something .... ndata = sino / float(np.amax(sino)) slider(ndata) # Find rotation center. rot_center = 962 binning = 1 ndata = tomopy.downsample(ndata, level=int(binning)) rot_center = rot_center/np.power(2, float(binning)) ndata = tomopy.minus_log(ndata) # Reconstruct object using Gridrec algorithm. rec = tomopy.recon(ndata, theta, center=rot_center, sinogram_order=True, algorithm='gridrec') # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) # Write data as stack of TIFs. dxchange.write_tiff_stack(rec, fname='recon_dir/recon')
def main(arg): parser = argparse.ArgumentParser() parser.add_argument("top", help="top directory where the tiff images are located: /data/") parser.add_argument("start", nargs='?', const=1, type=int, default=1, help="index of the first image: 1000 (default 1)") args = parser.parse_args() top = args.top index_start = int(args.start) template = os.listdir(top)[0] nfile = len(fnmatch.filter(os.listdir(top), '*.tif')) index_end = index_start + nfile ind_tomo = range(index_start, index_end) fname = top + template print (nfile, index_start, index_end, fname) # Select the sinogram range to reconstruct. start = 0 end = 512 sino=(start, end) # Read the tiff raw data. ndata = dxchange.read_tiff_stack(fname, ind=ind_tomo, slc=(sino, None)) # Normalize to 1 using the air counts ndata = tomopy.normalize_bg(ndata, air=5) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(ndata.shape[0]) ndata = tomopy.minus_log(ndata) # Set binning and number of iterations binning = 8 iters = 21 print("Original", ndata.shape) ndata = tomopy.downsample(ndata, level=binning, axis=1) # ndata = tomopy.downsample(ndata, level=binning, axis=2) print("Processing:", ndata.shape) fdir = 'aligned' + '/noblur_iter_' + str(iters) + '_bin_' + str(binning) print(fdir) cprj, sx, sy, conv = alignment.align_seq(ndata, theta, fdir=fdir, iters=iters, pad=(10, 10), blur=False, save=True, debug=True) np.save(fdir + '/shift_x', sx) np.save(fdir + '/shift_y', sy) # Write aligned projections as stack of TIFs. dxchange.write_tiff_stack(cprj, fname=fdir + '/radios/image')
def file_io(): ########################################################################################################## ## fdir = '/local/dataraid/' fdir = '/local/data/2018-04/Dubacq/' file_name_Im1 = 'tomo_manip7G-sc_7124eV_1200prj_354.h5' file_name_Im2 = 'tomo_manip7G-sc_7200eV_1200prj_352.h5' prj = 0 binning = 1 medfilt_size = 3 crop = [200, 300] ########################################################################################################## # Reading Images to align: Im1, flat1, dark1, theta = dxchange.read_aps_32id(fdir + file_name_Im1, proj=(prj, prj + 1, 1)) Im2, flat2, dark2, theta = dxchange.read_aps_32id(fdir + file_name_Im2, proj=(prj, prj + 1, 1)) Im1 = Im1 / np.mean(flat1, axis=0) Im2 = Im2 / np.mean(flat2, axis=0) if medfilt_size > 0: Im1 = ndimage.median_filter(Im1, footprint=np.ones( (1, medfilt_size, medfilt_size))) Im2 = ndimage.median_filter(Im2, footprint=np.ones( (1, medfilt_size, medfilt_size))) if binning > 0: Im1 = tomopy.downsample(Im1, level=binning) Im1 = tomopy.downsample(Im1, level=binning, axis=1) Im2 = tomopy.downsample(Im2, level=binning) Im2 = tomopy.downsample(Im2, level=binning, axis=1) # if 1: # plt.figure(), # plt.subplot(1,2,1), plt.imshow(np.squeeze(Im1), cmap='gray', aspect="auto", interpolation='none'), plt.colorbar() # plt.subplot(1,2,2), plt.imshow(np.squeeze(Im2), cmap='gray', aspect="auto", interpolation='none'), plt.colorbar() # plt.show() return Im1, Im2
def main(arg): parser = argparse.ArgumentParser() parser.add_argument("top", help="top directory where the tiff images are located: /data/") parser.add_argument("start", nargs='?', const=1, type=int, default=1, help="index of the first image: 1000 (default 1)") args = parser.parse_args() top = args.top index_start = int(args.start) template = os.listdir(top)[0] nfile = len(fnmatch.filter(os.listdir(top), '*.tif')) index_end = index_start + nfile ind_tomo = range(index_start, index_end) fname = top + template print (nfile, index_start, index_end, fname) # Select the sinogram range to reconstruct. start = 0 end = 512 sino=(start, end) # Read the tiff raw data. ndata = dxchange.read_tiff_stack(fname, ind=ind_tomo, slc=(sino, None)) print(ndata.shape) binning = 8 ndata = tomopy.downsample(ndata, level=binning, axis=1) print(ndata.shape) # Normalize to 1 using the air counts ndata = tomopy.normalize_bg(ndata, air=5) ## slider(ndata) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(ndata.shape[0]) rot_center = 960 print("Center of rotation: ", rot_center) ndata = tomopy.minus_log(ndata) # Reconstruct object using Gridrec algorithm. rec = tomopy.recon(ndata, theta, center=rot_center, algorithm='gridrec') # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) # Write data as stack of TIFs. dxchange.write_tiff_stack(rec, fname='/local/dataraid/mark/rec/recon')
def find_center_vo(tomo, ind=None, smin=-50, smax=50, srad=6, step=0.5, ratio=0.5, drop=20): """ Transplanted from TomoPy with minor fixes. Find rotation axis location using Nghia Vo's method. :cite:`Vo:14`. Parameters ---------- tomo : ndarray 3D tomographic data. ind : int, optional Index of the slice to be used for reconstruction. smin, smax : int, optional Coarse search radius. Reference to the horizontal center of the sinogram. srad : float, optional Fine search radius. step : float, optional Step of fine searching. ratio : float, optional The ratio between the FOV of the camera and the size of object. It's used to generate the mask. drop : int, optional Drop lines around vertical center of the mask. Returns ------- float Rotation axis location. """ tomo = dtype.as_float32(tomo) if ind is None: ind = tomo.shape[1] // 2 _tomo = tomo[:, ind, :] # Enable cache for FFTW. pyfftw.interfaces.cache.enable() # Reduce noise by smooth filters. Use different filters for coarse and fine search _tomo_cs = ndimage.filters.gaussian_filter(_tomo, (3, 1)) _tomo_fs = ndimage.filters.median_filter(_tomo, (2, 2)) # Coarse and fine searches for finding the rotation center. if _tomo.shape[0] * _tomo.shape[1] > 4e6: # If data is large (>2kx2k) _tomo_coarse = downsample(np.expand_dims(_tomo_cs,1), level=2)[:, 0, :] init_cen = _search_coarse(_tomo_coarse, smin/4, smax/4, ratio, drop) fine_cen = _search_fine(_tomo_fs, srad, step, init_cen*4, ratio, drop) else: init_cen = _search_coarse(_tomo_cs, smin, smax, ratio, drop) fine_cen = _search_fine(_tomo_fs, srad, step, init_cen, ratio, drop) # logger.debug('Rotation center search finished: %i', fine_cen) return fine_cen
def main(arg): fname = '/local/dataraid/elettra/Oak_16bit_slice343_all_repack.h5' # Read the hdf raw data. sino, sflat, sdark, th = dxchange.read_aps_32id(fname) slider(sino) proj = np.swapaxes(sino,0,1) flat = np.swapaxes(sflat,0,1) dark = np.swapaxes(sdark,0,1) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(proj.shape[0], ang1=0.0, ang2=180.0) print(proj.shape, dark.shape, flat.shape, theta.shape) # Flat-field correction of raw data. ndata = tomopy.normalize(proj, flat, dark) #slider(ndata) # Find rotation center. rot_center = 962 binning = 1 ndata = tomopy.downsample(ndata, level=int(binning)) rot_center = rot_center/np.power(2, float(binning)) ndata = tomopy.minus_log(ndata) # Reconstruct object using Gridrec algorithm. rec = tomopy.recon(ndata, theta, center=rot_center, algorithm='gridrec') # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) # Write data as stack of TIFs. dxchange.write_tiff_stack(rec, fname='recon_dir/recon')
def main(arg): fname = '/local/dataraid/elettra/Oak_16bit_slice343_all_repack.h5' # Read the hdf raw data. sino, sflat, sdark, th = dxchange.read_aps_32id(fname) slider(sino) proj = np.swapaxes(sino, 0, 1) flat = np.swapaxes(sflat, 0, 1) dark = np.swapaxes(sdark, 0, 1) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(proj.shape[0], ang1=0.0, ang2=180.0) print(proj.shape, dark.shape, flat.shape, theta.shape) # Flat-field correction of raw data. ndata = tomopy.normalize(proj, flat, dark) #slider(ndata) # Find rotation center. rot_center = 962 binning = 1 ndata = tomopy.downsample(ndata, level=int(binning)) rot_center = rot_center / np.power(2, float(binning)) ndata = tomopy.minus_log(ndata) # Reconstruct object using Gridrec algorithm. rec = tomopy.recon(ndata, theta, center=rot_center, algorithm='gridrec') # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) # Write data as stack of TIFs. dxchange.write_tiff_stack(rec, fname='recon_dir/recon')
def prepare_slice(grid, shift_grid, grid_lines, slice_in_tile, ds_level=0, method='max', blend_options=None, pad=None, rot_center=None, assert_width=None, sino_blur=None, color_correction=False, normalize=True, mode='180', phase_retrieval=None, **kwargs): sinos = [None] * grid.shape[1] for col in range(grid.shape[1]): try: sinos[col] = load_sino(grid[grid_lines[col], col], slice_in_tile[col], normalize=normalize) except: pass t = time.time() row_sino = register_recon(grid, grid_lines, shift_grid, sinos, method=method, blend_options=blend_options, color_correction=color_correction, assert_width=assert_width) if not pad is None: row_sino, rot_center = pad_sino(row_sino, pad, rot_center) print('stitch: ' + str(time.time() - t)) print('final size: ' + str(row_sino.shape)) t = time.time() row_sino = tomopy.downsample(row_sino, level=ds_level) print('downsample: ' + str(time.time() - t)) print('new shape : ' + str(row_sino.shape)) # t = time.time() # row_sino = tomopy.remove_stripe_fw(row_sino, 2) # print('strip removal: ' + str(time.time() - t)) # Minus Log row_sino = tomosaic.util.preprocess(row_sino) if sino_blur is not None: row_sino[:, 0, :] = gaussian_filter(row_sino[:, 0, :], sino_blur) if mode == '360': overlap = 2 * (row_sino.shape[2] - rot_center) row_sino = tomosaic.morph.sino_360_to_180(row_sino, overlap=overlap, rotation='right') if phase_retrieval: row_sino = tomopy.retrieve_phase(row_sino, kwargs['pixel_size'], kwargs['dist'], kwargs['energy'], kwargs['alpha']) return row_sino, rot_center
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'): sample_detector_distance = 8 # Propagation distance of the wavefront in cm detector_pixel_size_x = 2.247e-4 # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4) monochromator_energy = 24.9 # Energy of incident wave in keV alpha = 1e-02 # Phase retrieval coeff. zinger_level = 800 # Zinger level for projections zinger_level_w = 1000 # Zinger level for white # Read APS 32-BM raw data. proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino) # zinger_removal # proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0) # flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0) # Flat-field correction of raw data. ##data = tomopy.normalize(proj, flat, dark, cutoff=0.8) data = tomopy.normalize(proj, flat, dark) # remove stripes #data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True) #data = tomopy.remove_stripe_ti(data, alpha=1.5) #data = tomopy.remove_stripe_sf(data, size=150) # phase retrieval #data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True) print("Raw data: ", h5fname) print("Center: ", rot_center) data = tomopy.minus_log(data) data = tomopy.remove_nan(data, val=0.0) data = tomopy.remove_neg(data, val=0.00) data[np.where(data == np.inf)] = 0.00 rot_center = rot_center/np.power(2, float(binning)) data = tomopy.downsample(data, level=binning) data = tomopy.downsample(data, level=binning, axis=1) # padding N = data.shape[2] data_pad = np.zeros([data.shape[0],data.shape[1],3*N//2],dtype = "float32") data_pad[:,:,N//4:5*N//4] = data data_pad[:,:,0:N//4] = np.tile(np.reshape(data[:,:,0],[data.shape[0],data.shape[1],1]),(1,1,N//4)) data_pad[:,:,5*N//4:] = np.tile(np.reshape(data[:,:,-1],[data.shape[0],data.shape[1],1]),(1,1,N//4)) data = data_pad rot_center = rot_center+N//4 # Reconstruct object. if algorithm == 'sirtfbp': rec = rec_sirtfbp(data, theta, rot_center) else: rec = tomopy.recon(data, theta, center=rot_center, algorithm=algorithm, filter_name='parzen') rec = rec[:,N//4:5*N//4,N//4:5*N//4] print("Algorithm: ", algorithm) # Mask each reconstructed slice with a circle. # rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) return rec
#if remove_stripe1: d.stripe_removal(level=stripe_lvl, sigma=sig, wname=Wname) if remove_stripe1: data = tomopy.remove_stripe_fw(data, level=stripe_lvl, wname=Wname, sigma=sig) # z = 3 # eng = 31 # pxl = 0.325e-4 # rat = 5e-03 # rat = 1e-03 #d.phase_retrieval(dist=z, energy=eng, pixel_size=pxl, alpha=rat,padding=True) #data = tomopy.retrieve_phase(data, dist=z, energy=eng, pixel_size=pxl, alpha=rat,pad=True) #if remove_stripe2: d.stripe_removal2() if remove_stripe2: data = tomopy.remove_stripe_ti(data) #d.downsample2d(level=level) # apply binning on the data data = tomopy.downsample(data, level=level) # apply binning on the data theta = tomopy.angles(data.shape[0]) if 1: #if not best_center: d.optimize_center() if not best_center: calc_center = tomopy.find_center(data, theta, emission=False, ind=0, tol=0.3) else: #d.center=best_center/pow(2,level) # Manage the rotation center calc_center = best_center/pow(2,level) # Manage the rotation center #d.gridrec(ringWidth=RingW) # Run the reconstruction rec = tomopy.recon(data, theta, center=calc_center, algorithm='gridrec', emission=False) #d.apply_mask(ratio=1) rec = tomopy.circ_mask(rec, axis=0) # Write data as stack of TIFs. #tomopy.xtomo_writer(d.data_recon, output_name,
def write_center_360(file_name, output_path, slice_no, center_st, center_end, medfilt_size=1, level=0, debug=1): try: prj0, flat, dark, theta = dxchange.read_aps_32id(file_name, sino=(slice_no, slice_no + 1)) except: prj0, flat, dark = dxchange.read_aps_32id(file_name, sino=(slice_no, slice_no + 1)) f = h5py.File(file_name, "r") theta = tomopy.angles(f['exchange/data'].shape[0], ang1=0, ang2=360) if debug: print('## Debug: after reading data:') print('\n** Shape of the data:' + str(np.shape(prj0))) print('** Shape of theta:' + str(np.shape(theta))) print('\n** Min and max val in prj before recon: %0.5f, %0.3f' % (np.min(prj0), np.max(prj0))) prj0 = tomopy.normalize(prj0, flat, dark) print('\n** Flat field correction done!') for center in range(center_st, center_end): overlap = 2 * (f['exchange/data'].shape[2] - center) prj = np.copy(prj0) prj = sino_360_to_180(prj, overlap=overlap, rotation='right') print('\n** Sinogram converted!') if debug: print('## Debug: after normalization:') print('\n** Min and max val in prj before recon: %0.5f, %0.3f' % (np.min(prj), np.max(prj))) prj = tomopy.minus_log(prj) print('\n** minus log applied!') if debug: print('## Debug: after minus log:') print('\n** Min and max val in prj before recon: %0.5f, %0.3f' % (np.min(prj), np.max(prj))) prj = tomopy.misc.corr.remove_neg(prj, val=0.001) prj = tomopy.misc.corr.remove_nan(prj, val=0.001) prj[np.where(prj == np.inf)] = 0.001 if debug: print('## Debug: after cleaning bad values:') print('\n** Min and max val in prj before recon: %0.5f, %0.3f' % (np.min(prj), np.max(prj))) prj = tomopy.remove_stripe_ti(prj, 4) print('\n** Stripe removal done!') if debug: print('## Debug: after remove_stripe:') print('\n** Min and max val in prj before recon: %0.5f, %0.3f' % (np.min(prj), np.max(prj))) prj = tomopy.median_filter(prj, size=medfilt_size) print('\n** Median filter done!') if debug: print('## Debug: after nedian filter:') print('\n** Min and max val in prj before recon: %0.5f, %0.3f' % (np.min(prj), np.max(prj))) if level > 0: prj = tomopy.downsample(prj, level=level) print('\n** Down sampling done!\n') if debug: print('## Debug: after down sampling:') print('\n** Min and max val in prj before recon: %0.5f, %0.3f' % (np.min(prj), np.max(prj))) rec = tomopy.recon(prj, theta, center=center, algorithm='gridrec') print('\nReconstruction done!\n') dxchange.write_tiff(rec, fname=os.path.join(output_path, '{0:.2f}'.format(center)), dtype='float32')
def _binning(data, params): data = tomopy.downsample(data, level=int(params.binning), axis=2) data = tomopy.downsample(data, level=int(params.binning), axis=1) return data
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'): sample_detector_distance = 25 # Propagation distance of the wavefront in cm detector_pixel_size_x = 2.143e-4 # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4) #monochromator_energy = 24.9 # Energy of incident wave in keV # used pink beam alpha = 1e-02 # Phase retrieval coeff. zinger_level = 800 # Zinger level for projections zinger_level_w = 1000 # Zinger level for white # Read APS 2-BM raw data. # DIMAX saves 3 files: proj, flat, dark # when loading the data set select the proj file (larger size) fname = os.path.splitext(h5fname)[0] fbase = fname.rsplit('_', 1)[0] fnum = fname.rsplit('_', 1)[1] fext = os.path.splitext(h5fname)[1] fnum_flat = str("%4.4d" % (int(fnum) + 1)) fnum_dark = str("%4.4d" % (int(fnum) + 2)) fnproj = fbase + '_' + fnum + fext fnflat = fbase + '_' + fnum_flat + fext fndark = fbase + '_' + fnum_dark + fext fnflat = '/local/data/2018-11/Chawla/1G_A/1G_A_0002.hdf' fndark = '/local/data/2018-11/Chawla/1G_A/1G_A_0003.hdf' print('proj', fnproj) print('flat', fnflat) print('dark', fndark) # Read APS 2-BM DIMAX raw data. proj, dum, dum2, theta = dxchange.read_aps_32id(fnproj, sino=sino) dum3, flat, dum4, dum5 = dxchange.read_aps_32id(fnflat, sino=sino) dum6, dum7, dark, dum8 = dxchange.read_aps_32id(fndark, sino=sino) # Flat-field correction of raw data. data = tomopy.normalize(proj, flat, dark, cutoff=1.4) # remove stripes data = tomopy.remove_stripe_fw(data, level=7, wname='sym16', sigma=1, pad=True) # zinger_removal proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0) flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0) # Flat-field correction of raw data. ##data = tomopy.normalize(proj, flat, dark, cutoff=0.8) data = tomopy.normalize(proj, flat, dark) # remove stripes #data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True) #data = tomopy.remove_stripe_ti(data, alpha=1.5) data = tomopy.remove_stripe_sf(data, size=150) # phase retrieval #data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True) print("Raw data: ", h5fname) print("Center: ", rot_center) data = tomopy.minus_log(data) data = tomopy.remove_nan(data, val=0.0) data = tomopy.remove_neg(data, val=0.00) data[np.where(data == np.inf)] = 0.00 rot_center = rot_center / np.power(2, float(binning)) data = tomopy.downsample(data, level=binning) data = tomopy.downsample(data, level=binning, axis=1) # Reconstruct object. if algorithm == 'sirtfbp': rec = rec_sirtfbp(data, theta, rot_center) else: rec = tomopy.recon(data, theta, center=rot_center, algorithm=algorithm, filter_name='parzen') print("Algorithm: ", algorithm) # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) #rec = np.swapaxes(rec,0,2) return rec
def tomo(params): fname = str(params.input_file_path) start = params.slice_start end = params.slice_end # Read raw data. if (params.full_reconstruction == False): end = start + 1 # LOG.info('Slice start/end: %s', end) proj, flat, dark, theta = dxchange.read_aps_32id(fname, sino=(start, end)) LOG.info('Slice start/end: %s, %s', start, end) LOG.info('Data successfully imported: %s', fname) LOG.info('Projections: %s', proj.shape) LOG.info('Flat: %s', flat.shape) LOG.info('Dark: %s', dark.shape) # Flat-field correction of raw data. data = tomopy.normalize(proj, flat, dark) LOG.info('Normalization completed') data = tomopy.downsample(data, level=int(params.binning)) LOG.info('Binning: %s', params.binning) # remove stripes data = tomopy.remove_stripe_fw(data, level=5, wname='sym16', sigma=1, pad=True) LOG.info('Ring removal completed') # phase retrieval #data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=8e-3,pad=True) # Find rotation center #rot_center = tomopy.find_center(proj, theta, init=290, ind=0, tol=0.5) # Set rotation center. rot_center = params.center / np.power(2, float(params.binning)) LOG.info('Rotation center: %s', rot_center) data = tomopy.minus_log(data) LOG.info('Minus log compled') # Reconstruct object using Gridrec algorithm. LOG.info('Reconstruction started using %s', params.reconstruction_algorithm) if (str(params.reconstruction_algorithm) == 'sirt'): LOG.info('Iteration: %s', params.iteration_count) rec = tomopy.recon(data, theta, center=rot_center, algorithm='sirt', num_iter=params.iteration_count) else: LOG.info('Filter: %s', params.filter) rec = tomopy.recon(data, theta, center=rot_center, algorithm='gridrec', filter_name=params.filter) LOG.info('Reconstrion of %s completed', rec.shape) # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) if (params.dry_run == False): # Write data as stack of TIFs. fname = str(params.output_path) + 'reco' dxchange.write_tiff_stack(rec, fname=fname, overwrite=True) LOG.info('Reconstrcution saved: %s', fname) if (params.full_reconstruction == False): return rec
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'): sample_detector_distance = 31 # Propagation distance of the wavefront in cm detector_pixel_size_x = 1.17e-4 # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4) monochromator_energy = 65 # Energy of incident wave in keV # used pink beam alpha = 1e-5 * 2**4 # Phase retrieval coeff. zinger_level = 800 # Zinger level for projections zinger_level_w = 1000 # Zinger level for white # Read APS 2-BM raw data. # DIMAX saves 3 files: proj, flat, dark # when loading the data set select the proj file (larger size) fname = os.path.splitext(h5fname)[0] fbase = fname.rsplit('_', 1)[0] fnum = fname.rsplit('_', 1)[1] fext = os.path.splitext(h5fname)[1] fnum_flat = str("%4.4d" % (int(fnum) + 1)) fnum_dark = str("%4.4d" % (int(fnum) + 2)) fnproj = fbase + '_' + fnum + fext fnflat = fbase + '_' + fnum_flat + fext fndark = fbase + '_' + fnum_dark + fext print('proj', fnproj) print('flat', fnflat) print('dark', fndark) # Read APS 2-BM DIMAX raw data. proj, dum, dum2, theta = dxchange.read_aps_32id(fnproj, sino=sino) dum3, flat, dum4, dum5 = dxchange.read_aps_32id(fnflat, sino=sino) #flat, dum3, dum4, dum5 = dxchange.read_aps_32id(fnflat, sino=sino) dum6, dum7, dark, dum8 = dxchange.read_aps_32id(fndark, sino=sino) # Flat-field correction of raw data. data = tomopy.normalize(proj, flat, dark, cutoff=1.4) # remove stripes data = tomopy.remove_stripe_fw(data, level=7, wname='sym16', sigma=1, pad=True) # zinger_removal proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0) flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0) # Flat-field correction of raw data. ##data = tomopy.normalize(proj, flat, dark, cutoff=0.8) data = tomopy.normalize(proj, flat, dark) # remove stripes #data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True) #data = tomopy.remove_stripe_ti(data, alpha=1.5) data = tomopy.remove_stripe_sf(data, size=150) # phase retrieval data = tomopy.prep.phase.retrieve_phase(data, pixel_size=detector_pixel_size_x, dist=sample_detector_distance, energy=monochromator_energy, alpha=alpha, pad=True) print("Raw data: ", h5fname) print("Center: ", rot_center) data = tomopy.minus_log(data) data = tomopy.remove_nan(data, val=0.0) data = tomopy.remove_neg(data, val=0.00) data[np.where(data == np.inf)] = 0.00 rot_center = rot_center / np.power(2, float(binning)) data = tomopy.downsample(data, level=binning) data = tomopy.downsample(data, level=binning, axis=1) # padding N = data.shape[2] data_pad = np.zeros([data.shape[0], data.shape[1], 3 * N // 2], dtype="float32") data_pad[:, :, N // 4:5 * N // 4] = data data_pad[:, :, 0:N // 4] = np.tile( np.reshape(data[:, :, 0], [data.shape[0], data.shape[1], 1]), (1, 1, N // 4)) data_pad[:, :, 5 * N // 4:] = np.tile( np.reshape(data[:, :, -1], [data.shape[0], data.shape[1], 1]), (1, 1, N // 4)) data = data_pad rot_center = rot_center + N // 4 nframes = 1 nproj = 1500 theta = np.linspace(0, np.pi * nframes, nproj * nframes, endpoint=False) rec = np.zeros((nframes, data.shape[1], data.shape[2], data.shape[2]), dtype='float32') for time_frame in range(0, nframes): rec0 = tomopy.recon(data[time_frame * nproj:(time_frame + 1) * nproj], theta[time_frame * nproj:(time_frame + 1) * nproj], center=rot_center, algorithm='gridrec') # Mask each reconstructed slice with a circle. rec[time_frame] = tomopy.circ_mask(rec0, axis=0, ratio=0.95) rec = rec[:, :, N // 4:5 * N // 4, N // 4:5 * N // 4] print("Algorithm: ", algorithm) return rec
file_name = sys.argv[1] ntheta = 900 sino_start = 0 sino_end = 800 ptheta = 100 # chunk size for reading binning = 0 for k in range(int(np.ceil(ntheta/ptheta))): print(k) prj, flat, dark, theta = dxchange.read_aps_32id( file_name, sino=(sino_start, sino_end), proj=(ptheta*k,min(ntheta,ptheta*(k+1)))) prj = tomopy.normalize(prj, flat, dark) prj[prj <= 0] = 1 prj = tomopy.minus_log(prj) # prj = tomopy.remove_stripe_fw( # prj, level=7, wname='sym16', sigma=1, pad=True) # prj = tomopy.remove_stripe_ti(prj,2) prj = tomopy.downsample(prj, level=binning) prj = tomopy.downsample(prj, level=binning, axis=1) # save data dxchange.write_tiff_stack(prj, f'{file_name[:-3]}/data/d', start=ptheta*k, overwrite=True) # save theta np.save(file_name[:-3]+'/data/theta',theta) print(theta)
def prepare_slice(grid, shift_grid, grid_lines, slice_in_tile, ds_level=0, method='max', blend_options=None, pad=None, rot_center=None, assert_width=None, sino_blur=None, color_correction=False, normalize=True, mode='180', phase_retrieval=None, data_format='aps_32id', **kwargs): sinos = [None] * grid.shape[1] t = time.time() for col in range(grid.shape[1]): if os.path.exists(grid[grid_lines[col], col]): sinos[col] = load_sino(grid[grid_lines[col], col], slice_in_tile[col], normalize=normalize, data_format=data_format) else: pass internal_print('reading: ' + str(time.time() - t)) t = time.time() row_sino = register_recon(grid, grid_lines, shift_grid, sinos, method=method, blend_options=blend_options, color_correction=color_correction, assert_width=assert_width) if not pad is None: row_sino, rot_center = pad_sino(row_sino, pad, rot_center) internal_print('stitch: ' + str(time.time() - t)) internal_print('final size: ' + str(row_sino.shape)) t = time.time() row_sino = tomopy.downsample(row_sino, level=ds_level) internal_print('downsample: ' + str(time.time() - t)) internal_print('new shape : ' + str(row_sino.shape)) # t = time.time() # row_sino = tomopy.remove_stripe_fw(row_sino, 2) # print('strip removal: ' + str(time.time() - t)) # Minus Log row_sino = tomosaic.util.preprocess(row_sino) if sino_blur is not None: row_sino[:, 0, :] = gaussian_filter(row_sino[:, 0, :], sino_blur) if mode == '360': overlap = 2 * (row_sino.shape[2] - rot_center) row_sino = tomosaic.sino_360_to_180(row_sino, overlap=overlap, rotation='right') if phase_retrieval: row_sino = tomopy.retrieve_phase(row_sino, kwargs['pixel_size'], kwargs['dist'], kwargs['energy'], kwargs['alpha']) return row_sino, rot_center
binning = 1 medfilt_size = 1 nProj = 4841 #################################################################################################################### if 0: #prj, flat, dark, theta = dxchange.read_aps_32id(file_name, proj=(nProj, nProj+1)) prj, flat, dark, theta = dxchange.read_aps_32id(file_name, proj=(0, nProj+1, 1210)) # open proj from 0 to 720 deg with 180deg step --> 5 proj prj = tomopy.normalize(prj, flat, dark) prj = tomopy.misc.corr.remove_neg(prj, val=0.000) prj = tomopy.misc.corr.remove_nan(prj, val=0.000) prj[np.where(prj == np.inf)] = 0.000 prj[np.where(prj > 1.0)] = 1 prj = tomopy.downsample(prj, level=binning) prj = tomopy.downsample(prj, level=binning, axis=1) prj = ndimage.median_filter(prj,footprint=np.ones((1, medfilt_size, medfilt_size))) #prj = np.squeeze(prj); avg = np.mean(prj); std = np.std(prj) #plt.imshow(prj, cmap='gray', aspect="auto", interpolation='none', vmin=avg-3*std, vmax=avg+3*std), plt.colorbar(), plt.show() ##plt.imshow(prj, cmap='gray', aspect="auto", interpolation='none', vmin=0, vmax=0.1), plt.colorbar(), plt.show() dxchange.write_tiff(prj, fname='/local/dataraid/2018-06/DeAndrade/2018-06-19/brain_petrapoxy/tmp/data', dtype='float32', overwrite=False) sino_st = 750 sino_end = 1250 if 0: prj, flat, dark, theta = dxchange.read_aps_32id(file_name, sino=(sino_st,sino_end,1), proj=(0, 1210, 1)) # open proj from 0 to 720 deg with 180deg step --> 5 proj prj = tomopy.normalize(prj, flat, dark)
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'): sample_detector_distance = 31 # Propagation distance of the wavefront in cm detector_pixel_size_x = 1.17e-4 # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4) monochromator_energy = 65 # Energy of incident wave in keV # used pink beam alpha = 4*1e-4 # Phase retrieval coeff. zinger_level = 800 # Zinger level for projections zinger_level_w = 1000 # Zinger level for white # Read APS 2-BM raw data. # DIMAX saves 3 files: proj, flat, dark # when loading the data set select the proj file (larger size) fname = os.path.splitext(h5fname)[0] fbase = fname.rsplit('_', 1)[0] fnum = fname.rsplit('_', 1)[1] fext = os.path.splitext(h5fname)[1] fnum_flat = str("%4.4d" % (int(fnum)+1)) fnum_dark = str("%4.4d" % (int(fnum)+2)) fnproj = fbase + '_' + fnum + fext fnflat = fbase + '_' + fnum_flat + fext fndark = fbase + '_' + fnum_dark + fext print('proj', fnproj) print('flat', fnflat) print('dark', fndark) # Read APS 2-BM DIMAX raw data. proj, dum, dum2, theta = dxchange.read_aps_32id(fnproj, sino=sino) dum3, flat, dum4, dum5 = dxchange.read_aps_32id(fnflat, sino=sino) #flat, dum3, dum4, dum5 = dxchange.read_aps_32id(fnflat, sino=sino) dum6, dum7, dark, dum8 = dxchange.read_aps_32id(fndark, sino=sino) # Flat-field correction of raw data. data = tomopy.normalize(proj, flat, dark, cutoff=1.4) # remove stripes data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True) # zinger_removal proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0) flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0) # Flat-field correction of raw data. ##data = tomopy.normalize(proj, flat, dark, cutoff=0.8) data = tomopy.normalize(proj, flat, dark) # remove stripes #data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True) #data = tomopy.remove_stripe_ti(data, alpha=1.5) data = tomopy.remove_stripe_sf(data, size=150) # phase retrieval #data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True) print("Raw data: ", h5fname) print("Center: ", rot_center) data = tomopy.minus_log(data) data = tomopy.remove_nan(data, val=0.0) data = tomopy.remove_neg(data, val=0.00) data[np.where(data == np.inf)] = 0.00 rot_center = rot_center/np.power(2, float(binning)) data = tomopy.downsample(data, level=binning) data = tomopy.downsample(data, level=binning, axis=1) # padding N = data.shape[2] data_pad = np.zeros([data.shape[0],data.shape[1],3*N//2],dtype = "float32") data_pad[:,:,N//4:5*N//4] = data data_pad[:,:,0:N//4] = np.tile(np.reshape(data[:,:,0],[data.shape[0],data.shape[1],1]),(1,1,N//4)) data_pad[:,:,5*N//4:] = np.tile(np.reshape(data[:,:,-1],[data.shape[0],data.shape[1],1]),(1,1,N//4)) data = data_pad rot_center = rot_center+N//4 nframes = 8 nproj = 1500 theta = np.linspace(0, np.pi*nframes, nproj*nframes, endpoint=False) rec = np.zeros( (nframes, data.shape[1], data.shape[2], data.shape[2]), dtype='float32') for time_frame in range(0, nframes): rec0 = tomopy.recon(data[time_frame*nproj:(time_frame+1)*nproj], theta[time_frame*nproj:( time_frame+1)*nproj], center=rot_center, algorithm='gridrec') # Mask each reconstructed slice with a circle. rec[time_frame] = tomopy.circ_mask(rec0, axis=0, ratio=0.95) rec = rec[:,:,N//4:5*N//4,N//4:5*N//4] print("Algorithm: ", algorithm) return rec
def image_downsample(img, ds): temp = downsample(downsample(img, level=int(np.log2(ds)), axis=1), level=int(np.log2(ds)), axis=2) return temp
# Flat-field correction of raw data. ndata = tomopy.normalize(proj, flat, dark) ndata = tomopy.remove_stripe_ti(ndata) ndata = tomopy.remove_stripe_sf(ndata) # phase retrieval # ndata = tomopy.prep.phase.retrieve_phase(ndata, pixel_size=detector_pixel_size_x, dist=sample_detector_distance, energy=monochromator_energy, alpha=8e-3, pad=True) # Find rotation center. #rot_center = tomopy.find_center(ndata, theta, init=1024, ind=0, tol=0.5) rot_center = 576 binning = 0 ndata = tomopy.downsample(ndata, level=int(binning)) rot_center = rot_center / np.power(2, float(binning)) ndata = tomopy.minus_log(ndata) rec_method = None #rec_method = 'sirf-fbp' if rec_method == 'sirf-fbp': # Reconstruct object using sirt-fbp algorithm. # Use test_sirtfbp_iter = True to test which number of iterations is suitable for your dataset # Filters are saved in .mat files in "./¨ test_sirtfbp_iter = True if test_sirtfbp_iter: nCol = ndata.shape[2] output_name = './test_iter/'
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec', options=None, num_iter=100, dark_file=None): sample_detector_distance = 10 # Propagation distance of the wavefront in cm detector_pixel_size_x = 2.247e-4 # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4) monochromator_energy = 35 # Energy of incident wave in keV alpha = 1e-01 # Phase retrieval coeff. zinger_level = 500 # Zinger level for projections zinger_level_w = 1000 # Zinger level for white # Read APS 32-BM raw data. proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino) if dark_file is not None: proj_, flat, dark, theta_ = dxchange.read_aps_32id(dark_file, sino=sino) del proj_, theta_ # zinger_removal proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0) # flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0) # Flat-field correction of raw data. ##data = tomopy.normalize(proj, flat, dark, cutoff=0.8) data = tomopy.normalize(proj, flat, dark) # remove stripes data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True) data = tomopy.remove_stripe_ti(data, alpha=1.5) data = tomopy.remove_stripe_sf(data, size=150) # phase retrieval #data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True) print("Raw data: ", h5fname) print("Center: ", rot_center) data = tomopy.minus_log(data) data = tomopy.remove_nan(data, val=0.0) data = tomopy.remove_neg(data, val=0.00) data[np.where(data == np.inf)] = 0.00 rot_center = rot_center/np.power(2, float(binning)) data = tomopy.downsample(data, level=binning) data = tomopy.downsample(data, level=binning, axis=1) print(algorithm) # Reconstruct object. if algorithm == 'sirtfbp': rec = rec_sirtfbp(data, theta, rot_center) elif algorithm == "astra_fbp": if options == 'linear': options = {'proj_type':'linear', 'method':'FBP'} else: options = {'proj_type':'cuda', 'method':'FBP_CUDA'} rec = tomopy.recon(data, theta, center=rot_center, algorithm=tomopy.astra, options=options, ncore=1) elif algorithm == "astra_sirt": extra_options = {'MinConstraint':0} options = {'proj_type':'cuda', 'method':'SIRT_CUDA', 'num_iter':num_iter, 'extra_options':extra_options} rec = tomopy.recon(data, theta, center=rot_center, algorithm=tomopy.astra, options=options) elif algorithm == tomopy.astra: rec = tomopy.recon(data, theta, center=rot_center, algorithm=tomopy.astra, options=options) else: try: rec = tomopy.recon(data, theta, center=rot_center, algorithm=algorithm, filter_name='parzen') except: rec = tomopy.recon(data, theta, center=rot_center, algorithm=algorithm) print("Algorithm: ", algorithm) # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) return rec
def image_downsample(img, ds): temp = downsample(downsample(img, level=ds-1, axis=1), level=ds-1, axis=2) return temp
def evaluate(self): self.tomo.value = tomopy.downsample( self.tomo.value, level=int(self.level.value), axis=int(self.axis.value))
def main(arg): parser = argparse.ArgumentParser() parser.add_argument( "top", help="top directory where the tiff images are located: /data/") parser.add_argument("start", nargs='?', const=1, type=int, default=1, help="index of the first image: 1000 (default 1)") args = parser.parse_args() top = args.top index_start = int(args.start) template = os.listdir(top)[0] nfile = len(fnmatch.filter(os.listdir(top), '*.tif')) index_end = index_start + nfile ind_tomo = range(index_start, index_end) fname = top + template print(nfile, index_start, index_end, fname) # Select the sinogram range to reconstruct. start = 0 end = 512 sino = (start, end) # Read the tiff raw data. ndata = dxchange.read_tiff_stack(fname, ind=ind_tomo, slc=(sino, None)) print(ndata.shape) binning = 8 ndata = tomopy.downsample(ndata, level=binning, axis=1) print(ndata.shape) # Normalize to 1 using the air counts ndata = tomopy.normalize_bg(ndata, air=5) ## slider(ndata) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(ndata.shape[0]) rot_center = 960 print("Center of rotation: ", rot_center) ndata = tomopy.minus_log(ndata) # Reconstruct object using Gridrec algorithm. rec = tomopy.recon(ndata, theta, center=rot_center, algorithm='gridrec') # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) # Write data as stack of TIFs. dxchange.write_tiff_stack(rec, fname='/local/dataraid/mark/rec/recon')
# Flat-field correction of raw data. ndata = tomopy.normalize(proj, flat, dark) ndata = tomopy.remove_stripe_ti(ndata) ndata = tomopy.remove_stripe_sf(ndata) # phase retrieval # ndata = tomopy.prep.phase.retrieve_phase(ndata, pixel_size=detector_pixel_size_x, dist=sample_detector_distance, energy=monochromator_energy, alpha=8e-3, pad=True) # Find rotation center. #rot_center = tomopy.find_center(ndata, theta, init=1024, ind=0, tol=0.5) rot_center = 576 binning = 0 ndata = tomopy.downsample(ndata, level=int(binning)) rot_center = rot_center/np.power(2, float(binning)) ndata = tomopy.minus_log(ndata) rec_method = None #rec_method = 'sirf-fbp' if rec_method == 'sirf-fbp': # Reconstruct object using sirt-fbp algorithm. # Use test_sirtfbp_iter = True to test which number of iterations is suitable for your dataset # Filters are saved in .mat files in "./¨ test_sirtfbp_iter = True if test_sirtfbp_iter: nCol = ndata.shape[2] output_name = './test_iter/'
def main(arg): parser = argparse.ArgumentParser() parser.add_argument( "top", help="top directory where the tiff images are located: /data/") parser.add_argument("start", nargs='?', const=1, type=int, default=1, help="index of the first image: 1000 (default 1)") args = parser.parse_args() top = args.top index_start = int(args.start) template = os.listdir(top)[0] nfile = len(fnmatch.filter(os.listdir(top), '*.tif')) index_end = index_start + nfile ind_tomo = range(index_start, index_end) fname = top + template print(nfile, index_start, index_end, fname) # Select the sinogram range to reconstruct. start = 0 end = 512 sino = (start, end) # Read the tiff raw data. ndata = dxchange.read_tiff_stack(fname, ind=ind_tomo, slc=(sino, None)) # Normalize to 1 using the air counts ndata = tomopy.normalize_bg(ndata, air=5) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(ndata.shape[0]) ndata = tomopy.minus_log(ndata) # Set binning and number of iterations binning = 8 iters = 21 print("Original", ndata.shape) ndata = tomopy.downsample(ndata, level=binning, axis=1) # ndata = tomopy.downsample(ndata, level=binning, axis=2) print("Processing:", ndata.shape) fdir = 'aligned' + '/noblur_iter_' + str(iters) + '_bin_' + str(binning) print(fdir) cprj, sx, sy, conv = alignment.align_seq(ndata, theta, fdir=fdir, iters=iters, pad=(10, 10), blur=False, save=True, debug=True) np.save(fdir + '/shift_x', sx) np.save(fdir + '/shift_y', sy) # Write aligned projections as stack of TIFs. dxchange.write_tiff_stack(cprj, fname=fdir + '/radios/image')
h5fname = "/home/beams/VNIKITIN/tomobank_rec/dk_MCFG_1_p_s1_.h5" sino = (1300, 1316) # slices for reconstructions nframes = 8 # time frames for reconstruction frame = 94 # middle time frame for reconstruction nproj = 300 # number of angles for 180 degrees interval binning = 2 proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino) proj = proj[(frame-nframes//2)*nproj:(frame+nframes//2)*nproj, :, :] # Flat-field correction of raw data. data = tomopy.normalize(proj, flat, dark, cutoff=1.4) # remove stripes data = tomopy.remove_stripe_fw( data, level=7, wname='sym16', sigma=1, pad=True) # log fitler data = tomopy.minus_log(data) data = tomopy.remove_nan(data, val=0.0) data = tomopy.remove_neg(data, val=0.00) data[np.where(data == np.inf)] = 0.00 # Binning data = tomopy.downsample(data, level=binning, axis=2) if data.shape[1] > 1: data = tomopy.downsample(data, level=binning, axis=1) # reshape for 4d data = np.reshape(data,[nframes,nproj,data.shape[1],data.shape[2]]) np.save('data.npy',data)
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'): sample_detector_distance = 30 # Propagation distance of the wavefront in cm detector_pixel_size_x = 1.17e-4 # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4) monochromator_energy = 25.74 # Energy of incident wave in keV alpha = 1e-02 # Phase retrieval coeff. zinger_level = 1000 # Zinger level for projections zinger_level_w = 1000 # Zinger level for white miss_angles = [500, 1050] # Read APS 32-BM raw data. proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino) print(theta) # Manage the missing angles: #proj_size = np.shape(proj) #theta = np.linspace(0,180,proj_size[0]) print(proj.shape, theta.shape) proj = np.concatenate( (proj[0:miss_angles[0], :, :], proj[miss_angles[1] + 1:-1, :, :]), axis=0) theta = np.concatenate( (theta[0:miss_angles[0]], theta[miss_angles[1] + 1:-1])) print(proj.shape, theta.shape) # zinger_removal #proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0) #flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0) # Flat-field correction of raw data. data = tomopy.normalize(proj, flat, dark, cutoff=0.8) # remove stripes # data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True) data = tomopy.remove_stripe_ti(data, 2) # phase retrieval # data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True) print("Raw data: ", h5fname) print("Center: ", rot_center) data = tomopy.minus_log(data) data = tomopy.remove_nan(data, val=0.0) data = tomopy.remove_neg(data, val=0.00) data[np.where(data == np.inf)] = 0.00 rot_center = rot_center / np.power(2, float(binning)) data = tomopy.downsample(data, level=binning) data = tomopy.downsample(data, level=binning, axis=1) # Reconstruct object. if algorithm == 'sirtfbp': rec = rec_sirtfbp(data, theta, rot_center) else: rec = tomopy.recon(data, theta, center=rot_center, algorithm=algorithm, filter_name='parzen') print("Algorithm: ", algorithm) # Mask each reconstructed slice with a circle. ##rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) return rec
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'): sample_detector_distance = 25 # Propagation distance of the wavefront in cm detector_pixel_size_x = 2.143e-4 # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4) #monochromator_energy = 24.9 # Energy of incident wave in keV # used pink beam alpha = 1e-02 # Phase retrieval coeff. zinger_level = 800 # Zinger level for projections zinger_level_w = 1000 # Zinger level for white # Read APS 2-BM raw data. # DIMAX saves 3 files: proj, flat, dark # when loading the data set select the proj file (larger size) fname = os.path.splitext(h5fname)[0] fbase = fname.rsplit('_', 1)[0] fnum = fname.rsplit('_', 1)[1] fext = os.path.splitext(h5fname)[1] fnum_flat = str("%4.4d" % (int(fnum)+1)) fnum_dark = str("%4.4d" % (int(fnum)+2)) fnproj = fbase + '_' + fnum + fext fnflat = fbase + '_' + fnum_flat + fext fndark = fbase + '_' + fnum_dark + fext fnflat = '/local/data/2018-11/Chawla/1G_A/1G_A_0002.hdf' fndark = '/local/data/2018-11/Chawla/1G_A/1G_A_0003.hdf' print('proj', fnproj) print('flat', fnflat) print('dark', fndark) # Read APS 2-BM DIMAX raw data. proj, dum, dum2, theta = dxchange.read_aps_32id(fnproj, sino=sino) dum3, flat, dum4, dum5 = dxchange.read_aps_32id(fnflat, sino=sino) dum6, dum7, dark, dum8 = dxchange.read_aps_32id(fndark, sino=sino) # Flat-field correction of raw data. data = tomopy.normalize(proj, flat, dark, cutoff=1.4) # remove stripes data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True) # zinger_removal proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0) flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0) # Flat-field correction of raw data. ##data = tomopy.normalize(proj, flat, dark, cutoff=0.8) data = tomopy.normalize(proj, flat, dark) # remove stripes #data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True) #data = tomopy.remove_stripe_ti(data, alpha=1.5) data = tomopy.remove_stripe_sf(data, size=150) # phase retrieval #data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True) print("Raw data: ", h5fname) print("Center: ", rot_center) data = tomopy.minus_log(data) data = tomopy.remove_nan(data, val=0.0) data = tomopy.remove_neg(data, val=0.00) data[np.where(data == np.inf)] = 0.00 rot_center = rot_center/np.power(2, float(binning)) data = tomopy.downsample(data, level=binning) data = tomopy.downsample(data, level=binning, axis=1) # Reconstruct object. if algorithm == 'sirtfbp': rec = rec_sirtfbp(data, theta, rot_center) else: rec = tomopy.recon(data, theta, center=rot_center, algorithm=algorithm, filter_name='parzen') print("Algorithm: ", algorithm) # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) #rec = np.swapaxes(rec,0,2) return rec
print('## Debug: after remove_stripe:') print('\n** Min and max val in prj before recon: %0.5f, %0.3f' % (np.min(prj), np.max(prj))) # phase retrieval #data = tomopy.prep.phase.retrieve_phase(prj,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True) prj = tomopy.median_filter(prj,size=medfilt_size) print('\n** Median filter done!') if debug: print('## Debug: after nedian filter:') print('\n** Min and max val in prj before recon: %0.5f, %0.3f' % (np.min(prj), np.max(prj))) if level>0: prj = tomopy.downsample(prj, level=level) print('\n** Down sampling done!\n') if debug: print('## Debug: after down sampling:') print('\n** Min and max val in prj before recon: %0.5f, %0.3f' % (np.min(prj), np.max(prj))) if auto_center == False: tomopy.write_center(prj,theta,dpath=output_path,cen_range=[Center_st/pow(2,level),Center_end/pow(2,level),((Center_end - Center_st)/float(N_recon))/pow(2,level)]) else: rot_axis = tomopy.find_center_vo(prj) rot_axis = rot_axis * pow(2,level) print('*** Rotation center: %0.2f' % rot_axis) rec = tomopy.recon(prj, theta, center=rot_axis/pow(2,level), algorithm='gridrec', filter_name='parzen') rec=np.squeeze(rec)
def preprocess_data(prj, flat, dark, FF_norm=flat_field_norm, remove_rings = remove_rings, medfilt_size=medfilt_size, FF_drift_corr=flat_field_drift_corr, downspling=binning): if FF_norm: # normalize the prj print('\n*** Applying flat field correction:') start_norm_time = time.time() prj = tomopy.normalize(prj, flat, dark) print(' done in %0.3f min' % ((time.time() - start_norm_time)/60)) if FF_drift_corr: print('\n*** Applying flat field drift correction:') start_norm_bg_time = time.time() prj = tomopy.normalize_bg(prj, air=100) print(' done in %0.3f min' % ((time.time() - start_norm_bg_time)/60)) # Applying -log print('\n*** Applying -log:') start_log_time = time.time() prj = tomopy.minus_log(prj) print(' done in %0.3f min' % ((time.time() - start_log_time)/60)) prj = tomopy.misc.corr.remove_neg(prj, val=0.000) prj = tomopy.misc.corr.remove_nan(prj, val=0.000) prj[np.where(prj == np.inf)] = 0.000 # prj[np.where(prj == 0)] = 0.000 print('\n*** Min and max val in prj before recon: %0.3f, %0.3f' % (np.min(prj), np.max(prj))) if remove_rings: # remove ring artefacts tmp = prj[-1,:,:] # use to fixe the bug of remove_stripe_ti print('\n*** Applying ring removal algo:') start_ring_time = time.time() prj = tomopy.remove_stripe_ti(prj,2) # prj = tomopy.remove_stripe_sf(prj,10); prj = tomopy.misc.corr.remove_neg(prj, val=0.000) # remove the neg values coming from remove_stripe_sf print(' done in %0.3f min' % ((time.time() - start_ring_time)/60)) prj[-1,:,:] = tmp # fixe the bug of remove_stripe_ti if phase_retrieval: # phase retrieval prj = tomopy.prep.phase.retrieve_phase(prj,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True) # Filtering data with 2D median filter before downsampling and recon if medfilt_size>1: start_filter_time = time.time() print('\n*** Applying median filter') #prj = tomopy.median_filter(prj,size=1) prj = ndimage.median_filter(prj,footprint=np.ones((1, medfilt_size, medfilt_size))) print(' done in %0.3f min' % ((time.time() - start_filter_time)/60)) # Downsampling data: if downspling>0: print('\n** Applying downsampling') start_down_time = time.time() prj = tomopy.downsample(prj, level=binning) prj = tomopy.downsample(prj, level=binning, axis=1) print(' done in %0.3f min' % ((time.time() - start_down_time)/60)) print('\n*** Shape of the data:'+str(np.shape(prj))) print(' Dimension of theta:'+str(np.shape(theta))) return prj
def reconstruct(h5fname, sino, nframes, frame, nproj, binning, tv): # Read APS 32-BM raw data. print("Read data") proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino) print("Processing") proj = proj[(frame - nframes / 2) * nproj:(frame + nframes / 2) * nproj, :, :] # Flat-field correction of raw data. data = tomopy.normalize(proj, flat, dark, cutoff=1.4) # remove stripes data = tomopy.remove_stripe_fw(data, level=7, wname='sym16', sigma=1, pad=True) print("Raw data: ", h5fname) print("Frames for reconstruction:", (frame - nframes / 2), "..", (frame + nframes / 2)) # Phase retrieval for tomobank id 00080 # sample_detector_distance = 25 # detector_pixel_size_x = 3.0e-4 # monochromator_energy = 16 # phase retrieval # data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=8e-03,pad=True) data = tomopy.minus_log(data) data = tomopy.remove_nan(data, val=0.0) data = tomopy.remove_neg(data, val=0.00) data[np.where(data == np.inf)] = 0.00 # Binning data = tomopy.downsample(data, level=binning, axis=2) if data.shape[1] > 1: data = tomopy.downsample(data, level=binning, axis=1) rot_center = data.shape[2] / 2 theta = np.linspace(0, np.pi * nframes, nproj * nframes, endpoint=False) if tv: import rectv # Reconstruct. Iterative TV. [Ntheta, Nz, N] = data.shape Nzp = 4 # number of slices to process simultaniously by gpus M = nframes # number of basis functions, must be a multiple of nframes lambda0 = pow(2, -9) # regularization parameter 1 lambda1 = pow(2, 2) # regularization parameter 2 niters = 1024 # number of iterations ngpus = 1 # number of gpus # reorder input data for compatibility data = np.ndarray.flatten(data.swapaxes(0, 1)) rec = np.zeros([N * N * Nz * M], dtype='float32') # memory for result # Make a class for tv cl = rectv.rectv(N, Ntheta, M, nframes, Nz, Nzp, ngpus, lambda0, lambda1) # Run iterations cl.itertvR_wrap(rec, data, niters) rec = np.rot90( np.reshape(rec, [Nz, M, N, N]).swapaxes(0, 1), axes=(2, 3) ) / Ntheta * nframes * 2 # reorder result for compatibility rec = rec[::M / nframes] else: # Reconstruct object. FBP. rec = np.zeros((nframes, data.shape[1], data.shape[2], data.shape[2]), dtype='float32') for time_frame in range(0, nframes): rec0 = tomopy.recon( data[time_frame * nproj:(time_frame + 1) * nproj], theta[time_frame * nproj:(time_frame + 1) * nproj], center=rot_center - np.mod(time_frame, 2), algorithm='gridrec') # Mask each reconstructed slice with a circle. rec[time_frame] = tomopy.circ_mask(rec0, axis=0, ratio=0.95) return rec