- warn instead of raise error when file is empty upon closing

This commit is contained in:
Wim Pomp
2022-07-01 10:32:02 +02:00
parent c09bba61bc
commit 41006c62bc
2 changed files with 56 additions and 50 deletions

View File

@@ -5,7 +5,7 @@ with open('README.md', 'r') as fh:
setuptools.setup( setuptools.setup(
name='tiffwrite', name='tiffwrite',
version='2022.5.4', version='2022.6.0',
author='Wim Pomp @ Lenstra lab NKI', author='Wim Pomp @ Lenstra lab NKI',
author_email='w.pomp@nki.nl', author_email='w.pomp@nki.nl',
description='Parallel tiff writer compatible with ImageJ.', description='Parallel tiff writer compatible with ImageJ.',

View File

@@ -1,3 +1,4 @@
import os
import tifffile import tifffile
import colorcet import colorcet
import struct import struct
@@ -485,46 +486,50 @@ class IJTiffFile:
dtype=int).T.flatten()]) for color in self.colors] dtype=int).T.flatten()]) for color in self.colors]
def close(self): def close(self):
assert len(self.frames_added) >= 1, 'at least one frame should be added to the tiff'
if multiprocessing.current_process().pid == self.main_pid: if multiprocessing.current_process().pid == self.main_pid:
self.pool_manager.close(self) self.pool_manager.close(self)
with self.fh.lock() as fh: with self.fh.lock() as fh:
if len(self.frames_written) < np.prod(self.shape): # add empty frames if needed if len(self.frames_added) == 0:
for n in product(*[range(i) for i in self.shape]): warn('At least one frame should be added to the tiff, removing file.')
if n not in self.frames_written: fh.close()
self.add_empty_frame(n) os.remove(self.path)
else:
if len(self.frames_written) < np.prod(self.shape): # add empty frames if needed
for n in product(*[range(i) for i in self.shape]):
if n not in self.frames_written:
self.add_empty_frame(n)
for n, tags in self.frame_extra_tags.items(): for n, tags in self.frame_extra_tags.items():
framenr, channel = self.get_frame_number(n) framenr, channel = self.get_frame_number(n)
self.ifds[framenr].update(tags) self.ifds[framenr].update(tags)
if self.colormap is not None: if self.colormap is not None:
self.ifds[0][320] = Tag('SHORT', self.colormap_bytes) self.ifds[0][320] = Tag('SHORT', self.colormap_bytes)
self.ifds[0][262] = Tag('SHORT', 3) self.ifds[0][262] = Tag('SHORT', 3)
if self.colors is not None: if self.colors is not None:
for c, color in enumerate(self.colors_bytes): for c, color in enumerate(self.colors_bytes):
self.ifds[c][320] = Tag('SHORT', color) self.ifds[c][320] = Tag('SHORT', color)
self.ifds[c][262] = Tag('SHORT', 3) self.ifds[c][262] = Tag('SHORT', 3)
if 306 not in self.ifds[0]: if 306 not in self.ifds[0]:
self.ifds[0][306] = Tag('ASCII', datetime.now().strftime('%Y:%m:%d %H:%M:%S')) self.ifds[0][306] = Tag('ASCII', datetime.now().strftime('%Y:%m:%d %H:%M:%S'))
for framenr in range(self.nframes): for framenr in range(self.nframes):
stripbyteoffsets, stripbytecounts = zip(*[self.strips[(framenr, channel)] stripbyteoffsets, stripbytecounts = zip(*[self.strips[(framenr, channel)]
for channel in range(self.spp)]) for channel in range(self.spp)])
self.ifds[framenr][258].value = self.spp * self.ifds[framenr][258].value self.ifds[framenr][258].value = self.spp * self.ifds[framenr][258].value
self.ifds[framenr][270] = Tag('ASCII', self.description) self.ifds[framenr][270] = Tag('ASCII', self.description)
self.ifds[framenr][273] = Tag('LONG8', sum(stripbyteoffsets, [])) self.ifds[framenr][273] = Tag('LONG8', sum(stripbyteoffsets, []))
self.ifds[framenr][277] = Tag('SHORT', self.spp) self.ifds[framenr][277] = Tag('SHORT', self.spp)
self.ifds[framenr][279] = Tag('LONG8', sum(stripbytecounts, [])) self.ifds[framenr][279] = Tag('LONG8', sum(stripbytecounts, []))
self.ifds[framenr][305] = Tag('ASCII', 'tiffwrite_tllab_NKI') self.ifds[framenr][305] = Tag('ASCII', 'tiffwrite_tllab_NKI')
if self.extratags is not None: if self.extratags is not None:
self.ifds[framenr].update(self.extratags) self.ifds[framenr].update(self.extratags)
if self.colormap is None and self.colors is None and self.shape[0] > 1: if self.colormap is None and self.colors is None and self.shape[0] > 1:
self.ifds[framenr][284] = Tag('SHORT', 2) self.ifds[framenr][284] = Tag('SHORT', 2)
self.ifds[framenr].write(fh, self.header, self.write) self.ifds[framenr].write(fh, self.header, self.write)
if framenr: if framenr:
self.ifds[framenr].write_offset(self.ifds[framenr - 1].where_to_write_next_ifd_offset) self.ifds[framenr].write_offset(self.ifds[framenr - 1].where_to_write_next_ifd_offset)
else: else:
self.ifds[framenr].write_offset(self.header.offset - self.header.offsetsize) self.ifds[framenr].write_offset(self.header.offset - self.header.offsetsize)
fh.close() fh.close()
def __enter__(self): def __enter__(self):
return self return self
@@ -598,19 +603,20 @@ class PoolManager:
self.tifs.pop(tif.path) self.tifs.pop(tif.path)
if not self.tifs: if not self.tifs:
self.__class__.instance = None self.__class__.instance = None
self.is_alive = False if self.is_alive:
self.done.set() self.is_alive = False
while not self.queue.empty(): self.done.set()
self.queue.get() while not self.queue.empty():
self.queue.close() self.queue.get()
self.queue.join_thread() self.queue.close()
while not self.error_queue.empty(): self.queue.join_thread()
print(self.error_queue.get()) while not self.error_queue.empty():
self.error_queue.close() print(self.error_queue.get())
self.ifd_queue.close() self.error_queue.close()
self.ifd_queue.join_thread() self.ifd_queue.close()
self.pool.close() self.ifd_queue.join_thread()
self.pool.join() self.pool.close()
self.pool.join()
def get_ifds_from_queue(self): def get_ifds_from_queue(self):
while not self.ifd_queue.empty(): while not self.ifd_queue.empty():