1
1
import itertools
2
+ import logging
2
3
from io import BytesIO
3
4
from numpy import frombuffer , prod , random , asarray , expand_dims
4
5
@@ -283,8 +284,8 @@ def frombinary(path, shape=None, dtype=None, ext='bin', start=None, stop=None, r
283
284
raise ValueError ("Last dimension '%d' must be divisible by nplanes '%d'" %
284
285
(shape [- 1 ], nplanes ))
285
286
286
- def getarray (idxAndBuf ):
287
- idx , buf = idxAndBuf
287
+ def getarray (idx_buffer_filename ):
288
+ idx , buf , _ = idx_buffer_filename
288
289
ary = frombuffer (buf , dtype = dtype , count = int (prod (shape ))).reshape (shape , order = order )
289
290
if nplanes is None :
290
291
yield (idx ,), ary
@@ -294,17 +295,17 @@ def getarray(idxAndBuf):
294
295
if shape [- 1 ] % nplanes :
295
296
npoints += 1
296
297
timepoint = 0
297
- lastPlane = 0
298
- curPlane = 1
299
- while curPlane < ary .shape [- 1 ]:
300
- if curPlane % nplanes == 0 :
301
- slices = [slice (None )] * (ary .ndim - 1 ) + [slice (lastPlane , curPlane )]
298
+ last_plane = 0
299
+ current_plane = 1
300
+ while current_plane < ary .shape [- 1 ]:
301
+ if current_plane % nplanes == 0 :
302
+ slices = [slice (None )] * (ary .ndim - 1 ) + [slice (last_plane , current_plane )]
302
303
yield idx * npoints + timepoint , ary [slices ].squeeze ()
303
304
timepoint += 1
304
- lastPlane = curPlane
305
- curPlane += 1
305
+ last_plane = current_plane
306
+ current_plane += 1
306
307
# yield remaining planes
307
- slices = [slice (None )] * (ary .ndim - 1 ) + [slice (lastPlane , ary .shape [- 1 ])]
308
+ slices = [slice (None )] * (ary .ndim - 1 ) + [slice (last_plane , ary .shape [- 1 ])]
308
309
yield (idx * npoints + timepoint ,), ary [slices ].squeeze ()
309
310
310
311
recount = False if nplanes is None else True
@@ -315,7 +316,7 @@ def getarray(idxAndBuf):
315
316
dims = newdims , dtype = dtype , labels = labels , recount = recount ,
316
317
engine = engine , credentials = credentials )
317
318
318
- 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 ):
319
320
"""
320
321
Loads images from single or multi-page TIF files.
321
322
@@ -346,29 +347,38 @@ def fromtif(path, ext='tif', start=None, stop=None, recursive=False, nplanes=Non
346
347
347
348
labels : array, optional, default = None
348
349
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
349
354
"""
350
355
import skimage .external .tifffile as tifffile
351
356
352
357
if nplanes is not None and nplanes <= 0 :
353
358
raise ValueError ('nplanes must be positive if passed, got %d' % nplanes )
354
359
355
- def getarray (idxAndBuf ):
356
- idx , buf = idxAndBuf
360
+ def getarray (idx_buffer_filename ):
361
+ idx , buf , fname = idx_buffer_filename
357
362
fbuf = BytesIO (buf )
358
363
tfh = tifffile .TiffFile (fbuf )
359
364
ary = tfh .asarray ()
360
365
pageCount = ary .shape [0 ]
361
366
if nplanes is not None :
362
- values = [ary [i :(i + nplanes )] for i in range (0 , ary .shape [0 ], nplanes )]
367
+ extra = pageCount % nplanes
368
+ if extra :
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 ))
374
+ values = [ary [i :(i + nplanes )] for i in range (0 , pageCount , nplanes )]
363
375
else :
364
376
values = [ary ]
365
377
tfh .close ()
366
378
367
379
if ary .ndim == 3 :
368
380
values = [val .squeeze () for val in values ]
369
381
370
- if nplanes and (pageCount % nplanes ):
371
- raise ValueError ("nplanes '%d' does not evenly divide '%d'" % (nplanes , pageCount ))
372
382
nvals = len (values )
373
383
keys = [(idx * nvals + timepoint ,) for timepoint in range (nvals )]
374
384
return zip (keys , values )
@@ -408,8 +418,8 @@ def frompng(path, ext='png', start=None, stop=None, recursive=False, npartitions
408
418
"""
409
419
from scipy .misc import imread
410
420
411
- def getarray (idxAndBuf ):
412
- idx , buf = idxAndBuf
421
+ def getarray (idx_buffer_filename ):
422
+ idx , buf , _ = idx_buffer_filename
413
423
fbuf = BytesIO (buf )
414
424
yield (idx ,), imread (fbuf )
415
425
0 commit comments