11from __future__ import annotations
22
33from collections import defaultdict
4+ from typing import TYPE_CHECKING
45
56import numpy as np
67from emmet .core .phonon import PhononBS , PhononBSDOSDoc , PhononDOS
78
89from mp_api .client .core import BaseRester , MPRestError
910from mp_api .client .core .utils import validate_ids
1011
12+ if TYPE_CHECKING :
13+ from typing import Any
14+ from emmet .core .math import Matrix3D
15+
1116
1217class PhononRester (BaseRester ):
1318 suffix = "materials/phonon"
@@ -63,7 +68,7 @@ def search(
6368 ** query_params ,
6469 )
6570
66- def get_bandstructure_from_material_id (self , material_id : str , phonon_method : str ):
71+ def get_bandstructure_from_material_id (self , material_id : str , phonon_method : str ) -> PhononBS | dict [ str , Any ] :
6772 """Get the phonon band structure pymatgen object associated with a given material ID and phonon method.
6873
6974 Arguments:
@@ -73,10 +78,13 @@ def get_bandstructure_from_material_id(self, material_id: str, phonon_method: st
7378 Returns:
7479 bandstructure (PhononBS): PhononBS object
7580 """
76- result = self ._query_open_data (
77- bucket = "materialsproject-parsed" ,
78- key = f"ph-bandstructures/{ phonon_method } /{ material_id } .json.gz" ,
79- )[0 ]
81+ try :
82+ result = self ._query_open_data (
83+ bucket = "materialsproject-parsed" ,
84+ key = f"ph-bandstructures/{ phonon_method } /{ material_id } .json.gz" ,
85+ )[0 ]
86+ except OSError :
87+ result = None
8088
8189 if not result or not result [0 ]:
8290 raise MPRestError ("No object found" )
@@ -86,7 +94,7 @@ def get_bandstructure_from_material_id(self, material_id: str, phonon_method: st
8694
8795 return result [0 ]
8896
89- def get_dos_from_material_id (self , material_id : str , phonon_method : str ):
97+ def get_dos_from_material_id (self , material_id : str , phonon_method : str ) -> PhononDOS | dict [ str , Any ] :
9098 """Get the phonon dos pymatgen object associated with a given material ID and phonon method.
9199
92100 Arguments:
@@ -96,10 +104,13 @@ def get_dos_from_material_id(self, material_id: str, phonon_method: str):
96104 Returns:
97105 dos (PhononDOS): PhononDOS object
98106 """
99- result = self ._query_open_data (
100- bucket = "materialsproject-parsed" ,
101- key = f"ph-dos/{ phonon_method } /{ material_id } .json.gz" ,
102- )[0 ]
107+ try :
108+ result = self ._query_open_data (
109+ bucket = "materialsproject-parsed" ,
110+ key = f"ph-dos/{ phonon_method } /{ material_id } .json.gz" ,
111+ )[0 ]
112+ except OSError :
113+ result = None
103114
104115 if not result or not result [0 ]:
105116 raise MPRestError ("No object found" )
@@ -109,7 +120,7 @@ def get_dos_from_material_id(self, material_id: str, phonon_method: str):
109120
110121 return result [0 ]
111122
112- def get_forceconstants_from_material_id (self , material_id : str ):
123+ def get_forceconstants_from_material_id (self , material_id : str ) -> list [ list [ Matrix3D ]] :
113124 """Get the force constants associated with a given material ID.
114125
115126 Arguments:
@@ -118,10 +129,13 @@ def get_forceconstants_from_material_id(self, material_id: str):
118129 Returns:
119130 force constants (list[list[Matrix3D]]): PhononDOS object
120131 """
121- result = self ._query_open_data (
122- bucket = "materialsproject-parsed" ,
123- key = f"ph-force-constants/{ material_id } .json.gz" ,
124- )[0 ]
132+ try :
133+ result = self ._query_open_data (
134+ bucket = "materialsproject-parsed" ,
135+ key = f"ph-force-constants/{ material_id } .json.gz" ,
136+ )[0 ]
137+ except OSError :
138+ result = None
125139
126140 if not result or not result [0 ]:
127141 raise MPRestError ("No object found" )
0 commit comments