Python API¶
TrainCraft exposes a curated public API via import traincraft. Every stage
that the CLI runs is available as a standalone Python function.
Top-level functions¶
import traincraft as tc
# Config
cfg = tc.load_config("my_run.toml") # → TrainCraftConfig
cfg2 = tc.loads_config(toml_string) # → TrainCraftConfig
# Pipeline stages (pure functions — same ones the CLI calls)
structure = tc.build_geometry(cfg.geometry)
calc = tc.make_calculator(cfg.calculator)
frames = tc.run_sampling(structure, calc, job, cfg.sampling)
selected = tc.run_funnel(frames, cfg.selection)
summary = tc.run_pipeline(cfg) # the whole pipeline
# Dataset IO
tc.write_frames("out.extxyz", frames)
frames = tc.read_frames("dataset.extxyz")
Structure¶
Structure
dataclass
¶
Source code in src/traincraft/core/structure.py
n_fragments
property
¶
Number of distinct mobile fragments (excludes framework atoms).
from_ase
classmethod
¶
to_ase
¶
Return a copy of the atoms with properties/provenance in info.
Source code in src/traincraft/core/structure.py
copy
¶
set_fragments
¶
to_pymatgen
¶
from_pymatgen
classmethod
¶
Build a :class:Structure from a pymatgen Structure/Molecule.
to_rdkit
¶
Return an RDKit Mol with bonds perceived (non-periodic only).
from_rdkit
classmethod
¶
Build a :class:Structure from one conformer of an RDKit Mol.
Source code in src/traincraft/core/structure.py
Provenance¶
Provenance
dataclass
¶
Source code in src/traincraft/core/provenance.py
Workspace and Job¶
Workspace
¶
Owns an absolute run directory and hands out sub-directories/jobs.
Source code in src/traincraft/core/workspace.py
subdir
¶
Job
dataclass
¶
Source code in src/traincraft/core/workspace.py
Geometry¶
build_geometry
¶
Resolve a :class:GeometryConfig into a single :class:Structure.
Source code in src/traincraft/geometry/__init__.py
Converter¶
ase_to_pymatgen
¶
Convert ASE Atoms to a pymatgen Structure (periodic) or Molecule.
The choice is driven by periodicity: an Atoms periodic in all three
directions becomes a Structure; otherwise a Molecule (the cell is
dropped, since a partially periodic slab/wire has no pymatgen analogue).
Source code in src/traincraft/core/converter.py
pymatgen_to_ase
¶
Convert a pymatgen Structure or Molecule to ASE Atoms.
ase_to_rdkit
¶
Convert non-periodic ASE Atoms to an RDKit Mol with bonds perceived.
Bonds are inferred from the 3D geometry by RDKit's DetermineBonds (the
xyz2mol algorithm). Raises if the structure is periodic in any direction.
Source code in src/traincraft/core/converter.py
rdkit_to_ase
¶
Convert one conformer of an RDKit Mol to ASE Atoms.
conf_id selects which embedded conformer to read (default: the first).
Source code in src/traincraft/core/converter.py
Fragment helpers¶
get_fragments
¶
Return the per-atom fragment array, or None if unset.
set_fragments
¶
Attach/overwrite the per-atom fragment array (length must equal len(atoms)).
Source code in src/traincraft/core/fragments.py
infer_fragments
¶
infer_fragments(atoms: Atoms, scale: float = 1.2, framework_mask: ndarray | None = None) -> np.ndarray
Assign fragment ids by connected components of a covalent-radius graph.
Two atoms bond if distance < scale * (r_cov[i] + r_cov[j]).
framework_mask (optional bool array, length == len(atoms)): atoms marked
True are forced to FRAMEWORK (-1) and excluded from the connectivity graph.
Returns the array; does NOT mutate atoms.
Source code in src/traincraft/core/fragments.py
fragment_ids
¶
Sorted list of mobile fragment ids (excludes FRAMEWORK == -1).
fragment_mask
¶
Boolean mask selecting atoms of fragment fid.
Registry¶
register
¶
Decorator: register obj under (kind, name).