Skip to content

Commit

Permalink
Improve error message on various sources.
Browse files Browse the repository at this point in the history
  • Loading branch information
manthey committed Aug 9, 2022
1 parent 7b5142b commit 445ca94
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
6 changes: 5 additions & 1 deletion sources/bioformats/large_image_source_bioformats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,14 @@ def __init__(self, path, **kwargs): # noqa

if self.levels < 1:
raise TileSourceError(
'OpenSlide image must have at least one level.')
'Bioformats image must have at least one level.')

if self.sizeX <= 0 or self.sizeY <= 0:
raise TileSourceError('Bioformats tile size is invalid.')
try:
self.getTile(0, 0, self.levels - 1)
except Exception as exc:
raise TileSourceError('Bioformats cannot read a tile: %r' % exc)

def __del__(self):
if getattr(self, '_bioimage', None) is not None:
Expand Down
2 changes: 1 addition & 1 deletion sources/deepzoom/large_image_source_deepzoom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self, path, **kwargs):
xml = ElementTree.parse(self._largeImagePath).getroot()
self._info = etreeToDict(xml)['Image']
except (ElementTree.ParseError, KeyError, UnicodeDecodeError):
raise TileSourceError('File cannot be opened via Deepzoom reader.')
raise TileSourceError('File cannot be opened via deepzoom reader.')
except FileNotFoundError:
if not os.path.isfile(self._largeImagePath):
raise TileSourceFileNotFoundError(self._largeImagePath) from None
Expand Down
8 changes: 4 additions & 4 deletions sources/gdal/large_image_source_gdal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ def __init__(self, path, projection=None, unitsPerPixel=None, **kwargs):
with self._getDatasetLock:
self.sourceSizeX = self.sizeX = self.dataset.RasterXSize
self.sourceSizeY = self.sizeY = self.dataset.RasterYSize
except AttributeError:
except AttributeError as exc:
if not os.path.isfile(self._largeImagePath):
raise TileSourceFileNotFoundError(self._largeImagePath) from None
raise TileSourceError('File cannot be opened via GDAL.')
raise TileSourceError('File cannot be opened via GDAL: %r' % exc)
is_netcdf = self._checkNetCDF()
try:
scale = self.getPixelSizeInMeters()
except RuntimeError:
raise TileSourceError('File cannot be opened via GDAL.')
except RuntimeError as exc:
raise TileSourceError('File cannot be opened via GDAL: %r' % exc)
if (self.projection or self._getDriver() in {
'PNG',
}) and not scale and not is_netcdf:
Expand Down
8 changes: 4 additions & 4 deletions sources/openjpeg/large_image_source_openjpeg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def __init__(self, path, **kwargs):
if not os.path.isfile(self._largeImagePath):
raise FileNotFoundError()
raise TileSourceError('File cannot be opened via Glymur and OpenJPEG.')
except (glymur.jp2box.InvalidJp2kError, struct.error):
raise TileSourceError('File cannot be opened via Glymur and OpenJPEG.')
except (glymur.jp2box.InvalidJp2kError, struct.error) as exc:
raise TileSourceError('File cannot be opened via Glymur and OpenJPEG: %r' % exc)
except FileNotFoundError:
if not os.path.isfile(self._largeImagePath):
raise TileSourceFileNotFoundError(self._largeImagePath) from None
Expand All @@ -112,8 +112,8 @@ def __init__(self, path, **kwargs):
self._openjpegHandles.put(self._openjpeg)
try:
self.sizeY, self.sizeX = self._openjpeg.shape[:2]
except IndexError:
raise TileSourceError('File cannot be opened via Glymur and OpenJPEG.')
except IndexError as exc:
raise TileSourceError('File cannot be opened via Glymur and OpenJPEG: %r' % exc)
self.levels = int(self._openjpeg.codestream.segment[2].num_res) + 1
self._minlevel = 0
self.tileWidth = self.tileHeight = 2 ** int(math.ceil(max(
Expand Down
17 changes: 10 additions & 7 deletions sources/tifffile/large_image_source_tifffile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def _biggestSeries(self):
"""
maxseries = None
maxsamples = 0
ex = 'no maximum series'
try:
for idx, s in enumerate(self._tf.series):
samples = numpy.prod(s.shape)
Expand All @@ -145,9 +146,11 @@ def _biggestSeries(self):
maxsamples = samples
except Exception as exc:
self.logger.debug('Cannot use tifffile: %r', exc)
ex = exc
maxseries = None
if maxseries is None:
raise TileSourceError('File cannot be opened via tifffile source.')
raise TileSourceError(
'File cannot be opened via tifffile source: %r' % ex)
return maxseries, maxsamples

def _findMatchingSeries(self):
Expand Down Expand Up @@ -205,7 +208,7 @@ def _findAssociatedImages(self):
"""
Find associated images from unused pages and series.
"""
pagesInSeries = [p for s in self._tf.series for l in s.pages.levels for p in l.pages]
pagesInSeries = [p for s in self._tf.series for ll in s.pages.levels for p in ll.pages]
self._associatedImages = {}
for p in self._tf.pages:
if (p not in pagesInSeries and p.keyframe is not None and
Expand Down Expand Up @@ -330,7 +333,7 @@ def getInternalMetadata(self, **kwargs):
"""
result = {}
pages = [s.pages[0] for s in self._tf.series]
pagesInSeries = [p for s in self._tf.series for l in s.pages.levels for p in l.pages]
pagesInSeries = [p for s in self._tf.series for ll in s.pages.levels for p in ll.pages]
pages.extend([page for page in self._tf.pages if page not in pagesInSeries])
for page in pages:
for tag in getattr(page, 'tags', []):
Expand Down Expand Up @@ -402,11 +405,11 @@ def getTile(self, x, y, z, pilImageAllowed=False, numpyAllowed=False, **kwargs):
if hasattr(za[0], 'get_basic_selection'):
bza = za[0]
# we could cache this
for l in range(len(series.levels) - 1, 0, -1):
scale = round(max(za[0].shape[xidx] / za[l].shape[xidx],
za[0].shape[yidx] / za[l].shape[yidx]))
for ll in range(len(series.levels) - 1, 0, -1):
scale = round(max(za[0].shape[xidx] / za[ll].shape[xidx],
za[0].shape[yidx] / za[ll].shape[yidx]))
if scale <= step and step // scale == step / scale:
bza = za[l]
bza = za[ll]
x0 //= scale
x1 //= scale
y0 //= scale
Expand Down
3 changes: 2 additions & 1 deletion test/test_source_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
SourceAndFiles = {
'bioformats': {
'read': r'\.(czi|jp2|svs|scn)$',
'noread': r'(JK-kidney_B|TCGA-AA-A02O|\.scn$)',
# We need to modify the bioformats reader similar to tiff's
# getTileFromEmptyDirectory
'skipTiles': r'(JK-kidney_B|TCGA-AA-A02O|TCGA-DU-6399|sample_jp2k_33003|\.scn$)'},
'skipTiles': r'(TCGA-DU-6399|sample_jp2k_33003)'},
'deepzoom': {},
'dummy': {'any': True, 'skipTiles': r''},
'gdal': {
Expand Down

0 comments on commit 445ca94

Please sign in to comment.