From 7f61bf8f0388b8e423c28a7f3e67e1f5d2a1d0d3 Mon Sep 17 00:00:00 2001 From: David Manthey Date: Thu, 31 Oct 2024 11:55:32 -0400 Subject: [PATCH] Ensure tile source _dtype is an actual numpy dtype --- CHANGELOG.md | 1 + large_image/tilesource/base.py | 6 +++--- sources/multi/large_image_source_multi/__init__.py | 3 ++- sources/test/large_image_source_test/__init__.py | 2 +- sources/zarr/large_image_source_zarr/__init__.py | 4 ++-- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24301b553..626a40a46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Improvements - Format dates in item lists ([#1707](../../pull/1707)) +- Guard dtype types ([#1710](../../pull/1710)) ### Changes diff --git a/large_image/tilesource/base.py b/large_image/tilesource/base.py index 17b46bb56..17dd069ce 100644 --- a/large_image/tilesource/base.py +++ b/large_image/tilesource/base.py @@ -290,7 +290,7 @@ def dtype(self) -> np.dtype: self, '_unstyledInstance', self).getRegion( region=dict(left=0, top=0, width=1, height=1), format=TILE_FORMAT_NUMPY)) - self._dtype = sample.dtype + self._dtype = np.dtype(sample.dtype) self._bandCount = len( getattr(getattr(self, '_unstyledInstance', self), '_bandInfo', [])) if not self._bandCount: @@ -1190,14 +1190,14 @@ def _outputTile( if self._dtype is None or (isinstance(self._dtype, str) and self._dtype == 'check'): if isinstance(tile, np.ndarray): - self._dtype = tile.dtype + self._dtype = np.dtype(tile.dtype) self._bandCount = tile.shape[-1] if len(tile.shape) == 3 else 1 elif isinstance(tile, PIL.Image.Image): self._dtype = np.uint8 if ';16' not in tile.mode else np.uint16 self._bandCount = len(tile.mode) else: _img = _imageToNumpy(tile)[0] - self._dtype = _img.dtype + self._dtype = np.dtype(_img.dtype) self._bandCount = _img.shape[-1] if len(_img.shape) == 3 else 1 mode = None diff --git a/sources/multi/large_image_source_multi/__init__.py b/sources/multi/large_image_source_multi/__init__.py index c6838d574..64fc977bd 100644 --- a/sources/multi/large_image_source_multi/__init__.py +++ b/sources/multi/large_image_source_multi/__init__.py @@ -897,7 +897,8 @@ def _openSource(self, source, params=None): if params is None: params = source.get('params', {}) ts = openFunc(source['path'], **params) - if (self._dtype and np.dtype(ts.dtype).kind == 'f' and self._dtype.kind != 'f' and + if (self._dtype and np.dtype(ts.dtype).kind == 'f' and + np.dtype(self._dtype.kind) != 'f' and 'sampleScale' not in source and 'sampleOffset' not in source): minval = maxval = 0 for f in range(ts.frames): diff --git a/sources/test/large_image_source_test/__init__.py b/sources/test/large_image_source_test/__init__.py index 036a8d91e..89c9f6b91 100644 --- a/sources/test/large_image_source_test/__init__.py +++ b/sources/test/large_image_source_test/__init__.py @@ -128,7 +128,7 @@ def __init__(self, ignored_path=None, minLevel=0, maxLevel=9, } for idx, band in enumerate(bands)} if low < 0 or high < 2 or low >= 65536 or high >= 65536: - self._dtype = float + self._dtype = np.dtype(float) elif low >= 256 or high >= 256: self._dtype = np.uint16 # Used for reporting tile information diff --git a/sources/zarr/large_image_source_zarr/__init__.py b/sources/zarr/large_image_source_zarr/__init__.py index 92df8acd1..b99b423be 100644 --- a/sources/zarr/large_image_source_zarr/__init__.py +++ b/sources/zarr/large_image_source_zarr/__init__.py @@ -433,7 +433,7 @@ def _validateZarr(self): # self.tileWidth = self.tileHeight = baseArray.chunks[self._axes['x']] self.levels = int(max(1, math.ceil(math.log(max( self.sizeX / self.tileWidth, self.sizeY / self.tileHeight)) / math.log(2)) + 1)) - self._dtype = baseArray.dtype + self._dtype = np.dtype(baseArray.dtype) self._bandCount = 1 if ('c' in self._axes and 's' not in self._axes and not self._channels and baseArray.shape[self._axes.get('c')] in {1, 3, 4}): @@ -754,7 +754,7 @@ def addTile(self, tile, x=0, y=0, mask=None, axes=None, **kwargs): # If base data changed, update large_image attributes if store_path == '0': - self._dtype = tile.dtype + self._dtype = np.dtype(tile.dtype) self._bandCount = new_dims.get(axes[-1]) # last axis is assumed to be bands self.sizeX = new_dims.get('x') self.sizeY = new_dims.get('y')