Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ND2File.binary_data property - extract binary layers from file #108

Merged
merged 7 commits into from
Nov 1, 2022

Conversation

tlambert03
Copy link
Owner

@tlambert03 tlambert03 commented Oct 30, 2022

figured out how to decode the binary data in an nd2 file

Info below also present in the readme for anyone who stumbles on this PR:


The ND2File.binary_data property returns an nd2.BinaryLayers object representing all of the
binary masks in the nd2 file.

A nd2.BinaryLayers object is a sequence of individual nd2.BinaryLayer
objects (one for each binary layer found in the file). Each BinaryLayer in
the sequence is a named tuple that has, among other things, a name attribute,
and a data attribute that is list of numpy arrays (one for each frame in the
experiment) or None if the binary layer had no data in that frame.

The most common use case will be to cast either the entire BinaryLayers object
or an individual BinaryLayer to a numpy.ndarray:

>>> import nd2
>>> nd2file = nd2.ND2File('path/to/file.nd2')
>>> binary_layers = nd2file.binary_data

# The output array will have shape
# (n_binary_layers, *coord_shape, *frame_shape).
>>> np.asarray(binary_layers)

For example, if the data in the nd2 file has shape (nT, nZ, nC, nY, nX), and
there are 4 binary layers, then the output of np.asarray(nd2file.binary_data) will
have shape (4, nT, nZ, nY, nX). (Note that the nC dimension is not present
in the output array, and the binary layers are always in the first axis).

You can also cast an individual BinaryLayer to a numpy array:

>>> binary_layer = binary_layers[0]
>>> np.asarray(binary_layer)

@tlambert03 tlambert03 changed the title feat: working binary mask extraction prototype feat: Add ND2File.binary_data property - extract binary layers from file Nov 1, 2022
@tlambert03 tlambert03 changed the title feat: Add ND2File.binary_data property - extract binary layers from file feat: Add ND2File.binary_data property - extract binary layers from file Nov 1, 2022
@tlambert03 tlambert03 merged commit 9281882 into main Nov 1, 2022
@tlambert03 tlambert03 deleted the binary branch November 1, 2022 00:50
@tlambert03 tlambert03 added the enhancement New feature or request label Nov 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant