# pymatgen.ext namespace

## Submodules

## pymatgen.ext.cod module

This module provides classes to interface with the Crystallography Open Database. If you use data from the COD, please cite the following works (as stipulated by the COD developers).

> Merkys, A., Vaitkus, A., Butkus, J., Okulič-Kazarinas, M., Kairys, V. & Gražulis, S. (2016) “COD::CIF::Parser: an error-correcting CIF parser for the Perl language”. Journal of Applied Crystallography 49.

> Gražulis, S., Merkys, A., Vaitkus, A. & Okulič-Kazarinas, M. (2015) “Computing stoichiometric molecular composition from crystal structures”. Journal of Applied Crystallography 48, 85-91.

> Gražulis, S., Daškevič, A., Merkys, A., Chateigner, D., Lutterotti, L., Quirós, M., Serebryanaya, N. R., Moeck, P., Downs, R. T. & LeBail, A. (2012) “Crystallography Open Database (COD): an open-access collection of crystal structures and platform for world-wide collaboration”. Nucleic Acids Research 40, D420-D427.

> Grazulis, S., Chateigner, D., Downs, R. T., Yokochi, A. T., Quiros, M., Lutterotti, L., Manakova, E., Butkus, J., Moeck, P. & Le Bail, A. (2009) “Crystallography Open Database - an open-access collection of crystal structures”. J. Appl. Cryst. 42, 726-729.

> Downs, R. T. & Hall-Wallace, M. (2003) “The American Mineralogist Crystal Structure Database”. American Mineralogist 88, 247-250.

class COD(timeout: int = 60)

  Bases: `object`

  An interface to the Crystallography Open Database.

  Reference:

    https://wiki.crystallography.net/RESTful_API/

  Initialize the COD class.

  Parameters:

    timeout (int) – request timeout in seconds.

  get_cod_ids(formula: str) → list[int]

    Query the COD for all COD IDs associated with a formula.

    Parameters:

      formula (str) – The formula to request

  get_structure_by_formula(formula: str, **kwargs) → list[dict[Literal['structure', 'cod_id', 'sg'], str | int | Structure]]

    Query the COD for structures by formula.

    Parameters:

      - formula (str) – Chemical formula.

      - kwargs – All kwargs supported by Structure.from_str.

    Returns:

      {“structure”: Structure, “cod_id”: int, “sg”: “P n m a”}

    Return type:

      A list of dict of

  get_structure_by_id(cod_id: int, timeout: int | None = None, **kwargs) → Structure

    Query the COD for a structure by ID.

    Parameters:

      - cod_id (int) – COD ID.

      - timeout (int) – DEPRECATED. request timeout in seconds.

      - kwargs – kwargs passed to Structure.from_str.

    Returns:

      A Structure.

## pymatgen.ext.matproj module

This module provides classes to interface with the Materials Project REST API v2 to enable the creation of data structures and pymatgen objects using Materials Project data.

To make use of the Materials API, you need to be a registered user of the Materials Project, and obtain an API key by going to your dashboard at https://materialsproject.org/dashboard.

exception MPRestError

  Bases: `Exception`

  Exception class for legacy MPRestAdaptor. Raised when query is malformed.

class MPRester(*args, **kwargs)

  Bases: `object`

  A class to conveniently interface with the new and legacy Materials Project REST interface.

  The recommended way to use MPRester is as a context manager to ensure that sessions are properly closed after usage:

  > with MPRester(“API_KEY”) as mpr:

  >   docs = mpr.call_some_method()

  MPRester uses the “requests” package, which provides HTTP connection pooling. All connections are made via https for security.

  For more advanced uses of the Materials API, please consult the API documentation at https://materialsproject.org/api and https://docs.materialsproject.org.

  This class handles the transition between old and new MP API, making it easy to switch between them by passing a new (length 32) or old (15 <= length <= 17) API key. See https://docs.materialsproject.org for which API to use.

  Parameters:

    - *args – Pass through to either legacy or new MPRester.

    - **kwargs – Pass through to either legacy or new MPRester.

## pymatgen.ext.matproj_legacy module

This module provides classes to interface with the Materials Project REST API v1 to enable the creation of data structures and pymatgen objects using Materials Project data.

exception MPRestError

  Bases: `Exception`

  Exception class for legacy MPRestAdaptor. Raised when query is malformed.

