From 9c0ccdfc0e19ac46bebd69cad4b6764e4f963ccb Mon Sep 17 00:00:00 2001 From: Matthew Evans Date: Fri, 29 Mar 2024 11:33:54 +0000 Subject: [PATCH] Dynamically patch old structural data to work with latest pymatgen when depickling --- modnet/tests/conftest.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/modnet/tests/conftest.py b/modnet/tests/conftest.py index d5061c90..8a433c30 100644 --- a/modnet/tests/conftest.py +++ b/modnet/tests/conftest.py @@ -2,6 +2,7 @@ from pathlib import Path from modnet.utils import get_hash_of_file +from pymatgen.core import Structure _TEST_DATA_HASHES = { @@ -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")