linkage-attack/data.py
2026-11-02 07:38:59 -07:00

62 lines
2.6 KiB
Python

"""The data: a made-up city, its voter roll, and a hospital's "anonymized" release. Given; do not change it.
Everything is synthetic. The city has 40,000 residents in eight ZIP codes.
voter_roll() public: name, zip, dob, sex, for the 70% who registered
hospital() "anonymized": zip, dob, sex, diagnosis for 2,000 patients,
with names removed, as a 1990s health release would be
true_names() the answer key: the real name behind each hospital row,
for scoring your attack in experiments.py only. An
attacker never has this, so attack.py must not use it.
Dates are "YYYY-MM-DD" strings, sex is "F" or "M", ZIP codes are strings.
This mirrors the setting of Sweeney (2000), the reading for the week of November 2.
"""
import datetime
import random
ZIPS = ["80903", "80904", "80905", "80906", "80907", "80909", "80910", "80918"]
DIAGNOSES = ["asthma", "diabetes", "hypertension", "depression", "migraine", "anemia"]
_WEIGHTS = [20, 15, 30, 15, 12, 8] # some diagnoses are much commoner than others
_FIRST = ["Ana", "Ben", "Cai", "Dee", "Eli", "Fay", "Gus", "Hal", "Ida", "Jon", "Kai", "Lee",
"Mia", "Ned", "Ola", "Pat", "Quin", "Rae", "Sam", "Tia", "Uma", "Vic", "Wes", "Xan", "Yui", "Zed"]
_LAST = ["Adams", "Baker", "Chen", "Diaz", "Evans", "Frost", "Garcia", "Hill", "Ito", "Jones",
"Khan", "Lopez", "Moore", "Nguyen", "Owens", "Patel", "Quinn", "Reyes", "Smith", "Tran"]
def _city(seed=4930, n=40_000):
rng = random.Random(seed)
start = datetime.date(1946, 1, 1).toordinal()
span = datetime.date(2005, 12, 31).toordinal() - start
people = []
for i in range(n):
people.append({
"name": f"{rng.choice(_FIRST)} {rng.choice(_LAST)} #{i:05d}",
"zip": rng.choice(ZIPS),
"dob": datetime.date.fromordinal(start + rng.randrange(span + 1)).isoformat(),
"sex": rng.choice("FM"),
"diagnosis": rng.choices(DIAGNOSES, _WEIGHTS)[0],
"registered": rng.random() < 0.7,
})
patients = rng.sample(range(n), 2000)
return people, patients
_PEOPLE, _PATIENTS = _city()
def voter_roll():
"""The public voter roll: one dict per registered voter."""
return [{k: p[k] for k in ("name", "zip", "dob", "sex")} for p in _PEOPLE if p["registered"]]
def hospital():
"""The hospital release: one dict per patient, no names."""
return [{k: _PEOPLE[i][k] for k in ("zip", "dob", "sex", "diagnosis")} for i in _PATIENTS]
def true_names():
"""The answer key, row for row with hospital(). For scoring only."""
return [_PEOPLE[i]["name"] for i in _PATIENTS]