class TaskType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)

  Bases: `Enum`

  task types available in legacy MP data.

  GGAU_DEF = 'GGA+U Deformation'
  GGAU_LINE = 'GGA+U NSCF Line'
  GGAU_OPT = 'GGA+U Structure Optimization'
  GGAU_STATIC = 'GGA+U Static'
  GGAU_STATIC_DIEL = 'GGA+U Static Dielectric'
  GGAU_UNIFORM = 'GGA+U NSCF Uniform'
  GGA_DEF = 'GGA Deformation'
  GGA_LINE = 'GGA NSCF Line'
  GGA_OPT = 'GGA Structure Optimization'
  GGA_STATIC = 'GGA Static'
  GGA_STATIC_DIEL = 'GGA Static Dielectric'
  GGA_UNIFORM = 'GGA NSCF Uniform'
  LDA_STATIC_DIEL = 'LDA Static Dielectric'
  SCAN_OPT = 'SCAN Structure Optimization'

get_chunks(sequence: Sequence[Any], size=1)
  Parameters:

    - sequence (Sequence[Any]) – Any sequence.

    - size (int) – Chunk length. Defaults to 1.

  Returns:

    input sequence in chunks of length size.

  Return type:

    list[Sequence[Any]]

## pymatgen.ext.optimade module

Optimade support.

