reference_obj, _ = util.load_file(file_path[raw]) reference_obj = abs(reference_obj) / abs(reference_obj).max() nbz, nby, nbx = reference_obj.shape reference_obj = util.crop_pad(array=reference_obj, output_shape=[nbz + 10, nby + 10, nbx + 10]) correlation[raw, raw] = 1 for col in range(raw + 1, nbfiles): test_obj, _ = util.load_file(file_path[col]) # which index? test_obj = abs(test_obj) / abs(test_obj).max() test_obj = util.crop_pad(array=test_obj, output_shape=[nbz + 10, nby + 10, nbx + 10]) # align reconstructions shiftz, shifty, shiftx = reg.getimageregistration(abs(reference_obj), abs(test_obj), precision=100) test_obj = reg.subpixel_shift(test_obj, shiftz, shifty, shiftx) print("\nReference =", raw, " Test =", col) print( "z shift", str("{:.2f}".format(shiftz)), ", y shift", str("{:.2f}".format(shifty)), ", x shift", str("{:.2f}".format(shiftx)), ) correlation[raw, col] = pearsonr( np.ndarray.flatten( abs(reference_obj[reference_obj > threshold_correlation])), np.ndarray.flatten( abs(test_obj[reference_obj > threshold_correlation])),
plt.figure() plt.imshow(np.angle(obj)[numz // 2, :, :], cmap=my_cmap, vmin=-phase_range, vmax=phase_range) plt.title('Phase before subpixel shift') plt.pause(0.1) ############################## # align datasets ############################## # dft registration and subpixel shift (see Matlab code) shiftz, shifty, shiftx = reg.getimageregistration(amp_simu, abs(obj), precision=1000) obj = reg.subpixel_shift(obj, shiftz, shifty, shiftx) print("Shift calculated from dft registration: (", str('{:.2f}'.format(shiftz)), ',', str('{:.2f}'.format(shifty)), ',', str('{:.2f}'.format(shiftx)), ') pixels') new_amp = abs(obj) new_phase = np.angle(obj) del obj _, new_strain, _ = np.gradient(planar_dist / (2 * np.pi) * new_phase, voxel_size) # q is along y after rotating the crystal plt.figure() plt.imshow(new_phase[numz // 2, :, :], cmap=my_cmap, vmin=-phase_range, vmax=phase_range) plt.title('Phase after subpixel shift')
rgi = RegularGridInterpolator((old_z, old_y, old_x), data, method='linear', bounds_error=False, fill_value=0) data = rgi( np.concatenate((new_z.reshape( (1, new_z.size)), new_y.reshape( (1, new_z.size)), new_x.reshape( (1, new_z.size)))).transpose()) data = data.reshape((nbz, nby, nbx)).astype(refdata.dtype) else: shiftz, shifty, shiftx = reg.getimageregistration(abs(refdata), abs(data), precision=1000) data = reg.subpixel_shift(data, shiftz, shifty, shiftx) print('\n x shift', shiftx, 'y shift', shifty, 'z shift', shiftz) correlation = pearsonr(np.ndarray.flatten(abs(refdata)), np.ndarray.flatten(abs(data)))[0] print('\n Rocking curve ', idx + 1, ', pearson correlation coefficient = ', str('{:.3f}'.format(correlation))) if correlation >= correlation_threshold: sumdata = sumdata + data savedir = datadir + sample_name + 'sum_S' + str(scan_list[0]) + '_to_S' + str( scan_list[-1]) + '/' pathlib.Path(savedir).mkdir(parents=True, exist_ok=True) np.savez_compressed(savedir + 'pynx_S' + str(scan_list[0]) + '_to_S' + str(scan_list[-1]) + '.npz', obj=sumdata)