blosc2.register_codec#

blosc2.register_codec(codec_name, id, encoder=None, decoder=None, version=1)#

Register a user defined codec.

Parameters:
  • codec_name (str) – Name of the codec.

  • id (int) – Codec id, must be between 160 and 255 (both included).

  • encoder (Python function or None) – This will receive an input to compress as a ndarray of dtype uint8, an output to fill the compressed buffer in as a ndarray of dtype uint8, the codec meta and the SChunk instance. It must return the size of the compressed buffer in bytes. If None then the codec name indicates a dynamic plugin which must be installed.

  • decoder (Python function or None) – This will receive an input to decompress as a ndarray of dtype uint8, an output to fill the decompressed buffer in as a ndarray of dtype uint8, the codec meta and the SChunk instance. It must return the size of the decompressed buffer in bytes. If None then the codec name indicates a dynamic plugin which must be installed.

  • version (int) – Codec version. Default is 1.

Returns:

out

Return type:

None

Notes

  • Cannot use multi-threading when using an user defined codec.

  • User defined codecs can only be used inside a SChunk instance.

  • Both encoder and encoder functions must be given (for a Python codec), or none (for a dynamic plugin).

Examples

# Define encoder and decoder functions
def encoder(input, output, meta, schunk):
    # Check whether the data is an arange
    step = int(input[1] - input[0])
    res = input[1:] - input[:-1]
    if np.min(res) == np.max(res):
        output[0:4] = input[0:4]  # start
        n = step.to_bytes(4, sys.byteorder)
        output[4:8] = [n[i] for i in range(4)]
        return 8
    else:
        # Not compressible, tell Blosc2 to do a memcpy
        return 0

def decoder1(input, output, meta, schunk):
    # For decoding we only have to worry about the arange case
    # (other cases are handled by Blosc2)
    output[:] = [input[0] + i * input[1] for i in range(output.size)]

    return output.size

# Register codec
codec_name = 'codec1'
id = 180
blosc2.register_codec(codec_name, id, encoder, decoder)