gpridge._observers ================== .. py:module:: gpridge._observers .. autoapi-nested-parse:: ASE-optimizer-style observers for the GP drivers. :class:`~gpridge.neb.GPNEB` and :class:`~gpridge.dimer.GPDimer` run their whole outer loop in C++, so there is no Python step at which an ASE ``Optimizer`` would call its observers. The one Python event the loop has is the oracle evaluation: every true energy and force call passes through the calculator bridge in :mod:`gpridge.ase_bridge`. The drivers therefore count one *step* per oracle call, and ``attach``, ``logfile`` and ``trajectory`` act on those steps the way they act on optimizer steps in ASE: - ``attach(function, interval=1, *args, **kwargs)`` calls ``function(*args, **kwargs)`` after every ``interval``-th oracle call; - ``logfile`` receives one line per oracle call (``"-"`` is stdout); - ``trajectory`` receives the evaluated geometry with its energy and forces. A path is a readcon trajectory, one frame per oracle call, readable by readcon, eOn and the drivers. An object with ``write`` is used as is. GP-predicted geometries are never reported: they cost no oracle call and are not ground truth. Classes ------- .. autoapisummary:: gpridge._observers.ConTrajectoryWriter gpridge._observers.OracleObservers gpridge._observers.ObservableDriver Module Contents --------------- .. py:class:: ConTrajectoryWriter(path: str) Append evaluated geometries to a ``.con`` file through readcon. Each ``write`` adds one frame with the positions, the cell, the fixed flags, the energy and the forces of an ``Atoms`` whose calculator holds them, and flushes it, so the file is complete after every call. .. py:attribute:: path .. py:method:: write(atoms: Any) -> None Append ``atoms`` as one frame and flush it to the file. .. py:method:: close() -> None Do nothing; every :meth:`write` has already flushed the file. .. py:class:: OracleObservers(name: str, logfile: Any = None, trajectory: Any = None) Observer list, log and trajectory driven by oracle evaluations. :param name: Label written at the start of each log line. :type name: :py:class:`str` :param logfile: ``"-"`` writes to stdout, a string or path-like object opens that path for writing, an object with ``write`` is used as is, ``None`` disables logging. :type logfile: :py:class:`str`, :py:class:`os.PathLike`, :py:class:`file-like` or :py:obj:`None` :param trajectory: A path opens a readcon trajectory. An object with ``write`` is used as is. :type trajectory: :py:class:`str` or :py:obj:`None` .. py:attribute:: name :value: '' .. py:attribute:: nsteps :value: 0 .. py:attribute:: observers :type: List[Tuple[Callable[Ellipsis, Any], int, tuple, dict, bool]] :value: [] .. py:attribute:: last_atoms :type: Any :value: None .. py:attribute:: logfile :value: None .. py:attribute:: trajectory :value: None .. py:method:: attach(function: Callable[Ellipsis, Any], interval: int = 1, *args, **kwargs) -> None Call ``function(*args, **kwargs)`` every ``interval`` oracle calls. As in ASE, an object with a ``write`` method may stand in for the function. Its ``write`` is called with the evaluated ``Atoms`` (energy and forces attached) followed by ``args``, because the drivers do not move the caller's ``Atoms`` during a run. Plain callables read the same geometry from :attr:`last_atoms`. .. py:method:: notify(atoms: Any, energy: float, forces: numpy.ndarray) -> None Record one oracle evaluation of ``atoms``. .. py:method:: close() -> None Close the log and trajectory this object opened itself. .. py:class:: ObservableDriver ``attach`` / ``logfile`` / ``trajectory`` surface for a GP driver. Subclasses call ``_init_observers`` in ``__init__`` and pass :attr:`observers` to the calculator bridge. .. py:attribute:: observers :type: OracleObservers .. py:property:: nsteps :type: int Oracle evaluations recorded since construction. .. py:property:: last_atoms The most recent oracle-evaluated geometry, or ``None``. .. py:method:: attach(function: Callable[Ellipsis, Any], interval: int = 1, *args, **kwargs) -> None Attach an observer; see :meth:`OracleObservers.attach`. .. py:method:: close() -> None Close a log or trajectory the driver opened from a path.