Source code for httk.atomistic.structure_numeric_view
"""
A view presenting any structure backend as a NumericStructure (the plain-numpy presentation).
"""
from typing import Any, Self
from httk.core import unwrap
from .numeric_structure import NumericStructure
from .structure import Structure
from .structure_backend import StructureBackend
from .structure_like import StructureLike
from .structure_view import StructureView
[docs]
class StructureNumericView(StructureView, NumericStructure):
"""
A view presenting an underlying structure backend as a ``NumericStructure``.
This view is a genuine ``NumericStructure``, so it can be passed anywhere one is accepted. Its
exact ``Structure`` is built eagerly from the backend's quartet on construction. Like a
``NumericStructure`` it requires numpy (raising :class:`ImportError` otherwise).
"""
_backend: StructureBackend
def __new__(cls, obj: StructureLike, **hints: Any) -> Self:
if isinstance(obj, cls):
return obj
backend = cls._prepare_backend(obj, hints)
instance = super().__new__(cls)
# NumericStructure is mutable, so its state is initialized here in __new__ (keeping __init__
# a no-op), so that rewrapping an existing view via cls(view) does not re-initialize it.
NumericStructure.__init__(
instance,
Structure(backend.cell, backend.sites, backend.species, backend.species_at_sites),
)
instance._backend = backend
return instance
def __init__(self, obj: StructureLike, **hints: Any) -> None:
pass
[docs]
def unwrap(self) -> Any:
return unwrap(self._backend)