Skip to content

Commit

Permalink
Merge pull request #923 from girder/improve-errors
Browse files Browse the repository at this point in the history
Improve some error messages.
  • Loading branch information
manthey authored Aug 10, 2022
2 parents d60004b + b284879 commit e89ad59
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
8 changes: 4 additions & 4 deletions large_image/tilesource/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,10 +934,10 @@ def histogram(self, dtype=None, onlyMinMax=False, bins=256,
'count': tilecount
}
else:
results['min'] = numpy.minimum(results['min'], tilemin)
results['max'] = numpy.maximum(results['max'], tilemax)
results['sum'] += tilesum
results['sum2'] += tilesum2
results['min'] = numpy.minimum(results['min'], tilemin[:len(results['min'])])
results['max'] = numpy.maximum(results['max'], tilemax[:len(results['min'])])
results['sum'] += tilesum[:len(results['min'])]
results['sum2'] += tilesum2[:len(results['min'])]
results['count'] += tilecount
results['mean'] = results['sum'] / results['count']
results['stdev'] = numpy.maximum(
Expand Down
6 changes: 3 additions & 3 deletions sources/openjpeg/large_image_source_openjpeg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ def __init__(self, path, **kwargs):
if not self._openjpeg.shape:
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) as exc:
raise TileSourceError('File cannot be opened via Glymur and OpenJPEG: %r' % exc)
raise TileSourceError('File cannot be opened via Glymur and OpenJPEG (no shape).')
except (glymur.jp2box.InvalidJp2kError, struct.error):
raise TileSourceError('File cannot be opened via Glymur and OpenJPEG.')
except FileNotFoundError:
if not os.path.isfile(self._largeImagePath):
raise TileSourceFileNotFoundError(self._largeImagePath) from None
Expand Down
8 changes: 5 additions & 3 deletions sources/tiff/large_image_source_tiff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
from large_image.tilesource import FileTileSource, nearPowerOfTwo

from .tiff_reader import (InvalidOperationTiffException, IOTiffException,
TiffException, TiledTiffDirectory,
ValidationTiffException)
IOTiffOpenException, TiffException,
TiledTiffDirectory, ValidationTiffException)

try:
from importlib.metadata import PackageNotFoundError
Expand Down Expand Up @@ -104,6 +104,8 @@ def __init__(self, path, **kwargs):

try:
alldir = self._scanDirectories()
except IOTiffOpenException:
raise TileSourceError('File cannot be opened via tiff source.')
except (ValidationTiffException, TiffException) as exc:
alldir = []
lastException = exc
Expand Down Expand Up @@ -547,7 +549,7 @@ def getTile(self, x, y, z, pilImageAllowed=False, numpyAllowed=False,
frame = self._getFrame(**kwargs)
self._xyzInRange(x, y, z, frame, len(self._frames) if hasattr(self, '_frames') else None)
if frame > 0:
if self._frames[frame]['dirs'][z] is not None:
if hasattr(self, '_frames') and self._frames[frame]['dirs'][z] is not None:
dir = self._getDirFromCache(*self._frames[frame]['dirs'][z])
else:
dir = None
Expand Down
11 changes: 10 additions & 1 deletion sources/tiff/large_image_source_tiff/tiff_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ class IOTiffException(TiffException):
pass


class IOTiffOpenException(IOTiffException):
"""
An exception caused by an internal failure where the file cannot be opened
by the main library.
"""

pass


class ValidationTiffException(TiffException):
"""
An exception caused by the TIFF reader not being able to support a given
Expand Down Expand Up @@ -170,7 +179,7 @@ def _open(self, filePath, directoryNum, subDirectoryNum=0):
bytePath = filePath.encode()
self._tiffFile = libtiff_ctypes.TIFF.open(bytePath)
except TypeError:
raise IOTiffException(
raise IOTiffOpenException(
'Could not open TIFF file: %s' % filePath)
# pylibtiff changed the case of some functions between version 0.4 and
# the version that supports libtiff 4.0.6. To support both, ensure
Expand Down
2 changes: 2 additions & 0 deletions sources/tifffile/large_image_source_tifffile/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import math
import os
import threading
Expand Down Expand Up @@ -41,6 +42,7 @@ def _lazyImport():
if not hasattr(tifffile.TiffTag, 'dtype_name') or not hasattr(tifffile.TiffPage, 'aszarr'):
tifffile = None
raise TileSourceError('tifffile module is too old.')
logging.getLogger('tifffile.tifffile').setLevel(logging.ERROR)


def et_findall(tag, text):
Expand Down

0 comments on commit e89ad59

Please sign in to comment.