From b11bb886b0a19438afcc324580559770cc01ad8e Mon Sep 17 00:00:00 2001 From: Henry Pinkard <7969470+henrypinkard@users.noreply.github.com> Date: Tue, 18 Jun 2024 21:20:05 +0200 Subject: [PATCH] fix 8 bit bug --- python/ndstorage/_version.py | 2 +- python/ndstorage/ndtiff_file.py | 2 ++ python/ndstorage/test/writing_test.py | 28 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/python/ndstorage/_version.py b/python/ndstorage/_version.py index 6c43823..78f8d1b 100644 --- a/python/ndstorage/_version.py +++ b/python/ndstorage/_version.py @@ -1,2 +1,2 @@ -version_info = (0, 1, 5) +version_info = (0, 1, 6) __version__ = ".".join(map(str, version_info)) diff --git a/python/ndstorage/ndtiff_file.py b/python/ndstorage/ndtiff_file.py index 398f27a..08c0242 100644 --- a/python/ndstorage/ndtiff_file.py +++ b/python/ndstorage/ndtiff_file.py @@ -276,6 +276,8 @@ def _bytes_per_image_pixels(self, pixels, rgb): return len(pixels) elif isinstance(pixels, np.ndarray) and pixels.dtype == np.uint16: return pixels.size * 2 + elif isinstance(pixels, np.ndarray) and pixels.dtype == np.uint8: + return pixels.size else: raise RuntimeError("unknown pixel type") diff --git a/python/ndstorage/test/writing_test.py b/python/ndstorage/test/writing_test.py index 9dadc02..81b898c 100644 --- a/python/ndstorage/test/writing_test.py +++ b/python/ndstorage/test/writing_test.py @@ -63,6 +63,34 @@ def test_write_full_dataset(test_data_path): assert np.all(read_image == pixels) assert dataset.read_metadata(**axes) == {'time_metadata': time} +def test_write_full_dataset_8_bit(test_data_path): + """ + Write an NDTiff dataset and read it back in, testing pixels and metadata + """ + full_path = os.path.join(test_data_path, 'test_write_full_dataset') + dataset = NDTiffDataset(full_path, summary_metadata={}, writable=True) + + image_height = 256 + image_width = 256 + images = [] + for time in range(10): + pixels = np.ones(image_height * image_width, dtype=np.uint16).reshape((image_height, image_width)) * time + images.append(pixels) + for time in range(10): + axes = {'time': time} + dataset.put_image(axes, images[time], {'time_metadata': time}) + + dataset.finish() + + # read the file back in + dataset = NDTiffDataset(full_path) + for time in range(10): + pixels = np.ones(image_height * image_width, dtype=np.uint8).reshape((image_height, image_width)) * time + axes = {'time': time} + read_image = dataset.read_image(**axes) + assert np.all(read_image == pixels) + assert dataset.read_metadata(**axes) == {'time_metadata': time} + def test_write_full_dataset_RAM(): dataset = NDRAMDataset()