Skip to content

Commit

Permalink
Endpoint updates (#326)
Browse files Browse the repository at this point in the history
* Update phonon endpoint

* Fix tests and include explicit methods in mprester

* Linting

* Fix phonon client stubs

* Skip client tests if no API key found.

* Electrode client test xfail

* Add chemsys to materials util test

* Add wildcard to materials utils test

* Enable bare response on most endpoints

* Increase rate limit setting

* Lower request rate limit setting
  • Loading branch information
Jason Munro authored Jul 8, 2021
1 parent 428f1dd commit 959dedc
Show file tree
Hide file tree
Showing 50 changed files with 269 additions and 247 deletions.
24 changes: 4 additions & 20 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
)
magnetism_store_json = os.environ.get("MAGNETISM_STORE", "magnetism_store.json")
phonon_bs_store_json = os.environ.get("PHONON_BS_STORE", "phonon_bs_store.json")
phonon_img_store_json = os.environ.get("PHONON_IMG_STORE", "phonon_img_store.json")
eos_store_json = os.environ.get("EOS_STORE", "eos_store.json")
similarity_store_json = os.environ.get("SIMILARITY_STORE", "similarity_store.json")
xas_store_json = os.environ.get("XAS_STORE", "xas_store.json")
Expand Down Expand Up @@ -118,15 +117,8 @@
phonon_bs_store = MongoURIStore(
uri=f"mongodb+srv://{db_uri}",
database="mp_core",
key="task_id",
collection_name="phonon_bs",
)

phonon_img_store = MongoURIStore(
uri=f"mongodb+srv://{db_uri}",
database="mp_core",
key="task_id",
collection_name="phonon_img",
key="material_id",
collection_name="pmg_ph_bs",
)

