api icon indicating copy to clipboard operation
api copied to clipboard

[Bug]: `MPRester(use_docment_model=False).get_prop_by_material_id()` does not work when `use_document_model=False` (prop = ['bandstructure', 'dos'])

Open AntObi opened this issue 1 year ago • 0 comments

Code snippet

from mp_api.client import MPRester
with MPRester(use_document_model=False) as mpr:
    bs = mpr.get_bandstructure_by_material_id("mp-23")

What happened?

I am attempting to get the bandstructure of a material using the convenience method get_bandstructure_by_material_id. When I pass use_document_model=False, I get the following error: TypeError: 'MPDataDoc' object is not subscriptable

When I run the same search but with use_document_model=True, the bandstructure is returned with no errors.

This issue is also present with other properties:

  • get_dos_by_material_id
  • get_bandstructure_by_material_id

I have no issues when trying to obtain phonon properties through either get_phonon_dos_by_material_id or get_phonon_bandstructure_by_material_id, which may mean the issue is related to the electronic_structure route.

Version

0.41.2

Which OS?

  • [X] MacOS
  • [ ] Windows
  • [ ] Linux

Log output

{
	"name": "TypeError",
	"message": "'MPDataDoc' object is not subscriptable",
	"stack": "---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[51], line 2
      1 with MPRester(use_document_model=False) as mpr:
----> 2     bsdoc = mpr.get_bandstructure_by_material_id(\"mp-23\")

File ~/miniforge3/envs/smact_dev/lib/python3.10/site-packages/mp_api/client/mprester.py:1233, in MPRester.get_bandstructure_by_material_id(self, material_id, path_type, line_mode)
   1217 def get_bandstructure_by_material_id(
   1218     self,
   1219     material_id: str,
   1220     path_type: BSPathType = BSPathType.setyawan_curtarolo,
   1221     line_mode=True,
   1222 ):
   1223     \"\"\"Get the band structure pymatgen object associated with a Materials Project ID.
   1224 
   1225     Arguments:
   (...)
   1231         bandstructure (Union[BandStructure, BandStructureSymmLine]): BandStructure or BandStructureSymmLine object
   1232     \"\"\"
-> 1233     return self.electronic_structure_bandstructure.get_bandstructure_from_material_id(  # type: ignore
   1234         material_id=material_id, path_type=path_type, line_mode=line_mode
   1235     )

File ~/miniforge3/envs/smact_dev/lib/python3.10/site-packages/mp_api/client/routes/materials/electronic_structure.py:280, in BandStructureRester.get_bandstructure_from_material_id(self, material_id, path_type, line_mode)
    274 if not bs_doc:
    275     raise MPRestError(\"No electronic structure data found.\")
    277 bs_data = (
    278     bs_doc[0].bandstructure  # type: ignore
    279     if self.use_document_model
--> 280     else bs_doc[0][\"bandstructure\"]  # type: ignore
    281 )
    283 if bs_data is None:
    284     raise MPRestError(
    285         f\"No {path_type.value} band structure data found for {material_id}\"
    286     )

TypeError: 'MPDataDoc' object is not subscriptable"
}

AntObi avatar Jul 25 '24 14:07 AntObi