Skip to content

Commit

Permalink
Merge pull request #105 from sameeul/fix_interleaved_ch_data
Browse files Browse the repository at this point in the history
bugfix: Reading multi-channel data in correct order
  • Loading branch information
sameeul authored Dec 31, 2024
2 parents c5a3a75 + 2504bcb commit 217133f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
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

0 comments on commit 217133f

Please sign in to comment.