Skip to content

Commit 05bf7cb

Browse files
committed
- added a flag discard_extra
- added a test
1 parent 941b5dd commit 05bf7cb

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

test/test_images_io.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ def test_from_tif_multi_planes(eng):
114114
assert [x.sum() for x in data.toarray()] == [1140006, 1119161, 1098917]
115115

116116

117+
def test_from_tif_multi_planes_discard_extra(eng):
118+
path = os.path.join(resources, 'multilayer_tif', 'dotdotdot_lzw.tif')
119+
data = fromtif(path, nplanes=2, engine=eng, discard_extra=True)
120+
assert data.shape[0] == 1
121+
assert data.shape[1] == 2
122+
with pytest.raises(BaseException) as error_msg:
123+
data = fromtif(path, nplanes=2, engine=eng, discard_extra=False)
124+
assert 'nplanes' in str(error_msg.value)
125+
126+
117127
def test_from_tif_multi_planes_many(eng):
118128
path = os.path.join(resources, 'multilayer_tif', 'dotdotdot_lzw*.tif')
119129
data = fromtif(path, nplanes=3, engine=eng)

thunder/images/readers.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import itertools
2-
import warnings
2+
import logging
33
from io import BytesIO
44
from numpy import frombuffer, prod, random, asarray, expand_dims
55

@@ -316,7 +316,7 @@ def getarray(idx_buffer_filename):
316316
dims=newdims, dtype=dtype, labels=labels, recount=recount,
317317
engine=engine, credentials=credentials)
318318

319-
def fromtif(path, ext='tif', start=None, stop=None, recursive=False, nplanes=None, npartitions=None, labels=None, engine=None, credentials=None):
319+
def fromtif(path, ext='tif', start=None, stop=None, recursive=False, nplanes=None, npartitions=None, labels=None, engine=None, credentials=None, discard_extra=False):
320320
"""
321321
Loads images from single or multi-page TIF files.
322322
@@ -347,6 +347,10 @@ def fromtif(path, ext='tif', start=None, stop=None, recursive=False, nplanes=Non
347347
348348
labels : array, optional, default = None
349349
Labels for records. If provided, should be one-dimensional.
350+
351+
discard_extra : boolean, optional, default = False
352+
If True and nplanes doesn't divide by the number of pages in a multi-page tiff, the reminder will
353+
be discarded and a warning will be shown. If False, it will raise an error
350354
"""
351355
import skimage.external.tifffile as tifffile
352356

@@ -362,8 +366,11 @@ def getarray(idx_buffer_filename):
362366
if nplanes is not None:
363367
extra = pageCount % nplanes
364368
if extra:
365-
pageCount = pageCount - extra
366-
warnings.warn('Ignored %d pages in file %s' % (extra, fname), RuntimeWarning)
369+
if discard_extra:
370+
pageCount = pageCount - extra
371+
logging.getLogger('thunder').warn('Ignored %d pages in file %s' % (extra, fname))
372+
else:
373+
raise ValueError("nplanes '%d' does not evenly divide '%d'" % (nplanes, pageCount))
367374
values = [ary[i:(i+nplanes)] for i in range(0, pageCount, nplanes)]
368375
else:
369376
values = [ary]

0 commit comments

Comments
 (0)