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

bugfix: Reading multi-channel data in correct order #105

Merged
merged 2 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ requires-python = ">=3.8"
[project.optional-dependencies]
dev = [
"ome_zarr",
"pillow",
"requests>=2.26.0"
]

Expand Down
12 changes: 1 addition & 11 deletions src/bfio/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,17 +988,7 @@ def read_metadata(self):

def _read_image(self, X, Y, Z, C, T, output):
out = self._image
pseudo_interleaved = any(
channel.samples_per_pixel > 1
for channel in self.frontend.metadata.images[0].pixels.channels
)

interleaved = False
if (
self.frontend.metadata.images[0].pixels.interleaved
or pseudo_interleaved
):
interleaved = True
interleaved = self.frontend.metadata.images[0].pixels.interleaved

self._prev_read_cached_loc = None
self._cached_read_data = None
Expand Down
36 changes: 36 additions & 0 deletions tests/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import numpy as np
import pickle
import random
import tifffile
from PIL import Image
import zarr
from ome_zarr.utils import download as zarr_download

Expand Down Expand Up @@ -457,3 +459,37 @@ def test_tensorstore_backend_pickle(self):
assert unpickled_rdr.Y == img_height
assert unpickled_rdr[:].sum() == data_sum
unpickled_rdr.close()


class TestMultiChannelBioformats(unittest.TestCase):

def test_multi_channel_bioformats(self):

# Set image dimensions
width, height = 256, 256

# Create random 3-channel image data
random_image_data = np.random.randint(
0, 256, (height, width, 3), dtype=np.uint8
)

# Save interleaved TIFF (RGBRGB...)
interleaved_image = Image.fromarray(random_image_data, mode="RGB")
interleaved_image.save(TEST_DIR.joinpath("interleaved.tiff"))

# Create non-interleaved data (separate planes for R, G, B)
non_interleaved_data = np.moveaxis(
random_image_data, 2, 0
) # Shape: (3, height, width)

# Save non-interleaved TIFF
with tifffile.TiffWriter(TEST_DIR.joinpath("non_interleaved.tiff")) as tif:
tif.write(non_interleaved_data, photometric="rgb")

with bfio.BioReader(TEST_DIR.joinpath("non_interleaved.tiff")) as br:
for c in range(br.C):
assert (br[:, :, :, c] - random_image_data[:, :, c]).sum() == 0

with bfio.BioReader(TEST_DIR.joinpath("interleaved.tiff")) as br:
for c in range(br.C):
assert (br[:, :, :, c] - non_interleaved_data[c, :, :]).sum() == 0
Loading