stack

numcodecs_combinators.stack

This module defines the CodecStack class, which exposes a stack of codecs as a combined codec.

Classes:

  • CodecStack

    A stack of codecs, which makes up a combined codec.

CodecStack

CodecStack(*args: dict | Codec)

Bases: Codec, CodecCombinatorMixin, tuple[Codec]

A stack of codecs, which makes up a combined codec.

On encoding, the codecs are applied to encode from left to right, i.e.

CodecStack(a, b, c).encode(buf)
computes
c.encode(b.encode(a.encode(buf)))

On decoding, the codecs are applied to decode from right to left, i.e.

CodecStack(a, b, c).decode(buf)
computes
a.decode(b.decode(c.decode(buf)))

The CodecStack provides the additional encode_decode(buf) method that computes

stack.decode(stack.encode(buf))
but makes use of knowing the shapes and dtypes of all intermediary encoding stages.

Methods:

  • encode

    Encode the data in buf.

  • decode

    Decode the data in buf.

  • encode_decode

    Encode, then decode the data in buf.

  • encode_decode_data_array

    Encode, then decode the data array da. If da is chunked, each chunk

  • get_config

    Returns the configuration of the codec stack.

  • from_config

    Instantiate the codec stack from a configuration dict.

  • map

    Apply the mapper to all codecs that are in this stack.

codec_id class-attribute instance-attribute

codec_id: str = 'combinators.stack'

encode

encode(buf: Buffer) -> Buffer

Encode the data in buf.

Parameters:
  • buf (Buffer) –

    Data to be encoded. May be any object supporting the new-style buffer protocol.

Returns:
  • enc( Buffer ) –

    Encoded data. May be any object supporting the new-style buffer protocol.

decode

decode(buf: Buffer, out: Optional[Buffer] = None) -> Buffer

Decode the data in buf.

Parameters:
  • buf (Buffer) –

    Encoded data. May be any object supporting the new-style buffer protocol.

  • out (Buffer, default: None ) –

    Writeable buffer to store decoded data. N.B. if provided, this buffer must be exactly the right size to store the decoded data.

Returns:
  • dec( Buffer ) –

    Decoded data. May be any object supporting the new-style buffer protocol.

encode_decode

encode_decode(buf: Buffer) -> Buffer

Encode, then decode the data in buf.

Parameters:
  • buf (Buffer) –

    Data to be encoded. May be any object supporting the new-style buffer protocol.

Returns:
  • dec( Buffer ) –

    Decoded data. May be any object supporting the new-style buffer protocol.

encode_decode_data_array

encode_decode_data_array(da: DataArray) -> DataArray

Encode, then decode the data array da. If da is chunked, each chunk is encoded and decoded independently.

Since each chunk is encoded independently, this method may cause chunk boundary artifacts. Do not use this method if the codec requires access to the entire data at once or if it needs to access a neighbourhood of points across the chunk boundary. In these cases, it is preferable to use da.copy(data=stack.encode_decode(da.values)) instead.

The encode-decode computation may be deferred until the compute method is called on the result.

This method requires the optional xarray dependency to be installed.

Parameters:
Returns:

get_config

get_config() -> dict

Returns the configuration of the codec stack.

numcodecs.registry.get_codec(config) can be used to reconstruct this stack from the returned config.

Returns:
  • config( dict ) –

    Configuration of the codec stack.

from_config classmethod

from_config(config: dict) -> Self

Instantiate the codec stack from a configuration dict.

Parameters:
  • config (dict) –

    Configuration of the codec stack.

Returns:
  • stack( CodecStack ) –

    Instantiated codec stack.

map

map(mapper: Callable[[Codec], Codec]) -> CodecStack

Apply the mapper to all codecs that are in this stack. In the returned stack, each codec is replaced by its mapped codec.

The mapper should recursively apply itself to any inner codecs that also implement the CodecCombinatorMixin mixin.

To automatically handle the recursive application as a caller, you can use

numcodecs_combinators.map_codec(stack, mapper)
instead.

Parameters:
  • mapper (Callable[[Codec], Codec]) –

    The callable that should be applied to each codec to map over this codec stack.

Returns:
  • mapped( CodecStack ) –

    The mapped codec stack.