Skip to content

Commit

Permalink
fix: RTDCWriter.rectify_metadata fails when image feature is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Jan 10, 2024
1 parent b155b4e commit 5dcc337
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 8 additions & 5 deletions dclab/rtdc_dataset/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions tests/test_rtdc_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 5dcc337

Please sign in to comment.