def write_center(tomo, theta, dpath='tmp/center', cen_range=None, pad_length=0): for center in np.arange(*cen_range): rec = tomopy.recon(tomo[:, 0:1, :], theta, algorithm='gridrec', center=center) if not pad_length == 0: rec = rec[:, pad_length:-pad_length, pad_length:-pad_length] dxchange.write_tiff(np.squeeze(rec), os.path.join(dpath, '{:.2f}'.format(center-pad_length)), overwrite=True)
def search_in_folder_dnn(dest_folder, window=((600, 600), (1300, 1300)), dim_img=128, seed=1337, batch_size=50): patch_size = (dim_img, dim_img) nb_classes = 2 save_intermediate = False # number of convolutional filters to use nb_filters = 32 # size of pooling area for max pooling nb_pool = 2 # convolution kernel size nb_conv = 3 nb_evl = 100 fnames = glob.glob(os.path.join(dest_folder, '*.tiff')) fnames = np.sort(fnames) mdl = model(dim_img, nb_filters, nb_conv, nb_classes) mdl.load_weights('weight_center.h5') start_time = time.time() Y_score = np.zeros((len(fnames))) for i in range(len(fnames)): print(fnames[i]) img = dxchange.read_tiff(fnames[i]) X_evl = np.zeros((nb_evl, dim_img, dim_img)) for j in range(nb_evl): X_evl[j] = xlearn.img_window(img[window[0][0]:window[1][0], window[0][1]:window[1][1]], dim_img, reject_bg=True, threshold=1.2e-4, reset_random_seed=True, random_seed=j) X_evl = xlearn.convolve_stack(X_evl, xlearn.get_gradient_kernel()) X_evl = xlearn.nor_data(X_evl) if save_intermediate: dxchange.write_tiff(X_evl, os.path.join('debug', 'x_evl', 'x_evl_{}'.format(i)), dtype='float32', overwrite=True) X_evl = X_evl.reshape(X_evl.shape[0], 1, dim_img, dim_img) Y_evl = mdl.predict(X_evl, batch_size=batch_size) Y_score[i] = sum(np.dot(Y_evl, [0, 1])) # print('The evaluate score is:', Y_score[i]) # Y_score = sum(np.round(Y_score))/len(Y_score) ind_max = np.argmax(Y_score) best_center = float(os.path.splitext(fnames[ind_max])[0]) print('Center search done in {} s. Optimal center is {}.'.format( time.time() - start_time, best_center)) return best_center
def predict_patch(img, patch_size, patch_step, nb_filters, nb_conv, batch_size, wpath, spath): """ the cnn model for image transformation Parameters ---------- img : array The image need to be calculated Returns ------- y_img Description. """ patch_shape = (patch_size, patch_size) img = nor_data(img) pn, iy, ix = img.shape mdl = model_test(patch_size, patch_size, nb_filters, nb_conv) mdl.load_weights(wpath) for i in range(pn): print('Processing the %s th projection' % i) tstart = time.time() x_img = img[i] x_img = extract_3d(x_img, patch_shape, patch_step) x_img = np.reshape(x_img, (len(x_img), patch_size, patch_size, 1)) y_img = mdl.predict(x_img, batch_size=batch_size) y_img = np.reshape(y_img, (len(x_img), patch_size, patch_size)) y_img = reconstruct_patches(y_img, (iy, ix), patch_step) fname = spath + 'prj-' + str(i) dxchange.write_tiff(y_img, fname, dtype='float32') print('The prediction runs for %s seconds' % (time.time() - tstart))
def find_angle(rec, axis=0): rec_diff = rec.copy() # take different between layers to measure angle rec_diff[rec_diff < 0] = 0 # smooth data between IC layers and take difference for i in range(rec_diff.shape[1]): filters.gaussian_filter(rec_diff[:, i], (9, 9), output=rec_diff[:, i]) if i > 0: rec_diff[:, i - 1] -= rec_diff[:, i] # now collapse axis to correct angle (already dist on zero) layer_diff = np.sum(rec_diff, axis=axis) if axis == 2: layer_diff = np.rot90(layer_diff) # smooth out layer_diff filters.gaussian_filter(layer_diff, (3, 3), output=layer_diff) # remove sides side_trim = layer_diff.shape[0] // 4 layer_diff = layer_diff[side_trim:-side_trim] # if DEBUG: dxchange.write_tiff(layer_diff, fname='layer_diff_%d.tiff' % axis, overwrite=True) angle_deg = find_angle_from_layer_diff(layer_diff, axis=1) #FIXME: axis return angle_deg
def stitch_all_recons_local(self, save_path=None, fname='recon_local'): self.full_recon_local = np.zeros( [self.raw_sino.shape[1], self.raw_sino.shape[1]]) fov = self.inst.fov self.full_recon_local = np.pad(self.full_recon_local, fov, 'constant', constant_values=0) for sino in self.sinos_local: y, x = sino.coords print('Stitching reconstructions at ({:d}, {:d}).'.format(y, x)) y, x = map(operator.add, (y, x), (fov, fov)) dy, dx = sino.recon.shape dy2, dx2 = map(int, map(operator.div, sino.recon.shape, (2, 2))) ystart, xstart = map(operator.sub, (y, x), (dy2, dx2)) self.full_recon_local[ystart:ystart + dy, xstart:xstart + dx][ sino.recon_mask] = sino.recon[sino.recon_mask] self.full_recon_local = self.full_recon_local[fov:fov + self.raw_sino.shape[1], fov:fov + self.raw_sino.shape[1]] if save_path is not None: dxchange.write_tiff(self.full_recon_local, os.path.join(save_path, fname), overwrite=True, dtype='float32') return self.full_recon_local
def rec(self): id_start = 0 count = 1 center = 1210.750000 while (True): ids = np.mod(np.arange(id_start, self.num_proj), self.buffer_size) if (len(ids) == 0): continue id_start = self.num_proj if (len(ids) > self.buffer_size): ids = np.arange(self.buffer_size) if (count % 10 == 0): center += 10 proj_part = self.proj_buffer[ids].copy() theta_part = self.theta_buffer[ids].copy() uniqueids_part = self.uniqueids_buffer[ids].copy() print(len(ids)) # # reconstruct on GPU util.tic() rec = self.slv.recon_optimized(proj_part, theta_part, uniqueids_part, center, 1224, 1224, 1024, dbg=False) print('rec time: ', util.toc()) dxchange.write_tiff(rec, '/local/data/2020-07/Nikitin/rec_optimized/t') # reconstruction rate limit #time.sleep(2) count += 1
def get_kernel_ir(dist_nm, lmbda_nm, voxel_nm, grid_shape): """ Get Fresnel propagation kernel for IR algorithm. Parameters: ----------- simulator : :class:`acquisition.Simulator` The Simulator object. dist : float Propagation distance in cm. """ size_nm = np.array(voxel_nm) * np.array(grid_shape) k = 2 * PI / lmbda_nm ymin, xmin = np.array(size_nm)[:2] / -2. dy, dx = voxel_nm[0:2] x = np.arange(xmin, xmin + size_nm[1], dx) y = np.arange(ymin, ymin + size_nm[0], dy) x, y = np.meshgrid(x, y) try: h = np.exp(1j * k * dist_nm) / (1j * lmbda_nm * dist_nm) * np.exp( 1j * k / (2 * dist_nm) * (x**2 + y**2)) H = np_fftshift(fft2(h)) * voxel_nm[0] * voxel_nm[1] dxchange.write_tiff(x, '2d_512/monitor_output/x', dtype='float32', overwrite=True) except: h = tf.exp(1j * k * dist_nm) / (1j * lmbda_nm * dist_nm) * tf.exp( 1j * k / (2 * dist_nm) * (x**2 + y**2)) # h = tf.convert_to_tensor(h, dtype='complex64') H = fftshift(tf.fft2d(h)) * voxel_nm[0] * voxel_nm[1] return H
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 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 write_first_frames(folder='.', data_format='aps_32id'): flist = glob.glob(os.path.join(folder, '*.h5')) for f in flist: print(f) dat, flt, drk, _ = read_data_adaptive(os.path.join(folder, f), proj=(0, 1), data_format=data_format) dat = tomopy.normalize(dat, flt, drk) f = os.path.splitext(os.path.basename(f))[0] dxchange.write_tiff(dat, os.path.join('first_frames', f), dtype='float32', overwrite=True)
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 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 fourier_ring_correlation(obj, ref, step_size=1, save_path='frc', save_mask=False): if not os.path.exists(save_path): os.makedirs(save_path) radius_max = int(min(obj.shape) / 2) f_obj = np_fftshift(fft2(obj)) f_ref = np_fftshift(fft2(ref)) f_prod = f_obj * np.conjugate(f_ref) f_obj_2 = np.real(f_obj * np.conjugate(f_obj)) f_ref_2 = np.real(f_ref * np.conjugate(f_ref)) radius_ls = np.arange(1, radius_max, step_size) fsc_ls = [] np.save(os.path.join(save_path, 'radii.npy'), radius_ls) for rad in radius_ls: print(rad) if os.path.exists( os.path.join(save_path, 'mask_rad_{:04d}.tiff'.format(int(rad)))): mask = dxchange.read_tiff( os.path.join(save_path, 'mask_rad_{:04d}.tiff'.format(int(rad)))) else: mask = generate_ring(obj.shape, rad, anti_aliasing=2) if save_mask: dxchange.write_tiff( mask, os.path.join(save_path, 'mask_rad_{:04d}.tiff'.format(int(rad))), dtype='float32', overwrite=True) fsc = abs(np.sum(f_prod * mask)) fsc /= np.sqrt(np.sum(f_obj_2 * mask) * np.sum(f_ref_2 * mask)) fsc_ls.append(fsc) np.save(os.path.join(save_path, 'fsc.npy'), fsc_ls) matplotlib.rcParams['pdf.fonttype'] = 'truetype' fontProperties = { 'family': 'serif', 'serif': ['Times New Roman'], 'weight': 'normal', 'size': 12 } plt.rc('font', **fontProperties) plt.plot(radius_ls.astype(float) / radius_ls[-1], fsc_ls) plt.xlabel('Spatial frequency (1 / Nyquist)') plt.ylabel('FRC') plt.savefig(os.path.join(save_path, 'frc.pdf'), format='pdf')
def initialize_guess_3d(dev, ini_kind, grid_path, f_grid, recon_path, f_recon_grid, f_initial_guess, init_const=0.5): if ini_kind == "rand": X = np.load(os.path.join(grid_path, f_grid)).astype( np.float32 ) # The shape of X(3d) is initally (5,5,5,5) = (n_element, n_z, n_x, n_y) X = tc.from_numpy(X).float().to(dev) X = X + 0.1 * tc.rand( X.shape[0], X.shape[1], X.shape[2], X.shape[3], device=dev) X = tc.clamp(X, 0, 10) elif ini_kind == "randn": X = np.load(os.path.join(grid_path, f_grid)).astype \ (np.float32) # The shape of X(3d) is initally (5,5,5,5) = (n_element, n_z, n_x, n_y) X = tc.from_numpy(X).float().to(dev) X = X + 0.1 * tc.randn( X.shape[0], X.shape[1], X.shape[2], X.shape[3], device=dev) X = tc.clamp(X, 0, 10) elif ini_kind == "const": X = np.load(os.path.join(grid_path, f_grid)).astype \ (np.float32) # X is loaded just to get the shape of the model X X = tc.from_numpy(X).float().to(dev) X = tc.zeros( X.shape[0], X.shape[1], X.shape[2], X.shape[3], device=dev) + init_const else: print( "Please specify the correct kind of the initialization condition.") ## Save the initial guess for future reference np.save(os.path.join(recon_path, f_initial_guess) + '.npy', X.cpu()) dxchange.write_tiff(X.cpu(), os.path.join(recon_path, f_initial_guess), dtype='float32', overwrite=True) ## Save the initial guess which will be used in reconstruction and will be updated to the current reconstructing result np.save(os.path.join(recon_path, f_recon_grid) + '.npy', X.cpu()) dxchange.write_tiff(X.cpu(), os.path.join(recon_path, f_recon_grid), dtype='float32', overwrite=True) return X
def correct_abs_intensity(self, ref): mask = tomopy.misc.corr._get_mask(self.recon.shape[0], self.recon.shape[1], 0.6) local_mean = np.mean(self.recon[mask]) y0, x0 = self.coords yy, xx = map(int, (self.recon.shape[0] / 2, self.recon.shape[1] / 2)) ref_spot = ref[y0 - yy:y0 - yy + self.recon.shape[0], x0 - xx:x0 - xx + self.recon.shape[1]] ref_mean = np.mean(ref_spot[mask]) dxchange.write_tiff(self.recon, 'tmp/loc') dxchange.write_tiff(ref_spot, 'tmp/ref') self.recon = self.recon + (ref_mean - local_mean)
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 find_angle_mpi(mpi_rec, axis=0): from mpiarray import MpiArray mpi_rec_diff = mpi_rec.copy(deep=True) # take different between layers to measure angle rec_diff = mpi_rec_diff.scatter(0, 5) # add padding rec_diff[rec_diff < 0] = 0 # smooth data between IC layers and take difference for i in range(rec_diff.shape[1]): filters.gaussian_filter(rec_diff[:, i], (9, 9), output=rec_diff[:, i]) if i > 0: rec_diff[:, i - 1] -= rec_diff[:, i] rec_diff = mpi_rec_diff.scatter(0) # remove padding # now collapse axis to correct angle (already dist on zero) if axis == 0: layer_diff = np.sum(rec_diff, axis=0, keepdims=True) layer_diff = MpiArray(layer_diff).allgather() layer_diff = np.sum(layer_diff, axis=axis) layer_diff /= rec_diff.shape[0] else: layer_diff = np.sum(rec_diff, axis=axis) layer_diff = MpiArray(layer_diff).allgather() layer_diff = np.rot90(layer_diff, axes=(1, 0)) #TODO: currently specific to axis=2 # smooth out layer_diff filters.gaussian_filter(layer_diff, (3, 3), output=layer_diff) # remove sides side_trim = layer_diff.shape[0] // 4 layer_diff = layer_diff[side_trim:-side_trim] if mpi_rec.mpi_rank == 0: # TODO: need to return layer_diff or ignore, don't write to disk dxchange.write_tiff(layer_diff, fname='layer_diff_%d.tiff' % axis, overwrite=True) angle_deg = find_angle_from_layer_diff(layer_diff, axis=1) #FIXME: axis # find where the layer starts and stops rotated_layer_diff = rotate(layer_diff, angle_deg, reshape=False) start, end = find_extent_from_layer_diff(rotated_layer_diff, axis=1) # NOTE: don't forget to add back on the trimmed off amount from earlier! start += side_trim end += side_trim else: angle_deg, start, end = None, None, None angle_deg = mpi_rec.comm.bcast(angle_deg, root=0) start = mpi_rec.comm.bcast(start, root=0) end = mpi_rec.comm.bcast(end, root=0) return angle_deg, start, end
def recon_full_tomosaic(self, save_path=None, fname='recon_tomosaic', mask_ratio=1): fov = self.stitched_sino_tomosaic.sinogram.shape[1] print('Reconstructing full tomosaic sinogram.') self.stitched_sino_tomosaic.reconstruct(mask_ratio=mask_ratio) self.full_recon_tomosaic = self.stitched_sino_tomosaic.recon if save_path is not None: dxchange.write_tiff(self.full_recon_tomosaic, os.path.join(save_path, fname), overwrite=True, dtype='float32') return self.full_recon_tomosaic
def write_first_frames(ui): root = os.getcwd() os.chdir(ui.raw_folder) try: os.mkdir('first_frames') except: pass for i in ui.filelist: ui.boxMetaOut.insert(END, i + '\n') prj, flt, drk = dxchange.read_aps_32id(i, proj=(0, 1)) prj = tomopy.normalize(prj, flt, drk) prj = preprocess(prj) dxchange.write_tiff( prj, os.path.join('first_frames', os.path.splitext(i)[0]))
def main(arg): parser = argparse.ArgumentParser() parser.add_argument("fname", help="directory containing multiple datasets or file name of a single dataset: /data/ or /data/sample.h5") parser.add_argument("--element", nargs='?', type=str, default="Ca", help="element selection (default Si)") parser.add_argument("--output_fname", nargs='?', type=str, default="./data", help="output file path and prefix (default ./tmp/data)") parser.add_argument("--output_fformat", nargs='?', type=str, default="hdf", help="output file format: hdf or tiff (default hdf)") parser.add_argument("--theta_index", nargs='?', type=int, default=657, help="theta_index: 2-ID-E: 663; 2-ID-E prior 2017: 657; BNP 8; (default 657)") args = parser.parse_args() fname = args.fname element = args.element out = args.output_fname fformat = args.output_fformat theta_index = args.theta_index if os.path.isfile(fname): proj, theta = read_projection(fname, element, theta_index) print ("theta:", theta) print ("projection shape", proj.shape) elif os.path.isdir(fname): # Add a trailing slash if missing top = os.path.join(fname, '') h5_file_list = list(filter(lambda x: x.endswith(('.h5', '.hdf')), os.listdir(top))) # elements = read_elements(top+h5_file_list[0]) # print ("Elements in the files: ", elements) proj, theta = read_projection(top+h5_file_list[0], element, theta_index) print("\n (element, theta.shape, proj.shape)", element, len(h5_file_list), proj.shape) data = zeros([len(h5_file_list), proj.shape[0], proj.shape[1]]) theta = zeros([len(h5_file_list)]) for i, fname in enumerate(h5_file_list): proj, theta_image = read_projection(top+fname, element, theta_index) data[i, :, :] = proj theta[i] = theta_image if fformat == "tiff": dxchange.write_tiff(proj, out + "_" + element + ".tiff") if fformat == "hdf": write_dxfile(out + "_" + element + ".h5", data, theta, element) else: print("Directory or File Name does not exist: ", fname)
def write_layers_to_file_mpi(mpi_layers, path="layers"): layers = mpi_layers.scattermovezero(1) layer_names = ["contacts"] for i in range(1, 100): layer_names.append("metal%02d" % i) layer_names.append("via%02d-%02d" % (i, i + 1)) # layer_names.reverse() # make dirs if necessary, if mpi_layers.mpi_rank == 0: os.makedirs(str(path), exist_ok=True) mpi_layers.comm.Barrier() for i in range(layers.shape[0]): l = mpi_layers.shape[0] - 1 - (i + mpi_layers.offset) dxchange.write_tiff(layers[i], fname='%s/%02d-%s' % (str(path), l, layer_names[l]), overwrite=True)
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 recon_hdf5_mpi(src_fanme, dest_folder, sino_range, sino_step, center_vec, shift_grid, dtype='float32', algorithm='gridrec', tolerance=1, save_sino=False, sino_blur=None, **kwargs): """ Reconstruct a single tile, or fused HDF5 created using util/total_fusion. MPI supported. """ raise DeprecationWarning if rank == 0: if not os.path.exists(dest_folder): os.mkdir(dest_folder) sino_ini = int(sino_range[0]) sino_end = int(sino_range[1]) f = h5py.File(src_fanme) dset = f['exchange/data'] full_shape = dset.shape theta = tomopy.angles(full_shape[0]) center_vec = np.asarray(center_vec) sino_ls = np.arange(sino_ini, sino_end, sino_step, dtype='int') grid_bins = np.ceil(shift_grid[:, 0, 0]) t0 = time.time() alloc_set = allocate_mpi_subsets(sino_ls.size, size, task_list=sino_ls) for slice in alloc_set[rank]: print(' Rank {:d}: reconstructing {:d}'.format(rank, slice)) grid_line = np.digitize(slice, grid_bins) grid_line = grid_line - 1 center = center_vec[grid_line] data = dset[:, slice, :] if sino_blur is not None: data = gaussian_filter(data, sino_blur) data = data.reshape([full_shape[0], 1, full_shape[2]]) data[np.isnan(data)] = 0 data = data.astype('float32') if save_sino: dxchange.write_tiff(data[:, slice, :], fname=os.path.join(dest_folder, 'sino/recon_{:05d}_{:d}.tiff').format(slice, center)) # data = tomopy.remove_stripe_ti(data) rec = tomopy.recon(data, theta, center=center, algorithm=algorithm, **kwargs) # rec = tomopy.remove_ring(rec) rec = tomopy.remove_outlier(rec, tolerance) rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) dxchange.write_tiff(rec, fname='{:s}/recon/recon_{:05d}_{:d}'.format(dest_folder, slice, center), dtype=dtype) print('Rank {:d} finished in {:.2f} s.'.format(rank, time.time()-t0)) return
def reorganize_tiffs(): tiff_list = glob.glob('*.tiff') for fname in tiff_list: print('Now processing ' + str(fname)) # make downsampled subdirectories for ds in [1, 2, 4]: # create downsample folder if not existing folder_name = 'tiff_' + str(ds) + 'x' if not os.path.exists(folder_name): os.mkdir(folder_name) # copy file if downsample level is 1 if ds == 1: shutil.copyfile(fname, folder_name + '/' + fname) # otherwise perform downsampling else: temp = dxchange.read_tiff(fname).flatten() temp = image_downsample(temp, ds) dxchange.write_tiff(temp, folder_name + '/' + 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 retrieve_phase_far_field(src_fname, save_path, output_fname=None, pad_length=256, n_epoch=100, learning_rate=0.001): # raw data is assumed to be centered at zero frequency prj_np = dxchange.read_tiff(src_fname) if output_fname is None: output_fname = os.path.basename( os.path.splitext(src_fname)[0]) + '_recon' # take modulus and inverse shift prj_np = ifftshift(np.sqrt(prj_np)) obj_init = np.random.normal(50, 10, list(prj_np.shape) + [2]) obj = tf.Variable(obj_init, dtype=tf.float32, name='obj') prj = tf.constant(prj_np, name='prj') obj_real = tf.cast(obj[:, :, 0], dtype=tf.complex64) obj_imag = tf.cast(obj[:, :, 1], dtype=tf.complex64) # obj_pad = tf.pad(obj, [[pad_length, pad_length], [pad_length, pad_length], [0, 0]], mode='SYMMETRIC') det = tf.fft2d(obj_real + 1j * obj_imag, name='detector_plane') loss = tf.reduce_mean(tf.squared_difference(tf.abs(det), prj, name='loss')) sess = tf.Session() optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) optimizer = optimizer.minimize(loss) sess.run(tf.global_variables_initializer()) for i_epoch in range(n_epoch): t0 = time.time() _, current_loss = sess.run([optimizer, loss]) print('Iteration {}: loss = {}, Δt = {} s.'.format( i_epoch, current_loss, time.time() - t0)) det_final = sess.run(det) obj_final = sess.run(obj) res = np.linalg.norm(obj_final, 2, axis=2) dxchange.write_tiff(res, os.path.join(save_path, output_fname), dtype='float32', overwrite=True) dxchange.write_tiff(fftshift(np.angle(det_final)), os.path.join(save_path, 'detector_phase'), dtype='float32', overwrite=True) dxchange.write_tiff(fftshift(np.abs(det_final)**2), os.path.join(save_path, 'detector_mag'), dtype='float32', overwrite=True) return
def fourier_ring_correlation(obj, ref, step_size=1, save_path=None, save_mask=True, save_fname='fsc', threshold_curve=False): if not os.path.exists(save_path): os.makedirs(save_path) if obj.ndim == 2: fft_func = fft2 gen_mask = generate_ring gen_kwargs = {'anti_aliasing: 2'} elif obj.ndim == 3: fft_func = fftn gen_mask = generate_shell gen_kwargs = {} radius_max = min(obj.shape) // 2 f_obj = np_fftshift(fft_func(obj)) f_ref = np_fftshift(fft_func(ref)) f_prod = f_obj * np.conjugate(f_ref) f_obj_2 = np.real(f_obj * np.conjugate(f_obj)) f_ref_2 = np.real(f_ref * np.conjugate(f_ref)) radius_ls = np.arange(1, radius_max, step_size) fsc_ls = [] if save_path is not None: np.save(os.path.join(save_path, 'radii.npy'), radius_ls) for rad in radius_ls: if os.path.exists(os.path.join(save_path, 'mask_rad_{:04d}.tiff'.format(int(rad)))): mask = dxchange.read_tiff(os.path.join(save_path, 'mask_rad_{:04d}.tiff'.format(int(rad)))) else: mask = gen_mask(obj.shape, rad, **gen_kwargs) if save_mask: dxchange.write_tiff(mask, os.path.join(save_path, 'mask_rad_{:04d}.tiff'.format(int(rad))), dtype='float32', overwrite=True) fsc = abs(np.sum(f_prod * mask)) fsc /= np.sqrt(np.sum(f_obj_2 * mask) * np.sum(f_ref_2 * mask)) fsc_ls.append(fsc) if save_path is not None: np.save(os.path.join(save_path, '{}.npy'.format(save_fname)), fsc_ls) return np.array(fsc_ls)
def admm(self, data, h, e, psi, phi, lamd, mu, u, alpha, piter, titer, NITER, model): data = data.copy() * self.coefdata # normalization # init penalties rho, tau = 1, 1 # Lagrangian for each iter lagr = cp.zeros([NITER, 7], dtype="float32") lagr0 = self.take_lagr(psi, phi, data, h, e, lamd, mu, tau, rho, alpha, model) for m in range(NITER): # keep previous iteration for penalty updates h0, e0 = h, e psi = self.cg_ptycho_batch(data, psi, h, lamd, rho, piter, model) # tomography problem xi0, xi1, K, pshift = self.takexi(psi, phi, lamd, mu, rho, tau) u = self.cg_tomo(xi0, xi1, K, u, rho, tau, titer) # regularizer problem phi = self.solve_reg(u, mu, tau, alpha) # h,e updates h = self.exptomo(self.fwd_tomo(u)) * cp.exp(1j * pshift) e = self.fwd_reg(u) # lambda, mu updates lamd = lamd + rho * (h - psi) mu = mu + tau * (e - phi) # update rho, tau for a faster convergence rho, tau = self.update_penalty(psi, h, h0, phi, e, e0, rho, tau) # Lagrangians difference between two iterations if (np.mod(m, 10) == 0): lagr[m] = self.take_lagr(psi, phi, data, h, e, lamd, mu, alpha, rho, tau, model) print( "%d/%d) rho=%.2e, tau=%.2e, Lagr terms diff: %.2e %.2e %.2e %.2e %.2e %.2e, Sum: %.2e" % (m, NITER, rho, tau, *(lagr0 - lagr[m]))) lagr0 = lagr[m] name = 'reg'+str(model)+str(piter)+str(titer) + \ str(NITER)+str(np.amax(data)) dxchange.write_tiff(u[u.shape[0] // 2].imag.get(), 'betap/beta' + name) dxchange.write_tiff(u[u.shape[0] // 2].real.get(), 'deltap/delta' + name) dxchange.write_tiff(cp.abs(psi).get(), 'psip/psiamp' + name) dxchange.write_tiff( cp.angle(psi).get(), 'psip/psiangle' + name) lagrr = self.take_lagr(psi, phi, data, h, e, lamd, mu, tau, rho, alpha, model) print(lagrr) return u, psi, lagrr
def gen_cyl_data(n, ntheta, pprot, adef, noise=False): """Generate cylinders in 3D volume, deform them, compute projections, save to disk""" print('Start data generation') nz = n # vertical object size ne = 3 * n // 2 # padded size # generate cylinders f = cyl(0.01, [0.1, 0.2], n, 30, 45, 30, n) f = f + 1 * cyl(0.01, [-0.1, -0.2], n, -30, -15, 30, n) f = f + 1 * cyl(0.01, [-0.3, -0.3], n, -30, -95, -40, n) f = f + cyl(0.01, [-0.4, 0.4], n, 15, 30, 90, n) f = f + 1 * cyl(0.01, [0.4, -0.45], n, 90, 30, 90, n) f = f + 1 * cyl(0.01, [0.2, -0.25], n, -90, 30, 15, n) f = f + 1 * cyl(0.01, [0.3, -0.15], n, -10, 110, -15, n) f[f > 1] = 1 f[f < 0] = 0 [x, y] = np.mgrid[-ne // 2:ne // 2, -ne // 2:ne // 2] circ = (2 * x / ne)**2 + (2 * y / ne)**2 < 1 fe = np.zeros([nz, ne, ne], dtype='float32') fe[:, ne // 2 - n // 2:ne // 2 + n // 2, ne // 2 - n // 2:ne // 2 + n // 2] = f if (noise): fe += (ndimage.filters.gaussian_filter( np.random.random(fe.shape), 3, truncate=8).astype('float32') - 0.5) * 12 f = (fe - np.min(fe)) / (np.max(fe) - np.min(fe)) * circ namepart = '_pprot' + str(pprot) + '_noise' + str(noise) dxchange.write_tiff(f, 'data/init_object' + namepart, overwrite=True) # generate angles theta = np.array(gen_ang(ntheta, pprot)).astype('float32') / 360 * np.pi * 2 # deform data points = [3, 3, 3] r = np.random.rand(3, *points) # displacement in 3d # compute data without deformation with SolverTomo(theta, ntheta, nz, ne, 32, n / 2 + (ne - n) / 2, 1) as tslv: data = tslv.fwd_tomo_batch(f) dxchange.write_tiff(data, 'data/data' + namepart, overwrite=True) # compute data with deformation data = deform_data_batch(f, theta, r * adef)[:, :, ne // 2 - n // 2:ne // 2 + n // 2] dxchange.write_tiff(data, 'data/deformed_data' + namepart, overwrite=True) # save angles, and displacement vectors np.save('data/theta' + namepart, theta) print('generated data have been written in data/')
def write_center( tomo, theta, dpath='tmp/center', cen_range=None, ind=None, mask=False, ratio=1., sinogram_order=False): """ Save images reconstructed with a range of rotation centers. Helps finding the rotation center manually by visual inspection of images reconstructed with a set of different centers.The output images are put into a specified folder and are named by the center position corresponding to the image. Parameters ---------- tomo : ndarray 3D tomographic data. theta : array Projection angles in radian. dpath : str, optional Folder name to save output images. cen_range : list, optional [start, end, step] Range of center values. ind : int, optional Index of the slice to be used for reconstruction. mask : bool, optional If ``True``, apply a circular mask to the reconstructed image to limit the analysis into a circular region. ratio : float, optional The ratio of the radius of the circular mask to the edge of the reconstructed image. sinogram_order: bool, optional Determins whether data is a stack of sinograms (True, y-axis first axis) or a stack of radiographs (False, theta first axis). """ tomo = dtype.as_float32(tomo) theta = dtype.as_float32(theta) if sinogram_order: dy, dt, dx = tomo.shape else: dt, dy, dx = tomo.shape if ind is None: ind = dy // 2 if cen_range is None: center = np.arange(dx / 2 - 5, dx / 2 + 5, 0.5) else: center = np.arange(*cen_range) stack = dtype.empty_shared_array((len(center), dt, dx)) for m in range(center.size): if sinogram_order: stack[m] = tomo[ind] else: stack[m] = tomo[:, ind, :] # Reconstruct the same slice with a range of centers. rec = recon(stack, theta, center=center, sinogram_order=True, algorithm='gridrec', nchunk=1) # Apply circular mask. if mask is True: rec = circ_mask(rec, axis=0) # Save images to a temporary folder. for m in range(len(center)): fname = os.path.join( dpath, str('{0:.2f}'.format(center[m]) + '.tiff')) dxchange.write_tiff(rec[m], fname=fname, overwrite=True)
theta = np.arange(0, 180., 180. / n_proj) # Set data collection angles as equally spaced between 0-180 degrees. start_angle = 0 start_angle_unit = 'deg' end_angle = 180 end_angle_unit = 'deg' angular_step_unit = 'deg' # Phantom generation end time end_date = iso_time() # Write ground_truth ground_truth = discrete_phantom(phantom, ccd_x, prop='mass_atten') fname_gt='tomobank/phantoms/' + tomobank_id + '/' + tomobank_id + '_ground_truth' dxchange.write_tiff(ground_truth, fname=fname_gt, dtype='float32') # Save into a data-exchange file. if os.path.isfile(fname): print ("Data Exchange file already exists: ", fname) else: # Create new folder. dirPath = os.path.dirname(fname) if not os.path.exists(dirPath): os.makedirs(dirPath) # Open DataExchange file f = dx.File(fname, mode='w') # Write the Data Exchange HDF5 file. f.add_entry(dx.Entry.experimenter(affiliation={'value': experimenter_affiliation}))
prj, flat, dark, theta = dxchange.read_aps_32id(file_name, proj=(0, nProj+1, 1210)) # open proj from 0 to 720 deg with 180deg step --> 5 proj prj = tomopy.normalize(prj, flat, dark) prj = tomopy.misc.corr.remove_neg(prj, val=0.000) prj = tomopy.misc.corr.remove_nan(prj, val=0.000) prj[np.where(prj == np.inf)] = 0.000 prj[np.where(prj > 1.0)] = 1 prj = tomopy.downsample(prj, level=binning) prj = tomopy.downsample(prj, level=binning, axis=1) prj = ndimage.median_filter(prj,footprint=np.ones((1, medfilt_size, medfilt_size))) #prj = np.squeeze(prj); avg = np.mean(prj); std = np.std(prj) #plt.imshow(prj, cmap='gray', aspect="auto", interpolation='none', vmin=avg-3*std, vmax=avg+3*std), plt.colorbar(), plt.show() ##plt.imshow(prj, cmap='gray', aspect="auto", interpolation='none', vmin=0, vmax=0.1), plt.colorbar(), plt.show() dxchange.write_tiff(prj, fname='/local/dataraid/2018-06/DeAndrade/2018-06-19/brain_petrapoxy/tmp/data', dtype='float32', overwrite=False) sino_st = 750 sino_end = 1250 if 0: prj, flat, dark, theta = dxchange.read_aps_32id(file_name, sino=(sino_st,sino_end,1), proj=(0, 1210, 1)) # open proj from 0 to 720 deg with 180deg step --> 5 proj prj = tomopy.normalize(prj, flat, dark) 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)
mdl = model(dim_img, nb_filters, nb_conv) mdl.load_weights('transform_training_weights.h5') print('Predicting') folder = '../../test/test_data/' files = [f for f in sorted(os.listdir(folder)) if re.match(r'.+.tiff', f)] for fname in files: time_start = time.time() sname = fname.split('.') time_start = time.time() fname_save = folder + sname[0] + '_result' img_test = dxchange.read_tiff(folder + fname) img_rec = predict(mdl, img_test, patch_size, patch_step, batch_size, dim_img) dxchange.write_tiff(img_rec, fname_save, dtype='float32') print(time.time()-time_start)
prefix = '*' psize_cm = 99.8e-7 dist_cm_ls = np.array([7.36, 7.42, 7.70]) energy_ev = 17500 alpha_1 = 5e-4 alpha_2 = 1e-16 energy_kev = energy_ev * 1e-3 flist, n_theta, n_dists, raw_img_shape = adorym.parse_source_folder( src_dir, prefix) for i_theta in range(n_theta): prj_ls = [] for i_dist in range(n_dists): img = np.squeeze(dxchange.read_tiff(flist[i_theta * n_dists + i_dist])) prj_ls.append(img) phase = multidistance_ctf(prj_ls, dist_cm_ls, psize_cm, energy_kev, kappa=50, sigma_cut=0.01, alpha_1=alpha_1, alpha_2=alpha_2) dxchange.write_tiff(np.squeeze(phase), os.path.join( 'data_ctf_orig', os.path.basename(flist[i_theta * n_dists])), dtype='float32', overwrite=True)
def create_ptychography_data(energy_ev, psize_cm, n_theta, phantom_path, save_folder, fname, probe_pos, probe_type='gaussian', probe_size=(72, 72), wavefront_initial=None, theta_st=0, theta_end=2 * PI, probe_circ_mask=0.9, n_dp_batch=20, **kwargs): """ If probe_type is 'gaussian', supply parameters 'probe_mag_sigma', 'probe_phase_sigma', 'probe_phase_max'. """ def rotate_and_project(i, obj): # obj_rot = apply_rotation(obj, coord_ls[rand_proj], 'arrsize_64_64_64_ntheta_500') obj_rot = tf_rotate(obj, theta_ls_tensor[i], interpolation='BILINEAR') probe_pos_batch_ls = np.array_split( probe_pos, int(np.ceil(float(n_pos) / n_dp_batch))) # probe_pos_batch_ls = np.array_split(probe_pos, 6) exiting_ls = [] # pad if needed pad_arr = np.array([[0, 0], [0, 0]]) if probe_pos[:, 0].min() - probe_size_half[0] < 0: pad_len = probe_size_half[0] - probe_pos[:, 0].min() obj_rot = tf.pad(obj_rot, ((pad_len, 0), (0, 0), (0, 0), (0, 0)), mode='CONSTANT') pad_arr[0, 0] = pad_len if probe_pos[:, 0].max() + probe_size_half[0] > obj_size[0]: pad_len = probe_pos[:, 0].max() + probe_size_half[0] - obj_size[0] obj_rot = tf.pad(obj_rot, ((0, pad_len), (0, 0), (0, 0), (0, 0)), mode='CONSTANT') pad_arr[0, 1] = pad_len if probe_pos[:, 1].min() - probe_size_half[1] < 0: pad_len = probe_size_half[1] - probe_pos[:, 1].min() obj_rot = tf.pad(obj_rot, ((0, 0), (pad_len, 0), (0, 0), (0, 0)), mode='CONSTANT') pad_arr[1, 0] = pad_len if probe_pos[:, 1].max() + probe_size_half[1] > obj_size[1]: pad_len = probe_pos[:, 1].max() + probe_size_half[0] - obj_size[1] obj_rot = tf.pad(obj_rot, ((0, 0), (0, pad_len), (0, 0), (0, 0)), mode='CONSTANT') pad_arr[1, 1] = pad_len for k, pos_batch in enumerate(probe_pos_batch_ls): subobj_ls = [] for j, pos in enumerate(pos_batch): pos = [int(x) for x in pos] pos[0] = pos[0] + pad_arr[0, 0] pos[1] = pos[1] + pad_arr[1, 0] subobj = obj_rot[pos[0] - probe_size_half[0]:pos[0] - probe_size_half[0] + probe_size[0], pos[1] - probe_size_half[1]:pos[1] - probe_size_half[1] + probe_size[1], :, :] subobj_ls.append(subobj) subobj_ls = tf.stack(subobj_ls) exiting = multislice_propagate_batch( subobj_ls[:, :, :, :, 0], subobj_ls[:, :, :, :, 1], probe_real, probe_imag, energy_ev, psize_cm, h=h, free_prop_cm='inf', obj_batch_shape=[len(pos_batch), *probe_size, obj_size[-1]]) exiting_ls.append(exiting) exiting_ls = tf.concat(exiting_ls, 0) return exiting_ls config = tf.ConfigProto(device_count={'GPU': 0}) sess = tf.Session(config=config) i = tf.placeholder(dtype=tf.int32) # read model grid_delta = np.load(os.path.join(phantom_path, 'grid_delta.npy')) grid_beta = np.load(os.path.join(phantom_path, 'grid_beta.npy')) img_dim = grid_delta.shape probe_size_half = (np.array(probe_size) / 2).astype('int') obj_init = np.zeros(np.append(img_dim, 2)) obj_init[:, :, :, 0] = grid_delta obj_init[:, :, :, 1] = grid_beta obj_size = grid_delta.shape obj = tf.Variable(initial_value=obj_init, dtype=tf.float32) # list of angles theta_ls = -np.linspace(theta_st, theta_end, n_theta) theta_ls_tensor = tf.constant(theta_ls, dtype='float32') n_pos = len(probe_pos) probe_pos = np.array(probe_pos) # generate Fresnel kernel voxel_nm = np.array([psize_cm] * 3) * 1.e7 lmbda_nm = 1240. / energy_ev delta_nm = voxel_nm[-1] kernel = get_kernel(delta_nm, lmbda_nm, voxel_nm, probe_size) h = tf.convert_to_tensor(kernel, dtype=tf.complex64, name='kernel') # create data file flag_overwrite = 'y' if os.path.exists(os.path.join(save_folder, fname)): flag_overwrite = input('File exists. Overwrite? (y/n) ') if flag_overwrite in ['y', 'Y']: f = h5py.File(os.path.join(save_folder, fname), 'w') grp = f.create_group('exchange') dat = grp.create_dataset('data', shape=(n_theta, len(probe_pos), probe_size[0], probe_size[1]), dtype=np.complex64) else: return # create probe if probe_type == 'gaussian': py = np.arange(probe_size[0]) - (probe_size[0] - 1.) / 2 px = np.arange(probe_size[1]) - (probe_size[1] - 1.) / 2 pxx, pyy = np.meshgrid(px, py) probe_mag = np.exp(-(pxx**2 + pyy**2) / (2 * kwargs['probe_mag_sigma']**2)) probe_phase = kwargs['probe_phase_max'] * np.exp( -(pxx**2 + pyy**2) / (2 * kwargs['probe_phase_sigma']**2)) probe_real, probe_imag = mag_phase_to_real_imag(probe_mag, probe_phase) if probe_circ_mask is not None: probe_real, probe_imag = tomopy.circ_mask(np.array( [probe_real, probe_imag]), axis=0, ratio=probe_circ_mask) probe_mask = tomopy.circ_mask( np.ones_like(probe_real)[np.newaxis, :, :], axis=0, ratio=probe_circ_mask) probe_mask = gaussian_filter(np.squeeze(probe_mask), 3) probe_mask = tf.constant(probe_mask, dtype=tf.complex64) waveset = rotate_and_project(i, obj) for ii, theta in enumerate(theta_ls): print('Theta: {}'.format(ii)) sess.run(tf.global_variables_initializer()) waveset_out = np.array(sess.run(waveset, feed_dict={i: ii})) dat[ii, :, :, :] = waveset_out dxchange.write_tiff(abs(waveset_out), os.path.join(save_folder, 'diffraction_dat', 'mag_{:05d}'.format(ii)), overwrite=True, dtype='float32') f.close() return
def create_ptychography_data_batch_numpy(energy_ev, psize_cm, n_theta, phantom_path, save_folder, fname, probe_pos, probe_type='gaussian', probe_size=(72, 72), wavefront_initial=None, theta_st=0, theta_end=2 * PI, probe_circ_mask=0.9, minibatch_size=20, **kwargs): """ If probe_type is 'gaussian', supply parameters 'probe_mag_sigma', 'probe_phase_sigma', 'probe_phase_max'. """ def rotate_and_project(theta, obj): obj_rot = sp_rotate(obj, theta, reshape=False, axes=(1, 2)) # pad if needed pad_arr = np.array([[0, 0], [0, 0]]) if probe_pos[:, 0].min() - probe_size_half[0] < 0: pad_len = probe_size_half[0] - probe_pos[:, 0].min() obj_rot = np.pad(obj_rot, ((pad_len, 0), (0, 0), (0, 0), (0, 0)), mode='constant') pad_arr[0, 0] = pad_len if probe_pos[:, 0].max() + probe_size_half[0] > img_dim[0]: pad_len = probe_pos[:, 0].max() + probe_size_half[0] - img_dim[0] obj_rot = np.pad(obj_rot, ((0, pad_len), (0, 0), (0, 0), (0, 0)), mode='constant') pad_arr[0, 1] = pad_len if probe_pos[:, 1].min() - probe_size_half[1] < 0: pad_len = probe_size_half[1] - probe_pos[:, 1].min() obj_rot = np.pad(obj_rot, ((0, 0), (pad_len, 0), (0, 0), (0, 0)), mode='constant') pad_arr[1, 0] = pad_len if probe_pos[:, 1].max() + probe_size_half[1] > img_dim[1]: pad_len = probe_pos[:, 1].max() + probe_size_half[0] - img_dim[1] obj_rot = np.pad(obj_rot, ((0, 0), (0, pad_len), (0, 0), (0, 0)), mode='constant') pad_arr[1, 1] = pad_len for k, pos_batch in tqdm(enumerate(probe_pos_batches)): grid_delta_ls = [] grid_beta_ls = [] for j, pos in enumerate(pos_batch): pos = np.array(pos, dtype=int) pos[0] = pos[0] + pad_arr[0, 0] pos[1] = pos[1] + pad_arr[1, 0] subobj = obj_rot[pos[0] - probe_size_half[0]:pos[0] - probe_size_half[0] + probe_size[0], pos[1] - probe_size_half[1]:pos[1] - probe_size_half[1] + probe_size[1], :, :] grid_delta_ls.append(subobj[:, :, :, 0]) grid_beta_ls.append(subobj[:, :, :, 1]) grid_delta_ls = np.array(grid_delta_ls) grid_beta_ls = np.array(grid_beta_ls) exiting = multislice_propagate_batch_numpy(grid_delta_ls, grid_beta_ls, probe_real, probe_imag, energy_ev, psize_cm, free_prop_cm='inf', obj_batch_shape=[ len(pos_batch), probe_size[0], probe_size[1], grid_delta.shape[-1] ]) if probe_circ_mask is not None: exiting = exiting * probe_mask if k == 0: exiting_ls = np.copy(exiting) else: exiting_ls = np.vstack([exiting_ls, exiting]) return exiting_ls probe_pos = np.array(probe_pos) n_pos = len(probe_pos) minibatch_size = min([minibatch_size, n_pos]) n_batch = np.ceil(float(n_pos) / minibatch_size) print(n_pos, minibatch_size, n_batch) probe_pos_batches = np.array_split(probe_pos, n_batch) # read model grid_delta = np.load(os.path.join(phantom_path, 'grid_delta.npy')) grid_beta = np.load(os.path.join(phantom_path, 'grid_beta.npy')) img_dim = grid_delta.shape probe_size_half = (np.array(probe_size) / 2).astype('int') obj = np.zeros(np.append(img_dim, 2)) obj[:, :, :, 0] = grid_delta obj[:, :, :, 1] = grid_beta # list of angles theta_ls = -np.linspace(theta_st, theta_end, n_theta) theta_ls = np.rad2deg(theta_ls) # create data file flag_overwrite = 'y' if os.path.exists(os.path.join(save_folder, fname)): flag_overwrite = input('File exists. Overwrite? (y/n) ') if flag_overwrite in ['y', 'Y']: f = h5py.File(os.path.join(save_folder, fname), 'w') grp = f.create_group('exchange') dat = grp.create_dataset('data', shape=(n_theta, len(probe_pos), probe_size[0], probe_size[1]), dtype=np.complex64) else: return # create probe if probe_type == 'gaussian': py = np.arange(probe_size[0]) - (probe_size[0] - 1.) / 2 px = np.arange(probe_size[1]) - (probe_size[1] - 1.) / 2 pxx, pyy = np.meshgrid(px, py) probe_mag = np.exp(-(pxx**2 + pyy**2) / (2 * kwargs['probe_mag_sigma']**2)) probe_phase = kwargs['probe_phase_max'] * np.exp( -(pxx**2 + pyy**2) / (2 * kwargs['probe_phase_sigma']**2)) probe_real, probe_imag = mag_phase_to_real_imag(probe_mag, probe_phase) if probe_circ_mask is not None: probe_real, probe_imag = tomopy.circ_mask(np.array( [probe_real, probe_imag]), axis=0, ratio=probe_circ_mask) probe_mask = tomopy.circ_mask( np.ones_like(probe_real)[np.newaxis, :, :], axis=0, ratio=probe_circ_mask) probe_mask = gaussian_filter(np.squeeze(probe_mask), 3) for ii, theta in enumerate(theta_ls): print('Theta: {}'.format(ii)) waveset_out = rotate_and_project(theta, obj) dat[ii, :, :, :] = waveset_out dxchange.write_tiff(abs(waveset_out), os.path.join(save_folder, 'diffraction_dat', 'mag_{:05d}'.format(ii)), overwrite=True, dtype='float32') f.close() return
def write_center(tomo, theta, dpath='tmp/center', cen_range=None, ind=None, mask=False, ratio=1., sinogram_order=False, algorithm='gridrec', filter_name='parzen'): """ Save images reconstructed with a range of rotation centers. Helps finding the rotation center manually by visual inspection of images reconstructed with a set of different centers.The output images are put into a specified folder and are named by the center position corresponding to the image. Parameters ---------- tomo : ndarray 3D tomographic data. theta : array Projection angles in radian. dpath : str, optional Folder name to save output images. cen_range : list, optional [start, end, step] Range of center values. ind : int, optional Index of the slice to be used for reconstruction. mask : bool, optional If ``True``, apply a circular mask to the reconstructed image to limit the analysis into a circular region. ratio : float, optional The ratio of the radius of the circular mask to the edge of the reconstructed image. sinogram_order: bool, optional Determins whether data is a stack of sinograms (True, y-axis first axis) or a stack of radiographs (False, theta first axis). algorithm : {str, function} One of the following string values. 'art' Algebraic reconstruction technique :cite:`Kak:98`. 'bart' Block algebraic reconstruction technique. 'fbp' Filtered back-projection algorithm. 'gridrec' Fourier grid reconstruction algorithm :cite:`Dowd:99`, :cite:`Rivers:06`. 'mlem' Maximum-likelihood expectation maximization algorithm :cite:`Dempster:77`. 'osem' Ordered-subset expectation maximization algorithm :cite:`Hudson:94`. 'ospml_hybrid' Ordered-subset penalized maximum likelihood algorithm with weighted linear and quadratic penalties. 'ospml_quad' Ordered-subset penalized maximum likelihood algorithm with quadratic penalties. 'pml_hybrid' Penalized maximum likelihood algorithm with weighted linear and quadratic penalties :cite:`Chang:04`. 'pml_quad' Penalized maximum likelihood algorithm with quadratic penalty. 'sirt' Simultaneous algebraic reconstruction technique. 'tv' Total Variation reconstruction technique :cite:`Chambolle:11`. 'grad' Gradient descent method with a constant step size filter_name : str, optional Name of the filter for analytic reconstruction. 'none' No filter. 'shepp' Shepp-Logan filter (default). 'cosine' Cosine filter. 'hann' Cosine filter. 'hamming' Hamming filter. 'ramlak' Ram-Lak filter. 'parzen' Parzen filter. 'butterworth' Butterworth filter. 'custom' A numpy array of size `next_power_of_2(num_detector_columns)/2` specifying a custom filter in Fourier domain. The first element of the filter should be the zero-frequency component. 'custom2d' A numpy array of size `num_projections*next_power_of_2(num_detector_columns)/2` specifying a custom angle-dependent filter in Fourier domain. The first element of each filter should be the zero-frequency component. """ tomo = dtype.as_float32(tomo) theta = dtype.as_float32(theta) if sinogram_order: dy, dt, dx = tomo.shape else: dt, dy, dx = tomo.shape if ind is None: ind = dy // 2 if cen_range is None: center = np.arange(dx / 2 - 5, dx / 2 + 5, 0.5) else: center = np.arange(*cen_range) stack = dtype.empty_shared_array((len(center), dt, dx)) for m in range(center.size): if sinogram_order: stack[m] = tomo[ind] else: stack[m] = tomo[:, ind, :] # Reconstruct the same slice with a range of centers. rec = recon(stack, theta, center=center, sinogram_order=True, algorithm=algorithm, filter_name=filter_name, nchunk=1) # Apply circular mask. if mask is True: rec = circ_mask(rec, axis=0) # Save images to a temporary folder. for m in range(len(center)): fname = os.path.join(dpath, str('{0:.2f}'.format(center[m]) + '.tiff')) dxchange.write_tiff(rec[m], fname=fname, overwrite=True)
def recon_block(grid, shift_grid, src_folder, dest_folder, slice_range, sino_step, center_vec, ds_level=0, blend_method='max', blend_options=None, tolerance=1, sinogram_order=False, algorithm='gridrec', init_recon=None, ncore=None, nchunk=None, dtype='float32', crop=None, save_sino=False, assert_width=None, sino_blur=None, color_correction=False, flattened_radius=120, normalize=True, test_mode=False, mode='180', phase_retrieval=None, **kwargs): """ Reconstruct dsicrete HDF5 tiles, blending sinograms only. """ raw_folder = os.getcwd() os.chdir(src_folder) sino_ini = int(slice_range[0]) sino_end = int(slice_range[1]) mod_start_slice = 0 center_vec = np.asarray(center_vec) center_pos_cache = 0 sino_ls = np.arange(sino_ini, sino_end, sino_step, dtype='int') pix_shift_grid = np.ceil(shift_grid) pix_shift_grid[pix_shift_grid < 0] = 0 alloc_set = allocate_mpi_subsets(sino_ls.size, size, task_list=sino_ls) for i_slice in alloc_set[rank]: print('############################################') print('Reconstructing ' + str(i_slice)) # judge from which tile to retrieve sinos grid_lines = np.zeros(grid.shape[1], dtype=np.int) slice_in_tile = np.zeros(grid.shape[1], dtype=np.int) for col in range(grid.shape[1]): bins = pix_shift_grid[:, col, 0] grid_lines[col] = int(np.squeeze(np.digitize(i_slice, bins)) - 1) if grid_lines[col] == -1: print("WARNING: The specified starting slice number does not allow for full sinogram construction. Trying next slice...") mod_start_slice = 1 break else: mod_start_slice = 0 slice_in_tile[col] = i_slice - bins[grid_lines[col]] if mod_start_slice == 1: continue center_pos = int(np.round(center_vec[grid_lines].mean())) if center_pos_cache == 0: center_pos_cache = center_pos center_diff = center_pos - center_pos_cache center_pos_0 = center_pos row_sino, center_pos = prepare_slice(grid, shift_grid, grid_lines, slice_in_tile, ds_level=ds_level, method=blend_method, blend_options=blend_options, rot_center=center_pos, assert_width=assert_width, sino_blur=sino_blur, color_correction=color_correction, normalize=normalize, mode=mode, phase_retrieval=phase_retrieval) rec0 = recon_slice(row_sino, center_pos, sinogram_order=sinogram_order, algorithm=algorithm, init_recon=init_recon, ncore=ncore, nchunk=nchunk, **kwargs) rec = tomopy.remove_ring(np.copy(rec0)) cent = int((rec.shape[1] - 1) / 2) xx, yy = np.meshgrid(np.arange(rec.shape[2]), np.arange(rec.shape[1])) mask0 = ((xx - cent) ** 2 + (yy - cent) ** 2 <= flattened_radius ** 2) mask = np.zeros(rec.shape, dtype='bool') for i in range(mask.shape[0]): mask[i, :, :] = mask0 rec[mask] = (rec[mask] + rec0[mask]) / 2 rec = tomopy.remove_outlier(rec, tolerance) rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) print('Center: {:d}'.format(center_pos)) rec = np.squeeze(rec) if center_diff != 0: rec = np.roll(rec, -center_diff, axis=0) if not crop is None: crop = np.asarray(crop) rec = rec[crop[0, 0]:crop[1, 0], crop[0, 1]:crop[1, 1]] os.chdir(raw_folder) if test_mode: dxchange.write_tiff(rec, fname=os.path.join(dest_folder, 'recon/recon_{:05d}_{:04d}.tiff'.format(i_slice, center_pos)), dtype=dtype) else: dxchange.write_tiff(rec, fname=os.path.join(dest_folder, 'recon/recon_{:05d}.tiff'.format(i_slice)), dtype=dtype) if save_sino: dxchange.write_tiff(np.squeeze(row_sino), fname=os.path.join(dest_folder, 'sino/sino_{:05d}.tiff'.format(i_slice)), overwrite=True) os.chdir(src_folder) os.chdir(raw_folder) return
def recon_hdf5(src_fanme, dest_folder, sino_range, sino_step, shift_grid, center_vec=None, center_eq=None, dtype='float32', algorithm='gridrec', tolerance=1, chunk_size=20, save_sino=False, sino_blur=None, flattened_radius=120, mode='180', test_mode=False, phase_retrieval=None, ring_removal=True, **kwargs): """ center_eq: a and b parameters in fitted center position equation center = a*slice + b. """ if not os.path.exists(dest_folder): try: os.mkdir(dest_folder) except: pass sino_ini = int(sino_range[0]) sino_end = int(sino_range[1]) sino_ls_all = np.arange(sino_ini, sino_end, sino_step, dtype='int') alloc_set = allocate_mpi_subsets(sino_ls_all.size, size, task_list=sino_ls_all) sino_ls = alloc_set[rank] # prepare metadata f = h5py.File(src_fanme) dset = f['exchange/data'] full_shape = dset.shape theta = tomopy.angles(full_shape[0]) if center_eq is not None: a, b = center_eq center_ls = sino_ls * a + b center_ls = np.round(center_ls) for iblock in range(int(sino_ls.size/chunk_size)+1): print('Beginning block {:d}.'.format(iblock)) t0 = time.time() istart = iblock*chunk_size iend = np.min([(iblock+1)*chunk_size, sino_ls.size]) fstart = sino_ls[istart] fend = sino_ls[iend] center = center_ls[istart:iend] data = dset[:, fstart:fend:sino_step, :] data[np.isnan(data)] = 0 data = data.astype('float32') data = tomopy.remove_stripe_ti(data, alpha=4) if sino_blur is not None: for i in range(data.shape[1]): data[:, i, :] = gaussian_filter(data[:, i, :], sino_blur) rec = tomopy.recon(data, theta, center=center, algorithm=algorithm, **kwargs) rec = tomopy.remove_ring(rec) rec = tomopy.remove_outlier(rec, tolerance) rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) for i in range(rec.shape[0]): slice = fstart + i*sino_step dxchange.write_tiff(rec[i, :, :], fname=os.path.join(dest_folder, 'recon/recon_{:05d}_{:05d}.tiff').format(slice, sino_ini)) if save_sino: dxchange.write_tiff(data[:, i, :], fname=os.path.join(dest_folder, 'sino/recon_{:05d}_{:d}.tiff').format(slice, int(center[i]))) iblock += 1 print('Block {:d} finished in {:.2f} s.'.format(iblock, time.time()-t0)) else: # divide chunks grid_bins = np.append(np.ceil(shift_grid[:, 0, 0]), full_shape[1]) chunks = [] center_ls = [] istart = 0 counter = 0 # irow should be 0 for slice 0 irow = np.searchsorted(grid_bins, sino_ls[0], side='right')-1 for i in range(sino_ls.size): counter += 1 sino_next = i+1 if i != sino_ls.size-1 else i if counter >= chunk_size or sino_ls[sino_next] >= grid_bins[irow+1] or sino_next == i: iend = i+1 chunks.append((istart, iend)) istart = iend center_ls.append(center_vec[irow]) if sino_ls[sino_next] >= grid_bins[irow+1]: irow += 1 counter = 0 # reconstruct chunks iblock = 1 for (istart, iend), center in izip(chunks, center_ls): print('Beginning block {:d}.'.format(iblock)) t0 = time.time() fstart = sino_ls[istart] fend = sino_ls[iend-1] print('Reading data...') data = dset[:, fstart:fend+1:sino_step, :] if mode == '360': overlap = 2 * (dset.shape[2] - center) data = tomosaic.morph.sino_360_to_180(data, overlap=overlap, rotation='right') theta = tomopy.angles(data.shape[0]) data[np.isnan(data)] = 0 data = data.astype('float32') if sino_blur is not None: for i in range(data.shape[1]): data[:, i, :] = gaussian_filter(data[:, i, :], sino_blur) if ring_removal: data = tomopy.remove_stripe_ti(data, alpha=4) if phase_retrieval: data = tomopy.retrieve_phase(data, kwargs['pixel_size'], kwargs['dist'], kwargs['energy'], kwargs['alpha']) rec0 = tomopy.recon(data, theta, center=center, algorithm=algorithm, **kwargs) rec = tomopy.remove_ring(np.copy(rec0)) cent = int((rec.shape[1]-1) / 2) xx, yy = np.meshgrid(np.arange(rec.shape[2]), np.arange(rec.shape[1])) mask0 = ((xx-cent)**2+(yy-cent)**2 <= flattened_radius**2) mask = np.zeros(rec.shape, dtype='bool') for i in range(mask.shape[0]): mask[i, :, :] = mask0 rec[mask] = (rec[mask] + rec0[mask])/2 else: rec = tomopy.recon(data, theta, center=center, algorithm=algorithm, **kwargs) rec = tomopy.remove_outlier(rec, tolerance) rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) for i in range(rec.shape[0]): slice = fstart + i*sino_step if test_mode: dxchange.write_tiff(rec[i, :, :], fname=os.path.join(dest_folder, 'recon/recon_{:05d}_{:d}.tiff').format(slice, center), dtype=dtype) else: dxchange.write_tiff(rec[i, :, :], fname=os.path.join(dest_folder, 'recon/recon_{:05d}.tiff').format(slice), dtype=dtype) if save_sino: dxchange.write_tiff(data[:, i, :], fname=os.path.join(dest_folder, 'sino/recon_{:05d}_{:d}.tiff').format(slice, center), dtype=dtype) print('Block {:d} finished in {:.2f} s.'.format(iblock, time.time()-t0)) iblock += 1 return
def rec_try(h5fname, nsino, rot_center, center_search_width, algorithm, binning, dark_file): data_shape = get_dx_dims(h5fname, 'data') print(data_shape) ssino = int(data_shape[1] * nsino) center_range = (rot_center-center_search_width, rot_center+center_search_width, 0.5) #print(sino,ssino, center_range) #print(center_range[0], center_range[1], center_range[2]) # Select sinogram range to reconstruct sino = None start = ssino end = start + 1 sino = (start, end) # Read APS 32-BM raw data. proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino) if dark_file is not None: print('Reading white/dark from {}'.format(dark_file)) proj_, flat, dark, theta_ = dxchange.read_aps_32id(dark_file, sino=sino) del proj_, theta_ print(proj.shape, flat.shape, dark.shape) # Flat-field correction of raw data. data = tomopy.normalize(proj, flat, dark, cutoff=1.4) # remove stripes # data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True) print("Raw data: ", h5fname) print("Center: ", rot_center) data = tomopy.minus_log(data) data = tomopy.remove_nan(data, val=0.0) data = tomopy.remove_neg(data, val=0.00) data[np.where(data == np.inf)] = 0.00 stack = np.empty((len(np.arange(*center_range)), data_shape[0], data_shape[2])) index = 0 for axis in np.arange(*center_range): stack[index] = data[:, 0, :] index = index + 1 # Reconstruct the same slice with a range of centers. rec = tomopy.recon(stack, theta, center=np.arange(*center_range), sinogram_order=True, algorithm='gridrec', filter_name='parzen', nchunk=1) # Mask each reconstructed slice with a circle. rec = tomopy.circ_mask(rec, axis=0, ratio=0.95) index = 0 # Save images to a temporary folder. #fname = os.path.dirname(h5fname) + os.sep + 'try_rec/' + path_base_name(h5fname) + os.sep + 'recon_' + os.path.splitext(os.path.basename(h5fname))[0] fname = os.path.dirname(h5fname) + os.sep + 'centers/' + path_base_name(h5fname) + os.sep + 'recon_' + os.path.splitext(os.path.basename(h5fname))[0] for axis in np.arange(*center_range): rfname = fname + '_' + str('{0:.2f}'.format(axis) + '.tiff') dxchange.write_tiff(rec[index], fname=rfname, overwrite=True) index = index + 1 print("Reconstructions: ", fname)
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)
batch_size = 400 nb_epoch = 50 dim_img = 64 nb_filters = 40 nb_conv = 3 patch_step = 1 patch_size = (dim_img, dim_img) smooth = 1. test1 = dxchange.read_tiff('/home/beams/YANGX/ptychography/datatolearn/lr_0034.tiff') ih, iw = test1.shape img_prd = np.zeros((ih, iw)) wpath = 'th_weights/full2.h5' img1 = test1[:ih/2, :iw/2] img_rec1 = predict(img1, patch_size, patch_step, nb_filters, nb_conv, batch_size, dim_img, wpath) img_prd[:ih/2, :iw/2] = img_rec1 img1 = test1[ih/2+1:, iw/2+1:] img_rec1 = predict(img1, patch_size, patch_step, nb_filters, nb_conv, batch_size, dim_img, wpath) img_prd[ih/2+1:, iw/2+1:] = img_rec1 img1 = test1[:ih/2, iw/2+1:] img_rec1 = predict(img1, patch_size, patch_step, nb_filters, nb_conv, batch_size, dim_img, wpath) img_prd[:ih/2, iw/2+1:] = img_rec1 img1 = test1[ih/2+1:, :iw/2] img_rec1 = predict(img1, patch_size, patch_step, nb_filters, nb_conv, batch_size, dim_img, wpath) img_prd[ih/2+1:, :iw/2] = img_rec1 fsave = '/home/beams/YANGX/ptychography/full0034_prd' dxchange.write_tiff(img_prd, fsave, dtype='float32')
def align_seq( prj, ang, fdir='.', iters=10, pad=(0, 0), blur=True, center=None, algorithm='sirt', upsample_factor=10, rin=0.5, rout=0.8, save=False, debug=True): """ Aligns the projection image stack using the sequential re-projection algorithm :cite:`Gursoy:17`. Parameters ---------- prj : ndarray 3D stack of projection images. The first dimension is projection axis, second and third dimensions are the x- and y-axes of the projection image, respectively. ang : ndarray Projection angles in radians as an array. iters : scalar, optional Number of iterations of the algorithm. pad : list-like, optional Padding for projection images in x and y-axes. blur : bool, optional Blurs the edge of the image before registration. center: array, optional Location of rotation axis. algorithm : {str, function} One of the following string values. 'art' Algebraic reconstruction technique :cite:`Kak:98`. 'gridrec' Fourier grid reconstruction algorithm :cite:`Dowd:99`, :cite:`Rivers:06`. 'mlem' Maximum-likelihood expectation maximization algorithm :cite:`Dempster:77`. 'sirt' Simultaneous algebraic reconstruction technique. 'tv' Total Variation reconstruction technique :cite:`Chambolle:11`. 'grad' Gradient descent method with a constant step size upsample_factor : integer, optional The upsampling factor. Registration accuracy is inversely propotional to upsample_factor. rin : scalar, optional The inner radius of blur function. Pixels inside rin is set to one. rout : scalar, optional The outer radius of blur function. Pixels outside rout is set to zero. save : bool, optional Saves projections and corresponding reconstruction for each algorithm iteration. debug : book, optional Provides debugging info such as iterations and error. Returns ------- ndarray 3D stack of projection images with jitter. ndarray Error array for each iteration. """ # Needs scaling for skimage float operations. prj, scl = scale(prj) # Shift arrays sx = np.zeros((prj.shape[0])) sy = np.zeros((prj.shape[0])) conv = np.zeros((iters)) # Pad images. npad = ((0, 0), (pad[1], pad[1]), (pad[0], pad[0])) prj = np.pad(prj, npad, mode='constant', constant_values=0) # Register each image frame-by-frame. for n in range(iters): # Reconstruct image. rec = recon(prj, ang, center=center, algorithm=algorithm) # Re-project data and obtain simulated data. sim = project(rec, ang, center=center, pad=False) # Blur edges. if blur: _prj = blur_edges(prj, rin, rout) _sim = blur_edges(sim, rin, rout) else: _prj = prj _sim = sim # Initialize error matrix per iteration. err = np.zeros((prj.shape[0])) # For each projection for m in range(prj.shape[0]): # Register current projection in sub-pixel precision shift, error, diffphase = register_translation( _prj[m], _sim[m], upsample_factor) err[m] = np.sqrt(shift[0]*shift[0] + shift[1]*shift[1]) sx[m] += shift[0] sy[m] += shift[1] # Register current image with the simulated one tform = tf.SimilarityTransform(translation=(shift[1], shift[0])) prj[m] = tf.warp(prj[m], tform, order=5) if debug: print('iter=' + str(n) + ', err=' + str(np.linalg.norm(err))) conv[n] = np.linalg.norm(err) if save: dxchange.write_tiff(prj, fdir + '/tmp/iters/prj/prj') dxchange.write_tiff(sim, fdir + '/tmp/iters/sim/sim') dxchange.write_tiff(rec, fdir + '/tmp/iters/rec/rec') # Re-normalize data prj *= scl return prj, sx, sy, conv
datap2 = data[ntheta // 2:, :, ::-1] if (dbg): center = 10 datap1 = data[:ntheta // 2, :, n // 2 - center:-center] datap2 = data[:ntheta // 2, :, center:n // 2 + center] n = n // 2 shiftza = np.zeros(ntheta // 2) shiftxa = np.zeros(ntheta // 2) for k in np.arange(0, ntheta // 2): # find location datap1 in datap2 by cross-correlation corr = scipy.signal.correlate2d( datap1[k, :, 0:w] - np.mean(datap1[k, :, 0:w]), datap2[k, :, -w:] - np.mean(datap2[k, :, -w:]), boundary='symm', mode='same') dxchange.write_tiff(datap1[k], 't1.tiff', overwrite=True) dxchange.write_tiff(datap2[k], 't2.tiff', overwrite=True) dxchange.write_tiff(datap1[k, :, 0:w] - np.mean(datap1[k, :, 0:w]), 'tc1.tiff', overwrite=True) dxchange.write_tiff(datap2[k, :, -w:] - np.mean(datap2[k, :, -w:]), 'tc2.tiff', overwrite=True) #dxchange.write_tiff(corr.astype('float32'),'t3.tiff',overwrite=True) shiftz, shiftx = np.unravel_index(np.argmax(corr), corr.shape) shiftza[k] = shiftz - nz / 2 + 1 shiftxa[k] = shiftx - w / 2 + 1 print(k, shiftza[k], shiftxa[k]) shiftz = np.mean(shiftza) shiftx = np.mean(shiftxa)
def align_seq( prj, ang, fdir='.', iters=10, pad=(0, 0), blur=True, save=False, debug=True): """ Aligns the projection image stack using the sequential re-projection algorithm :cite:`Gursoy:17`. Parameters ---------- prj : ndarray 3D stack of projection images. The first dimension is projection axis, second and third dimensions are the x- and y-axes of the projection image, respectively. ang : ndarray Projection angles in radians as an array. iters : scalar, optional Number of iterations of the algorithm. pad : list-like, optional Padding for projection images in x and y-axes. blur : bool, optional Blurs the edge of the image before registration. save : bool, optional Saves projections and corresponding reconstruction for each algorithm iteration. debug : book, optional Provides debugging info such as iterations and error. Returns ------- ndarray 3D stack of projection images with jitter. ndarray Error array for each iteration. """ # Needs scaling for skimage float operations. prj, scl = scale(prj) # Shift arrays sx = np.zeros((prj.shape[0])) sy = np.zeros((prj.shape[0])) conv = np.zeros((iters)) # Pad images. npad = ((0, 0), (pad[1], pad[1]), (pad[0], pad[0])) prj = np.pad(prj, npad, mode='constant', constant_values=0) # Register each image frame-by-frame. for n in range(iters): # Reconstruct image. rec = recon(prj, ang, algorithm='sirt') # Re-project data and obtain simulated data. sim = project(rec, ang, pad=False) # Blur edges. if blur: _prj = blur_edges(prj, 0.1, 0.5) _sim = blur_edges(sim, 0.1, 0.5) else: _prj = prj _sim = sim # Initialize error matrix per iteration. err = np.zeros((prj.shape[0])) # For each projection for m in range(prj.shape[0]): # Register current projection in sub-pixel precision shift, error, diffphase = register_translation(_prj[m], _sim[m], 2) err[m] = np.sqrt(shift[0]*shift[0] + shift[1]*shift[1]) sx[m] += shift[0] sy[m] += shift[1] # Register current image with the simulated one tform = tf.SimilarityTransform(translation=(shift[1], shift[0])) prj[m] = tf.warp(prj[m], tform, order=5) if debug: print('iter=' + str(n) + ', err=' + str(np.linalg.norm(err))) conv[n] = np.linalg.norm(err) if save: dxchange.write_tiff(prj, fdir + '/tmp/iters/prj/prj') dxchange.write_tiff(sim, fdir + '/tmp/iters/sim/sim') dxchange.write_tiff(rec, fdir + '/tmp/iters/rec/rec') # Re-normalize data prj *= scl return prj, sx, sy, conv
signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTSTP, signal_handler) # Compute data psi = slv.exptomo(slv.fwd_tomo(obj)) data = np.zeros(slv.ptychoshape, dtype='float32') for k in range(0, ptheta): # angle partitions in ptyocgraphy ids = np.arange(k*ntheta//ptheta, (k+1)*ntheta//ptheta) slv.cl_ptycho.setobj(scan[:, ids].data.ptr, prb.data.ptr) data[ids] = (cp.abs(slv.fwd_ptycho(psi[ids]))**2/slv.coefdata).get() print("max data = ", np.amax(data)) if (noise == True):# Apply Poisson noise data = np.random.poisson(data).astype('float32') # Save one angle dxchange.write_tiff(np.fft.fftshift( data[ntheta//2]), 'data') for imodel in range(len(modela)): model = modela[imodel] for ialpha in range(len(alphaa)): alpha = alphaa[ialpha] # Initial guess h = cp.zeros(tomoshape, dtype='complex64', order='C')+1*cp.exp(1j*initshift).astype('complex64') psi = cp.zeros(tomoshape, dtype='complex64', order='C')+1*cp.exp(1j*initshift).astype('complex64') e = cp.zeros([3, *obj.shape], dtype='complex64', order='C') phi = cp.zeros([3, *obj.shape], dtype='complex64', order='C') lamd = cp.zeros(tomoshape, dtype='complex64', order='C') mu = cp.zeros([3, *obj.shape], dtype='complex64', order='C') u = cp.zeros(obj.shape, dtype='complex64', order='C')
def test_out(img, save_folder): img[np.abs(img) < 1e-3] = 1e-3 img[img > 1] = 1 img = -np.log(img) img[np.where(np.isnan(img) == True)] = 0 dxchange.write_tiff(np.squeeze(img), fname=save_folder + 'test')