Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding jarvis-tools structures #297

Merged
merged 9 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions optimade/adapters/structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .cif import get_cif
from .proteindatabank import get_pdb, get_pdbx_mmcif
from .pymatgen import get_pymatgen
from .jarvis import get_jarvis_atoms


__all__ = ("Structure",)
Expand All @@ -24,4 +25,5 @@ class Structure(EntryAdapter):
"pdb": get_pdb,
"pdbx_mmcif": get_pdbx_mmcif,
"pymatgen": get_pymatgen,
"jarvis": get_jarvis_atoms,
}
43 changes: 43 additions & 0 deletions optimade/adapters/structures/jarvis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from warnings import warn
from optimade.models import StructureResource as OptimadeStructure
from optimade.adapters.exceptions import ConversionError
from optimade.adapters.structures.utils import pad_positions

try:
from jarvis.core.atoms import Atoms
except (ImportError, ModuleNotFoundError):
Atoms = None
JARVIS_NOT_FOUND = "jarvis-tools package not found, cannot convert structure to a JARVIS Atoms. Visit https://github.com/usnistgov/jarvis"


__all__ = ("get_jarvis_atoms",)


def get_jarvis_atoms(optimade_structure: OptimadeStructure) -> Atoms:
""" Get jarvis Atoms from OPTIMADE structure

NOTE: Cannot handle partial occupancies

:param optimade_structure: OPTIMADE structure
:return: jarvis.core.Atoms
"""
if globals().get("Atoms", None) is None:
warn(JARVIS_NOT_FOUND)
return None

attributes = optimade_structure.attributes

# Cannot handle partial occupancies
if "disorder" in attributes.structure_features:
raise ConversionError(
"jarvis-tools cannot handle structures with partial occupancies."
)

cartesian_site_positions, _ = pad_positions(attributes.cartesian_site_positions)

return Atoms(
lattice_mat=attributes.lattice_vectors,
elements=[specie.name for specie in attributes.species],
coords=cartesian_site_positions,
cartesian=True,
)
12 changes: 11 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
cif_deps = ["numpy~=1.18"]
pdb_deps = cif_deps
pymatgen_deps = ["pymatgen~=2020.3"]
jarvis_deps = ["jarvis-tools~=2020.6"]
client_deps = cif_deps

# General
Expand All @@ -27,7 +28,15 @@
"jsondiff",
] + server_deps
dev_deps = ["pylint", "black", "pre-commit", "invoke"] + testing_deps + client_deps
all_deps = dev_deps + django_deps + elastic_deps + aiida_deps + ase_deps + pymatgen_deps
all_deps = (
dev_deps
+ django_deps
+ elastic_deps
+ aiida_deps
+ ase_deps
+ pymatgen_deps
+ jarvis_deps
)

setup(
name="optimade",
Expand Down Expand Up @@ -76,6 +85,7 @@
"cif": cif_deps,
"pdb": pdb_deps,
"pymatgen": pymatgen_deps,
"jarvis": jarvis_deps,
},
entry_points={
"console_scripts": ["optimade_validator=optimade.validator:validate"]
Expand Down
53 changes: 53 additions & 0 deletions tests/adapters/structures/test_jarvis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# pylint: disable=import-error
import pytest

from .utils import get_min_ver

min_ver = get_min_ver("jarvis-tools")
jarvis = pytest.importorskip(
"jarvis",
minversion=min_ver,
reason=f"jarvis-tools must be installed with minimum version {min_ver} for these tests to"
" be able to run",
)

from jarvis.core.atoms import Atoms
from optimade.adapters import Structure
from optimade.adapters.exceptions import ConversionError
from optimade.adapters.structures.jarvis import get_jarvis_atoms


def test_successful_conversion(RAW_STRUCTURES):
"""Make sure its possible to convert"""
for structure in RAW_STRUCTURES:
assert isinstance(get_jarvis_atoms(Structure(structure)), Atoms)


def test_null_positions(null_position_structure):
"""Make sure null positions are handled"""
assert isinstance(get_jarvis_atoms(null_position_structure), Atoms)


def test_null_lattice_vectors(null_lattice_vector_structure):
"""Make sure null lattice vectors are handled"""
assert isinstance(get_jarvis_atoms(null_lattice_vector_structure), Atoms)


def test_special_species(SPECIAL_SPECIES_STRUCTURES):
"""Make sure vacancies and non-chemical symbols ("X") are handled"""
for special_structure in SPECIAL_SPECIES_STRUCTURES:
structure = Structure(special_structure)

# Since all the special species structure only have a single species, this works fine.
if len(structure.species[0].chemical_symbols) > 1:
# If the structure is disordered (has partial occupancies of any kind),
# jarvis-tools cannot convert the structure
with pytest.raises(
ConversionError,
match="jarvis-tools cannot handle structures with partial occupancies",
):
get_jarvis_atoms(structure)
else:
# No partial occupancies, just special/non-standard species.
# jarvis-tools should convert these structure fine enough.
assert isinstance(get_jarvis_atoms(structure), Atoms)
4 changes: 3 additions & 1 deletion tests/adapters/structures/test_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ase # noqa: F401
import numpy # noqa: F401
import pymatgen # noqa: F401
import jarvis # noqa: F401
except ImportError:
all_modules_found = False
else:
Expand Down Expand Up @@ -107,10 +108,11 @@ def test_no_module_conversion(self, structure):
"ase": ["ase"],
"numpy": ["cif", "pdb", "pdbx_mmcif"],
"pymatgen": ["pymatgen"],
"jarvis": ["jarvis"],
}

modules_to_test = []
for module in ("aiida", "ase", "numpy", "pymatgen"):
for module in ("aiida", "ase", "numpy", "pymatgen", "jarvis"):
try:
importlib.import_module(module)
except (ImportError, ModuleNotFoundError):
Expand Down