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

Ensure tile source _dtype is an actual numpy dtype #1711

Merged
merged 1 commit into from
Oct 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Improvements

- Format dates in item lists ([#1707](../../pull/1707))
- Guard dtype types ([#1710](../../pull/1710))

### Changes

Expand Down
6 changes: 3 additions & 3 deletions large_image/tilesource/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion sources/multi/large_image_source_multi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion sources/test/large_image_source_test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions sources/zarr/large_image_source_zarr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}):
Expand Down Expand Up @@ -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')
Expand Down