# opencr.ml

*module*

Optional machine-learning surrogates of the CR solver. Imports scikit-learn only when used.

* `generate_dataset`: solutions over sampled conditions, with
  provenance and a reproducible train/validation/test split;
* `Surrogate`: a scikit-learn baseline with held-out errors, speed-up
  and an out-of-domain flag. The solver stays the reference.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/__init__.py#L1-L1)

## opencr.ml.SPLITS

*attribute* · *module attribute*

```python
SPLITS = ('train', 'validation', 'test')
```

Re-exported from: `opencr.ml.dataset`

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/dataset.py#L31-L31)

## opencr.ml.Prediction

*class* · *dataclass*

```python
class Prediction
```

Re-exported from: `opencr.ml.surrogate`

Surrogate outputs and whether each input is inside the training domain.

**Attributes**

- `values` (`np.ndarray`) — The predicted outputs (sample, output).
- `in_domain` (`np.ndarray`) — True where every input is inside the training ranges.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L43-L53)

### opencr.ml.Prediction.in_domain

*attribute* · *instance attribute*

```python
in_domain: np.ndarray
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L53-L53)

### opencr.ml.Prediction.values

*attribute* · *instance attribute*

```python
values: np.ndarray
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L52-L52)

## opencr.ml.Surrogate

*class*

```python
class Surrogate
```

Re-exported from: `opencr.ml.surrogate`

A regression of the dataset's outputs on its inputs, in log space.

**Parameters**

- `estimator` (`Any`) (default: `None`) — A scikit-learn regressor with multi-output ``fit`` and ``predict``; by default a Gaussian process with an anisotropic RBF kernel on standard-scaled inputs (smooth, few samples).
- `seed` (`int`) (default: `0`) — The seed of the default estimator.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L56-L141)

### opencr.ml.Surrogate.estimator

*attribute* · *instance attribute*

```python
estimator = estimator
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L68-L68)

### opencr.ml.Surrogate.high

*attribute* · *instance attribute*

```python
high = np.empty(0)
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L73-L73)

### opencr.ml.Surrogate.low

*attribute* · *instance attribute*

```python
low = np.empty(0)
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L72-L72)

### opencr.ml.Surrogate.outputs

*attribute* · *instance attribute*

```python
outputs: tuple[str, ...] = ()
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L71-L71)

### opencr.ml.Surrogate.parameters

*attribute* · *instance attribute*

```python
parameters: tuple[str, ...] = ()
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L70-L70)

### opencr.ml.Surrogate.provenance

*attribute* · *instance attribute*

```python
provenance: dict[str, Any] = {}
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L74-L74)

### opencr.ml.Surrogate.seed

*attribute* · *instance attribute*

```python
seed = seed
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L69-L69)

### opencr.ml.Surrogate.evaluate

*method*

```python
def evaluate(dataset: xr.Dataset, split: int = 2, solve_time: float | None = None) -> xr.Dataset
```

Return the held-out relative errors and the speed-up on one split.

**Parameters**

- `dataset` (`xr.Dataset`) — The dataset the surrogate was trained on (or a new one from the same model).
- `split` (`int`) (default: `2`) — 1 for validation, 2 for test.
- `solve_time` (`float | None`) (default: `None`) — The solver's time per sample in s, for the speed-up; by default the dataset's ``solve_time``.

**Returns**

- (`xr.Dataset`) — Per output: the median, 95th percentile and maximum of
- (`xr.Dataset`) — ``|predicted / solved - 1|``, and in the attributes the surrogate's
- (`xr.Dataset`) — time per sample and the speed-up.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L105-L141)

### opencr.ml.Surrogate.fit

*method*

```python
def fit(dataset: xr.Dataset) -> Surrogate
```

Train on the dataset's training split and return ``self``.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L76-L89)

### opencr.ml.Surrogate.forward

*method*

```python
def forward(theta: np.ndarray) -> np.ndarray
```

One sample's outputs, as a forward model for `opencr.inference`.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L101-L103)

### opencr.ml.Surrogate.predict

*method*

```python
def predict(inputs: np.ndarray) -> Prediction
```

Return the outputs at ``inputs`` (sample, parameter) with the domain flag.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/surrogate.py#L91-L99)

## opencr.ml.generate_dataset

*function*

```python
def generate_dataset(model: Model, conditions: Conditions, bounds: Mapping[str, tuple[float, float]], outputs: Sequence[str], samples: int = 200, seed: int = 0, mode: str = 'quasi-static', fractions: Sequence[float] = (0.7, 0.15, 0.15)) -> xr.Dataset
```

Re-exported from: `opencr.ml.dataset`

Solve ``model`` at log-uniform samples of the condition fields in ``bounds``.

**Parameters**

- `model` (`Model`) — The model.
- `conditions` (`Conditions`) — The conditions the sampled fields are set in.
- `bounds` (`Mapping[str, tuple[float, float]]`) — The range of each sampled field (as for `opencr.scan`: ``electron_temperature``, ``electron_density``, ``density:<state>``), in eV or m^-3, or quantities.
- `outputs` (`Sequence[str]`) — The quantities to store.
- `samples` (`int`) (default: `200`) — The number of samples.
- `seed` (`int`) (default: `0`) — The seed of the sampling; the split uses ``seed + 1``.
- `mode` (`str`) (default: `'quasi-static'`) — The steady-state mode.
- `fractions` (`Sequence[float]`) (default: `(0.7, 0.15, 0.15)`) — The train, validation and test fractions.

**Returns**

- (`xr.Dataset`) — A dataset over ``sample`` with ``inputs`` (sample, parameter),
- (`xr.Dataset`) — ``outputs`` (sample, output), ``split`` (0, 1, 2 for train, validation,
- (`xr.Dataset`) — test) and the provenance in its attributes (with ``solve_time``, the
- (`xr.Dataset`) — solver's time per sample in s).

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/dataset.py#L61-L132)

## opencr.ml.manifest_digest

*function*

```python
def manifest_digest(model: Model) -> str
```

Re-exported from: `opencr.ml.dataset`

Return the SHA-256 of the model's manifest (states, reactions, data).

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/dataset.py#L34-L37)

## opencr.ml.split_indices

*function*

```python
def split_indices(samples: int, fractions: Sequence[float] = (0.7, 0.15, 0.15), seed: int = 0) -> np.ndarray
```

Re-exported from: `opencr.ml.dataset`

Return the split label (0 train, 1 validation, 2 test) of each sample.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/ml/dataset.py#L47-L58)

**Modules**

- `opencr.ml.dataset`
- `opencr.ml.surrogate`
