Skip to content

Commit

Permalink
bugfix finalize
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ committed Dec 6, 2024
1 parent feec872 commit 6085bfd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
6 changes: 3 additions & 3 deletions tests/test_single_obs_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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
3 changes: 2 additions & 1 deletion znh5md/interface/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 10 additions & 3 deletions znh5md/interface/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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."
Expand Down
21 changes: 13 additions & 8 deletions znh5md/serialization/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import functools
import json
import typing as t
import contextlib

import ase
import h5py
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 6085bfd

Please sign in to comment.