Skip to content

Commit 6eeba6e

Browse files
Make package version checking optional, fix/refactor ISIF test
1 parent ff1017c commit 6eeba6e

File tree

6 files changed

+507
-227
lines changed

6 files changed

+507
-227
lines changed

pymatgen/io/validation/check_incar.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ def check(
118118
append_comments=working_params.defaults[key]["comment"],
119119
severity=working_params.defaults[key]["severity"],
120120
)
121+
# if key == "ISIF":
122+
# print("batz",key,working_params.parameters[key], parameters.get("ISIF"),working_params.valid_values[key],working_params.defaults[key])
121123

122124

123125
class UpdateParameterValues:
@@ -671,6 +673,9 @@ def update_electronic_params(self):
671673

672674
def update_ionic_params(self):
673675
"""Update parameters related to ionic relaxation."""
676+
677+
self.valid_values["ISIF"] = 2
678+
674679
# IBRION.
675680
self.valid_values["IBRION"] = [-1, 1, 2]
676681
if self.input_set.incar.get("IBRION"):
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from __future__ import annotations
2+
from importlib.metadata import version
3+
import requests
4+
import warnings
5+
6+
7+
def package_version_check() -> None:
8+
"""Warn the user if pymatgen / pymatgen-io-validation is not up-to-date."""
9+
10+
packages = {
11+
"pymatgen": "Hence, if any pymatgen input sets have been updated, this validator will be outdated.",
12+
"pymagen-io-validation": "Hence, if any checks in this package have been updated, the validator you use will be outdated.",
13+
}
14+
15+
for package, context_msg in packages.items():
16+
if not is_package_is_up_to_date(package):
17+
warnings.warn(
18+
"We *STRONGLY* recommend you to update your "
19+
f"`{package}` package, which is behind the most "
20+
f"recent version. {context_msg}"
21+
)
22+
23+
24+
def is_package_is_up_to_date(package_name: str) -> bool:
25+
"""Check if a package is up-to-date with the PyPI version."""
26+
27+
try:
28+
cur_version = version(package_name)
29+
except Exception:
30+
raise ImportError(f"Package `{package_name}` is not installed!")
31+
32+
try:
33+
response = requests.get(f"https://pypi.org/pypi/{package_name}/json")
34+
latest_version = response.json()["info"]["version"]
35+
except Exception:
36+
raise ImportError(f"Package `{package_name}` does not exist in PyPI!")
37+
38+
return cur_version == latest_version

pymatgen/io/validation/validation.py

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,34 +33,14 @@
3333
from pymatgen.io.validation.check_potcar import CheckPotcar
3434
from pymatgen.io.validation.settings import IOValidationSettings
3535

36-
from importlib.metadata import version
37-
import requests
38-
39-
from typing import TYPE_CHECKING
36+
from typing import TYPE_CHECKING, Any
4037

4138
if TYPE_CHECKING:
4239
pass
4340

4441
SETTINGS = IOValidationSettings()
4542
_vasp_defaults = loadfn(SETTINGS.VASP_DEFAULTS_FILENAME)
4643

47-
48-
def is_package_is_up_to_date(package_name: str):
49-
"""check if a package is up-to-date."""
50-
try:
51-
cur_version = version(package_name)
52-
except Exception:
53-
cur_version = "not installed"
54-
55-
try:
56-
response = requests.get(f"https://pypi.org/pypi/{package_name}/json")
57-
latest_version = response.json()["info"]["version"]
58-
except Exception:
59-
latest_version = "package does not exist"
60-
61-
return cur_version == latest_version
62-
63-
6444
# TODO: check for surface/slab calculations. Especially necessary for external calcs.
6545
# TODO: implement check to make sure calcs are within some amount (e.g. 250 meV) of the convex hull in the MPDB
6646

@@ -78,6 +58,14 @@ class ValidationDoc(EmmetBaseModel):
7858
description="Last updated date for this document",
7959
default_factory=datetime.utcnow,
8060
)
61+
check_package_versions: bool = Field(
62+
False,
63+
description=(
64+
"Whether to check if the currently installed versions "
65+
"of pymatgen and pymatgen-io-validation are the most "
66+
"up to date versions on PyPI."
67+
),
68+
)
8169

8270
reasons: list[str] = Field(None, description="List of deprecation tags detailing why this task isn't valid")
8371

@@ -88,22 +76,11 @@ class ValidationDoc(EmmetBaseModel):
8876
# " Useful for post-mortem analysis"
8977
# )
9078

91-
def model_post_init(self, ctx):
92-
"""Warn the user if pymatgen / pymatgen-io-validation is not up-to-date."""
93-
import warnings
79+
def model_post_init(self, context: Any) -> None:
80+
if self.check_package_versions:
81+
from check_package_versions import package_version_check
9482

95-
pymatgen_is_up_to_date = is_package_is_up_to_date("pymatgen")
96-
if not pymatgen_is_up_to_date:
97-
warnings.warn(
98-
"We *STRONGLY* recommend you to update your `pymatgen` package, which is behind the most recent version. "
99-
"Hence, if any pymatgen input sets have been updated, this validator will be outdated."
100-
)
101-
pymatgen_io_validation_is_up_to_date = is_package_is_up_to_date("pymatgen-io-validation")
102-
if not pymatgen_io_validation_is_up_to_date:
103-
warnings.warn(
104-
"We *STRONGLY* recommend you to update your `pymatgen-io-validation` package, which is behind the most recent version. "
105-
"Hence, if any checks in this package have been updated, the validator you use will be outdated."
106-
)
83+
package_version_check()
10784

10885
class Config: # noqa
10986
extra = "allow"
@@ -156,8 +133,6 @@ def from_task_doc(
156133
if orig_inputs["kpoints"] is not None:
157134
orig_inputs["kpoints"] = orig_inputs["kpoints"].as_dict()
158135

159-
calcs_reversed[0]["output"]["ionic_steps"]
160-
161136
potcars = calcs_reversed[0]["input"]["potcar_spec"]
162137

163138
calc_type = _get_calc_type(calcs_reversed, orig_inputs)
677 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)