# Contributing

Contributions are welcome: bug reports, documentation, and code. For larger changes, please open an issue first to discuss the idea.


# Setup

1.  Install [Pixi](https://pixi.prefix.dev/latest/installation/).

2.  Fork and clone the repository.

3.  In the clone, install the environments and the Git hooks:

    ``` bash
    pixi install --all
    pixi run lefthook install
    ```

The project follows the [copier-pixi-python](https://github.com/avcopan/copier-pixi-python) template, whose README explains the tooling in more detail.


# Workflow

| task | what it does |
|----|----|
| `pixi run test` | Run the tests, including the doctests in `src/` |
| `pixi run test-lowest` | Run the tests with the lowest dependency versions allowed by `pyproject.toml` |
| `pixi run pre-commit` | Run every check that runs on commit: format, lint, types, import contracts, both test runs, and a clean-tree check |
| `pixi run pre-commit-check` | The same, without the tests |
| `pixi run docs-preview` | Build and preview the documentation site |
| `pixi run add <pkg>` | Add a run dependency to `pyproject.toml` |

To run a single test, call pytest in the `test` environment:

``` bash
pixi run -e test pytest tests/test_geom.py::test__center_of_mass
```

Coverage must stay at or above 80%. Docstrings use the [Google style](https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings), with types taken from the annotations rather than repeated in `Args:` and `Returns:`. Docstring examples run as doctests, so keep them working.


# Design


## One model, functions at the root

autogeom is centered on the [Geometry](reference/Geometry.html#autogeom.Geometry) model, and everything that operates on it is a function exported from the package root:

``` python
import autogeom

geo = autogeom.from_smiles("O")
com = autogeom.center_of_mass(geo)
```

- **Algorithms are functions**, not methods, and take the [Geometry](reference/Geometry.html#autogeom.Geometry) as their first argument. Algorithms that need more than a geometry take a model containing one first, e.g. `vibrational_analysis(point: PESPoint)`. [Geometry](reference/Geometry.html#autogeom.Geometry) only has methods for what it needs to construct and display itself (xyz I/O, [relabel_atoms](reference/Geometry.relabel_atoms.html#autogeom.Geometry.relabel_atoms)).
- **Variables** holding a [Geometry](reference/Geometry.html#autogeom.Geometry) are named `geo`.
- **Add new functions to a private module** (`_measure.py`, `_transform.py`, …) and export them in `autogeom/__init__.py`. The private modules exist to keep files small; users never import from them.
- **Public submodules** are for things that aren't about [Geometry](reference/Geometry.html#autogeom.Geometry): `autogeom.rd` (RDKit `Mol` functions), `autogeom.element` (periodic table data), and `autogeom.types` (array types and Pydantic fields).


## Layers

Modules are layered, and a module may only import from the layers below it. `pixi run imports` enforces this with the contract in `pyproject.toml`:

    _view | _convert            (highest)
    _transform
    _measure
    _trajectory
    _geometry
    rd
    element | types | units
    exc                         (lowest)

The one exception is `Geometry._repr_html_`, which imports [view](reference/view.html#autogeom.view) lazily to display itself in Jupyter.


## Conversions

Each conversion between [Geometry](reference/Geometry.html#autogeom.Geometry) and an external library or format has exactly one home in autogeom. Bridge modules know only their external library: `autogeom.rd` deals only in RDKit `Mol` objects and never imports [Geometry](reference/Geometry.html#autogeom.Geometry). The conversions themselves, such as [rdkit_mol()](reference/rdkit_mol.html#autogeom.rdkit_mol) and [from_rdkit_mol()](reference/from_rdkit_mol.html#autogeom.from_rdkit_mol), live above the bridge, in `_convert.py`. Other packages, such as autostorage, call these conversions rather than reimplementing them.

This works for libraries autogeom depends on anyway, like RDKit. For a library that is heavy or optional, import it lazily inside the function that needs it, as [render_svg](reference/render_svg.html#autogeom.render_svg) does with xyzrender.


## Element data

`src/autogeom/elements-data.json` is generated by `pixi run elements-data` (`scripts/elements-data.py`, in the `data` environment). Don't edit it by hand.
