Run records and the store

The common output schema, observables, fidelity reports, run records and the content-addressed store.

abmfact.records

Run requests, run records, the common output schema and fidelity reports.

abmfact.records.schema

The common output schema every engine’s results are collected into.

Cells are one table with a row per cell per saved time. Fields are one frame per substrate per saved time on a regular grid. Observables are a tidy table of named series. Tables are plain mappings of column name to NumPy array, so the core needs no data-frame library.

abmfact.records.schema.LIVE = 0

Values of the state column.

abmfact.records.schema.APOPTOTIC = 1

Values of the state column.

abmfact.records.schema.NECROTIC = 2

Values of the state column.

abmfact.records.schema.empty_cells() → dict[str, ndarray][source]

A cells table with every column and no rows.

abmfact.records.schema.cells_table(rows: Mapping[str, Any]) → dict[str, ndarray][source]

Build a cells table from columns, filling z with zeros when absent and checking lengths.

class abmfact.records.schema.FieldFrame(time: float, substrate: str, origin: tuple[float, ...], spacing: tuple[float, ...], values: ndarray)[source]

Bases: object

One substrate on a regular grid at one time. values is indexed [iy, ix] in 2D and [iz, iy, ix] in 3D; origin is the centre of the first voxel.

class abmfact.records.schema.RunOutputs(cells: dict[str, ~numpy.ndarray]=<factory>, fields: list[FieldFrame] = <factory>, observables: dict[str, ~numpy.ndarray]=<factory>, scalars: dict[str, float | list[float]]=<factory>, type_names: list[str] = <factory>, save_times: list[float] = <factory>, warnings: list[str] = <factory>)[source]

Bases: object

Everything one run produced, in the common schema. scalars holds v0.1-style outputs.

times() → list[float][source]

Saved times: the declared ones, or else every distinct time in the cells table and fields.

cells_at(time: float) → dict[str, ndarray][source]

The rows of the cells table saved at time, matched with numpy.isclose.

field_at(substrate: str, time: float) → FieldFrame | None[source]

The frame of substrate saved at time, or None when there is none.

observable(name: str) → tuple[ndarray, ndarray][source]

The times and values of one observable, from the tidy observables table.

abmfact.records.observables

Observables computed from the common schema, so every engine’s results are measured the same way.

abmfact.records.observables.value_at(spec: Observable, outputs: RunOutputs, time: float) → float[source]

The value of one observable at one saved time. Empty selections give NaN for averages.

abmfact.records.observables.compute_observables(model: IRModel, outputs: RunOutputs) → RunOutputs[source]

Fill outputs.observables (tidy) and outputs.scalars (one series per observable).

abmfact.records.observables.compute_series(specs: Iterable[Observable], outputs: RunOutputs) → RunOutputs[source]

Like compute_observables(), for observable specs that do not come from an IR model.

abmfact.records.fidelity

Fidelity reports: what a translation mapped exactly, approximated, dropped, or left as native code.

abmfact.records.fidelity.TIERS = {'T0': "native: the model's own files run unchanged", 'T1': 'structural: same paradigm, concepts mapped one to one', 'T2': 'calibrated: different mechanics, parameters fitted to match observables', 'T3': 'certified: a T1 or T2 translation that passed docking within stated tolerances'}

Fidelity tiers from the platform plan.

class abmfact.records.fidelity.FidelityEntry(path: str, status: Literal['exact', 'approximated', 'dropped', 'native'], note: str = '')[source]

Bases: object

How one part of a model was carried over by a translation.

path

Where the item sits in the model, such as cell_types[tumor].custom.

Type:

str

status

One of exact, approximated, dropped or native.

Type:

Literal[‘exact’, ‘approximated’, ‘dropped’, ‘native’]

note

What was changed or lost, if anything.

Type:

str

class abmfact.records.fidelity.FidelityReport(source: str, target: str, tier: str = 'T1', entries: list[FidelityEntry] = <factory>)[source]

Bases: object

What a translation mapped exactly, approximated, dropped or left as native code.

source

What was translated, such as abm-ir or physicell:<project>.

Type:

str

target

What it was translated into, in the same form.

Type:

str

tier

The fidelity tier, a key of TIERS.

Type:

str

entries

One entry per path, in the order they were added.

Type:

list[abmfact.records.fidelity.FidelityEntry]

add(path: str, status: Literal['exact', 'approximated', 'dropped', 'native'], note: str = '') → None[source]

Record how one path was translated.

counts() → dict[str, int][source]

The number of entries with each status, including statuses with none.

property lossless: bool

True when nothing was approximated, dropped or left native.

to_dict() → dict[str, Any][source]

The report as a dictionary for JSON, with the tier’s meaning and the counts.

to_markdown() → str[source]

The report as a Markdown page: the tier, the counts and a table of every entry.

