Skip to content

Commit

Permalink
Merge pull request #903 from girder/harden-tifffile
Browse files Browse the repository at this point in the history
Harden tifffile source
  • Loading branch information
manthey authored Aug 1, 2022
2 parents 2b9402a + 0996e61 commit 8f14861
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
## 1.16.0

### Features
- Add a tifffile tile source ([885](../../pull/885), [896](../../pull/896))
- Add a tifffile tile source ([885](../../pull/885), [896](../../pull/896), [903](../../pull/903))
- Added a canReadList method to large_image to show which source can be used ([895](../../pull/895))
- Optionally show metadata in item lists ([901](../../pull/901))

### Improvements
- Pass options to the annotationLayer mode ([881](../../pull/881))
Expand Down
35 changes: 24 additions & 11 deletions sources/tifffile/large_image_source_tifffile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,7 @@ def __init__(self, path, **kwargs):
if not os.path.isfile(self._largeImagePath):
raise TileSourceFileNotFoundError(self._largeImagePath) from None
raise TileSourceError('File cannot be opened via tifffile.')
# Find the series with the most pixels. Use all series that have the
# same dimensionality and resolution. They can differ in X, Y size.
maxseries = None
maxsamples = 0
for idx, s in enumerate(self._tf.series):
samples = numpy.prod(s.shape)
if samples > maxsamples and 'X' in s.axes and 'Y' in s.axes:
maxseries = idx
maxsamples = samples
if maxseries is None:
raise TileSourceError('File cannot be opened via tifffile source.')
maxseries, maxsamples = self._biggestSeries()
self.tileWidth = self.tileHeight = self._tileSize
s = self._tf.series[maxseries]
self._baseSeries = s
Expand Down Expand Up @@ -136,6 +126,29 @@ def __init__(self, path, **kwargs):
getattr(self._tf, key)):
getattr(self, '_handle_' + key[3:])()

def _biggestSeries(self):
"""
Find the series with the most pixels. Use all series that have the
same dimensionality and resolution. They can differ in X, Y size.
:returns: index of the largest series, number of pixels in a frame in
that series.
"""
maxseries = None
maxsamples = 0
try:
for idx, s in enumerate(self._tf.series):
samples = numpy.prod(s.shape)
if samples > maxsamples and 'X' in s.axes and 'Y' in s.axes:
maxseries = idx
maxsamples = samples
except Exception as exc:
self.logger.debug('Cannot use tifffile: %r', exc)
maxseries = None
if maxseries is None:
raise TileSourceError('File cannot be opened via tifffile source.')
return maxseries, maxsamples

def _findMatchingSeries(self):
"""
Given a series in self._baseSeries, find other series that have the
Expand Down

0 comments on commit 8f14861

Please sign in to comment.