Skip to content

Commit

Permalink
fix IndexError (#88)
Browse files Browse the repository at this point in the history
* fix IndexError

* test missing properties

* bump version

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
PythonFZ and pre-commit-ci[bot] authored Dec 21, 2023
1 parent d1b4f43 commit 496844f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "znh5md"
version = "0.1.8"
version = "0.1.9"
description = "High Performance Interface for H5MD Trajectories"
authors = ["zincwarecode <[email protected]>"]
license = "Apache-2.0"
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def atoms_list(request) -> list[ase.Atoms]:
)
for _ in range(21)
]

if getattr(request, "param", "").endswith("vary_pbc"):
atoms[0].pbc = np.array([True, True, False])
atoms[1].pbc = np.array([True, False, True])
Expand Down
16 changes: 16 additions & 0 deletions tests/test_ASEH5MD.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,19 @@ def test_request_missing_properties(tmp_path, atoms_list, remove_calc):
group_names=["stress"]
):
db.add_chunk_data(**chunk)


@pytest.mark.parametrize("atoms_list", ["vary_size"], indirect=True)
def test_skip_property(tmp_path, atoms_list):
os.chdir(tmp_path)

db = znh5md.io.DataWriter(filename="db.h5")
db.initialize_database_groups()

atoms_list[-1].arrays.pop("momenta")
atoms_list[-1].get_momenta()

db.add(znh5md.io.AtomsReader(atoms_list))

traj = znh5md.ASEH5MD("db.h5")
assert traj.get_atoms_list() == atoms_list
2 changes: 1 addition & 1 deletion tests/test_znh5md.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def test_version():
assert znh5md.__version__ == "0.1.8"
assert znh5md.__version__ == "0.1.9"


def test_shape(example_h5):
Expand Down
14 changes: 9 additions & 5 deletions znh5md/znh5md/h5ase.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ def _gather_value(particles_data, key, idx):
Returns None if the key is not present in the data.
"""
if key in particles_data:
if key in [GRP.species, GRP.position, GRP.velocity, GRP.forces, GRP.momentum]:
# use PARTICLES_GRP
return rm_nan(particles_data[key][idx])
return particles_data[key][idx]
try:
if key in particles_data:
if key in [GRP.species, GRP.position, GRP.velocity, GRP.forces, GRP.momentum]:
# use PARTICLES_GRP
return rm_nan(particles_data[key][idx])
return particles_data[key][idx]
except IndexError:
# a property might not be available at all frames.
pass
return None


Expand Down

0 comments on commit 496844f

Please sign in to comment.