diff --git a/CHANGELOG b/CHANGELOG index 53def79..8e1b2d9 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +0.13.3 + - fix: correctly raise KeyError for missing image-based feature from + HDF5Data._image_cache 0.13.2 - fix: properly convert variable-length string logs in `copy_metadata` 0.13.1 diff --git a/dcnum/read/hdf5_data.py b/dcnum/read/hdf5_data.py index f2fe884..2722d9a 100644 --- a/dcnum/read/hdf5_data.py +++ b/dcnum/read/hdf5_data.py @@ -53,7 +53,11 @@ def __exit__(self, exc_type, exc_val, exc_tb): def __getitem__(self, feat): if feat in ["image", "image_bg", "mask"]: - return self.get_image_cache(feat) + data = self.get_image_cache(feat) + if data is None: + raise KeyError(f"Feature '{feat}' not found in {self}!") + else: + return data elif feat in self._cache_scalar: # check for scalar cached return self._cache_scalar[feat] elif (feat in self.h5["events"] diff --git a/tests/test_read_hdf5.py b/tests/test_read_hdf5.py index fa6b70b..8e2a9fd 100644 --- a/tests/test_read_hdf5.py +++ b/tests/test_read_hdf5.py @@ -97,6 +97,16 @@ def test_image_cache_iter_chunks(size, chunks, tmp_path): assert list(hic.iter_chunks()) == list(range(chunks)) +def test_keyerror_when_image_is_none(tmp_path): + path = tmp_path / "test.hdf5" + with h5py.File(path, "w") as hw: + hw["events/deform"] = np.random.rand(100) + + h5dat = read.HDF5Data(path) + with pytest.raises(KeyError, match="image"): + _ = h5dat["image"] + + def test_pixel_size_getset(tmp_path): path = tmp_path / "test.hdf5" with h5py.File(path, "w") as hw: