Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/microcosm-dataset-selectors.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add Microcosm selectors for the certified US national and local-area datasets and accept `--data-producer microcosm` in the release certification CLI. Existing selectors and certification metadata remain supported.
7 changes: 7 additions & 0 deletions changelog.d/microcosm-dataset-selectors.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Managed US flat year files now record their source SHA-256. `load_datasets` refuses a managed flat file whose recorded hash is missing or differs from the bundle's pin. `ensure_datasets` regenerates it when given the bundle key or an exact URI identifying the pinned artifact.

US result keys and year filenames now use the full local filename stem. For `sample@local.h5`, the 2024 result is `sample@local_2024` and its file is `sample@local_year_2024.h5`. `create_datasets` raises `ValueError` for a missing explicit local file before writing any year files, except for inputs an earlier request can produce.

When the bundle pins the same filename stem, unpinned US requests read and write year files only in a subdirectory keyed by the resolved local path or URI. Use `ensure_datasets(..., allow_unmanaged=True)` to build a missing source-specific year file.

`managed_microsimulation` records verified source provenance for explicit Hugging Face URIs that exactly match a bundle pin. Pinned model inputs and any pinned metadata use a pin-specific cache. US managed state and district runs derive registered legacy SPM declarations from the full ACS-local source and align them to the scoped simulation and its branches.
22 changes: 12 additions & 10 deletions docs/bundles.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,17 @@ package scaffold:
```python
from policyengine.provenance import materialize_dataset

result = materialize_dataset("us", "populace_us_2024")
result = materialize_dataset("us", "microcosm_us_2024")
print(result.path)
print(result.bundle_dataset.sha256)
```

