Source code for chemparseplot.api

# SPDX-FileCopyrightText: 2023-present Rohit Goswami <rog32@hi.is>
#
# SPDX-License-Identifier: MIT
"""Stable public library entry points for chemparseplot.

Prefer this module for new consumers (cookbooks, wailord, notebooks).

**Representative in-process path (parse → typed result):**

.. code-block:: python

    from chemparseplot.api import extract_orca_geomscan_energy

    dist_bohr, energy_Eh = extract_orca_geomscan_energy(orca_out_text, "Actual Energy")
    # dist_bohr, energy_Eh are pint Quantities (bohr, hartree)

Heavy plot stacks are re-exported lazily so bare installs stay light.

Suite pins/config: use ``rgpycrumbs.api`` (shared rgpkgs TOML) — this package
does **not** invent ``~/.config/chemparseplot``.

.. versionadded:: 1.9.8
.. versionchanged:: 1.9.9
   Expose ORCA geomscan energy parse as the stable library path.
.. versionchanged:: 1.9.11
   Expand re-exports (SurfaceFitConfig, plot helpers, basetypes); hub suite_pins.
"""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

from chemparseplot.parse.orca.geomscan import extract_energy_data
from chemparseplot.units import (
    ENERGY_UNITS,
    convert_energy_magnitude,
    energy_quantity,
    normalize_energy_unit,
)

if TYPE_CHECKING:
    from chemparseplot.units import Q_

__all__ = [
    "ENERGY_UNITS",
    "Q_",
    "SurfaceFitConfig",
    "chemfiles_available",
    "convert_energy_magnitude",
    "energy_quantity",
    "extract_orca_geomscan_energy",
    "frames_to_table",
    "grammar_available",
    "load_con_trajectory",
    "load_trajectory",
    "normalize_energy_unit",
    "parse_orca_final_energy",
    "parse_orca_ir_spectrum",
    "parse_orca_populations",
    "parse_orca_vibrational_frequencies",
    "parse_orca_vpt2_fundamentals",
    "parse_xyz",
    "plot_con_energy_profile",
    "plot_con_overlay",
    "plot_con_overview",
    "plot_con_structure_strip",
    "plot_landscape_surface",
    "suite_pins",
    "ureg",
]


[docs] def grammar_available() -> bool: """True if the optional parsimonious grammar track is importable.""" from chemparseplot.parse.grammar import grammar_available as _ga return _ga()
[docs] def _looks_like_path(s: str) -> bool: """True for short path-like strings (avoid Path on multi-line blobs).""" if "\n" in s or len(s) > 4096: return False from pathlib import Path try: return Path(s).is_file() except OSError: return False
[docs] def parse_xyz(path_or_text: str): """Parse XYZ via grammar track (file path if exists, else text). Requires ``chemparseplot[grammar]``. """ from chemparseplot.parse.grammar.xyz import parse_xyz_file, parse_xyz_text if _looks_like_path(path_or_text): return parse_xyz_file(path_or_text) return parse_xyz_text(path_or_text)
[docs] def parse_orca_final_energy(path_or_text: str): """Last ``FINAL SINGLE POINT ENERGY`` as a hartree Quantity. Grammar track; requires ``chemparseplot[grammar]``. """ from chemparseplot.parse.grammar.orca_text import ( parse_orca_text_file, parse_orca_text_summary, ) if _looks_like_path(path_or_text): summary = parse_orca_text_file(path_or_text) else: summary = parse_orca_text_summary(path_or_text) return summary.final_energy
[docs] def extract_orca_geomscan_energy( data: str, energy_type: str = "Actual Energy" ) -> tuple[Q_, Q_]: """Parse ORCA geometry-scan surface energies from output text. In-process library path: text → typed ``(distance, energy)`` pint Quantities in Bohr and Hartree (ORCA defaults). """ return extract_energy_data(data, energy_type)
[docs] def load_trajectory(path: str): """Load a ``.con`` / ``.convel`` file, or an ASE trajectory of another suffix. ``.con`` goes through :mod:`chemparseplot.parse.eon.con_io`. ``fmax`` comes from ``frame.frc`` when a forces section is present. The extxyz reader in :mod:`chemparseplot.parse.trajectory.neb` is a different function. """ from chemparseplot.parse.eon.con_trajectory import load_trajectory as _load return _load(path)
[docs] def plot_con_overview(*args, **kwargs): """Draw one loaded trajectory. This does not parse a file.""" from chemparseplot.plot.con import plot_con_overview as _plot return _plot(*args, **kwargs)
[docs] def plot_con_overlay(*args, **kwargs): """Draw several loaded trajectories on one axes. This does not parse.""" from chemparseplot.plot.con import plot_con_overlay as _plot return _plot(*args, **kwargs)
[docs] def suite_pins() -> dict[str, str]: """Return suite package pins from rgpkgs config + env (soft on old hub).""" import importlib try: mod = importlib.import_module("rgpycrumbs.api") fn = getattr(mod, "suite_pins", None) if callable(fn): return fn() except ImportError: pass try: mod = importlib.import_module("rgpycrumbs.api") pins_from_env = mod.pins_from_env load_config = mod.load_config except (ImportError, AttributeError): return {} pins = dict(pins_from_env()) try: cfg = load_config() pins.update(cfg.merged_package_pins_normalized()) except Exception: # Older hubs raise while reading a partial config. An empty pin set # is the same result as a missing hub. pass return pins
[docs] def __getattr__(name: str) -> Any: """Lazy optional re-exports (plot stack / units / ORCA analysis).""" if name in {"ureg", "Q_"}: from chemparseplot import units as _units return getattr(_units, name) if name == "SurfaceFitConfig": from chemparseplot.plot.neb import SurfaceFitConfig return SurfaceFitConfig if name == "plot_landscape_surface": from chemparseplot.plot.neb import plot_landscape_surface return plot_landscape_surface if name == "parse_orca_vibrational_frequencies": from chemparseplot.parse.orca.freq import parse_orca_vibrational_frequencies return parse_orca_vibrational_frequencies if name == "parse_orca_ir_spectrum": from chemparseplot.parse.orca.freq import parse_orca_ir_spectrum return parse_orca_ir_spectrum if name == "parse_orca_vpt2_fundamentals": from chemparseplot.parse.orca.vpt2 import parse_orca_vpt2_fundamentals return parse_orca_vpt2_fundamentals if name == "parse_orca_populations": from chemparseplot.parse.orca.populations import parse_orca_populations return parse_orca_populations if name == "load_con_trajectory": from chemparseplot.parse.con import load_con_trajectory return load_con_trajectory if name == "frames_to_table": from chemparseplot.parse.con import frames_to_table return frames_to_table if name == "plot_con_energy_profile": from chemparseplot.plot.con import plot_con_energy_profile return plot_con_energy_profile if name == "plot_con_overview": from chemparseplot.plot.con import plot_con_overview return plot_con_overview if name == "plot_con_structure_strip": from chemparseplot.plot.con import plot_con_structure_strip return plot_con_structure_strip if name == "chemfiles_available": from chemparseplot.parse.con import chemfiles_available return chemfiles_available msg = f"module {__name__!r} has no attribute {name!r}" raise AttributeError(msg)