abmfact.records.record

Run requests and run records: what was asked for, and what happened.

abmfact.records.record.canonical(obj: Any) → str[source]

Compact JSON of obj with sorted keys, for hashing. NaN is rejected and other objects become strings.

class abmfact.records.record.RunRequest(engine: str, model_hash: str, params: dict[str, Any], seed: int, knobs: dict[str, Any], engine_version: str | None = None, image_digest: str | None = None, outputs_spec: str = 'default')[source]

Bases: object

Everything that decides a run’s result. Two requests with the same key give the same run.

engine_version or image_digest pin the engine; outputs_spec names what was collected.

to_dict() → dict[str, Any][source]

The request as a plain dictionary, the form that is hashed and stored.

classmethod from_dict(d: dict[str, Any]) → RunRequest[source]

Rebuild a request from the output of to_dict().

key() → str[source]

SHA-256 of the canonical JSON of the request: the run’s content address.

abmfact.records.record.now() → str[source]

The current UTC time as an ISO 8601 string, to the second.

class abmfact.records.record.RunRecord(request: RunRequest, status: str = 'ok', wall_time: float | None = None, log_path: str | None = None, output_paths: dict[str, str]=<factory>, output_sha256: dict[str, str]=<factory>, backend: str = 'local', abmfact_version: str = '', created: str = <factory>, error: str | None = None, warnings: list[str] = <factory>, outputs: RunOutputs | None = None)[source]

Bases: object

One run: the request, how it went, where its outputs are. outputs stays in memory only.

property run_id: str

The run’s identifier, which is the key of its request.

to_dict() → dict[str, Any][source]

The record as a plain dictionary with its version and run id, leaving out the in-memory outputs.

classmethod from_dict(d: dict[str, Any]) → RunRecord[source]

Rebuild a record from the output of to_dict(), without outputs.

abmfact.records.store

A content-addressed run store: identical requests are served from disk instead of rerun.

Layout:

root/runs/<key[:2]>/<key>/record.json      the run record
                          meta.json        type names and saved times
                          scalars.json     v0.1-style outputs
                          cells.npz | cells.parquet
                          fields.npz | fields.zarr
                          observables.csv | observables.parquet
root/index.jsonl | root/index.duckdb       one row per run, for queries

The key is the SHA-256 of the canonical run request (RunRequest.key()). With the [store] extra installed, cells and observables are written as Parquet, fields as Zarr and the index to DuckDB; otherwise NumPy .npz, CSV and JSON lines. Only one process should write to a store at a time; parallel runs return their records to the parent, which stores them.

abmfact.records.store.write_run(record: RunRecord, folder: str | Path, formats: str = 'auto') → RunRecord[source]

Write one run outside a store, in the same layout: record.json plus its output files.

abmfact.records.store.read_run(folder: str | Path) → RunRecord[source]

Read a run written by write_run() (or a store entry): its record with the outputs loaded.

class abmfact.records.store.RunStore(root: str | Path, formats: str = 'auto', index: str = 'auto')[source]

Bases: object

Store and look up runs by request.

formats is "auto" (Parquet and Zarr when installed), "columnar" (require them) or "basic" (NumPy and CSV only). index is "auto", "duckdb" or "jsonl".

run_dir(key: str) → Path[source]

The folder of the run with this key: runs/<first two characters>/<key> under the root.

property index_path: Path

The run index file under the root, index.duckdb or index.jsonl.

get(request: RunRequest) → RunRecord | None[source]

The stored record for this request with status="cached", or None. Failed runs miss, and so does everything while abmfact.records.cache.bypass_cache() is active.

load_outputs(record: RunRecord) → RunOutputs[source]

Read a stored run’s outputs back from its folder.

put(record: RunRecord) → RunRecord[source]

Write a record and its in-memory outputs. Returns the record with paths and hashes filled.

write_run(record: RunRecord, d: Path) → RunRecord[source]

Write one run’s record and outputs into folder d (the layout of a store entry).

query(**filters: Any) → list[dict[str, Any]][source]

Index rows matching every column=value filter, latest write per run, ordered by run id.

reindex() → int[source]

Rebuild the index from the stored records. Returns the number of runs indexed.

abmfact.records.cache

Bypassing caches when a run must really execute again.

The determinism check reruns seeds that have already run, with the same request. Any cache keyed on the request (the run store, or a study’s own cache) would hand back the first result and make the check pass whatever the model does. The audit runs its reruns inside bypass_cache(); caches consult cache_bypassed() and then neither read nor write.

abmfact.records.cache.cache_bypassed() → bool[source]

True while a run must execute again rather than be served from a cache.

abmfact.records.cache.bypass_cache() → Iterator[None][source]

A context manager in which cache_bypassed() is true, so caches neither read nor write.