best

numcodecs_combinators.best

This module defines the PickBestCodec class, which picks the codec that encoded the data best.

Classes:

  • PickBestCodec

    A codec that tries encoding with all combined codecs and then picks the one with the fewest bytes.

PickBestCodec

PickBestCodec(*args: dict | Codec)

Bases: Codec, CodecCombinatorMixin, tuple[Codec]

A codec that tries encoding with all combined codecs and then picks the one with the fewest bytes.

The inner codecs must all encode to 1D byte arrays. To use a codec not encoding to bytes with this combinator, you can wrap it using FramedCodecStack(codec) combinator.

This combinator uses the ULEB128 variable length integer encoding to encode the index of the codec that was chosen to encode and uses this index as a header before the encoded bytes. The header index is only included if this combinator wraps at least two codecs. If this combinator wraps zero codecs, it passes the original data through unchanged.

Methods:

  • encode

    Encode the data in buf.

  • decode

    Decode the data in buf.

  • get_config

    Returns the configuration of the best codec combinator.

  • from_config

    Instantiate the best codec combinator from a configuration dict.

  • map

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

codec_id class-attribute instance-attribute

codec_id: str = 'combinators.best'

encode

encode(buf: Buffer) -> bytes

Encode the data in buf.

Parameters:
  • buf (Buffer) –

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

Returns:
  • enc( bytes ) –

    Encoded and data as a bytestring.

decode

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

Decode the data in buf.

Parameters:
  • buf (Buffer) –

    Encoded data. Must be an object representing a bytestring, e.g. bytes or a 1D array of np.uint8s etc.

  • 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.

get_config

get_config() -> dict

Returns the configuration of the best codec combinator.

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

Returns:
  • config( dict ) –

    Configuration of the best codec combinator.

from_config classmethod

from_config(config: dict) -> Self

Instantiate the best codec combinator from a configuration dict.

Parameters:
  • config (dict) –

    Configuration of the best codec combinator.

Returns:

map

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

Apply the mapper to all codecs that are in this combinator. In the returned combinator, 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(best, mapper)
instead.

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

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

Returns: