# MxlPy — Import / export & code generation

[← back to index](llms.txt)

## SBML (`mxlpy.sbml`)

Read and write the Systems Biology Markup Language — the standard for sharing models across tools and languages. `read()` always returns a plain `Model`.

```python
from mxlpy import sbml

model = sbml.read("model.xml")
sbml.write(model, file="out.xml")

# Optional meta-information on export (see sbml.org for legal values)
sbml.write(model, file="out.xml", extent_units="mole", substance_units="mole", time_units="second")
```

### MIRIAM annotations

Attach standardised ontology identifiers (UniProt, ChEBI, KEGG, GO, BioModels, …) to components and the model, e.g. to submit to the BioModels Database. An `Annotation` pairs an [identifiers.org](https://identifiers.org/) `uri` with a MIRIAM `predicate` (default `"is"`).

- **Component** annotations use **biological** qualifiers (`bqbiol`): `is`, `hasPart`, `isPartOf`, `isVersionOf`, `hasVersion`, `isHomologTo`, `isEncodedBy`, `encodes`, `occursIn`, `hasTaxon`, `isDescribedBy`.
- **Model** annotations use **model** qualifiers (`bqmodel`): `is`, `isDerivedFrom`, `isDescribedBy`, `isInstanceOf`, `hasInstance`.

Using a predicate from the wrong context raises `ValueError` on export. The `annotations=` argument (a single `Annotation` or an iterable) is accepted by every `add_*` / `update_*` method; `annotate_model` adds model-level annotations. They serialise as `<annotation><rdf:RDF>` CVTerms and round-trip through `sbml.read`.

```python
from mxlpy import Annotation, Model, fns

model = (
    Model()
    .add_parameter("k1", 1.0,
        annotations=Annotation("https://identifiers.org/ec-code:1.1.1.1", predicate="isVersionOf"))
    .add_variable("S", 0.0,
        annotations=[
            Annotation("https://identifiers.org/uniprot:P00533"),                 # predicate "is"
            Annotation("https://identifiers.org/chebi:16796", predicate="hasPart"),
        ])
    .annotate_model(
        Annotation("https://identifiers.org/biomodels.db:BIOMD0000000048", predicate="isDerivedFrom"))
)

sbml.write(model, file="annotated.xml")
reloaded = sbml.read("annotated.xml")
reloaded.get_annotations()                            # model-level annotations
reloaded.get_raw_variables()["S"].annotations         # component annotations
reloaded.get_raw_parameters()["k1"].annotations
```

## Native JSON (`mxlpy.save` / `mxlpy.load`)

A version-controllable `.mxl.json` format capturing the full model structure (variables, parameters, reactions, derived quantities, readouts), designed for clean diffs. Rate expressions are stored as **trees of math nodes** shared with [MxlWeb](https://github.com/Computational-Biology-Aachen/mxl-web), so the same files serve both tools.

```python
import mxlpy

mxlpy.save(model, "model.mxl.json", model_id="glycolysis", description="...")  # id/description optional
restored = mxlpy.load("model.mxl.json")
```

- **Lossless dynamics, regenerated functions.** Rate functions go through SymPy, so a loaded model uses generated (not the original named) functions but simulates identically. `save → load → save` reaches a stable fixed point.
- Each file references a `$schema` from the shared [`mxl-schemas`](https://github.com/Computational-Biology-Aachen/mxl-schemas) repo for editor validation/autocompletion. A `spec_version` field tracks the format version (shared with MxlWeb, decoupled from the package version).
- Surrogates and rate functions that cannot be parsed into an expression raise `mxlpy.types.SerializationError` — use SBML for those, or for cross-tool interchange.
- Terms and hidden-state blocks are saved too (spec_version 0.5, written only when used): `terms`, and `nn_blocks` with `hidden_states` / `input_scales` / `output_scales`, their weights (incl. readout `W` and `h0`) in `<block>.weights.json`. Every `nn_blocks` entry — also one exported from a torch surrogate or authored in mxlweb, with any mechanism — loads as a symbolic block (`get_raw_hidden_states`). SBML export and mxlweb codegen reject terms.
- To persist a single trained surrogate in its backend's native format (any architecture, not just dense networks), use `surrogate.save(path)` / `mxlpy.surrogates.load(path)` — see [mechanistic learning](llms-mxl.txt).

## Code & LaTeX generation (`mxlpy.meta`)

Inspect the Python source of model functions (parsed via `ast`) and re-emit the model as code or LaTeX. Requires **pure Python** functions (no `numpy`, no closures). Use cases: hand a model to collaborators in another language, generate a methods section, or diff two model versions.

```python
from mxlpy import meta

# Round-trip back into mxlpy builder syntax (e.g. to freeze a fitted/SBML-imported model)
print(meta.generate_mxlpy_code(model))

# Self-contained ODE function in a target language. Result has .model, .derived, .inits.
cg = meta.generate_model_code_py(model)        # also _rs (Rust), _ts (TypeScript), _jl (Julia)
print(cg.model); print(cg.derived); print(cg.inits)
```

- `free_parameters=[...]` — leave these as function arguments instead of inlined constants (to drive scans/fits in the target environment).
- `derived_to_calculate=[...]` — emit only these derived quantities; intermediates needed solely for omitted ones are pruned.
- Code targets raise `ValueError` on untranslatable constructs (a loud failure beats nonsense output).
- Every function takes the model time as its first argument, `time`, which is the name the expressions use (also in MATLAB/Octave, `model(time, variables)`). Rust prints every number as `f64` and parenthesises `if`/`else` expressions (e.g. piecewise `fns_input` splines and steps), so the output compiles as is.

### LaTeX

```python
# Structured tables (parameters, variables, rates, stoichiometry)
print(*meta.generate_model_code_latex(model, gls={"k_in": r"k_{in}"}, long_name_cutoff=20).as_tables(), sep="\n\n")

# Highlighted diff between two model versions (only_changes=True to drop unchanged rows)
print(*meta.generate_latex_diff(model_a, model_b, only_changes=False).as_default(), sep="\n\n")

# Wrap either result in a complete, compilable .tex document
print(meta.generate_latex_document(meta.generate_model_code_latex(model)))
```

The LaTeX exporter inserts a red placeholder for untranslatable equations (rather than raising) and continues, so the rest of the document remains usable.

Terms and hidden-state blocks (`generate_mxlpy_code`, `generate_model_code_latex`, `generate_latex_diff`, `codegen_latex.to_tex_export` / `generate_latex_code`):

- `generate_mxlpy_code` emits each block as `add_hidden_states(...)` (structure: `n_hidden`, targets, mechanism, layers, activation, inputs, input scales, `scale`, seed) followed by `update_hidden_states(name, weights=[...], biases=[...], readout=[[...]], h0=[...], output_scales={...})` with the values as literals, and `update_variable(h, InitialAssignment(...))` for assigned `h(0)`. Blocks come after the user terms on their targets, so terms on one target compose in the same order, and terms, blocks and readouts each come after the names they read. Linear hidden layers (as read from mxlweb `nn_blocks`) are `activation=None`. Blocks with per-layer or custom (MathML) activations or an activated output layer raise `NotImplementedError` (save them with `mxlpy.save`).
- LaTeX: user terms are derived quantities shown with their mechanism on their target (`base + t`, `base · (t + 1)`, a callable written out symbolically). A block is `s^b_x (W_b h_b)_x` on each target (times its `scale` parameter), one line `dh_b/dt = NN_b(u_b / s^b_u, h_b)`, and a `tabular` with hidden states, inputs, input/output scales, targets, mechanism, `h(0)`, layer sizes, activations and the number of weights. Weights, internal nodes and hidden states never appear one by one. The table data is `HiddenStates.summary(name)` (`get_raw_hidden_states()[name].summary(name)`).
