neu_lib.grid¶
Aligning a box to a block grid, per axis, and the BBox type.
Which grid matters depends on the caller: a store’s chunks or shards (a partial write is a
read-modify-write, and two concurrent partial writes into one chunk lose one of them), a
pyramid’s cumulative downsampling factor, or the per-axis LCM of two chunkings. This module
only does the integer arithmetic, independently per axis, because real grids are
anisotropic: (1, 2, 2) pyramids and (128, 2048, 2048) chunks are both common.
Boxes are half-open, [lo, hi): a hi already on a block boundary stays there.
Attributes¶
Classes¶
A half-open box |
Functions¶
|
Align the half-open box |
|
Trim the box to |
|
Per-axis least common multiple of several block shapes: a box on this grid is on |
|
Axes where an edge of |
Module Contents¶
- class neu_lib.grid.BBox¶
A half-open box
[lo, hi)with integer bounds, of any rank.Frozen and hashable; every operation returns a new box. Axis order is the caller’s (the suite uses zyx).
- aligned(block: Sequence[int], mode: str = 'outer') BBox¶
align_box(), as a method.
- clamped(extent: Sequence[int]) BBox¶
clamp_box(), as a method. Raises if nothing is left, which usually means the box is in the wrong frame; useintersect()when no overlap is a valid answer.
- contains(points: Any) Any¶
Whether each point falls in
[lo, hi).A single point gives a
bool; an(N, D)array gives a(N,)bool array.
- classmethod empty(ndim: int = 3) BBox¶
A box with zero extent on every axis — the identity for
union().
- classmethod from_points(points: Any) BBox¶
The smallest box containing every point of an
(N, D)array.hiismax + 1, so the box contains the extreme points. Raises on an empty set; useempty()for an empty box.
- is_empty() bool¶
- misaligned_axes(block: Sequence[int], extent: Sequence[int] | None = None) list[int]¶
misaligned_axes(), as a method.
- slices() tuple[slice, ...]¶
- translate(offset: Sequence[float]) BBox¶
Move the box. A fractional offset is floored, like the grid arithmetic in this module, rather than truncated toward zero: a shift of -0.5 moves the box by -1, not by nothing.
- union(other: BBox) BBox¶
The smallest box containing both. An empty operand is ignored, so
reduce(BBox.union, boxes, BBox.empty())works.
- property center: Any¶
The midpoint, as a float array. Not rounded: a box of odd extent has no integer centre.
- hi: tuple[int, ...]¶
- property hi_array: Any¶
hias an int64 array.
- lo: tuple[int, ...]¶
- property lo_array: Any¶
loas an int64 array.
- property ndim: int¶
- property shape: tuple[int, ...]¶
- property size: int¶
Voxel count. Zero exactly when the box is empty.
- neu_lib.grid.align_box(lo: Sequence[int], hi: Sequence[int], block: Sequence[int], mode: str = 'outer') tuple[tuple[int, ...], tuple[int, ...]]¶
Align the half-open box
[lo, hi)to theblockgrid, per axis.outerfloorsloand ceilshi, so no requested voxel is lost. It never fails.innerceilsloand floorshi, so the result lies inside the request.nearestrounds both ends.originroundsloand keeps the extent, for a fixed-size crop;hiis then generally off the grid.
Raises
ValueErrorif an axis would be left empty (inneron a box spanning no whole block,neareston one whose ends round together).
- neu_lib.grid.clamp_box(lo: Sequence[int], hi: Sequence[int], extent: Sequence[int]) tuple[tuple[int, ...], tuple[int, ...]]¶
Trim the box to
[0, extent)per axis. An edge clamped to the end of the volume may fall off the grid, which is fine (seemisaligned_axes()).
- neu_lib.grid.lcm_grid(*grids: Sequence[int]) tuple[int, ...]¶
Per-axis least common multiple of several block shapes: a box on this grid is on all of them, e.g.
lcm_grid((128, 128, 128), (1, 2, 2)) == (128, 128, 128).
- neu_lib.grid.misaligned_axes(lo: Sequence[int], hi: Sequence[int], block: Sequence[int], extent: Sequence[int] | None = None) list[int]¶
Axes where an edge of
[lo, hi)falls inside a block.With
extentgiven, an edge at the end of the volume counts as aligned: the final block is partial anyway, so there is no neighbouring data to read-modify-write.
- neu_lib.grid.MODES = ('outer', 'inner', 'nearest', 'origin')¶