Can't retrieve band structure or dos using MPRester

I am trying to retrieve the band gaps of materials from MP using my API key:

import pymatgen
from pymatgen.ext.matproj import MPRester
from pymatgen.core.structure import *

a = MPRester(‘MY_API_KEY’)
entries = a.get_entries({“elements”:{“$in”:[“Cl”,“Br”,“I”,“F”],“$all”:[“Li”]}})

for i in entries:
band_structure = a.get_bandstructure_by_material_id(i.entry_id)
band_gap = band_structure.get_band_gap()

for most of my entries (i = 1, 3-7, 9-16, 18-29, 31-45 …etc), the band_structure comes back empty and no band_gap can be retrieved. But the same lines work fine for i = 0, 2, 8, 17, 30, 46 …etc. I don’t understand why it works for some and not others. There is not error message, the band_structure object is simply empty, and printing it returns ‘‘None’’.

Can someone explain why this happens and if there is another fast way to retireve the band_gap ?

PS: I have also tried get_dos_by_material_id and the same phenomenon was observed for the same entries

Thank you for your help !

Hi @Yasmine_Benabed, if you want to keep using the legacy API I would suggest using the query method with your MPIDs as the input, and ask for band_gap:

entries = mpr.query({"elements":{"$in":["Cl","Br","I","F"],"$all":["Li"]}}, properties=["band_gap"])

You can also get a more recent dataset with the new API, and ask for band_gap using

– Jason

It’s a good alternative, thank you Jason !

1 Like