Skip to content

Commit

Permalink
test available properties for plotting (#782)
Browse files Browse the repository at this point in the history
* test with pytest-cov

* test `model_json_schema_from_atoms`

* refactor into function

* bump version

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

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

* sleep longer

* increase wait timeout

* increase timeout

---------

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 Feb 18, 2025
1 parent df96183 commit 438b22a
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 28 deletions.
16 changes: 10 additions & 6 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ jobs:
- name: Pytest
run: |
uv run python --version
uv run coverage run -m pytest -vv
uv run coverage xml
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
uv run pytest --cov --junitxml=junit.xml -o junit_family=legacy
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
- name: Upload test results to Codecov
if: ${{ !cancelled() }}
uses: codecov/test-results-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "zndraw"
version = "0.5.8a1"
version = "0.5.8"
description = "Display and Edit Molecular Structures and Trajectories in the Browser."
authors = [
{ name = "Fabian Zills", email = "[email protected]" },
Expand Down Expand Up @@ -63,9 +63,9 @@ skip = "*.svg,*.lock"

[dependency-groups]
dev = [
"coverage>=7.6.10",
"mdanalysis>=2.8.0",
"pytest>=8.3.4",
"pytest-cov>=6.0.0",
"rdkit2ase>=0.1.4",
"tidynamics>=1.1.2",
"znh5md>=0.4.4",
Expand Down
27 changes: 26 additions & 1 deletion tests/test_analysis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
import znsocket

from zndraw import ZnDraw
from zndraw.analyse import Properties1D


def run_queue(vis, key, msg: dict):
Expand All @@ -11,7 +13,7 @@ def run_queue(vis, key, msg: dict):
)
modifier_queue.update(msg)
vis.socket.emit("room:worker:run")
vis.socket.sleep(7)
vis.socket.sleep(10)


def test_run_analysis_distance(server, s22_energy_forces):
Expand Down Expand Up @@ -68,3 +70,26 @@ def test_run_analysis_DihedralAngle(server, s22_energy_forces):
fig = vis.figures["DihedralAngle"]
# assert that the x-axis label is "step"
assert fig.layout.xaxis.title.text == "step"


def test_analysis_Properties1D_json_schema(s22_energy_forces):
# add custom info keys
for atoms in s22_energy_forces:
atoms.info["custom"] = 42
atoms.info["custom2"] = np.random.rand(10)
atoms.info["custom3"] = np.random.rand(10, 5)
atoms.calc.results["custom4"] = np.random.rand(10)
atoms.arrays["arr"] = np.zeros_like(atoms.get_positions())

schema = Properties1D.model_json_schema_from_atoms(s22_energy_forces[0])
assert set(schema["properties"]["value"]["enum"]) == {
"energy",
"forces",
"custom",
"numbers",
"positions",
"arr",
"custom2",
"custom3",
"custom4",
}
6 changes: 3 additions & 3 deletions tests/test_geometries.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_geometry_selection_position(server):
"height": 10,
}
vis.socket.emit("room:worker:run")
vis.socket.sleep(5)
vis.socket.sleep(8)

assert len(vis.geometries) == 1
assert vis.geometries[0].position == [0, 0, 0]
Expand All @@ -66,7 +66,7 @@ def test_geometry_selection_position(server):
"height": 10,
}
vis.socket.emit("room:worker:run")
vis.socket.sleep(5)
vis.socket.sleep(8)

assert len(vis.geometries) == 2
assert vis.geometries[1].position == vis.atoms.positions[1].tolist()
Expand All @@ -85,7 +85,7 @@ def test_geometry_selection_position(server):
"height": 10,
}
vis.socket.emit("room:worker:run")
vis.socket.sleep(5)
vis.socket.sleep(8)

assert len(vis.geometries) == 3
assert (
Expand Down
2 changes: 1 addition & 1 deletion tests/test_modifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def run_queue(vis, key, msg: dict):
)
modifier_queue.update(msg)
vis.socket.emit("room:worker:run")
vis.socket.sleep(5)
vis.socket.sleep(10)


def test_run_selection(server, s22):
Expand Down
22 changes: 20 additions & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 11 additions & 13 deletions zndraw/analyse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
pass


def _get_available_properties(atoms: ase.Atoms) -> list[str]:
available_properties = list(atoms.arrays.keys())
available_properties += list(atoms.info.keys())
if atoms.calc is not None:
available_properties += list(atoms.calc.results.keys())
return available_properties


log = logging.getLogger(__name__)


Expand Down Expand Up @@ -130,10 +138,7 @@ class Properties2D(AnaylsisMethod):
def model_json_schema_from_atoms(cls, atoms: ase.Atoms) -> dict:
schema = cls.model_json_schema()

available_properties = list(atoms.arrays.keys())
available_properties += list(atoms.info.keys())
if atoms.calc is not None:
available_properties += list(atoms.calc.results.keys()) # global ATOMS object
available_properties = _get_available_properties(atoms)

available_properties += ["step"]
schema["properties"]["x_data"]["enum"] = available_properties
Expand Down Expand Up @@ -195,10 +200,7 @@ class ForceCorrelation(AnaylsisMethod):
def model_json_schema_from_atoms(cls, atoms: ase.Atoms) -> dict:
schema = cls.model_json_schema()

available_properties = list(atoms.arrays.keys())
available_properties += list(atoms.info.keys())
if atoms.calc is not None:
available_properties += list(atoms.calc.results.keys())
available_properties = _get_available_properties(atoms)
schema["properties"]["x_data"]["enum"] = available_properties
schema["properties"]["y_data"]["enum"] = available_properties

Expand Down Expand Up @@ -254,11 +256,7 @@ class Properties1D(AnaylsisMethod):
def model_json_schema_from_atoms(cls, atoms: ase.Atoms) -> dict:
schema = cls.model_json_schema()

available_properties = list(atoms.arrays.keys())
available_properties += list(atoms.info.keys())
if atoms.calc is not None:
available_properties += list(atoms.calc.results.keys())
log.critical(f"AVAILABLE PROPERTIES: {available_properties=}")
available_properties = _get_available_properties(atoms)
schema["properties"]["value"]["enum"] = available_properties

return schema
Expand Down

0 comments on commit 438b22a

Please sign in to comment.