numz, numy, numx = obj.shape
print("Cropped/padded size: (", numz, ',', numy, ',', numx, ')')
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,
correlation = np.zeros((nbfiles, nbfiles))
for raw in range(nbfiles):
    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])),
Example #3
0
                                     debugging=debug)
     phase = pru.wrap(phase,
                      start_angle=-extent_phase / 2,
                      range_angle=extent_phase)
     phase = phase - phase[zcom, ycom, xcom]
     phase, extent_phase = pu.unwrap(amp * np.exp(1j * phase),
                                     support_threshold=0.05,
                                     debugging=debug)
     phase = pru.wrap(
         phase, start_angle=-extent_phase / 2,
         range_angle=extent_phase)  # rewrap after modifying phase
     ref_amp = np.copy(amp)
 else:  # align it with the reference object
     # TODO: interpolate object if the shape is different from ref_amp (different voxel sizes between datasets)
     shiftz, shifty, shiftx = reg.getimageregistration(ref_amp,
                                                       amp,
                                                       precision=1000)
     print('Shift of array', index, 'with the reference array:', shiftz,
           shifty, shiftx)
     obj = amp * np.exp(1j * phase)
     obj = reg.subpixel_shift(obj, shiftz, shifty, shiftx)
     amp = abs(obj)
     phase = np.angle(obj)
     phase, extent_phase = pu.unwrap(amp * np.exp(1j * phase),
                                     support_threshold=0.05,
                                     debugging=False)
     phase = pru.wrap(phase,
                      start_angle=-extent_phase / 2,
                      range_angle=extent_phase)
     phase = phase - phase[zcom, ycom, xcom]
     phase, extent_phase = pu.unwrap(amp * np.exp(1j * phase),
Example #4
0
        new_x = myx + offset_x
        del myx, myy, myz
        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' +