# opencr.ml.dataset

*module*

Datasets of CR solutions for surrogates, with provenance and reproducible splits.

`generate_dataset` samples conditions in given ranges (log-uniform, by
a seeded Latin hypercube) and solves the model at each sample, storing the
inputs, the quantities (`opencr.analysis.quantities`) and the
provenance: the model's manifest digest, the OpenCR version, the sampling
and the seed. The split into training, validation and test sets is part of
the dataset (``split``), drawn from its own seed, so every surrogate trained
on the dataset sees the same split.

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

## opencr.ml.dataset.SPLITS

*attribute* · *module attribute*

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

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

## opencr.ml.dataset.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
```

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.dataset.manifest_digest

*function*

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

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.dataset.split_indices

*function*

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

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)
