Skip to content

Commit

Permalink
- added a flag discard_extra
Browse files Browse the repository at this point in the history
- added a test
  • Loading branch information
boazmohar committed May 23, 2016
1 parent 941b5dd commit 05bf7cb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
10 changes: 10 additions & 0 deletions test/test_images_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ def test_from_tif_multi_planes(eng):
assert [x.sum() for x in data.toarray()] == [1140006, 1119161, 1098917]


def test_from_tif_multi_planes_discard_extra(eng):
path = os.path.join(resources, 'multilayer_tif', 'dotdotdot_lzw.tif')
data = fromtif(path, nplanes=2, engine=eng, discard_extra=True)
assert data.shape[0] == 1
assert data.shape[1] == 2
with pytest.raises(BaseException) as error_msg:
data = fromtif(path, nplanes=2, engine=eng, discard_extra=False)
assert 'nplanes' in str(error_msg.value)


def test_from_tif_multi_planes_many(eng):
path = os.path.join(resources, 'multilayer_tif', 'dotdotdot_lzw*.tif')
data = fromtif(path, nplanes=3, engine=eng)
Expand Down
15 changes: 11 additions & 4 deletions thunder/images/readers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import itertools
import warnings
import logging
from io import BytesIO
from numpy import frombuffer, prod, random, asarray, expand_dims

Expand Down Expand Up @@ -316,7 +316,7 @@ def getarray(idx_buffer_filename):
dims=newdims, dtype=dtype, labels=labels, recount=recount,
engine=engine, credentials=credentials)

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

Expand All @@ -362,8 +366,11 @@ def getarray(idx_buffer_filename):
if nplanes is not None:
extra = pageCount % nplanes
if extra:
pageCount = pageCount - extra
warnings.warn('Ignored %d pages in file %s' % (extra, fname), RuntimeWarning)
if discard_extra:
pageCount = pageCount - extra
logging.getLogger('thunder').warn('Ignored %d pages in file %s' % (extra, fname))
else:
raise ValueError("nplanes '%d' does not evenly divide '%d'" % (nplanes, pageCount))
values = [ary[i:(i+nplanes)] for i in range(0, pageCount, nplanes)]
else:
values = [ary]
Expand Down

0 comments on commit 05bf7cb

Please sign in to comment.