Skip to content

Commit

Permalink
Dynamically patch old structural data to work with latest pymatgen wh…
Browse files Browse the repository at this point in the history
…en depickling
  • Loading branch information
ml-evs committed Mar 29, 2024
1 parent 8bfebb1 commit 9c0ccdf
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion modnet/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path

from modnet.utils import get_hash_of_file
from pymatgen.core import Structure


_TEST_DATA_HASHES = {
Expand Down Expand Up @@ -41,7 +42,22 @@ def _load_moddata(filename):
# what it was when created
assert get_hash_of_file(data_file) == _TEST_DATA_HASHES[filename]

return MODData.load(data_file)
moddata = MODData.load(data_file)
# For forwards compatibility with pymatgen, we have to patch our old test data to have the following attributes
# to allow for depickling
# This is hopefully only a temporary solution, and in future, we should serialize pymatgen objects
# with Monty's `from_dict`/`to_dict` to avoid having to hack this private interface
for ind, s in enumerate(moddata.structures):
if isinstance(moddata.structures[ind], Structure):
# assume all previous data was periodic
moddata.structures[ind].lattice._pbc = [True, True, True]
for jnd, site in enumerate(s.sites):
# assume all of our previous data had ordered sites
moddata.structures[ind].sites[jnd].label = str(next(iter(site.species)))
# required for the global structure.is_ordered to work
moddata.structures[ind].sites[jnd].species._n_atoms = 1.0

return moddata


@pytest.fixture(scope="function")
Expand Down

0 comments on commit 9c0ccdf

Please sign in to comment.