macromax.utils.ft package
Submodules
macromax.utils.ft.grid module
- class macromax.utils.ft.grid.Grid(shape=None, step=None, *, extent=None, first=None, center=None, last=None, include_last=False, ndim=None, flat=False, origin_at_center=True, center_at_index=True)[source]
Bases:
SequenceA class representing an immutable uniformly-spaced plaid Cartesian grid and its Fourier Transform. Unlike the MutableGrid, objects of this class cannot be changed after creation.
See also
MutableGrid- __init__(shape=None, step=None, *, extent=None, first=None, center=None, last=None, include_last=False, ndim=None, flat=False, origin_at_center=True, center_at_index=True)[source]
Construct an immutable Grid object.
Its values are defined by shape, step, center, and the boolean flags include_last and`center_at_index`. If not specified, the values for shape, step, and center center are deduced from the other values, including first, last, and extent. A larger even shape is assumed in case of ambiguity. If all else fails, first the step is assumed to be 1, then the center is assumed to be as close as possible to 0, and finally the shape is assumed to be 1.
Specific invariants:
`shape * step == extent == last + step - first`if include_last`shape * step == extent == last - first`if not include_last`center == first + shape // 2 * step`if center_at_index`center == first + (shape - 1) / 2 * step`if not center_at_index
General invariants:
`shape * step == extent == last + step * include_last - first``center == first + (shape // 2 * center_at_index + (shape - 1) / 2 * (1 - center_at_index)) * step`
- Parameters:
shape – An integer vector array with the shape of the sampling grid.
step – A vector array with the spacing of the sampling grid. This defaults to 1 if no two of first, center, or last are specified.
extent – The extent of the sampling grid as shape * step = last - first.
first – A vector array with the first element for each dimension. The first element is the smallest element if step is positive, and the largest when step is negative.
center – A vector array with the central value for each dimension. The center value is that at the central index in the grid, rounded up if center_at_index==True, or the average of the surrounding values when False. The center value defaults to 0 if neither first nor last is specified.
last – A vector array with the last element for each dimension. Note that unless include_last is set to True for the associated dimension, all but the last element is returned when calling self[axis]!
include_last – A boolean vector array indicating whether the returned vectors, self[axis], should include the last element (True) or the penultimate element (default == False). When the step size is not specified, it is determined so that the step * (shape - 1) == extent.
ndim (
int) – A scalar integer indicating the number of dimensions of the sampling space.flat (
Union[bool,Sequence,ndarray]) – A boolean vector array indicating whether the returned vectors, self[axis], should be flattened (True) or returned as an open grid (False)origin_at_center (
Union[bool,Sequence,ndarray]) – A boolean vector array indicating whether the origin should be fft-shifted (True) or be ifftshifted to the front (False) of the returned vectors for self[axis].center_at_index (
Union[bool,Sequence,ndarray]) – A boolean vector array indicating whether the center of an even dimension is included in the grid. When left as False and the shape has an even number of elements in the corresponding dimension, then the next index is used as the center, (self.shape / 2).astype(int). When set to True and the number of elements in the corresponding dimension is even, then the center value is not included, only its preceding and following elements.
- static from_ranges(*ranges)[source]
Converts one or more ranges of numbers to a single Grid object representation. The ranges can be specified as separate parameters or as a tuple.
- property shape: array
The number of sample points along each axis of the grid.
- property center_at_index: array
Boolean vector indicating whether the central coordinate is aligned with a grid point when the number of points is even along the associated axis. This has no effect when the the number of sample points is odd.
- property flat: array
Boolean vector indicating whether self[axis] returns flattened (raveled) vectors (True) or not (False).
- property origin_at_center: array
Boolean vector indicating whether self[axis] returns ranges that are monotonous (True) or ifftshifted so that the central index is the first element of the sequence (False).
- property as_flat: Grid
return: A new Grid object where all the ranges are 1d-vectors (flattened or raveled)
- property as_non_flat: Grid
return: A new Grid object where all the ranges are 1d-vectors (flattened or raveled)
- property as_origin_at_0: Grid
return: A new Grid object where all the ranges are ifftshifted so that the origin as at index 0.
- property as_origin_at_center: Grid
return: A new Grid object where all the ranges have the origin at the center index, even when the number of elements is odd.
- project(axes_to_keep=None, axes_to_remove=None)[source]
Removes all but the specified axes and reduces the dimensions to the number of specified axes.
- Parameters:
- Return type:
- Returns:
A Grid object with ndim == len(axes) and shape == shape[axes].
- property dtype
The numeric data type for the coordinates.
- __matmul__(other)[source]
Determines the Grid spanning the tensor space, with ndim equal to the sum of both ndims.
- __len__()[source]
The number of axes in this sampling grid. Or, the number of elements when this object is not multi-dimensional.
- Return type:
- property mutable: MutableGrid
Return a new MutableGrid object.
- property multidimensional: bool
Single-dimensional grids behave as Sequences, multi-dimensional behave as a Sequence of vectors. TODO: Remove this feature? It tends to be a source of bugs.
- __init_subclass__()
Function to initialize subclasses.
- count(value) integer -- return number of occurrences of value
- index(value[, start[, stop]]) integer -- return first index of value.
Raises ValueError if the value is not present.
Supporting start and stop arguments is optional, but recommended.
- class macromax.utils.ft.grid.MutableGrid(shape=None, step=None, extent=None, first=None, center=None, last=None, include_last=False, ndim=None, flat=False, origin_at_center=True, center_at_index=True)[source]
Bases:
GridA class representing a mutable uniformly-spaced plaid Cartesian grid and its Fourier Transform.
See also
Grid- __init__(shape=None, step=None, extent=None, first=None, center=None, last=None, include_last=False, ndim=None, flat=False, origin_at_center=True, center_at_index=True)[source]
Construct a mutable Grid object.
- Parameters:
shape – An integer vector array with the shape of the sampling grid.
step – A vector array with the spacing of the sampling grid.
extent – The extent of the sampling grid as shape * step
first – A vector array with the first element for each dimension. The first element is the smallest element if step is positive, and the largest when step is negative.
center – A vector array with the center element for each dimension. The center position in the grid is rounded to the next integer index unless center_at_index is set to False for that partical axis.
last – A vector array with the last element for each dimension. Unless include_last is set to True for the associated dimension, all but the last element is returned when calling self[axis].
include_last – A boolean vector array indicating whether the returned vectors, self[axis], should include the last element (True) or all-but-the-last (False)
ndim (
int) – A scalar integer indicating the number of dimensions of the sampling space.flat (
Union[bool,Sequence,ndarray]) – A boolean vector array indicating whether the returned vectors, self[axis], should be flattened (True) or returned as an open grid (False)origin_at_center (
Union[bool,Sequence,ndarray]) – A boolean vector array indicating whether the origin should be fft-shifted (True) or be ifftshifted to the front (False) of the returned vectors for self[axis].center_at_index (
Union[bool,Sequence,ndarray]) – A boolean vector array indicating whether the center of the grid should be rounded to an integer index for each dimension. If False and the shape has an even number of elements, the next index is used as the center, (self.shape / 2).astype(int).
- property shape: array
The number of sample points along each axis of the grid.
- property flat: array
Boolean vector indicating whether self[axis] returns flattened (raveled) vectors (True) or not (False).
- property origin_at_center: array
Boolean vector indicating whether self[axis] returns ranges that are monotonous (True) or ifftshifted so that the central index is the first element of the sequence (False).
- __init_subclass__()
Function to initialize subclasses.
- __iter__()
- __len__()
The number of axes in this sampling grid. Or, the number of elements when this object is not multi-dimensional.
- Return type:
- __matmul__(other)
Determines the Grid spanning the tensor space, with ndim equal to the sum of both ndims.
- __mul__(factor)
Scales all ranges with a factor.
- __neg__()
Invert the coordinate values and the direction of the axes.
- __rmul__(factor)
Scales all ranges with a factor.
- __truediv__(denominator)
Divide the grid coordinates by a value.
- property as_flat: Grid
return: A new Grid object where all the ranges are 1d-vectors (flattened or raveled)
- property as_non_flat: Grid
return: A new Grid object where all the ranges are 1d-vectors (flattened or raveled)
- property as_origin_at_0: Grid
return: A new Grid object where all the ranges are ifftshifted so that the origin as at index 0.
- property as_origin_at_center: Grid
return: A new Grid object where all the ranges have the origin at the center index, even when the number of elements is odd.
- property center_at_index: array
Boolean vector indicating whether the central coordinate is aligned with a grid point when the number of points is even along the associated axis. This has no effect when the the number of sample points is odd.
- count(value) integer -- return number of occurrences of value
- static from_ranges(*ranges)
Converts one or more ranges of numbers to a single Grid object representation. The ranges can be specified as separate parameters or as a tuple.
- index(value[, start[, stop]]) integer -- return first index of value.
Raises ValueError if the value is not present.
Supporting start and stop arguments is optional, but recommended.
- property multidimensional: bool
Single-dimensional grids behave as Sequences, multi-dimensional behave as a Sequence of vectors. TODO: Remove this feature? It tends to be a source of bugs.
- property mutable: MutableGrid
Return a new MutableGrid object.
- project(axes_to_keep=None, axes_to_remove=None)
Removes all but the specified axes and reduces the dimensions to the number of specified axes.
- Parameters:
- Return type:
- Returns:
A Grid object with ndim == len(axes) and shape == shape[axes].
- property dtype
The numeric data type for the coordinates.
macromax.utils.ft.subpixel module
Classes and functions to register a subject array to a reference array with subpixel precision. This is based on the algorithm described in: Manuel Guizar-Sicairos, Samuel T. Thurman, and James R. Fienup, “Efficient subpixel image registration algorithms,” Optics Letters. 33, 156-158 (2008).
- macromax.utils.ft.subpixel.register(subject=None, reference=None, precision=None)[source]
Registers a subject array to a reference array with subpixel precision. This is based on the algorithm described in Manuel Guizar-Sicairos, Samuel T. Thurman, and James R. Fienup, “Efficient subpixel image registration algorithms,” Optics Letters. 33, 156-158 (2008).
- Parameters:
- Return type:
- Returns:
A Registration instance describing the registered image, the shift, and the amplitude.
- macromax.utils.ft.subpixel.roll(subject, shift, axis=None)[source]
Rolls (shifting with wrapping around) and nd-array with sub-pixel precision.
- Parameters:
subject (
ndarray) – The to-be-shifted nd-array. Default: all zeros except first element.shift (
Union[int,float,Sequence,ndarray,None]) – The (fractional) shift. Default: all zeros except first element.axis (
Union[int,Sequence,ndarray,None]) – Optional int or sequence of ints. The axis or axes along which elements are shifted. Unlike numpy’s roll function, by default, the left-most axes are used.
- Returns:
The shifted nd-array.
- macromax.utils.ft.subpixel.roll_ft(subject_ft, shift, axes=None)[source]
Rolls (shifting with wrapping around) and nd-array with sub-pixel precision. The input and output array are Fourier transformed.
- Parameters:
subject_ft (
ndarray) – The Fourier transform of the to-be-shifted nd-array.shift (
Union[int,float,Sequence,ndarray,None]) – The (fractional) shift.axes (
Union[int,Sequence,ndarray,None]) – Optional int or sequence of ints. The axis or axes along which elements are shifted. Unlike numpy’s roll function, by default, the left-most axes are used.
- Returns:
The Fourier transform of the shifted nd-array.
- class macromax.utils.ft.subpixel.Registration(shift, factor=1.0, error=0.0, original_ft=None, original=None, registered_ft=None, registered=None)[source]
Bases:
objectA class to represent the result of a registration of reference class.
- __init__(shift, factor=1.0, error=0.0, original_ft=None, original=None, registered_ft=None, registered=None)[source]
Constructs a registration result.
- Parameters:
shift (
Union[int,float,Sequence,ndarray]) – translation in pixels of the registered with respect to original referencefactor (
complex) – scaling factor between registered and original reference imageerror (
float) – The root-mean-square difference after registration. See`register`for more details.original_ft (
Union[int,float,Sequence,ndarray,None]) – The Fourier transform of the original image, prior to registration.original (
Union[int,float,Sequence,ndarray,None]) – The original image, prior to registration.registered_ft (
Union[int,float,Sequence,ndarray,None]) – The Fourier transform of the registered image, identical to the original but with a sub-pixel shift.registered (
Union[int,float,Sequence,ndarray,None]) – The registered image, identical to the original but with a sub-pixel shift.
- property shift: ndarray
Vector indicating subpixel shift between the original and registration image.
- property factor
(Complex) scaling factor indicating the ratio between original and registered image.
- property error: float
The rms difference between the registered and the original image including rescaling factor.
- class macromax.utils.ft.subpixel.Reference(reference_image=None, precision=None, axes=None, ndim=None, reference_image_ft=None)[source]
Bases:
objectRepresents a reference ‘image’ (which can be n-dimensional) to be used to register against. Multi-channel arrays should be handled iteratively or by averaging over the channels.
- __init__(reference_image=None, precision=None, axes=None, ndim=None, reference_image_ft=None)[source]
Construct a reference ‘image’ object. If neither reference_image or its Fourier transform reference_image_ft are specified, a point-reference is assumed, where the point is in the first element of the nd-array (top-left corner).
- Parameters:
reference_image (
Union[int,float,Sequence,ndarray,None]) – The reference image nd-array. As an alternative, its unshifted Fourier transform can be specified as reference_image_ft.precision (
Optional[float]) – (optional) The default sub-pixel precision (default: 1/128).axes (
Union[int,float,Sequence,ndarray,None]) – (optional) The axes to operate on. If not specified, all dimensions of the reference image or its Fourier transform are used.ndim (
Optional[int]) – The number of dimensions is usually determined from . If neither is specified, ndim determines the number of dimensions to operate on.reference_image_ft (
Union[int,float,Sequence,ndarray,None])
- property shape
- register(subject=None, subject_ft=None, precision=None)[source]
Register an nd-image to sub-pixel precision.
Algorithm based on the 2-d implementation of: Manuel Guizar-Sicairos, Samuel T. Thurman, and James R. Fienup, “Efficient subpixel image registration algorithms,” Opt. Lett. 33, 156-158 (2008).
- Parameters:
subject (
Optional[array]) – The subject image as an nd-array. If this is not specified, its Fourier transform should be.subject_ft (
Optional[array]) – (optional) The (non-fftshifted) Fourier transform of the subject image.precision (
float) – (optional) The sub-pixel precision in units of pixel (default: 1/128).
- Return type:
- Returns:
A Registration object representing the registered image as well as the shift, global phase change, and error.