API Reference
Enhancement
Settings
Configuration dataclasses for the VSPI fluorescence enhancement algorithm.
- class ptychozoon.settings.SaveFileExtensions(value)[source]
Bases:
StrEnumSupported output file format extensions for saving VSPI results.
- H5 = '.h5'
- TIFF = '.tiff'
- class ptychozoon.settings.InterpolationTypes(value)[source]
Bases:
StrEnumInterpolation strategies for mapping probe positions to object pixels.
- FOURIER
Fourier-shift interpolation. Requires a GPU (CuPy) array module. Produces the most accurate sub-pixel results.
- Type:
- BARYCENTRIC
Bilinear (barycentric) interpolation. Works on both CPU and GPU but is considerably slower than the Fourier method.
- Type:
- FOURIER = 'fourier'
- BARYCENTRIC = 'barycentric'
- class ptychozoon.settings.SolverTypes(value)[source]
Bases:
StrEnumIterative linear solver to use for the VSPI deconvolution.
- LSMR
Least-Squares Minimum Residual solver (
scipy.sparse.linalg.lsmr()). Supports optional Tikhonov regularisation via a damping factor.- Type:
- LSMR = 'lsmr'
- class ptychozoon.settings.LSMRSettings(damping_factor=0.0, gradient_smoothness=0.0, max_iter=10, atol=1e-06, btol=1e-06, checkpoint_interval=None)[source]
Bases:
objectHyper-parameters for the LSMR iterative solver.
- damping_factor
Tikhonov regularisation parameter. A value of
0.0(default) means no regularisation is applied.- Type:
- gradient_smoothness
Gradient-smoothness regularisation weight (λ). Penalises pixel-to-pixel variation via finite-difference operators along both image axes. A value of
0.0(default) disables this regulariser.- Type:
- atol
Tolerance on the norm of the relative residual
‖b − A x‖ / ‖b‖. Iteration stops when the residual drops below this value.- Type:
- btol
Tolerance on the norm of
A^T (b − A x). Controls convergence of the adjoint residual.- Type:
- checkpoint_interval
If set, the algorithm yields an intermediate result every this many LSMR iterations (e.g.
5→ yields at iterations 5, 10, 15, …). IfNone, only the final result is yielded.- Type:
int or None
- class ptychozoon.settings.GPUSettings(enabled=True, index=0)[source]
Bases:
objectSettings for GPU (CuPy) accelerated computation.
- enabled
Whether to use GPU acceleration. Requires CuPy and a CUDA-capable device. When
False, NumPy / SciPy are used instead.- Type:
- class ptychozoon.settings.DeconvolutionEnhancementSettings(solver=SolverTypes.LSMR, lsmr=<factory>, gpu=<factory>, _interpolation=InterpolationTypes.FOURIER)[source]
Bases:
objectTop-level configuration for the VSPI fluorescence enhancement algorithm.
- solver
Iterative linear solver to use for the deconvolution.
- Type:
- lsmr
Hyper-parameters for the LSMR solver.
- Type:
- interpolation
Interpolation strategy for mapping probe positions to object pixels.
- Type:
- gpu
GPU acceleration settings.
- Type:
- solver: SolverTypes = 'lsmr'
- lsmr: LSMRSettings
- gpu: GPUSettings
Viewer
Saving Results
Functions for saving VSPI fluorescence enhancement results.
- ptychozoon.save.save_vspi_results(folder, name, vspi_results, filetype, save_every_n_frames=1)[source]
Save VSPI results to disk.
Each element’s 2D maps across all checkpoint iterations are stacked into a 3D array of shape (n_frames, height, width) before saving. When saving to HDF5, an additional
"epochs"dataset is written containing the iteration number corresponding to each frame.- Parameters:
folder (str) – Parent output directory.
name (str) – Name suffix for this result set.
vspi_results (List[Tuple[FluorescenceDataset, int]]) – List of
(FluorescenceDataset, iteration_number)tuples as returned byVSPIFluorescenceEnhancingAlgorithm.enhance.filetype (SaveFileExtensions) – Output format —
SaveFileExtensions.TIFForSaveFileExtensions.H5.save_every_n_frames (int) – Stride for subsampling results before saving. For example,
2saves every other result. Defaults to1(save all results).
- ptychozoon.save.load_vspi_results_h5(h5_path)[source]
Load VSPI results from an HDF5 file saved by
save_vspi_results().
Patch Operations
- ptychozoon.patches.extract_patches_fourier_shift(image, positions, shape, pad=1)
Extract patches from a 2D array. If a patch’s footprint goes outside the image, the image is padded with zeros to account for the missing pixels.
- Parameters:
image (ndarray) – The whole image.
positions (ndarray) – An array of shape (N, 2) giving the center positions of the patches in pixels. The origin of the given positions are assumed to be the TOP LEFT corner of the image.
shape (tuple of int) – A tuple giving the patch shape in pixels.
pad (Optional[int]) – If given, patches with larger size than the intended size by this amount are cropped out from the patches before shifting.
- Returns:
An array of shape (N, H, W) containing the extracted patches.
- Return type:
ndarray
- ptychozoon.patches.fourier_shift(images, shifts, strictly_preserve_zeros=False)
Apply Fourier shift to a batch of images.
- Parameters:
images (ndarray) – An array of shape (N, H, W) containing the images to shift.
shifts (ndarray) – An array of shape (N, 2) giving the shifts in pixels.
strictly_preserve_zeros (bool) – If True, a mask of strictly zero pixels will be generated and shifted by the same amount. Pixels that have a non-zero value in the shifted mask will be set to zero in the shifted image. This preserves the zero pixels in the original image, preventing FFT from introducing small non-zero values due to machine precision.
- Returns:
Shifted images of the same shape as images.
- Return type:
ndarray
- ptychozoon.patches.batch_slice(image, sy, sx, patch_size)
Slice patches from an image at given window positions. The patch size is assumed to be the same for all patches.
- Parameters:
- Returns:
An array of shape (N, h, w) containing the extracted patches.
- Return type:
ndarray
- ptychozoon.patches.place_patches_fourier_shift(image, positions, patches, op='add', adjoint_mode=True, pad=1)
Place patches into a 2D array. If a patch’s footprint goes outside the image, the image is padded with zeros to account for the missing pixels.
- Parameters:
image (ndarray) – The whole image.
positions (ndarray) – An array of shape (N, 2) giving the center positions of the patches in pixels. The origin of the given positions are assumed to be the TOP LEFT corner of the image.
patches (ndarray) – A (N, H, W) or (H, W) array of image patches.
op (Literal["add", "set"]) – The operation to perform.
"add"adds the patches to the image,"set"replaces the existing values with the patch values.adjoint_mode (bool) – If True, this function performs the exact adjoint operation of
extract_patches_fourier_shift(). It runs the adjoint of every extraction step in reverse order: zero-pads the patches, shifts them back, and places them into the image. Use this when backpropagating gradients. Note that the zero-padding can introduce ripple artifacts around patch borders, so set this to False when placing non-gradient patches; in that case the patches are cropped after shifting to remove Fourier wrap-around.pad (Optional[int]) – If given, patches are padded (or cropped) by this amount before shifting. When
adjoint_modeis True the patches are zero-padded; otherwise they are cropped after shifting to remove wrap-around.
- Returns:
An array with the same shape as image with patches added or set.
- Return type:
ndarray
- ptychozoon.patches.batch_put(image, patches, sy, sx, op='add')
Place patches into an array at given window positions. The patch size is assumed to be the same for all patches.
- Parameters:
image (ndarray) – A (H, W) array acting as the buffer to place the patches into.
patches (ndarray) – A (N, h, w) array of the patches to place.
sy (ndarray) – A (N,) array of integers giving the starting y-coordinates of the patches.
sx (ndarray) – A (N,) array of integers giving the starting x-coordinates of the patches.
op (Literal["add", "set"]) – The operation to perform.
"add"adds the patches to the image,"set"replaces the existing values with the patch values.
- Returns:
An array of shape (H, W) containing the image with patches added or set.
- Return type:
ndarray
- ptychozoon.patches.get_scipy_module(array)[source]
Return the appropriate SciPy module for the given array type.
Selects
cupyx.scipywhen array is a CuPy array, andscipyfor NumPy arrays, enabling array-module-agnostic code.- Parameters:
array (ndarray) – Array whose module determines the SciPy variant returned.
- Returns:
cupyx.scipyfor CuPy arrays,scipyfor NumPy arrays.- Return type:
module