From d2cc047cfb4b90cc80f62d60aae08584381379fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20M=C3=BCller?= Date: Mon, 23 Nov 2026 11:53:25 +0100 Subject: [PATCH] fix: support basins without a name The basin name is optional, but sorting the basins in `HDF5Data` compared the names directly, so a file with two basins of the same type without a name (missing or None) could not be opened (`KeyError` or `TypeError`). `copy_basins` and the job runner also required the "name" and "description" keys. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG | 3 +++ dcnum/logic/ctrl.py | 4 ++-- dcnum/read/hdf5_data.py | 5 ++-- dcnum/write/writer.py | 8 +++---- tests/test_read_hdf5_basins.py | 44 ++++++++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 8 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 1134ca7..d7d839c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -53,6 +53,9 @@ so their pipeline identifier could not be resolved - fix: `detect_flickering` raised a `TypeError` for `HDF5Data` instances, although the docstring allowed them + - fix: files with more than one basin without a name could not be + opened (regression in 0.28.3), and basins without a name or + description could not be copied - docs: comment on intended behavior of `fill_holes` (mask post-processing), Haralick texture features, and the sparsemed background assignment after cleansing diff --git a/dcnum/logic/ctrl.py b/dcnum/logic/ctrl.py index b4bf84f..2a991cf 100644 --- a/dcnum/logic/ctrl.py +++ b/dcnum/logic/ctrl.py @@ -723,8 +723,8 @@ def task_enforce_basin_strategy(self): else: basinmap1 = basinmap_in[basinmap0] # Store the internal mapping in the output file - hw.store_basin(name=bn_dict["name"], - description=bn_dict["description"], + hw.store_basin(name=bn_dict.get("name"), + description=bn_dict.get("description"), mapping=basinmap1, internal_data={"image_bg": bg_images1} ) diff --git a/dcnum/read/hdf5_data.py b/dcnum/read/hdf5_data.py index 3118e8b..db1d860 100644 --- a/dcnum/read/hdf5_data.py +++ b/dcnum/read/hdf5_data.py @@ -311,8 +311,9 @@ def __setstate__(self, state): def basin_sort_cmp(a, b): """Sort internal basins before any other basins""" if a["type"] == b["type"]: - an = a["name"] - bn = b["name"] + # The basin name is optional + an = a.get("name") or "" + bn = b.get("name") or "" if an == bn: return 0 if an < bn: diff --git a/dcnum/write/writer.py b/dcnum/write/writer.py index 989bb2d..10cbc5a 100644 --- a/dcnum/write/writer.py +++ b/dcnum/write/writer.py @@ -418,8 +418,8 @@ def copy_basins(h5_src: "h5py.File", internal_data = {} for feat in bn_dict["features"]: internal_data[feat] = h5_src["basin_events"][feat] - hw.store_basin(name=bn_dict["name"], - description=bn_dict["description"], + hw.store_basin(name=bn_dict.get("name"), + description=bn_dict.get("description"), mapping=h5_src["events"][bn_dict["mapping"]][:], internal_data=internal_data, ) @@ -428,8 +428,8 @@ def copy_basins(h5_src: "h5py.File", mapping = h5_src["events"][bn_dict["mapping"]][:] else: mapping = None - hw.store_basin(name=bn_dict["name"], - description=bn_dict["description"], + hw.store_basin(name=bn_dict.get("name"), + description=bn_dict.get("description"), paths=bn_dict["paths"], features=bn_dict["features"], mapping=mapping, diff --git a/tests/test_read_hdf5_basins.py b/tests/test_read_hdf5_basins.py index 493330a..256bd0d 100644 --- a/tests/test_read_hdf5_basins.py +++ b/tests/test_read_hdf5_basins.py @@ -1,3 +1,4 @@ +import json import pathlib import h5py @@ -257,6 +258,49 @@ def test_basin_multiple(mapping, numevents): assert "area_um" not in hd +@pytest.mark.parametrize("names", [ + [None, None], + ["peter", None], + ["omitted", "omitted"], + ["peter", "omitted"], +]) +def test_basin_without_name(tmp_path, names): + """The basin name is optional (dcnum 0.31.2 could not open the file)""" + path_src = retrieve_data( + "fmt-hdf5_cytoshot_full-features_legacy_allev_2023.zip") + path = tmp_path / "unnamed.rtdc" + path_copy = tmp_path / "copy.rtdc" + + with h5py.File(path_src) as src, h5py.File(path, "w") as dst: + write.copy_metadata(h5_src=src, h5_dst=dst) + dst["events/deform"] = src["events/deform"][:] + image = src["events/image"][:] + for ii, (name, bpath) in enumerate(zip( + names, [str(path_src), str(tmp_path / "missing.rtdc")])): + bn_dict = {"type": "file", + "format": "hdf5", + "paths": [bpath], + "features": ["image"], + } + if name != "omitted": + bn_dict["name"] = name + dst.require_group("basins").create_dataset( + f"basin_{ii}", + data=[ln.encode() for ln in + json.dumps(bn_dict, indent=2).split("\n")]) + + with read.HDF5Data(path) as hd: + assert len(hd.basins) == 2 + assert np.all(hd["image"][:] == image) + + with h5py.File(path) as src, h5py.File(path_copy, "w") as dst: + write.copy_metadata(h5_src=src, h5_dst=dst) + write.copy_basins(h5_src=src, h5_dst=dst) + + with read.HDF5Data(path_copy) as hd: + assert len(hd.basins) == 2 + + def test_basin_no_inception(): """We do not support basin inception in dcnum