Spatial utilities¶
Functions for spatial partitioning, bounding-box computation, fragment index arithmetic, and chunk coordinate manipulation. These are the internal building blocks used by the read/write functions; they are also useful for contributors implementing custom readers or geometry operations.
Chunking¶
Spatial chunk assignment and query utilities.
All functions are pure numpy — no store or encoding dependencies.
Chunk assignment is vectorised: assign_chunks processes millions
of vertices in one pass using np.floor and structured-array
np.unique.
- zarr_vectors.spatial.chunking.assign_bins(positions, bin_shape)[source]¶
Assign each vertex to a supervoxel bin.
Identical to
assign_chunks()but uses bin_shape instead of chunk_shape — produces a finer spatial grouping.- Parameters:
- Returns:
Dict mapping
bin_coords→(N_k,)array of vertex indices.- Return type:
- zarr_vectors.spatial.chunking.assign_chunks(positions, chunk_shape)[source]¶
Assign each vertex to a spatial chunk.
- Parameters:
- Returns:
Dict mapping
chunk_coords→(N_k,)array of vertex indices belonging to that chunk. Indices are into the original positions array.- Raises:
ChunkingError – If dimensions are inconsistent.
- Return type:
- zarr_vectors.spatial.chunking.bin_to_chunk(bin_coords, bins_per_chunk)[source]¶
Map a bin coordinate to its parent chunk coordinate.
- zarr_vectors.spatial.chunking.bin_to_fragment_index(bin_coords, chunk_coords, bins_per_chunk)[source]¶
Linearise an intra-chunk bin coordinate to a fragment index.
Uses row-major (C-order) linearisation within the chunk’s bin grid.
- zarr_vectors.spatial.chunking.bins_intersecting_bbox(bbox_min, bbox_max, bin_shape)[source]¶
Return all bin coordinates that intersect a bounding box.
Finer-grained version of
chunks_intersecting_bbox().- Parameters:
- Returns:
Sorted list of bin coordinate tuples.
- Return type:
- zarr_vectors.spatial.chunking.chunk_to_bin_range(chunk_coords, bins_per_chunk)[source]¶
Return the range of bin coordinates within a chunk (inclusive).
- zarr_vectors.spatial.chunking.chunks_intersecting_bbox(bbox_min, bbox_max, chunk_shape)[source]¶
Return all chunk coordinates that intersect a bounding box.
- Parameters:
- Returns:
Sorted list of chunk coordinate tuples.
- Return type:
- zarr_vectors.spatial.chunking.compute_chunk_coords(position, chunk_shape)[source]¶
Compute chunk coordinates for a single position.
- zarr_vectors.spatial.chunking.compute_grid_shape(bounds, chunk_shape)[source]¶
Compute number of chunks per dimension.
- Parameters:
- Returns:
Tuple of chunk counts per dimension. Each value is at least 1.
- Return type:
- zarr_vectors.spatial.chunking.fragment_index_to_bin(fragment_index, chunk_coords, bins_per_chunk)[source]¶
Convert a fragment index back to a global bin coordinate.
Inverse of
bin_to_fragment_index().
- zarr_vectors.spatial.chunking.group_bins_by_chunk(bin_assignments, bins_per_chunk)[source]¶
Group bin assignments into chunks with linearised fragment indices.
Takes the output of
assign_bins()and organises it by chunk. Each entry maps a fragment index (linearised bin position within the chunk) to the array of global vertex indices in that bin.
- zarr_vectors.spatial.chunking.neighbouring_chunk_keys(key, *, halo=1, occupied_keys=None, include_self=False)[source]¶
Return chunk keys within
haloofkeyalong every axis.Pure integer-tuple work — no store I/O — so it composes with any chunk-key arity (3D, 4D when attribute-chunked, etc.).
- Parameters:
halo (int) – Maximum per-axis distance.
halo=1returns the (3^ndim - 1) chunks adjacent on at least one face, edge, or corner;halo=2widens to a 5^ndim cube.occupied_keys (Iterable[tuple[int, ...]] | None) – If given, restrict the output to keys that actually exist in the store. Pass
set(level.chunk_keys)to filter to occupied chunks.include_self (bool) – If
True, includekeyitself in the output.
- Returns:
Sorted list of unique
ChunkCoordstuples.- Return type: