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:
- A value that cannot be computed is
NaNwith a status code beside it, never0. Missing data must not read as low risk. - Counts (buildings, people, repairs) aggregate by sum; intensities (PGA, probabilities, travel times) aggregate by median or maximum.
- Every run can emit a manifest whose SHA-256 fingerprint is a pure function of the algorithm id, parameters, inputs and package version.
PARTIAL
module-attribute
¶
Some contributing inputs were missing; the value is a bound, not an estimate.
MISSING_PREFIX
module-attribute
¶
A required input (named by the stage, e.g. "no_vs30") was missing.
EMPTY
module-attribute
¶
The unit holds nothing to compute on (no building, no resident, no road).
OUTSIDE_ENVELOPE
module-attribute
¶
The input lies outside the model's calibration envelope; the value is kept but must be read as an extrapolation.
BASE_STATUSES
module-attribute
¶
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
¶
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
¶
Aggregation rule of a count (buildings, people, repairs): summed over a group.
INTENSIVE
module-attribute
¶
Aggregation rule of an intensity (ground motion, probability, ratio): a statistic over a
group, the median by default (see aggregate).
SCHEMA
module-attribute
¶
Schema identifier written into every run manifest.
ContractError
¶
Bases: ValueError
Raised with every violation at once, so one run lists all problems.
Column
dataclass
¶
Specification of one output column.
is_valid_status
¶
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 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
¶
Return values as float with NaN wherever status is not computed/partial/envelope.
check_status_consistency
¶
Problems where a value and its status disagree (computed but NaN, missing but a number).
validate_frame
¶
Check frame against spec; raise ContractError listing every problem.
aggregate
¶
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:
emptywhen every row is empty (the zero is true);outside_envelopewhen every row is complete and some are extrapolations;computedwhen every row is complete otherwise;partialwhen 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
¶
Canonical JSON text (sorted keys, no spaces, NaN/inf as strings) for hashing.
build_manifest
¶
Deterministic run manifest; run_fingerprint ignores the creation time.
validate_manifest
¶
True when manifest has this schema and its fingerprint matches its core fields.
require
¶
Raise a clear error when a coefficient without a cited default is not supplied.