Skip to content

nd2 (binary masks)

nd2: A Python library for reading and writing ND2 files.

BinaryLayers

Sequence of Binary Layers found in an ND2 file.

This object is a sequence of BinaryLayer objects, one for each binary layer in the file. Each layer has a name attribute, and a data attribute that is list of numpy arrays - one for each frame in the experiment - or None if the layer was not present in that frame.

The wrapper can be cast to a numpy array (with BinaryLayers.asarray() or np.asarray(BinaryLayers)) to stack all the layers into a single array. The output array will have shape (n_layers, *coord_shape, *frame_shape).

asarray() -> np.ndarray

Stack all the layers/frames into a single array.

The output array will have shape (n_layers, coord_shape, frame_shape).

BinaryLayer dataclass

Wrapper for data from a single binary layer in an nd2.ND2File.

A "layer" is a set of binary data that can be associated with a specific component in an ND2 file, such as a single channel.

This object behaves like a list[numpy.ndarray] | None. It will have a length matching the number of frames in the file, with None for any frames that lack binary data.

Attributes:

  • data (list[ndarray] | None) –

    The data for each frame. If a frame has no binary data, the value will be None. Data will have the same length as the number of sequences in the file.

  • name (str) –

    The name of the binary layer.

  • comp_name (str) –

    The name of the associated component, if Any.

  • comp_order (int) –

    The order of the associated component, if Any.

  • color (int) –

    The color of the binary layer.

  • color_mode (int) –

    The color mode of the binary layer. I believe this is related to how colors are chosen in NIS-Elements software. Where "0" is direct color (i.e. use, the color value), "8" is color by 3D ... and I'm not sure about the rest :)

  • state (int) –

    The state of the binary layer. (meaning still unclear)

  • file_tag (str) –

    The key for the binary layer in the CustomData metadata, e.g. RleZipBinarySequence_1_v1

  • layer_id (int) –

    The ID of the binary layer.

  • coordinate_shape (tuple[int, ...]) –

    The shape of the coordinates for the associated nd2 file. This is used to reshape the data into a 3D array in asarray.

frame_shape: tuple[int, ...] property

Shape (Y, X) of each mask in data.

asarray() -> np.ndarray | None

Stack all the frames into a single array.

If there are no frames, returns None.