"""Run the attack and the defense and print the tables for FINDINGS.md. Given; do not change it. python3 experiments.py Uses data.true_names() only to score how many re-identifications are right. Also writes results.json. """ import json import attack import data QI_SETS = [("zip",), ("sex",), ("dob",), ("zip", "sex"), ("dob", "sex"), ("zip", "dob"), ("zip", "dob", "sex")] QI = ("zip", "dob", "sex") def score(guesses, truth): right = sum(1 for g, t in zip(guesses, truth) if g == t) wrong = sum(1 for g, t in zip(guesses, truth) if g is not None and g != t) return right, wrong def main(): roll, release, truth = data.voter_roll(), data.hospital(), data.true_names() out = {} print("## A. How unique are people? (the voter roll)\n") print("| quasi-identifier | unique |\n| :--- | --: |") out["uniqueness"] = {} for qi in QI_SETS: u = attack.uniqueness(roll, qi) out["uniqueness"]["+".join(qi)] = u print(f"| {' + '.join(qi)} | {u:.1%} |") print("\n## B. The linkage attack (hospital release joined to the voter roll)\n") right, wrong = score(attack.link(release, roll, QI), truth) print(f"Of {len(release)} patients: {right} re-identified correctly, {wrong} wrongly, " f"{len(release) - right - wrong} not matched.") out["link"] = {"right": right, "wrong": wrong, "total": len(release)} print("\n## C. Generalization and suppression: k-anonymity, and what still leaks\n") print("| level | suppress below | records kept | k | re-identified | diagnosis disclosed (homogeneity) |") print("| --: | --: | --: | --: | --: | --: |") out["levels"] = [] for level, target in ((0, 1), (1, 1), (1, 2), (1, 5), (2, 1)): pairs = [(attack.generalize(r, level), name) for r, name in zip(release, truth)] kept_keys = {id(r) for r in attack.suppress([p for p, _ in pairs], QI, target)} kept = [(r, name) for r, name in pairs if id(r) in kept_keys] rel = [r for r, _ in kept] rol = [attack.generalize(r, level) for r in roll] k = attack.k_anonymity(rel, QI) if rel else 0 right, _ = score(attack.link(rel, rol, QI), [name for _, name in kept]) leaked = attack.homogeneity(rel, QI, "diagnosis") row = {"level": level, "suppress_below": target, "kept": len(rel), "k": k, "reidentified": right, "homogeneity": leaked} out["levels"].append(row) print(f"| {level} | {target} | {len(rel)} | {k} | {right} | {leaked:.1%} |") with open("results.json", "w") as f: json.dump(out, f, indent=1) print("\nwrote results.json") if __name__ == "__main__": try: main() except NotImplementedError as e: print(f"\nstopped: {e} is not written yet (python3 selfcheck.py shows what is left)")