ExZarr.ChunkGrid behaviour (ExZarr v1.1.0)
View SourceBehavior for chunk grid implementations in Zarr v3.
Chunk grids define how arrays are partitioned into chunks. The Zarr v3 specification supports multiple chunk grid types:
- Regular: All chunks have the same shape (except edge chunks)
- Irregular: Chunks can have variable sizes
- Custom: User-defined chunk grid patterns via extensions
Behavior Callbacks
Implementations must provide:
init/1- Initialize chunk grid state from configurationchunk_shape/2- Get shape of a specific chunk by indexchunk_count/1- Calculate total number of chunksvalidate/2- Validate chunk grid configuration against array shape
Example
defmodule MyChunkGrid do
@behaviour ExZarr.ChunkGrid
defstruct [:chunk_size]
@impl true
def init(config) do
{:ok, %__MODULE__{chunk_size: config["chunk_size"]}}
end
@impl true
def chunk_shape(_chunk_index, state) do
{state.chunk_size, state.chunk_size}
end
@impl true
def chunk_count(state) do
# Calculate based on array shape
100
end
@impl true
def validate(config, array_shape) do
# Validate configuration
:ok
end
endSpecification
Zarr v3 Chunk Grids: https://zarr-specs.readthedocs.io/en/latest/v3/core/index.html#chunk-grids
Summary
Callbacks
Calculate the total number of chunks in the grid.
Get the shape of a specific chunk.
Initialize chunk grid state from configuration.
Validate chunk grid configuration against array shape.
Types
Callbacks
@callback chunk_count(state()) :: non_neg_integer()
Calculate the total number of chunks in the grid.
Parameters
state- Chunk grid state
Returns
- Non-negative integer representing total chunk count
@callback chunk_shape(chunk_index(), state()) :: chunk_shape()
Get the shape of a specific chunk.
Parameters
chunk_index- Tuple of chunk coordinates (e.g.,{0, 1, 2})state- Chunk grid state
Returns
chunk_shape- Tuple representing chunk dimensions
Initialize chunk grid state from configuration.
Parameters
config- Chunk grid configuration map
Returns
{:ok, state}- Initialized chunk grid state{:error, reason}- Initialization failure
@callback validate(config(), array_shape()) :: :ok | {:error, term()}
Validate chunk grid configuration against array shape.
Parameters
config- Chunk grid configuration maparray_shape- Shape of the array
Returns
:ok- Configuration is valid{:error, reason}- Validation failure
Functions
Parse and initialize a chunk grid from v3 metadata.
Dispatches to the appropriate chunk grid implementation based on the name field.
Parameters
chunk_grid_config- Map with"name"and"configuration"keys
Returns
{:ok, {module, state}}- Initialized chunk grid with module and state{:error, reason}- Parse or initialization failure
Examples
iex> config = %{
...> "name" => "regular",
...> "configuration" => %{"chunk_shape" => [10, 20, 30]}
...> }
iex> {:ok, {module, _state}} = ExZarr.ChunkGrid.parse(config)
iex> module
ExZarr.ChunkGrid.Regular
@spec validate(map(), array_shape()) :: :ok | {:error, term()}
Validate a chunk grid configuration.
Parameters
chunk_grid_config- Map with"name"and"configuration"keysarray_shape- Shape of the array
Returns
:ok- Configuration is valid{:error, reason}- Validation failure