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

Harden reading with tifffile on images with bad axes information #1273

Merged
merged 1 commit into from
Aug 24, 2023
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 @@ -6,6 +6,7 @@
- Harden when the bioformats reader claims it has a zero sized image ([#1268](../../pull/1268))
- Harden internal retiling. This sometimes affected histograms ([#1269](../../pull/1269))
- Set annotation update user in client so it appears faster ([#1270](../../pull/1270))
- Harden reading with tifffile on images with bad axes information ([#1273](../../pull/1273))

### Changes
- Adjust tifffile log level ([#1265](../../pull/1265))
Expand Down
11 changes: 10 additions & 1 deletion sources/tifffile/large_image_source_tifffile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class TifffileFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
_minAssociatedImageSize = 64
_maxAssociatedImageSize = 8192

def __init__(self, path, **kwargs):
def __init__(self, path, **kwargs): # noqa
"""
Initialize the tile class. See the base class for other available
parameters.
Expand Down Expand Up @@ -146,6 +146,15 @@ def __init__(self, path, **kwargs):
getattr(self._tf, key)):
getattr(self, '_handle_' + key[3:])()
self._populatedLevels = len(self._baseSeries.levels)
# Some files have their axes listed in the wrong order. Try to access
# the lastmost pixel; if that fails, probably the axes and shape don't
# match the file (or the file is corrupted).
try:
self.getPixel(region={'left': self.sizeX - 1, 'top': self.sizeY - 1},
frame=self.frames - 1)
except Exception:
msg = 'File cannot be opened via tifffile: axes and shape do not match access pattern.'
raise TileSourceError(msg)

def _biggestSeries(self):
"""
Expand Down
2 changes: 2 additions & 0 deletions test/datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
'synthetic_qptiff.qptiff': 'sha512:652e61c650bcc57beeb2f89b8fa9ac4ba44ce5c0b48c5d3c6fb40ca539b015e36fe831ea5abd95d5650ec342de6a4207a9c22d410f5e2d8bfafbf19e2a6d5d96', # noqa
# Blank imageJ file with multiple channels
'synthetic_imagej.tiff': 'sha512:f4fcf9633b7fc8c819aace248352b3ef89364418d734aa8326096829d64956f11b961c24003bd5ca1f7e46462a8230b64177b37b1517a416d7c7123a0932d5dc', # noqa
# Blank untiled tiff with bad tifffile axes information
'bad_axes_description.tif': 'sha512:cbe82614a83d315a2f22c32b14fd32560fadb3f263bfeffc75f42f88e535e8a11728f691499cb7f5714bbbc55938c472ee4a706d7281bdfefe6fdb965c0a2d6b', # noqa
}


Expand Down
6 changes: 3 additions & 3 deletions test/test_source_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
'openjpeg': {'read': r'\.(jp2)$'},
'openslide': {
'read': r'\.(ptif|svs|tif.*|qptiff|dcm)$',
'noread': r'(oahu|DDX58_AXL|huron\.image2_jpeg2k|landcover_sample|d042-353\.crop|US_Geo\.|extraoverview|imagej)', # noqa
'noread': r'(oahu|DDX58_AXL|huron\.image2_jpeg2k|landcover_sample|d042-353\.crop|US_Geo\.|extraoverview|imagej|bad_axes)', # noqa
'skipTiles': r'one_layer_missing',
},
'pil': {
Expand All @@ -78,11 +78,11 @@
'read': r'\.(ptif|scn|svs|tif.*|qptiff)$',
'noread': r'(oahu|DDX58_AXL|G10-3_pelvis_crop|'
r'd042-353\.crop\.small\.float|landcover_sample|US_Geo\.|'
r'imagej)',
r'imagej|bad_axes)',
'skipTiles': r'(sample_image\.ptif|one_layer_missing_tiles)'},
'tifffile': {
'read': r'',
'noread': r'\.(nc|nd2|yml|yaml|json|czi|png|jpg|jpeg|jp2|dcm)$',
'noread': r'((\.(nc|nd2|yml|yaml|json|czi|png|jpg|jpeg|jp2|dcm)$)|bad_axes)',
'python': sys.version_info >= (3, 7),
},
'vips': {
Expand Down