diff --git a/tests/test_single_obs_key.py b/tests/test_single_obs_key.py index 1477a35..d23d3f5 100644 --- a/tests/test_single_obs_key.py +++ b/tests/test_single_obs_key.py @@ -15,7 +15,7 @@ def test_single_entry_info(tmp_path): io.extend([water for _ in range(5)]) assert len(io) == 6 assert len(list(io)) == 6 - # assert len(io[:]) == 6 + assert len(io[:]) == 6 def test_single_entry_arrays(tmp_path): # Test a special case where only the first config has the key @@ -28,7 +28,7 @@ def test_single_entry_arrays(tmp_path): io.extend([water for _ in range(5)]) assert len(io) == 6 assert len(list(io)) == 6 - # assert len(io[:]) == 6 + assert len(io[:]) == 6 def test_single_entry_calc(tmp_path): @@ -42,4 +42,4 @@ def test_single_entry_calc(tmp_path): io.extend([water for _ in range(5)]) assert len(io) == 6 assert len(list(io)) == 6 - # assert len(io[:]) == 6 + assert len(io[:]) == 6 diff --git a/znh5md/interface/io.py b/znh5md/interface/io.py index d20400c..280bc84 100644 --- a/znh5md/interface/io.py +++ b/znh5md/interface/io.py @@ -103,7 +103,8 @@ def __getitem__( # FileNotFoundError is an OSError, but we want to handle it # separately from the OSError h5py raises raise - except OSError: + except OSError as err: + print(err) raise IndexError("Index out of range") def extend(self, frames: list[ase.Atoms]) -> None: diff --git a/znh5md/interface/read.py b/znh5md/interface/read.py index 787e71e..4d733b5 100644 --- a/znh5md/interface/read.py +++ b/znh5md/interface/read.py @@ -181,8 +181,13 @@ def getitem( if f"/observables/{self.particles_group}" in f: observables = f[f"/observables/{self.particles_group}"] process_observables(self, frames, observables, index) + + print(len(frames)) + print(particles) + print(is_single_item) - return list(frames) if not is_single_item else frames[0] + # return list(frames) if not is_single_item else frames[0] + return frames def process_species_group(self, frames: Frames, particles, index) -> None: @@ -341,10 +346,12 @@ def process_observables(self, frames: Frames, observables, index) -> None: update_frames( frames, grp_name, grp["value"][index], origin, self.use_ase_calc ) - except (OSError, IndexError): + except (OSError, IndexError) as err: + print(err) # ??? why is this not triggering? pass # Handle backfilling for invalid values - except KeyError: + except KeyError as err: + print(err) raise KeyError( f"Key '{grp_name}' does not seem to be a valid H5MD group" " - missing 'value' dataset." diff --git a/znh5md/serialization/base.py b/znh5md/serialization/base.py index ce47cdf..bd13217 100644 --- a/znh5md/serialization/base.py +++ b/znh5md/serialization/base.py @@ -2,6 +2,7 @@ import functools import json import typing as t +import contextlib import ase import h5py @@ -247,19 +248,23 @@ def __getitem__(self, idx: int) -> ase.Atoms: for key in self.arrays: if isinstance(self.arrays[key][idx], _MISSING): continue - if key == "velocities": - atoms.set_velocities(self.arrays[key][idx]) - else: - atoms.arrays[key] = self.arrays[key][idx] + with contextlib.suppress(KeyError): + if key == "velocities": + atoms.set_velocities(self.arrays[key][idx]) + else: + atoms.arrays[key] = self.arrays[key][idx] for key in self.info: if not isinstance(self.info[key][idx], _MISSING): - atoms.info[key] = self.info[key][idx] + with contextlib.suppress(KeyError): + atoms.info[key] = self.info[key][idx] for key in self.calc: if not isinstance(self.calc[key][idx], _MISSING): - if atoms.calc is None: - atoms.calc = SinglePointCalculator(atoms) - atoms.calc.results[key] = self.calc[key][idx] + with contextlib.suppress(KeyError): + value = self.calc[key][idx] + if atoms.calc is None: + atoms.calc = SinglePointCalculator(atoms) + atoms.calc.results[key] = value return atoms