# integrator { #tvbo.behaviour.integrator }

`behaviour.integrator`

Ontology-backed metadata, code generation and factories for :class:`Integrator`.

Attached to the generated classes by name (``IntegratorBehaviour`` -> ``Integrator``).
Only schema fields are ever stored; everything derived from the ontology is a property, so an integrator can be serialized at any point without carrying runtime state.

Population from the ontology is NOT done at construction. It is :meth:`IntegratorBehaviour.enrich`, called explicitly and idempotent, so that resolving an integrator against the ontology stays an act the caller asks for rather than the cost of naming one. The wrapper this replaces called it from ``__init__``, which meant it ran for a directly constructed integrator and not for a loaded one; the loaded path compensated with its own call, and that call is now the only one.

A mixin *can* hook construction — see :mod:`tvbo.behaviour` on ``__post_init__``, which :class:`CouplingBehaviour` uses. This one deliberately does not.

## Classes

| Name | Description |
| --- | --- |
| [IntegratorBehaviour](#tvbo.behaviour.integrator.IntegratorBehaviour) | Ontology metadata, rendering and loading for an integrator. |

### IntegratorBehaviour { #tvbo.behaviour.integrator.IntegratorBehaviour }

```python
behaviour.integrator.IntegratorBehaviour()
```

Ontology metadata, rendering and loading for an integrator.

#### Attributes

| Name | Description |
| --- | --- |
| [class_name](#tvbo.behaviour.integrator.IntegratorBehaviour.class_name) | The generated integrator class name, suffixed with `Stochastic` when noisy. |
| [current_step](#tvbo.behaviour.integrator.IntegratorBehaviour.current_step) | The current integration step, a stateless default of `0`. |
| [info](#tvbo.behaviour.integrator.IntegratorBehaviour.info) | The code-generation metadata dict for this integrator's ontology class. |
| [metadata](#tvbo.behaviour.integrator.IntegratorBehaviour.metadata) | The integrator itself, exposed for backward compatibility. |
| [noise_wrapper](#tvbo.behaviour.integrator.IntegratorBehaviour.noise_wrapper) | The noise as a runtime `Noise` wrapper, or `None` when non-stochastic. |
| [ontoclass](#tvbo.behaviour.integrator.IntegratorBehaviour.ontoclass) | The ontology class for this integrator, resolved from `method`. |
| [stochastic](#tvbo.behaviour.integrator.IntegratorBehaviour.stochastic) | Whether the integrator is stochastic, i.e. has a noise component. |

#### Methods

| Name | Description |
| --- | --- |
| [enrich](#tvbo.behaviour.integrator.IntegratorBehaviour.enrich) | Fill the ontology-derived fields that are still unset. Idempotent. |
| [execute](#tvbo.behaviour.integrator.IntegratorBehaviour.execute) | Render, execute, and instantiate the integrator backend object. |
| [from_db](#tvbo.behaviour.integrator.IntegratorBehaviour.from_db) | Load an Integrator by name from the tvbo database. |
| [from_file](#tvbo.behaviour.integrator.IntegratorBehaviour.from_file) | Load an Integrator from a YAML file. |
| [list_db](#tvbo.behaviour.integrator.IntegratorBehaviour.list_db) | List available integrators in the tvbo database. |
| [render_code](#tvbo.behaviour.integrator.IntegratorBehaviour.render_code) | Render the integrator as source code for the requested backend. |
| [to_yaml](#tvbo.behaviour.integrator.IntegratorBehaviour.to_yaml) | Serialize the integrator to YAML, optionally writing it to a file. |

##### enrich { #tvbo.behaviour.integrator.IntegratorBehaviour.enrich }

```python
behaviour.integrator.IntegratorBehaviour.enrich(source=None)
```

Fill the ontology-derived fields that are still unset. Idempotent.

The same verb as :meth:`IriEnrichable.enrich`, and the same gap-filling contract, but its own implementation and no *key*: an integrator names an ontology method rather than a curated entity, so there is nothing for the database to answer and nothing to redirect the lookup at.

##### execute { #tvbo.behaviour.integrator.IntegratorBehaviour.execute }

```python
behaviour.integrator.IntegratorBehaviour.execute(format='tvb')
```

Render, execute, and instantiate the integrator backend object.

For the `tvb` backend the integrator class is instantiated, wiring in an executed noise object when stochastic; for other backends the generated class is returned.

###### Parameters {.doc-section .doc-section-parameters}

| Name   | Type   | Description                                       | Default   |
|--------|--------|---------------------------------------------------|-----------|
| format |        | Target backend passed through to code generation. | `'tvb'`   |

###### Returns {.doc-section .doc-section-returns}

| Name   | Type   | Description                                      |
|--------|--------|--------------------------------------------------|
|        |        | The executed backend integrator object or class. |

##### from_db { #tvbo.behaviour.integrator.IntegratorBehaviour.from_db }

```python
behaviour.integrator.IntegratorBehaviour.from_db(name)
```

Load an Integrator by name from the tvbo database.

##### from_file { #tvbo.behaviour.integrator.IntegratorBehaviour.from_file }

```python
behaviour.integrator.IntegratorBehaviour.from_file(filepath)
```

Load an Integrator from a YAML file.

##### list_db { #tvbo.behaviour.integrator.IntegratorBehaviour.list_db }

```python
behaviour.integrator.IntegratorBehaviour.list_db()
```

List available integrators in the tvbo database.

##### render_code { #tvbo.behaviour.integrator.IntegratorBehaviour.render_code }

```python
behaviour.integrator.IntegratorBehaviour.render_code(format='tvb', **kwargs)
```

Render the integrator as source code for the requested backend.

###### Parameters {.doc-section .doc-section-parameters}

| Name     | Type   | Description                                                                                               | Default   |
|----------|--------|-----------------------------------------------------------------------------------------------------------|-----------|
| format   |        | Target backend; `"tvb"` selects the TVB template, while `"autodiff"` or `"jax"` selects the JAX template. | `'tvb'`   |
| **kwargs |        | Extra values forwarded to the JAX template render context.                                                | `{}`      |

###### Returns {.doc-section .doc-section-returns}

| Name   | Type   | Description                           |
|--------|--------|---------------------------------------|
|        |        | The rendered source code as a string. |

###### Raises {.doc-section .doc-section-raises}

| Name   | Type       | Description                              |
|--------|------------|------------------------------------------|
|        | ValueError | If `format` is not a recognized backend. |

##### to_yaml { #tvbo.behaviour.integrator.IntegratorBehaviour.to_yaml }

```python
behaviour.integrator.IntegratorBehaviour.to_yaml(filepath=None)
```

Serialize the integrator to YAML, optionally writing it to a file.

###### Parameters {.doc-section .doc-section-parameters}

| Name     | Type        | Description                                                              | Default   |
|----------|-------------|--------------------------------------------------------------------------|-----------|
| filepath | str \| None | Destination path to write to; when `None`, the YAML is returned instead. | `None`    |

###### Returns {.doc-section .doc-section-returns}

| Name   | Type   | Description                                              |
|--------|--------|----------------------------------------------------------|
|        |        | The YAML string, or the result of writing to `filepath`. |