eos_store = MongoURIStore(
Expand Down Expand Up @@ -317,7 +309,6 @@
dielectric_piezo_store = loadfn(dielectric_piezo_store_json)
magnetism_store = loadfn(magnetism_store_json)
phonon_bs_store = loadfn(phonon_bs_store_json)
phonon_img_store = loadfn(phonon_img_store_json)
eos_store = loadfn(eos_store_json)
similarity_store = loadfn(similarity_store_json)
xas_store = loadfn(xas_store_json)
Expand Down Expand Up @@ -405,16 +396,9 @@
resources.update({"piezoelectric": [piezo_resource(dielectric_piezo_store)]})

# Phonon
from mp_api.routes.phonon.resources import phonon_bs_resource, phonon_img_resource
from mp_api.routes.phonon.resources import phonon_bsdos_resource

resources.update(
{
"phonon": [
phonon_img_resource(phonon_img_store),
phonon_bs_resource(phonon_bs_store),
]
}
)
resources.update({"phonon": [phonon_bsdos_resource(phonon_bs_store),]})

# EOS
from mp_api.routes.eos.resources import eos_resource
Expand Down
167 changes: 81 additions & 86 deletions src/mp_api/matproj.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from os import environ
from os import environ, path
import warnings
from typing import Optional, Tuple, List
from enum import Enum, unique
Expand All @@ -11,6 +11,7 @@
from emmet.core.symmetry import CrystalSystem

from mp_api.core.client import BaseRester
from mp_api.routes.electronic_structure.models.core import BSPathType
from mp_api.routes import *

_DEPRECATION_WARNING = (
Expand Down Expand Up @@ -106,27 +107,18 @@ def __init__(
self, cls.suffix.replace("/", "_"), rester,
)

self.find_structure = self.materials.find_structure

self.get_bandstructure_by_material_id = (
self.electronic_structure_bandstructure.get_bandstructure_from_material_id
)
self.get_dos_by_material_id = (
self.electronic_structure_dos.get_dos_from_material_id
)

self.get_charge_density_from_calculation_id = (
self.charge_density.get_charge_density_from_calculation_id
)

self.get_charge_density_calculation_details = (
self.charge_density.get_charge_density_calculation_details
)

self.get_charge_density_calculation_ids_from_material_id = (
self.charge_density.get_charge_density_calculation_ids_from_material_id
)

self.get_charge_density_calculation_details = (
self.charge_density.get_charge_density_calculation_details
)

def __enter__(self):
"""
Support for "with" context.
Expand Down Expand Up @@ -287,6 +279,32 @@ def get_structures(self, chemsys_formula, final=True):

return structures

def find_structure(
self, filename_or_structure, ltol=0.2, stol=0.3, angle_tol=5, limit=1
):
"""
Finds matching structures on the Materials Project site.
Args:
filename_or_structure: filename or Structure object
ltol: fractional length tolerance
stol: site tolerance
angle_tol: angle tolerance in degrees
limit: amount of structure matches to limit to
Returns:
A list of matching materials project ids for structure, \
including normalized rms distances and max distances between paired sites.
Raises:
MPRestError
"""

return self.materials.find_structure(
filename_or_structure,
ltol=ltol,
stol=stol,
angle_tol=angle_tol,
limit=limit,
)

def get_entries(
self, chemsys_formula, sort_by_e_above_hull=False,
):
Expand Down Expand Up @@ -343,6 +361,41 @@ def get_entry_by_material_id(self, material_id):
).entries.values()
)

def get_bandstructure_by_material_id(
self,
material_id: str,
path_type: BSPathType = BSPathType.setyawan_curtarolo,
line_mode=True,
):
"""
Get the band structure pymatgen object associated with a Materials Project ID.
Arguments:
materials_id (str): Materials Project ID for a material
path_type (BSPathType): k-point path selection convention
line_mode (bool): Whether to return data for a line-mode calculation
Returns:
bandstructure (Union[BandStructure, BandStructureSymmLine]): BandStructure or BandStructureSymmLine object
"""
return self.electronic_structure_bandstructure.get_bandstructure_from_material_id( # type: ignore
material_id=material_id, path_type=path_type, line_mode=line_mode
)

def get_dos_by_material_id(self, material_id: str):
"""
Get the complete density of states pymatgen object associated with a Materials Project ID.
Arguments:
materials_id (str): Materials Project ID for a material
Returns:
dos (CompleteDos): CompleteDos object
"""
return self.electronic_structure_dos.get_dos_from_material_id( # type: ignore
material_id=material_id
)

def get_phonon_dos_by_material_id(self, material_id):
"""
Get phonon density of states data corresponding to a material_id.
Expand All @@ -354,7 +407,7 @@ def get_phonon_dos_by_material_id(self, material_id):
CompletePhononDos: A phonon DOS object.
"""
raise NotImplementedError
return self.phonon.get_document_by_id(material_id, fields=["ph_dos"]).ph_dos

def get_phonon_bandstructure_by_material_id(self, material_id):
"""
Expand All @@ -366,9 +419,20 @@ def get_phonon_bandstructure_by_material_id(self, material_id):
Returns:
PhononBandStructureSymmLine: phonon band structure.
"""
doc = self.phonon.get_document_by_id(material_id)
return self.phonon.get_document_by_id(material_id, fields=["ph_bs"]).ph_bs

return doc.ph_bs
def get_charge_density_by_material_id(self, material_id: str):
"""
Get charge density data for a given Materials Project ID.
Arguments:
material_id (str): Material Project ID
Returns:
chgcar (dict): Pymatgen CHGCAR object.
"""

return self.charge_density.get_charge_density_from_material_id(material_id) # type: ignore

def query(
self,
Expand Down Expand Up @@ -559,39 +623,6 @@ def submit_structures(self, structures, public_name, public_email):
# TODO: call new MPComplete endpoint
raise NotImplementedError

def get_stability(self, entries):
"""
Returns the stability of all entries.
"""
# TODO: discuss
raise NotImplementedError

def get_cohesive_energy(self, material_id, per_atom=False):
"""
Gets the cohesive for a material (eV per formula unit). Cohesive energy
is defined as the difference between the bulk energy and the sum of
total DFT energy of isolated atoms for atom elements in the bulk.
Args:
material_id (str): Materials Project material_id, e.g. 'mp-123'.
per_atom (bool): Whether or not to return cohesive energy per atom
Returns:
Cohesive energy (eV).
"""
raise NotImplementedError

def get_reaction(self, reactants, products):
"""
Gets a reaction from the Materials Project.
Args:
reactants ([str]): List of formulas
products ([str]): List of formulas
Returns:
rxn
"""
raise NotImplementedError

def get_substrates(self, material_id, orient=None):
"""
Get a substrate list for a material id. The list is in order of
Expand Down Expand Up @@ -720,39 +751,3 @@ def get_gb_data(
rotation_axis=rotation_axis,
)
]

def get_interface_reactions(
self,
reactant1,
reactant2,
open_el=None,
relative_mu=None,
use_hull_energy=False,
):
"""
Gets critical reactions between two reactants.
Get critical reactions ("kinks" in the mixing ratio where
reaction products change) between two reactants. See the
`pymatgen.analysis.interface_reactions` module for more info.
Args:
reactant1 (str): Chemical formula for reactant
reactant2 (str): Chemical formula for reactant
open_el (str): Element in reservoir available to system
relative_mu (float): Relative chemical potential of element in
reservoir with respect to pure substance. Must be non-positive.
use_hull_energy (bool): Whether to use the convex hull energy for a
given composition for the reaction energy calculation. If false,
the energy of the ground state structure will be preferred; if a
ground state can not be found for a composition, the convex hull
energy will be used with a warning message.
Returns:
list: list of dicts of form {ratio,energy,rxn} where `ratio` is the
reactant mixing ratio, `energy` is the reaction energy
in eV/atom, and `rxn` is a
`pymatgen.analysis.reaction_calculator.Reaction`.
"""
raise NotImplementedError
2 changes: 1 addition & 1 deletion src/mp_api/routes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from mp_api.routes.substrates.client import SubstratesRester
from mp_api.routes.surface_properties.client import SurfacePropertiesRester
from mp_api.routes.wulff.client import WulffRester
from mp_api.routes.phonon.client import PhononRester, PhononImgRester
from mp_api.routes.phonon.client import PhononRester
from mp_api.routes.elasticity.client import ElasticityRester
from mp_api.routes.thermo.client import ThermoRester
from mp_api.routes.dielectric.client import DielectricRester
Expand Down
28 changes: 27 additions & 1 deletion src/mp_api/routes/charge_density/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,33 @@ def get_charge_density_calculation_ids_from_material_id(self, material_id: str):

if len(calculation_ids) > 0:
result = self.search(
task_ids=",".join(calculation_ids), fields=["task_id"], chunk_size=10
task_ids=",".join(calculation_ids),
fields=["task_id", "last_updated"],
chunk_size=10,
)

return result

def get_charge_density_from_material_id(self, material_id: str):
"""
Get charge density data for a given Materials Project ID.
Arguments:
material_id (str): Material Project ID
Returns:
chgcar (dict): Pymatgen CHGCAR object.
"""

task_id = sorted(
self.get_charge_density_calculation_ids_from_material_id(material_id),
key=lambda d: d["last_updated"],
reverse=True,
)[0]["task_id"]

result = self.search(task_ids=task_id, fields=["task_id", "data"], chunk_size=1)

if len(result) > 0:
return result[0]["data"]
else:
raise MPRestError("No charge density found")
5 changes: 4 additions & 1 deletion src/mp_api/routes/dielectric/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ def dielectric_resource(dielectric_store):
DielectricQuery(),
SortQuery(),
PaginationQuery(),
SparseFieldsQuery(DielectricDoc, default_fields=["task_id", "last_updated"]),
SparseFieldsQuery(
DielectricDoc, default_fields=["task_id", "last_updated"]
),
],
tags=["Dielectric"],
monty_encoded_response=True,
)

