Skip to content

Can I use the optimade JSON serialization to store ASE atoms? #2246

@jan-janssen

Description

@jan-janssen

For my structures to be stored in an interoperable format - in the context of FAIR - I would like to use the JSON representation of the optimade structure.

I naively tired the following:

import json
from ase.build import bulk
from optimade.adapters.structures.ase import from_ase_atoms, get_ase_atoms
from optimade.models.structures import StructureResourceAttributes

al = bulk("Al", cubic=True)
struct_opt = from_ase_atoms(atoms=al)
json_dict = struct_opt.model_dump(mode="json")
structure_restore = StructureResourceAttributes.model_validate_json(json.dumps(json_dict))
get_ase_atoms(optimade_structure=structure_restore)

Unfortunately, this fails with the following error message:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[8], line 1
----> 1 get_ase_atoms(optimade_structure=structure_restore)

File ~/mambaforge/lib/python3.12/site-packages/optimade/adapters/structures/ase.py:55, in get_ase_atoms(optimade_structure)
     52     warn(ASE_NOT_FOUND, AdapterPackageNotFound)
     53     return None
---> 55 attributes = optimade_structure.attributes
     57 # Cannot handle partial occupancies
     58 if StructureFeatures.DISORDER in attributes.structure_features:

File ~/mambaforge/lib/python3.12/site-packages/pydantic/main.py:891, in BaseModel.__getattr__(self, item)
    888     return super().__getattribute__(item)  # Raises AttributeError if appropriate
    889 else:
    890     # this is the current error
--> 891     raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')

AttributeError: 'StructureResourceAttributes' object has no attribute 'attributes'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions