neuroglancer_scripts.chunk_encoding module

Encoding / decoding of Neuroglancer precomputed chunks.

The central component here is the ChunkEncoder base class. Use get_encoder() for instantiating a concrete encoder object.

class neuroglancer_scripts.chunk_encoding.ChunkEncoder(data_type, num_channels)[source]

Bases: object

Encode/decode chunks from NumPy arrays to byte buffers.

Parameters:
  • data_type (str) – data type supported by Neuroglancer

  • num_channels (int) – number of image channels

decode(buf, chunk_size)[source]

Decode a chunk from bytes into a NumPy array.

Parameters:
  • buf (bytes) – encoded chunk

  • chunk_size (tuple) – the 3-D size of the chunk (X, Y, Z)

Returns:

chunk contained in a 4-D NumPy array (C, Z, Y, X)

Return type:

numpy.ndarray

Raises:

InvalidFormatError – if there the chunk data cannot be decoded properly

encode(chunk)[source]

Encode a chunk from a NumPy array into bytes.

Parameters:

chunk (numpy.ndarray) – array with four dimensions (C, Z, Y, X)

Returns:

encoded chunk

Return type:

bytes

lossy = False

True if this encoder is lossy.

mime_type = 'application/octet-stream'

MIME type of the encoded chunk.

class neuroglancer_scripts.chunk_encoding.CompressedSegmentationEncoder(data_type, num_channels, block_size)[source]

Bases: ChunkEncoder

Codec for to the Neuroglancer precomputed chunk format.

Parameters:
  • data_type (str) – data type supported by Neuroglancer

  • num_channels (int) – number of image channels

  • block_size (list) – block_size for the compressed segmentation compression algorithm

Raises:

IncompatibleEncoderError – if data_type or num_channels are unsupported

decode(buf, chunk_size)[source]

Decode a chunk from bytes into a NumPy array.

Parameters:
  • buf (bytes) – encoded chunk

  • chunk_size (tuple) – the 3-D size of the chunk (X, Y, Z)

Returns:

chunk contained in a 4-D NumPy array (C, Z, Y, X)

Return type:

numpy.ndarray

Raises:

InvalidFormatError – if there the chunk data cannot be decoded properly

encode(chunk)[source]

Encode a chunk from a NumPy array into bytes.

Parameters:

chunk (numpy.ndarray) – array with four dimensions (C, Z, Y, X)

Returns:

encoded chunk

Return type:

bytes

lossy = False

True if this encoder is lossy.

exception neuroglancer_scripts.chunk_encoding.IncompatibleEncoderError[source]

Bases: Exception

Raised when an Encoder cannot handle the requested data type.

exception neuroglancer_scripts.chunk_encoding.InvalidFormatError[source]

Bases: Exception

Raised when chunk data cannot be decoded properly.

exception neuroglancer_scripts.chunk_encoding.InvalidInfoError[source]

Bases: Exception

Raised when an info dict is invalid or inconsistent.

class neuroglancer_scripts.chunk_encoding.JpegChunkEncoder(data_type, num_channels, jpeg_quality=95, jpeg_plane='xy')[source]

Bases: ChunkEncoder

Codec for to the Neuroglancer raw chunk format.

Parameters:
  • data_type (str) – data type supported by Neuroglancer

  • num_channels (int) – number of image channels

  • jpeg_quality (int) – quality factor for JPEG compression

  • jpeg_plane (str) – plane of JPEG compression ("xy" or "xz")

Raises:

IncompatibleEncoderError – if data_type or num_channels are unsupported

decode(buf, chunk_size)[source]

Decode a chunk from bytes into a NumPy array.

Parameters:
  • buf (bytes) – encoded chunk

  • chunk_size (tuple) – the 3-D size of the chunk (X, Y, Z)

Returns:

chunk contained in a 4-D NumPy array (C, Z, Y, X)

Return type:

numpy.ndarray

Raises:

InvalidFormatError – if there the chunk data cannot be decoded properly

encode(chunk)[source]

Encode a chunk from a NumPy array into bytes.

Parameters:

chunk (numpy.ndarray) – array with four dimensions (C, Z, Y, X)

Returns:

encoded chunk

Return type:

bytes

lossy = True

True if this encoder is lossy.

mime_type = 'image/jpeg'

MIME type of the encoded chunk.

class neuroglancer_scripts.chunk_encoding.RawChunkEncoder(data_type, num_channels)[source]

Bases: ChunkEncoder

Codec for to the Neuroglancer raw chunk format.

Parameters:
  • data_type (str) – data type supported by Neuroglancer

  • num_channels (int) – number of image channels

decode(buf, chunk_size)[source]

Decode a chunk from bytes into a NumPy array.

Parameters:
  • buf (bytes) – encoded chunk

  • chunk_size (tuple) – the 3-D size of the chunk (X, Y, Z)

Returns:

chunk contained in a 4-D NumPy array (C, Z, Y, X)

Return type:

numpy.ndarray

Raises:

InvalidFormatError – if there the chunk data cannot be decoded properly

encode(chunk)[source]

Encode a chunk from a NumPy array into bytes.

Parameters:

chunk (numpy.ndarray) – array with four dimensions (C, Z, Y, X)

Returns:

encoded chunk

Return type:

bytes

lossy = False

True if this encoder is lossy.

neuroglancer_scripts.chunk_encoding.add_argparse_options(parser, allow_lossy=False)[source]

Add command-line options for chunk encoding.

Parameters:

The extrinsic encoder parameters can be obtained from command-line arguments with add_argparse_options() and passed to get_encoder():

import argparse
parser = argparse.ArgumentParser()
add_argparse_options(parser)
args = parser.parse_args()
get_encoder(info, scale_info, vars(args))
neuroglancer_scripts.chunk_encoding.get_encoder(info, scale_info, encoder_options={})[source]

Create an Encoder object for the provided scale.

Parameters:
  • info (dict) – a Neuroglancer info dictionary (The Neuroglancer info file) containing general encoding parameters (data_type and num_channels)

  • scale_info (dict) – an element of (info["scales"]) containing scale-specific encoding parameters (encoding and encoding-specific parameters)

  • encoder_options (dict) – extrinsic encoder parameters

Returns:

an instance of a chunk encoder

Return type:

ChunkEncoder

Raises:

InvalidInfoError – if the provided info dict is invalid