pymatgen icon indicating copy to clipboard operation
pymatgen copied to clipboard

BandStructureSymmLine from MP cannot be serialized

Open JaGeo opened this issue 2 years ago • 1 comments

@janosh @munrojm I am not sure the issue is pymatgen related or emmet-related. The following code fails as the BandstructureSymmline cannot be converted to a serializable json....

from mp_api.client import MPRester
import json
with MPRester("YOURKEY") as a:
    band=a.get_bandstructure_by_material_id("mp-1219182")
with open("bandstructure.json",'w') as f:
    json.dump(band.as_dict(),f)

I get TypeError: Object of type int64 is not JSON serializable. It works for other mpids such as "mp-406".

Plotting also works:

from mp_api.client import MPRester
import json
from pymatgen.electronic_structure.plotter import BSPlotter
with MPRester("YOURKEY") as a:
    band=a.get_bandstructure_by_material_id("mp-1219182")
BSPlotter(band).get_plot().show()

JaGeo avatar Jun 23 '23 07:06 JaGeo

I am not sure the issue is pymatgen related or emmet-related.

Neither I think. I assume bandstructures should not be saved using numpy types, so just an issue with our db.

@munrojm Would a re-parse fix this?

@JaGeo Here's a temporary workaround:

json.dump(band.as_dict(), f, default=lambda x: int(x) if isinstance(x, np.int64) else x)

janosh avatar Jun 23 '23 13:06 janosh