ExZarr.ChunkGrid behaviour (ExZarr v1.1.0)

View Source

Behavior 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 configuration
  • chunk_shape/2 - Get shape of a specific chunk by index
  • chunk_count/1 - Calculate total number of chunks
  • validate/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
end

Specification

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.

Functions

Parse and initialize a chunk grid from v3 metadata.

Validate a chunk grid configuration.

Types

array_shape()

@type array_shape() :: tuple()

chunk_index()

@type chunk_index() :: tuple()

chunk_shape()

@type chunk_shape() :: tuple()

config()

@type config() :: map()

state()

@type state() :: struct()

Callbacks

chunk_count(state)

@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

chunk_shape(chunk_index, state)

@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

init(config)

@callback init(config()) :: {:ok, state()} | {:error, term()}

Initialize chunk grid state from configuration.

Parameters

  • config - Chunk grid configuration map

Returns

  • {:ok, state} - Initialized chunk grid state
  • {:error, reason} - Initialization failure

validate(config, array_shape)

@callback validate(config(), array_shape()) :: :ok | {:error, term()}

Validate chunk grid configuration against array shape.

Parameters

  • config - Chunk grid configuration map
  • array_shape - Shape of the array

Returns

  • :ok - Configuration is valid
  • {:error, reason} - Validation failure

Functions

parse(arg1)

@spec parse(map()) :: {:ok, {module(), state()}} | {:error, term()}

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

validate(arg1, array_shape)

@spec validate(map(), array_shape()) :: :ok | {:error, term()}

Validate a chunk grid configuration.

Parameters

  • chunk_grid_config - Map with "name" and "configuration" keys
  • array_shape - Shape of the array

Returns

  • :ok - Configuration is valid
  • {:error, reason} - Validation failure