def build_panorama(src_folder, file_grid, shift_grid, frame=0, method='max', method2=None, blend_options={}, blend_options2={}, blur=None, color_correction=False, margin=100): t00 = time.time() root = os.getcwd() os.chdir(src_folder) cam_size = g_shapes(file_grid[0, 0]) cam_size = cam_size[1:3] img_size = shift_grid[-1, -1] + cam_size buff = np.zeros([1, 1]) last_none = False if method2 is None: for (y, x), value in np.ndenumerate(file_grid): if (value != None and frame < g_shapes(value)[0]): prj, flt, drk = read_aps_32id_adaptive(value, proj=(frame, frame + 1)) prj = tomopy.normalize(prj, flt, drk) prj = preprocess(prj, blur=blur) t0 = time.time() buff = blend(buff, np.squeeze(prj), shift_grid[y, x, :], method=method, color_correction=color_correction, **blend_options) print('Rank: {:d}; Frame: {:d}; Pos: ({:d}, {:d}); Method: {:s}; Color Corr.:{:b}; Tile stitched in ' '{:.2f} s.'.format(rank, frame, y, x, method, color_correction, time.time()-t0)) if last_none: buff[margin:, margin:-margin][np.isnan(buff[margin:, margin:-margin])] = 0 last_none = False else: last_none = True else: for y in range(file_grid.shape[0]): temp_grid = file_grid[y:y+1, :] temp_shift = np.copy(shift_grid[y:y+1, :, :]) offset = np.min(temp_shift[:, :, 0]) temp_shift[:, :, 0] = temp_shift[:, :, 0] - offset row_buff = np.zeros([1, 1]) prj, flt, drk = read_aps_32id_adaptive(temp_grid[0, 0], proj=(frame, frame + 1)) prj = tomopy.normalize(prj, flt, drk) prj = preprocess(prj, blur=blur) row_buff, _ = arrange_image(row_buff, np.squeeze(prj), temp_shift[0, 0, :], order=1) for x in range(1, temp_grid.shape[1]): value = temp_grid[0, x] if (value != None and frame < g_shapes(value)[0]): prj, flt, drk = read_aps_32id_adaptive(value, proj=(frame, frame + 1)) prj = tomopy.normalize(prj, flt, drk) prj = preprocess(prj, blur=blur) t0 = time.time() row_buff = blend(row_buff, np.squeeze(prj), temp_shift[0, x, :], method=method, color_correction=color_correction, **blend_options) print('Rank: {:d}; Frame: {:d}; Pos: ({:d}, {:d}); Method: {:s}; Color Corr.:{:b}; Tile stitched in ' '{:.2f} s.'.format(rank, frame, y, x, method, color_correction, time.time() - t0)) if last_none: row_buff[margin:, margin:-margin][np.isnan(row_buff[margin:, margin:-margin])] = 0 last_none = False else: last_none = True t0 = time.time() buff = blend(buff, row_buff, [offset, 0], method=method2, color_correction=False, **blend_options2) print('Rank: {:d}; Frame: {:d}; Row: {:d}; Row stitched in {:.2f} s.'.format(rank, frame, y, time.time()-t0)) print('Rank: {:d}; Frame: {:d}; Panorama built in {:.2f} s.'.format(rank, frame, time.time()-t00)) os.chdir(root) return buff
def rec_test(file_name, sino_start, sino_end): print "\n#### Processing " + file_name sino_start = sino_start + 200 sino_end = sino_start + 2 print "Test reconstruction of slice [%d]" % sino_start # Read HDF5 file. prj, flat, dark = tomopy.io.exchange.read_aps_32id(file_name, sino=(sino_start, sino_end)) # Manage the missing angles: theta = tomopy.angles(prj.shape[0]) prj = np.concatenate((prj[0 : miss_angles[0], :, :], prj[miss_angles[1] + 1 : -1, :, :]), axis=0) theta = np.concatenate((theta[0 : miss_angles[0]], theta[miss_angles[1] + 1 : -1])) # normalize the prj prj = tomopy.normalize(prj, flat, dark) # reconstruct rec = tomopy.recon(prj, theta, center=best_center, algorithm="gridrec", emission=False) # Write data as stack of TIFs. tomopy.io.writer.write_tiff_stack(rec, fname=output_name) print "Slice saved as [%s_00000.tiff]" % output_name # show the reconstructed slice pl.gray() pl.axis("off") pl.imshow(rec[0])
def main(argv): try: opts, args = getopt.getopt(argv,"hc:s:",["core=","sino="]) except getopt.GetoptError: print 'test.py -c <ncore> -s <nsino>' sys.exit(2) for opt, arg in opts: if opt == '-h': print 'test.py -c <ncore> -s <nsino>' sys.exit() elif opt in ("-c", "--core"): ncore = int(arg) elif opt in ("-s", "--sino"): nsino = int(arg) file_name = '/local/decarlo/data/proj_10.hdf' output_name = './recon/proj10_rec' sino_start = 200 # Read HDF5 file. prj, flat, dark = tomopy.io.exchange.read_aps_32id(file_name, sino=(sino_start, sino_start+nsino)) # Fix flats because sample did not move flat = np.full((flat.shape[0], flat.shape[1], flat.shape[2]), 1000) # Set angles theta = tomopy.angles(prj.shape[0])
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=0, type=int, default=0, help="index of the first image: 10001 (default 0)") args = parser.parse_args() top = args.top # Select the sinogram range to reconstruct. start = 290 end = 294 print(top) # Read the Australian Synchrotron Facility data proj, flat, dark = dxchange.read_aps_5bm(top) # proj, flat, dark = dxchange.read_aps_5bm(fname, sino=(start, end)) slider(proj) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(proj.shape[0]) # Flat-field correction of raw data. proj = tomopy.normalize(proj, flat, dark) slider(proj)
def rec_test(file_name, sino_start, sino_end, astra_method, extra_options, num_iter=1): print '\n#### Processing '+ file_name sino_start = sino_start + 200 sino_end = sino_start + 2 print "Test reconstruction of slice [%d]" % sino_start # Read HDF5 file. prj, flat, dark = tomopy.io.exchange.read_aps_32id(file_name, sino=(sino_start, sino_end)) # Manage the missing angles: theta = tomopy.angles(prj.shape[0]) prj = np.concatenate((prj[0:miss_angles[0],:,:], prj[miss_angles[1]+1:-1,:,:]), axis=0) theta = np.concatenate((theta[0:miss_angles[0]], theta[miss_angles[1]+1:-1])) # normalize the prj prj = tomopy.normalize(prj, flat, dark) # remove ring artefacts prjn = tomopy.remove_stripe_fw(prj) # reconstruct rec = tomopy.recon(prj[:,::reduce_amount,::reduce_amount], theta, center=float(best_center)/reduce_amount, algorithm=tomopy.astra, options={'proj_type':proj_type,'method':astra_method,'extra_options':extra_options,'num_iter':num_iter}, emission=False) # Write data as stack of TIFs. tomopy.io.writer.write_tiff_stack(rec, fname=output_name) print "Slice saved as [%s_00000.tiff]" % output_name
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) # 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)
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 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) # 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 main(): #**************************************************************************** file_name = '/local/dataraid/databank/dataExchange/tmp/Australian_rank3.h5' output_name = '/local/dataraid/databank/dataExchange/tmp/rec/Australian_rank3' sino_start = 290 sino_end = 294 # Read HDF5 file. exchange_rank = 3; prj, flat, dark = tomopy.io.exchange.read_aps_32id(file_name, exchange_rank, sino=(sino_start, sino_end)) theta = tomopy.angles(prj.shape[0]) # normalize the data prj = tomopy.normalize(prj, flat, dark) best_center=1184 print "Best Center: ", best_center calc_center = best_center #calc_center = tomopy.find_center(prj, theta, emission=False, ind=0, init=best_center, tol=0.8) print "Calculated Center:", calc_center # reconstruct rec = tomopy.recon(prj, theta, center=calc_center, algorithm='gridrec', emission=False) #rec = tomopy.circ_mask(rec, axis=0) # Write data as stack of TIFs. tomopy.io.writer.write_tiff_stack(rec, fname=output_name) plt.gray() plt.axis('off') plt.imshow(rec[0])
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 = 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 save_partial_frames(file_grid, save_folder, prefix, frame=0): for (y, x), value in np.ndenumerate(file_grid): print(value) if (value != None): prj, flt, drk = read_aps_32id_adaptive(value, proj=(frame, frame + 1)) prj = tomopy.normalize(prj, flt, drk) prj = preprocess(prj) fname = prefix + 'Y' + str(y).zfill(2) + '_X' + str(x).zfill(2) dxchange.write_tiff(np.squeeze(prj), fname=os.path.join(save_folder, 'partial_frames', 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 # 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 load_sino(filename, sino_n, normalize=True): print('Loading {:s}, slice {:d}'.format(filename, sino_n)) sino_n = int(sino_n) sino, flt, drk = read_aps_32id_adaptive(filename, sino=(sino_n, sino_n + 1)) if not normalize: flt[:, :, :] = flt.max() drk[:, :, :] = 0 sino = tomopy.normalize(sino, flt, drk) # 1st slice of each tile of some samples contains mostly abnormally large values which should be removed. if sino.max() > 1e2: sino[np.abs(sino) > 1] = 1 return np.squeeze(sino)
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 main(argv): try: opts, args = getopt.getopt(argv,"hc:s:",["core=","sino="]) except getopt.GetoptError: print 'test.py -c <ncore> -s <nsino>' sys.exit(2) for opt, arg in opts: if opt == '-h': print 'test.py -c <ncore> -s <nsino>' sys.exit() elif opt in ("-c", "--core"): ncore = int(arg) elif opt in ("-s", "--sino"): nsino = int(arg) # ********************************************** #file_name = '/local/decarlo/data/proj_10.hdf' #output_name = './recon/proj10_rec' #sino_start = 0 #sino_end = 2048 # ********************************************** file_name = '/local/decarlo/data/Hornby_APS_2011.h5' output_name = './recon/Hornby_APS_2011_' best_center=1024 sino_start = 0 sino_end = 1792 # ********************************************** step_00 = time.time() step_02_delta_total = 0 count = 0 while (sino_start <= (sino_end - nsino)): # Read HDF5 file. prj, flat, dark = tomopy.io.exchange.read_aps_32id(file_name, sino=(sino_start, sino_start+nsino)) # Fix flats because sample did not move flat = np.full((flat.shape[0], flat.shape[1], flat.shape[2]), 1000) # Set angles theta = tomopy.angles(prj.shape[0]) # normalize the prj prj = tomopy.normalize(prj, flat, dark) best_center = 1298 step_01 = time.time() # reconstruct rec = tomopy.recon(prj, theta, center=best_center, algorithm='gridrec', emission=False, ncore = ncore)
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: 10001 (default 1)") args = parser.parse_args() top = args.top index_start = int(args.start) template = os.listdir(top)[1] nfile = len(fnmatch.filter(os.listdir(top), '*.tif')) index_end = index_start + nfile ind_tomo = range(index_start, index_end) fname = top + template # Read the tiff raw data. rdata = dxchange.read_tiff_stack(fname, ind=ind_tomo) particle_bed_reference = particle_bed_location(rdata[0], plot=False) print("Particle bed location: ", particle_bed_reference) # Cut the images to remove the particle bed cdata = rdata[:, 0:particle_bed_reference, :] # Find the image when the shutter starts to close dark_index = shutter_off(rdata) print("shutter closes on image: ", dark_index) # Set the [start, end] index of the blocked images, flat and dark. flat_range = [0, 1] data_range = [48, dark_index] dark_range = [dark_index, nfile] # # for fast testing # data_range = [48, dark_index] flat = cdata[flat_range[0]:flat_range[1], :, :] proj = cdata[data_range[0]:data_range[1], :, :] dark = np.zeros((dark_range[1]-dark_range[0], proj.shape[1], proj.shape[2])) # if you want to use the shutter closed images as dark uncomment this: #dark = cdata[dark_range[0]:dark_range[1], :, :] ndata = tomopy.normalize(proj, flat, dark) ndata = tomopy.normalize_bg(ndata, air=ndata.shape[2]/2.5) ndata = tomopy.minus_log(ndata) sharpening(ndata) slider(ndata)
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 flat_correction(proj, flat, dark, params): log.info(' *** normalization') if (params.flat_correction_method == 'standard'): data = tomopy.normalize(proj, flat, dark, cutoff=params.normalization_cutoff) log.info(' *** *** ON %f cut-off' % params.normalization_cutoff) elif (params.flat_correction_method == 'air'): data = tomopy.normalize_bg(proj, air=params.air) log.info(' *** *** air %d pixels' % params.air) elif (params.flat_correction_method == 'none'): data = proj log.warning(' *** *** normalization is turned off') return data
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 save_partial_frames(file_grid, save_folder, prefix, frame=0, data_format='aps_32id'): for (y, x), value in np.ndenumerate(file_grid): print(value) if (value != None): prj, flt, drk, _ = read_data_adaptive(value, proj=(frame, frame + 1), data_format=data_format) prj = tomopy.normalize(prj, flt, drk) prj = preprocess(prj) fname = prefix + 'Y' + str(y).zfill(2) + '_X' + str(x).zfill(2) dxchange.write_tiff(np.squeeze(prj), fname=os.path.join(save_folder, 'partial_frames', fname))
def rec_full(file_name, sino_start, sino_end): print "\n#### Processing " + file_name chunks = 10 # number of data chunks for the reconstruction nSino_per_chunk = (sino_end - sino_start) / chunks print "Reconstructing [%d] slices from slice [%d] to [%d] in [%d] chunks of [%d] slices each" % ( (sino_end - sino_start), sino_start, sino_end, chunks, nSino_per_chunk, ) 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 # Read HDF5 file. prj, flat, dark = tomopy.io.exchange.read_aps_32id(file_name, sino=(sino_chunk_start, sino_chunk_end)) # Manage the missing angles: theta = tomopy.angles(prj.shape[0]) prj = np.concatenate((prj[0 : miss_angles[0], :, :], prj[miss_angles[1] + 1 : -1, :, :]), axis=0) theta = np.concatenate((theta[0 : miss_angles[0]], theta[miss_angles[1] + 1 : -1])) # normalize the prj prj = tomopy.normalize(prj, flat, dark) # reconstruct rec = tomopy.recon(prj, theta, center=best_center, algorithm="gridrec", emission=False) print output_name # Write data as stack of TIFs. tomopy.io.writer.write_tiff_stack(rec, fname=output_name, start=sino_chunk_start)
def rec_full(file_name, sino_start, sino_end, astra_method, extra_options, num_iter=1): print '\n#### Processing '+ file_name chunks = 10 # number of data chunks for the reconstruction nSino_per_chunk = (sino_end - sino_start)/chunks print "Reconstructing [%d] slices from slice [%d] to [%d] in [%d] chunks of [%d] slices each" % ((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 # Read HDF5 file. prj, flat, dark = tomopy.io.exchange.read_aps_32id(file_name, sino=(sino_chunk_start, sino_chunk_end)) # Manage the missing angles: theta = tomopy.angles(prj.shape[0]) prj = np.concatenate((prj[0:miss_angles[0],:,:], prj[miss_angles[1]+1:-1,:,:]), axis=0) theta = np.concatenate((theta[0:miss_angles[0]], theta[miss_angles[1]+1:-1])) # normalize the prj prj = tomopy.normalize(prj, flat, dark) # remove ring artefacts prj = tomopy.remove_stripe_fw(prj) # reconstruct rec = tomopy.recon(prj[:,::reduce_amount,::reduce_amount], theta, center=float(best_center)/reduce_amount, algorithm=tomopy.astra, options={'proj_type':proj_type,'method':astra_method,'extra_options':extra_options,'num_iter':num_iter}, emission=False) print output_name # Write data as stack of TIFs. tomopy.io.writer.write_tiff_stack(rec, fname=output_name, start=strt) strt += prj[:,::reduce_amount,:].shape[1]
def preprocess_13bm(fname, dark_value=None, zinger_threshold=0.2, zinger_filter_size=3): logging.basicConfig( #filename=fname + '_preprocess.log', #filemode='w', format='%(asctime)s %(levelname)-8s %(message)s', level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S') logging.info('Reading data') proj, flat, dark, theta = dxchange.read_aps_13bm(fname + '.h5', 'hdf5') logging.info('proj.shape: %s', proj.shape) logging.info('flat.shape: %s', flat.shape) logging.info('dark.shape: %s', dark.shape) if (dark_value != None): dark = dark * 0 + dark_value logging.info('dark[0]: %s', dark.item(0)) logging.info('last theta: %s', theta[-1]) logging.info('Normalizing') proj = tomopy.normalize(proj, flat, dark) logging.info("After normalizing, min=%f, max=%f", proj.min(), proj.max()) logging.info('Removing zingers. threshold=%f, size=%d', zinger_threshold, zinger_filter_size) proj = tomopy.misc.corr.remove_outlier(proj, zinger_threshold, size=zinger_filter_size) logging.info("After remove_outlier, min=%f, max=%f", proj.min(), proj.max()) logging.info('Converting to integer') proj = (10000. * proj).astype(np.int16) logging.info('Writing volume file') dxchange.write_netcdf4(proj, fname=fname + '.volume') logging.info('Volume file written')
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 flat_correction(proj, flat, dark, params): log.info(' *** normalization') if(params.flat_correction_method == 'standard'): try: data = tomopy.normalize(proj, flat, dark, cutoff=params.normalization_cutoff / params.bright_exp_ratio) data *= params.bright_exp_ratio except AttributeError: log.warning(' *** *** No bright_exp_ratio found. Ignore') log.info(' *** *** ON %f cut-off' % params.normalization_cutoff) elif(params.flat_correction_method == 'air'): data = tomopy.normalize_bg(proj, air=params.air) log.info(' *** *** air %d pixels' % params.air) elif(params.flat_correction_method == 'none'): data = proj log.warning(' *** *** normalization is turned off') else: raise ValueError("Unknown value for *flat_correction_method*: {}. " "Valid options are {}" "".format(params.flat_correction_method, config.SECTIONS['flat-correction']['flat-correction-method']['choices'])) return data
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, 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 fast_tomo_recon(argv): """ Reconstruct subset slices (sinograms) equally spaced within tomographic dataset """ logger = logging.getLogger('fast_tomopy.fast_tomo_recon') # Parse arguments passed to function parser = argparse.ArgumentParser() parser.add_argument('-i', '--input', type=str, help='path to input raw ' 'dataset', required=True) parser.add_argument('-o', '--output-file', type=str, help='full path to h5 output ' 'file', default=os.path.join(os.getcwd(), "fast-tomopy.h5")) parser.add_argument('-sn', '--sino-num', type=int, help='Number of slices ' 'to reconstruct', default=5) parser.add_argument('-a', '--algorithm', type=str, help='Reconstruction' ' algorithm', default='gridrec', choices=[ 'art', 'bart', 'fbp', 'gridrec', 'mlem', 'ospml_hybrid', 'ospml_quad', 'pml_hybrid', 'pml_quad', 'sirt' ]) parser.add_argument('-c', '--center', type=float, help='Center of rotation', default=None) parser.add_argument('-fn', '--filter-name', type=str, help='Name of filter' ' used for reconstruction', choices=[ 'none', 'shepp', 'cosine', 'hann', 'hamming', 'ramlak', 'parzen', 'butterworth' ], default='butterworth') parser.add_argument('-rr', '--ring-remove', type=str, help='Ring removal ' 'method', choices=['Octopus', 'Tomopy-FW', 'Tomopy-T'], default='Tomopy-T') parser.add_argument('-lf', '--log-file', type=str, help='log file name', default='fast-tomopy.log') args = parser.parse_args() fh = logging.FileHandler(args.log_file) fh.setLevel(logging.INFO) fh.setFormatter(formatter) logger.addHandler(fh) if os.path.isdir(os.path.dirname(args.output_file)) is False: raise IOError(2, 'Directory of output file does not exist', args.output_file) # Read file metadata logger.info('Reading input file metadata') fdata, gdata = read_als_832h5_metadata(args.input) proj_total = int(gdata['nangles']) last = proj_total - 1 sino_total = int(gdata['nslices']) ray_total = int(gdata['nrays']) px_size = float(gdata['pxsize']) / 10 # cm # Set parameters for sinograms to read step = sino_total // (args.sino_num + 2) start = step end = step * (args.sino_num + 1) sino = (start, end, step) # Read full first and last projection to determine center of rotation if args.center is None: logger.info('Reading full first and last projection for COR') first_last, flats, darks, foobar = dx.read_als_832h5(args.input, ind_tomo=(0, last)) first_last = tomopy.normalize(first_last, flats, darks) args.center = tomopy.find_center_pc(first_last[0, :, :], first_last[1, :, :], tol=0.1) logger.info('Detected center: %f', args.center) # Read and normalize raw sinograms logger.info('Reading raw data') tomo, flats, darks, foobar = dx.read_als_832h5(args.input, sino=sino) logger.info('Normalizing raw data') tomo = tomopy.normalize(tomo, flats, darks) tomo = tomopy.minus_log(tomo) # Remove stripes from sinograms (remove rings) logger.info('Preprocessing normalized data') if args.ring_remove == 'Tomopy-FW': logger.info('Removing stripes from sinograms with %s', args.ring_remove) tomo = tomopy.remove_stripe_fw(tomo) elif args.ring_remove == 'Tomopy-T': logger.info('Removing stripes from sinograms with %s', args.ring_remove) tomo = tomopy.remove_stripe_ti(tomo) # Pad sinograms with edge values npad = int(np.ceil(ray_total * np.sqrt(2)) - ray_total) // 2 tomo = tomopy.pad(tomo, 2, npad=npad, mode='edge') args.center += npad # account for padding filter_name = np.array(args.filter_name, dtype=(str, 16)) theta = tomopy.angles(proj_total, 270, 90) logger.info('Reconstructing normalized data') # Reconstruct sinograms # rec = tomopy.minus_log(tomo, out=tomo) rec = tomopy.recon(tomo, theta, center=args.center, algorithm=args.algorithm, filter_name=filter_name) rec = tomopy.circ_mask(rec[:, npad:-npad, npad:-npad], 0) rec = rec / px_size # Remove rings from reconstruction if args.ring_remove == 'Octopus': logger.info('Removing rings from reconstructions with %s', args.ring_remove) thresh = float(gdata['ring_threshold']) thresh_max = float(gdata['upp_ring_value']) thresh_min = float(gdata['low_ring_value']) theta_min = int(gdata['max_arc_length']) rwidth = int(gdata['max_ring_size']) rec = tomopy.remove_rings(rec, center_x=args.center, thresh=thresh, thresh_max=thresh_max, thresh_min=thresh_min, theta_min=theta_min, rwidth=rwidth) # Write reconstruction data to new hdf5 file fdata['stage'] = 'fast-tomopy' fdata['stage_flow'] = '/raw/' + fdata['stage'] fdata['stage_version'] = 'fast-tomopy-0.1' # Generate a new uuid based on host ID and current time fdata['uuid'] = str(uuid.uuid1()) gdata['Reconstruction_Type'] = 'tomopy-gridrec' gdata['ring_removal_method'] = args.ring_remove gdata['rfilter'] = args.filter_name logger.info('Writing reconstructed data to h5 file') write_als_832h5(rec, args.input, fdata, gdata, args.output_file, step) return
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 for tomobank id from 00032 to 00056 # sample_detector_distance = 6.0 # detector_pixel_size_x = 0.65e-4 # monochromator_energy = 27.4 # Phase retrieval for tomobank id 00058 and tomobank id 00059 # sample_detector_distance = 6.0 # detector_pixel_size_x = 0.65e-4 # monochromator_energy = 27.4 # Phase retrieval for tomobank id 00060 and tomobank id 00063 # sample_detector_distance = 2.5 # detector_pixel_size_x = 0.65e-4 # monochromator_energy = 27.4 # Phase retrieval for tomobank id 00064 # sample_detector_distance = 0.8 # detector_pixel_size_x = 1.4e-4 # monochromator_energy = 55.0 # Phase retrieval for tomobank id 00065 # sample_detector_distance = 5.8 # detector_pixel_size_x = 1.4e-4 # monochromator_energy = 55.0 # Phase retrieval for tomobank id 00066 # sample_detector_distance = 15.8 # detector_pixel_size_x = 1.4e-4 # monochromator_energy = 55.0 # Phase retrieval for tomobank id 00067 # sample_detector_distance = 30.8 # detector_pixel_size_x = 1.4e-4 # monochromator_energy = 55.0 # Phase retrieval for tomobank id 00068 # sample_detector_distance = 15.0 # detector_pixel_size_x = 4.1e-4 # monochromator_energy = 14.0 # Phase retrieval for tomobank id 00069 # sample_detector_distance = 0.4 # detector_pixel_size_x = 3.7e-4 # monochromator_energy = 36.085 # Phase retrieval for tomobank id 00070 # sample_detector_distance = 5.0 # detector_pixel_size_x = 0.65e-4 # monochromator_energy = 24.999 # Phase retrieval for tomobank id 00071 # sample_detector_distance = 1.5 # detector_pixel_size_x = 0.65e-4 # monochromator_energy = 24.999 # Phase retrieval for tomobank id 00072 # sample_detector_distance = 1.5 # detector_pixel_size_x = 1.43e-4 # monochromator_energy = 20.0 # Phase retrieval for tomobank id 00073 # sample_detector_distance = 1.0 # detector_pixel_size_x = 0.74e-4 # monochromator_energy = 25.0 # Phase retrieval for tomobank id 00074 # sample_detector_distance = 1.0 # detector_pixel_size_x = 0.74e-4 # monochromator_energy = 25.0 # Phase retrieval for tomobank id 00075 # sample_detector_distance = 11.0 # detector_pixel_size_x = 1.43e-4 # monochromator_energy = 60 # Phase retrieval for tomobank id 00076 # sample_detector_distance = 9.0 # detector_pixel_size_x = 2.2e-4 # monochromator_energy = 65 # # 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 fast_tomo_recon(argv): """ Reconstruct subset slices (sinograms) equally spaced within tomographic dataset """ logger = logging.getLogger("fast_tomopy.fast_tomo_recon") # Parse arguments passed to function parser = argparse.ArgumentParser() parser.add_argument("-i", "--input", type=str, help="path to input raw " "dataset", required=True) parser.add_argument( "-o", "--output-file", type=str, help="full path to h5 output " "file", default=os.path.join(os.getcwd(), "fast-tomopy.h5"), ) parser.add_argument("-sn", "--sino-num", type=int, help="Number of slices " "to reconstruct", default=5) parser.add_argument( "-a", "--algorithm", type=str, help="Reconstruction" " algorithm", default="gridrec", choices=[ "art", "bart", "fbp", "gridrec", "mlem", "ospml_hybrid", "ospml_quad", "pml_hybrid", "pml_quad", "sirt", ], ) parser.add_argument("-c", "--center", type=float, help="Center of rotation", default=None) parser.add_argument( "-fn", "--filter-name", type=str, help="Name of filter" " used for reconstruction", choices=["none", "shepp", "cosine", "hann", "hamming", "ramlak", "parzen", "butterworth"], default="butterworth", ) parser.add_argument( "-rr", "--ring-remove", type=str, help="Ring removal " "method", choices=["Octopus", "Tomopy-FW", "Tomopy-T"], default="Tomopy-T", ) parser.add_argument("-lf", "--log-file", type=str, help="log file name", default="fast-tomopy.log") args = parser.parse_args() fh = logging.FileHandler(args.log_file) fh.setLevel(logging.INFO) fh.setFormatter(formatter) logger.addHandler(fh) if os.path.isdir(os.path.dirname(args.output_file)) is False: raise IOError(2, "Directory of output file does not exist", args.output_file) # Read file metadata logger.info("Reading input file metadata") fdata, gdata = read_als_832h5_metadata(args.input) proj_total = int(gdata["nangles"]) last = proj_total - 1 sino_total = int(gdata["nslices"]) ray_total = int(gdata["nrays"]) px_size = float(gdata["pxsize"]) / 10 # cm # Set parameters for sinograms to read step = sino_total // (args.sino_num + 2) start = step end = step * (args.sino_num + 1) sino = (start, end, step) # Read full first and last projection to determine center of rotation if args.center is None: logger.info("Reading full first and last projection for COR") first_last, flats, darks, floc = tomopy.read_als_832h5(args.input, ind_tomo=(0, last)) first_last = tomopy.normalize(first_last, flats, darks) args.center = tomopy.find_center_pc(first_last[0, :, :], first_last[1, :, :], tol=0.1) logger.info("Detected center: %f", args.center) # Read and normalize raw sinograms logger.info("Reading raw data") tomo, flats, darks, floc = tomopy.read_als_832h5(args.input, sino=sino) logger.info("Normalizing raw data") tomo = tomopy.normalize_nf(tomo, flats, darks, floc) # Remove stripes from sinograms (remove rings) logger.info("Preprocessing normalized data") if args.ring_remove == "Tomopy-FW": logger.info("Removing stripes from sinograms with %s", args.ring_remove) tomo = tomopy.remove_stripe_fw(tomo) elif args.ring_remove == "Tomopy-T": logger.info("Removing stripes from sinograms with %s", args.ring_remove) tomo = tomopy.remove_stripe_ti(tomo) # Pad sinograms with edge values npad = int(np.ceil(ray_total * np.sqrt(2)) - ray_total) // 2 tomo = tomopy.pad(tomo, 2, npad=npad, mode="edge") args.center += npad # account for padding filter_name = np.array(args.filter_name, dtype=(str, 16)) theta = tomopy.angles(proj_total, 270, 90) logger.info("Reconstructing normalized data") # Reconstruct sinograms # rec = tomopy.minus_log(tomo, out=tomo) rec = tomopy.recon( tomo, theta, center=args.center, emission=False, algorithm=args.algorithm, filter_name=filter_name ) rec = tomopy.circ_mask(rec[:, npad:-npad, npad:-npad], 0) rec = rec / px_size # Remove rings from reconstruction if args.ring_remove == "Octopus": logger.info("Removing rings from reconstructions with %s", args.ring_remove) thresh = float(gdata["ring_threshold"]) thresh_max = float(gdata["upp_ring_value"]) thresh_min = float(gdata["low_ring_value"]) theta_min = int(gdata["max_arc_length"]) rwidth = int(gdata["max_ring_size"]) rec = tomopy.remove_rings( rec, center_x=args.center, thresh=thresh, thresh_max=thresh_max, thresh_min=thresh_min, theta_min=theta_min, rwidth=rwidth, ) # Write reconstruction data to new hdf5 file fdata["stage"] = "fast-tomopy" fdata["stage_flow"] = "/raw/" + fdata["stage"] fdata["stage_version"] = "fast-tomopy-0.1" # Generate a new uuid based on host ID and current time fdata["uuid"] = str(uuid.uuid1()) gdata["Reconstruction_Type"] = "tomopy-gridrec" gdata["ring_removal_method"] = args.ring_remove gdata["rfilter"] = args.filter_name logger.info("Writing reconstructed data to h5 file") write_als_832h5(rec, args.input, fdata, gdata, args.output_file, step) return
# exchange_rank = ExchangeRank, # slices_start=slice_first, # slices_end=slice_first+1) exchange_rank = ExchangeRank data, white, dark = tomopy.io.exchange.read_aps_32id(file_name, exchange_rank, sino=(slice_first, slice_first+4)) theta = tomopy.angles(data.shape[0]) # Xtomo object creation and pipeline of methods. ##d = tomopy.xtomo_dataset(log='debug') ##d.dataset(data, white, dark, theta) #if perform_norm: d.normalize() # flat & dark field correction if perform_norm: data = tomopy.normalize(data, white, dark) ##if drift_correct: d.correct_drift() if drift_correct: data = tomopy.normalize_bg(data) #d.median_filter(size=medfilt_size, axis=0) # Apply a median filter in the projection plane data = tomopy.median_filter(data, size=medfilt_size, axis=0) #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
def transform_scalars(dataset, rot_center=0, tune_rot_center=True): """Reconstruct sinograms using the tomopy gridrec algorithm Typically, a data exchange file would be loaded for this reconstruction. This operation will attempt to perform flat-field correction of the raw data using the dark and white background data found in the data exchange file. This operator also requires either the tomviz/tomopy-pipeline docker image, or a python environment with tomopy installed. """ from tomviz import utils import numpy as np import tomopy # Get the current volume as a numpy array. array = utils.get_array(dataset) dark = dataset.dark white = dataset.white angles = utils.get_tilt_angles(dataset) tilt_axis = dataset.tilt_axis # TomoPy wants the tilt axis to be zero, so ensure that is true if tilt_axis == 2: order = [2, 1, 0] array = np.transpose(array, order) if dark is not None and white is not None: dark = np.transpose(dark, order) white = np.transpose(white, order) if angles is not None: # tomopy wants radians theta = np.radians(angles) else: # Assume it is equally spaced between 0 and 180 degrees theta = tomopy.angles(array.shape[0]) # Perform flat-field correction of raw data if white is not None and dark is not None: array = tomopy.normalize(array, white, dark, cutoff=1.4) if rot_center == 0: # Try to find it automatically init = array.shape[2] / 2.0 rot_center = tomopy.find_center(array, theta, init=init, ind=0, tol=0.5) elif tune_rot_center: # Tune the center rot_center = tomopy.find_center(array, theta, init=rot_center, ind=0, tol=0.5) # Calculate -log(array) array = tomopy.minus_log(array) # Remove nan, neg, and inf values array = tomopy.remove_nan(array, val=0.0) array = tomopy.remove_neg(array, val=0.00) array[np.where(array == np.inf)] = 0.00 # Perform the reconstruction array = tomopy.recon(array, theta, center=rot_center, algorithm='gridrec') # Mask each reconstructed slice with a circle. array = tomopy.circ_mask(array, axis=0, ratio=0.95) # Set the transformed array child = utils.make_child_dataset(dataset) utils.mark_as_volume(child) utils.set_array(child, array) return_values = {} return_values['reconstruction'] = child return return_values
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 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)
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) #data = tomopy.prep.stripe.remove_stripe_sf(data,size=51) # phase retrieval data = tomopy.prep.phase.retrieve_phase( data, pixel_size=detector_pixel_size_x, dist=sample_detector_distance,
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
def center(io_paras, data_paras, center_start, center_end, center_step, diag_cycle=0, mode='diag', normalize=True, stripe_removal=10, phase_retrieval=False): # Input and output datafile = io_paras.get('datafile') path2white = io_paras.get('path2white', datafile) path2dark = io_paras.get('path2dark', path2white) out_dir = io_paras.get('out_dir') diag_cent_dir = io_paras.get('diag_cent_dir', out_dir+"/center_diagnose/") recon_dir = io_paras.get('recon_dir', out_dir+"/recon/") out_prefix = io_paras.get('out_prefix', "recon_") # Parameters of dataset NumCycles = data_paras.get('NumCycles', 1) # Number of cycles used for recon ProjPerCycle = data_paras.get('ProjPerCycle') # Number of projections per cycle, N_theta cycle_offset = data_paras.get('cycle_offset', 0) # Offset in output cycle number proj_start = data_paras.get('proj_start', 0) # Starting projection of reconstruction proj_step = data_paras.get('proj_step') z_start = data_paras.get('z_start', 0) z_end = data_paras.get('z_end', z_start+1) z_step = data_paras.get('z_step') x_start = data_paras.get('x_start') x_end = data_paras.get('x_end', x_start+1) x_step = data_paras.get('x_step') white_start = data_paras.get('white_start') white_end = data_paras.get('white_end') dark_start = data_paras.get('dark_start') dark_end = data_paras.get('dark_end') # Set start and end of each subcycle projections_start = diag_cycle * ProjPerCycle + proj_start projections_end = projections_start + ProjPerCycle slice1 = slice(projections_start, projections_end, proj_step) slice2 = slice(z_start, z_end, z_step) slice3 = slice(x_start, x_end, x_step) slices = (slice1, slice2, slice3) white_slices = (slice(white_start, white_end), slice2, slice3) dark_slices = (slice(dark_start, dark_end), slice2, slice3) print("Running center diagnosis (projs %s to %s)" % (projections_start, projections_end)) # Read HDF5 file. print("Reading datafile %s..." % datafile, end="") sys.stdout.flush() data, white, dark = reader.read_aps_2bm(datafile, slices, white_slices, dark_slices, path2white=path2white, path2dark=path2dark) theta = gen_theta(data.shape[0]) print("Done!") print("Data shape = %s;\nwhite shape = %s;\ndark shape = %s." % (data.shape, white.shape, dark.shape)) ## Normalize dataset using data_white and data_dark if normalize: data = tomopy.normalize(data, white, dark, cutoff=None, ncore=_ncore, nchunk=None) ## Remove stripes caused by dead pixels in the detector if stripe_removal: data = tomopy.remove_stripe_fw(data, level=stripe_removal, wname='db5', sigma=2, pad=True, ncore=None, nchunk=None) # data = tomopy.remove_stripe_ti(data, nblock=0, alpha=1.5, # ncore=None, nchunk=None) # # Show preprocessed projection # plt.figure("%s-prep" % projections_start) # plt.imshow(d.data[0,:,:], cmap=cm.Greys_r) # plt.savefig(out_dir+"/preprocess/%s-prep.jpg" # % projections_start) # # plt.show() # continue ## Phase retrieval if phase_retrieval: data = tomopy.retrieve_phase(data, pixel_size=6.5e-5, dist=33, energy=30, alpha=1e-3, pad=True, ncore=_ncore, nchunk=None) ## Determine and set the center of rotation ### Using optimization method to automatically find the center # d.optimize_center() if 'opti' in mode: print("Optimizing center ...", end="") sys.stdout.flush() rot_center = tomopy.find_center(data, theta, ind=None, emission=True, init=None, tol=0.5, mask=True, ratio=1.) print("Done!") print("center = %s" % rot_center) ### Output the reconstruction results using a range of centers, ### and then manually find the optimal center. if 'diag' in mode: if not os.path.exists(diag_cent_dir): os.makedirs(diag_cent_dir) print("Testing centers ...", end="") sys.stdout.flush() tomopy.write_center(data, theta, dpath=diag_cent_dir, cen_range=[center_start, center_end, center_step], ind=None, emission=False, mask=False, ratio=1.) print("Done!")
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 = tomopy.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) # Reconstruct object using Gridrec algorithm. rec = tomopy.recon(proj, theta, center=1024, algorithm='gridrec', emission=False) # Write data as stack of TIFs. tomopy.write_tiff_stack(rec, fname='recon_dir/recon', start=sino_start)
def main(): args = parse_arguments() context = zmq.Context() # TQM setup if args.my_distributor_addr is not None: addr_split = re.split("://|:", args.my_distributor_addr) tmq.init_tmq() # Handshake w. remote processes print(addr_split) tmq.handshake(addr_split[1], int(addr_split[2]), args.num_sinograms, args.num_columns) else: print("No distributor..") # Subscriber setup print("Subscribing to: {}".format(args.data_source_addr)) subscriber_socket = context.socket(zmq.SUB) subscriber_socket.set_hwm(args.data_source_hwm) subscriber_socket.connect(args.data_source_addr) subscriber_socket.setsockopt(zmq.SUBSCRIBE, b'') # Local publisher socket if args.my_publisher_addr is not None: publisher_socket = context.socket(zmq.PUB) publisher_socket.set_hwm(200) # XXX publisher_socket.bind(args.my_publisher_addr) if args.data_source_synch_addr is not None: synchronize_subs(context, args.data_source_synch_addr) # Setup flatbuffer builder and serializer builder = flatbuffers.Builder(0) serializer = TraceSerializer.ImageSerializer(builder) # White/dark fields white_imgs = [] tot_white_imgs = 0 dark_imgs = [] tot_dark_imgs = 0 # Receive images total_received = 0 total_size = 0 seq = 0 time0 = time.time() while True: msg = subscriber_socket.recv() total_received += 1 total_size += len(msg) if msg == b"end_data": break # End of data acquisition # This is mostly for data rate tests if args.skip_serialize: print("Skipping rest. Received msg: {}".format(total_received)) continue # Deserialize msg to image read_image = serializer.deserialize(serialized_image=msg) serializer.info(read_image) # print image information # Local checks if args.check_seq: if seq != read_image.Seq(): print("Wrong sequence number: {} != {}".format( seq, read_image.Seq())) seq += 1 # Push image to workers (REQ/REP) my_image_np = read_image.TdataAsNumpy() if args.uint8_to_float32: my_image_np.dtype = np.uint8 sub = np.array(my_image_np, dtype="float32") elif args.uint16_to_float32: my_image_np.dtype = np.uint16 sub = np.array(my_image_np, dtype="float32") else: sub = my_image_np sub = sub.reshape((1, read_image.Dims().Y(), read_image.Dims().X())) # If incoming data is projection if read_image.Itype() is serializer.ITypes.Projection: rotation = read_image.Rotation() if args.degree_to_radian: rotation = rotation * math.pi / 180. # Tomopy operations expect 3D data, reshape incoming projections. if args.normalize: # flat/dark fields' corresponding rows if tot_white_imgs > 0 and tot_dark_imgs > 0: print( "normalizing: white_imgs.shape={}; dark_imgs.shape={}". format( np.array(white_imgs).shape, np.array(dark_imgs).shape)) sub = tp.normalize(sub, flat=white_imgs, dark=dark_imgs) if args.remove_stripes: print("removing stripes") sub = tp.remove_stripe_fw(sub, level=7, wname='sym16', sigma=1, pad=True) if args.mlog: print("applying -log") sub = -np.log(sub) if args.remove_invalids: print("removing invalids") sub = tp.remove_nan(sub, val=0.0) sub = tp.remove_neg(sub, val=0.00) sub[np.where(sub == np.inf)] = 0.00 # Publish to the world if (args.my_publisher_addr is not None) and (total_received % args.my_publisher_freq == 0): builder.Reset() serializer = TraceSerializer.ImageSerializer(builder) mub = np.reshape( sub, (read_image.Dims().Y(), read_image.Dims().X())) serialized_data = serializer.serialize( image=mub, uniqueId=0, rotation=0, itype=serializer.ITypes.Projection) print("Publishing:{}".format(read_image.UniqueId())) publisher_socket.send(serialized_data) # Send to workers if args.num_sinograms is not None: sub = sub[:, args.beg_sinogram:args.beg_sinogram + args.num_sinograms, :] ncols = sub.shape[2] sub = sub.reshape(sub.shape[0] * sub.shape[1] * sub.shape[2]) if args.my_distributor_addr is not None: tmq.push_image(sub, args.num_sinograms, ncols, rotation, read_image.UniqueId(), read_image.Center()) # If incoming data is white field if read_image.Itype() is serializer.ITypes.White: #print("White field data is received: {}".format(read_image.UniqueId())) white_imgs.extend(sub) tot_white_imgs += 1 # If incoming data is white-reset if read_image.Itype() is serializer.ITypes.WhiteReset: #print("White-reset data is received: {}".format(read_image.UniqueId())) white_imgs = [] white_imgs.extend(sub) tot_white_imgs += 1 # If incoming data is dark field if read_image.Itype() is serializer.ITypes.Dark: #print("Dark data is received: {}".format(read_image.UniqueId())) dark_imgs.extend(sub) tot_dark_imgs += 1 # If incoming data is dark-reset if read_image.Itype() is serializer.ITypes.DarkReset: #print("Dark-reset data is received: {}".format(read_image.UniqueId())) dark_imgs = [] dark_imgs.extend(sub) tot_dark_imgs += 1 time1 = time.time() # Profile information elapsed_time = time1 - time0 tot_MiBs = (total_size * 1.) / 2**20 print( "Received number of projections: {}; Total size (MiB): {:.2f}; Elapsed time (s): {:.2f}" .format(total_received, tot_MiBs, elapsed_time)) print("Rate (MiB/s): {:.2f}; (msg/s): {:.2f}".format( tot_MiBs / elapsed_time, total_received / elapsed_time)) # Finalize TMQ if not args.my_distributor_addr is not None: tmq.done_image() tmq.finalize_tmq()
cores = (1,) #range(mp.cpu_count(), 0, -4) f = open('benchmark_results.txt', 'a') for dataset in datasets: f.write('*****************************************************************************************************\n') f.write(dataset + '\n\n') for algorithm in algorithms: for sino in sinos: for core in cores: start_time = time.time() tomo, flats, darks, floc = tomopy.read_als_832h5(dataset, sino=(0, sino, 1)) end_time = time.time() - start_time f.write('Function: {0}, Number of sinos: {1}, Runtime (s): {2}\n'.format('read', sino, end_time)) theta = tomopy.angles(tomo.shape[0]) tomo = tomopy.normalize(tomo, flats, darks, ncore=core) end_time = time.time() - start_time - end_time f.write('Function: {0}, Number of sinos: {1}, Number of cores: {2}, Runtime (s): {3}\n'.format('normalize', sino, core, end_time)) tomo = tomopy.remove_stripe_fw(tomo, ncore=core) end_time = time.time() - start_time - end_time f.write('Function: {0}, Number of sinos: {1}, Number of cores: {2}, Runtime (s): {3}\n'.format('stripe_fw', sino, core, end_time)) rec = tomopy.recon(tomo, theta, center=datasets[dataset], algorithm=algorithm, emission=False, ncore=core) end_time = time.time() - start_time - end_time rec = tomopy.circ_mask(rec, 0) f.write('Function: {0}, Number of sinos: {1}, Number of cores: {2}, Algorithm: {3}, Runtime (s): {4}\n'.format('recon', sino, core, algorithm, end_time)) outname = os.path.join('.', '{0}_{1}_slices_{2}_cores_{3}'.format(dataset.split('.')[0], str(algorithm), str(sino), str(core)), dataset.split('.')[0]) tomopy.write_tiff_stack(rec, fname=outname) end_time = time.time() - start_time - end_time f.write('Function: {0}, Number of images: {1}, Runtime (s): {2}\n\n'.format('write', rec.shape[0], end_time))
break sino = (int(sino_chunk_start), int(sino_chunk_end)) # Read APS 2-BM raw data. if (int(key) > 6): proj, flat, dark, theta = read_aps_2bm_custom(fname, sino=sino) else: proj, flat, dark, theta = dxchange.read_aps_2bm(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 #proj = tomopy.remove_stripe_fw(proj,level=5,wname='sym16',sigma=1,pad=True) proj = tomopy.remove_stripe_ti(proj,2) proj = tomopy.remove_stripe_sf(proj,10) # 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(index, rot_center) proj = tomopy.minus_log(proj)
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") proj.scatter(0) tomopy.normalize(proj.local_arr, flat, dark, ncore=1, out=proj.local_arr) np.clip(proj.local_arr, 1e-6, 1.0, proj.local_arr) del flat, dark # Remove Stripe # NOTE: we need to change remove_strip_fw to take sinogram order data, since it internally rotates the data #proj.scatter(1) #proj.local_arr = tomopy.remove_stripe_fw(proj.local_arr, ncore=1) # Take the minus log to prepare for reconstruction #NOTE: no scatter required since minus_log doesn't care about order tomopy.minus_log(proj.local_arr, ncore=1, out=proj.local_arr) # Find rotation center per set of sinograms logger.info("Finding center of rotation") proj.scatter(1)
def recon( filename, inputPath='./', outputPath=None, outputFilename=None, doOutliers1D=False, # outlier removal in 1d (along sinogram columns) outlier_diff1D=750, # difference between good data and outlier data (outlier removal) outlier_size1D=3, # radius around each pixel to look for outliers (outlier removal) doOutliers2D=False, # outlier removal, standard 2d on each projection outlier_diff2D=750, # difference between good data and outlier data (outlier removal) outlier_size2D=3, # radius around each pixel to look for outliers (outlier removal) doFWringremoval=True, # Fourier-wavelet ring removal doTIringremoval=False, # Titarenko ring removal doSFringremoval=False, # Smoothing filter ring removal ringSigma=3, # damping parameter in Fourier space (Fourier-wavelet ring removal) ringLevel=8, # number of wavelet transform levels (Fourier-wavelet ring removal) ringWavelet='db5', # type of wavelet filter (Fourier-wavelet ring removal) ringNBlock=0, # used in Titarenko ring removal (doTIringremoval) ringAlpha=1.5, # used in Titarenko ring removal (doTIringremoval) ringSize=5, # used in smoothing filter ring removal (doSFringremoval) doPhaseRetrieval=False, # phase retrieval alphaReg=0.0002, # smaller = smoother (used for phase retrieval) propagation_dist=75, # sample-to-scintillator distance (phase retrieval) kev=24, # energy level (phase retrieval) butterworth_cutoff=0.25, #0.1 would be very smooth, 0.4 would be very grainy (reconstruction) butterworth_order=2, # for reconstruction doPolarRing=False, # ring removal Rarc=30, # min angle needed to be considered ring artifact (ring removal) Rmaxwidth=100, # max width of rings to be filtered (ring removal) Rtmax=3000.0, # max portion of image to filter (ring removal) Rthr=3000.0, # max value of offset due to ring artifact (ring removal) Rtmin=-3000.0, # min value of image to filter (ring removal) cor=None, # center of rotation (float). If not used then cor will be detected automatically corFunction='pc', # center of rotation function to use - can be 'pc', 'vo', or 'nm' voInd=None, # index of slice to use for cor search (vo) voSMin=-40, # min radius for searching in sinogram (vo) voSMax=40, # max radius for searching in sinogram (vo) voSRad=10, # search radius (vo) voStep=0.5, # search step (vo) voRatio=2.0, # ratio of field-of-view and object size (vo) voDrop=20, # drop lines around vertical center of mask (vo) nmInd=None, # index of slice to use for cor search (nm) nmInit=None, # initial guess for center (nm) nmTol=0.5, # desired sub-pixel accuracy (nm) nmMask=True, # if True, limits analysis to circular region (nm) nmRatio=1.0, # ratio of radius of circular mask to edge of reconstructed image (nm) nmSinoOrder=False, # if True, analyzes in sinogram space. If False, analyzes in radiograph space use360to180=False, # use 360 to 180 conversion doBilateralFilter=False, # if True, uses bilateral filter on image just before write step # NOTE: image will be converted to 8bit if it is not already bilateral_srad=3, # spatial radius for bilateral filter (image will be converted to 8bit if not already) bilateral_rrad=30, # range radius for bilateral filter (image will be converted to 8bit if not already) castTo8bit=False, # convert data to 8bit before writing cast8bit_min=-10, # min value if converting to 8bit cast8bit_max=30, # max value if converting to 8bit useNormalize_nf=False, # normalize based on background intensity (nf) chunk_proj=100, # chunk size in projection direction chunk_sino=100, # chunk size in sinogram direction npad=None, # amount to pad data before reconstruction projused=None, #should be slicing in projection dimension (start,end,step) sinoused=None, #should be sliceing in sinogram dimension (start,end,step). If first value is negative, it takes the number of slices from the second value in the middle of the stack. correcttilt=0, #tilt dataset tiltcenter_slice=None, # tilt center (x direction) tiltcenter_det=None, # tilt center (y direction) angle_offset=0, #this is the angle offset from our default (270) so that tomopy yields output in the same orientation as previous software (Octopus) anglelist=None, #if not set, will assume evenly spaced angles which will be calculated by the angular range and number of angles found in the file. if set to -1, will read individual angles from each image. alternatively, a list of angles can be passed. doBeamHardening=False, #turn on beam hardening correction, based on "Correction for beam hardening in computed tomography", Gabor Herman, 1979 Phys. Med. Biol. 24 81 BeamHardeningCoefficients=None, #6 values, tomo = a0 + a1*tomo + a2*tomo^2 + a3*tomo^3 + a4*tomo^4 + a5*tomo^5 projIgnoreList=None, #projections to be ignored in the reconstruction (for simplicity in the code, they will not be removed and will be processed as all other projections but will be set to zero absorption right before reconstruction. *args, **kwargs): start_time = time.time() print("Start {} at:".format(filename) + time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())) outputPath = inputPath if outputPath is None else outputPath outputFilename = filename if outputFilename is None else outputFilename tempfilenames = [outputPath + 'tmp0.h5', outputPath + 'tmp1.h5'] filenametowrite = outputPath + '/rec' + filename.strip( ".h5") + '/' + outputFilename #filenametowrite = outputPath+'/rec'+filename+'/'+outputFilename print("cleaning up previous temp files", end="") for tmpfile in tempfilenames: try: os.remove(tmpfile) except OSError: pass print(", reading metadata") datafile = h5py.File(inputPath + filename, 'r') gdata = dict(dxchange.reader._find_dataset_group(datafile).attrs) pxsize = float(gdata['pxsize']) / 10 # /10 to convert unites from mm to cm numslices = int(gdata['nslices']) numangles = int(gdata['nangles']) angularrange = float(gdata['arange']) numrays = int(gdata['nrays']) npad = int(np.ceil(numrays * np.sqrt(2)) - numrays) // 2 if npad is None else npad projused = (0, numangles - 1, 1) if projused is None else projused # ndark = int(gdata['num_dark_fields']) # ind_dark = list(range(0, ndark)) # group_dark = [numangles - 1] inter_bright = int(gdata['i0cycle']) nflat = int(gdata['num_bright_field']) ind_flat = list(range(0, nflat)) if inter_bright > 0: group_flat = list(range(0, numangles, inter_bright)) if group_flat[-1] != numangles - 1: group_flat.append(numangles - 1) elif inter_bright == 0: group_flat = [0, numangles - 1] else: group_flat = None ind_tomo = list(range(0, numangles)) floc_independent = dxchange.reader._map_loc(ind_tomo, group_flat) #figure out the angle list (a list of angles, one per projection image) dtemp = datafile[list(datafile.keys())[0]] fltemp = list(dtemp.keys()) firstangle = float(dtemp[fltemp[0]].attrs.get('rot_angle', 0)) if anglelist is None: #the offset angle should offset from the angle of the first image, which is usually 0, but in the case of timbir data may not be. #we add the 270 to be inte same orientation as previous software used at bl832 angle_offset = 270 + angle_offset - firstangle anglelist = tomopy.angles(numangles, angle_offset, angle_offset - angularrange) elif anglelist == -1: anglelist = np.zeros(shape=numangles) for icount in range(0, numangles): anglelist[icount] = np.pi / 180 * (270 + angle_offset - float( dtemp[fltemp[icount]].attrs['rot_angle'])) #if projused is different than default, need to chnage numangles and angularrange #can't do useNormalize_nf and doOutliers2D at the same time, or doOutliers2D and doOutliers1D at the same time, b/c of the way we chunk, for now just disable that if useNormalize_nf == True and doOutliers2D == True: useNormalize_nf = False print( "we cannot currently do useNormalize_nf and doOutliers2D at the same time, turning off useNormalize_nf" ) if doOutliers2D == True and doOutliers1D == True: doOutliers1D = False print( "we cannot currently do doOutliers1D and doOutliers2D at the same time, turning off doOutliers1D" ) #figure out how user can pass to do central x number of slices, or set of slices dispersed throughout (without knowing a priori the value of numslices) if sinoused is None: sinoused = (0, numslices, 1) elif sinoused[0] < 0: sinoused = ( int(np.floor(numslices / 2.0) - np.ceil(sinoused[1] / 2.0)), int(np.floor(numslices / 2.0) + np.floor(sinoused[1] / 2.0)), 1) num_proj_per_chunk = np.minimum(chunk_proj, projused[1] - projused[0]) numprojchunks = (projused[1] - projused[0] - 1) // num_proj_per_chunk + 1 num_sino_per_chunk = np.minimum(chunk_sino, sinoused[1] - sinoused[0]) numsinochunks = (sinoused[1] - sinoused[0] - 1) // num_sino_per_chunk + 1 numprojused = (projused[1] - projused[0]) // projused[2] numsinoused = (sinoused[1] - sinoused[0]) // sinoused[2] BeamHardeningCoefficients = ( 0, 1, 0, 0, 0, .1) if BeamHardeningCoefficients is None else BeamHardeningCoefficients if cor is None: print("Detecting center of rotation", end="") if angularrange > 300: lastcor = int(np.floor(numangles / 2) - 1) else: lastcor = numangles - 1 #I don't want to see the warnings about the reader using a deprecated variable in dxchange with warnings.catch_warnings(): warnings.simplefilter("ignore") tomo, flat, dark, floc = dxchange.read_als_832h5( inputPath + filename, ind_tomo=(0, lastcor)) tomo = tomo.astype(np.float32) if useNormalize_nf: tomopy.normalize_nf(tomo, flat, dark, floc, out=tomo) else: tomopy.normalize(tomo, flat, dark, out=tomo) if corFunction == 'vo': # same reason for catching warnings as above with warnings.catch_warnings(): warnings.simplefilter("ignore") cor = tomopy.find_center_vo(tomo, ind=voInd, smin=voSMin, smax=voSMax, srad=voSRad, step=voStep, ratio=voRatio, drop=voDrop) elif corFunction == 'nm': cor = tomopy.find_center( tomo, tomopy.angles(numangles, angle_offset, angle_offset - angularrange), ind=nmInd, init=nmInit, tol=nmTol, mask=nmMask, ratio=nmRatio, sinogram_order=nmSinoOrder) elif corFunction == 'pc': cor = tomopy.find_center_pc(tomo[0], tomo[1], tol=0.25) else: raise ValueError("\'corFunction\' must be one of: [ pc, vo, nm ].") print(", {}".format(cor)) else: print("using user input center of {}".format(cor)) function_list = [] if doOutliers1D: function_list.append('remove_outlier1d') if doOutliers2D: function_list.append('remove_outlier2d') if useNormalize_nf: function_list.append('normalize_nf') else: function_list.append('normalize') function_list.append('minus_log') if doBeamHardening: function_list.append('beam_hardening') if doFWringremoval: function_list.append('remove_stripe_fw') if doTIringremoval: function_list.append('remove_stripe_ti') if doSFringremoval: function_list.append('remove_stripe_sf') if correcttilt: function_list.append('correcttilt') if use360to180: function_list.append('do_360_to_180') if doPhaseRetrieval: function_list.append('phase_retrieval') function_list.append('recon_mask') if doPolarRing: function_list.append('polar_ring') if castTo8bit: function_list.append('castTo8bit') if doBilateralFilter: function_list.append('bilateral_filter') function_list.append('write_output') # Figure out first direction to slice for func in function_list: if slice_dir[func] != 'both': axis = slice_dir[func] break done = False curfunc = 0 curtemp = 0 while True: # Loop over reading data in certain chunking direction if axis == 'proj': niter = numprojchunks else: niter = numsinochunks for y in range(niter): # Loop over chunks print("{} chunk {} of {}".format(axis, y + 1, niter)) if curfunc == 0: with warnings.catch_warnings(): warnings.simplefilter("ignore") if axis == 'proj': tomo, flat, dark, floc = dxchange.read_als_832h5( inputPath + filename, ind_tomo=range( y * num_proj_per_chunk + projused[0], np.minimum( (y + 1) * num_proj_per_chunk + projused[0], numangles)), sino=(sinoused[0], sinoused[1], sinoused[2])) else: tomo, flat, dark, floc = dxchange.read_als_832h5( inputPath + filename, ind_tomo=range(projused[0], projused[1], projused[2]), sino=(y * num_sino_per_chunk + sinoused[0], np.minimum((y + 1) * num_sino_per_chunk + sinoused[0], numslices), 1)) else: if axis == 'proj': start, end = y * num_proj_per_chunk, np.minimum( (y + 1) * num_proj_per_chunk, numprojused) tomo = dxchange.reader.read_hdf5( tempfilenames[curtemp], '/tmp/tmp', slc=((start, end, 1), (0, numslices, 1), (0, numrays, 1))) #read in intermediate file else: start, end = y * num_sino_per_chunk, np.minimum( (y + 1) * num_sino_per_chunk, numsinoused) tomo = dxchange.reader.read_hdf5(tempfilenames[curtemp], '/tmp/tmp', slc=((0, numangles, 1), (start, end, 1), (0, numrays, 1))) dofunc = curfunc keepvalues = None while True: # Loop over operations to do in current chunking direction func_name = function_list[dofunc] newaxis = slice_dir[func_name] if newaxis != 'both' and newaxis != axis: # We have to switch axis, so flush to disk if y == 0: try: os.remove(tempfilenames[1 - curtemp]) except OSError: pass appendaxis = 1 if axis == 'sino' else 0 dxchange.writer.write_hdf5( tomo, fname=tempfilenames[1 - curtemp], gname='tmp', dname='tmp', overwrite=False, appendaxis=appendaxis) #writing intermediate file... break print(func_name, end=" ") curtime = time.time() if func_name == 'remove_outlier1d': tomo = tomo.astype(np.float32, copy=False) remove_outlier1d(tomo, outlier_diff1D, size=outlier_size1D, out=tomo) if func_name == 'remove_outlier2d': tomo = tomo.astype(np.float32, copy=False) tomopy.remove_outlier(tomo, outlier_diff2D, size=outlier_size2D, axis=0, out=tomo) elif func_name == 'normalize_nf': tomo = tomo.astype(np.float32, copy=False) tomopy.normalize_nf( tomo, flat, dark, floc_independent, out=tomo ) #use floc_independent b/c when you read file in proj chunks, you don't get the correct floc returned right now to use here. elif func_name == 'normalize': tomo = tomo.astype(np.float32, copy=False) tomopy.normalize(tomo, flat, dark, out=tomo) elif func_name == 'minus_log': mx = np.float32(0.00000000000000000001) ne.evaluate('where(tomo>mx, tomo, mx)', out=tomo) tomopy.minus_log(tomo, out=tomo) elif func_name == 'beam_hardening': loc_dict = { 'a{}'.format(i): np.float32(val) for i, val in enumerate(BeamHardeningCoefficients) } tomo = ne.evaluate( 'a0 + a1*tomo + a2*tomo**2 + a3*tomo**3 + a4*tomo**4 + a5*tomo**5', local_dict=loc_dict, out=tomo) elif func_name == 'remove_stripe_fw': tomo = tomopy.remove_stripe_fw(tomo, sigma=ringSigma, level=ringLevel, pad=True, wname=ringWavelet) elif func_name == 'remove_stripe_ti': tomo = tomopy.remove_stripe_ti(tomo, nblock=ringNBlock, alpha=ringAlpha) elif func_name == 'remove_stripe_sf': tomo = tomopy.remove_stripe_sf(tomo, size=ringSize) elif func_name == 'correcttilt': if tiltcenter_slice is None: tiltcenter_slice = numslices / 2. if tiltcenter_det is None: tiltcenter_det = tomo.shape[2] / 2 new_center = tiltcenter_slice - 0.5 - sinoused[0] center_det = tiltcenter_det - 0.5 #add padding of 10 pixels, to be unpadded right after tilt correction. This makes the tilted image not have zeros at certain edges, which matters in cases where sample is bigger than the field of view. For the small amounts we are generally tilting the images, 10 pixels is sufficient. # tomo = tomopy.pad(tomo, 2, npad=10, mode='edge') # center_det = center_det + 10 cntr = (center_det, new_center) for b in range(tomo.shape[0]): tomo[b] = st.rotate( tomo[b], correcttilt, center=cntr, preserve_range=True, order=1, mode='edge', clip=True ) #center=None means image is rotated around its center; order=1 is default, order of spline interpolation # tomo = tomo[:, :, 10:-10] elif func_name == 'do_360_to_180': # Keep values around for processing the next chunk in the list keepvalues = [ angularrange, numangles, projused, num_proj_per_chunk, numprojchunks, numprojused, numrays, anglelist ] #why -.5 on one and not on the other? if tomo.shape[0] % 2 > 0: tomo = sino_360_to_180( tomo[0:-1, :, :], overlap=int( np.round((tomo.shape[2] - cor - .5)) * 2), rotation='right') angularrange = angularrange / 2 - angularrange / ( tomo.shape[0] - 1) else: tomo = sino_360_to_180( tomo[:, :, :], overlap=int(np.round((tomo.shape[2] - cor)) * 2), rotation='right') angularrange = angularrange / 2 numangles = int(numangles / 2) projused = (0, numangles - 1, 1) num_proj_per_chunk = np.minimum(chunk_proj, projused[1] - projused[0]) numprojchunks = (projused[1] - projused[0] - 1) // num_proj_per_chunk + 1 numprojused = (projused[1] - projused[0]) // projused[2] numrays = tomo.shape[2] anglelist = anglelist[:numangles] elif func_name == 'phase_retrieval': tomo = tomopy.retrieve_phase(tomo, pixel_size=pxsize, dist=propagation_dist, energy=kev, alpha=alphaReg, pad=True) elif func_name == 'recon_mask': tomo = tomopy.pad(tomo, 2, npad=npad, mode='edge') if projIgnoreList is not None: for badproj in projIgnoreList: tomo[badproj] = 0 rec = tomopy.recon( tomo, anglelist, center=cor + npad, algorithm='gridrec', filter_name='butterworth', filter_par=[butterworth_cutoff, butterworth_order]) rec = rec[:, npad:-npad, npad:-npad] rec /= pxsize # convert reconstructed voxel values from 1/pixel to 1/cm rec = tomopy.circ_mask(rec, 0) elif func_name == 'polar_ring': rec = np.ascontiguousarray(rec, dtype=np.float32) rec = tomopy.remove_ring(rec, theta_min=Rarc, rwidth=Rmaxwidth, thresh_max=Rtmax, thresh=Rthr, thresh_min=Rtmin, out=rec) elif func_name == 'castTo8bit': rec = convert8bit(rec, cast8bit_min, cast8bit_max) elif func_name == 'bilateral_filter': rec = pyF3D.run_BilateralFilter( rec, spatialRadius=bilateral_srad, rangeRadius=bilateral_rrad) elif func_name == 'write_output': dxchange.write_tiff_stack(rec, fname=filenametowrite, start=y * num_sino_per_chunk + sinoused[0]) print('(took {:.2f} seconds)'.format(time.time() - curtime)) dofunc += 1 if dofunc == len(function_list): break if y < niter - 1 and keepvalues: # Reset original values for next chunk angularrange, numangles, projused, num_proj_per_chunk, numprojchunks, numprojused, numrays, anglelist = keepvalues curtemp = 1 - curtemp curfunc = dofunc if curfunc == len(function_list): break axis = slice_dir[function_list[curfunc]] print("cleaning up temp files") for tmpfile in tempfilenames: try: os.remove(tmpfile) except OSError: pass print("End Time: " + time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())) print('It took {:.3f} s to process {}'.format(time.time() - start_time, inputPath + filename))
def hdf5_retrieve_phase(src_folder, src_fname, dest_folder, dest_fname, method='paganin', corr_flat=False, dtype='float16', sino_range=None, **kwargs): src_fname = check_fname_ext(src_fname, 'h5') dest_fname = check_fname_ext(dest_fname, 'h5') o = h5py.File('{:s}/{:s}'.format(src_folder, src_fname)) dset_src = o['exchange/data'] n_frames = dset_src.shape[0] if rank == 0: if not os.path.exists(dest_folder): os.mkdir(dest_folder) if os.path.exists(dest_folder + '/' + dest_fname): print('Warning: File already exists. Continue anyway? (y/n) ') cont = six.moves.input() if cont in ['n', 'N']: exit() else: print('Old file will be overwritten.') os.remove(dest_folder + '/' + dest_fname) #f = make_empty_hdf5(dest_folder, dest_fname, dset_src.shape, dtype=dtype) f = h5py.File(dest_folder + '/' + dest_fname) comm.Barrier() if rank != 0: f = h5py.File(dest_folder + '/' + dest_fname) full_shape = dset_src.shape grp = f.create_group('exchange') if sino_range is None: dset_dest = grp.create_dataset('data', full_shape, dtype=dtype) dset_flat = grp.create_dataset('data_white', (1, full_shape[1], full_shape[2]), dtype=dtype) dset_dark = grp.create_dataset('data_dark', (1, full_shape[1], full_shape[2]), dtype=dtype) else: sino_start = sino_range[0] sino_end = sino_range[1] dset_dest = grp.create_dataset( 'data', (full_shape[0], (sino_end - sino_start), full_shape[2]), dtype=dtype) dset_flat = grp.create_dataset( 'data_white', (1, (sino_end - sino_start), full_shape[2]), dtype=dtype) dset_dark = grp.create_dataset( 'data_dark', (1, (sino_end - sino_start), full_shape[2]), dtype=dtype) dset_flat[:, :, :] = np.ones(dset_flat.shape, dtype=dtype) dset_dark[:, :, :] = np.zeros(dset_dark.shape, dtype=dtype) comm.Barrier() flt = o['exchange/data_white'].value drk = o['exchange/data_dark'].value print('Method: {:s}'.format(method), kwargs) alloc_set = allocate_mpi_subsets(n_frames, size) for frame in alloc_set[rank]: t0 = time.time() print(' Rank: {:d}; current frame: {:d}.'.format(rank, frame)) if sino_range is None: temp = dset_src[frame, :, :] else: sino_start = sino_range[0] sino_end = sino_range[1] temp = dset_src[frame, sino_start:sino_end, :] if corr_flat: temp = temp.reshape([1, temp.shape[0], temp.shape[1]]) temp = tomopy.normalize(temp, flt, drk) temp = preprocess(temp) temp = np.squeeze(temp) temp = retrieve_phase(temp, method=method, **kwargs) dset_dest[frame, :, :] = temp.astype(dtype) print(' Done in {:.2f}s. '.format(time.time() - t0)) f.close() comm.Barrier() return
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. 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) # 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] 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 recon(io_paras, data_paras, rot_center=None, normalize=True, stripe_removal=10, phase_retrieval=False, opt_center=False, diag_center=False, output="tiff"): # Input and output datafile = io_paras.get('datafile') path2white = io_paras.get('path2white', datafile) path2dark = io_paras.get('path2dark', path2white) out_dir = io_paras.get('out_dir') diag_cent_dir = io_paras.get('diag_cent_dir', out_dir+"/center_diagnose/") recon_dir = io_paras.get('recon_dir', out_dir+"/recon/") out_prefix = io_paras.get('out_prefix', "recon_") # Parameters of dataset NumCycles = data_paras.get('NumCycles', 1) # Number of cycles used for recon ProjPerCycle = data_paras.get('ProjPerCycle') # Number of projections per cycle, N_theta cycle_offset = data_paras.get('cycle_offset', 0) # Offset in output cycle number proj_start = data_paras.get('proj_start', 0) # Starting projection of reconstruction proj_step = data_paras.get('proj_step') z_start = data_paras.get('z_start', 0) z_end = data_paras.get('z_end', z_start+1) z_step = data_paras.get('z_step') x_start = data_paras.get('x_start') x_end = data_paras.get('x_end', x_start+1) x_step = data_paras.get('x_step') white_start = data_paras.get('white_start') white_end = data_paras.get('white_end') dark_start = data_paras.get('dark_start') dark_end = data_paras.get('dark_end') rot_center_copy = rot_center for cycle in xrange(NumCycles): # Set start and end of each cycle projections_start = cycle * ProjPerCycle + proj_start projections_end = projections_start + ProjPerCycle slice1 = slice(projections_start, projections_end, proj_step) slice2 = slice(z_start, z_end, z_step) slice3 = slice(x_start, x_end, x_step) slices = (slice1, slice2, slice3) white_slices = (slice(white_start, white_end), slice2, slice3) dark_slices = (slice(dark_start, dark_end), slice2, slice3) print("Running cycle #%s (projs %s to %s)" % (cycle, projections_start, projections_end)) # Read HDF5 file. print("Reading datafile %s..." % datafile, end="") sys.stdout.flush() data, white, dark = reader.read_aps_2bm(datafile, slices, white_slices, dark_slices, path2white=path2white, path2dark=path2dark) theta = gen_theta(data.shape[0]) print("Done!") print("Data shape = %s;\nwhite shape = %s;\ndark shape = %s." % (data.shape, white.shape, dark.shape)) ## Normalize dataset using data_white and data_dark if normalize: print("Normalizing data ...") # white = white.mean(axis=0).reshape(-1, *data.shape[1:]) # dark = dark.mean(axis=0).reshape(-1, *data.shape[1:]) # data = (data - dark) / (white - dark) data = tomopy.normalize(data, white, dark, cutoff=None, ncore=_ncore, nchunk=None)[...] ## Remove stripes caused by dead pixels in the detector if stripe_removal: print("Removing stripes ...") data = tomopy.remove_stripe_fw(data, level=stripe_removal, wname='db5', sigma=2, pad=True, ncore=_ncore, nchunk=None) # data = tomopy.remove_stripe_ti(data, nblock=0, alpha=1.5, # ncore=None, nchunk=None) # # Show preprocessed projection # plt.figure("%s-prep" % projections_start) # plt.imshow(d.data[0,:,:], cmap=cm.Greys_r) # plt.savefig(out_dir+"/preprocess/%s-prep.jpg" # % projections_start) # # plt.show() # continue ## Phase retrieval if phase_retrieval: print("Retrieving phase ...") data = tomopy.retrieve_phase(data, pixel_size=1e-4, dist=50, energy=20, alpha=1e-3, pad=True, ncore=_ncore, nchunk=None) ## Determine and set the center of rotation if opt_center or (rot_center == None): ### Using optimization method to automatically find the center # d.optimize_center() print("Optimizing center ...", end="") sys.stdout.flush() rot_center = tomopy.find_center(data, theta, ind=None, emission=True, init=None, tol=0.5, mask=True, ratio=1.) print("Done!") print("center = %s" % rot_center) if diag_center: ### Output the reconstruction results using a range of centers, ### and then manually find the optimal center. # d.diagnose_center() if not os.path.exists(diag_cent_dir): os.makedirs(diag_cent_dir) print("Testing centers ...", end="") sys.stdout.flush() tomopy.write_center(data, theta, dpath=diag_cent_dir, cen_range=[center_start, center_end, center_step], ind=None, emission=False, mask=False, ratio=1.) print("Done!") ## Flip odd frames if (cycle % 2): data[...] = data[...,::-1] rot_center = data.shape[-1] - rot_center_copy else: rot_center = rot_center_copy ## Reconstruction using FBP print("Running gridrec ...", end="") sys.stdout.flush() recon = tomopy.recon(data, theta, center=rot_center, emission=False, algorithm='gridrec', # num_gridx=None, num_gridy=None, filter_name='shepp', ncore=_ncore, nchunk=_nchunk) print("Done!") ## Collect background # if cycle == 0: # bg = recon # elif cycle < 4: # bg += recon # else: # recon -= bg/4. # Write to stack of TIFFs. if not os.path.exists(recon_dir): os.makedirs(recon_dir) out_fname = recon_dir+"/"+out_prefix+"t_%d" % (cycle + cycle_offset) if "hdf" in output: hdf_fname = out_fname + ".hdf5" print("Writing reconstruction output file %s..." % hdf_fname, end="") sys.stdout.flush() tomopy.write_hdf5(recon, fname=hdf_fname, gname='exchange', overwrite=False) print("Done!") if "tif" in output: tiff_fname = out_fname + ".tiff" print("Writing reconstruction tiff files %s ..." % tiff_fname, end="") sys.stdout.flush() tomopy.write_tiff_stack(recon, fname=tiff_fname, axis=0, digit=5, start=0, overwrite=False) print("Done!") if "bin" in output: bin_fname = out_fname + ".bin" print("Writing reconstruction to binary files %s..." % bin_fname, end="") sys.stdout.flush() recon.tofile(bin_fname)
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, 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 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: 10001 (default 1)") args = parser.parse_args() top = args.top index_start = int(args.start) # Total number of images to read nfile = len(fnmatch.filter(os.listdir(top), '*.tif')) # Read the raw data rdata = hspeed.load_raw(top, index_start) particle_bed_reference = hspeed.particle_bed_location(rdata[0], plot=False) print("Particle bed location: ", particle_bed_reference) # Cut the images to remove the particle bed cdata = rdata[:, 0:particle_bed_reference, :] # Find the image when the shutter starts to close dark_index = hspeed.shutter_off(rdata) print("Shutter CLOSED on image: ", dark_index) # Find the images when the laser is on laser_on_index = hspeed.laser_on(rdata, particle_bed_reference, alpha=1.00) print("Laser ON on image: ", laser_on_index) # Set the [start, end] index of the blocked images, flat and dark. laser_on_index = 47 flat_range = [0, 1] data_range = [laser_on_index, dark_index] dark_range = [dark_index, nfile] flat = cdata[flat_range[0]:flat_range[1], :, :] proj = cdata[data_range[0]:data_range[1], :, :] dark = np.zeros( (dark_range[1] - dark_range[0], proj.shape[1], proj.shape[2])) # if you want to use the shutter closed images as dark uncomment this: #dark = cdata[dark_range[0]:dark_range[1], :, :] # ndata = tomopy.normalize(proj, flat, dark) # ndata = tomopy.normalize_bg(ndata, air=ndata.shape[2]/2.5) # ndata = tomopy.minus_log(ndata) # hspeed.slider(ndata) # ndata = hspeed.scale_to_one(ndata) # ndata = hspeed.sobel_stack(ndata) # hspeed.slider(ndata) ndata = tomopy.normalize(proj, flat, dark) ndata = tomopy.normalize_bg(ndata, air=ndata.shape[2] / 2.5) ndata = tomopy.minus_log(ndata) blur_radius = 3.0 threshold = .04 nddata = hspeed.label(ndata, blur_radius, threshold) f = tp.locate(ndata[100, :, :], 41, invert=True) print(f.head) plt.figure() # make a new figure tp.annotate(f, ndata[100, :, :])
# Select the sinogram range to reconstruct. start = 800 end = 804 # Read the APS 1-ID raw data. proj, flat, dark = tomopy.io.exchange.read_anka_topotomo(fname, ind_tomo, ind_flat, ind_dark, sino=(start, end)) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(proj.shape[0], 0, 180) print proj.shape print flat.shape print dark.shape # Flat-field correction of raw data. proj = tomopy.normalize(proj, flat, dark) # Set rotation axis location manually. best_center = 993.825; rot_center = best_center # Find rotation center. #rot_center = tomopy.find_center(proj, theta, emission=False, init=best_center, ind=0, tol=0.3) print "Center of rotation:", rot_center # Reconstruct object using Gridrec algorithm. rec = tomopy.recon(proj, theta, center=rot_center, algorithm='gridrec', emission=False) # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)
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
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
# test angle = 0 print getFilename(data_folder + "/2012*_%.3f_*.fits", angle) # step = 0.85 angles = np.arange(0.0, 181.9 + step, step) list_data_files = map( lambda x: getFilename(data_folder + "/2012*_%.3f_*.fits", x), angles) data = [] for _file in list_data_files: data.append(pyfits.open(_file)[0].data) # now start processing # normalize print "* normalizing ..." proj = tomopy.normalize(data, ob_data, df_data) print " done." # checking print proj.shape theta = np.arange(0.0, 181.9 + step, step) print(theta) theta *= np.pi / 180. # calculate rotation center print "* finding center ..." rot_center = tomopy.find_center(proj, theta, emission=False, init=1024, tol=0.5) print " done." print("Center of rotation: ", rot_center)
print(scanlog_content) # Read raw data. prj, flat, dark, theta = p05.reco.get_rawdata(scanlog_content, raw_dir, verbose=True) print(prj.shape, flat.shape, dark.shape, theta.shape) # Select the sinogram range to reconstruct. start = 1024 end = 1024 prj = prj[:, [start,end], :] flat = flat[:, [start,end], :] dark = dark[:, [start,end], :] # Flat-field correction of raw data. data = tomopy.normalize(prj, 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,energy=monochromator_energy,alpha=8e-3,pad=True) # Set rotation center. rot_center = 1552 print(rot_center) data = tomopy.minus_log(data) # Use test_sirtfbp_iter = True to test which number of iterations is suitable for your dataset # Filters are saved in .mat files in "./¨
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