Skip to content

Draw district maps on the boundaries each analysis was computed on - #235

Merged
DTrim99 merged 6 commits into
mainfrom
feat/cd120-districts
Nov 25, 2026
Merged

DTrim99 merged 6 commits into
mainfrom
feat/cd120-districts

Conversation

@DTrim99

@DTrim99 DTrim99 commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The Districts tab draws ArcGIS's 118th Congress district layer. Every state analysis, though, runs on the policyengine-us-data state files, which assign households to 119th Congress districts (make_block_cd_distributions.py calls fetch_block_to_district_map(119)). Five states redrew between the 118th and 119th Congresses (Alabama, Georgia, Louisiana, North Carolina and New York), so in those states the published district results sit on the wrong boundaries today.

This PR draws each analysis on the boundaries it was computed on, and records that Congress with every analysis. It also bumps ui-kit to 0.15.0, which has the 120th Congress boundaries, so moving the tracker to the 120th for the 2027 sessions then takes two steps: add a dataset source and recompute. Nine states redrew for the 120th: AL, CA, FL, LA, NC, OH, TN, TX and UT.

Changes

Map (DistrictMap.jsx, ReformAnalyzer.jsx, new districtGeo.js)

  • Loads boundaries from ui-kit's loadCongressionalDistrictsGeo instead of ArcGIS, filtered to the state by DISTRICT_ID.
  • Picks the Congress from model_notes.district_congress, or from a year's own districtCongress in multi-year analyses. Untagged analyses default to 119, which is what every existing analysis used.
  • The map header names the Congress, e.g. "119th Congress districts".
  • Bumps @policyengine/ui-kit from 0.9.0 to 0.15.0, which adds the 120th boundaries (Add 120th Congress district maps and tables policyengine-ui-kit#41), so analyses tagged 119 or 120 draw on their own boundaries. Nothing else the tracker uses from ui-kit changed between those versions: it only imports theme.css and the districts loader.
  • Any other Congress falls back to a list of the shown year's district results, with a note that its boundaries aren't in the app yet, rather than drawing another Congress's boundaries. A failed boundary load falls back the same way. A check on the file's CDSESSN catches a file from the wrong Congress, since ui-kit loads the 119th file for a Congress it doesn't have.
  • Any boundary feature with an area over a hemisphere (rings wound the wrong way for d3-geo) is rewound before drawing.
  • Removes the fallback's fixed district list, whose region names came from an older map.
  • Removes the unused hard-coded 118th Utah map.

Compute (scripts/compute_impacts.py)

  • State datasets are named sources in STATE_DATASETS, chosen with --state-dataset or TRACKER_STATE_DATASET. Each source says which Congress's districts it uses. The default, us-data-cd119, is the current files, so existing runs don't change.
  • Analyses with district results store model_notes.district_congress, plus district_dataset_revision, the Hugging Face revision of the state file. Multi-year analyses also tag each year.
  • us-data-cd119 is pinned to revision 21280dca (the repo's head on November 24, 2026; last commit May 31), so every run reads the same files.
  • Warns when a state's district GEOIDs don't match state_fips * 100 + 1..n.
  • Supabase writes now take the dataset name from the source, so federal runs record populace-us, as the offline path already did.

Path to the 120th Congress

  1. Done here: Add 120th Congress district maps and tables policyengine-ui-kit#41 is merged and released as ui-kit 0.15.0, and this PR bumps to it and adds 120 to DRAWABLE_CONGRESSES.
  2. Add a STATE_DATASETS entry for the microcosm release that assigns households to 120th districts. Tracked in Move congressional districts to the 120th Congress microcosm#1526.
  3. Recompute all bills on that source. The map follows the tag.

Testing

  • next build passes, and the boundary file builds as its own lazy-loaded chunk.
  • eslint: no errors (the existing warnings are unchanged).
  • src/components/reform/districtGeo.test.js: 16 tests covering Congress selection, labels, district parsing, state filtering, loading the 119th and 120th, refusing other Congresses, and rewinding.
  • src/components/reform/DistrictMap.test.jsx: 6 render tests. One checks that an analysis tagged 120 loads and draws the 120th boundaries. Five cover the fallback: a Congress the app can't draw, a failed 120th load, a selected year's results, a failed 119th load and an analysis without district results. The fallback tests fail on the component before 52756fc with ReferenceError: yearImpacts is not defined, and the 120 tests fail without 120 in DRAWABLE_CONGRESSES.
  • Smoke check against the real ui-kit 0.15.0 files (not committed): through loadStateDistrictsGeo, every state's 120th districts load, match the 119th district counts, are numbered 1 to N (or at-large) and have no inverted rings.
  • scripts/test_compute_impacts_datasets.py: 10 tests covering source selection, the pinned revision, and the tags in single-year, multi-year and no-district writes, with a fake Supabase client.
  • Full vitest suite: 74 passed.
  • Checked in a browser on a local dev server against production data, before the ui-kit bump (see the comment below): the Districts tab draws 119th boundaries with the new header for NC, NY and GA bills. No production analysis is tagged 120 yet.

🤖 Generated with Claude Code

DTrim99 and others added 2 commits November 12, 2026 20:23
The Districts tab fetched ArcGIS's 118th Congress layer, but every analysis
runs on the policyengine-us-data state files, which assign households to
119th Congress districts. In the five states that redrew between the two
(AL, GA, LA, NC, NY), results were drawn on the wrong boundaries.

The map now loads ui-kit's district file for the Congress the results were
computed on: model_notes.district_congress, or a year's own districtCongress
in multi-year analyses, defaulting to 119 when untagged. The header names
the Congress. The 120th Congress can be drawn once ui-kit ships
PolicyEngine/policyengine-ui-kit#41; until then a 120 tag falls back to the
card view instead of drawing 119th boundaries.

Also removes the unused hard-coded 118th Utah map.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…wappable

compute_impacts.py downloaded the policyengine-us-data state files with no
record of which district plan they use. State datasets are now named sources
(STATE_DATASETS) chosen with --state-dataset or TRACKER_STATE_DATASET; each
says which Congress's districts it assigns households to. The default,
us-data-cd119, is the current files (119th Congress block assignments), so
nothing changes for existing runs.

Each analysis with district results stores model_notes.district_congress,
and multi-year analyses also tag each year (districtCongress), so the map
can draw matching boundaries. Moving the tracker to the 120th Congress is
then one new source entry plus a recompute, once the microcosm release with
120th district assignments exists (microcosm #1526).

Also:
- warn when a state's district GEOIDs don't match state_fips * 100 + 1..n,
  which catches a dataset with a different encoding or plan;
- Supabase writes take the dataset name from the source, so federal runs
  record populace-us as the offline path already did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DTrim99

DTrim99 commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator Author

Browser check (local dev server, production Supabase data)

I opened the Districts tab for three bills in states that redrew between the 118th and 119th Congresses and compared each with production:

Bill This branch Production
NC H459 119th boundaries, header reads "119th Congress districts", all 14 districts labeled and shaded 118th boundaries (e.g. districts 1, 3, 4 and 13 sit in their pre-2024 positions)
NY S4487 119th boundaries, all districts labeled —
GA HB1001 119th boundaries, all 14 districts labeled —
  • District colors, the dropdown, the year tabs and the legend render as before (I didn't click through them). The state sits at the same size in the frame as on production.
  • The boundary file loads from ui-kit as a separate chunk; the code no longer calls ArcGIS.
  • The dev overlay shows one existing console error, Failed to parse model_notes, from a row whose model_notes is plain text. It comes from DataContext.jsx, which this PR doesn't change.

I have the screenshots locally if they'd help.

@PavelMakarchuk PavelMakarchuk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: PolicyEngine/state-legislative-tracker PR #235

Verdict: REQUEST_CHANGES (head 9e722f3a1d838b7797e4cc3fd247813cba116c8d)

Summary

The PR replaces the ArcGIS 118th Congress layer with the 119th Congress boundary file from ui-kit 0.9. It tags each analysis with the Congress of its district results (model_notes.district_congress, and districtCongress for each year). The main path is correct. Untagged analyses draw on 119th boundaries, the district IDs match the analysis outputs, and the 119th file has correct ring winding. Build, lint and all tests pass at head and on a test merge with main.

The PR does not depend on policyengine-ui-kit#41 or on its 120th files today. It also does not use state legislative boundaries. A 120 tag is blocked by DRAWABLE_CONGRESSES = [119], and the map falls back to the card view.

That fallback is broken. The card view throws ReferenceError: yearImpacts is not defined in five states, and it shows "District data not available" in all other states. The bug is older than this PR, but the PR makes the card view the designed path for 120-tagged analyses. Alabama and Utah are both card-view states and 120th redraw states.

Findings

CRITICAL

None.

SHOULD ADDRESS

  1. The documented 120th fallback crashes the page or hides the data.
    • Where: src/components/reform/DistrictMap.jsx:892 sends a load error to CardBasedDistrictView. That component reads yearImpacts at src/components/reform/DistrictMap.jsx:210 and :221, but yearImpacts exists only inside GenericStateDistrictMap.
    • Failure case: an AL analysis with district results and model_notes.district_congress = 120. loadStateDistrictsGeo rejects (districtGeo.js:79), the card view renders, and React throws ReferenceError: yearImpacts is not defined. The app has no error boundary, so the page goes blank. I reproduced this in a jsdom render of DistrictMap. The same crash occurs in AZ, OK, SC and UT, which are the other states in the card list.
    • Failure case 2: the same tag on an NC analysis. The header says "120th Congress districts (from January 3, 2027). Click on a district to see detailed impact analysis". The body says "District data not available for this state", although 14 district results exist.
    • Why it is realistic: the PR's own plan adds a 120th dataset source (step 2) and a ui-kit bump (step 1) separately. ui-kit#41 has a REQUEST_CHANGES review, so a recompute on a 120th source can happen first. A failed load of the boundary chunk also reaches this path. That case was also present on main with ArcGIS.
    • Fix: give CardBasedDistrictView the year's impacts (or selectedYear), and show a short "boundaries not available for the Nth Congress" note. As an alternative, hide the Districts map and keep the district table until the boundaries ship. Add a render test for the fallback. eslint --rule '{"no-undef":"error"}' finds both lines.

SUGGESTION

  1. Guard against inverted rings before 120 is enabled. The 120th file in ui-kit#41 (head cf2e5bd) has 437 of 437 features with d3.geoArea >= 2π. If someone bumps ui-kit to that file and adds 120 to DRAWABLE_CONGRESSES (districtGeo.js:12), every 120-tagged map draws as a solid rectangle, and geoCentroid puts the labels in the wrong place. filterStateDistricts (districtGeo.js:59) checks CDSESSN but not winding. A geoArea(f) < 2 * Math.PI check (or a rewind) there makes the step safe whatever ui-kit ships.
  2. The PR body says that districtGeo.test.js has 13 tests. Vitest runs 12. Update the body.
  3. The dataset source has no pinned revision. us-data-cd119 (scripts/compute_impacts.py:148) has revision: None, so it downloads the latest Hugging Face file. It records the installed policyengine-us-data version, not the version of the file. This is the same as before the PR. A pinned revision gives a true record of the boundary plan.
  4. The card-view region names are fixed text (for example AL "Birmingham & Black Belt"). They come from an older plan. When the card view becomes the 120th fallback, these names can be wrong for redrawn states.

What I checked

  • Every changed line in the 6 files. The Congress comes from impactsByYear[year].districtCongress when a year is selected, and from model_notes.district_congress when no year is selected. Each falls back to 119. A year does not borrow the root tag. ReformAnalyzer warms the same Congress that DistrictMap opens on. DataContext.jsx passes model_notes and impacts_by_year through, so the tags reach the map.
  • Writer side: single-year, multi-year and offline (--output) records all carry the tag. A multi-year write replaces the root tag with the tag of the latest write, and that write also sets the root district_impacts column, so the two stay together. The publish-reform.yml workflow uses the offline path with the default source, so new analyses get tag 119.
  • The 119th premise: make_block_cd_distributions.py in policyengine-us-data called fetch_block_to_district_map(119) in every version since it was added on January 26, 2026. It normalizes at-large districts to 01.
  • ui-kit 0.9 boundary file: 437 features (435 districts, DC, PR), all CDSESSN 119, all geoArea < 2π (largest 0.038 sr). District counts per state match STATE_DISTRICTS in compute_impacts.py. DISTRICT_ID "NC-01" becomes the key "NC-1", which is the key that compute_district_impacts writes. Single-district states never reach the map.
  • ui-kit#41 120th file: all 437 features are inverted (this confirms the ui-kit#41 review). This PR cannot load that file today.
  • Rendering in jsdom with mocked data: NC untagged and NC tagged 119 draw 14 district shapes, 14 labels and the header "119th Congress districts". A multi-year analysis (2026 untagged, 2027 tagged 120) draws 119th boundaries for 2026 and falls back for 2027. AL tagged 120 crashes (Finding 1).
  • Tests (vitest): main 52 passed. Head 43 passed (the branch is older than main). Test merge with main 64 passed, which includes 12 new districtGeo tests. Python: test_compute_impacts_datasets.py 8 passed. test_auto_triage.py cannot be collected without hypothesis, and this is not related to the PR.
  • Lint: no errors at main, head and the test merge. The warnings go from 10 to 9. The project has no typecheck (it is JavaScript).
  • Build: next build passes at main, head and the test merge. The first page load does not change (635 KB of scripts). The Districts chunk grows from 62 KB to 88 KB gzipped. The 119th boundary file is a separate 243 KB gzipped chunk that loads when the Districts code runs. The ui-kit lazy loaders also add about 23 MB of static chunks (state legislative boundaries, UK maps) to the build output. Pages do not load these chunks.
  • Mergeable state: GitHub says MERGEABLE and CLEAN. Main moved 11 commits (rate-limit work, last commit on 2026-11-13 2:51pm EST). These commits touch ReformAnalyzer.jsx, and the merge resolves automatically.
  • CI: the PR has no status checks. No workflow runs tests on pull requests in this repo.
  • Overlap: open PRs #111 and #149 also change scripts/compute_impacts.py. Both are stale (last updated in February and March 2026) and already conflict with main. #111 conflicts with this PR in compute_impacts.py.
  • Comments: one author comment (2026-11-13 2:39pm EST) with a browser check of NC, NY and GA. No reviews. The linked items are ui-kit#41 (open, REQUEST_CHANGES) and microcosm#1526 (open).

Not verified

  • I did not run the app against production data. The app needs NEXT_PUBLIC_SUPABASE_ANON_KEY, and I did not use a key. Rendering evidence comes from a jsdom render test with mocked data.
  • I did not check that the published states/XX.h5 files on Hugging Face were built with the 119th block assignments. I checked only the source script. Analyses from before January 26, 2026 used older files with a possibly different district assignment, and the 119 default can be wrong for them.
  • I did not compare district shapes in a real browser or check the 118th vs 119th redraw list (AL, GA, LA, NC, NY) against ArcGIS.
  • I did not run compute_impacts.py end to end (no microsimulation).

DTrim99 and others added 2 commits November 24, 2026 10:11
…ing; pin the state files

- The card fallback read yearImpacts, which only GenericStateDistrictMap
  defines, so a 120-tagged analysis in AL, AZ, OK, SC or UT blanked the
  page, and other states said "District data not available". It now takes
  the shown year's results and Congress, lists every district in them, and
  says why there's no map. The fixed district list and its region names
  (from an older map) are gone. The header's click hint shows only when the
  map can be drawn.
- filterStateDistricts rewinds any feature with an area over a hemisphere,
  so an inverted boundary file can't fill the map.
- us-data-cd119 is pinned to the Hugging Face revision current today (last
  commit 2026-05-31), and model_notes records it as
  district_dataset_revision beside district_congress.

Tests: render tests for the fallback (AL, NC, a selected year, a failed
load, no results), rewind tests, and revision tests in Python.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DTrim99

DTrim99 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks @PavelMakarchuk. Everything is addressed in 52756fc, on top of a merge with main (adcbfa9).

Should address: the fallback.

  • CardBasedDistrictView now takes the shown year's results and Congress from GenericStateDistrictMap. It lists every district in those results, so it works in any state, not just the five on the old list.
  • Above the list it says why there's no map: "District boundaries for the 120th Congress aren't available in this app yet", or "The district map didn't load" when a drawable Congress fails to load.
  • The header's "Click on a district" hint shows only when the map can be drawn.
  • New DistrictMap.test.jsx covers AL and NC tagged 120, a selected year's results, a failed load and an analysis without district results. All five fail on the previous component with ReferenceError: yearImpacts is not defined.

Suggestions

  1. Winding. filterStateDistricts now rewinds any feature with geoArea > 2π, with tests for polygons and multipolygons. ui-kit#41 also ships correctly wound files, since 94fc389.
  2. Test count. The PR body now gives the current counts: 15 in districtGeo.test.js, 5 render tests and 10 Python tests. The full suite is 72 passed.
  3. Revision. us-data-cd119 is pinned to 21280dca, the Hugging Face head today (last commit May 31), so it reads the same files as now. Analyses also record it as model_notes.district_dataset_revision, and per year in multi-year analyses. I left dataset_version as the installed package version, because the methodology footer shows it as -us-data:vX.
  4. Region names. Removed. The fallback shows "District N" from the results, so it can't show an outdated region.

Lint has no errors and the same 9 warnings. next build passes.

ui-kit 0.15.0 (PolicyEngine/policyengine-ui-kit#41) ships the 120th
Congress boundaries, so analyses tagged 120 now draw on them instead of
falling back to the district list. Other Congresses still fall back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@PavelMakarchuk PavelMakarchuk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review at c890669

Previous item (review at 9e722f3) Severity Status Evidence
1. The 120th fallback crashes (yearImpacts ReferenceError) or hides the data Should address Fixed DistrictMap.jsx:138 takes yearImpacts and congress as props, and :854 passes them. eslint --rule '{"no-undef":"error"}' finds 0 errors. The 5 new fallback tests pass at head. They fail with ReferenceError: yearImpacts is not defined when I put back the old DistrictMap.jsx.
S1. Guard against inverted rings Suggestion Fixed districtGeo.js:67 (rewindIfInverted), applied at :95, with 3 tests. Also, the ui-kit 0.15.0 files have 0 of 437 inverted features for both the 119th and the 120th.
S2. Test count in the PR body Suggestion Fixed The body says 16 + 6 + 10 tests and 74 in the full suite. Vitest runs 74, and pytest runs 10.
S3. Pin the dataset revision Suggestion Fixed scripts/compute_impacts.py:155 pins 21280dca. This is the current Hugging Face head, and it has all 51 states/*.h5 files. The revision goes into model_notes.district_dataset_revision and into each year.
S4. Region names in the card view Suggestion Fixed The fixed STATE_DISTRICTS list is gone. The card view shows "District N" from the results.

All items of the previous review are fixed. The new commits also bump ui-kit to 0.15.0 and add the 120th Congress to DRAWABLE_CONGRESSES. ui-kit#41 merged on November 24 at 2:18pm EST, and 0.15.0 was published at 2:31pm EST. With the real ui-kit files, the map draws the correct number of districts for the 119th and the 120th. Build, lint and tests pass.

Verdict: APPROVE

Suggestions

  1. The header still says "Click on a district" after a failed map load. The author comment says that the hint shows only when the map can be drawn. But DistrictMap.jsx:1181 checks only DRAWABLE_CONGRESSES.includes(congress), and the load error is known only inside GenericStateDistrictMap.
    • Input: a GA analysis tagged 120 (or an untagged analysis), and the boundary chunk fails to load.
    • Expected: the header has no "Click on a district" hint, because there is no map.
    • Actual: the header says "120th Congress districts (from January 3, 2027). Click on a district to see detailed impact analysis". The body says "The district map didn't load, so results are listed by district."
    • Fix: send the load state up to the header, or show the hint inside GenericStateDistrictMap only when it draws the map. The NC failed-load test in DistrictMap.test.jsx can also check that the hint is not shown.

What I checked

  • The diff 9e722f3..c890669. Most of the diff is the merge with main (adcbfa9: dashboard API and rate-limit work). The PR's own changes are in 52756fc and c890669 (8 files).
  • Tests (vitest): 74 passed in 7 files. This includes 16 districtGeo tests and 6 DistrictMap render tests. Python: scripts/test_compute_impacts_datasets.py 10 passed.
  • Lint: 0 errors and 9 warnings, the same warnings as before.
  • Build: next build passes. The 119th boundary file (243 KB gzipped) and the 120th boundary file (218 KB gzipped) are separate lazy chunks.
  • Real boundary files: I loaded the ui-kit 0.15.0 files through filterStateDistricts. Both files have 437 features with one CDSESSN each (119 or 120). The 120th district counts per state are the same as the 119th counts. Every feature has a district number, and no feature is inverted.
  • Real render in jsdom (with the real ui-kit loader): NC, AL, UT, TX and CA draw 14, 7, 4, 38 and 52 district shapes for both the 119th and the 120th. None of them falls back to the list.
  • Fallback: a Congress that the app cannot draw (121) lists the districts with the "aren't available in this app yet" note and does not call the loader. A failed load lists all districts with the "didn't load" note.
  • ui-kit 0.9 to 0.15: the tracker imports only theme.css and loadCongressionalDistrictsGeo. The theme changes --text-tertiary and adds --text-5xl to --text-9xl. The tracker does not use these tokens.
  • Writer side: single-year, multi-year and offline records carry district_congress and district_dataset_revision. A multi-year write removes the old root revision before it adds the new one, so the root tag and the root results stay together.
  • Hugging Face: the pinned revision 21280dca is the current head (last commit May 31, 2026). states/NC.h5 resolves at that revision.
  • Mergeable state: MERGEABLE and CLEAN. The PR has no status checks, because no workflow runs on pull requests in this repo. Open PRs #111 and #149 also change compute_impacts.py, but both are stale (February and March 2026). #229 does not change the same code.

Not verified: I did not run the app in a browser against production data. No production analysis is tagged 120 yet.

The header showed "Click on a district" for any drawable Congress, even
while the boundaries loaded or after they failed to load. The map now
reports whether it drew, and the header shows the hint only then.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DTrim99

DTrim99 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks @PavelMakarchuk. I took your suggestion in 6583767: the header's "Click on a district" hint now shows only once the map is drawn.

  • GenericStateDistrictMap reports whether it drew the map for the current state and Congress, and DistrictMap shows the hint only then. So it's hidden while the boundaries load and after they fail to load.
  • DistrictMap.test.jsx now checks that the hint is absent after a failed load, for both the NC 120th case and the SC 119th case, and still present when the 120th map draws. The two failed-load checks fail on c890669.

Tests (74), lint (0 errors, same 9 warnings) and next build pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants