def recons_auto_correct(obj, weight=None, **kwargs): if weight is None: mask = recons_auto_mask(obj, **kwargs) else: mask = weight obj = u.rmphaseramp(obj, weight=mask) obj *= np.exp(-1j * np.median(np.angle(obj)[mask])) return obj
def convert_ptyr_to_mapping(file_path, border=80, rmramp=True): ''' :param file_path: path the ptypy ptyr :param border: The pixel border to trim from the object reconstruction :param rmramp: Remove phase ramp :return: dictionary with lists containing paths to 'complex', 'magnitude' and 'phase' nexus formatted file paths ''' fread = h5.File(file_path, 'r') obj_keys = '/content/obj' complex_paths = [] phase_paths = [] magnitudes_paths = [] for obj_name, obj in fread[obj_keys].items(): x, y = obj['grids'][...] x = x[0, :, 0] / 1e-3 # get them into mm y = y[0, 0, :] / 1e-3 # get them into mm x = x[border:-border] y = y[border:-border] data = obj['data'][...].squeeze() data = data[border:-border, border:-border] if rmramp: data = u.rmphaseramp(data) data = data.reshape(data.shape+(1, 1)) magnitudes_path = file_path.split('.')[0] + obj_name + '_mag.nxs' write_dataset_to_file(np.abs(data), magnitudes_path, obj_name, x, y, tag='mag_') phase_path = file_path.split('.')[0] + obj_name+'_phase.nxs' write_dataset_to_file(np.angle(data), phase_path, obj_name, x, y, tag='phase_') complex_path = file_path.split('.')[0] + obj_name+'_complex.nxs' write_dataset_to_file(data, complex_path, obj_name, x, y, tag='complex_', dtype=np.complex128) complex_paths.append(complex_path) phase_paths.append(phase_paths) magnitudes_paths.append(magnitudes_path) return {'complex': complex_paths, 'phase': phase_paths, 'magnitude': magnitudes_paths}
def write_multiple_ptyr_to_nxtomo(file_paths, angles, out_path, prefix="", border=80, norm=True, rmramp=True, rmradius=0.5, rmiter=1, save_odens=False, save_complex=False): out_phase = out_path + "/" + prefix + "tomo_phase.nxs" if save_odens: out_odens = out_path + "/" + prefix + "tomo_odens.nxs" if save_complex: out_complex = out_path + "/" + prefix + "tomo_complex.nxs" shapes = [] nfiles = len(file_paths) for idx in range(nfiles): fread = h5.File(file_paths[idx], 'r') obj_keys = '/content/obj' obj = list(fread[obj_keys].values())[0] sh = obj['data'].shape[1:] if border > 0: sh = (sh[0] - 2*border, sh[1] - 2*border) shapes.append(np.array(sh)) log(3, "The tomographic angles range from {} to {} degress".format(angles[0], angles[-1])) shapes = np.array(shapes) full_shape = np.max(shapes[:, 0]), np.max(shapes[:, 1]) log(3, "The common full shape of the object is {}".format(full_shape)) # Create Nexus files for phase fp = create_nxtomo_file(out_phase, angles, full_shape, nfiles) if save_odens: fo = create_nxtomo_file(out_odens, angles, full_shape, nfiles) if save_complex: fc = create_nxtomo_file(out_complex, angles, full_shape, nfiles) ctr = 0 for idx in range(nfiles): print("Projection %03d/%03d" %(idx, nfiles), end='\r', flush=True) slow_top = shapes[idx, 0] fast_top = shapes[idx, 1] fread = h5.File(file_paths[idx], 'r') obj_keys = '/content/obj' obj = list(fread[obj_keys].values())[0] e,v = ortho(obj['data']) O = v[0] # select most dominant orthogonal object mode if border > 0: O = O[border:-border,border:-border] if rmramp: ny,nx = O.shape XX,YY = np.meshgrid(np.arange(nx) - nx//2, np.arange(ny) - ny//2) W = np.sqrt(XX**2 + YY**2) < (rmradius * (nx+ny) / 4) for i in range(rmiter): O = u.rmphaseramp(O, weight=W) if norm: O *= np.exp(-1j*np.median(np.angle(O))) phase = np.angle(O) odens = -np.log(np.abs(O)**2) if norm: odens -= np.median(odens) fp['entry1/data'][ctr, :slow_top, :fast_top] = phase if save_odens: fo['entry1/data'][ctr, :slow_top, :fast_top] = odens if save_complex: fc['entry1/data'][ctr, :slow_top, :fast_top] = O ctr += 1 fp.close() log(3, "Saved phase to {}".format(out_phase)) if save_odens: fo.close() log(3, "Saved optical density to {}".format(out_odens)) if save_complex: fc.close() log(3, "Saved complex object to {}".format(out_complex))
def write_single_ptyr_to_nxstxm(file_path, out_path, prefix="", border=80, rmramp=True, norm=True, rmradius=0.5, rmiter=1): out_phase = out_path + "/" + prefix + "phase.nxs" out_odensity = out_path + "/" + prefix + "optical_density.nxs" out_amplitude = out_path + "/" + prefix + "amplitude.nxs" shapes = [] energy = [] objs = [] fread = h5.File(file_path, 'r') obj_keys = '/content/obj' scan_keys = list(fread[obj_keys].keys()) for obj in fread[obj_keys].values(): enrg = obj['_energy'][...] data = obj['data'][0] if border > 0: data = data[border:-border,border:-border] sh = data.shape objs.append(data) energy.append(enrg) shapes.append(np.array(sh)) energy = np.array(energy)*1e3 # convert to eV log(3, "The energy ranges from {} to {} eV".format(energy[0], energy[-1])) shapes = np.array(shapes) full_shape = np.max(shapes[:, 0]), np.max(shapes[:, 1]) log(3, "The common full shape of the object is {}".format(full_shape)) # Create Mantis/Nexus files for phase and optical density fp = create_nxstxm_file(out_phase, energy, full_shape, len(scan_keys)) fo = create_nxstxm_file(out_odensity, energy, full_shape, len(scan_keys)) fa = create_nxstxm_file(out_amplitude, energy, full_shape, len(scan_keys)) ctr = 0 for idx in range(len(scan_keys)): slow_top = shapes[idx, 0] fast_top = shapes[idx, 1] O = objs[idx].squeeze() if rmramp: ny,nx = O.shape XX,YY = np.meshgrid(np.arange(nx) - nx//2, np.arange(ny) - ny//2) W = np.sqrt(XX**2 + YY**2) < (rmradius * (nx+ny) / 4) for i in range(rmiter): O = u.rmphaseramp(O, weight=W) if norm: O *= np.exp(-1j*np.median(np.angle(O))) phase = np.angle(O) odensity = -np.log(np.abs(O)**2) odensity[np.isinf(odensity)] = 0 if norm: odensity -= np.median(odensity) amplitude = np.abs(O) fp['entry1/Counter1/data'][ctr, :slow_top, :fast_top] = phase fo['entry1/Counter1/data'][ctr, :slow_top, :fast_top] = odensity fa['entry1/Counter1/data'][ctr, :slow_top, :fast_top] = amplitude ctr += 1 fp.close() fo.close() fa.close() log(3, "Saved phase to {}".format(out_phase)) log(3, "Saved optical density to {}".format(out_odensity)) log(3, "Saved amplitude to {}".format(out_amplitude))
def write_multiple_ptyr_to_nxstxm(file_paths, out_path, prefix="", border=80, norm=True, rescale=True, rmramp=True, rmradius=0.5, rmiter=1): out_phase = out_path + "/" + prefix + "phase.nxs" out_odensity = out_path + "/" + prefix + "optical_density.nxs" out_amplitude = out_path + "/" + prefix + "amplitude.nxs" out_probe_intensity = out_path + "/" + prefix + "probe_intensity.nxs" out_complex = out_path + "/" + prefix + "complex.nxs" shapes = [] energy = [] objs = [] prbs = [] nfiles = len(file_paths) for idx in range(nfiles): fread = h5.File(file_paths[idx], 'r') obj_keys = '/content/obj' obj = list(fread[obj_keys].values())[0] enrg = obj['_energy'][...] data = obj['data'][0] if border > 0: data = data[border:-border,border:-border] sh = data.shape objs.append(data) energy.append(enrg) shapes.append(np.array(sh)) probe_keys = '/content/probe' probe = list(fread[probe_keys].values())[0] probe_shape = tuple(np.array(probe['data'].shape[1:])) prbs.append(np.abs(probe['data'][0])) energy = np.array(energy)*1e3 # convert to eV prb_int_mean = (np.array(prbs)**2).mean(axis=(1,2)) #print("Probe amplitude means", prb_mean) log(3, "The energy ranges from {} to {} eV".format(energy[0], energy[-1])) shapes = np.array(shapes) full_shape = np.max(shapes[:, 0]), np.max(shapes[:, 1]) log(3, "The common full shape of the object is {}".format(full_shape)) # Create Mantis/Nexus files for phase and optical density fp = create_nxstxm_file(out_phase, energy, full_shape, nfiles) fo = create_nxstxm_file(out_odensity, energy, full_shape, nfiles) fa = create_nxstxm_file(out_amplitude, energy, full_shape, nfiles) fi = create_nxstxm_file(out_probe_intensity, energy, probe_shape, nfiles) fc = create_nxstxm_file(out_complex, energy, full_shape, nfiles, dtype='complex') ctr = 0 for idx in range(nfiles): slow_top = shapes[idx, 0] fast_top = shapes[idx, 1] O = objs[idx].squeeze() if rmramp: ny,nx = O.shape XX,YY = np.meshgrid(np.arange(nx) - nx//2, np.arange(ny) - ny//2) W = np.sqrt(XX**2 + YY**2) < (rmradius * (nx+ny) / 4) for i in range(rmiter): O = u.rmphaseramp(O, weight=W) if norm: O *= np.exp(-1j*np.median(np.angle(O))) phase = np.angle(O) if rescale: O *= np.sqrt(prb_int_mean[idx] / prb_int_mean.mean()) odensity = -np.log(np.abs(O)**2) odensity[np.isinf(odensity)] = 0 if norm: odensity -= np.median(odensity) amplitude = np.abs(O) #print("probe intensity before: ", (prbs[idx]**2).mean()) probeint = prbs[idx]**2 / (prb_int_mean[idx] / prb_int_mean.mean()) #print("probe intensity after: ", (probeint).mean()) fp['entry1/Counter1/data'][ctr, :slow_top, :fast_top] = phase fo['entry1/Counter1/data'][ctr, :slow_top, :fast_top] = odensity fa['entry1/Counter1/data'][ctr, :slow_top, :fast_top] = amplitude fi['entry1/Counter1/data'][ctr] = probeint fc['entry1/Counter1/data'][ctr, :slow_top, :fast_top] = O ctr += 1 fp.close() fo.close() fa.close() fi.close() fc.close() log(3, "Saved phase to {}".format(out_phase)) log(3, "Saved optical density to {}".format(out_odensity)) log(3, "Saved amplitude to {}".format(out_amplitude)) log(3, "Saved probe intensity to {}".format(out_probe_intensity)) log(3, "Saved complex to {}".format(out_complex))
def _deramped_data(self): weights = np.zeros_like(self.original_data) M, N = weights.shape weights[M//3:M//3*2, N//3:N//3*2] = 1 return rmphaseramp(self.original_data, weights)