`materialize_dataset` returns the selected source URI and local path. For a
bundle-managed input, `bundle_dataset` also contains the selected source
package, repository type, revision, verified SHA-256, and optional metadata
path.
path. Runtime materialization uses pin-specific paths for verified files.
Bundle keys and the default selection also populate the repository-relative raw
cache paths used by bundle installation.
`policyengine-*-data` and `populace-data` artifacts use the repository type
recorded in the bundle. Callers do not infer repository type from the repository
name.
Expand Down Expand Up @@ -203,18 +205,18 @@ To certify a new data release from a data-producer manifest, run:
python scripts/bundle.py certify-data \
--country uk \
--data-producer populace \
--manifest-uri hf://dataset/policyengine/populace-uk-private@<release>/releases/<release>/release_manifest.json
--manifest-uri "hf://dataset/policyengine/populace-uk-private@<release>/releases/<release>/release_manifest.json"
```

For US Microcosm releases, certify the Microcosm release manifest directly:
For US Microcosm releases, use the commit that published each build.
Choose listed, non-withdrawn builds from the
[public release index](https://huggingface.co/datasets/policyengine/populace-us/resolve/main/releases.json).

```bash
python scripts/bundle.py certify-data \
--country us \
--data-producer populace \
--manifest-uri hf://dataset/policyengine/populace-us@<release>/releases/<release>/release_manifest.json \
--regional-manifest-uri hf://dataset/policyengine/populace-us@<local-area-release>/releases/<local-area-release>/release_manifest.json \
--model-version <policyengine-us-version>
python scripts/bundle.py certify-data --country us --data-producer microcosm \
--manifest-uri "hf://dataset/policyengine/populace-us@<national-commit>/releases/microcosm-us-2024-national/<national-build>/release_manifest.json" \
--regional-manifest-uri "hf://dataset/policyengine/populace-us@<local-commit>/releases/microcosm-us-2024-local/<local-build>/release_manifest.json" \
--model-version "<policyengine-us-version>"
```

US state and congressional-district regions scope one certified ACS-local
Expand Down
37 changes: 14 additions & 23 deletions docs/engineering/runbooks/build-m-us-populace-certification.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# US Microcosm certification runbook

Use this runbook to certify a new national US Microcosm release together with
the shared ACS-local release used for state and congressional-district
simulations. For now, state and district simulations use the national release
instead; see the interim exception in Step 2. Read the
the shared ACS-local release. State and congressional-district simulations use
the shared ACS-local release through the bundle's regional routes. Read the
[data certification](../skills/data-certification.md) skill first for
validation semantics.

Expand All @@ -12,11 +11,10 @@ procedure itself is release-independent.

## When to use

Both release manifests have been published to `policyengine/populace-us`, and
the model version used by the national release is known. The national release
will remain the default dataset; the local-area release will remain
non-default and be selected only for supported regional runs (none while the
interim exception in Step 2 holds).
Use this procedure when both release manifests are available in
`policyengine/populace-us` and you know the policyengine-us version to certify.
The bundle selects the national release by default and routes supported
regional runs to the ACS-local release.

## Prerequisites

Expand All @@ -29,14 +27,9 @@ interim exception in Step 2 holds).

## Step 1 — identify the releases

Set values from the two published release manifests rather than copying an
older certification:

```
NATIONAL_RELEASE_ID = populace-us-2024-<national-build>-<sha>-<timestamp>Z
LOCAL_AREA_RELEASE_ID = populace-us-2024-<local-build>-<sha>-<timestamp>Z
MODEL_VERSION = <policyengine-us version declared by the national release>
```
Choose a listed, non-withdrawn build for each release in the [public release index](https://huggingface.co/datasets/policyengine/populace-us/resolve/main/releases.json).
Use each build's identifier and its publishing commit in the command below.
Use the policyengine-us version that both builds support.

The local-area manifest must declare `dataset_role: non_default_local_area`,
`is_default: false`, no default datasets, and exactly one pinned H5 microdata
Expand All @@ -45,12 +38,10 @@ artifact.
## Step 2 — certify both releases

```bash
python scripts/bundle.py certify-data \
--country us \
--data-producer populace \
--model-version "$MODEL_VERSION" \
--manifest-uri "hf://dataset/policyengine/populace-us@$NATIONAL_RELEASE_ID/releases/$NATIONAL_RELEASE_ID/release_manifest.json" \
--regional-manifest-uri "hf://dataset/policyengine/populace-us@$LOCAL_AREA_RELEASE_ID/releases/$LOCAL_AREA_RELEASE_ID/release_manifest.json"
python scripts/bundle.py certify-data --country us --data-producer microcosm \
--manifest-uri "hf://dataset/policyengine/populace-us@<national-commit>/releases/microcosm-us-2024-national/<national-build>/release_manifest.json" \
--regional-manifest-uri "hf://dataset/policyengine/populace-us@<local-commit>/releases/microcosm-us-2024-local/<local-build>/release_manifest.json" \
--model-version "<policyengine-us-version>"
```

This one command:
Expand All @@ -61,7 +52,7 @@ This one command:
- runs `generate(check=False)`, which re-normalizes `manifest.json` and
updates `pyproject.toml` **only if the model pins moved**;
- writes the changelog fragment
`changelog.d/certify-us-$NATIONAL_RELEASE_ID.changed.md`.
`changelog.d/certify-us-<national-build>.changed.md`.

## Step 3 — regenerate TRACE sidecars

Expand Down
18 changes: 13 additions & 5 deletions docs/engineering/runbooks/canonical-spm-development.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,19 @@ Ordinary `check` passes for the staged draft; this is metadata consistency only.
pass. Resolve the actual country/wrapper publication sequence with the
coordinator; do not invent future versions to break that dependency.
Refresh country and wrapper locks against real registry bytes.
5. Certify the published data release through `scripts/bundle.py certify-data
--country us --data-producer populace --model-version
"$PUBLISHED_COUNTRY_VERSION" --manifest-uri "$PUBLISHED_RELEASE_MANIFEST_URI"`.
The URI must identify the actual immutable producer release. This command
updates the country pin and data metadata from real compatibility evidence.
5. Certify the published national release with the ACS-local release that the
bundle will route regional simulations to. Use immutable producer manifest
URIs for both releases.

```bash
python scripts/bundle.py certify-data --country us --data-producer microcosm \
--model-version "$PUBLISHED_COUNTRY_VERSION" \
--manifest-uri "$PUBLISHED_RELEASE_MANIFEST_URI" \
--regional-manifest-uri "$PUBLISHED_REGIONAL_RELEASE_MANIFEST_URI"
```

This command updates the country pin and data metadata from the releases'
compatibility evidence.
Regenerate bundle artifacts and TRACE sidecars, inspect full-manifest hash
bindings, and run the required exact model/data tests. Staging an SPM hash
changes the manifest bytes; old sidecar bindings cannot establish provenance
Expand Down
4 changes: 2 additions & 2 deletions docs/engineering/skills/data-certification.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ folder (it has `releases.json`), so give each manifest's folder at the commit
that published it:

```bash
python scripts/bundle.py certify-data --country us --data-producer populace \
python scripts/bundle.py certify-data --country us --data-producer microcosm \
--manifest-uri "hf://dataset/policyengine/populace-us@<commit>/releases/microcosm-us-2024-national/<build>/release_manifest.json" \
--regional-manifest-uri "hf://dataset/policyengine/populace-us@<commit>/releases/microcosm-us-2024-local/<build>/release_manifest.json" \
--model-version "<policyengine-us-version>"
Expand Down Expand Up @@ -362,7 +362,7 @@ exact target installed, then certify with the reason and the evidence.
--limitation "<what the build model and the target differ in>" \
--output regional-input-surface.json

