From 5dcc337fd92c6dc7928576ece2317e3884647133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20M=C3=BCller?= Date: Wed, 10 Jan 2024 23:59:39 +0100 Subject: [PATCH] fix: RTDCWriter.rectify_metadata fails when image feature is empty --- CHANGELOG | 1 + dclab/rtdc_dataset/writer.py | 13 ++++++++----- tests/test_rtdc_writer.py | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 9004c2b7..9ac9bc99 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,5 @@ 0.57.1 + - fix: RTDCWriter.rectify_metadata fails when image feature is empty - docs: improve documentation of hierarchy child mapper 0.57.0 - fix: integer overflow in downsample_grid diff --git a/dclab/rtdc_dataset/writer.py b/dclab/rtdc_dataset/writer.py index b81853f9..3c90e3c0 100644 --- a/dclab/rtdc_dataset/writer.py +++ b/dclab/rtdc_dataset/writer.py @@ -148,9 +148,12 @@ def rectify_metadata(self): else: raise ValueError(f"No features in '{self.path}'!") - # make sure that "trace" is not empty - if "trace" in feats and len(self.h5file["events"]["trace"]) == 0: - feats.remove("trace") + # ignore empty features in the checks further below + for feat in feats[:]: # iterate over a copy of the list + obj = self.h5file["events"][feat] + if ((isinstance(feat, h5py.Group) and len(obj) == 0) # groups + or obj.shape[0] == 0): # datasets + feats.remove(feat) # set samples per event if "trace" in feats: @@ -166,9 +169,9 @@ def rectify_metadata(self): self.h5file.attrs["fluorescence:channel count"] = chcount # set roi size x/y - if "image" in self.h5file["events"]: + if "image" in feats: shape = self.h5file["events"]["image"][0].shape - elif "mask" in self.h5file["events"]: + elif "mask" in feats: shape = self.h5file["events"]["mask"][0].shape else: shape = None diff --git a/tests/test_rtdc_writer.py b/tests/test_rtdc_writer.py index 0945ac0c..46116423 100644 --- a/tests/test_rtdc_writer.py +++ b/tests/test_rtdc_writer.py @@ -675,6 +675,25 @@ def test_real_time_single(): assert len(logs["log1"]) == n +def test_rectify_metadata_ignore_empty_image(): + # test introduced in 0.39.7 + rtdc_file = tempfile.mktemp(suffix=".rtdc", + prefix="dclab_test_error_") + with h5py.File(rtdc_file, "a") as h5: + h5.require_group("events") + h5["events"].require_dataset(name="image", + shape=(0, 80, 100), + dtype=np.uint8) + h5["events/deform"] = np.linspace(.1, .12, 7) + + # Initialize writer + hw = RTDCWriter(h5, mode="append") + # in previous versions, this did not work, because of the empty trace + hw.rectify_metadata() + # make sure that something happened + assert h5.attrs["experiment:event count"] == 7 + + def test_rectify_metadata_ignore_empty_trace(): # test introduced in 0.39.7 rtdc_file = tempfile.mktemp(suffix=".rtdc",