11import itertools
2- import warnings
2+ import logging
33from io import BytesIO
44from 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