-
Notifications
You must be signed in to change notification settings - Fork 50
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 support for serializing ase Atoms objects #582
Conversation
for more information, see https://pre-commit.ci
I agree this is useful. But shouldn't this be implemented in ASE itself? Eg pymatgen implements Structure serialization. Monty is not exactly a materials science code. It is strange to have ASE serialization here. |
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## master #582 +/- ##
==========================================
+ Coverage 75.56% 75.59% +0.03%
==========================================
Files 27 27
Lines 1465 1475 +10
Branches 309 312 +3
==========================================
+ Hits 1107 1115 +8
- Misses 302 304 +2
Partials 56 56
☔ View full report in Codecov by Sentry. |
I agree that this feels quite specific for monty. I'll raise an issue on the ase repo and see if they would be happy with me adding monty support directly. |
ASE does have its own from ase.io.jsonio import decode, encode
def atoms_as_dict(s: Atoms) -> dict:
return {"@module": "ase.atoms", "@class": "Atoms", "atoms_json": encode(s)}
def atoms_from_dict(d: dict) -> Atoms:
return decode(d["atoms_json"])
Atoms.as_dict = atoms_as_dict
Atoms.from_dict = atoms_from_dict Not sure of the pros/cons compared to your approach @utf. I'd need to investigate. But I just wanted to share what I already know about the topic. |
I'm a maintainer of ASE now, so feel free to float something by me. I might be able to help get it through. My gitlab username is andrewrosen. |
I would be fine adding support for todict and fromdict as alternatives to as_dict and from_dict. Though it would be better if ASE simply adds these aliases. |
Closing this for now. |
I know this is quite old, but I wanted to chime in to say that there is basically 0% chance such methods will be able to be added to ASE. It either has to happen in
Did we want to revisit this proposition? It is not necessarily ideal since I will note that the method proposed by @utf is not ideal. The below will crash because from ase.build import bulk
from ase.constraints import FixAtoms
from monty.json import MontyEncoder
atoms = bulk("Cu")
c = FixAtoms([0])
atoms.set_constraint(c)
{"@module": "ase", "@class": "Atoms", "data": MontyEncoder().encode(atoms.todict())} My approach using ASE's JSON encoder/decoder will always work, but it has the downside that the |
This PR adds support for serializing ase
Atoms
objects.We've found this might be a useful feature to have in atomate2. But I think it could be useful more broadly. E.g.,
Atoms
objects are used in the MD interface inmatgl
, inchgnet
, andquacc
(developed by @Andrew-S-Rosen). Although I understand if this sort of feature is outside the scope of monty.