2.25.3.6. pycsamt.ai.experiments#
Reproducible experiment configuration, seed plans, dataset references, and acceptance-gate records.
Reproducible experiment configuration and artifact provenance.
Experiment records will capture configuration hashes, seeds, realization splits, frequency and depth grids, normalisation state, software versions, metrics, and artifact checksums. Runtime outputs and large artifacts are not package resources and must remain outside source control.
- class pycsamt.ai.experiments.SeedPlan(root_seed, namespace='pycsamt.ai')#
Bases:
objectDerive stable, labeled child seeds from one experiment root seed.
- Parameters:
Examples
Child seeds are label-stable and request-order independent:
>>> plan = SeedPlan(42, namespace="willy") >>> plan.derive("dataset") == plan.derive("dataset") True >>> plan.derive("dataset") != plan.derive("network") True
- derive(label)#
Derive one unsigned 32-bit seed for a named subsystem.
- Parameters:
label (str) – Stable descriptive label such as
"geology","noise", or"network/seed-0".- Returns:
Deterministic value in
[0, 2**32).- Return type:
- Raises:
ValueError – If
labelis empty.
Examples
>>> child = SeedPlan(1).derive("training") >>> 0 <= child < 2**32 True
- derive_many(labels)#
Derive seeds for multiple unique labels.
- Parameters:
labels (sequence of str) – Unique non-empty subsystem labels.
- Returns:
Labels in caller order mapped to stable child seeds.
- Return type:
Examples
>>> sorted(SeedPlan(2).derive_many(["data", "model"])) ['data', 'model']
- to_dict()#
Return a JSON-serializable seed plan.
- Returns:
Schema version, root seed, and namespace.
- Return type:
Examples
>>> SeedPlan(3).to_dict()["root_seed"] 3
- class pycsamt.ai.experiments.DatasetReference(dataset_id, manifest_hash, split_hash, normalization_hash=None, manifest_uri=None)#
Bases:
objectPin an experiment to exact dataset preparation artifacts.
- Parameters:
dataset_id (str) – Portable identifier from the dataset manifest.
manifest_hash (str) – SHA-256 digests of the complete dataset manifest and realization split.
split_hash (str) – SHA-256 digests of the complete dataset manifest and realization split.
normalization_hash (str or None, optional) – Digest of fitted normalization state. It may be absent for an audit or forward-only experiment that has not normalized data.
manifest_uri (str or None, optional) – Informational local path or remote URI. Integrity relies on the hash, not on this location.
Examples
>>> reference = DatasetReference("willy-v1", "a" * 64, "b" * 64) >>> reference.dataset_id 'willy-v1'
- to_dict()#
Return a JSON-serializable artifact reference.
- Returns:
Schema version and pinned artifact identifiers.
- Return type:
Examples
>>> DatasetReference("d", "a" * 64, "b" * 64).to_dict()[ ... "schema_version" ... ] 1
- class pycsamt.ai.experiments.AcceptanceCriterion(metric, operator, threshold, description='')#
Bases:
objectPredeclare one numerical condition required for an experiment gate.
- Parameters:
metric (str) – Exact metric key, preferably namespaced, for example
"test.impedance_nrms".operator ({"<", "<=", ">", ">=", "=="}) – Comparison applied as
observed operator threshold.threshold (float) – Finite value fixed before results are inspected.
description (str, optional) – Human-readable scientific justification.
Examples
>>> criterion = AcceptanceCriterion("test.impedance_nrms", "<=", 2.0) >>> criterion.evaluate(1.7) True >>> criterion.evaluate(2.5) False
- evaluate(observed)#
Evaluate an observed metric against the frozen threshold.
- Parameters:
observed (float) – Finite measured value.
- Returns:
Result of
observed operator threshold.- Return type:
- Raises:
ValueError – If
observedis NaN or infinite.
Examples
>>> AcceptanceCriterion("coverage", ">=", 0.9).evaluate(0.95) True
- to_dict()#
Return a JSON-serializable criterion.
- Returns:
Metric, comparison, threshold, and description.
- Return type:
Examples
>>> AcceptanceCriterion("x", "<", 1).to_dict()["operator"] '<'
- class pycsamt.ai.experiments.GateEvaluation(passed, criteria, observed, missing=(), complete=True)#
Bases:
objectImmutable result of evaluating configured acceptance criteria.
- Parameters:
passed (bool) – Whether every criterion passed and no required metric was missing.
criteria (mapping of str to bool) – Per-metric pass/fail results.
observed (mapping of str to float) – Finite observed values for metrics that were available.
missing (sequence of str) – Required metric keys absent from the supplied result mapping.
complete (bool, default=True) – Whether every configured criterion was evaluated. A partial status report can never pass the final gate.
Examples
>>> result = GateEvaluation(True, {"nrms": True}, {"nrms": 1.2}, ()) >>> result.failed_metrics ()
- class pycsamt.ai.experiments.ExperimentConfig(experiment_id, stage, dataset, seeds, model, training, physics=<factory>, acceptance=(), description='', tags=(), created_utc=None, schema_version=1)#
Bases:
objectImmutable source of truth for one reproducible inversion experiment.
- Parameters:
experiment_id (str) – Portable unique experiment identifier.
stage (str) – Roadmap stage such as
"baseline","forward_2d", or"field_evaluation".dataset (DatasetReference) – Exact dataset, split, and optional normalization artifacts.
seeds (SeedPlan) – Root and namespace used for labeled child seeds.
model (mapping) – Finite JSON-compatible configuration sections. Their schemas are owned by later model/trainer/solver adapters; this class freezes and hashes them without importing optional dependencies.
training (mapping) – Finite JSON-compatible configuration sections. Their schemas are owned by later model/trainer/solver adapters; this class freezes and hashes them without importing optional dependencies.
physics (mapping) – Finite JSON-compatible configuration sections. Their schemas are owned by later model/trainer/solver adapters; this class freezes and hashes them without importing optional dependencies.
acceptance (sequence of AcceptanceCriterion, optional) – Criteria fixed before results are viewed. Metric names must be unique.
description (str, optional) – Human-readable experiment objective.
tags (sequence of str, optional) – Unique searchable labels.
created_utc (str or None, optional) – Timezone-aware ISO-8601 timestamp normalized to UTC.
schema_version (int, default=1) – Configuration schema version.
Examples
>>> dataset = DatasetReference("willy-v1", "a" * 64, "b" * 64) >>> config = ExperimentConfig( ... "m0-baseline", ... "baseline", ... dataset, ... SeedPlan(42, "willy"), ... model={"architecture": "unet"}, ... training={"epochs": 100}, ... acceptance=[AcceptanceCriterion("test.nrms", "<=", 2.0)], ... ) >>> len(config.config_hash) 64 >>> config.child_seed("network") == config.seeds.derive("network") True
- dataset: DatasetReference#
- acceptance: tuple[AcceptanceCriterion, ...] = ()#
- property config_hash: str#
Return the canonical digest of the complete configuration.
- Returns:
SHA-256 digest covering every serialized field.
- Return type:
Examples
>>> d = DatasetReference("d", "a" * 64, "b" * 64) >>> len( ... ExperimentConfig( ... "e", "baseline", d, SeedPlan(0), {}, {} ... ).config_hash ... ) 64
- child_seed(label)#
Derive a stable subsystem seed from this experiment.
- Parameters:
label (str) – Subsystem label passed to
SeedPlan.derive().- Returns:
Deterministic unsigned 32-bit seed.
- Return type:
Examples
>>> d = DatasetReference("d", "a" * 64, "b" * 64) >>> c = ExperimentConfig("e", "baseline", d, SeedPlan(0), {}, {}) >>> c.child_seed("data") == c.child_seed("data") True
- evaluate_gate(metrics, *, require_all=True)#
Evaluate observed metrics against predeclared criteria.
- Parameters:
metrics (mapping of str to float) – Observed metric values.
require_all (bool, default=True) – Treat absent configured metrics as missing failures. When false, absent metrics are omitted; this is useful only for partial status reports and cannot prove the final gate passed unless all criteria were supplied.
- Returns:
Immutable overall, per-metric, observed, and missing results.
- Return type:
Examples
>>> d = DatasetReference("d", "a" * 64, "b" * 64) >>> c = ExperimentConfig( ... "e", ... "baseline", ... d, ... SeedPlan(0), ... {}, ... {}, ... acceptance=[AcceptanceCriterion("nrms", "<=", 2)], ... ) >>> c.evaluate_gate({"nrms": 1.5}).passed True
- to_dict()#
Return the complete JSON-compatible configuration.
- Returns:
Mutable schema-1 representation.
- Return type:
Examples
>>> d = DatasetReference("d", "a" * 64, "b" * 64) >>> ExperimentConfig( ... "e", "baseline", d, SeedPlan(0), {}, {} ... ).to_dict()["stage"] 'baseline'
- write_json(path, *, overwrite=True)#
Write a deterministic UTF-8 JSON configuration file.
- Parameters:
path (str or pathlib.Path) – Destination file.
overwrite (bool, default=True) – Permit replacement of an existing file.
- Returns:
Destination path.
- Return type:
Examples
>>> from tempfile import TemporaryDirectory >>> d = DatasetReference("d", "a" * 64, "b" * 64) >>> c = ExperimentConfig("e", "baseline", d, SeedPlan(0), {}, {}) >>> with TemporaryDirectory() as directory: ... path = c.write_json(Path(directory) / "experiment.json") ... loaded = ExperimentConfig.read_json(path) >>> loaded.config_hash == c.config_hash True
- classmethod from_dict(data)#
Restore and validate a serialized configuration.
- Parameters:
data (mapping) – State returned by
to_dict().- Returns:
Immutable source-of-truth configuration.
- Return type:
Examples
>>> d = DatasetReference("d", "a" * 64, "b" * 64) >>> c = ExperimentConfig("e", "baseline", d, SeedPlan(0), {}, {}) >>> ExperimentConfig.from_dict( ... c.to_dict() ... ).config_hash == c.config_hash True
- classmethod read_json(path)#
Read and validate a JSON experiment configuration.
- Parameters:
path (str or pathlib.Path) – Existing UTF-8 JSON file.
- Returns:
Validated immutable configuration.
- Return type:
Examples
>>> from tempfile import TemporaryDirectory >>> d = DatasetReference("d", "a" * 64, "b" * 64) >>> c = ExperimentConfig("e", "baseline", d, SeedPlan(0), {}, {}) >>> with TemporaryDirectory() as directory: ... path = c.write_json(Path(directory) / "config.json") ... loaded = ExperimentConfig.read_json(path) >>> loaded.experiment_id 'e'
|
Immutable source-of-truth configuration for AI inversion experiments. |