def run(f_input, f_output, stack_begin, stack_end, z_step): ''' f_input: first slice of a tiff stack f_output: output dir ''' data_generator = omni_generator(f_input, begin=stack_begin, end=stack_end, step=z_step) ref = omni_read(f_input, begin=stack_begin, end=stack_begin + 1) x1, y1 = ecdf(ref) #bin_edges = [0, 50, 100, 150, 200, 255] #quantiles = [y1[e] for e in bin_edges] quantiles = np.arange(0.1, 1, 0.1) bin_edges = [np.where(y1 > q)[0][0] for q in quantiles[:]] print(bin_edges) print(quantiles) for (i, data) in tqdm(data_generator, total=(stack_end - stack_begin) // z_step): #print(i,data.shape) L = data.shape[0] data_out = Parallel(n_jobs=L)( delayed(hist_norm)(data[i], bin_edges, quantiles, inplace=False) for i in range(L)) dxchange.write_tiff_stack(np.stack(data_out), os.path.join(f_output, 'data'), start=i, overwrite=True) return
def rec_full(h5fname, rot_center, algorithm, binning): data_shape = get_dx_dims(h5fname, 'data') # Select sinogram range to reconstruct. sino_start = 0 sino_end = data_shape[1] chunks = 6 # number of sinogram chunks to reconstruct # only one chunk at the time is reconstructed # allowing for limited RAM machines to complete a full 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 = np.int(sino_start + nSino_per_chunk*iChunk) sino_chunk_end = np.int(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)) # Reconstruct. rec = reconstruct(h5fname, sino, rot_center, binning, algorithm) # Write data as stack of TIFs. fname = os.path.dirname(h5fname) + '/' + os.path.splitext(os.path.basename(h5fname))[0]+ '_full_rec/' + 'recon' print("Reconstructions: ", fname) dxchange.write_tiff_stack(rec, fname=fname, start=strt) strt += sino[1] - sino[0]
def rec_full(h5fname, rot_center, algorithm, binning): data_shape = get_dx_dims(h5fname, 'data') # Select sinogram range to reconstruct. sino_start = 0 sino_end = data_shape[1] chunks = 6 # number of sinogram chunks to reconstruct # only one chunk at the time is reconstructed # allowing for limited RAM machines to complete a full 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 sino = (int(sino_chunk_start), int(sino_chunk_end)) # Reconstruct. rec = reconstruct(h5fname, sino, rot_center, binning, algorithm) # Write data as stack of TIFs. fname = os.path.dirname(h5fname) + '/' + os.path.splitext(os.path.basename(h5fname))[0]+ '_full_rec/' + 'recon' print("Reconstructions: ", fname) dxchange.write_tiff_stack(rec, fname=fname, start=strt) strt += sino[1] - sino[0]
def main(arg): fname = '/local/dataraid/elettra/Oak_16bit_slice343_all_repack.h5' # Read the hdf raw data. sino, sflat, sdark, th = dxchange.read_aps_32id(fname) slider(sino) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(sino.shape[1], ang1=0.0, ang2=180.0) print(sino.shape, sdark.shape, sflat.shape, theta.shape) # Quick normalization just to see something .... ndata = sino / float(np.amax(sino)) slider(ndata) # Find rotation center. rot_center = 962 binning = 1 ndata = tomopy.downsample(ndata, level=int(binning)) rot_center = rot_center/np.power(2, float(binning)) ndata = tomopy.minus_log(ndata) # Reconstruct object using Gridrec algorithm. rec = tomopy.recon(ndata, theta, center=rot_center, sinogram_order=True, algorithm='gridrec') # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) # Write data as stack of TIFs. dxchange.write_tiff_stack(rec, fname='recon_dir/recon')
def rec_slice(h5fname, nsino, rot_center, algorithm, binning): data_shape = get_dx_dims(h5fname, 'data') ssino = int(data_shape[1] * nsino) # Select sinogram range to reconstruct sino = None start = ssino end = start + 1 sino = (start, end) rec = reconstruct(h5fname, sino, rot_center, binning, algorithm) nframes = 8 for time_frame in range(0, nframes): fname = os.path.dirname(os.path.abspath(h5fname)) + '/' + os.path.splitext(os.path.basename( h5fname))[0] + '_rec_slice/' + 'recon' + str(time_frame) + '_' dxchange.write_tiff_stack(rec[time_frame], fname=fname) print("Rec: ", fname) print("Slice: ", start) fname = os.path.dirname(h5fname) + '/' + 'slice_rec/' + 'recon_' + os.path.splitext(os.path.basename(h5fname))[0] dxchange.write_tiff_stack(rec, fname=fname) print("Rec: ", fname) print("Slice: ", start)
def reconstruct_sirt(fname, sino_range, theta_st=0, theta_end=PI, n_epochs=200, output_folder=None, downsample=None, center=None): if output_folder is None: output_folder = 'sirt_niter_{}_ds_{}_{}_{}'.format(n_epochs, *downsample) t0 = time.time() print('Reading data...') prj, flt, drk, _ = dxchange.read_aps_32id(fname, sino=sino_range) print('Data reading: {} s'.format(time.time() - t0)) print('Data shape: {}'.format(prj.shape)) prj = tomopy.normalize(prj, flt, drk) prj = preprocess(prj) # scale up to prevent precision issue prj *= 1.e2 if downsample is not None: prj = tomopy.downsample(prj, level=downsample[0], axis=0) prj = tomopy.downsample(prj, level=downsample[1], axis=1) prj = tomopy.downsample(prj, level=downsample[2], axis=2) print('Downsampled shape: {}'.format(prj.shape)) n_theta = prj.shape[0] theta = np.linspace(theta_st, theta_end, n_theta) print('Starting reconstruction...') t0 = time.time() extra_options = {'MinConstraint': 0} options = {'proj_type': 'cuda', 'method': 'SIRT_CUDA', 'num_iter': n_epochs, 'extra_options': extra_options} res = tomopy.recon(prj, theta, center=center, algorithm=tomopy.astra, options=options) dxchange.write_tiff_stack(res, fname=os.path.join(output_folder, 'recon'), dtype='float32', overwrite=True) print('Reconstruction time: {} s'.format(time.time() - t0))
def rec_subset(h5fname, nsino, nframes, frame, nproj, binning, tv): data_size = get_dx_dims(h5fname, 'data') # Select sinogram range to reconstruct. ssino = int(data_size[1] * nsino) sino_start = ssino - 4 * pow(2, binning) sino_end = ssino + 4 * pow(2, binning) print("Reconstructing [%d] slices from slice [%d] to [%d]" % ((sino_end - sino_start), sino_start, sino_end)) sino = (int(sino_start), int(sino_end)) # Reconstruct. rec = reconstruct(h5fname, sino, nframes, frame, nproj, binning, tv) # Write data as stack of TIFs. for time_frame in range(0, nframes): fname = os.path.dirname( os.path.abspath(h5fname)) + '/' + os.path.splitext( os.path.basename(h5fname))[0] + '_rec_subset/' + 'recon' + str( frame - nframes / 2 + time_frame) + '_' print("Reconstructions: ", fname) dxchange.write_tiff_stack(rec[time_frame], fname=fname, start=sino_start)
def rec_slice(h5fname, nsino, rot_center, algorithm, binning): data_shape = get_dx_dims(h5fname, 'data') ssino = int(data_shape[1] * nsino) # Select sinogram range to reconstruct sino = None start = ssino end = start + 1 sino = (start, end) rec = reconstruct(h5fname, sino, rot_center, binning, algorithm) nframes = 8 for time_frame in range(0, nframes): fname = os.path.dirname( os.path.abspath(h5fname)) + '/' + os.path.splitext( os.path.basename(h5fname))[0] + '_rec_slice/' + 'recon' + str( time_frame) + '_' dxchange.write_tiff_stack(rec[time_frame], fname=fname) print("Rec: ", fname) print("Slice: ", start) fname = os.path.dirname( h5fname) + '/' + 'slice_rec/' + 'recon_' + os.path.splitext( os.path.basename(h5fname))[0] dxchange.write_tiff_stack(rec, fname=fname) print("Rec: ", fname) print("Slice: ", start)
def evaluate(self): dxchange.write_tiff_stack(self.recon.value, fname=self.fname.value, dtype=self.dtype.value, axis=self.axis.value, start=self.start.value, digit=self.digit.value, overwrite=self.overwrite.value)
def main(arg): parser = argparse.ArgumentParser() parser.add_argument("top", help="top directory where the tiff images are located: /data/") parser.add_argument("start", nargs='?', const=1, type=int, default=1, help="index of the first image: 1000 (default 1)") args = parser.parse_args() top = args.top index_start = int(args.start) template = os.listdir(top)[0] nfile = len(fnmatch.filter(os.listdir(top), '*.tif')) index_end = index_start + nfile ind_tomo = range(index_start, index_end) fname = top + template print (nfile, index_start, index_end, fname) # Select the sinogram range to reconstruct. start = 0 end = 512 sino=(start, end) # Read the tiff raw data. ndata = dxchange.read_tiff_stack(fname, ind=ind_tomo, slc=(sino, None)) # Normalize to 1 using the air counts ndata = tomopy.normalize_bg(ndata, air=5) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(ndata.shape[0]) ndata = tomopy.minus_log(ndata) # Set binning and number of iterations binning = 8 iters = 21 print("Original", ndata.shape) ndata = tomopy.downsample(ndata, level=binning, axis=1) # ndata = tomopy.downsample(ndata, level=binning, axis=2) print("Processing:", ndata.shape) fdir = 'aligned' + '/noblur_iter_' + str(iters) + '_bin_' + str(binning) print(fdir) cprj, sx, sy, conv = alignment.align_seq(ndata, theta, fdir=fdir, iters=iters, pad=(10, 10), blur=False, save=True, debug=True) np.save(fdir + '/shift_x', sx) np.save(fdir + '/shift_y', sy) # Write aligned projections as stack of TIFs. dxchange.write_tiff_stack(cprj, fname=fdir + '/radios/image')
def save_partial_raw(file_list, save_folder): for value in file_list: if (value != None): prj, flt, drk = read_aps_32id_adaptive(value, proj=(0, 1)) fname = value dxchange.write_tiff_stack(np.squeeze(flt), fname=os.path.join(save_folder, 'partial_flats', fname)) dxchange.write_tiff_stack(np.squeeze(drk), fname=os.path.join(save_folder, 'partial_darks', fname)) prj = prj.astype('float32') dxchange.write_tiff(np.squeeze(prj), fname=os.path.join(save_folder, 'partial_frames_raw', fname))
def main(arg): parser = argparse.ArgumentParser() parser.add_argument("top", help="top directory where the tiff images are located: /data/") parser.add_argument("start", nargs='?', const=1, type=int, default=1, help="index of the first image: 1000 (default 1)") args = parser.parse_args() top = args.top index_start = int(args.start) template = os.listdir(top)[0] nfile = len(fnmatch.filter(os.listdir(top), '*.tif')) index_end = index_start + nfile ind_tomo = range(index_start, index_end) fname = top + template print (nfile, index_start, index_end, fname) # Select the sinogram range to reconstruct. start = 0 end = 512 sino=(start, end) # Read the tiff raw data. ndata = dxchange.read_tiff_stack(fname, ind=ind_tomo, slc=(sino, None)) print(ndata.shape) binning = 8 ndata = tomopy.downsample(ndata, level=binning, axis=1) print(ndata.shape) # Normalize to 1 using the air counts ndata = tomopy.normalize_bg(ndata, air=5) ## slider(ndata) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(ndata.shape[0]) rot_center = 960 print("Center of rotation: ", rot_center) ndata = tomopy.minus_log(ndata) # Reconstruct object using Gridrec algorithm. rec = tomopy.recon(ndata, theta, center=rot_center, algorithm='gridrec') # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) # Write data as stack of TIFs. dxchange.write_tiff_stack(rec, fname='/local/dataraid/mark/rec/recon')
def reconstruct(sname, rot_center, ovlpfind, s_start, s_end): fname = dfolder + sname + '.h5' print(fname) start = s_start end = s_end chunks = 24 num_sino = (end - start) // chunks for m in range(chunks): sino_start = start + num_sino * m sino_end = start + num_sino * (m + 1) start_read_time = time.time() proj, flat, dark, thetat = dxchange.read_aps_2bm(fname, sino=(sino_start, sino_end)) print(' done read in %0.1f min' % ((time.time() - start_read_time) / 60)) dark = proj[9001:9002] flat = proj[0:1] proj = proj[1:9000] theta = tomopy.angles(proj.shape[0], 0., 360.) proj = tomopy.sino_360_to_180(proj, overlap=ovlpfind, rotation='right') proj = tomopy.remove_outlier(proj, dif=0.4) proj = tomopy.normalize_bg(proj, air=10) proj = tomopy.minus_log(proj) center = rot_center start_ring_time = time.time() proj = tomopy.remove_stripe_fw(proj, wname='sym5', sigma=4, pad=False) proj = tomopy.remove_stripe_sf(proj, size=3) print(' done pre-process in %0.1f min' % ((time.time() - start_ring_time) / 60)) start_phase_time = time.time() proj = tomopy.retrieve_phase(proj, pixel_size=detector_pixel_size_x, dist=sample_detector_distance, energy=energy, alpha=alpha, pad=True, ncore=None, nchunk=None) print(' done phase retrieval in %0.1f min' % ((time.time() - start_phase_time) / 60)) start_recon_time = time.time() rec = tomopy.recon(proj, theta, center=center, algorithm='gridrec', filter_name='ramalk') tomopy.circ_mask(rec, axis=0, ratio=0.95) print("Reconstructed", rec.shape) dxchange.write_tiff_stack(rec, fname=dfolder + '/' + sname + '/' + sname, overwrite=True, start=sino_start) print(' Chunk reconstruction done in %0.1f min' % ((time.time() - start_recon_time) / 60)) print("Done!")
def save_slice_images(self, save_path='data/sav/slices'): if not os.path.exists(save_path): os.makedirs(save_path) dxchange.write_tiff_stack(self.grid_delta, os.path.join(save_path, 'delta'), overwrite=True, dtype=np.float32) dxchange.write_tiff_stack(self.grid_beta, os.path.join(save_path, 'beta'), overwrite=True, dtype=np.float32)
def main(arg): parser = argparse.ArgumentParser() parser.add_argument( "top", help="top directory where the tiff images are located: /data/") parser.add_argument("start", nargs='?', const=1, type=int, default=1, help="index of the first image: 1000 (default 1)") args = parser.parse_args() top = args.top index_start = int(args.start) template = os.listdir(top)[0] nfile = len(fnmatch.filter(os.listdir(top), '*.tiff')) index_end = index_start + nfile ind_tomo = range(index_start, index_end) fname = top + template print(nfile, index_start, index_end, fname) # Select the sinogram range to reconstruct. start = 70 end = 72 sino = (start, end) # Read the tiff raw data. ndata = dxchange.read_tiff_stack(fname, ind=ind_tomo, slc=(sino, None)) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(ndata.shape[0]) rot_center = 251 print("Center of rotation: ", rot_center) #ndata = tomopy.minus_log(ndata) # Reconstruct object using Gridrec algorithm. rec = tomopy.recon(ndata, theta, center=rot_center, algorithm='gridrec') # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) # Write data as stack of TIFs. dxchange.write_tiff_stack(rec, fname='/local/dataraid/mark/rec/recon')
def 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 save_partial_raw(file_list, save_folder, data_format='aps_32id'): for value in file_list: if (value != None): prj, flt, drk, _ = read_data_adaptive(value, proj=(0, 1), data_format=data_format) fname = value dxchange.write_tiff_stack(np.squeeze(flt), fname=os.path.join( save_folder, 'partial_flats', fname)) dxchange.write_tiff_stack(np.squeeze(drk), fname=os.path.join( save_folder, 'partial_darks', fname)) prj = prj.astype('float32') dxchange.write_tiff(np.squeeze(prj), fname=os.path.join(save_folder, 'partial_frames_raw', fname))
def rec_slice(h5fname, nsino, rot_center, algorithm, binning): data_shape = get_dx_dims(h5fname, 'data') ssino = int(data_shape[1] * nsino) # Select sinogram range to reconstruct sino = None start = ssino end = start + 1 sino = (start, end) rec = reconstruct(h5fname, sino, rot_center, binning, algorithm) fname = os.path.dirname(h5fname) + os.sep + 'slice_rec/' + 'recon_' + os.path.splitext(os.path.basename(h5fname))[0] dxchange.write_tiff_stack(rec, fname=fname) print("Rec: ", fname) print("Slice: ", start)
def rec_slice(h5fname, nsino, rot_center, algorithm, binning): data_shape = get_dx_dims(h5fname, 'data') ssino = int(data_shape[1] * nsino) # Select sinogram range to reconstruct sino = None start = ssino end = start + 1 sino = (start, end) rec = reconstruct(h5fname, sino, rot_center, binning, algorithm) fname = os.path.dirname(h5fname) + os.sep + 'slice_rec/' + 'recon_' + os.path.splitext(os.path.basename(h5fname))[0] dxchange.write_tiff_stack(rec, fname=fname) print("Rec: ", fname) print("Slice: ", start)
def rec_full(h5fname, nframes, frame, nproj, binning, tv): data_size = get_dx_dims(h5fname, 'data') # Select sinogram range to reconstruct. sino_start = 0 sino_end = data_size[1] # number of sinogram chunks to reconstruct chunks = data_size[1] / (8 * pow(2, binning)) # only one chunk at the time is reconstructed # allowing for limited RAM machines to complete a full 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 sino = (int(sino_chunk_start), int(sino_chunk_end)) # Reconstruct. rec = reconstruct(h5fname, sino, nframes, frame, nproj, binning, tv) # Write data as stack of TIFs. for time_frame in range(0, nframes): fname = os.path.dirname( os.path.abspath(h5fname)) + '/' + os.path.splitext( os.path.basename(h5fname) )[0] + '_rec_full/' + 'recon' + str(frame - nframes / 2 + time_frame) + '_' print("Reconstructions: ", fname) dxchange.write_tiff_stack(rec[time_frame], fname=fname, start=strt) strt += (sino[1] - sino[0]) / pow(2, binning)
def rec_slice(h5fname, nsino, rot_center, blocked_views): 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) # Reconstruct rec = reconstruct(h5fname, sino, rot_center, blocked_views) fname = os.path.dirname(h5fname) + '/' + os.path.splitext(os.path.basename(h5fname))[0]+ '_rec_slice/' + 'recon' dxchange.write_tiff_stack(rec, fname=fname) print("Rec: ", fname) print("Slice: ", start)
def rec_sirtfbp(data, theta, rot_center, start=0, test_sirtfbp_iter=False): # Use test_sirtfbp_iter = True to test which number of iterations is suitable for your dataset # Filters are saved in .mat files in "./¨ if test_sirtfbp_iter: nCol = data.shape[2] output_name = './test_iter/' num_iter = [50, 100, 150] filter_dict = sirtfilter.getfilter(nCol, theta, num_iter, filter_dir='./') for its in num_iter: tomopy_filter = sirtfilter.convert_to_tomopy_filter( filter_dict[its], nCol) rec = tomopy.recon(data, theta, center=rot_center, algorithm='gridrec', filter_name='custom2d', filter_par=tomopy_filter) output_name_2 = output_name + 'sirt_fbp_%iiter_slice_' % its dxchange.write_tiff_stack(data, fname=output_name_2, start=start, dtype='float32') # Reconstruct object using sirt-fbp algorithm: num_iter = 100 nCol = data.shape[2] sirtfbp_filter = sirtfilter.getfilter(nCol, theta, num_iter, filter_dir='./') tomopy_filter = sirtfilter.convert_to_tomopy_filter(sirtfbp_filter, nCol) rec = tomopy.recon(data, theta, center=rot_center, algorithm='gridrec', filter_name='custom2d', filter_par=tomopy_filter) return rec
def rec_slice(h5fname, nsino, nframes, frame, nproj, binning, tv): 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) # Reconstruct rec = reconstruct(h5fname, sino, nframes, frame, nproj, binning, tv) # Write data as stack of TIFs. for time_frame in range(0,nframes): fname = os.path.dirname(os.path.abspath(h5fname)) + '/' + os.path.splitext(os.path.basename(h5fname))[0]+ '_rec_slice/' + 'recon' + str(frame-nframes/2+time_frame) + '_' dxchange.write_tiff_stack(rec[time_frame], fname=fname) print("Rec: ", fname) print("Slice: ", start)
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 rec_sirtfbp(data, theta, rot_center, start=0, test_sirtfbp_iter = True): # Use test_sirtfbp_iter = True to test which number of iterations is suitable for your dataset # Filters are saved in .mat files in "./¨ if test_sirtfbp_iter: nCol = data.shape[2] output_name = './test_iter/' num_iter = [50,100,150] filter_dict = sirtfilter.getfilter(nCol, theta, num_iter, filter_dir='./') for its in num_iter: tomopy_filter = sirtfilter.convert_to_tomopy_filter(filter_dict[its], nCol) rec = tomopy.recon(data, theta, center=rot_center, algorithm='gridrec', filter_name='custom2d', filter_par=tomopy_filter) output_name_2 = output_name + 'sirt_fbp_%iiter_slice_' % its dxchange.write_tiff_stack(data, fname=output_name_2, start=start, dtype='float32') # Reconstruct object using sirt-fbp algorithm: num_iter = 100 nCol = data.shape[2] sirtfbp_filter = sirtfilter.getfilter(nCol, theta, num_iter, filter_dir='./') tomopy_filter = sirtfilter.convert_to_tomopy_filter(sirtfbp_filter, nCol) rec = tomopy.recon(data, theta, center=rot_center, algorithm='gridrec', filter_name='custom2d', filter_par=tomopy_filter) return rec
def main(): data_top = '/local/data/2020-02/Stock/' file_name = '099_B949_81_84_B2' # data_top = '/local/data/' # file_name = 'tomo_00001' top = '/local/data/2020-02/Stock/' # log.info(os.listdir(top)) h5_file_list = list(filter(lambda x: x.endswith(('.h5', '.hdf')), os.listdir(top))) h5_file_list.sort() print("Found: %s" % h5_file_list) for fname in h5_file_list: full_file_name = data_top + fname data_size = get_dx_dims(full_file_name) print(data_size) ssino = int(data_size[1] * 0.5) detector_center = int(data_size[2] * 0.5) # Select sinogram range to reconstruct sino_start = ssino sino_end = sino_start + 10 sino = (int(sino_start), int(sino_end)) # Read APS 2-BM raw data proj, flat, dark, theta = dxchange.read_aps_32id(full_file_name, sino=sino) tomo_ind = tomopy.normalize(proj, flat, dark) print(os.path.splitext(full_file_name)[0]+'_sino') dxchange.write_tiff_stack(tomo_ind,fname=os.path.splitext(full_file_name)[0]+'_sino', axis=1)
def reconstruct(sname, rot_center, ovlpfind, s_start, s_end): fname = dfolder + sname + '.h5' print (fname) start = s_start end = s_end chunks = 24 num_sino = (end - start) // chunks for m in range(chunks): sino_start = start + num_sino * m sino_end = start + num_sino * (m + 1) start_read_time = time.time() proj, flat, dark, thetat = dxchange.read_aps_2bm(fname, sino=(sino_start, sino_end)) print(' done read in %0.1f min' % ((time.time() - start_read_time)/60)) dark = proj[9001:9002] flat = proj[0:1] proj = proj[1:9000] theta = tomopy.angles(proj.shape[0], 0., 360.) proj = tomopy.sino_360_to_180(proj, overlap=ovlpfind, rotation='right') proj = tomopy.remove_outlier(proj, dif=0.4) proj = tomopy.normalize_bg(proj, air=10) proj = tomopy.minus_log(proj) center = rot_center start_ring_time = time.time() proj = tomopy.remove_stripe_fw(proj, wname='sym5', sigma=4, pad=False) proj = tomopy.remove_stripe_sf(proj, size=3) print(' done pre-process in %0.1f min' % ((time.time() - start_ring_time)/60)) start_phase_time = time.time() proj = tomopy.retrieve_phase(proj, pixel_size=detector_pixel_size_x, dist=sample_detector_distance, energy=energy, alpha=alpha, pad=True, ncore=None, nchunk=None) print(' done phase retrieval in %0.1f min' % ((time.time() - start_phase_time)/60)) start_recon_time = time.time() rec = tomopy.recon(proj, theta, center=center, algorithm='gridrec', filter_name='ramalk') tomopy.circ_mask(rec, axis=0, ratio=0.95) print ("Reconstructed", rec.shape) dxchange.write_tiff_stack(rec, fname = dfolder + '/' + sname + '/' + sname, overwrite=True, start=sino_start) print(' Chunk reconstruction done in %0.1f min' % ((time.time() - start_recon_time)/60)) print ("Done!")
def main(arg): parser = argparse.ArgumentParser() parser.add_argument( "fname", help= "directory containing multiple dxfiles or a single DataExchange file: /data/ or /data/sample.h5" ) parser.add_argument( "--tiff", action="store_true", help="convert a single DataExchange file to a stack of tiff files") args = parser.parse_args() # Set path to the micro-CT data to reconstruct. fname = args.fname tiff = args.tiff if os.path.isfile(fname): dump_hdf5_file_structure(fname) if tiff: # Read APS 32-BM raw data. print("Reading HDF5 file: ", fname) proj, flat, dark, theta = dxchange.read_aps_32id(fname) print("Converting ....") top_out = os.path.join( os.path.dirname(fname), os.path.splitext(os.path.basename(fname))[0]) flats_out = os.path.join(top_out, "flats", "image") darks_out = os.path.join(top_out, "darks", "image") radios_out = os.path.join(top_out, "radios", "image") print("flats: ", flat.shape) dxchange.write_tiff_stack(flat, fname=flats_out) print("darks: ", dark.shape) dxchange.write_tiff_stack(dark, fname=darks_out) print("projections: ", proj.shape) dxchange.write_tiff_stack(proj, fname=radios_out) print("Converted data: ", top_out) print("Done!") elif os.path.isdir(fname): # Add a trailing slash if missing top = os.path.join(fname, '') # Set the file name that will store the rotation axis positions. h5_file_list = list( filter(lambda x: x.endswith(('.h5', '.hdf')), os.listdir(top))) print("Found: ", h5_file_list.sort()) for fname in h5_file_list: h5fname = top + fname dump_hdf5_file_structure(h5fname) else: print("Directory or File Name does not exist: ", fname)
import dxchange import dxchange.reader as dxreader import tomopy h5fname = '/local/data/2018-03/Lindley/Exp005_subsea_bolt_sample_1_26C_04_YPos12.2mm_FriMar23_15_11_42_2018_edge_2x_750mm_800.0msecExpTime_0.12DegPerSec_Rolling_20umLuAG_1mmAl15mmSi4mmSn0.5mmCu_0.0mrad_USArm1.25_monoY_-16.0_AHutch/proj_0005.hdf' zinger_level = 800 # Zinger level for projections zinger_level_w = 1000 # Zinger level for white # Read the txrm raw data. start = 0 end = start + 2000 sino = (start, end) # Read APS 32-BM raw data. data, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino) # zinger_removal #proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0) #flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0) # Flat-field correction of raw data. #data = tomopy.normalize(proj, flat, dark, cutoff=1.4) dxchange.write_tiff_stack(data, fname='/local/data/2018-03/Lindley/Exp005_02/recon_')
sino_st = 750 sino_end = 1250 if 0: prj, flat, dark, theta = dxchange.read_aps_32id(file_name, sino=(sino_st,sino_end,1), proj=(0, 1210, 1)) # open proj from 0 to 720 deg with 180deg step --> 5 proj prj = tomopy.normalize(prj, flat, dark) print('\n*** Shape of the data:'+str(np.shape(prj))) prj = tomopy.misc.corr.remove_neg(prj, val=0.000) prj = tomopy.misc.corr.remove_nan(prj, val=0.000) prj[np.where(prj == np.inf)] = 0.000 prj[np.where(prj > 1.0)] = 1 prj = tomopy.downsample(prj.copy(), level=binning) prj = tomopy.downsample(prj.copy(), level=binning, axis=1) print('\n*** Shape of the data:'+str(np.shape(prj))) dxchange.write_tiff_stack(prj, fname='/local/dataraid/2018-06/DeAndrade/2018-06-19/brain_petrapoxy/rot1/prj', dtype='float32', axis=0, digit=4, start=0, overwrite=False) if 0: prj, flat, dark, theta = dxchange.read_aps_32id(file_name, sino=(sino_st,sino_end,1), proj=(1210, 1210+1210, 1)) # open proj from 0 to 720 deg with 180deg step --> 5 proj prj = tomopy.normalize(prj, flat, dark) prj = tomopy.misc.corr.remove_neg(prj, val=0.000) prj = tomopy.misc.corr.remove_nan(prj, val=0.000) prj[np.where(prj == np.inf)] = 0.000 prj[np.where(prj > 1.0)] = 1 prj = tomopy.downsample(prj.copy(), level=binning) prj = tomopy.downsample(prj.copy(), level=binning, axis=1) dxchange.write_tiff_stack(prj, fname='/local/dataraid/2018-06/DeAndrade/2018-06-19/brain_petrapoxy/rot2/prj', dtype='float32', axis=0, digit=4, start=0, overwrite=False) if 0: prj, flat, dark, theta = dxchange.read_aps_32id(file_name, sino=(sino_st,sino_end,1), proj=(2420, 2420+1210, 1)) # open proj from 0 to 720 deg with 180deg step --> 5 proj
shiftz = np.mean(shiftza) shiftx = np.mean(shiftxa) ishiftx = np.int(shiftx) fshiftx = shiftx - ishiftx # resulting data datanew = np.zeros([ntheta // 2, nz, 2 * n], dtype='float32') # make smooth border between data sets fw1 = np.ones(n) fw2 = np.ones(n) fw1[0:ishiftx + w] = np.linspace(0, 1, (ishiftx + w)) fw2[-(ishiftx + w):] = np.linspace(1, 0, (ishiftx + w)) datanew[:, :, n:] = datap1 * fw1 datanew[:, :, ishiftx + w:n + ishiftx + w] = apply_shift_batch(datap2, [0, fshiftx]) * fw1 dxchange.write_tiff_stack(datanew, 't/t.tiff') fid = h5py.File(args.foutname, 'w') fid.create_dataset('/exchange/data', (3000 // 2, 1024, n * 2), chunks=(3000 // 2, 1, 2 * n), dtype='float32') fid.create_dataset('/exchange/data_white', (flat.shape[0], 1024, n * 2), chunks=(flat.shape[0], 1, 2 * n), dtype='float32') fid.create_dataset('/exchange/data_dark', (dark.shape[0], 1024, n * 2), chunks=(dark.shape[0], 1, 2 * n), dtype='float32') for k in range(0, 8): print(k) proj, flat, dark, theta = dxchange.read_aps_32id(args.fname, sino=(k * 128,
data = tomopy.normalize(proj, flat, dark) # remove stripes data = tomopy.prep.stripe.remove_stripe_fw(data, level=5, wname='sym16', sigma=1, pad=True) # phase retrieval data = tomopy.prep.phase.retrieve_phase(data, pixel_size=detector_pixel_size_x, dist=sample_detector_distance, energy=monochromator_energy, alpha=8e-3, pad=True) # Set rotation center. rot_center = rot_center data = tomopy.minus_log(data) # Reconstruct object using Gridrec algorithm. 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) # Write data as stack of TIFs. dxchange.write_tiff_stack(rec, fname='recon_dir/esrf')
end = 1025 proj = proj[:, [start,end], :] flat = flat[:, [start,end], :] dark = dark[:, [start,end], :] # Flat-field correction of raw data. data = tomopy.normalize(proj, flat, dark) # remove stripes data = tomopy.prep.stripe.remove_stripe_fw(data,level=5,wname='sym16',sigma=1,pad=True) # # phase retrieval #data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=8e-3,pad=True) # Set rotation center. rot_center = 1552 print(rot_center) data = tomopy.minus_log(data) # Reconstruct object using Gridrec algorithm. rec = tomopy.recon(data, theta, center=rot_center, algorithm='gridrec', filter_name = 'parzen', nchunk=1) # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) # Write data as stack of TIFs. fname='/local/decarlo/data/hzg/microtomography/fabian_wilde/recon_dir/recon' dxchange.write_tiff_stack(rec, fname=fname)
def reconstruct(filename,inputPath="", outputPath="", COR=COR, doOutliers=doOutliers, outlier_diff=outlier_diff, outlier_size=outlier_size, doFWringremoval=doFWringremoval, ringSigma=ringSigma,ringLevel=ringLevel, ringWavelet=ringWavelet,pad_sino=pad_sino, doPhaseRetrieval=doPhaseRetrieval, propagation_dist=propagation_dist, kev=kev,alphaReg=alphaReg, butterworthpars=butterworthpars, doPolarRing=doPolarRing,Rarc=Rarc, Rmaxwidth=Rmaxwidth, Rtmax=Rtmax, Rthr=Rthr, Rtmin=Rtmin, useAutoCOR=useAutoCOR, use360to180=use360to180, num_substacks=num_substacks,recon_slice=recon_slice): # Convert filename to list type if only one file name is given if type(filename) != list: filename=[filename] # If useAutoCor == true, a list of COR will be automatically calculated for all files # If a list of COR is given, only entries with boolean False will use automatic COR calculation if useAutoCOR==True or (len(COR) != len(filename)): logging.info('using auto COR for all input files') COR = [False]*len(filename) for x in range(len(filename)): logging.info('opening data set, checking metadata') fdata, gdata = read_als_832h5_metadata(inputPath[x]+filename[x]+'.h5') pxsize = float(gdata['pxsize'])/10.0 # convert from metadata (mm) to this script (cm) numslices = int(gdata['nslices']) # recon_slice == True, only center slice will be reconstructed # if integer is given, a specific if recon_slice != False: if (type(recon_slice) == int) and (recon_slice <= numslices): sinorange [recon_slice-1, recon_slice] else: sinorange = [numslices//2-1, numslices//2] else: sinorange = [0, numslices] # Calculate number of substacks (chunks) substacks = num_substacks #(sinorange[1]-sinorange[0]-1)//num_sino_per_substack+1 if (sinorange[1]-sinorange[0]) >= substacks: num_sino_per_substack = (sinorange[1]-sinorange[0])//num_substacks else: num_sino_per_substack = 1 firstcor, lastcor = 0, int(gdata['nangles'])-1 projs, flat, dark, floc = dxchange.read_als_832h5(inputPath[x]+filename[x]+'.h5', ind_tomo=(firstcor, lastcor)) projs = tomopy.normalize_nf(projs, flat, dark, floc) autocor = tomopy.find_center_pc(projs[0], projs[1], tol=0.25) if (type(COR[x]) == bool) or (COR[x]<0) or (COR[x]=='auto'): firstcor, lastcor = 0, int(gdata['nangles'])-1 projs, flat, dark, floc = dxchange.read_als_832h5(inputPath[x]+filename[x]+'.h5', ind_tomo=(firstcor, lastcor)) projs = tomopy.normalize_nf(projs, flat, dark, floc) cor = tomopy.find_center_pc(projs[0], projs[1], tol=0.25) else: cor = COR[x] logging.info('Dataset %s, has %d total slices, reconstructing slices %d through %d in %d substack(s), using COR: %f',filename[x], int(gdata['nslices']), sinorange[0], sinorange[1]-1, substacks, cor) for y in range(0, substacks): logging.info('Starting dataset %s (%d of %d), substack %d of %d',filename[x], x+1, len(filename), y+1, substacks) logging.info('Reading sinograms...') projs, flat, dark, floc = dxchange.read_als_832h5(inputPath[x]+filename[x]+'.h5', sino=(sinorange[0]+y*num_sino_per_substack, sinorange[0]+(y+1)*num_sino_per_substack, 1)) logging.info('Doing remove outliers, norm (nearest flats), and -log...') if doOutliers: projs = tomopy.remove_outlier(projs, outlier_diff, size=outlier_size, axis=0) flat = tomopy.remove_outlier(flat, outlier_diff, size=outlier_size, axis=0) tomo = tomopy.normalize_nf(projs, flat, dark, floc) tomo = tomopy.minus_log(tomo, out=tomo) # in place logarithm # Use padding to remove halo in reconstruction if present if pad_sino: npad = int(np.ceil(tomo.shape[2] * np.sqrt(2)) - tomo.shape[2])//2 tomo = tomopy.pad(tomo, 2, npad=npad, mode='edge') cor_rec = cor + npad # account for padding else: cor_rec = cor if doFWringremoval: logging.info('Doing ring (Fourier-wavelet) function...') tomo = tomopy.remove_stripe_fw(tomo, sigma=ringSigma, level=ringLevel, pad=True, wname=ringWavelet) if doPhaseRetrieval: logging.info('Doing Phase retrieval...') #tomo = tomopy.retrieve_phase(tomo, pixel_size=pxsize, dist=propagation_dist, energy=kev, alpha=alphaReg, pad=True) tomo = tomopy.retrieve_phase(tomo, pixel_size=pxsize, dist=propagation_dist, energy=kev, alpha=alphaReg, pad=True) logging.info('Doing recon (gridrec) function and scaling/masking, with cor %f...',cor_rec) rec = tomopy.recon(tomo, tomopy.angles(tomo.shape[0], 270, 90), center=cor_rec, algorithm='gridrec', filter_name='butterworth', filter_par=butterworthpars) #rec = tomopy.recon(tomo, tomopy.angles(tomo.shape[0], 180+angularrange/2, 180-angularrange/2), center=cor_rec, algorithm='gridrec', filter_name='butterworth', filter_par=butterworthpars) rec /= pxsize # intensity values in cm^-1 if pad_sino: rec = tomopy.circ_mask(rec[:, npad:-npad, npad:-npad], 0) else: rec = tomopy.circ_mask(rec, 0, ratio=1.0, val=0.0) if doPolarRing: logging.info('Doing ring (polar mean filter) function...') rec = tomopy.remove_ring(rec, theta_min=Rarc, rwidth=Rmaxwidth, thresh_max=Rtmax, thresh=Rthr, thresh_min=Rtmin) logging.info('Writing reconstruction slices to %s', filename[x]) #dxchange.write_tiff_stack(rec, fname=outputPath+'alpha'+str(alphaReg)+'/rec'+filename[x]+'/rec'+filename[x], start=sinorange[0]+y*num_sino_per_substack) dxchange.write_tiff_stack(rec, fname=outputPath + 'recon_'+filename[x]+'/recon_'+filename[x], start=sinorange[0]+y*num_sino_per_substack) logging.info('Reconstruction Complete: '+ filename[x])
# Select the sinogram range to reconstruct. start = 290 end = 294 # Read the APS 5-BM raw data proj, flat, dark = dxchange.read_aps_5bm(fname, sino=(start, end)) # 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) # remove stripes proj = tomopy.remove_stripe_fw(proj,level=7,wname='sym16',sigma=1,pad=True) # Set rotation center. rot_center = proj.shape[2] / 2.0 print("Center of rotation: ", rot_center) proj = tomopy.minus_log(proj) # Reconstruct object using Gridrec algorithm. rec = tomopy.recon(proj, 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')
inputPath = '{}_{:d}{}'.format(fn,y,fileextension) tomo[y] = dxchange.reader.read_tiff(inputPath,slc = (sinoused, raysused)) print('loading flat images') for y in range(0,len(floc)): inputPath = '{}{}_{:d}{}'.format(fn,flatextension,floc[y],fileextension) flat[y] = dxchange.reader.read_tiff(inputPath,slc = (sinoused, raysused)) print('loading dark images') for y in range(0,numdrk): inputPath = '{}{}_{:d}{}'.format(fn,darkextension,y,fileextension) dark[y] = dxchange.reader.read_tiff(inputPath,slc = (sinoused, raysused)) print('normalizing') tomo = tomo.astype(np.float32) tomopy.normalize_nf(tomo, flat, dark, floc, out=tomo) tomopy.minus_log(tomo, out=tomo) tomo = tomopy.pad(tomo, 2, npad=npad, mode='edge') rec = tomopy.recon(tomo, tomopy.angles(numangles, angle_offset, angle_offset-angularrange), center=cor+npad, algorithm='gridrec', filter_name='butterworth', filter_par=[.25, 2]) rec = rec[:, npad:-npad, npad:-npad] rec /= pxsize # convert reconstructed voxel values from 1/pixel to 1/cm rec = tomopy.circ_mask(rec, 0) print('writing recon') dxchange.write_tiff_stack(rec, fname='rec/'+fn, start=sinoused[0])
if pad_sino: logging.info('Unpadding...') rec = tomopy.circ_mask(rec[:, npad:-npad, npad:-npad], 0) CORtoWrite = CORtoWrite - npad else: rec = tomopy.circ_mask(rec, 0, ratio=1.0, val=0.0) logging.info('Writing reconstruction slices to %s', iname[x]) if testCOR_insteps: filenametowrite = odirectory+'/rec'+iname[x]+'/'+'cor'+str(CORtoWrite)+'_'+iname[x] else: filenametowrite = odirectory+'/rec'+iname[x]+'/'+iname[x] if castTo8bit: rec = convert8bit(rec,data_min,data_max) dxchange.write_tiff_stack(rec, fname=filenametowrite, start=sinorange[0]+y*num_sino_per_chunk) logging.info('virtual memory before gc: %s',psutil.virtual_memory()) gc.collect() logging.info('virtual memory after gc: %s',psutil.virtual_memory()) logging.info('Time: %s',datetime.now().time()) tomo = None
# Set number of data chunks for the reconstruction. chunks = 64 num_sino = (end - start) // chunks for m in range(chunks): sino_start = start + num_sino * m sino_end = start + num_sino * (m + 1) # Read APS 32-ID raw data. proj, flat, dark = dxchange.read_aps_32id(fname, sino=(sino_start, sino_end)) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(proj.shape[0]) # Remove the missing angles from data. proj = np.concatenate((proj[0:miss_projs[0], :, :], proj[miss_projs[1] + 1:-1, :, :]), axis=0) theta = np.concatenate((theta[0:miss_projs[0]], theta[miss_projs[1] + 1:-1])) # Flat-field correction of raw data. proj = tomopy.normalize(proj, flat, dark) proj = tomopy.minus_log(proj) # Reconstruct object using Gridrec algorithm. rec = tomopy.recon(proj, theta, center=rot_center, algorithm='gridrec') # Write data as stack of TIFs. dxchange.write_tiff_stack(rec, fname='recon_dir/recon', start=sino_start)
#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) # NOTE: center finding doesn't work for my datasets :-( #center = tomopy.find_center(proj.local_arr, theta, sinogram_order=True) center = proj.shape[2] // 2 logger.info("Center for sinograms [%d:%d] is %f" % (proj.offset, proj.offset+proj.size, center)) alg = 'gridrec' logger.info("Reconstructing using: %s" % alg) # Reconstruct object using algorithm proj.scatter(1) rec = tomopy.recon(proj.local_arr, theta, center=center, algorithm=alg, sinogram_order=True, ncore=1) logger.info("Writing result to file") dxchange.write_tiff_stack(rec, fname='%s(mpi)/recon' % (alg), start=proj.offset, overwrite=True) logger.info("Done in %0.2f seconds"%(time.time() - start))
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 doTranslationCorrection = False, # correct for linear drift during scan xshift = 0, # undesired dx transation correction (from 0 degree to 180 degree proj) yshift = 0, # undesired dy transation correction (from 0 degree to 180 degree proj) 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 outputFilename = outputFilename.replace('.h5','') 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 units 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 == 'translation_correction': tomo = linear_translation_correction(tomo,dx=xshift,dy=yshift,interpolation=False): 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 main(arg): parser = argparse.ArgumentParser() parser.add_argument("fname", help="Full file name: /data/fname.raw") parser.add_argument("--start", nargs='?', type=int, default=0, help="First image to read") parser.add_argument("--nimg", nargs='?', type=int, default=1, help="Number of images to read") parser.add_argument("--ndark", nargs='?', type=int, default=10, help="Number of dark images") parser.add_argument("--nflat", nargs='?', type=int, default=10, help="Number of white images") args = parser.parse_args() fname = args.fname start = args.start end = args.start + args.nimg nflat, ndark, nimg, height, width = read_adimec_header(fname) print("Image Size:", width, height) print("Dataset metadata (nflat, ndark, nimg:", nflat, ndark, nimg) # override nflat and ndark from header with the passed parameter # comment the two lines below if the meta data in the binary # file for nflat and ndark is correct nflat = args.nflat ndark = args.ndark proj = read_adimec_stack(fname, img=(start, end)) print("Projection:", proj.shape) # slider(proj) flat = read_adimec_stack(fname, img=(nimg-ndark-nflat, nimg-ndark)) print("Flat:", flat.shape) # slider(flat) dark = read_adimec_stack(fname, img=(nimg-ndark, nimg)) print("Dark:", dark.shape) # slider(dark) nproj = tomopy.normalize(proj, flat, dark) print("Normalized projection:", nproj.shape) # slider(proj) proj = nproj[:,100:110, :] print("Sino chunk:", proj.shape) slider(proj) theta = tomopy.angles(proj.shape[0]) print(theta.shape) proj = tomopy.minus_log(proj) proj = tomopy.remove_nan(proj, val=0.0) proj = tomopy.remove_neg(proj, val=0.00) proj[np.where(proj == np.inf)] = 0.00 rot_center = 1280 # Reconstruct object using Gridrec algorithm. rec = tomopy.recon(proj, 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')
if __name__ == '__main__': # Set path to the micro-CT data to reconstruct. fname = '../../../tomopy/data/tooth.h5' # Select the sinogram range to reconstruct. start = 0 end = 2 # Read the APS 2-BM 0r 32-ID raw data. proj, flat, dark = dxchange.read_aps_32id(fname, sino=(start, end)) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(proj.shape[0]) # Set data collection angles as equally spaced between 0-180 degrees. proj = tomopy.normalize(proj, flat, dark) # Set data collection angles as equally spaced between 0-180 degrees. rot_center = tomopy.find_center(proj, theta, init=290, ind=0, tol=0.5) tomopy.minus_log(proj) # Reconstruct object using Gridrec algorithm. recon = tomopy.recon(proj, theta, center=rot_center, algorithm='gridrec') # Mask each reconstructed slice with a circle. recon = tomopy.circ_mask(recon, axis=0, ratio=0.95) # Write data as stack of TIFs.
end = 302 # Read the Anka tiff raw data. proj, flat, dark, theta = dxchange.read_aps_32id(fname, sino=(start, end)) # 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. data = tomopy.normalize(proj, flat, dark) #proj = tomopy.minus_log(proj) # Find rotation center. #rot_center = tomopy.find_center(proj, theta, emission=False, init=1024, ind=0, tol=0.5) rot_center = 1296 print("Center of rotation: ", rot_center) data = tomopy.minus_log(data) # Reconstruct object using Gridrec algorithm. 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) # Write data as stack of TIFs. dxchange.write_tiff_stack(rec, fname= top + h5name +'/recon')
# Read APS 32-ID raw data. proj, flat, dark, theta = dxchange.read_aps_32id(fname, sino=(sino_start, sino_end)) # If data collection angles is not defined in the hdf file then set it as equally spaced between 0-180 degrees. if (theta is None): theta = tomopy.angles(proj.shape[0]) else: pass # Remove the missing angles from data. proj = np.concatenate( (proj[0:miss_projs[0], :, :], proj[miss_projs[1] + 1:-1, :, :]), axis=0) theta = np.concatenate( (theta[0:miss_projs[0]], theta[miss_projs[1] + 1:-1])) # Flat-field correction of raw data. proj = tomopy.normalize(proj, flat, dark) proj = tomopy.minus_log(proj) # Reconstruct object using Gridrec algorithm. rec = tomopy.recon(proj, theta, center=rot_center, algorithm='gridrec') # Write data as stack of TIFs. dxchange.write_tiff_stack(rec, fname='recon_dir/recon', start=sino_start)
#rec_method = 'sirf-fbp' if rec_method == 'sirf-fbp': # Reconstruct object using sirt-fbp algorithm. # Use test_sirtfbp_iter = True to test which number of iterations is suitable for your dataset # Filters are saved in .mat files in "./¨ test_sirtfbp_iter = True if test_sirtfbp_iter: nCol = ndata.shape[2] output_name = './test_iter/' num_iter = [50,100,150] filter_dict = sirtfilter.getfilter(nCol, theta, num_iter, filter_dir='./') for its in num_iter: tomopy_filter = sirtfilter.convert_to_tomopy_filter(filter_dict[its], nCol) rec = tomopy.recon(ndata, theta, center=rot_center, algorithm='gridrec', filter_name='custom2d', filter_par=tomopy_filter) output_name_2 = output_name + 'sirt_fbp_%iiter_slice_' % its dxchange.write_tiff_stack(rec, fname=output_name_2, start=start, dtype='float32') # Reconstruct object using sirt-fbp algorithm: num_iter = 100 nCol = ndata.shape[2] print("sirt-fbp") sirtfbp_filter = sirtfilter.getfilter(nCol, theta, num_iter, filter_dir='./') tomopy_filter = sirtfilter.convert_to_tomopy_filter(sirtfbp_filter, nCol) rec = tomopy.recon(ndata, theta, center=rot_center, algorithm='gridrec', filter_name='custom2d', filter_par=tomopy_filter) # 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=top + 'recon' + '/recon') elif rec_method == 'sirt': print("sirt")
remove_rings=remove_rings, FF_drift_corr=flat_field_drift_corr, downsapling=binning) dxchange.write_tiff(prj[0], name + '/theta' + str(ang) + '/' + str(j), overwrite=True) arr[j] = prj[0] mmin, mmax = utils.find_min_max(prj[:1]) mmin[:] = 0 mmin[:] = 0.1 mmax[:] = 2 # parameters for non-dense flow in Farneback's algorithm, # resulting flow is constant, i.e. equivalent to a shift res = arr.copy() pars = [0.5, 4, 12, 16, 5, 1.1, 0] print(mmin, mmax) for k in range(arr.shape[0]): with solver_deform.SolverDeform(1, 2048, 2448, 1, 1) as dslv: print(np.linalg.norm(arr[k:k + 1])) print(np.linalg.norm(arr[0:1])) t1 = arr[k:k + 1].copy() t2 = arr[0:1].copy() flow = dslv.registration_flow_batch(t1, t2, mmin, mmax, None, pars) print(np.linalg.norm(flow)) res[k:k + 1] = dslv.apply_flow_gpu_batch(arr[k:k + 1], flow) dxchange.write_tiff_stack(res, name + '/theta' + str(ang) + 'aligned/r', overwrite=True)
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) # Reconstruct object using Gridrec algorithm. rec = tomopy.recon(proj, 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. recfname = top +'full_rec/' + prefix + index + '/recon' print("Rec: ", recfname) dxchange.write_tiff_stack(rec, fname=recfname, start=strt) strt += proj.shape[1]
Tpsi = dslv.apply_flow_gpu_batch(psi1, flow) lagr = np.zeros(7) lagr[0] = np.linalg.norm(Tpsi - data)**2 lagr[1] = np.sum(np.real(np.conj(lamd1) * (h1 - psi1))) lagr[2] = rho1 * np.linalg.norm(h1 - psi1)**2 lagr[3] = alpha * \ np.sum(np.sqrt(np.real(np.sum(psi2*np.conj(psi2), 0)))) lagr[4] = np.sum(np.real(np.conj(lamd2 * (h2 - psi2)))) lagr[5] = rho2 * np.linalg.norm(h2 - psi2)**2 lagr[6] = np.sum(lagr[0:5]) print(k, rho1, rho2, lagr) print('times:', t1, t2, t3) sys.stdout.flush() dxchange.write_tiff_stack( u, '/local/data/vnikitin/lamino/rec_align_shift' + str(idset) + '/tmp' + '_' + str(ntheta) + '_' + str(alpha) + '/rect' + str(k) + '/r', overwrite=True) dxchange.write_tiff_stack( psi1, '/local/data/vnikitin/lamino/prj_align_shift' + str(idset) + '/tmp' + '_' + str(ntheta) + '_' + str(alpha) + '/psir' + str(k) + '/r', overwrite=True) if not os.path.exists( '/local/data/vnikitin/lamino/flowshift' + str(alpha)): os.makedirs('/local/data/vnikitin/lamino/flowshift' + str(alpha)) np.save( '/local/data/vnikitin/lamino/flowshift' + str(alpha) +
def rec(params): data_shape = file_io.get_dx_dims(params) if params.rotation_axis < 0: params.rotation_axis = data_shape[2] / 2 # Select sinogram range to reconstruct if (params.reconstruction_type == "full"): nSino_per_chunk = params.nsino_per_chunk chunks = int(np.ceil(data_shape[1] / nSino_per_chunk)) sino_start = 0 sino_end = chunks * nSino_per_chunk else: # "slice" and "try" nSino_per_chunk = pow(2, int(params.binning)) chunks = 1 ssino = int(data_shape[1] * params.nsino) sino_start = ssino sino_end = sino_start + pow(2, int(params.binning)) log.info("reconstructing [%d] slices from slice [%d] to [%d] in [%d] chunks of [%d] slices each" % \ ((sino_end - sino_start)/pow(2, int(params.binning)), sino_start/pow(2, int(params.binning)), sino_end/pow(2, int(params.binning)), \ chunks, nSino_per_chunk/pow(2, int(params.binning)))) strt = 0 for iChunk in range(0, chunks): log.info('chunk # %i/%i' % (iChunk, chunks)) sino_chunk_start = np.int(sino_start + nSino_per_chunk * iChunk) sino_chunk_end = np.int(sino_start + nSino_per_chunk * (iChunk + 1)) log.info(' *** [%i, %i]' % (sino_chunk_start / pow(2, int(params.binning)), sino_chunk_end / pow(2, int(params.binning)))) 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, rotation_axis = file_io.read_tomo( sino, params) # apply all preprocessing functions data = prep.all(proj, flat, dark, params) # Reconstruct if (params.reconstruction_type == "try"): # try passes an array of rotation centers and this is only supported by gridrec reconstruction_algorithm_org = params.reconstruction_algorithm params.reconstruction_algorithm = 'gridrec' center_search_width = params.center_search_width / np.power( 2, float(params.binning)) center_range = (rotation_axis - center_search_width, rotation_axis + center_search_width, 0.5) stack = np.empty( (len(np.arange(*center_range)), data_shape[0], int(data_shape[2] / np.power(2, float(params.binning))))) index = 0 for axis in np.arange(*center_range): stack[index] = data[:, 0, :] index = index + 1 log.warning( ' reconstruct slice [%d] with rotation axis range [%.2f - %.2f] in [%.2f] pixel steps' % (ssino, center_range[0], center_range[1], center_range[2])) rotation_axis = np.arange(*center_range) rec = padded_rec(stack, theta, rotation_axis, params) # Save images to a temporary folder. fname = os.path.dirname( params.hdf_file ) + '_rec' + os.sep + 'try_center' + os.sep + file_io.path_base_name( params.hdf_file) + os.sep + 'recon_' index = 0 for axis in np.arange(*center_range): rfname = fname + str('{0:.2f}'.format( axis * np.power(2, float(params.binning))) + '.tiff') dxchange.write_tiff(rec[index], fname=rfname, overwrite=True) index = index + 1 # restore original method params.reconstruction_algorithm = reconstruction_algorithm_org else: # "slice" and "full" rec = padded_rec(data, theta, rotation_axis, params) # handling of the last chunk if (params.reconstruction_type == "full"): if (iChunk == chunks - 1): log.info("handling of the last chunk") log.info(" *** chunk # %d" % (chunks)) log.info(" *** last rec size %d" % ((data_shape[1] - (chunks - 1) * nSino_per_chunk) / pow(2, int(params.binning)))) rec = rec[0:data_shape[1] - (chunks - 1) * nSino_per_chunk, :, :] # Save images if (params.reconstruction_type == "full"): tail = os.sep + os.path.splitext( os.path.basename(params.hdf_file))[0] + '_rec' + os.sep fname = os.path.dirname( params.hdf_file) + '_rec' + tail + 'recon' dxchange.write_tiff_stack(rec, fname=fname, start=strt) strt += int( (sino[1] - sino[0]) / np.power(2, float(params.binning))) if (params.reconstruction_type == "slice"): fname = os.path.dirname( params.hdf_file ) + os.sep + 'slice_rec/recon_' + os.path.splitext( os.path.basename(params.hdf_file))[0] dxchange.write_tiff_stack(rec, fname=fname, overwrite=False) log.info(" *** reconstructions: %s" % fname)
sample_detector_distance = 18.8e2 detector_pixel_size_x = 19.8e-7 monochromator_energy = 11.0 # for scan_renamed_450projections proj_start = 0 proj_end = 451 flat_start = 0 flat_end = 93 dark_start = 0 dark_end = 10 ind_tomo = range(proj_start, proj_end) ind_flat = range(flat_start, flat_end) ind_dark = range(dark_start, dark_end) # Read the Anka tiff raw data. proj, flat, dark = dxchange.read_anka_topotomo(fname, ind_tomo, ind_flat, ind_dark) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(proj.shape[0]) # Flat-field correction of raw data. data = tomopy.normalize(proj, flat, dark) data = tomopy.minus_log(data) # Write aligned projections as stack of TIFs. dxchange.write_tiff_stack(data, fname='/local/decarlo/data/hzg/nanotomography/scan_renamed_450projections_crop_rotate/radios_org/image')
ndsets = np.int(sys.argv[2]) nth = np.int(sys.argv[3]) start = np.int(sys.argv[4]) name = sys.argv[5] binning = 0 data = np.zeros([nsets*ndsets*nth, (2048-512)//pow(2, binning), (2448-400)//pow(2, binning)], dtype='float32') theta = np.zeros(nsets*ndsets*nth, dtype='float32') strs = ['098','099','100'] for j in range(nsets): name0 = name[:-3]+strs[j]#name[:-2]+str(np.int(name[-2:])+j) print(name0) for k in range(ndsets): print(j,k) idstart = j*ndsets*nth+k*nth data[idstart:idstart+nth] = np.load(name0+'ti_bin'+str(binning)+str(k)+'.npy')[:,256:-256,200:-200].astype('float32') theta[idstart:idstart+nth] = np.load(name0+'_theta'+str(k)+'.npy').astype('float32') data[np.isnan(data)] = 0 data = np.ascontiguousarray(data[start::2]) theta = np.ascontiguousarray(theta[start::2]) ngpus = 4 pnz = 8 nitercg = 32 center = 1256-200 res = tomoalign.cg(data, theta, pnz, center, ngpus, nitercg, padding=True) name+='/'+str(len(theta)) dxchange.write_tiff_stack(res['u'], name+'/results_cg'+str(start)+'/u/r', overwrite=True)
nth = np.int(sys.argv[2]) name = sys.argv[3] binning = 1 data = np.zeros([ndsets*nth,2048//pow(2,binning),2448//pow(2,binning)],dtype='float32') theta = np.zeros(ndsets*nth,dtype='float32') for k in range(ndsets): data[k*nth:(k+1)*nth] = np.load(name+'_bin'+str(binning)+str(k)+'.npy').astype('float32') theta[k*nth:(k+1)*nth] = np.load(name+'_theta'+str(k)+'.npy').astype('float32') [ntheta, nz, n] = data.shape # object size n x,y data[np.isnan(data)]=0 data-=np.mean(data) # pad data ne = 3584//pow(2,binning) #ne=n print(data.shape) # data=prealign(data) center = centers[sys.argv[3]]+(ne//2-n//2)*pow(2,binning) pnz = 8*pow(2,binning) # number of slice partitions for simultaneous processing in tomography u = np.zeros([nz, ne, ne], dtype='float32') with tc.SolverTomo(np.array(theta[::2],order='C'), ntheta//2, nz, ne, pnz, center/pow(2, binning), ngpus) as tslv: ucg = tslv.cg_tomo_batch(pad(np.array(data[::2],order='C'),ne,n),u,64) dxchange.write_tiff_stack( ucg[:,ne//2-n//2:ne//2+n//2,ne//2-n//2:ne//2+n//2], name+'/cgn_resolution1_'+'_'+str(ntheta//2)+'/rect'+str(k)+'/r', overwrite=True) with tc.SolverTomo(np.array(theta[1::2],order='C'), ntheta//2, nz, ne, pnz, center/pow(2, binning), ngpus) as tslv: ucg = tslv.cg_tomo_batch(pad(np.array(data[1::2],order='C'),ne,n),u,64) dxchange.write_tiff_stack( ucg[:,ne//2-n//2:ne//2+n//2,ne//2-n//2:ne//2+n//2], name+'/cgn_resolution2_'+'_'+str(ntheta//2)+'/rect'+str(k)+'/r', overwrite=True)
end = 146 # Read the Anka tiff raw data. proj, flat, dark = dxchange.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]) # Flat-field correction of raw data. data = tomopy.normalize(proj, flat, dark) data = tomopy.minus_log(data) print(data.shape) #data = tomopy.downsample(data, level=2, axis=1) #data = tomopy.downsample(data, level=2, axis=2) print(data.shape) rot_center = 344 print("Center of rotation: ", rot_center) # Reconstruct object using Gridrec algorithm. 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) # Write data as stack of TIFs. dxchange.write_tiff_stack(rec, fname='/local/decarlo/data/hzg/nanotomography/scan_renamed_450projections_crop/recon_dir/recon')
data, pixel_size=detector_pixel_size_x, dist=sample_detector_distance, energy=monochromator_energy, alpha=alpha, pad=True) # Find rotation center # rot_center = 955 # rot_center = 953.25 rot_center = 960.25 # rot_center = tomopy.find_center(data, theta, init=rot_center, ind=0, tol=0.5) # rot_center = tomopy.find_center_vo(data) print(h5name, rot_center) data = tomopy.minus_log(data) # Reconstruct object using Gridrec algorithm. 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) # Write data as stack of TIFs. ##fname = top +'full_rec/' + prefix + h5name + '/recon' rname = top + h5name + '_full_rec/' + 'recon' print("Rec: ", rname) dxchange.write_tiff_stack(rec, fname=rname, start=strt) strt += data.shape[1]
ind_flat = range(flat_start, flat_end) ind_dark = range(dark_start, dark_end) # Select the sinogram range to reconstruct. start = 0 end = 16 # Read the Petra III P05 proj, flat, dark = dxchange.read_petraIII_p05(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]) # Flat-field correction of raw data. proj = tomopy.normalize(proj, flat, dark) # Find rotation center. rot_center = tomopy.find_center(proj, theta, init=1024, ind=0, tol=0.5) print("Center of rotation: ", rot_center) proj = tomopy.minus_log(proj) # Reconstruct object using Gridrec algorithm. rec = tomopy.recon(proj, 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/petra_')
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 "./¨ test_sirtfbp_iter = True if test_sirtfbp_iter: nCol = data.shape[2] output_name = './test_iter/' num_iter = [50,100,150] filter_dict = sirtfilter.getfilter(nCol, theta, num_iter, filter_dir='./') for its in num_iter: tomopy_filter = sirtfilter.convert_to_tomopy_filter(filter_dict[its], nCol) rec = tomopy.recon(data, theta, center=rot_center, algorithm='gridrec', filter_name='custom2d', filter_par=tomopy_filter) output_name_2 = output_name + 'sirt_fbp_%iiter_slice_' % its dxchange.write_tiff_stack(data, fname=output_name_2, start=start, dtype='float32') # Reconstruct object using sirt-fbp algorithm: num_iter = 100 nCol = data.shape[2] sirtfbp_filter = sirtfilter.getfilter(nCol, theta, num_iter, filter_dir='./') tomopy_filter = sirtfilter.convert_to_tomopy_filter(sirtfbp_filter, nCol) rec = tomopy.recon(data, theta, center=rot_center, algorithm='gridrec', filter_name='custom2d', filter_par=tomopy_filter) # Reconstruct object using Gridrec algorithm. # rec = tomopy.recon(data, theta, center=rot_center, algorithm='gridrec', nchunk=1) # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) # Write data as stack of TIFs.
# Set path to the micro-CT data to reconstruct. fname = 'data_dir/sample.h5' # Select the sinogram range to reconstruct. start = 0 end = 16 # Read the ALS raw data. proj, flat, dark, grp_flat = dxchange.read_als_832h5(fname, sino=(start, end)) # Set data collection angles as equally spaced between 0-180 degrees. theta = tomopy.angles(proj.shape[0], 0, 180) # Flat-field correction of raw data. proj = tomopy.normalize_nf(proj, flat, dark, grp_flat) # Find rotation center. rot_center = tomopy.find_center(proj, theta, init=1024, ind=0, tol=0.5) print("Center of rotation:", rot_center) proj = tomopy.minus_log(proj) # Reconstruct object using Gridrec algorithm. rec = tomopy.recon(proj, 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/als_h5')