-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the basic data type from workgraph to there (#2)
- Loading branch information
1 parent
2b49a8c
commit 3cb836d
Showing
6 changed files
with
89 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .pickled_data import PickledData | ||
from .pickled_function import PickledFunction | ||
from .serializer import general_serializer, serialize_to_aiida_nodes | ||
|
||
__all__ = ("PickledData", "PickledFunction") | ||
__all__ = ("PickledData", "PickledFunction", "serialize_to_aiida_nodes", "general_serializer") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import numpy as np | ||
from aiida.orm import Data | ||
from ase import Atoms | ||
from ase.db.row import atoms2dict | ||
|
||
__all__ = ("AtomsData",) | ||
|
||
|
||
class AtomsData(Data): | ||
"""Data to represent a ASE Atoms.""" | ||
|
||
_cached_atoms = None | ||
|
||
def __init__(self, value=None, **kwargs): | ||
"""Initialise a `AtomsData` node instance. | ||
:param value: ASE Atoms instance to initialise the `AtomsData` node from | ||
""" | ||
atoms = value or Atoms() | ||
super().__init__(**kwargs) | ||
data, keys = self.atoms2dict(atoms) | ||
self.base.attributes.set_many(data) | ||
self.base.attributes.set("keys", keys) | ||
|
||
@classmethod | ||
def atoms2dict(cls, atoms): | ||
data = atoms2dict(atoms) | ||
data.pop("unique_id") | ||
keys = list(data.keys()) | ||
formula = atoms.get_chemical_formula() | ||
data = cls._convert_numpy_to_native(data) | ||
data["formula"] = formula | ||
data["symbols"] = atoms.get_chemical_symbols() | ||
return data, keys | ||
|
||
@classmethod | ||
def _convert_numpy_to_native(cls, data): | ||
"""Convert numpy types to Python native types for JSON compatibility.""" | ||
for key, value in data.items(): | ||
if isinstance(value, np.bool_): | ||
data[key] = bool(value) | ||
elif isinstance(value, np.ndarray): | ||
data[key] = value.tolist() | ||
elif isinstance(value, np.generic): | ||
data[key] = value.item() | ||
return data | ||
|
||
@property | ||
def value(self): | ||
keys = self.base.attributes.get("keys") | ||
data = self.base.attributes.get_many(keys) | ||
data = dict(zip(keys, data)) | ||
return Atoms(**data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from aiida import orm | ||
|
||
|
||
class Dict(orm.Dict): | ||
@property | ||
def value(self): | ||
return self.get_dict() | ||
|
||
|
||
class List(orm.List): | ||
@property | ||
def value(self): | ||
return self.get_list() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters