Skip to content
Projects

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

NameDescription
SPLITSNo description.

Classes

NameDescription
PredictionSurrogate outputs and whether each input is inside the training domain.
SurrogateA regression of the dataset's outputs on its inputs, in log space.

Functions

NameDescription
generate_datasetSolve model at log-uniform samples of the condition fields in bounds.
manifest_digestReturn the SHA-256 of the model's manifest (states, reactions, data).
split_indicesReturn the split label (0 train, 1 validation, 2 test) of each sample.

Modules

  • datasetDatasets of CR solutions for surrogates, with provenance and reproducible splits.
  • surrogateA baseline surrogate of the CR solver with scikit-learn.

SPLITSattributemodule attribute#

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

Predictionclassdataclass#

class Prediction(values: np.ndarray, in_domain: np.ndarray)

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

Attributes

NameTypeDescription
valuesnp.ndarrayThe predicted outputs (sample, output).
in_domainnp.ndarrayTrue where every input is inside the training ranges.

in_domainattributeinstance attribute#

in_domain: np.ndarray

valuesattributeinstance attribute#

values: np.ndarray

Surrogateclass#

class Surrogate(estimator: Any = None, seed: int = 0)

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

Parameters

NameTypeDefaultDescription
estimatorAnyNoneA 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).
seedint0The seed of the default estimator.

estimatorattributeinstance attribute#

estimator = estimator

highattributeinstance 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 = seed

evaluatemethod#

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

NameTypeDefaultDescription
datasetxr.DatasetrequiredThe dataset the surrogate was trained on (or a new one from the same model).
splitint21 for validation, 2 for test.
solve_timefloat | NoneNoneThe 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.

fitmethod#

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

Train on the dataset’s training split and return self.

forwardmethod#

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

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

predictmethod#

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

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

generate_datasetfunction#

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

NameTypeDefaultDescription
modelModelrequiredThe model.
conditionsConditionsrequiredThe conditions the sampled fields are set in.
boundsMapping[str, tuple[float, float]]requiredThe range of each sampled field (as for opencr.scan: electron_temperature, electron_density, density:<state>), in eV or m^-3, or quantities.
outputsSequence[str]requiredThe quantities to store.
samplesint200The number of samples.
seedint0The seed of the sampling; the split uses seed + 1.
modestr'quasi-static'The steady-state mode.
fractionsSequence[float](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).

manifest_digestfunction#

def manifest_digest(model: Model) -> str

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

split_indicesfunction#

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.