return resource
1 change: 1 addition & 0 deletions src/mp_api/routes/dois/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def dois_resource(dois_store):
],
tags=["DOIs"],
enable_default_search=False,
monty_encoded_response=True,
)

return resource
5 changes: 4 additions & 1 deletion src/mp_api/routes/elasticity/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ def elasticity_resource(elasticity_store):
PoissonQuery(),
SortQuery(),
PaginationQuery(),
SparseFieldsQuery(ElasticityDoc, default_fields=["task_id", "pretty_formula"],),
SparseFieldsQuery(
ElasticityDoc, default_fields=["task_id", "pretty_formula"],
),
],
tags=["Elasticity"],
monty_encoded_response=True,
)

return resource
3 changes: 3 additions & 0 deletions src/mp_api/routes/electronic_structure/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def es_resource(es_store):
),
],
tags=["Electronic Structure"],
monty_encoded_response=True,
)

return resource
Expand All @@ -54,6 +55,7 @@ def bs_resource(es_store):
tags=["Electronic Structure"],
enable_get_by_key=False,
sub_path="/bandstructure/",
monty_encoded_response=True,
)

return resource
Expand Down Expand Up @@ -92,6 +94,7 @@ def dos_resource(es_store):
tags=["Electronic Structure"],
enable_get_by_key=False,
sub_path="/dos/",
monty_encoded_response=True,
)

return resource
Expand Down
1 change: 1 addition & 0 deletions src/mp_api/routes/eos/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def eos_resource(eos_store):
SparseFieldsQuery(EOSDoc, default_fields=["task_id"]),
],
tags=["EOS"],
monty_encoded_response=True,
)

return resource
Loading

0 comments on commit 959dedc

Please sign in to comment.