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 tifffile source #903

Merged
merged 1 commit into from
Aug 1, 2022
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
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