.venv/bin/python scripts/bundle.py certify-data --country us --data-producer populace \
.venv/bin/python scripts/bundle.py certify-data --country us --data-producer microcosm \
--manifest-uri "hf://dataset/policyengine/populace-us@<commit>/releases/microcosm-us-2024-national/<build>/release_manifest.json" \
--regional-manifest-uri "hf://dataset/policyengine/populace-us@<commit>/releases/microcosm-us-2024-local/<build>/release_manifest.json" \
--accept-regional-model-mismatch "<who accepted it and why>" \
Expand Down
94 changes: 77 additions & 17 deletions docs/microsim.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ Microdata is stored as HDF5 on Hugging Face. `ensure_datasets` downloads, caches

```python
datasets = pe.us.ensure_datasets(
datasets=["microcosm_us_2024"],
years=[2024, 2026],
data_folder="./data", # local cache directory
)
Expand All @@ -142,20 +143,55 @@ local-authority regions use its local-area release. **Enhanced FRS 2024–25**
(`enhanced_frs_2024_25`, policyengine-uk-data 1.58.1) and **Microcosm UK 2023**
remain available as named, non-default bundle datasets.

PolicyEngine.py obtains the repository type, immutable revision, and SHA-256
from the installed release bundle. An existing file in the configured data
directory is reused only after hash verification.

List datasets already known to the country:
PolicyEngine.py verifies cached source artifacts against the installed bundle's
SHA-256. It checks derived US year files against the year-file format, the
input-renames register and any required legacy SPM declarations. Managed flat
year files also need the source SHA-256 pinned by the bundle. When the bundle
records more than one distinct source SHA-256 for a filename stem,
PolicyEngine.py uses the source SHA-256 to keep the year files separate.

For either US selector pair, `populace_us_2024` and `microcosm_us_2024` or
`populace_us_2024_acs_local` and `microcosm_us_2024_acs_local`, the two names
select the same artifact when the bundle lists just one of them. If the bundle
records both spellings, each exact key selects its own artifact.
`create_datasets`, `load_datasets` and `ensure_datasets` return dictionaries keyed
by artifact filename stem and year. Request distinct artifacts with the same
stem separately so their result keys stay unique.

For local paths, the result key and year filename use the full filename stem,
including any `@`. For example, `sample@local.h5` gives `sample@local_2024`
and `sample@local_year_2024.h5`. Use the full key in lookups and rebuild older
year files written under the shortened stem. `create_datasets` raises
`ValueError` for a missing explicit local file before doing any writes, except
when an earlier request can create the file or its symlink target. A current
year file can still be loaded by an absolute or explicit relative source path
or by its source symlink after the source file has been removed.

Load the default dataset's current year files with `load_datasets`. For a
bundled US dataset, PolicyEngine.py writes year files straight into the data
folder unless the bundle records more than one distinct source SHA-256 for
that filename stem. Each of these flat year files records its source SHA-256.
`load_datasets` refuses one whose recorded hash is missing or differs from the
bundle's pin. `ensure_datasets` regenerates it when given the bundle key or
an exact URI identifying the pinned artifact. It uses the key by default and
keeps the explicit URI's ordered raw-cache updates with `allow_unmanaged=True`.
Validation and table loading use the same year-file snapshot.

```python
pe.us.load_datasets() # or pe.uk.load_datasets()
```

When the bundle pins an artifact with the same filename stem, an unpinned
request uses only its own subdirectory under `.policyengine-us-year-files/`,
keyed by the resolved local path or the URI. A bare Hugging Face URI and its
`@main` spelling use different subdirectories. Use
`ensure_datasets(..., allow_unmanaged=True)` to build a missing or stale
unpinned year file from its source.

### US local-area dataset

Alongside the certified national default, the bundle registers a US dataset for
finer geographic work: `populace_us_2024_acs_local`. It is a
For finer geographic work, select the bundled US local-area dataset with
`populace_us_2024_acs_local`. It is a
Microcosm US 2024 build of roughly **1.6 million households** on an **ACS 2024
multispine**, with each household **PUMA-assigned** to a 119th-Congress
congressional district, county, and state, and calibrated to **state
Expand All @@ -173,7 +209,7 @@ import policyengine as pe
sim = pe.us.managed_microsimulation(dataset="populace_us_2024_acs_local")
```

Or materialize it as a `PolicyEngineUSDataset` for `Simulation`:
Use `ensure_datasets` to materialize it as a `PolicyEngineUSDataset` for `Simulation`.

```python
datasets = pe.us.ensure_datasets(datasets=["populace_us_2024_acs_local"], years=[2024])
Expand All @@ -198,6 +234,20 @@ ca = Simulation(
)
```

For a native managed microsimulation, use the region's registered source and
row filter together.

