Skip to content

contract

Status codes, column specifications, aggregation and manifests.

quakecascade.contract

Output contract shared by every stage of the cascade.

Three rules hold across the package:

  1. A value that cannot be computed is NaN with a status code beside it, never 0. Missing data must not read as low risk.
  2. Counts (buildings, people, repairs) aggregate by sum; intensities (PGA, probabilities, travel times) aggregate by median or maximum.
  3. Every run can emit a manifest whose SHA-256 fingerprint is a pure function of the algorithm id, parameters, inputs and package version.

COMPUTED module-attribute

COMPUTED = 'computed'

The value was computed from complete inputs.

PARTIAL module-attribute

PARTIAL = 'partial'

Some contributing inputs were missing; the value is a bound, not an estimate.

MISSING_PREFIX module-attribute

MISSING_PREFIX = 'no_'

A required input (named by the stage, e.g. "no_vs30") was missing.

EMPTY module-attribute

EMPTY = 'empty'

The unit holds nothing to compute on (no building, no resident, no road).

OUTSIDE_ENVELOPE module-attribute

OUTSIDE_ENVELOPE = 'outside_envelope'

The input lies outside the model's calibration envelope; the value is kept but must be read as an extrapolation.

BASE_STATUSES module-attribute

BASE_STATUSES = (COMPUTED, PARTIAL, EMPTY, OUTSIDE_ENVELOPE)

The base status vocabulary. With the no_<input> codes (missing) and REGISTERED_STATUSES it makes up every valid status (is_valid_status).

REGISTERED_STATUSES module-attribute

REGISTERED_STATUSES = ...

Stage-specific codes that some *_status columns carry besides the base vocabulary and the no_<input> family, with the column and meaning. The emitting module defines a constant for each; tests/test_contract.py checks the two stay in step.

EXTENSIVE module-attribute

EXTENSIVE = 'extensive'

Aggregation rule of a count (buildings, people, repairs): summed over a group.

INTENSIVE module-attribute

INTENSIVE = 'intensive'

Aggregation rule of an intensity (ground motion, probability, ratio): a statistic over a group, the median by default (see aggregate).

SCHEMA module-attribute

SCHEMA = 'quakecascade-run-manifest-v1'

Schema identifier written into every run manifest.

ContractError

Bases: ValueError

Raised with every violation at once, so one run lists all problems.

Column dataclass

Column(
    kind,
    nullable,
    unit,
    description,
    valid_range=None,
    status_column=None,
)

Specification of one output column.

missing

missing(what)

Status code for a missing input, e.g. missing("vs30") == "no_vs30".

is_valid_status

is_valid_status(code, extra=())

True when code belongs to the package's status vocabulary.

Accepted: BASE_STATUSES, any no_<input> code (missing), the stage-specific REGISTERED_STATUSES and any code in extra.

status_from_inputs

status_from_inputs(**inputs)

Status per row from named input arrays: the first non-finite input wins.

status_from_inputs(vs30=[760, np.nan], pga=[0.2, 0.3]).tolist() ['computed', 'no_vs30']

mask_by_status

mask_by_status(values, status)

Return values as float with NaN wherever status is not computed/partial/envelope.

check_status_consistency

check_status_consistency(values, status, name='value')

Problems where a value and its status disagree (computed but NaN, missing but a number).

validate_frame

validate_frame(
    frame, spec, table="frame", key=(), allow_extra=False
)

Check frame against spec; raise ContractError listing every problem.

aggregate

aggregate(
    frame,
    by,
    columns,
    intensive="median",
    status_column=None,
)

Aggregate frame by by with the right rule for each column.

columns maps column name to EXTENSIVE or INTENSIVE. Extensive columns sum with min_count=1 so an all-missing group stays NaN instead of becoming 0. Intensive columns use intensive (median by default). When status_column is given, the output carries n_rows, n_computed (rows with a complete value: computed, outside_envelope or empty) and a group status:

  • empty when every row is empty (the zero is true);
  • outside_envelope when every row is complete and some are extrapolations; computed when every row is complete otherwise;
  • partial when some rows are complete and others are not (sums are lower bounds);
  • else the most common missing code. Columns that do not depend on the status (an input such as an occupant count) keep their sums.

stable_json

stable_json(value)

Canonical JSON text (sorted keys, no spaces, NaN/inf as strings) for hashing.

fingerprint

fingerprint(value)

SHA-256 hex digest of stable_json of value.

file_sha256

file_sha256(path)

SHA-256 hex digest of a file, read in 1 MiB chunks.

build_manifest

build_manifest(run_id, parameters, inputs=(), version=None)

Deterministic run manifest; run_fingerprint ignores the creation time.

validate_manifest

validate_manifest(manifest)

True when manifest has this schema and its fingerprint matches its core fields.

require

require(value, name, hint='')

Raise a clear error when a coefficient without a cited default is not supplied.