soft-matter / pims_nd2

Python nd2 reader based on the ND2 SDK
Other
16 stars 8 forks source link

Iterate/concatenate multiple .nd2 files #25

Open steveb-123 opened 2 years ago

steveb-123 commented 2 years ago

I'd like to iterate over iterated axes of multiple .nd2 files as if they were one continuous flat iterable. I think the outcome would be something similar to ImageSequence from other readers.

Is this missing from this nd2 reader?

The following code illustrates the use-case, where I accumulate a list of ND2_reader objects. I later then iterate with nested for loops, but this feels silly and complicates merging external metadata, for example.

# List of files:
image_file_dir = "/run/media/steve/usb_msata/microscopy_images/"
image_filenames = (
    "WellA1-A3.nd2",
    "WellA4-A7.nd2",
    "WellA8-A12.nd2",
    "WellB1-C12.nd2",
    "WellD1-E12.nd2",
    "WellF1-H12.nd2"
    )

files = []
for i in image_filenames:
    image = ND2Reader_SDK(image_file_dir+i)
    image.bundle_axes = ['x', 'y', 'z', 'c'] 
    image.iter_axes = "m"  # iterate over the scene/field of view axis which is 'm'
    files.append(image)

One of files might look like

<FramesSequenceND>
Axes: 5
Axis 'x' size: 2046
Axis 'y' size: 2048
Axis 'c' size: 3
Axis 'm' size: 12
Axis 'z' size: 13
Pixel Datatype: <class 'numpy.uint16'>

As far as I can tell I'm unable to use any of the usual external array functions (concatenation, chaining, joining) to get to where I want because the objects provided by pims are incompatible (due to the lazy loading, I am guessing).

Disclaimer: I'm extremely new to pims and even python, so I'm likely to just be missing something super obvious. Apologies in advance if so.