def find_rotation_axis(h5fname, nsino): data_size = get_dx_dims(h5fname, 'data') ssino = int(data_size[1] * nsino) # Select sinogram range to reconstruct sino = None start = ssino end = start + 1 sino = (start, end) # Read APS 32-BM raw data. h5fname_norm = '/local/data/2019-02/Dunand/In-situ_75_NC_1/In-situ_75_NC_1_0499.h5' proj1, flat, dark, theta1 = dxchange.read_aps_32id(h5fname_norm, sino=sino) proj, dummy, dummy1, theta = dxchange.read_aps_32id(h5fname, 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=5,wname='sym16',sigma=1,pad=True) # find rotation center rot_center = tomopy.find_center_vo(data) return rot_center
def on_calibrate_dx(self): fname = str(self.ui.dx_file_name_line.text()) last_ind = util.get_dx_dims(str(fname), 'theta') if (last_ind == None): last_ind = util.get_dx_dims(str(fname), 'data') proj, flat, dark, theta = dx.read_aps_32id(fname, proj=(0, 1)) ##self.ui.theta_step.setText(str((180.0 / np.pi * theta[1] - theta[0]).astype(np.float))) if self.params.flat_field: first = proj[0,:,:].astype(np.float)/flat[0,:,:].astype(np.float) else: first = proj[0,:,:].astype(np.float) proj, flat, dark, theta = dx.read_aps_32id(fname, proj=(last_ind[0]-1, last_ind[0])) if self.params.flat_field: last = proj[0,:,:].astype(np.float)/flat[0,:,:].astype(np.float) else: last = proj[0,:,:].astype(np.float) with spinning_cursor(): self.center_calibration = ufot.process.CenterCalibration(first, last) position = self.center_calibration.position self.overlap_viewer.set_images(first, last) self.overlap_viewer.set_position(position)
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. h5fname_norm = '/local/data/2019-02/Chen/sample_0001.h5' # replace with the data set you want to take the white/dark fields from 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=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 find_rotation_axis(h5fname, nsino): data_size = get_dx_dims(h5fname, 'data') ssino = int(data_size[1] * nsino) # Select sinogram range to reconstruct sino = None start = ssino end = start + 1 sino = (start, end) # Read APS 32-BM raw data. h5fname_norm = '/local/data/2019-02/Dunand/In-situ_75_3/In-situ_75_3_0099.h5' proj1, flat, dark, theta1 = dxchange.read_aps_32id(h5fname_norm, sino=sino) proj, dummy, dummy1, theta = dxchange.read_aps_32id(h5fname, 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=5, wname='sym16', sigma=1, pad=True) # find rotation center rot_center = tomopy.find_center_vo(data) return rot_center
def rec_try(h5fname, nsino, rot_center, center_search_width, algorithm, binning): data_shape = get_dx_dims(h5fname, 'data') print(data_shape) ssino = int(data_shape[1] * nsino) center_range = (rot_center-center_search_width, rot_center+center_search_width, 0.5) #print(sino,ssino, center_range) #print(center_range[0], center_range[1], center_range[2]) # Select sinogram range to reconstruct sino = None start = ssino end = start + 1 sino = (start, end) # Read APS 32-BM raw data. # 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) # 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("Center: ", rot_center) data = tomopy.minus_log(data) stack = np.empty((len(np.arange(*center_range)), data_shape[0], data_shape[2])) index = 0 for axis in np.arange(*center_range): stack[index] = data[:, 0, :] index = index + 1 # Reconstruct the same slice with a range of centers. rec = tomopy.recon(stack, theta, center=np.arange(*center_range), sinogram_order=True, algorithm='gridrec', filter_name='parzen', nchunk=1) # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) index = 0 # Save images to a temporary folder. fname = os.path.dirname(h5fname) + '/' + 'try_rec/' + 'recon_' + os.path.splitext(os.path.basename(h5fname))[0] for axis in np.arange(*center_range): rfname = fname + '_' + str('{0:.2f}'.format(axis) + '.tiff') dxchange.write_tiff(rec[index], fname=rfname, overwrite=True) index = index + 1 print("Reconstructions: ", fname)
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. h5fname_norm = '/local/data/2019-02/Dunand/In-situ_75_3/In-situ_75_3_0099.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=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 read_aps_32id_adaptive(fname, proj=None, sino=None): """ Adaptive data reading function that works with dxchange both below and beyond version 0.0.11. """ dxver = dxchange.__version__ m = re.search(r'(\d+)\.(\d+)\.(\d+)', dxver) ver = m.group(1, 2, 3) ver = map(int, ver) if ver[0] > 0 or ver[1] > 1 or ver[2] > 1: dat, flt, drk, _ = dxchange.read_aps_32id(fname, proj=proj, sino=sino) else: dat, flt, drk = dxchange.read_aps_32id(fname, proj=proj, sino=sino) return dat, flt, drk
def main(arg): parser = argparse.ArgumentParser() parser.add_argument("dname", help="directory containing multiple datasets: /data/") parser.add_argument("--iters", nargs='?', type=int, default=10, help="number of iteration for alignment (default 7)") args = parser.parse_args() dname = args.dname iters = args.iters if os.path.isdir(dname): # Add a trailing slash if missing top = os.path.join(dname, '') print("DNAME:", dname) h5_file_list = list(filter(lambda x: x.endswith(('.h5', '.hdf')), os.listdir(top))) prj = np.zeros((61, 101, 151), dtype='float32') for m in range(len(h5_file_list)): print(h5_file_list[m]) # Read the XRF raw data. # prj += dx.read_hdf5(os.path.join(top, h5_file_list[m]), dataset='/exchange/data').astype('float32').copy() # ang = dx.read_hdf5(os.path.join(top, h5_file_list[4]), dataset='/exchange/theta').astype('float32').copy() # ang *= np.pi / 180. proj, flat, dark, theta = dx.read_aps_32id(top+h5_file_list[m]) prj += proj proj, flat, dark, theta = dx.read_aps_32id(top+h5_file_list[0]) ang = theta # Clean folder. try: shutil.rmtree('tmp/iters') except: pass prj = tomopy.remove_nan(prj, val=0.0) prj = tomopy.remove_neg(prj, val=0.0) prj[np.where(prj == np.inf)] = 0.0 print (prj.min(), prj.max()) prj, sx, sy, conv = tomopy.align_joint(prj, ang, iters=100, pad=(0, 0), blur=True, rin=0.8, rout=0.95, center=None, algorithm='pml_hybrid', upsample_factor=100, save=True, debug=True)
def read_tomo(sino, params): if params.hdf_file_type == 'standard': # Read APS 32-BM raw data. log.info(" *** loading a stardard data set: %s" % params.hdf_file) proj, flat, dark, theta = dxchange.read_aps_32id(params.hdf_file, sino=sino) elif params.hdf_file_type == 'flip_and_stich': log.info(" *** loading a 360 deg flipped data set: %s" % params.hdf_file) proj360, flat360, dark360, theta360 = dxchange.read_aps_32id( params.hdf_file, sino=sino) proj, flat, dark = flip_and_stitch(variableDict, proj360, flat360, dark360) theta = theta360[:len(theta360) // 2] # take first half else: # params.hdf_file_type == 'mosaic': log.error(" *** loading a mosaic data set is not supported yet") exit() if params.reverse: log.info(" *** correcting for 180-0 data collection") step_size = (theta[1] - theta[0]) theta_size = dxreader.read_dx_dims(params.hdf_file, 'data')[0] theta = np.linspace(np.pi, (0 + step_size), theta_size) if params.blocked_views: log.info(" *** correcting for blocked view data collection") miss_angles = [params.missing_angles_start, params.missing_angle_end] # Manage the missing angles: 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])) # new missing projection handling # if params.blocked_views: # log.warning(" *** new missing angle handling") # miss_angles = [params.missing_angles_start, params.missing_angle_end] # data = patch_projection(data, miss_angles) proj, flat, dark = binning(proj, flat, dark, params) rotation_axis = params.rotation_axis / np.power(2, float(params.binning)) log.info(" *** rotation center: %f" % rotation_axis) return proj, flat, dark, theta, rotation_axis
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 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 _read_tomo(params, sino, proj): if (str(params.file_format) in {'dx', 'aps2bm', 'aps7bm', 'aps32id'}): # temporary work around #flat_file = '/local/data/2020-10/PazPuente/flat_samp2_417.h5' #print('flat fields are taken from:', flat_file) # proj_bad, flat, dark, theta_bad = dxchange.read_aps_32id(flat_file, sino=sino) # proj, flat_bad, dark_bad, theta = dxchange.read_aps_32id(params.file_name, sino=sino) proj, flat, dark, theta = dxchange.read_aps_32id(params.file_name, sino=sino, proj=proj) log.info(" *** %s is a valid dx file format" % params.file_name) # Check if the flat and dark fields are single images or sets if len(flat.shape) == len(proj.shape): log.info(' *** median filter flat images') # Do a median filter on the first dimension flat = np.median(flat, axis=0, keepdims=True).astype(flat.dtype) if len(dark.shape) == len(proj.shape): log.info(' *** median filter dark images') # Do a median filter on the first dimension dark = np.median(dark, axis=0, keepdims=True).astype(dark.dtype) else: log.error(" *** %s is not a supported file format" % params.file_format) exit() return proj, flat, dark, theta
def clean_sino(h5fname, chunks=None): import tomopy import dxchange # Read APS 32-BM raw data. #print('reading data') proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=chunks) #proj, flat, dark, theta = dxchange.read_aps_32id(h5fname) # # Manage the missing angles: # if blocked_views is not None: # print("Blocked Views: ", blocked_views) # proj = np.concatenate((proj[0:blocked_views[0],:,:], proj[blocked_views[1]+1:-1,:,:]), axis=0) # theta = np.concatenate((theta[0:blocked_views[0]], theta[blocked_views[1]+1:-1])) # Flat-field correction of raw data. data = tomopy.normalize(proj, flat, dark, cutoff=1.4) #data = tomopy.normalize(proj, flat, dark, cutoff=(1,2)) # remove stripes #print('removing stripes, may take a while',flush=True) data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,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 data=np.swapaxes(data,0,1) return data, theta
def read_raw_sinogram(self, fname, type='tiff', center=None, pixel_size=1, fin_angle=180, max_count=None, **kwargs): """ Read raw sinogram from file. :param fname: file name :param type: file format :param center: rotation center :param preprocess: whether or not to preprocess the sinogram to remove singularities :param pixel_size: pixel size (um) :param kwargs: :return: """ if type == 'hdf5': slice = kwargs['slice'] raw_sino = np.squeeze( dxchange.read_aps_32id(fname, sino=(slice, slice + 1))) else: raw_sino = dxchange.read_tiff(fname) raw_sino = np.copy(raw_sino) self.raw_sino = Sinogram(raw_sino, 'raw', coords=center, center=center, normalize_bg=False, minus_log=False, fin_angle=fin_angle, max_count=max_count) self.pixel_size = pixel_size
def set_gui_startup(self, path): data_size = util.get_dx_dims(str(path), 'data') data_dark_size = util.get_dx_dims(str(path), 'data_dark') data_white_size = util.get_dx_dims(str(path), 'data_white') theta_size = util.get_dx_dims(str(path), 'theta') self.ui.data_size.setText(str(data_size)) self.ui.data_dark_size.setText(str(data_dark_size)) self.ui.data_white_size.setText(str(data_white_size)) self.ui.theta_size.setText(str(theta_size)) self.ui.dx_file_name_line.setText(path) self.ui.input_path_line.setText(path) self.input_file_path = os.path.dirname(str(path)) fname = str(self.ui.dx_file_name_line.text()) proj, flat, dark, theta = dx.read_aps_32id(fname, proj=(0, 1)) self.ui.theta_step.setText(str(np.rad2deg((theta[1] - theta[0])))) self.params.theta_start = theta[0] self.params.theta_end = theta[-1] self.params.projection_number = data_size[0] self.ui.theta_start.setValue(np.rad2deg(theta[0])) self.ui.theta_end.setValue(np.rad2deg(theta[-1])) self.dsize = data_size[1] self.dsize_bin = (data_size[1]/np.power(2, float(self.params.binning))).astype(np.int) self.ui.slice_start.setRange(0, self.dsize_bin) self.ui.slice_start.setValue(self.dsize_bin/2) self.ui.slice_center.setRange(0, self.dsize) self.ui.slice_center.setValue(self.dsize/2) self.params.input_file_path = set_input_file_path(path, self.ui.dx_file_name_line, self.params.input_file_path) self.params.input_path = set_input_path(path, self.ui.input_path_line, self.params.input_path) self.params.output_path = set_output_path(path, self.ui.output_path_line, self.params.output_path) self.ui.preprocessing_container.setVisible(True) self.ui.reconstruction_container.setVisible(True) self.ui.output_container.setVisible(True) self.ui.flat_field.setVisible(True) self.ui.calibrate_dx.setVisible(True) self.ui.calibrate_container.setVisible(True) self.ui.dx_data_label.setVisible(True) self.ui.dx_data_white_label.setVisible(True) self.ui.dx_data_dark_label.setVisible(True) self.ui.dx_theta_label.setVisible(True) self.ui.pre_processing_box.setVisible(True) self.on_flat_field_clicked() self.on_pre_processing_box_clicked() self.on_show_projection_clicked()
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 reconstruct(h5fname, sino, rot_center, args, blocked_views=None): # Read APS 32-BM raw data. proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino) # Manage the missing angles: if blocked_views is not None: print("Blocked Views: ", blocked_views) proj = np.concatenate((proj[0:blocked_views[0], :, :], proj[blocked_views[1] + 1:-1, :, :]), axis=0) theta = np.concatenate( (theta[0:blocked_views[0]], theta[blocked_views[1] + 1:-1])) # 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("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 algorithm = args.algorithm ncores = args.ncores nitr = args.num_iter # always add algorithm _kwargs = {"algorithm": algorithm} # assign number of cores _kwargs["ncore"] = ncores # don't assign "num_iter" if gridrec or fbp if algorithm not in ["fbp", "gridrec"]: _kwargs["num_iter"] = nitr # Reconstruct object. with timemory.util.auto_timer( "[tomopy.recon(algorithm='{}')]".format(algorithm)): rec = tomopy.recon(proj, theta, **_kwargs) # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) return rec
def main(): # file_name = '/local/data/2020-02/Stock/100_B949_81_84_B2.h5' file_name = '/local/data/tomo_00001.h5' data_size = get_dx_dims(file_name) ssino = int(data_size[1] * 0.5) detector_center = int(data_size[2] * 0.5) # Select sinogram range to reconstruct sino_start = ssino sino_end = sino_start + 1 sino = (int(sino_start), int(sino_end)) # Read APS 2-BM raw data proj, flat, dark, theta = dxchange.read_aps_32id(file_name, sino=sino) tomo_ind = tomopy.normalize(proj, flat, dark) # data = tomopy.normalize_bg(proj, air=10) tomo_ind = tomopy.minus_log(tomo_ind) rec = recon(tomo_ind, theta, center=detector_center, sinogram_order=False, algorithm='gridrec', filter_name='shepp') rec = circ_mask(rec, axis=0) # tomopy score, simplified rescaling of histogram bounds hmin, hmax = _adjust_hist_limits( tomo_ind, theta, mask=True, sinogram_order=False) print(hmin, hmax) print(find_center(tomo_ind, theta)) centers = np.linspace(-25, 25, 101) + detector_center print(centers) tpscore = [] blur = [] for center in centers: val, bval = _find_center_cost( center, tomo_ind, theta, hmin, hmax, mask=True, ratio=1., sinogram_order=False) tpscore.append(val) blur.append(bval) plt.plot(centers, rescale(tpscore), label='tomopy score') plt.plot(centers, rescale(blur), label='blurriness') plt.legend() plt.title('centering scores') plt.xlabel('center') plt.show()
def main(arg): parser = argparse.ArgumentParser() parser.add_argument( "fname", help= "directory containing multiple dxfiles or a single DataExchange file: /data/ or /data/sample.h5" ) parser.add_argument( "--tiff", action="store_true", help="convert a single DataExchange file to a stack of tiff files") args = parser.parse_args() # Set path to the micro-CT data to reconstruct. fname = args.fname tiff = args.tiff if os.path.isfile(fname): dump_hdf5_file_structure(fname) if tiff: # Read APS 32-BM raw data. print("Reading HDF5 file: ", fname) proj, flat, dark, theta = dxchange.read_aps_32id(fname) print("Converting ....") top_out = os.path.join( os.path.dirname(fname), os.path.splitext(os.path.basename(fname))[0]) flats_out = os.path.join(top_out, "flats", "image") darks_out = os.path.join(top_out, "darks", "image") radios_out = os.path.join(top_out, "radios", "image") print("flats: ", flat.shape) dxchange.write_tiff_stack(flat, fname=flats_out) print("darks: ", dark.shape) dxchange.write_tiff_stack(dark, fname=darks_out) print("projections: ", proj.shape) dxchange.write_tiff_stack(proj, fname=radios_out) print("Converted data: ", top_out) print("Done!") elif os.path.isdir(fname): # Add a trailing slash if missing top = os.path.join(fname, '') # Set the file name that will store the rotation axis positions. h5_file_list = list( filter(lambda x: x.endswith(('.h5', '.hdf')), os.listdir(top))) print("Found: ", h5_file_list.sort()) for fname in h5_file_list: h5fname = top + fname dump_hdf5_file_structure(h5fname) else: print("Directory or File Name does not exist: ", fname)
def setup_simulation_data(input_f, beg_sinogram=0, num_sinograms=0): print("Loading tomography data: {}".format(input_f)) t0 = time.time() idata, flat, dark, itheta = dxchange.read_aps_32id(input_f) idata = np.array(idata, dtype=np.dtype('uint16')) flat = np.array(flat, dtype=np.dtype('uint16')) dark = np.array(dark, dtype=np.dtype('uint16')) itheta = np.array(itheta, dtype=np.dtype('float32')) print( "Projection dataset IO time={:.2f}; dataset shape={}; size={}; Theta shape={};" .format(time.time() - t0, idata.shape, idata.size, itheta.shape)) return idata, flat, dark, itheta
def load_files(self, filenames, ffc_correction): """Load *filenames* for display.""" self.filenames = filenames self.ffc_correction = ffc_correction proj, flat, dark, theta = dx.read_aps_32id(filenames, proj=(0, 1)) #self.slider.setRange(0, len(theta) - 1) self.slider.setRange(0, util.get_dx_dims(str(filenames), 'data')[0] - 1) self.slider.setSliderPosition(0) self.update_image()
def tomo_stat(h5fname): # Read APS 32-BM raw data proj, flat, dark, theta = dxchange.read_aps_32id(h5fname) mean = flat[10, :, :].mean() amin = flat[10, :, :].min() amax = flat[10, :, :].max() std = flat[10, :, :].std() var = flat[10, :, :].var() return amin, amax, mean, std, var
def update_image(self): """Update the currently display image.""" if self.filenames: pos = self.slider.value() proj, flat, dark, theta = dx.read_aps_32id(self.filenames, proj=(pos, pos + 1)) if self.ffc_correction: image = proj[0, :, :].astype(np.float) / flat[0, :, :].astype( np.float) else: image = proj[0, :, :].astype(np.float) self.image_item.setImage(image)
def reconstruct(h5fname, sino, rot_center, args, blocked_views=None): # Read APS 32-BM raw data. proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino) # Manage the missing angles: if blocked_views is not None: print("Blocked Views: ", blocked_views) proj = np.concatenate((proj[0:blocked_views[0], :, :], proj[blocked_views[1]+1:-1, :, :]), axis=0) theta = np.concatenate((theta[0:blocked_views[0]], theta[blocked_views[1]+1: -1])) # 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("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 algorithm = args.algorithm ncores = args.ncores nitr = args.num_iter # always add algorithm _kwargs = {"algorithm": algorithm} # assign number of cores _kwargs["ncore"] = ncores # don't assign "num_iter" if gridrec or fbp if algorithm not in ["fbp", "gridrec"]: _kwargs["num_iter"] = nitr # Reconstruct object. with timemory.util.auto_timer( "[tomopy.recon(algorithm='{}')]".format(algorithm)): rec = tomopy.recon(proj, theta, **_kwargs) # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) return rec
def write_first_frames(ui): root = os.getcwd() os.chdir(ui.raw_folder) try: os.mkdir('first_frames') except: pass for i in ui.filelist: ui.boxMetaOut.insert(END, i + '\n') prj, flt, drk = dxchange.read_aps_32id(i, proj=(0, 1)) prj = tomopy.normalize(prj, flt, drk) dxchange.write_tiff(prj, os.path.join('first_frames', os.path.splitext(i)[0]))
def _read_tomo(params, sino): if (str(params.file_format) in {'dx', 'aps2bm', 'aps7bm', 'aps32id'}): proj, flat, dark, theta = dxchange.read_aps_32id(params.file_name, sino=sino) log.info(" *** %s is a valid dx file format" % params.file_name) # elif: # # add here other dxchange loader # log.info(" *** %s is a valid xxx file format" % params.file_name) else: log.error(" *** %s is not a supported file format" % params.file_format) exit() return proj, flat, dark, theta
def write_first_frames(ui): root = os.getcwd() os.chdir(ui.raw_folder) try: os.mkdir('first_frames') except: pass for i in ui.filelist: ui.boxMetaOut.insert(END, i + '\n') prj, flt, drk = dxchange.read_aps_32id(i, proj=(0, 1)) prj = tomopy.normalize(prj, flt, drk) prj = preprocess(prj) dxchange.write_tiff( prj, os.path.join('first_frames', os.path.splitext(i)[0]))
def main(arg): parser = argparse.ArgumentParser() parser.add_argument("fname", help="file name of a single dataset to normalize: /data/sample.h5") args = parser.parse_args() fname = args.fname if os.path.isfile(fname): data_shape = get_dx_dims(fname, 'data') # Select projgram range to reconstruct. proj_start = 0 proj_end = data_shape[0] chunks = 6 # number of projgram chunks to reconstruct # only one chunk at the time is converted # allowing for limited RAM machines to complete a full reconstruction nProj_per_chunk = (proj_end - proj_start)/chunks print("Normalizing [%d] slices from slice [%d] to [%d] in [%d] chunks of [%d] slices each" % ((proj_end - proj_start), proj_start, proj_end, chunks, nProj_per_chunk)) strt = 0 for iChunk in range(0,chunks): print('\n -- chunk # %i' % (iChunk+1)) proj_chunk_start = np.int(proj_start + nProj_per_chunk*iChunk) proj_chunk_end = np.int(proj_start + nProj_per_chunk*(iChunk+1)) print('\n --------> [%i, %i]' % (proj_chunk_start, proj_chunk_end)) if proj_chunk_end > proj_end: break nproj = (int(proj_chunk_start), int(proj_chunk_end)) # Reconstruct. proj, flat, dark, dummy = dxchange.read_aps_32id(fname, proj=nproj) # Flat-field correction of raw data. data = tomopy.normalize(proj, flat, dark, cutoff=0.9) # Write data as stack of TIFs. tifffname = os.path.dirname(fname) + os.sep + os.path.splitext(os.path.basename(fname))[0]+ '_tiff' + os.sep + os.path.splitext(os.path.basename(fname))[0] print("Converted files: ", tifffname) dxchange.write_tiff_stack(data, fname=tifffname, start=strt) strt += nproj[1] - nproj[0] else: print("File Name does not exist: ", fname)
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 __init__(self): proj, flat, dark, theta = dxchange.read_aps_32id( '/local/data/2020-07/Nikitin/scan_057.h5', sino=(0, 2048)) proj = proj[:, ::2, ::2] dark = dark[:, ::2, ::2] flat = flat[:, ::2, ::2] theta = theta * 180 / np.pi [ntheta, height, width] = proj.shape proj = proj.reshape(proj.shape[0], proj.shape[1] * proj.shape[2]) self.buffer_size = 1500 self.proj_buffer = np.zeros([self.buffer_size, height * width], dtype='uint8') self.theta_buffer = np.zeros([self.buffer_size], dtype='float32') self.uniqueids_buffer = np.zeros([self.buffer_size], dtype='int32') self.slv = solver.Solver(self.buffer_size, width, height, 1224, 1224, 1224, 1024) self.slv.set_flat(flat) self.slv.set_dark(dark) self.num_proj = 0 self.proj = proj self.theta = theta
def reconstruct(h5fname, sino, rot_center, blocked_views=None): # Read APS 32-BM raw data. proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino) # Manage the missing angles: if blocked_views is not None: print("Blocked Views: ", blocked_views) proj = np.concatenate((proj[0:blocked_views[0],:,:], proj[blocked_views[1]+1:-1,:,:]), axis=0) theta = np.concatenate((theta[0:blocked_views[0]], theta[blocked_views[1]+1:-1])) # 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("Center: ", rot_center) # # 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) 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 # Reconstruct object. rec = tomopy.recon(data, theta, center=rot_center, algorithm='gridrec') # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) return rec
def main(): data_top = '/local/data/2020-02/Stock/' file_name = '099_B949_81_84_B2' # data_top = '/local/data/' # file_name = 'tomo_00001' top = '/local/data/2020-02/Stock/' # log.info(os.listdir(top)) h5_file_list = list(filter(lambda x: x.endswith(('.h5', '.hdf')), os.listdir(top))) h5_file_list.sort() print("Found: %s" % h5_file_list) for fname in h5_file_list: full_file_name = data_top + fname data_size = get_dx_dims(full_file_name) print(data_size) ssino = int(data_size[1] * 0.5) detector_center = int(data_size[2] * 0.5) # Select sinogram range to reconstruct sino_start = ssino sino_end = sino_start + 10 sino = (int(sino_start), int(sino_end)) # Read APS 2-BM raw data proj, flat, dark, theta = dxchange.read_aps_32id(full_file_name, sino=sino) tomo_ind = tomopy.normalize(proj, flat, dark) print(os.path.splitext(full_file_name)[0]+'_sino') dxchange.write_tiff_stack(tomo_ind,fname=os.path.splitext(full_file_name)[0]+'_sino', axis=1)
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
def read_data_adaptive(fname, proj=None, sino=None, data_format='aps_32id', shape_only=False, return_theta=True, **kwargs): """ Adaptive data reading function that works with dxchange both below and beyond version 0.0.11. """ theta = None dxver = dxchange.__version__ m = re.search(r'(\d+)\.(\d+)\.(\d+)', dxver) ver = m.group(1, 2, 3) ver = map(int, ver) if proj is not None: proj_step = 1 if len(proj) == 2 else proj[2] if sino is not None: sino_step = 1 if len(sino) == 2 else sino[2] if data_format == 'aps_32id': if shape_only: f = h5py.File(fname) d = f['exchange/data'] return d.shape try: if ver[0] > 0 or ver[1] > 1 or ver[2] > 1: dat, flt, drk, theta = dxchange.read_aps_32id(fname, proj=proj, sino=sino) else: dat, flt, drk = dxchange.read_aps_32id(fname, proj=proj, sino=sino) f = h5py.File(fname) theta = f['exchange/theta'].value theta = theta / 180 * np.pi except: f = h5py.File(fname) d = f['exchange/data'] theta = f['exchange/theta'].value theta = theta / 180 * np.pi if proj is None: dat = d[:, sino[0]:sino[1]:sino_step, :] flt = f['exchange/data_white'][:, sino[0]:sino[1]:sino_step, :] try: drk = f['exchange/data_dark'][:, sino[0]:sino[1]:sino_step, :] except: print('WARNING: Failed to read dark field. Using zero array instead.') drk = np.zeros([flt.shape[0], 1, flt.shape[2]]) elif sino is None: dat = d[proj[0]:proj[1]:proj_step, :, :] flt = f['exchange/data_white'].value try: drk = f['exchange/data_dark'].value except: print('WARNING: Failed to read dark field. Using zero array instead.') drk = np.zeros([1, flt.shape[1], flt.shape[2]]) else: dat = None flt = None drk = None print('ERROR: Sino and Proj cannot be specifed simultaneously. ') elif data_format == 'aps_13bm': f = cdf.Dataset(fname) if shape_only: return f['array_data'].shape if sino is None: dat = f['array_data'][proj[0]:proj[1]:proj_step, :, :].astype('uint16') basename = os.path.splitext(fname)[0] flt1 = cdf.Dataset(basename + '_flat1.nc')['array_data'][...] flt2 = cdf.Dataset(basename + '_flat2.nc')['array_data'][...] flt = np.vstack([flt1, flt2]).astype('uint16') drk = np.zeros([1, flt.shape[1], flt.shape[2]]).astype('uint16') drk[...] = 64 elif proj is None: dat = f['array_data'][:, sino[0]:sino[1]:sino_step, :].astype('uint16') basename = os.path.splitext(fname)[0] flt1 = cdf.Dataset(basename + '_flat1.nc')['array_data'][:, sino[0]:sino[1]:sino_step, :] flt2 = cdf.Dataset(basename + '_flat2.nc')['array_data'][:, sino[0]:sino[1]:sino_step, :] flt = np.vstack([flt1, flt2]).astype('uint16') drk = np.zeros([1, flt.shape[1], flt.shape[2]]).astype('uint16') drk[...] = 64 if not (abs(theta[-1] - theta[0] - 2 * np.pi) < 0.1 or abs(theta[-1] - theta[0] - np.pi) < 0.1): print('Detected theta problem in dataset. Replacing it with 0-180 linear space.') theta = np.linspace(0, np.pi, dat.shape[0]) if return_theta: return dat, flt, drk, theta else: return dat, flt, drk
import dxchange import dxchange.reader as dxreader import tomopy h5fname = '/local/data/2018-03/Lindley/Exp005_subsea_bolt_sample_1_26C_04_YPos12.2mm_FriMar23_15_11_42_2018_edge_2x_750mm_800.0msecExpTime_0.12DegPerSec_Rolling_20umLuAG_1mmAl15mmSi4mmSn0.5mmCu_0.0mrad_USArm1.25_monoY_-16.0_AHutch/proj_0005.hdf' zinger_level = 800 # Zinger level for projections zinger_level_w = 1000 # Zinger level for white # Read the txrm raw data. start = 0 end = start + 2000 sino = (start, end) # Read APS 32-BM raw data. data, 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=1.4) dxchange.write_tiff_stack(data, fname='/local/data/2018-03/Lindley/Exp005_02/recon_')
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 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
#rot_center = 175 detector_pixel_size_x = 1.4e-4 monochromator_energy = 55.0 # Set path to the micro-CT data to reconstruct. fname = '/tomobank/tomo_00064_to_00067/' + tomo_id + '.h5' # Select the sinogram range to reconstruct. start = 122 end = 124 sino = (start, end) # Read raw data. proj, flat, dark, theta = dxchange.read_aps_32id(fname, sino=(start, end)) # Flat-field correction of raw data. data = tomopy.normalize(proj, flat, dark) # remove stripes data = tomopy.prep.stripe.remove_stripe_fw(data, level=5, 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,
% ((sino_end - sino_start), sino_start, sino_end, chunks, nSino_per_chunk)) strt = 0 for iChunk in range(0, chunks): print('\n -- chunk # %i' % (iChunk + 1)) sino_chunk_start = sino_start + nSino_per_chunk * iChunk sino_chunk_end = sino_start + nSino_per_chunk * (iChunk + 1) print('\n --------> [%i, %i]' % (sino_chunk_start, sino_chunk_end)) if sino_chunk_end > sino_end: break sino = (int(sino_chunk_start), int(sino_chunk_end)) # Read APS 32-BM raw data. proj, flat, dark, theta = dxchange.read_aps_32id(fname, 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=1.4) # remove stripes data = tomopy.remove_stripe_fw(data, level=5, wname='sym16', sigma=1, pad=True) #data = tomopy.prep.stripe.remove_stripe_ti(data,alpha=7)
def reconstruct(h5fname, sino, rot_center, args, blocked_views=None): # Read APS 32-BM raw data. proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino) # Manage the missing angles: if blocked_views is not None: print("Blocked Views: ", blocked_views) proj = np.concatenate((proj[0:blocked_views[0], :, :], proj[blocked_views[1]+1:-1, :, :]), axis=0) theta = np.concatenate((theta[0:blocked_views[0]], theta[blocked_views[1]+1: -1])) # 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("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 algorithm = args.algorithm ncores = args.ncores nitr = args.num_iter # always add algorithm _kwargs = {"algorithm": algorithm} # assign number of cores _kwargs["ncore"] = ncores # use the accelerated version if algorithm in ["mlem", "sirt"]: _kwargs["accelerated"] = True # don't assign "num_iter" if gridrec or fbp if algorithm not in ["fbp", "gridrec"]: _kwargs["num_iter"] = nitr sname = os.path.join(args.output_dir, 'proj_{}'.format(args.algorithm)) print(proj.shape) tmp = np.zeros((proj.shape[0], proj.shape[2])) tmp[:,:] = proj[:,0,:] output_image(tmp, sname + "." + args.format) # Reconstruct object. with timemory.util.auto_timer( "[tomopy.recon(algorithm='{}')]".format(algorithm)): print("Starting reconstruction with kwargs={}...".format(_kwargs)) rec = tomopy.recon(data, theta, **_kwargs) print("Completed reconstruction...") # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) obj = np.zeros(rec.shape, dtype=rec.dtype) label = "{} @ {}".format(algorithm.upper(), h5fname) quantify_difference(label, obj, rec) return rec
miss_projs = [128, 256] # Select sinogram range to reconstruct. start = 512 end = 2048 # Set number of data chunks for the reconstruction. chunks = 64 num_sino = (end - start) // chunks for m in range(chunks): sino_start = start + num_sino * m sino_end = start + num_sino * (m + 1) # Read APS 32-ID raw data. proj, flat, dark = dxchange.read_aps_32id(fname, sino=(sino_start, sino_end)) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(proj.shape[0]) # Remove the missing angles from data. proj = np.concatenate((proj[0:miss_projs[0], :, :], proj[miss_projs[1] + 1:-1, :, :]), axis=0) theta = np.concatenate((theta[0:miss_projs[0]], theta[miss_projs[1] + 1:-1])) # Flat-field correction of raw data. proj = tomopy.normalize(proj, flat, dark) proj = tomopy.minus_log(proj) # Reconstruct object using Gridrec algorithm. rec = tomopy.recon(proj, theta, center=rot_center, algorithm='gridrec')
def rec_try(h5fname, nsino, rot_center, center_search_width, algorithm, binning): data_shape = get_dx_dims(h5fname, 'data') print(data_shape) ssino = int(data_shape[1] * nsino) rot_center+=data_shape[2]//4 center_range = (rot_center-center_search_width, rot_center+center_search_width, 0.5) #print(sino,ssino, center_range) #print(center_range[0], center_range[1], center_range[2]) # Select sinogram range to reconstruct sino = None start = ssino end = start + 1 sino = (start, end) # Read APS 32-BM raw data. proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino) # Flat-field correction of raw data. data = tomopy.normalize(proj, flat, dark, cutoff=1.4) data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=2,pad=True) #data = tomopy.remove_stripe_ti(data, alpha=1.5) data = tomopy.remove_stripe_sf(data, size=150) # remove stripes # data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True) print("Raw data: ", h5fname) print("Center: ", rot_center) data = tomopy.minus_log(data) # 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 stack = np.empty((len(np.arange(*center_range)), data.shape[0], data.shape[2])) print(stack.shape) print(data.shape) index = 0 for axis in np.arange(*center_range): stack[index] = data[:, 0, :] index = index + 1 # Reconstruct the same slice with a range of centers. rec = tomopy.recon(stack, theta, center=np.arange(*center_range), sinogram_order=True, algorithm='gridrec', filter_name='parzen', nchunk=1) rec = rec[:,N//4:5*N//4,N//4:5*N//4] # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) index = 0 # Save images to a temporary folder. fname = os.path.dirname(h5fname) + '/' + 'try_rec/' + 'recon_' + os.path.splitext(os.path.basename(h5fname))[0] for axis in np.arange(*center_range): rfname = fname + '_' + str('{0:.2f}'.format(axis-N//4) + '.tiff') dxchange.write_tiff(rec[index], fname=rfname, overwrite=True) index = index + 1 print("Reconstructions: ", fname)
import numpy as np 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 h5fname = '/local/dataraid/stu/proj_0206.hdf' fname = '/local/dataraid/stu/proj_0206p.hdf' print(fname) # Read APS 32-BM raw data. proj, flat, dark, theta = dxchange.read_aps_32id(h5fname) # 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)
def rec_try(h5fname, nsino, rot_center, center_search_width, algorithm, binning, dark_file): data_shape = get_dx_dims(h5fname, 'data') print(data_shape) ssino = int(data_shape[1] * nsino) center_range = (rot_center-center_search_width, rot_center+center_search_width, 0.5) #print(sino,ssino, center_range) #print(center_range[0], center_range[1], center_range[2]) # Select sinogram range to reconstruct sino = None start = ssino end = start + 1 sino = (start, end) # Read APS 32-BM raw data. proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino) if dark_file is not None: print('Reading white/dark from {}'.format(dark_file)) proj_, flat, dark, theta_ = dxchange.read_aps_32id(dark_file, sino=sino) del proj_, theta_ print(proj.shape, flat.shape, dark.shape) # 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("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 stack = np.empty((len(np.arange(*center_range)), data_shape[0], data_shape[2])) index = 0 for axis in np.arange(*center_range): stack[index] = data[:, 0, :] index = index + 1 # Reconstruct the same slice with a range of centers. rec = tomopy.recon(stack, theta, center=np.arange(*center_range), sinogram_order=True, algorithm='gridrec', filter_name='parzen', nchunk=1) # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) index = 0 # Save images to a temporary folder. #fname = os.path.dirname(h5fname) + os.sep + 'try_rec/' + path_base_name(h5fname) + os.sep + 'recon_' + os.path.splitext(os.path.basename(h5fname))[0] fname = os.path.dirname(h5fname) + os.sep + 'centers/' + path_base_name(h5fname) + os.sep + 'recon_' + os.path.splitext(os.path.basename(h5fname))[0] for axis in np.arange(*center_range): rfname = fname + '_' + str('{0:.2f}'.format(axis) + '.tiff') dxchange.write_tiff(rec[index], fname=rfname, overwrite=True) index = index + 1 print("Reconstructions: ", fname)
level=logging.INFO) logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) working_dir = "/mnt/ssd0/xray/20151216_APS_32ID/rs64_5X_9200eV_2s" filename = 'rs64_241proj_5X_9200eV_2_.h5' os.chdir(working_dir) start = time.time() # Read HDF5 file. logger.info("Reading data from H5 file %s" % filename) #TODO: read directly into different mpi processes if rank == 0: # read data into root node proj, flat, dark, theta = dxchange.read_aps_32id(filename, dtype=np.float32) else: proj, flat, dark, theta = None, None, None, None # create MpiArray from Proj data proj = MpiArray.fromglobalarray(proj) proj.scatter(0) proj.arr = None # remove full array to save memory # share flats, darks, and theta to all MPI nodes flat = comm.bcast(flat, root=0) dark = comm.bcast(dark, root=0) theta = comm.bcast(theta, root=0) # Flat field correct data logger.info("Flat field correcting data")
dict2 = dictionary[key] for h5name in dict2: prefix = 'exp_' fname = top + prefix + h5name + '/proj_' + h5name + '.hdf' rot_center = dict2[h5name] #print(fname, rot_center) # Select sinogram range to reconstruct. sino = None #start = 285 #end = 286 #sino = (start, end) # Read APS 32-ID raw data. proj, flat, dark, theta = dxchange.read_aps_32id(fname, sino=sino) # Flat-field correction of raw data. proj = tomopy.normalize(proj, flat, dark) # remove stripes proj = tomopy.remove_stripe_fw(proj,level=5,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=8e-3,pad=True) # Find rotation center #rot_center = tomopy.find_center(proj, theta, init=rot_center, ind=start, tol=0.5) print(h5name, rot_center) proj = tomopy.minus_log(proj)
def rec_try(h5fname, nsino, rot_center, center_search_width, algorithm, binning): zinger_level = 800 # Zinger level for projections zinger_level_w = 1000 # Zinger level for white data_shape = get_dx_dims(h5fname, 'data') print(data_shape) ssino = int(data_shape[1] * nsino) center_range = (rot_center-center_search_width, rot_center+center_search_width, 0.5) #print(sino,ssino, center_range) #print(center_range[0], center_range[1], center_range[2]) # Select sinogram range to reconstruct sino = None start = ssino end = start + 1 sino = (start, end) # Read APS 32-BM raw data. proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino) theta = np.linspace(0. , 2 * np.pi, 3000) # 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=1.4) # remove stripes data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True) print("Raw data: ", h5fname) print("Center: ", rot_center) data = tomopy.minus_log(data) stack = np.empty((len(np.arange(*center_range)), data_shape[0], data_shape[2])) index = 0 for axis in np.arange(*center_range): stack[index] = data[:, 0, :] index = index + 1 # Reconstruct the same slice with a range of centers. rec = tomopy.recon(stack, theta, center=np.arange(*center_range), sinogram_order=True, algorithm='gridrec', filter_name='parzen', nchunk=1) # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) index = 0 # Save images to a temporary folder. fname = os.path.dirname(h5fname) + '/' + 'try_rec/' + 'recon_' + os.path.splitext(os.path.basename(h5fname))[0] for axis in np.arange(*center_range): rfname = fname + '_' + str('{0:.2f}'.format(axis) + '.tiff') dxchange.write_tiff(rec[index], fname=rfname, overwrite=True) index = index + 1 print("Reconstructions: ", fname)
#!/usr/bin/env python # -*- coding: utf-8 -*- """ TomoPy example script to reconstruct the tomography data as with gridrec. """ from __future__ import print_function import tomopy import dxchange if __name__ == '__main__': # Set path to the micro-CT data to reconstruct. fname = '../../../tomopy/data/tooth.h5' # Select the sinogram range to reconstruct. start = 0 end = 2 # Read the APS 2-BM 0r 32-ID raw data. proj, flat, dark = dxchange.read_aps_32id(fname, sino=(start, end)) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(proj.shape[0]) # Set data collection angles as equally spaced between 0-180 degrees. proj = tomopy.normalize(proj, flat, dark) # Set data collection angles as equally spaced between 0-180 degrees. rot_center = tomopy.find_center(proj, theta, init=290, ind=0, tol=0.5)
""" from __future__ import print_function import tomopy import dxchange if __name__ == '__main__': # Set path to the micro-CT data to reconstruct. fname = '../../../tomopy/data/tooth.h5' # Select the sinogram range to reconstruct. start = 0 end = 2 # Read the APS 2-BM 0r 32-ID raw data. proj, flat, dark, theta = dxchange.read_aps_32id(fname, sino=(start, end)) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(proj.shape[0]) # Set data collection angles as equally spaced between 0-180 degrees. proj = tomopy.normalize(proj, flat, dark) # Set data collection angles as equally spaced between 0-180 degrees. rot_center = tomopy.find_center(proj, theta, init=290, ind=0, tol=0.5) proj = tomopy.minus_log(proj) # Reconstruct object using Gridrec algorithm. recon = tomopy.recon(proj, theta, center=rot_center, algorithm='gridrec')
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) if downsapling > 0: # binning prj = tomopy.downsample(prj, level=binning) prj = tomopy.downsample(prj, level=binning, axis=1) return prj if __name__ == "__main__": for k in range(ndsets): # read data prj, flat, dark, theta = dxchange.read_aps_32id( file_name, sino=(sino_start, sino_end), proj=(theta_end*k,theta_end*(k+1))) print(theta.shape) theta = theta[theta_end*k:theta_end*(k+1)] # preprocess prj = preprocess_data(prj, flat, dark, FF_norm=flat_field_norm, remove_rings=remove_rings, FF_drift_corr=flat_field_drift_corr, downsapling=binning) np.save(name+'_bin'+str(binning)+str(k),prj) np.save(name+'_theta'+str(k),theta)
sample_detector_distance = 10 # Propagation distance of the wavefront in cm detector_pixel_size_x = 0.000065 # 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. ExchangeRank = 0 auto_center = False debug = 1 #---------------------------------------------------------------------------------- file_name= input_path+file_string+'.h5' N_recon = Center_end - Center_st #prj, flat, dark, theta = dxchange.read_aps_32id(file_name, sino=(slice_no, slice_no+1)) prj, flat, dark, theta = dxchange.read_aps_32id(file_name, sino=(slice_no, slice_no+1)) # Read theta from the dataset: #File = h5py.File(file_name, "r"); dset_theta = File["/exchange/theta"]; theta = dset_theta[...]; theta = theta*np.pi/180 #prj = prj[130:1175,:,:] #theta = theta[130:1175] #theta = tomopy.angles(691, -78, 96) if debug: print('## Debug: after reading data:') print('\n** Shape of the data:'+str(np.shape(prj))) print('** Shape of theta:'+str(np.shape(theta))) print('\n** Min and max val in prj before recon: %0.5f, %0.3f' % (np.min(prj), np.max(prj))) prj = tomopy.normalize(prj, flat, dark) print('\n** Flat field correction done!')
help="Directory containing an output file name: /data/sample.h5") parser.add_argument( "--axis", nargs='?', type=str, default="100", help= "Approximate rotation axis location (pixel): 10.0 (default 10 image horizontal size)" ) args = parser.parse_args() apr_center = np.int(args.axis) # Read data proj, flat, dark, theta = dxchange.read_aps_32id(args.fname, sino=(0, 512), proj=(0, 3000, 1500)) print(theta[np.arange(0, 3000, 1500)]) # filter data data = proj data = tomopy.normalize(proj, flat, dark) #data[data<0] = 0 #data[np.isinf(data)] = 0 #data[np.isnan(data)] = 0 # remove stripes #data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True) #data = tomopy.remove_stripe_sf(data, size=150) #data = tomopy.minus_log(data) # stitched data w = apr_center * 2
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
########################################################################## if recon_1slice: start_time = time.time() print('\n#### Processing '+ file_name) f = h5py.File(file_name, "r"); sino_start = int(np.round(f["/exchange/data"].shape[1]/2)) sino_end = int(sino_start + pow(2,binning)) print("** Test reconstruction of slice [%d]" % sino_start) # Read HDF5 file. print('\n*** Reading data:') start_reading_time = time.time() try: prj, flat, dark, theta = dxchange.read_aps_32id(file_name, sino=(sino_start, sino_end)) print(prj.shape, flat.shape, dark.shape) except: prj, flat, dark = dxchange.read_aps_32id(file_name, sino=(sino_start, sino_end)) print(prj.shape, flat.shape, dark.shape) f = h5py.File(file_name, "r"); dset_theta = f["/exchange/theta"]; theta = dset_theta[...]; theta = theta*np.pi/180 print(" Reading time: %0.3f min" % ((time.time() - start_reading_time)/60)) # Pre-processing data prj = 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) # reconstruct ########################################## print('\n*** Reconstructing...') start_recon_time = time.time()
from scipy import misc, ndimage import h5py import sirtfilter ##################################### Inputs ######################################################################### file_name = '/local/dataraid/2018-06/DeAndrade/2018-06-19/brain_petrapoxy/Brain_Petrapoxy_day2_4800prj_720deg_166.h5' 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)