Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/materialsproject/api into main
Browse files Browse the repository at this point in the history
  • Loading branch information
munrojm committed May 4, 2021
2 parents 50106da + 93b556e commit 116d0b3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
33 changes: 27 additions & 6 deletions src/mp_api/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class BaseRester:
suffix: Optional[str] = None
document_model: Optional[BaseModel] = None
supports_versions: bool = False
primary_key: str = "material_id"

def __init__(
self,
Expand Down Expand Up @@ -407,12 +408,32 @@ def get_document_by_id(
if isinstance(fields, str):
fields = (fields,)

results = self.query(
criteria=criteria,
fields=fields,
monty_decode=monty_decode,
suburl=document_id,
)
try:
results = self.query(
criteria=criteria,
fields=fields,
monty_decode=monty_decode,
suburl=document_id,
)
except MPRestError:

if self.primary_key == "material_id":
# see if the material_id has changed, perhaps a task_id was supplied
# this should likely be re-thought
from mp_api.matproj import MPRester
with MPRester() as mpr:
new_document_id = mpr.get_materials_id_from_task_id(document_id)
warnings.warn(f"Document primary key has changed from {document_id} to {new_document_id}, "
f"returning data for {document_id} in {self.suffix} route. ")
document_id = new_document_id

results = self.query(
criteria=criteria,
fields=fields,
monty_decode=monty_decode,
suburl=document_id,
)


if not results:
warnings.warn(f"No result for record {document_id}.")
Expand Down
19 changes: 15 additions & 4 deletions src/mp_api/matproj.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,29 @@ def get_database_version(self):
"""
raise NotImplementedError

def get_materials_id_from_task_id(self, task_id):
def get_materials_id_from_task_id(self, task_id, version=None):
"""
Returns a new MP materials id from a task id (which can be
equivalent to an old materials id)
Returns the current material_id from a given task_id. The
materials_id should rarely change, and is usually chosen from
among the smallest numerical id from the group of task_ids for
that material. However, in some circumstances it might change,
and this method is useful for finding the new material_id.
Args:
task_id (str): A task id.
version (str): Specific database version to query.
Returns:
materials_id (str)
"""
raise NotImplementedError
docs = self.materials.search(task_ids=[task_id], fields=["material_id"], version=version)
if len(docs) == 1:
return docs[0].material_id
elif len(docs) > 1:
raise ValueError(f"Multiple documents return for {task_id}, this should not happen, please report it!")
else:
warnings.warn(f"No material found containing task {task_id}. Please report it if you suspect a task has gone missing.")
return None

def get_materials_ids(self, chemsys_formula):
"""
Expand Down

0 comments on commit 116d0b3

Please sign in to comment.