core.utils.chunks

Chunk definition computation module

gridr.core.utils.chunks.get_chunk_boundaries(nsize, chunk_size, merge_last=False)[source]

Compute chunks from a total number of elements and a chunk size.

This method divides a total number of elements (nsize) into smaller segments (chunks) based on a specified chunk_size. Each chunk is represented by a tuple (start_index, end_index).

The merge_last argument provides an option to merge the final chunk with the preceding one if its size is less than the chunk_size. This prevents very small chunks at the end of the sequence.

Parameters:
  • nsize (int) – The total number of elements to be divided into chunks.

  • chunk_size (int) – The desired maximum size for each chunk. Must be a positive integer.

  • merge_last (bool, default False) – If True, the last chunk will be merged with the second-to-last chunk if its size is smaller than chunk_size.

Returns:

A list of tuples, where each tuple (start, end) represents the inclusive start and exclusive end indices of a chunk.

Return type:

list[tuple[int, int]]

gridr.core.utils.chunks.get_chunk_shapes(shape, chunk_shape, merge_last=False)[source]

Compute chunks for an N-dimensional shape.

This method calculates the tensor product of chunks for each axis of an N-dimensional array based on a given chunk_shape.

Parameters:
  • shape (tuple of int) – The N-dimensional shape to be chunked, e.g., (rows, cols, depth). Each element must be a non-negative integer.

  • chunk_shape (tuple of int) – The desired shape of a single chunk, e.g., (chunk_rows, chunk_cols). Its length must match the shape’s length, and each element must be a positive integer.

  • merge_last (bool, default False) – If True, the last chunk along each dimension will be merged with the previous one if its size is smaller than the corresponding chunk_shape dimension.

Returns:

A list where each element is an N-dimensional chunk definition. Each N-dimensional chunk is represented as a tuple of N 2-element tuples, where each 2-element tuple (start, end) defines the inclusive start and exclusive end indices for that dimension.

Return type:

list[tuple[tuple[int, int], …]]