Skip to content

Commit c52f0da

Browse files
committed
[WIP] Faster DCF implementation
1 parent 21039fa commit c52f0da

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

src/xtl/diffraction/images/correlators.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ def correlate(self, points_radial: int = 500, points_azimuthal: int = 360, units
201201

202202
# Transpose array to match the axes of the intensity array
203203
self._ccf = ccf.T # dim = (delta, radial)
204-
self._results = AzimuthalCrossCorrelationResult(radial=ai2.radial, azimuthal=ai2.azimuthal, ccf=self._ccf)
205-
return self.results
204+
return self.ccf
206205

207206
@property
208207
def ccf(self):
@@ -374,7 +373,7 @@ def _get_pixel_geometry_data(self, apply_mask=False):
374373
pixel_geometry = self.image.geometry.corner_array(unit=self.units_radial, use_cython=True)
375374
# self.image.geometry.normalize_azimuth_range()
376375
# self.image.geometry.guess_npt_rad()
377-
376+
378377
# We need to grab the corner 0 pixel in order to perfectly align with the beam center
379378
radial = pixel_geometry[:, :, 0, 0]
380379
chi = np.rad2deg(pixel_geometry[:, :, 0, 1])
@@ -419,18 +418,15 @@ def _split_pixels_to_shells(self, no_shells: int = 500):
419418
# radial_shells_in_pixels = self._q2r(radial_shells, in_pixels=True)
420419
# average_width = np.mean((radial_shells_in_pixels - np.roll(radial_shells_in_pixels, 1))[1:])
421420
# print(f'Average shell width: {average_width:.2f} px')
422-
# dcf, x, _ = binned_statistic(x=lag.flatten(), values=udcf.flatten(), bins=x_bins, statistic='mean')
421+
for i, radial_low in enumerate(radial_shells[:-1]):
422+
radial_high = radial_shells[i + 1]
423423

424-
# Assign every pixel to a radial shell bin (first bin is 1, not 0)
425-
pixel_bins = np.digitize(x=radial, bins=radial_shells)
426-
for i, _ in enumerate(radial_shells[:-1]):
427-
ring = np.where(pixel_bins == i + 1)
424+
ring = (radial >= radial_low) & (radial < radial_high)
428425
self.shells_img[ring] = np.random.random()
429426

430-
intensities = self.image.data[ring]
431-
azimuthals = chi[ring]
432-
radials = radial[ring]
433-
427+
intensities = np.extract(ring, self.image.data)
428+
azimuthals = np.extract(ring, chi)
429+
radials = np.extract(ring, radial)
434430
sorter = np.argsort(azimuthals)
435431
pixels = np.vstack((azimuthals[sorter], radials[sorter], intensities[sorter]))
436432
self.pixels_per_shell.append(pixels)
@@ -497,11 +493,14 @@ def correlate(self, points_radial: int = 500, points_azimuthal: int = 360, units
497493
intensities as input for the calculations. The function being calculated is:
498494
499495
.. math::
500-
CCF(q, \\Delta) = \\frac{\\langle I(q,\\chi) \\times I(q, \\chi + \\Delta) \\rangle_\\chi -
501-
\\langle I(q,\\chi) \\rangle_\\chi^2}{Var(I(q,\\chi))}
496+
CCF(q,\\Delta) = \\frac{\\langle I(q,\\chi) \\times I(q, \\chi + \\Delta) \\rangle_\\chi -
497+
\\langle I(q,\\chi) \\rangle_\\chi^2}{\\langle I(q,\\chi) \\rangle_\\chi^2}
502498
503-
where ``q`` is the radial coordinate, ``\u03c7`` is the azimuthal coordinate, ``\u0394`` the offset in
504-
azimuthal coordinates and ``Var`` is the variance.
499+
.. math::
500+
CCF(q, \\Delta) =
501+
502+
where ``q`` is the radial coordinate, ``\u03c7`` is the azimuthal coordinate and ``\u0394`` the offset in
503+
azimuthal coordinates.
505504
506505
Since the intensities are unevenly distributed along the azimuthal axis (due to the projection from a finite
507506
size cartesian grid to azimuthal coordinates), the CCF is calculated using the Discrete Correlation Function.

0 commit comments

Comments
 (0)