11from __future__ import annotations
22
33import itertools
4+ import json
5+ import os
46import warnings
57from functools import cache , lru_cache
68from json import loads
7- from os import environ
89from typing import Literal
910
1011from emmet .core .electronic_structure import BSPathType
1112from emmet .core .mpid import MPID
1213from emmet .core .settings import EmmetSettings
1314from emmet .core .tasks import TaskDoc
1415from emmet .core .vasp .calc_types import CalcType
16+ from monty .json import MontyDecoder
1517from packaging import version
1618from pymatgen .analysis .phase_diagram import PhaseDiagram
1719from pymatgen .analysis .pourbaix_diagram import IonEntry
3941 ElectrodeRester ,
4042 ElectronicStructureRester ,
4143 EOSRester ,
42- FermiRester ,
4344 GrainBoundaryRester ,
4445 MagnetismRester ,
4546 OxidationStatesRester ,
6667)
6768
6869_EMMET_SETTINGS = EmmetSettings () # type: ignore
69- _MAPI_SETTINGS = MAPIClientSettings () # type: ignore
70+ _MAPI_SETTINGS = MAPIClientSettings () # typeL ignore # type: ignore
7071
71- DEFAULT_API_KEY = environ .get ("MP_API_KEY" , None )
72- DEFAULT_ENDPOINT = environ .get ("MP_API_ENDPOINT" , "https://api.materialsproject.org/" )
72+ DEFAULT_API_KEY = os .environ .get ("MP_API_KEY" , None )
73+ DEFAULT_ENDPOINT = os .environ .get (
74+ "MP_API_ENDPOINT" , "https://api.materialsproject.org/"
75+ )
7376
7477
7578class MPRester :
@@ -86,8 +89,7 @@ class MPRester:
8689 similarity : SimilarityRester
8790 tasks : TaskRester
8891 xas : XASRester
89- fermi : FermiRester
90- grain_boundary : GrainBoundaryRester
92+ grain_boundaries : GrainBoundaryRester
9193 substrates : SubstratesRester
9294 surface_properties : SurfacePropertiesRester
9395 phonon : PhononRester
@@ -195,7 +197,7 @@ def __init__(
195197 "tasks" ,
196198 "xas" ,
197199 "fermi" ,
198- "grain_boundary " ,
200+ "grain_boundaries " ,
199201 "substrates" ,
200202 "surface_properties" ,
201203 "phonon" ,
@@ -340,7 +342,7 @@ def __molecules_getattr__(_self, attr):
340342 return rester
341343
342344 MaterialsRester .__getattr__ = __materials_getattr__ # type: ignore
343- MoleculeRester .__getattr__ = __molecules_getattr__ # type: ignore
345+ MoleculeRester .__getattr__ = __molecules_getattr__ # type: ignore
344346
345347 for attr , rester in core_resters .items ():
346348 setattr (
@@ -598,14 +600,15 @@ def get_structures(
598600 input_params = {"formula" : chemsys_formula }
599601
600602 if final :
601- return [
602- doc .structure if self .use_document_model else doc ["structure" ] # type: ignore
603- for doc in self .materials .search (
604- ** input_params , # type: ignore
605- all_fields = False ,
606- fields = ["structure" ],
607- )
608- ]
603+ docs = self .materials .search (
604+ ** input_params , # type: ignore
605+ all_fields = False ,
606+ fields = ["structure" ],
607+ )
608+ if not self .use_document_model :
609+ return [doc ["structure" ] for doc in docs ] # type: ignore
610+
611+ return [doc .structure for doc in docs ] # type: ignore
609612 else :
610613 structures = []
611614
@@ -614,11 +617,12 @@ def get_structures(
614617 all_fields = False ,
615618 fields = ["initial_structures" ],
616619 ):
617- structures . extend (
620+ initial_structures = (
618621 doc .initial_structures # type: ignore
619622 if self .use_document_model
620623 else doc ["initial_structures" ] # type: ignore
621624 )
625+ structures .extend (initial_structures )
622626
623627 return structures
624628
@@ -1301,7 +1305,7 @@ def get_wulff_shape(self, material_id: str):
13011305 )
13021306 miller_energy_map = {}
13031307 for surf in surfaces :
1304- miller = tuple (surf .miller_index )
1308+ miller = tuple (surf .miller_index ) if surf . miller_index else ()
13051309 # Prefer reconstructed surfaces, which have lower surface energies.
13061310 if (miller not in miller_energy_map ) or surf .is_reconstructed :
13071311 miller_energy_map [miller ] = surf .surface_energy
@@ -1339,20 +1343,22 @@ def get_charge_density_from_material_id(
13391343 else x ["last_updated" ], # type: ignore
13401344 )
13411345
1342- result = (
1346+ decoder = MontyDecoder ().decode if self .monty_decode else json .loads
1347+ chgcar = (
13431348 self .tasks ._query_open_data (
13441349 bucket = "materialsproject-parsed" ,
1345- prefix = "chgcars" ,
1346- key = str (latest_doc .task_id ),
1347- )
1350+ key = f"chgcars/{ str (latest_doc .task_id )} .json.gz" ,
1351+ decoder = decoder ,
1352+ fields = ["data" ],
1353+ )[0 ]
13481354 or {}
13491355 )
13501356
1351- chgcar = result .get ("data" , None )
1352-
1353- if chgcar is None :
1357+ if not chgcar :
13541358 raise MPRestError (f"No charge density fetched for { material_id } ." )
13551359
1360+ chgcar = chgcar [0 ]["data" ] # type: ignore
1361+
13561362 if inc_task_doc :
13571363 task_doc = self .tasks .search (
13581364 task_ids = latest_doc .task_id
@@ -1384,7 +1390,7 @@ def get_download_info(self, material_ids, calc_types=None, file_patterns=None):
13841390 )
13851391
13861392 meta = {}
1387- for doc in self .materials .search (
1393+ for doc in self .materials .search ( # type: ignore
13881394 task_ids = material_ids ,
13891395 fields = ["calc_types" , "deprecated_tasks" , "material_id" ],
13901396 ):
0 commit comments