Skip to content

Commit

Permalink
update tests / supported table
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ committed Dec 6, 2024
1 parent c0b0144 commit 24591f1
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 19 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ repos:
args: [ --fix ]
# Run the formatter.
- id: ruff-format
- repo: https://github.com/executablebooks/mdformat
rev: 0.7.19
hooks:
- id: mdformat
args: ["--wrap=80"]
# - repo: https://github.com/executablebooks/mdformat
# rev: 0.7.19
# hooks:
# - id: mdformat
# args: ["--wrap=80"]
49 changes: 35 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,47 @@ ZnH5MD circumvents two current limitations of the H5MD standard.

## Supported

### `atoms.arrays`
### `atoms.info` entry

`dict` and `list` entries assume python standard types if not otherwise specified.

| | ZnH5MD | | ------------- | ------------- | | `dict[str, np.ndarray]` || |
`dict[str, list]` | ❌ |
| Type | ZnH5MD |
|--------------------------|--------|
| `np.ndarray` ||
| `float` ||
| `str` ||
| `dict` ||
| `list` ||
| `list[np.ndarray]` ||
| `dict[str, np.ndarray]` ||
| `list[dict]` ||

### `atoms.arrays`

### `atoms.info`
| Type | ZnH5MD |
|--------------------------|--------|
| `np.ndarray` ||
| `float` ||
| `str` ||
| `dict` ||
| `list` ||
| `list[np.ndarray]` ||
| `dict[str, np.ndarray]` ||
| `list[dict]` ||

| | ZnH5MD | | ------------- | ------------- | | `dict[str, np.ndarray]` || |
`dict[str, float]` | ✅ | | `dict[str, int]` | ✅ | | `dict[str, str]` | ✅ | |
`dict[str, dict]` | ✅ | | `dict[str, list[float]` | ✅ | | `dict[str, list[int]`
|| | `dict[str, list[str]` || | `dict[str, list[np.ndarray]` || |
`dict[str, list[dict]` | ❌ |

### `atoms.calc.results`

| | ZnH5MD | | ------------- | ------------- | | `dict[str, np.ndarray]` || |
`dict[str, float]` | ✅ | | `dict[str, int]` | ✅ | | `dict[str, str]` | ✅ | |
`dict[str, dict]` | ✅ | | `dict[str, list[float]` | ✅ | | `dict[str, list[int]`
|| | `dict[str, list[str]` || | `dict[str, list[np.ndarray]` || |
`dict[str, list[dict]` | ❌ |
| Type | ZnH5MD |
|--------------------------|--------|
| `np.ndarray` ||
| `float` ||
| `str` ||
| `dict` ||
| `list` ||
| `list[np.ndarray]` ||
| `dict[str, np.ndarray]` ||
| `list[dict]` ||

## Current limitations

Expand Down
145 changes: 145 additions & 0 deletions tests/format/test_info_array_calc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import pytest
import znh5md
import numpy as np
from ase.build import molecule
import numpy.testing as npt
from ase.calculators.singlepoint import SinglePointCalculator


# Define assertion functions for different data types
def assert_equal(actual, expected):
assert actual == expected


def assert_allclose(actual, expected):
npt.assert_allclose(actual, expected)


def assert_allclose_list(actual, expected):
for a, e in zip(actual, expected):
npt.assert_allclose(a, e)


def assert_dict_allclose(actual, expected):
for key in expected:
npt.assert_allclose(actual[key], expected[key])


# Pytest fixtures
@pytest.fixture
def io_fixture(tmp_path):
"""Fixture to initialize the ZnH5MD IO object."""
return znh5md.IO(tmp_path / "test.h5")


@pytest.fixture
def water_molecule():
"""Fixture to provide a simple water molecule."""
return molecule("H2O")


