ImageD11.sinograms.voxel_mask module#
Written by Axel Henningsson, 2026.
Updated and slightly mangled by James Ball, 2026.
A voxel at (sx,sy) is illuminated when: | y0 - sx*sin(omega) - sy*cos(omega) - dty | <= ystep for one voxel that traces a sinusoid in (omega, dty) space. The thing we want to avoid is finding the peaks for a given (sx,sy) by testing this predicate against every peak in the sinogram, which is very slow.
We do this in the following way (see VoxelSinoMasker.partition() for details): 1. We bin the peaks in omega and dty, either by supplied omega bin centres or by a heuristic that guesses a good omega bin size. dty bins are fixed by ystep. 2. We sort the peaks by omega bin indices (primary) and raw dty values (secondary) to determine a peak ordering. We use raw dty because it changes within an omega bin. 3. We make partition arrays, which track the start of each omega and dty bin in the sorted arrays omega_partitions[i] is the index of the first (sorted) peak in omega bin i dty_partitions[i,j] is the offset, within omega bin i, of the first (sorted) peak in dty bin j 4. We precompute sin(omega) and cos(omega) for each peak for speed 5. We precompute sin(omega_bin) and cos(omega_bin) for each omega bin for speed
Then, to find the peaks that illuminate a voxel at (sx,sy) (see fill_voxel_idx() for details): 1. We walk the omega bins, and for each bin compute the dty value of the voxel at that omega 2. We determine the corresponding dty value for the -next- omega bin 3. We convert the difference in dty values between omega bins into a dty bin padding for that omega bin 4. This tells us, as a function of omega, how “wide” our search space is in dty bins 5. We walk the omega bins again, computing the y coordinate of the voxel at that omega, and the corresponding dty bin 6. Including the dty bin padding from the previous step, we now know the dty partitions to visit for this omega bin 7. We get the start and end indices from dty_partitions and omega_partitions 8. Within an omega partition, peaks are monotonically increasing in dty, so we can just do peaks[start:end] 9. For any peak in that range, we compute the exact y distance to the voxel and check if it is within ystep. If so, we keep it. 10. We fill the idx_buffer and ydist_buffer with the (sorted!) indices and distances of the peaks that illuminate the voxel. We also keep track of how many peaks we found (m), and return that number. If the buffers are too small, we return -1. This whole process means that we never miss peaks. The only purpose is to reduce the number of peaks we have to test the predicate for.
We now have an idx_buffer and ydist_buffer that contain the indices and distances of the peaks that illuminate the voxel at (sx,sy). By just returning the first m elements of the buffers, we can avoid any memory allocation and reuse the buffers for all voxels in a grid. These buffers can be optimally sized for a given (sx,sy) grid - see max_candidates for details.
- class ImageD11.sinograms.voxel_mask.VoxelSinoMasker(omega, dty, dty_stepsize, ymin=None)#
Bases:
objectMasker for voxels in sinogram space.
This class is used to mask voxels in sinogram space. It is designed to be used with a regular grid of voxels.
A partition scheme is used such that peaks are sorted by omega (primary) and dty (secondary). This allows for fast retrieval of peaks that are within a voxel by only looking at the relevant bins.
The partition scheme is built on the assumption that the scan defines a regular grid in dty. Omega values can be arbitrary, (i.e merged 3d peaks should work as well as single 2d peaks).
omega_partitions[i] is the index of the first (sorted) peak in omega bin i dty_partitions[i,j] is the offset, within omega bin i, of the first (sorted) peak in dty bin j
Adaptable omega bin sizes are used to chunk the data over angles. For a call to mask() the algorithm then oterates over the omega bins and collects partitions of dty values that are candidates for the voxel.
- Parameters:
omega (
np.ndarray) – The omega values, one per peak, shape=(n_peaks,). (degrees)dty (
np.ndarray) – The dty values, one per peak, shape=(n_peaks,).dty_stepsize (
float) – The dty step size, assumed to be constant such that the scan defines a regular grid in dty.ymin – ds.ybincens.min()
- omega_binsize#
The omega bin size.
- Type:
float
- peak_ordering#
The peak ordering.
- Type:
np.ndarray
- omega_partitions#
The omega partitions.
- Type:
np.ndarray
- omega_bins#
The omega bins.
- Type:
np.ndarray
- dty_partitions#
The dty partitions.
- Type:
np.ndarray
- ymin#
The minimum y-coordinate.
- Type:
float
- n_peaks#
The number of peaks.
- Type:
int
- omega#
The omega values.
- Type:
np.ndarray
- sinomega#
The sin_omega values.
- Type:
np.ndarray
- cosomega#
The cos_omega values.
- Type:
np.ndarray
- dty#
The dty values.
- Type:
np.ndarray
- dty_stepsize#
The dty step size.
- Type:
float
- mask(xi0, yi0, ystep, y0)#
- max_candidates(xi0s, yi0s, ystep, y0)#
Largest number of peaks any of these voxels can pull out of the partition.
If you size the buffers with this, fill_voxel_idx() can never overflow, which matters when using Numba to avoid getting invalid results.
- partition(omega_binsize=None, inplace=False, omega_bin_indices=None, omega_bins=None, keep_columns=True)#
Partition the peaks for fast peak masking.
A partition scheme is used such that peaks are sorted by omega (primary) and dty (secondary). This allows for fast retrieval of peaks that are within a voxel by only looking at the relevant bins.
The partition scheme is built on the assumption that the scan defines a regular grid in dty. Omega values can be arbitrary, (i.e merged 3d peaks should work as well as single 2d peaks).
Adaptable omega bin sizes are used to chunk the data over angles. For a call to mask() the algorithm then oterates over the omega bins and collects partitions of dty values that are candidates for the voxel.
For a finer omega_binsize the algorithm can decrease the size of the dty partitions it needs to visit, however, more omega bins are then required. For any dataset there exists an optimal omega_binsize that minimizes the number of flops. This can also be machine dependent, so for absolutely best performance you should test different values. However, the default heuristic should be very performant for most datasets.
- Parameters:
omega_binsize (
float) – The omega bin size. If None, a heuristic will be used to find a good value. (degrees) (note that this is not the same as the experimental stepsize of the omega motor.)inplace (
bool) – If True, reorder self.omega and self.dty in place. These are the same arrays the caller passed to __init__, so in place means the caller’s columns are reordered too and every other column they hold is then misaligned against them. Defaults to False, which costs one copy of each.omega_bin_indices (
np.ndarray) – Optional precomputed bin index per peak, e.g. decoded from a frame number. Overrides omega_binsize.omega_bins (
np.ndarray) – Bin centres in degrees matching omega_bin_indices.keep_columns (
bool) – If False, the permuted dty/omega/ sinomega/cosomega copies and the mask() buffers are not built. Saves four full columns of memory. mask() then raises; use this when you only want peak_ordering and the partition arrays and will permute your own columns.
- Raises:
ValueError – If the omega binsize is not set and the heuristic fails to find a good value.
- ImageD11.sinograms.voxel_mask.choose_omega_bins(manager, omega, omega_binsize='auto', omega_step=None, verbose=True)#
Decide how to bin omega, best method first.
frm column decoded through dset.omega / obinedges – exact
dset.obinedges alone – pre-frm data
VoxelSinoMasker’s heuristic – last resort
An explicit omega_step or omega_binsize overrides all three.
manager needs .icolf and .dset; both PBPRefine and PBP have them.
Returns (kw, iomega) where kw goes straight to partition(**kw) and iomega is the per-peak bin index, or None when the heuristic is used and the caller must reconstruct it. Callers that need dev should use iomega rather than the masker’s permuted omega, which keep_columns=False does not build.
- Parameters:
manager – PBP or PBPRefine object
omega – Columnfile column
omega_binsize – _description_, by default “auto”
optional – _description_, by default “auto”
omega_step – _description_, by default None
optional – _description_, by default None
verbose – _description_, by default True
optional – _description_, by default True
- Returns:
iomega: Like dtyi but for omega. Integer omega_steps away from omega min. kw: dict of the stuff you need to build
- Return type:
(kw, iomega)
- ImageD11.sinograms.voxel_mask.fill_voxel_idx(sx, sy, y0, ystep, ymin, omega_partitions, dty_partitions, dty_sorted, sin_omega_sorted, cos_omega_sorted, sin_omega_bins, cos_omega_bins, idx_buffer, ydist_buffer)#
Get the voxel indices and y-distances for a given voxel centroid position.
This tracks the voxel in sinogram space and returns all peaks the voxel possibly intersects.
The idea is to use a partitioning scheme that allows for retrival of these peaks without touching the entire sinogram.
- Parameters:
sx (
float) – The x-coordinate of the voxel position in sample coordinates.sy (
float) – The y-coordinate of the voxel position in sample coordinates.y0 (
float) – Rotaiton axis offset.ystep (
float) – The y-step size. Voxels with centroids within ystep from the beam center are masked.ymin (
float) – The minimum y-coordinate expected dty_sorted.omega_partitions (
np.ndarray) – The omega partitions. See VoxelSinoMasker.partition() for details.dty_partitions (
np.ndarray) – The dty partitions. See VoxelSinoMasker.partition() for details.dty_sorted (
np.ndarray) – The sorted dty values. See VoxelSinoMasker.partition() for details.sin_omega_sorted (
np.ndarray) – The sorted sin_omega values. See VoxelSinoMasker.partition() for details.cos_omega_sorted (
np.ndarray) – The sorted cos_omega values. See VoxelSinoMasker.partition() for details.sin_omega_bins (
np.ndarray) – The sin_omega bins. See VoxelSinoMasker.partition() for details.cos_omega_bins (
np.ndarray) – The cos_omega bins. See VoxelSinoMasker.partition() for details.idx_buffer (
np.ndarray) – The buffer for the voxel indices. This is intended to be reused when masking voxels over a grid. The buffer size is user-defined allowing for optimizations. Setting it to dty_sorted.size is 100% safe, but likely very very overkill.idx_buffer – The buffer for the voxel indices. This is intended to be reused when masking voxels over a grid. The buffer size is user-defined allowing for optimizations. Setting it to dty_sorted.size is 100% safe, but likely very very overkill.
- Returns:
the number of peaks written into the buffers, or -1 if the buffers were too small.
- Return type:
int
Note
This returns a sentinel rather than raising because it is meant to be callable from inside a numba prange, where an exception is not reliably propagated: the thread stops writing and every item it still owned is silently left unfilled. get_voxel_idx() wraps this and raises, for Python-level callers.
- ImageD11.sinograms.voxel_mask.get_voxel_idx(sx, sy, y0, ystep, ymin, omega_partitions, dty_partitions, dty_sorted, sin_omega_sorted, cos_omega_sorted, sin_omega_bins, cos_omega_bins, idx_buffer, ydist_buffer)#
Slice-returning wrapper around fill_voxel_idx(). See that for details.
- Raises:
ValueError – If the buffer arrays are too small. Then simply try to set your buffer size larger.
- ImageD11.sinograms.voxel_mask.load_peak_index_cache(filename, mmap=False, verbose=True)#
Load peak index from the icolf HDF5 file, with optional mem-map.
Reads the “PBPPeakIndex” group written by save_peak_index_cache from the icolf HDF5 file.
Use it from multiprocessing workers: the arrays are identical in every worker and never written, so one mapping shared through the page cache replaces one heap copy per process. Falls back to a read for any dataset that is not contiguous.
- Parameters:
filename – icolf HDF5 file to load the index group from
mmap – returns read-only np.memmap views instead of copies. by default False
optional – returns read-only np.memmap views instead of copies. by default False
verbose – Print info, by default True
optional – Print info, by default True
- Return type:
Dictionary representing a peak index
- ImageD11.sinograms.voxel_mask.max_candidates(sx_arr, sy_arr, y0, ystep, ymin, dty_partitions, sin_omega_bins, cos_omega_bins)#
Upper bound on how many peaks can be found per voxel.
Works very similarly to fill_voxel_idx(). Here, we only read the partition offsets to find out how many peaks are in the dty sub-partition for each omega bin. We never compute the predicate on a peak level, so this remains fast. This is used to determine the appropriate buffer size for the idx_buffer and ydist_buffer that are used in fill_voxel_idx().
- Parameters:
sx_arr – Array of x-coordinates of the voxel positions in sample coordinates.
sy_arr – Array of y-coordinates of the voxel positions in sample coordinates.
y0 – The rotation axis offset.
ystep – The y-step size.
ymin – The minimum y-coordinate.
dty_partitions – The dty partitions.
sin_omega_bins – The sin_omega bins.
cos_omega_bins – The cos_omega bins.
- Return type:
The maximum number of candidates across all supplied voxels
- ImageD11.sinograms.voxel_mask.save_peak_index_cache(idx, filename)#
Save peak index into an HDF5 group in the icolf file, with versioning.
The peak index is a lookup/partition table FOR the icolf it accompanies, so it is stored as the “PBPPeakIndex” group inside the same HDF5 file as the icolf’s “peaks” group rather than as a separate sidecar file. The columns group is left untouched; only the peak index group is replaced.
- Parameters:
idx – Peak index to cache
filename – icolf HDF5 file to store the index group in