|
16 | 16 | # You should have received a copy of the GNU General Public License |
17 | 17 | # along with diffsims. If not, see <http://www.gnu.org/licenses/>. |
18 | 18 |
|
| 19 | +from typing import Tuple |
| 20 | + |
19 | 21 | import numpy as np |
| 22 | +from numba import jit |
20 | 23 |
|
21 | 24 | from numpy.random import default_rng |
22 | 25 | from scipy import ndimage as ndi |
|
31 | 34 | "add_shot_noise", |
32 | 35 | "add_shot_and_point_spread", |
33 | 36 | "constrain_to_dynamic_range", |
| 37 | + "get_pattern_from_pixel_coordinates_and_intensities", |
34 | 38 | ] |
35 | 39 |
|
36 | 40 |
|
@@ -243,3 +247,114 @@ def add_detector_offset(pattern, offset): |
243 | 247 | """ |
244 | 248 | pattern = np.add(pattern, offset) |
245 | 249 | return constrain_to_dynamic_range(pattern) |
| 250 | + |
| 251 | + |
| 252 | +def get_pattern_from_pixel_coordinates_and_intensities( |
| 253 | + coordinates: np.ndarray, |
| 254 | + intensities: np.ndarray, |
| 255 | + shape: Tuple[int, int], |
| 256 | + sigma: float, |
| 257 | + clip_threshold: float = 1, |
| 258 | +) -> np.ndarray: |
| 259 | + """Generate a diffraction pattern from spot pixel-coordinates and intensities, |
| 260 | + using a gaussian blur. |
| 261 | + This is subpixel-precise, meaning the coordinates can be floats. |
| 262 | + Values less than `clip_threshold` are rounded down to 0 to simplify computation. |
| 263 | +
|
| 264 | + Parameters |
| 265 | + ---------- |
| 266 | + coordinates : np.ndarray |
| 267 | + Coordinates of reflections, in pixels. Shape (n, 2) or (n, 3). Can be floats |
| 268 | + intensities : np.ndarray |
| 269 | + Intensities of each reflection. Must have same same first dimension as `coordinates` |
| 270 | + shape : tuple[int, int] |
| 271 | + Output shape |
| 272 | + sigma : float |
| 273 | + For Gaussian blur |
| 274 | + intensity_scale : float |
| 275 | + Scale to multiply the final diffraction pattern with |
| 276 | +
|
| 277 | + Returns |
| 278 | + ------- |
| 279 | + np.ndarray |
| 280 | + dtype int |
| 281 | +
|
| 282 | + Notes |
| 283 | + ----- |
| 284 | + Not all values below the clipping threshold are ignored. |
| 285 | + The threshold is used to estimate a radius (box) around each reflection where the pixel intensity is greater than the threshold. |
| 286 | + As the radius is rounded up and as the box is square rather than circular, some values below the threshold can be included. |
| 287 | +
|
| 288 | + When using float coordinates, the intensity is spread as if the edge was not there. |
| 289 | + This is in line with what should be expected from a beam on the edge of the detector, as part of the beam is simply outside the detector area. |
| 290 | + However, when using integer coordinates, the total intensity is preserved for the pixels in the pattern. |
| 291 | + This means that the intensity contribution from parts of the beam which would hit outside the detector are now kept in the pattern. |
| 292 | + Thus, reflections wich are partially outside the detector will have higher intensities than expected, when using integer coordinates. |
| 293 | + """ |
| 294 | + if np.issubdtype(coordinates.dtype, np.integer): |
| 295 | + # Much simpler with integer coordinates |
| 296 | + coordinates = coordinates.astype(int) |
| 297 | + out = np.zeros(shape) |
| 298 | + # coordinates are xy(z), out array indices are yx. |
| 299 | + out[coordinates[:, 1], coordinates[:, 0]] = intensities |
| 300 | + out = add_shot_and_point_spread(out, sigma, shot_noise=False) |
| 301 | + return out |
| 302 | + |
| 303 | + # coordinates of each pixel in the output, such that the final axis is yx coordinates |
| 304 | + inds = np.transpose(np.indices(shape), (1, 2, 0)) |
| 305 | + return _subpixel_gaussian( |
| 306 | + coordinates, |
| 307 | + intensities, |
| 308 | + inds, |
| 309 | + shape, |
| 310 | + sigma, |
| 311 | + clip_threshold, |
| 312 | + ) |
| 313 | + |
| 314 | + |
| 315 | +@jit( |
| 316 | + nopython=True |
| 317 | +) # Not parallel, we might get a race condition with overlapping spots |
| 318 | +def _subpixel_gaussian( |
| 319 | + coordinates: np.ndarray, |
| 320 | + intensities: np.ndarray, |
| 321 | + inds: np.ndarray, |
| 322 | + shape: Tuple[int, int], |
| 323 | + sigma: float, |
| 324 | + clip_threshold: float = 1, |
| 325 | +) -> np.ndarray: |
| 326 | + out = np.zeros(shape) |
| 327 | + |
| 328 | + # Pre-calculate the constants |
| 329 | + prefactor = 1 / (2 * np.pi * sigma**2) |
| 330 | + exp_prefactor = -1 / (2 * sigma**2) |
| 331 | + |
| 332 | + for i in range(intensities.size): |
| 333 | + # Reverse since coords are xy, but indices are yx |
| 334 | + coord = coordinates[i][:2][::-1] |
| 335 | + intens = intensities[i] |
| 336 | + |
| 337 | + # The gaussian is expensive to evaluate for all pixels and spots. |
| 338 | + # Therefore, we limit the calculations to a box around each reflection where the intensity is above a threshold. |
| 339 | + # Formula found by inverting the gaussian |
| 340 | + radius = np.sqrt(np.log(clip_threshold / (prefactor * intens)) / exp_prefactor) |
| 341 | + |
| 342 | + if np.isnan(radius): |
| 343 | + continue |
| 344 | + slic = ( |
| 345 | + slice( |
| 346 | + max(0, int(np.ceil(coord[0] - radius))), |
| 347 | + min(shape[0], int(np.floor(coord[0] + radius + 1))), |
| 348 | + ), |
| 349 | + slice( |
| 350 | + max(0, int(np.ceil(coord[1] - radius))), |
| 351 | + min(shape[1], int(np.floor(coord[1] + radius + 1))), |
| 352 | + ), |
| 353 | + ) |
| 354 | + # Calculate the values of the Gaussian manually |
| 355 | + out[slic] += ( |
| 356 | + intens |
| 357 | + * prefactor |
| 358 | + * np.exp(exp_prefactor * np.sum((inds[slic] - coord) ** 2, axis=-1)) |
| 359 | + ) |
| 360 | + return out |
0 commit comments