@pytest.mark.parametrize(
"key,value,assert_fn",
[
("str", "Hello World", assert_equal),
("float", 3.14, assert_equal),
("ndarray", np.random.rand(10), assert_allclose),
("list_float", [1.0, 2.0, 3.0], assert_allclose),
("list_str", ["Hello", "World"], assert_equal),
("dict", {"a": 1, "b": 2}, assert_equal),
("dict_str", {"a": "Hello", "b": "World"}, assert_equal),
("list_array", [np.random.rand(10), np.random.rand(10)], assert_allclose_list),
("dict_dict", {"a": {"x": 1, "y": 2}, "b": {"x": 3, "y": 4}}, assert_equal),
# (
# "dict_array",
# {"a": np.random.rand(10), "b": np.random.rand(10)},
# assert_dict_allclose,
# ),
# ("list_dict", [{"a": 1, "b": 2}, {"c": 3, "d": 4}], assert_equal),
],
)
def test_info(io_fixture, water_molecule, key, value, assert_fn):
"""Generic test for different info types."""
# Assign the value to the molecule's info
water_molecule.info[key] = value

# Append to the ZnH5MD IO object
io_fixture.append(water_molecule)

# Retrieve and test
assert_fn(io_fixture[0].info[key], value)
assert key not in io_fixture[0].arrays
assert io_fixture[0].calc is None




@pytest.mark.parametrize(
"key,value,assert_fn",
[
("str", "Hello World", assert_equal),
("float", 3.14, assert_equal),
("ndarray", np.random.rand(10), assert_allclose),
("list_float", [1.0, 2.0, 3.0], assert_allclose),
("list_str", ["Hello", "World"], assert_equal),
("dict", {"a": 1, "b": 2}, assert_equal),
("dict_str", {"a": "Hello", "b": "World"}, assert_equal),
("list_array", [np.random.rand(10), np.random.rand(10)], assert_allclose_list),
("dict_dict", {"a": {"x": 1, "y": 2}, "b": {"x": 3, "y": 4}}, assert_equal),
# (
# "dict_array",
# {"a": np.random.rand(10), "b": np.random.rand(10)},
# assert_dict_allclose,
# ),
# ("list_dict", [{"a": 1, "b": 2}, {"c": 3, "d": 4}], assert_equal),
],
)
def test_calc(io_fixture, water_molecule, key, value, assert_fn):
"""Generic test for different calc types."""
# Assign the value to the molecule's info
water_molecule.calc = SinglePointCalculator(water_molecule)
water_molecule.calc.results[key] = value

# Append to the ZnH5MD IO object
io_fixture.append(water_molecule)

# Retrieve and test
assert_fn(io_fixture[0].calc.results[key], value)
assert key not in io_fixture[0].arrays
assert io_fixture[0].info == {}



@pytest.mark.parametrize(
"key,value,assert_fn",
[
("str", "Hello World", assert_equal),
("float", 3.14, assert_equal),
("ndarray", np.random.rand(10), assert_allclose),
("list_float", [1.0, 2.0, 3.0], assert_allclose),
("list_str", ["Hello", "World"], assert_equal),
("dict", {"a": 1, "b": 2}, assert_equal),
("dict_str", {"a": "Hello", "b": "World"}, assert_equal),
("list_array", [np.random.rand(10), np.random.rand(10)], assert_allclose_list),
("dict_dict", {"a": {"x": 1, "y": 2}, "b": {"x": 3, "y": 4}}, assert_equal),
# (
# "dict_array",
# {"a": np.random.rand(10), "b": np.random.rand(10)},
# assert_dict_allclose,
# ),
# ("list_dict", [{"a": 1, "b": 2}, {"c": 3, "d": 4}], assert_equal),
],
)
def test_arrays(io_fixture, water_molecule, key, value, assert_fn):
"""Generic test for different calc types."""
# Assign the value to the molecule's info
water_molecule.arrays[key] = value

# Append to the ZnH5MD IO object
io_fixture.append(water_molecule)

# Retrieve and test
assert_fn(io_fixture[0].arrays[key], value)
# assert key not in io_fixture[0].arrays
assert io_fixture[0].info == {}
assert io_fixture[0].calc is None

0 comments on commit 24591f1

Please sign in to comment.