class OptimadeRester(aliases_or_resource_urls: str | list[str] | None = None, refresh_aliases: bool = False, timeout: int = 5)

  Bases: `object`

  Call OPTIMADE-compliant APIs, see https://optimade.org and [1].

  This class is ready to use but considered in-development and subject to change.

  Please also consider using the client in “OPTIMADE Python tools”:

  https://www.optimade.org/optimade-python-tools/latest/getting_started/client/

  The “OPTIMADE Python tools” client is less integrated with pymatgen, but more actively developed for the latest OPTIMADE features.

  [1] Andersen, C.W., et al.

    OPTIMADE, an API for exchanging materials data. Sci Data 8, 217 (2021). https://doi.org/10.1038/s41597-021-00974-z

  OPTIMADE is an effort to provide a standardized interface to retrieve information from many different materials science databases.

  This is a client to retrieve structures from OPTIMADE v1 compliant endpoints. It does not yet support all features of the OPTIMADE v1 specification but is intended as a way to quickly search an endpoint in a way familiar to users of pymatgen without needing to know the full OPTIMADE specification.

  For advanced usage, please see the OPTIMADE documentation at optimade.org and consider calling the APIs directly.

  For convenience, known OPTIMADE endpoints have been given aliases in pymatgen to save typing the full URL.

  To get an up-to-date list aliases, generated from the current list of OPTIMADE providers at optimade.org, call the refresh_aliases() method or pass refresh_aliases=True when creating instances of this class.

  Parameters:

    - aliases_or_resource_urls – the alias or structure resource URL or a list of

    - URLs (aliases or resource)

    - not (if providing the resource URL directly it should)

    - index (be an)

    - "v1/structures" (this interface can only currently access the)

    - URL (information from the specified resource)

    - refresh_aliases – if True, use an up-to-date list of providers/aliases from the live

    - https (list of OPTIMADE providers hosted at) – //providers.optimade.org.

    - timeout – number of seconds before an attempted request is abandoned, a good

    - providers (timeout is useful when querying many)

    - offline (some of which may be)

  aliases: ClassVar[dict[str, str]] = {'aflow': 'https://aflow.org/API/optimade/', 'alexandria': 'https://alexandria.icams.rub.de/pbe', 'alexandria.pbe': 'https://alexandria.icams.rub.de/pbe', 'alexandria.pbesol': 'https://alexandria.icams.rub.de/pbesol', 'cmr': 'https://cmr-optimade.fysik.dtu.dk', 'cod': 'https://www.crystallography.net/cod/optimade', 'jarvis': 'https://jarvis.nist.gov/optimade/jarvisdft', 'mcloud.2dtopo': 'https://aiida.materialscloud.org/2dtopo/optimade', 'mcloud.curated-cofs': 'https://aiida.materialscloud.org/curated-cofs/optimade', 'mcloud.mc2d': 'https://aiida.materialscloud.org/mc2d/optimade', 'mcloud.mc3d': 'https://aiida.materialscloud.org/mc3d/optimade', 'mcloud.optimade-sample': 'https://aiida.materialscloud.org/optimade-sample/optimade', 'mcloud.pyrene-mofs': 'https://aiida.materialscloud.org/pyrene-mofs/optimade', 'mcloud.scdm': 'https://aiida.materialscloud.org/autowannier/optimade', 'mcloud.stoceriaitf': 'https://aiida.materialscloud.org/stoceriaitf/optimade', 'mcloud.tc-applicability': 'https://aiida.materialscloud.org/tc-applicability/optimade', 'mcloud.tin-antimony-sulfoiodide': 'https://aiida.materialscloud.org/tin-antimony-sulfoiodide/optimade', 'mp': 'https://optimade.materialsproject.org', 'mpdd': 'http://mpddoptimade.phaseslab.org', 'mpds': 'https://api.mpds.io', 'nmd': 'https://nomad-lab.eu/prod/rae/optimade/', 'odbx': 'https://optimade.odbx.science', 'odbx.gnome': 'https://optimade-gnome.odbx.science', 'odbx.odbx_misc': 'https://optimade-misc.odbx.science', 'omdb.omdb_production': 'https://optimade.openmaterialsdb.se', 'oqmd': 'https://oqmd.org/optimade/', 'tcod': 'https://www.crystallography.net/tcod/optimade', 'twodmatpedia': 'http://optimade.2dmatpedia.org'}
  describe()

    Human-readable information about the resources being searched by the OptimadeRester.

  get_snls(elements: list[str] | str | None = None, nelements: int | None = None, nsites: int | None = None, chemical_formula_anonymous: str | None = None, chemical_formula_hill: str | None = None, additional_response_fields: str | list[str] | set[str] | None = None) → dict[str, dict[str, StructureNL]]

    Retrieve StructureNL from OPTIMADE providers.

    A StructureNL is an object provided by pymatgen which combines Structure with associated metadata, such as the URL is was downloaded from and any additional namespaced data.

    Not all functionality of OPTIMADE is currently exposed in this convenience method. To use a custom filter, call get_structures_with_filter().

    Parameters:

      - elements – List of elements

      - nelements – Number of elements, e.g. 4 or [2, 5] for the range >=2 and <=5

      - nsites – Number of sites, e.g. 4 or [2, 5] for the range >=2 and <=5

      - chemical_formula_anonymous – The desired chemical formula in OPTIMADE anonymous formula format

      - format (this is different from the pymatgen)

      - "A2BC"). (e.g. pymatgen "ABC2" should become)

      - chemical_formula_hill – The desired chemical formula in the OPTIMADE take on the Hill formula format.

      - Again ((NB.)

      - format

      - chemical (as the OPTIMADE version is a reduced)

      - ordering.) (formula simply using the IUPAC/Hill)

      - additional_response_fields – Any additional fields desired from the OPTIMADE API,

      - dictionary. (these will be stored under the '_optimade' key in each StructureNL.data)

    Returns:

      keyed by that database provider’s id system

    Return type:

      dict[str, StructureNL]

  get_snls_with_filter(optimade_filter: str, additional_response_fields: str | list[str] | set[str] | None = None) → dict[str, dict[str, StructureNL]]

    Get structures satisfying a given OPTIMADE filter.

    Parameters:

      - optimade_filter – An OPTIMADE-compliant filter

      - additional_response_fields – Any additional fields desired from the OPTIMADE API,

    Returns:

      keyed by that database provider’s id system

    Return type:

      dict[str, Structure]

  get_structures(elements: list[str] | str | None = None, nelements: int | None = None, nsites: int | None = None, chemical_formula_anonymous: str | None = None, chemical_formula_hill: str | None = None) → dict[str, dict[str, Structure]]

    Retrieve Structures from OPTIMADE providers.

    Not all functionality of OPTIMADE is currently exposed in this convenience method. To use a custom filter, call get_structures_with_filter().

    Parameters:

      - elements – List of elements

      - nelements – Number of elements, e.g. 4 or [2, 5] for the range >=2 and <=5

      - nsites – Number of sites, e.g. 4 or [2, 5] for the range >=2 and <=5

      - chemical_formula_anonymous – The desired chemical formula in OPTIMADE anonymous formula format

      - format (this is different from the pymatgen)

      - "A2BC"). (e.g. pymatgen "ABC2" should become)

      - chemical_formula_hill – The desired chemical formula in the OPTIMADE take on the Hill formula format.

      - Again ((NB.)

      - format

      - chemical (as the OPTIMADE version is a reduced)

      - ordering.) (formula simply using the IUPAC/Hill)

    Returns:

      keyed by that database provider’s id system

    Return type:

      dict[str, Structure]

  get_structures_with_filter(optimade_filter: str) → dict[str, dict[str, Structure]]

    Get structures satisfying a given OPTIMADE filter.

    Parameters:

      optimade_filter – An OPTIMADE-compliant filter

    Returns:

      keyed by that database provider’s id system

    Return type:

      dict[str, Structure]

  mandatory_response_fields = ('lattice_vectors', 'cartesian_site_positions', 'species', 'species_at_sites')
  refresh_aliases(providers_url='https://providers.optimade.org/providers.json')

    Update available OPTIMADE structure resources based on the current list of OPTIMADE providers.

class Provider(name: str, base_url: str, description: str, homepage: str, prefix: str)

  Bases: `NamedTuple`

  TODO: Import optimade-python-tool’s data structures will make more sense.

  Create new instance of Provider(name, base_url, description, homepage, prefix)

  base_url: str

    Alias for field number 1

  description: str

    Alias for field number 2

  homepage: str

    Alias for field number 3

  name: str

    Alias for field number 0

  prefix: str

    Alias for field number 4