Source code for mp_api.client.routes.materials.tasks

from __future__ import annotations

import warnings

from emmet.core.tasks import TaskDoc

from mp_api.client.core import BaseRester, MPRestError
from mp_api.client.core.utils import validate_ids


[docs] class TaskRester(BaseRester[TaskDoc]): suffix = "materials/tasks" document_model = TaskDoc # type: ignore primary_key = "task_id"
[docs] def get_trajectory(self, task_id): """Returns a Trajectory object containing the geometry of the material throughout a calculation. This is most useful for observing how a material relaxes during a geometry optimization. :param task_id: A specified task_id :return: List of trajectory objects """ traj_data = self._query_resource_data( suburl=f"trajectory/{task_id}/", use_document_model=False )[0].get("trajectories", None) if traj_data is None: raise MPRestError(f"No trajectory data for {task_id} found") return traj_data
[docs] def search_task_docs(self, *args, **kwargs): # pragma: no cover """Deprecated.""" warnings.warn( "MPRester.tasks.search_task_docs is deprecated. " "Please use MPRester.tasks.search instead.", DeprecationWarning, stacklevel=2, ) return self.search(*args, **kwargs)
[docs] def search( self, task_ids: list[str] | None = None, chemsys: str | list[str] | None = None, elements: list[str] | None = None, exclude_elements: list[str] | None = None, formula: str | list[str] | None = None, num_chunks: int | None = None, chunk_size: int = 1000, all_fields: bool = True, fields: list[str] | None = None, ): """Query core task docs using a variety of search criteria. Arguments: task_ids (List[str]): List of Materials Project IDs to return data for. chemsys (str, List[str]): A chemical system or list of chemical systems (e.g., Li-Fe-O, Si-*, [Si-O, Li-Fe-P]). elements (List[str]): A list of elements. exclude_elements (List[str]): A list of elements to exclude. formula (str, List[str]): A formula including anonymized formula or wild cards (e.g., Fe2O3, ABO3, Si*). A list of chemical formulas can also be passed (e.g., [Fe2O3, ABO3]). num_chunks (int): Maximum number of chunks of data to yield. None will yield all possible. chunk_size (int): Number of data entries per chunk. Max size is 100. all_fields (bool): Whether to return all fields in the document. Defaults to True. fields (List[str]): List of fields in TaskDoc to return data for. Default is material_id, last_updated, and formula_pretty if all_fields is False. Returns: ([TaskDoc]) List of task documents """ query_params = {} # type: dict if task_ids: query_params.update({"task_ids": ",".join(validate_ids(task_ids))}) if formula: query_params.update({"formula": formula}) if elements: query_params.update({"elements": ",".join(elements)}) if exclude_elements: query_params.update({"exclude_elements": ",".join(exclude_elements)}) if chemsys: if isinstance(chemsys, str): chemsys = [chemsys] query_params.update({"chemsys": ",".join(chemsys)}) return super()._search( num_chunks=num_chunks, chunk_size=chunk_size, all_fields=all_fields, fields=fields, **query_params, )