materialsproject / pymatgen

Python Materials Genomics (pymatgen) is a robust materials analysis code that defines classes for structures and molecules with support for many electronic structure codes. It powers the Materials Project.
https://pymatgen.org
Other
1.52k stars 867 forks source link

BandStructureSymmLine from MP cannot be serialized #3096

Open JaGeo opened 1 year ago

JaGeo commented 1 year ago

@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()
janosh commented 1 year ago

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)