Skip to content

Commit

Permalink
fix: correctly raise KeyError for missing image-based feature
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Nov 17, 2023
1 parent 0f65e43 commit fa7f90c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 5 additions & 1 deletion dcnum/read/hdf5_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
10 changes: 10 additions & 0 deletions tests/test_read_hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit fa7f90c

Please sign in to comment.