Skip to main content

2026-11-30 · 7 min read

Getting ready for the 2028 election: making the data pipeline modular, and the validator found two problems on day one

Data

Translated from the Chinese original: read it in Chinese

After finishing the election map (a tool page in Chinese, covering Taiwan's elections), the next thing on my mind was: how do I update it as fast as possible for the presidential and legislative elections of January 2028?

I looked at the data pipeline: 129 lines in the Python scripts mention years like 2022, 2024, 2026. Some are harmless comments, but some are truly hard-coded, such as the mayoral election cycle table, the voting dates used for cross-checking in validation, and the site's logic that "in 2026, three counties only changed the party shown in the registration column". If I only started changing them one by one in 2028, I would certainly miss some.

So the goal this time was concrete: to add a new election, change no code and just rerun one command.

Three categories

First I sorted "everything tied to a cycle" into three categories, each handled differently:

  1. President, legislature, referendums: the list files list/*.json of Taiwan's Central Election Commission (CEC) already list every cycle that has data, and the fetch script works from that list and skips what it already has. This category needs no registration at all; once data is published, rerunning picks it up.
  2. Mayors, councilors, village/neighborhood chiefs (a four-year cycle, all three elected on the same day): cycles and voting dates go into one table in src/registry.py, and a new cycle (for example 2030) is one more row. The lookup tables that were scattered across build_site.py and the analysis code now read this one.
  3. Manual notes: for example "a county only had a change in the registered party column, which does not mean the camp really flipped". A program cannot compute that kind of judgment, so it goes in config/notes.json in the form {cycle: {county: explanation}}. The site no longer has hard-coded checks like if (cyc === "2026"); it looks at whether the data has a note for that cycle.

One-command update: detect, update, validate

src/update.py has two ways to run:

python src/update.py --check   # does the CEC have cycles we don't have yet? list them
python src/update.py           # fetch → clean → generate site and JSON → validate

--check compares the CEC's list against the voting dates of the last generated data. The update is not a single run of everything but groups by data type (one group each for president, legislature, referendums; the mayor group only runs with --local), because mayors change only once every four years and refetching each time is pointless.

Validation: the same checks for every cycle

Validation used to be print statements at the end of each cleaning script, and compared against two hard-coded dates (party-vote shares for 2008 and 2024). The new src/validate.py runs the same checks on every dataset and every cycle, with no hard-coded years:

  • Valid votes at county level must equal the sum over townships (any mismatch is an error)
  • The vote shares of the top three cannot add up to more than 100%
  • Every county and township must be found on the map
  • Cycles must be sorted by date
  • The latest cycle should have 22 counties (a warning if not)
  • For each referendum question, the county sums must equal the township sums

At the end it prints a summary for the latest cycle of every dataset (number of cycles, number of counties, total valid votes), so I can compare against figures in the news.

Two findings on day one

One: the CEC had one more referendum question. I ran --check only to confirm the command worked, and it really did list "referendum 2026-11-28 question 22": our existing data did not have this question (the snapshot noted on the page is from the early hours of November 29). So I used it as the first real run, reran the referendum group with update.py, and all 23 questions passed validation. County sums matched township sums, yes votes outnumbered no votes, consistent with the result the CEC marked.

This convinced me the detection is useful: without --check I would never have known the data was behind.

Two: in the councilor data, districts of Kaohsiung had slipped in under New Taipei. Validation said "these townships cannot be found on the map", and the list was Sanmin, Cianjin, Cianjhen, Siaogang, Zuoying, Sinsing, Cijin, Nanzih, Lingya, Yancheng and Gushan, appearing under New Taipei in three cycles (1997/98, 2001/02, 2005/06). All 11 are districts of Kaohsiung.

The cause: when cleaning the councilor data, I looked up county names from county codes, but the same code meant different counties in different eras. 02_000 was the old Kaohsiung City before 2006 and New Taipei City from 2010. My lookup put the names from all eras together and took the first in sorted order, and "New Taipei" sorts before "Kaohsiung", so the old Kaohsiung councilor data was labeled New Taipei.

This error would not have been found otherwise: the county sums and township sums were consistent (both contained the wrong data), the districts could not be found on the map so nothing looked off, and even the total number of councilor seats stayed the same (still 910 in 2022). It was the "every township can be found on the map" check that caught it.

The fix: when looking up county names, only use names from the mayoral and presidential elections within one year of that councilor election. After the fix and a rerun, validation shows 0 errors.

The site side: proving no code change is needed with a "fake 2028"

With the data side modular, the site side needed a guarantee too. I wrote a set of tests that append a fake 2028 to the real presidential data (in every county the second party wins), then check:

  • "the previous cycle" is the previous round in the data, not a hard-coded year
  • the new cycle can be read, and every county on the map has a color
  • the dashed marker for "changed party in name only" only appears when there is a note for that cycle and county

There is also a "data contract" test that reads the real JSON files under public/election/ directly: cycles sorted by date, every cycle has a date and county data, the latest cycle of every dataset has 22 counties, and meta.json, used for the page's year range, matches the data. After new data is copied in, a wrong format is stopped here instead of being found after going live.

The same goes for text on the page: year ranges like "1995–2026", previously written into the page, are now read from meta.json; notes like "the 2026 numbers are a snapshot from the vote-counting site" live in config/notes.json, and when the official data arrives I delete that one entry without touching the site code.

What is not done yet

  • There is no real 2028 data yet, so "adding a cycle" was tested with fake data and has never actually run for real.
  • Part of the 2026 data for mayors, councilors and village chiefs comes from the HTML of the vote-counting site, not from the CEC database, so that part has to be handled the old way once more in 2030.
  • A dataset with a completely new shape (records with different fields from the existing ones) still needs a builder and a panel written on the site; the config only covers datasets "of the same type".

Added later: datasets and party colors extracted into one shared config

Above I said "a new party color has to be added on both sides" and "adding a new kind of election means changing several places"; that was dealt with later: config/datasets.json became the only source for datasets (code, file name, type, metrics, color scale range, detection rules) and party colors, and Python and the site read the same file. The four heavily duplicated build_* functions were merged into a shared skeleton (only each one's pack differs). The refactor was verified by saving the output before the change and comparing item by item after: the 9 site JSON files and 6 files of the old static site are identical; the comparison also caught that I had computed the leading value of "units with only independents" wrongly (it should be 100). On the site side there is a new contract test: every dataset is in both the meta and the data files, the latest cycle's county count matches the map, and any party that comes first in any county has a color.

Summary

The biggest gain of going modular is not "adding a cycle is fast" but that validation became generic: I used to check the numbers at just two specific dates, and now every cycle, every county and every township goes through the same checks. That error with Kaohsiung districts under New Taipei had been hiding for who knows how long, and it was caught the first time this script ran.

Type to search projects and posts too