materialsproject / api

New API client for the Materials Project
https://materialsproject.github.io/api/
Other
107 stars 39 forks source link

Monty dump and database_IDs in SummaryDoc not working #769

Open rasmusfr opened 1 year ago

rasmusfr commented 1 year ago

When trying to save a SummaryDoc with 'database_IDs' using monty serialization I get an error due to the datatype of 'database_IDs'. I'm not sure if this is intended or whether there is another way of doing it.

Input:

from monty.serialization import dumpfn
from mp_api.client import MPRester

mpid = 'mp-308'
fout = 'test_summary.json.gz'
with MPRester(MP_API_KEY) as mpr:
    docs = mpr.summary.search(material_ids=[mpid], theoretical=False, fields=['database_IDs'])[0]
dumpfn(docs, fout)

Output:

Traceback (most recent call last):
  File "C:\Users\user\PycharmProjects\PythonProject\Lib\dump_fail.py", line 10, in <module>
    dumpfn(docs, fout)
  File "C:\Users\user\PycharmProjects\PythonProject\lib\site-packages\monty\serialization.py", line 123, in dumpfn
    fp.write(json.dumps(obj, *args, **kwargs))
  File "C:\Program Files\Python310\lib\json\__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "C:\Program Files\Python310\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Program Files\Python310\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
TypeError: keys must be str, int, float, bool or None, not Database
rasmusfr commented 1 year ago

Seems like I can use use_document_model=False for now, but it would be nice if it worked regardless.

munrojm commented 1 year ago

Ah okay, this is good to know. I will mark this as an issue and take a closer look.

mattmcdermott commented 1 year ago

@munrojm Any idea why monty dump isn't working on MPRester docs? I think it used to...

https://matsci.org/t/saving-mprester-mpdatadoc-objects/46013/3

munrojm commented 1 year ago

It should be, I will take a closer look. This has been on my list for a bit, but I haven't had the bandwidth to address it yet.

munrojm commented 1 year ago

See https://matsci.org/t/saving-mprester-mpdatadoc-objects/46013/3