-
Notifications
You must be signed in to change notification settings - Fork 874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add as_dict()
method to AirssProvider
#2642
Conversation
pymatgen/core/tests/test_trajectory.py:8: in <module> from pymatgen.core.trajectory import Trajectory pymatgen/core/trajectory.py:35: in <module> Vector3D = tuple[float, float, float]
Hmm, I don't know the situations where |
I was thinking about that as well.
Thanks. 👍 |
As a general principle, inheriting from MSONAble should be the default unless there is good reason to implement a separate as_dict. If you implement as_dict, you generally have to implement from_dict as well. |
@shyuep Would the MSONable API work in this case? Lines 395 to 397 in 9407e8d
|
The MSONAble API does not rely on what method you use to init it. As long as res and parse_rems are part of the attributes, it will work. E.g., you rarely use the Poscar init method and mostly uses from_file. But Poscar still works with the MSONable api. All the API is meant to do is for serialization. |
Looking at the way MSONable works, if you make ResProvider inherit from it, then you get the as_dict and from_dict methods without having to do any implementation. They are just access wrappers so _res is all you need. Then you can override and stick any extra keys like structure in there after calling from super. |
@ScottNotFound Thanks for pointing that out! We should probably make both |
Res is a dataclass meaning it has a Edit: Oh I see the problem. I guess the class isn't smart enough to serialize a dataclass. You would have to have all of them inherit or you can do something with |
Hmmm... I assumed the output from |
Yeah, I think all the classes in |
Alternatively, you can modify the MSONAble protocol to handle dataclass? That would be better for future usability rather than just fixing the problem for this one case. MSONable was written at a time when dataclasses were not a thing.... |
At this point, the df_res = pd.DataFrame(
[AirssProvider.from_file(file).as_dict() for file in res_files]
).set_index("seed") Should we have another function, say |
I like this idea. |
How about I suggest this:
|
Is there any other functionality in pymatgen that takes calculation results and supplies it in a form suitable for a dataframe? It would be nice to keep a consistent interface. I see that |
@ScottNotFound I don't think there is a need for that functionality. The reason is because dataframes can take a dict. E.g., one of the most common ways I construct a dataframe is simply:
The MPRester.query method returns a list of dicts. If you simply do In essence, the as_dataframe method is rather redundant because you are replacing a single line of code. |
@janosh I just added support for dataclasses in the MSONAble protocol. It should now work without having to mess with the subclasses. |
@shyuep Great. I did what you suggested. |
@mkhorton This is a small follow-up to #2625. I think a convenience method to turn the most important attributes of an
AirssProvider
into a dict would be nice to have.sample output
@ScottNotFound Feel free to jump in if I missed any important attributes.