Operations¶
General¶
fourier_convolution(image, kernel, *, axes=(0, 1), fast_fft_shape=True, mode='same')
¶
Fourier convolution in n dimensions over the specified axes of an Array.
The convolution dimensions are determined by the axes argument. For
example, the default axes to perform convolution over are (0, 1), or the
first two axes of the input, which will perform a 2D convolution. The
kernel and image should have the same number of dimensions.
This function computes the convolution kernel * image by employing the
Fourier convolution theorem. The inputs are padded appropriately to avoid
circular convolutions.
By default, the inputs are further padded to the nearest power of 2 that is larger than the padded input shape for faster FFT performance. If the input shape causes the difference between padded and unpadded to be too large (causing either memory or performance issues), this extra padding can be disabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
ArrayLike
|
The input to be convolved. |
required |
kernel
|
ArrayLike
|
The convolution kernel. |
required |
fast_fft_shape
|
bool
|
Determines whether inputs should be further padded for
increased FFT performance. Defaults to |
True
|
mode
|
str
|
A string that determines whether to crop the result of the
convolution to the same shape as |
'same'
|
Source code in src/chromatix/ops/ops.py
Filters¶
__all__ = ['high_pass_filter', 'gaussian_filter']
module-attribute
¶
gaussian_filter(data, sigma, axes=(1, 2), kernel_shape=None)
¶
Performs a Gaussian filter on data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Array
|
The input to be Gaussian filtered. |
required |
sigma
|
Sequence[float]
|
The standard deviation of the Gaussian kernel. |
required |
kernel_shape
|
Sequence[int] | None
|
The shape of the kernel. If not provided, the shape will
be determined by the required shape of a Gaussian kernel truncated
to |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
The Gaussian filtered array. |
Source code in src/chromatix/ops/filters.py
high_pass_filter(data, sigma, axes=(1, 2), kernel_shape=None)
¶
Performs a high pass filter on data.
The high pass filter is constructed as the difference between a
delta kernel and a Gaussian kernel with standard deviation sigma.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ArrayLike
|
The input to be high pass filtered. |
required |
sigma
|
Sequence[float]
|
The standard deviation of the Gaussian kernel, which sets the low pass filter. The result of this low pass will be subtracted from the input. |
required |
kernel_shape
|
Sequence[int] | None
|
The shape of the kernel. If not provided, the shape will
be determined by the required shape of a Gaussian kernel truncated
to |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
The high pass filtered array. |
Source code in src/chromatix/ops/filters.py
Noise¶
approximate_shot_noise(key, image)
¶
Approximates Poisson shot noise using a Gaussian for differentiability.
Source code in src/chromatix/ops/noise.py
approximate_shotnoise_jvp(primals, tangents)
¶
Custom gradient for approximate_shot_noise.
This is necessary to fix an instability when the input image is 0.
Source code in src/chromatix/ops/noise.py
shot_noise(key, image)
¶
Simulates Poisson shot noise whose gradient is approximated using
the gradient of a Gaussian, just as if the simulation had been
approximate_shot_noise instead.
Source code in src/chromatix/ops/noise.py
shotnoise_jvp(primals, tangents)
¶
Custom gradient for shot_noise.
Because the Poisson distribution computed in shot_noise cannot be
differentiated, this function computes the gradient as if the forward pass
had been approximate_shot_noise.
Source code in src/chromatix/ops/noise.py
Quantization¶
__all__ = ['binarize', 'binarize_jvp', 'quantize', 'quantize_jvp']
module-attribute
¶
binarize(x, threshold=0.5)
¶
Binarize each pixel of amplitude mask to be either 0 or 1.
Optionally takes a threshold to choose the threshold at which values
are set to 1. Defaults to 0.5.
The gradient of binarize is approximated with the gradient of the
sigmoid function as in [1].
[1] Eybposh, M. Hossein, et al. "Optimization of time-multiplexed computer-generated holograms with surrogate gradients." Emerging Digital Micromirror Device Based Systems and Applications XIV. SPIE, 2022.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Array
|
Input to binarize to 0 or 1. |
required |
threshold
|
float
|
Threshold above which values are set to 1. Defaults to 0.5. |
0.5
|
Source code in src/chromatix/ops/quantization.py
binarize_jvp(primals, tangents)
¶
Custom gradient for binarize.
We approximate the gradient of binarize with the gradient of the
sigmoid function.
Source code in src/chromatix/ops/quantization.py
quantize(x, bit_depth, range=None)
¶
Quantize the input x to the specified bit_depth. Surrogate gradient
approach [1] is used to adjust the bit depth differentiably.
[1] Eybposh, M. Hossein, et al. "Optimization of time-multiplexed computer-generated holograms with surrogate gradients." Emerging Digital Micromirror Device Based Systems and Applications XIV. SPIE, 2022.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Array
|
Input to quantize. |
required |
bit_depth
|
float
|
Number of bits. This parameter does NOT represent the number of digitization levels, but the bit depth. |
required |
range
|
tuple[int, int] | None
|
Range to quantize to, provided as |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
The quantized input. |
Source code in src/chromatix/ops/quantization.py
quantize_jvp(primals, tangents)
¶
Custom gradient for quantize.
We approximate the gradient of quantize with surrogate gradients, as
described in [1] (see definition of quantize).
Source code in src/chromatix/ops/quantization.py
Resample¶
InterpolatingPlaneResampler
¶
Bases: Resampler
Source code in src/chromatix/ops/resample.py
out_shape = out_shape
class-attribute
instance-attribute
¶
out_spacing = out_spacing
instance-attribute
¶
resampling_method = resampling_method
class-attribute
instance-attribute
¶
__call__(resample_input, in_spacing)
¶
Source code in src/chromatix/ops/resample.py
__init__(out_shape, out_spacing, resampling_method='linear')
¶
Source code in src/chromatix/ops/resample.py
PoolingPlaneDownsampler
¶
Bases: Resampler
Source code in src/chromatix/ops/resample.py
init_plane_resample(out_shape, out_spacing, resampling_method='linear')
¶
Returns a function that resamples 2D planes to the specified output shape
and spacing. These functions are instances of Resamplers in Chromatix.
The returned function is allowed to be jitted because the shape of the output will no longer depend on the input of this function.
Multiple resampling_methods are supported: either 'pooling' which
uses sum pooling (for downsampling only) or any method supported by
jax.image.scale_and_translate ('linear', 'cubic', 'lanczos3',
or 'lanczos5').
The input may have any number of dimensions after the first two, but
the returned function assumes that the 2D planes to be downsampled are
contained in the first two axes. Any other dimensions are treated as batch
dimensions, i.e. resampling is parallelized across those dimensions. In
order to add arbitrary batch dimensions before the first two dimensions,
use jax.vmap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
out_shape
|
tuple[int, ...]
|
A tuple representing the number of samples (pixels) to
which the incoming plane should be resampled in the format |
required |
out_spacing
|
ScalarLike | Float[Array, 2]
|
Either a scalar or a 1D array of size 2 (in the format
|
required |
resampling_method
|
str
|
A string representing the type of resampling
method to initialize. Can be either |
'linear'
|
Returns:
A Resampler, which is a
callable that actually performs the resampling.