Add RegionGroupStrategy for union-of-regions scoping#477
Merged
Conversation
Lock the exact multi-entity cascade of filter_dataset_by_household_variable (households -> persons -> all group entities) before refactoring it, using the synthetic CA/CA/NJ fixture. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Split filter_dataset_by_household_variable into matching_household_ids (which households) and filter_dataset_by_household_ids (the entity cascade), keyed on the stable household_id rather than a positional mask. Public behavior is unchanged; the cascade now lives in one place so a union/group filter can reuse it. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Scope a single simulation to the union of several RowFilterStrategy regions (e.g. multiple whole states) by unioning their household-id sets and reusing the shared cascade. Members never overlap, so the union is a disjoint concatenation. cache_key is member-order independent; the strategy joins the ScopingStrategy discriminated union. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
Author
|
Update: the full local suite is green — |
…rage - Fix scoping_strategy module docstring (two -> three strategies; document RegionGroupStrategy). - Clarify that the wrapper's variable-specific empty-message and the id-set entry-point's generic guard front different callers (the specific message is test- and incident-referenced, so both are kept intentionally). - Move the shared US/UK group-entity lists + entity_data helper into conftest fixtures (us_entity_data / us_group_entities / uk_*), de-duplicating them across the filter and region-group tests. - Add coverage: direct filter_dataset_by_household_ids (explicit ids, any iterable, empty -> ValueError); a UK country group; overlapping members not double-counted; per-member additional_filters; empty union raises. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
anth-volk
marked this pull request as ready for review
July 20, 2026 14:31
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #476
Summary
Adds
RegionGroupStrategy— a scoping strategy that runs one simulation over the union of severalRowFilterStrategyregions (e.g. multiple whole states). This is the reusable.pycapability for segmenting a national macro run into ~20 parallel region-group simulations that reconstruct the national result exactly via map-reduce.Changes (commit by commit)
filter_dataset_by_household_variableinto a household-ID-set boundary —matching_household_ids(which households) +filter_dataset_by_household_ids(the entity cascade). Keyed on the stablehousehold_id, not a positional mask; public behavior unchanged.RegionGroupStrategy— unions members' household-id sets, reuses the shared cascade, joins theScopingStrategydiscriminated union. Disjoint by construction (member regions never overlap);cache_keyis member-order independent.added).Why
Validated end-to-end on staging: concatenating 20 balanced region-group runs reproduces the national macro output bit-for-bit — per-household
net_income/tax/weightidentical, Gini and budget exact — at ~160s vs ~1,414s for the monolithic national run.Tests
52 targeted tests pass (7 new
RegionGroupStrategy+ 5 new cascade characterization + 40 existing entity/scoping);ruff format --checkandruff checkclean;run_recordserializes the new strategy;towncrier --draftrenders. Full local suite was 90% green (all passing) at open time; CI runs the authoritative full suite.🤖 Generated with Claude Code