opencr.ml
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.
Attributes
| Name | Description |
|---|---|
SPLITS | No description. |
Classes
| Name | Description |
|---|---|
Prediction | Surrogate outputs and whether each input is inside the training domain. |
Surrogate | A regression of the dataset's outputs on its inputs, in log space. |
Functions
| Name | Description |
|---|---|
generate_dataset | Solve model at log-uniform samples of the condition fields in bounds. |
manifest_digest | Return the SHA-256 of the model's manifest (states, reactions, data). |
split_indices | Return the split label (0 train, 1 validation, 2 test) of each sample. |
Modules
SPLITSattributemodule attribute#
opencr.ml.datasetView sourceSPLITS = ('train', 'validation', 'test')Predictionclassdataclass#
opencr.ml.surrogateView sourceclass Prediction(values: np.ndarray, in_domain: np.ndarray)Surrogate outputs and whether each input is inside the training domain.
Attributes
| Name | Type | Description |
|---|---|---|
values | np.ndarray | The predicted outputs (sample, output). |
in_domain | np.ndarray | True where every input is inside the training ranges. |
Surrogateclass#
opencr.ml.surrogateView sourceclass Surrogate(estimator: Any = None, seed: int = 0)A regression of the dataset’s outputs on its inputs, in log space.
Parameters
estimatorattributeinstance attribute#
estimator = estimatorhighattributeinstance attribute#
high = np.empty(0)lowattributeinstance attribute#
low = np.empty(0)outputsattributeinstance attribute#
outputs: tuple[str, ...] = ()parametersattributeinstance attribute#
parameters: tuple[str, ...] = ()provenanceattributeinstance attribute#
provenance: dict[str, Any] = {}seedattributeinstance attribute#
seed = seedevaluatemethod#
def evaluate(dataset: xr.Dataset, split: int = 2, solve_time: float | None = None) -> xr.DatasetReturn the held-out relative errors and the speed-up on one split.
Parameters
Returns
xr.Dataset- Per output: the median, 95th percentile and maximum of
xr.Dataset|predicted / solved - 1|, and in the attributes the surrogate’sxr.Dataset- time per sample and the speed-up.
fitmethod#
def fit(dataset: xr.Dataset) -> SurrogateTrain on the dataset’s training split and return self.
forwardmethod#
def forward(theta: np.ndarray) -> np.ndarrayOne sample’s outputs, as a forward model for opencr.inference.
predictmethod#
def predict(inputs: np.ndarray) -> PredictionReturn the outputs at inputs (sample, parameter) with the domain flag.
generate_datasetfunction#
opencr.ml.datasetView sourcedef 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.DatasetSolve model at log-uniform samples of the condition fields in bounds.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
model | Model | required | The model. |
conditions | Conditions | required | The conditions the sampled fields are set in. |
bounds | Mapping[str, tuple[float, float]] | required | 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] | required | The quantities to store. |
samples | int | 200 | The number of samples. |
seed | int | 0 | The seed of the sampling; the split uses seed + 1. |
mode | str | 'quasi-static' | The steady-state mode. |
fractions | Sequence[float] | (0.7, 0.15, 0.15) | The train, validation and test fractions. |
Returns
xr.Dataset- A dataset over
samplewithinputs(sample, parameter), xr.Datasetoutputs(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).
manifest_digestfunction#
opencr.ml.datasetView sourcedef manifest_digest(model: Model) -> strReturn the SHA-256 of the model’s manifest (states, reactions, data).
split_indicesfunction#
opencr.ml.datasetView sourcedef split_indices(samples: int, fractions: Sequence[float] = (0.7, 0.15, 0.15), seed: int = 0) -> np.ndarrayReturn the split label (0 train, 1 validation, 2 test) of each sample.