Round-trip and replay scenario results
This tutorial follows the persistence part of the checked scenario workflow. You will save a complete response, convert execution rows to a Polars table, write an IPC file, and recover the native artifacts needed for replay. The scenario tutorial explains the scientific assumptions of the same four cases.
Learning outcomes
Section titled “Learning outcomes”You will distinguish normalized table columns from retained structured execution records, verify row identities after an Arrow round trip, and check a replay against the original result. You will explain why successful serialization alone does not prove provenance or compatibility across software versions.
Prerequisites
Section titled “Prerequisites”Use the native development environment. The example requires Polars and the installed package. It generates four synthetic scenarios and uses a temporary local directory; no dataset or network service is required. The temporary files are removed when the example finishes.
Read the interchange reference for the
current copied IPC boundary and the distinction between file and stream formats.
The file writer used here is Polars’
DataFrame.write_ipc.
Run the maintained source from the repository root:
.venv-native/bin/python -I examples/native_scenarios.py"""Check native synthetic scenarios, artifact round trips and deterministic replay.
Reuse the scenario payloads from the maintained tutorial. JSON and Arrow outputare written to a temporary directory and removed when the example exits. Theexample uses no external datasets, services or host-language numerical fallback."""
import jsonimport mathimport tempfilefrom pathlib import Path
import polars as pl
import innovate
def main() -> None: """Execute four synthetic scenarios and check their scientific invariants.""" common = { "description": "Synthetic scenario example", "time_horizon": 5, "time_unit": "year", "reference_year": 2026, } policy = { "id": "pause-innovation", "model_key": "bass", "specification": { **common, "scenario_type": "intervention", "name": "Pause innovation", "market_size": 100, "initial_adoption": 0, "intervention_type": "innovation_delta", "intervention_start_time": 0, "intervention_magnitude": -0.2, }, "simulation": { "inputs": {"time": [0, 5]}, "parameters": {"p": 0.2, "q": 0}, "simulation_options": { "engine": "kairos_abm", "seed": 42, "population": 100, }, }, } baseline = { **policy, "id": "baseline", "specification": { **common, "scenario_type": "baseline", "name": "Baseline", "market_size": 100, "initial_adoption": 0, }, } competition = { "id": "two-products", "model_key": "multi_product", "specification": { **common, "scenario_type": "competition", "name": "Two products", "market_size": 200, "initial_adoption": 0.5, "num_competitors": 2, "competitor_names": ["A", "B"], "market_share_initial": [0.25, 0.75], }, "simulation": { "inputs": {"time": [0, 5]}, "parameters": { "p1": 0, "p2": 0, "q1": 0, "q2": 0, "m1": 100, "m2": 100, "alpha_1_2": 0, "alpha_2_1": 0, }, }, } substitution = { "id": "replacement", "model_key": "fixed_pool_replacement", "specification": { **common, "scenario_type": "substitution", "name": "Fixed-pool replacement", "market_size": 200, "initial_adoption": 0.5, "incumbent_name": "A", "entrant_name": "B", "substitution_rate": 0.5, }, "simulation": {"inputs": {"time": [0, 2]}}, }
response = innovate.execute( "execute_scenario_grid", scenarios=[baseline, policy, competition, substitution], comparisons=[{"baseline": "baseline", "alternative": "pause-innovation"}], apply_specifications=True, include_execution_metadata=True, ) executions = response["result"]["executions"] for row in executions: if row["error"] is not None: raise RuntimeError(row["error"])
assert executions[1]["result"]["counts"] == [0, 0] comparison = response["result"]["comparisons"][0] if comparison["error"] is not None: raise RuntimeError(comparison["error"]) baseline_final = executions[0]["result"]["values"][-1] assert baseline_final > 0 assert comparison["result"]["difference"][-1] == -baseline_final assert executions[2]["result"]["rows"] == [[25, 75], [25, 75]] incumbent, entrant = executions[3]["result"]["rows"][-1] assert math.isclose(incumbent, 100 * math.exp(-1)) assert math.isclose(incumbent + entrant, 100)
with tempfile.TemporaryDirectory(prefix="innovate-scenario-example-") as directory: json_path = Path(directory) / "scenario-grid.json" arrow_path = Path(directory) / "scenario-executions.arrow" json_path.write_text(json.dumps(response, indent=2, allow_nan=False), encoding="utf-8") assert json.loads(json_path.read_text(encoding="utf-8")) == response innovate.scenario_executions(response).write_ipc(arrow_path) table = pl.read_ipc(arrow_path) assert table["scenario_id"].to_list() == ["baseline", "pause-innovation", "two-products", "replacement"] saved_rows = [json.loads(value) for value in table["execution_json"]] assert saved_rows == executions
replayed = 0 for row in saved_rows: imported = innovate.execute("import_scenario_artifact", artifact=row["artifact"])["result"] if not imported["replay_available"]: raise RuntimeError(imported["replay_reason"]) request = imported["artifact"]["replay_request"] replay = innovate.execute(request["operation"], **request["payload"]) assert replay["result"]["executions"][0]["result"] == row["result"] replayed += 1
assert len(executions) == replayed == 4 print( json.dumps( { "policy_counts": executions[1]["result"]["counts"], "replacement_total": round(incumbent + entrant, 6), "replayed": replayed, "scenario_ids": table["scenario_id"].to_list(), }, sort_keys=True, ) )
if __name__ == "__main__": main()The script first executes baseline, policy, competition and replacement
scenarios. It checks every execution and comparison for an error before saving
anything. Focus next on the temporary-directory block: it writes and rereads
JSON, converts execution rows with scenario_executions, writes and reads Arrow,
recovers execution_json, and imports each row’s artifact for replay.
Expected results
Section titled “Expected results”The complete script prints this contract value after all assertions pass:
{"policy_counts": [0, 0], "replacement_total": 100.0, "replayed": 4, "scenario_ids": ["baseline", "pause-innovation", "two-products", "replacement"]}The saved table retains the ordered identifiers baseline, pause-innovation,
two-products and replacement. Its decoded execution records equal the
original records, and all four replayed scientific results equal their original
results. The script treats a missing replay capability or a per-item error as a
failure, rather than reporting the count of attempted rows as successful replay.
Interpretation
Section titled “Interpretation”| Check | What it establishes in this fixture |
|---|---|
| Decoded JSON equals the original response | The saved response fields survive this JSON round trip |
| Scenario identifiers remain ordered | The execution table retains the expected row identity |
Decoded execution_json equals original executions |
The table retains these structured execution records |
| Imported artifact exposes a replay request | The current implementation accepts the artifact for its supported replay path |
| Re-executed result equals the saved result | The checked request reproduces the result in the current environment |
The Arrow format defines columnar serialization; it does not know whether a scenario is scientifically adequate. Its file and stream structures are specified in the Apache Arrow format. Innovate’s artifact fields carry the application-level replay meaning.
Reader task: which evidence is lost if you save only the final numeric prediction column? You lose the explicit replay request, model and scenario configuration, and any metadata or errors not copied into that column. A table of final numbers alone is therefore insufficient for the replay checked here.
Limitations
Section titled “Limitations”This example tests IPC file round trips, not every Arrow type, compression mode, consumer language or stream transport. The current binding copies buffers, and no zero-copy or transport-performance claim follows from a successful run. Metadata removed before export cannot be reconstructed by the writer.
Replay is checked against the installed version and its supported artifact contract. Compatibility with future versions, external consumer implementations and incomplete artifacts needs separate evidence. Preserve version information and the required complete records when creating a durable analysis output. For content checksums and their exact scope, continue with data ingestion and provenance.