```python
region = pe.us.model.get_region("state/ca")
ca = pe.us.managed_microsimulation(
dataset=region.dataset_path,
scoping_strategy=region.scoping_strategy,
allow_unmanaged=True,
)
```

The same call works with a district such as `congressional_district/CA-01`.

### UK private data and raw h5 access

UK population data uses licensed Family Resources Survey inputs. The default
Expand Down Expand Up @@ -436,7 +486,12 @@ Settings that conflict are recorded as an error for that year, and raise when
the response is calculated.

For managed simulations, `sim.policyengine_bundle` records the actual source
package, repository type, revision, verified SHA-256, and local path.
package, repository type, revision, verified SHA-256, and local path. It also
records this provenance when the explicit Hugging Face URI exactly matches a
bundle pin. The model uses a pin-specific cache path with verified source bytes
and any pinned metadata sidecar. US `managed_microsimulation` derives any
registered legacy SPM declarations from the full source, then aligns them to
the entity IDs in the selected cohort and its reform branches.

## Renamed inputs in stored data

Expand Down Expand Up @@ -486,14 +541,15 @@ under.
`create_datasets` stamps each year file, after writing everything else, with
the register it was cut under and a year-file format revision
(`legacy_inputs.YEAR_FILE_STAMP_H5_DATASET`, UTF-8 JSON
`{"register": {old: new, ...}, "revision": 1}`). `ensure_datasets` reuses a
year file only when its stamp equals this release's exactly and its renames
record lists only renames in the stamped register. Otherwise it logs why and
creates the year files again; `load_datasets` refuses the file with the same
reason, which names the renames the file may lack when its register is the
cause. To decide, both read only the stamp and the record, never the tables
or the source dataset. `legacy_inputs.year_file_stamp_problem(path)` returns
the reason, or `None` for a current year file.
`{"register": {old: new, ...}, "revision": 1}`). The stamp for a managed flat
year file also contains `source_sha256`. `ensure_datasets` checks that the
format revision and register match this release and that the renames record
lists only renames in the stamped register. For managed flat files, it also
checks that the stamp identifies the source pinned by the bundle. When a
check fails, it logs why and creates the year files again. `load_datasets`
refuses the file with the same reason, including any renames the file may
lack. Both decide from file metadata without reading the tables or the
source dataset.

A year file is out of date when:

Expand All @@ -508,6 +564,10 @@ A year file is out of date when:
- its register has an entry this release lacks, or maps an old name to
another new name. It may store a new name this release's engine does not
define, and a run skips any column the engine does not define;
- it is a managed flat year file whose stored source SHA-256 is missing or
differs from the bundle's pin;
- it records a source SHA-256 for a request whose cache path requires a stamp
without a source hash;
- it has another format revision, or its stamp or record cannot be read.

So any change to `LEGACY_INPUT_RENAMES` makes every year file out of date,
Expand Down
18 changes: 7 additions & 11 deletions docs/release-bundles.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,15 @@ It does not rebuild microdata artifacts.
Certification runs in this repository:
`src/policyengine/data/bundle/manifest.json` carries the certified
`data_releases.{country}` entry derived directly from the country's published
data release manifest. The entrypoint is:
data release manifest. For US Microcosm certification, pin the commit that
published each build. Choose listed, non-withdrawn builds for the national
and ACS-local releases in the
[public release index](https://huggingface.co/datasets/policyengine/populace-us/resolve/main/releases.json).

```bash
python scripts/bundle.py certify-data --country us \
--manifest-uri "hf://dataset/policyengine/populace-us@<tag>/releases/<tag>/release_manifest.json"
```

US Microcosm certification uses the Microcosm release manifest directly:

```bash
python scripts/bundle.py certify-data --country us --data-producer populace \
--manifest-uri "hf://dataset/policyengine/populace-us@<tag>/releases/<tag>/release_manifest.json" \
--regional-manifest-uri "hf://dataset/policyengine/populace-us@<local-area-tag>/releases/<local-area-tag>/release_manifest.json" \
python scripts/bundle.py certify-data --country us --data-producer microcosm \
--manifest-uri "hf://dataset/policyengine/populace-us@<national-commit>/releases/microcosm-us-2024-national/<national-build>/release_manifest.json" \
--regional-manifest-uri "hf://dataset/policyengine/populace-us@<local-commit>/releases/microcosm-us-2024-local/<local-build>/release_manifest.json" \
--model-version "<policyengine-us-version>"
```

Expand Down
2 changes: 1 addition & 1 deletion scripts/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def _parser() -> argparse.ArgumentParser:
certify.add_argument("--country", required=True, choices=["us", "uk"])
certify.add_argument(
"--data-producer",
choices=["legacy", "populace"],
choices=["legacy", "microcosm", "populace"],
help="Data-producer strategy. Defaults to the certification script default.",
)
certify.add_argument(
Expand Down
Loading
Loading