Utilities¶
General¶
center_crop(u, crop_length)
¶
Symmetrically crops u with lengths specified per axis in
crop_length, which should be iterable with same size as u.ndims.
Source code in src/chromatix/utils/utils.py
center_pad(u, pad_width, cval=0)
¶
Symmetrically pads u with lengths specified per axis in pad_width,
which should be an iterable of integers and have the same length as
u.ndims.
Source code in src/chromatix/utils/utils.py
create_grid(shape, spacing)
¶
Creates a 2D grid of vertical and horizontal coordinates with the specified
shape and spacing, with the origin in the center of the grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[int, int]
|
The shape of the grid, described as a tuple of
integers of the form |
required |
spacing
|
ScalarLike
|
The spacing of each pixel in the grid, either a single float
for square pixels or an array of shape |
required |
Returns:
The grid as an array of shape (2 H W).
Source code in src/chromatix/utils/utils.py
gaussian_kernel(sigma, truncate=4.0, shape=None)
¶
Creates ND Gaussian kernel of given sigma.
If shape is not provided, then the shape of the kernel is automatically
calculated using the given truncation (the same truncation for each
dimension) and sigma. The number of dimensions is determined by the
length of sigma, which should be a 1D array.
If shape is provided, then truncate is ignored and the result will
have the provided shape. The provided shape must be odd in all
dimensions to ensure that there is a center pixel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sigma
|
Sequence[float]
|
A 1D array whose length is the number of dimensions specifying the standard deviation of the Gaussian distribution in each dimension. |
required |
truncate
|
float
|
If |
4.0
|
shape
|
Sequence[int] | None
|
If provided, determines the |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
The ND Gaussian kernel. |
Source code in src/chromatix/utils/utils.py
grid_spatial_to_pupil(grid, f, NA, n)
¶
l1_norm(a, axis=-1)
¶
l2_norm(a, axis=-1)
¶
l2_sq_norm(a, axis=-1)
¶
linf_norm(a, axis=-1)
¶
matvec(x, y)
¶
Implements batched matrix - vector multiplication. Mostly used in polarization calculations. Example [..., N, M] x [...., M] -> [...., N]
next_order(val)
¶
outer(x, y, in_axis=-1)
¶
Calculates batched outer product (Numpy flattens input matrices) Includes additional in_axis for which axis to use. Output axes will always be last two.
Source code in src/chromatix/utils/utils.py
rotate_grid(grid, rotation)
¶
Rotates a 2D grid (an array of shape (2 H W)) by rotation radians.
Positive rotations are assumed to be in the counter-clockwise direction.
Source code in src/chromatix/utils/utils.py
sigmoid_taper(shape, width)
¶
Source code in src/chromatix/utils/utils.py
Initializers¶
__all__ = ['axicon_phase', 'flat_phase', 'microlens_array_amplitude_and_phase', 'hexagonal_microlens_array_amplitude_and_phase', 'rectangular_microlens_array_amplitude_and_phase', 'circular_phase', 'linear_phase', 'sawtooth_phase', 'sinusoid_phase', 'potato_chip', 'seidel_aberrations', 'zernike_aberrations', 'defocused_ramps']
module-attribute
¶
axicon_phase(shape, spacing, wavelength, n_axicon, slope_angle, n_medium=1.0)
¶
Source code in src/chromatix/utils/initializers.py
circular_phase(shape, spacing, shift, w)
¶
Source code in src/chromatix/utils/initializers.py
defocused_ramps(shape, spacing, wavelength, n, f, NA, num_ramps=6, delta=[2374.0] * 6, defocus=[-50.0, 150.0, -100.0, 50.0, -150.0, 100.0])
¶
Computes the "defocused ramps" phase mask as described in [1].
This phase mask is intended to be used in a 4f microscope to produce a number of "pencil" beams in the resulting PSF. The resulting PSF produces multiple subimages of the sample on the camera that are projections of the sample along different angles and through different axial ranges, intended to be used for 3D snapshot microscopy.
The name describes the fact that the phase mask consists of multiple phase ramps around a central flat region, combined with a bowl of defocus within each phase ramp to defocus the pencil beam that results from that arm of the phase mask.
[1]: Deb et al. "FourierNets enable the design of highly non-local optical encoders for computational imaging." NeurIPS, 2022.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[int, int]
|
The shape of the phase mask, described as a tuple of integers of the form (H W). |
required |
spacing
|
ScalarLike
|
The spacing of each pixel in the phase mask. |
required |
wavelength
|
ScalarLike
|
The wavelength to compute the phase mask for. |
required |
n
|
ScalarLike
|
Refractive index. |
required |
f
|
ScalarLike
|
The focal distance (should be in same units as |
required |
NA
|
ScalarLike
|
The numerical aperture. Phase will be 0 outside of this NA. |
required |
num_ramps
|
int
|
Sets the number of "pencil" beams or "ramps". The number of
pencil beams will be |
6
|
delta
|
Sequence[float]
|
Controls the "slope" of each phase ramp. Higher values move the resulting pencil further away from the center of the field. |
[2374.0] * 6
|
defocus
|
Sequence[float]
|
Controls the defocus of each pencil axially (should be in
same units as |
[-50.0, 150.0, -100.0, 50.0, -150.0, 100.0]
|
Source code in src/chromatix/utils/initializers.py
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | |
flat_phase(shape, *args, value=0.0)
¶
Computes a flat mask (one with constant value).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[int, int]
|
The shape of the mask, described as a tuple of integers of the form (H W). |
required |
value
|
ScalarLike
|
The constant value to use for the mask, defaults to 0. |
0.0
|
Source code in src/chromatix/utils/initializers.py
hexagonal_microlens_array_amplitude_and_phase(shape, spacing, wavelength, n, f, num_lenses_per_side, radius, separation)
¶
Source code in src/chromatix/utils/initializers.py
linear_phase(shape, spacing, wavelength, n_mask, max_thickness, rotation=0.0, n_medium=1.0)
¶
Source code in src/chromatix/utils/initializers.py
microlens_array_amplitude_and_phase(shape, spacing, wavelength, n, fs, centers, radii)
¶
Source code in src/chromatix/utils/initializers.py
potato_chip(shape, spacing, wavelength, n, f, NA, d=50.0, C0=-146.7)
¶
Computes the "potato chip" phase mask described by [1].
Also known as the "helical focus" phase mask, this phase mask was designed to produce an extended helical PSF for 3D snapshot microscopy.
[1]: Broxton, Michael. "Volume reconstruction and resolution limits for three dimensional snapshot microscopy." Dissertation, Stanford University, 2017.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[int, int]
|
The shape of the phase mask, described as a tuple of integers of the form (H W). |
required |
spacing
|
ScalarLike
|
The spacing of each pixel in the phase mask. |
required |
wavelength
|
ScalarLike
|
The wavelength to compute the phase mask for. |
required |
n
|
ScalarLike
|
Refractive index. |
required |
f
|
ScalarLike
|
The focal distance (should be in same units as |
required |
NA
|
ScalarLike
|
The numerical aperture. Phase will be 0 outside of this NA. |
required |
d
|
ScalarLike
|
Sets the axial extent of the PSF (should be in same units as
|
50.0
|
C0
|
ScalarLike
|
Adjusts the focus of the PSF. Set to value described in [1]. See [1] for more details. |
-146.7
|
Source code in src/chromatix/utils/initializers.py
rectangular_microlens_array_amplitude_and_phase(shape, spacing, wavelength, n, f, num_lenses_height, num_lenses_width, radius, separation)
¶
Source code in src/chromatix/utils/initializers.py
sawtooth_phase(shape, spacing, wavelength, n_grating, period, thickness, rotation=0.0, n_medium=1.0)
¶
Source code in src/chromatix/utils/initializers.py
seidel_aberrations(shape, spacing, wavelength, n, f, NA, coefficients, u=0, v=0)
¶
Computes the Seidel phase polynomial described by [1]. Accounts for spatially varying aberrations by creating a different phase mask for each object field position (u,v)
[1]: Voelz, David George. Computational fourier optics: a MATLAB tutorial. Vol. 534. Bellingham, Washington: SPIE press, 2011.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[int, int]
|
The shape of the phase mask, described as a tuple of integers of the form (H W). |
required |
spacing
|
ScalarLike
|
The spacing of each pixel in the phase mask. |
required |
wavelength
|
ScalarLike
|
The wavelength to compute the phase mask for. |
required |
n
|
ScalarLike
|
Refractive index. |
required |
f
|
ScalarLike
|
The focal distance (should be in same units as |
required |
NA
|
ScalarLike
|
The numerical aperture. Phase will be 0 outside of this NA. |
required |
coefficients
|
Sequence[float]
|
weight coefficients for Seidel aberrations |
required |
u
|
ScalarLike
|
The horizontal position of the object field point in normalized coordinates from 0 to +/- 1. A value of 0 represents the center coordinate in the plane while a value of 1 represents the farthest point from the center. Positive values go right and negative values go left. |
0
|
v
|
ScalarLike
|
The vertical position of the object field point in normalized coordinates from 0 to +/- 1. A value of 0 represents the center coordinate in the plane while a value of 1 represents the farthest point from the center. Positive values go down and negative values go up. |
0
|
Source code in src/chromatix/utils/initializers.py
sinusoid_phase(shape, spacing, wavelength, n_grating, period, thickness, rotation=0.0, n_medium=1.0)
¶
Source code in src/chromatix/utils/initializers.py
zernike_aberrations(shape, spacing, wavelength, n, f, NA, ansi_indices, coefficients, normalize=True)
¶
Computes Zernike aberrations given indices of Zernike modes and their corresponding weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[int, int]
|
The shape of the phase mask, described as a tuple of integers of the form (H W). |
required |
spacing
|
ScalarLike
|
The spacing of each pixel in the phase mask. |
required |
wavelength
|
ScalarLike
|
The wavelength to compute the phase mask for. |
required |
n
|
ScalarLike
|
Refractive index of the medium. |
required |
f
|
ScalarLike
|
The focal length of the objective (should be in same units as
|
required |
NA
|
ScalarLike
|
The numerical aperture of the objective. Phase will be 0 outside of this NA. |
required |
ansi_indices
|
Sequence[int]
|
Linear Zernike indices according to ANSI numbering. |
required |
coefficients
|
Sequence[float]
|
Weight coefficients for the Zernike polynomials. |
required |
normalize
|
bool
|
Whether to normalize the Zernike coefficients. Defaults to
|
True
|
Source code in src/chromatix/utils/initializers.py
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | |
FFT¶
fft(x, axes=(1, 2), shift=False)
¶
Computes fft2 for input of shape (B... H W C).
If shift is true, first applies ifftshift, than an fftshift to
make sure everything stays centered.
Source code in src/chromatix/utils/fft.py
ifft(x, axes=(1, 2), shift=False)
¶
Computes ifft2 for input of shape (B... H W C).
If shift is true, first applies ifftshift, than an fftshift to
make sure everything stays centered.
Source code in src/chromatix/utils/fft.py
Chirp Z-transform (CZT)¶
czt(x, m, a, w, axis=-1)
¶
Chirp Z-transform (CZT) of a signal along one dimension. The CZT is a generalization of the discrete Fourier transform (DFT). The DFT samples the Z plane at uniformly-spaced points on the unit circle, whereas the CZT samples the Z plane at uniformly-spaced points on a spiral. This can be used to interpolate the DFT to any desired frequency resolution.
Bluestein's algorithm is used to compute the CZT as a convolution.
Warning
Using float32/complex64 may have numerical accuracy issues.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
Input signal to transform. |
required |
m
|
int
|
Number of samples in the output. |
required |
a
|
Complex
|
The starting point in the complex plane. Must lie on the unit circle for numerical stability. |
required |
w
|
Complex
|
The ratio between points in each step. Should lie on the unit circle. |
required |
axis
|
int
|
Axis along which to perform the CZT. |
-1
|
Source code in src/chromatix/utils/czt.py
cztn(x, m, a, w, axes=(-2, -1))
¶
Chirp Z-transform (CZT) of a signal along multiple dimensions as defined by
the axes parameter. This implementation loops over the dimensions and
performs the CZT along each dimension.
Warning
Using float32/complex64 may have numerical accuracy issues.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
Input signal to transform. |
required |
m
|
tuple[int]
|
Number of samples in the output. List for each dimension. |
required |
a
|
Complex[Array, m]
|
The starting point in the complex plane. List for each dimension. |
required |
w
|
Complex[Array, m]
|
The ratio between points in each step. List for each dimension. |
required |
axes
|
tuple[int]
|
Axes along which to perform the CZT. |
(-2, -1)
|
Source code in src/chromatix/utils/czt.py
zoomed_fft(x, k_start, k_end, output_shape, axes=(-2, -1), include_end=True)
¶
Custom FFTN function that uses the Chirp Z-transform (CZT) to compute the Fourier transform of a signal. It allows to generate zoomed FFT in a region between k_start and k_end with arbitrary output shape. The usual FFT corresponds to output_shape = x.shape, k_start = 0, k_end = 2 * pi, and include_end = False.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
Input signal to transform. |
required |
k_start
|
float
|
Start of the frequency range. |
required |
k_end
|
float
|
End of the frequency range. |
required |
output_shape
|
tuple[int]
|
Desired shape of the output. |
required |
axes
|
tuple[int]
|
Axes along which to perform the CZT. |
(-2, -1)
|
include_end
|
bool
|
Whether to include the end point in the frequency range. |
True
|
Returns:
| Type | Description |
|---|---|
ArrayLike
|
The Fourier transform of the input signal. |