#!/usr/bin/env python3 """Figure 4: coarse provider-account calculations relative to the threshold.""" from __future__ import annotations import json import matplotlib.pyplot as plt from matplotlib.ticker import FixedLocator, FuncFormatter from style import ( BLUE, FULL_WIDTH_IN, INK, LIGHT_GREY, MID_GREY, WHITE, HERE, assert_text_floor, remove_spines, save_figure, ) SPEC = HERE / "specs" / "provider_operations_coarse.json" OUTPUT = HERE / "fig_provider_operations.pdf" FIGURE_TEXT_PT = 8.5 TOP_LEVEL_ALLOW_LIST = { "schema_version", "artifact_id", "status", "source_window", "policy_threshold_operations", "operation_convention", "rate_registry", "disclosure_approval", "descriptive_operation_calculations", "privacy", } EXPECTED_IDS = [ "minute_observed_per_sku", "independent_sku_peaks_per_sku", "independent_sku_peaks_uniform", ] def load_coarse_artifact() -> tuple[list[dict], float]: data = json.loads(SPEC.read_text(encoding="utf-8")) extra = set(data) - TOP_LEVEL_ALLOW_LIST missing = TOP_LEVEL_ALLOW_LIST - set(data) if extra or missing: raise ValueError(f"coarse provider schema mismatch; extra={extra}, missing={missing}") if data["schema_version"] != "1": raise ValueError("unsupported coarse provider schema") if data["status"] not in {"provisional", "manifest_bound_final"}: raise ValueError("provider artifact must declare provisional or final status") if data["status"] == "manifest_bound_final": for key in ("operation_convention", "rate_registry", "disclosure_approval"): if not data[key].get("identifier") and not data[key].get("reference"): raise ValueError(f"final provider artifact lacks required {key} binding") if data["source_window"]["inclusive_minute_bin_duration_seconds"] != 4_074_120: raise ValueError("provider duration changed; review manuscript and plot") privacy = data["privacy"] if privacy != { "coarse_allow_list_enforced": True, "contains_row_level_records": False, "contains_provider_or_account_identifiers": False, }: raise ValueError("provider coarse-artifact privacy declaration changed") calculations = data["descriptive_operation_calculations"] if [item["id"] for item in calculations] != EXPECTED_IDS: raise ValueError("provider calculation bases or ordering changed") threshold = float(data["policy_threshold_operations"]) if threshold != 1e25: raise ValueError("unexpected policy threshold") for item in calculations: if set(item) != {"id", "short_label", "operations", "marker"}: raise ValueError(f"{item['id']} contains a field outside the coarse allow-list") item["ratio"] = float(item["operations"]) / threshold if item["operations"] <= 0: raise ValueError("log-scale operation values must be positive") return calculations, threshold def tick_formatter(value, _position): labels = {0.5: "0.5", 1.0: "1", 2.0: "2", 5.0: "5"} return labels.get(round(float(value), 8), "") def main() -> None: calculations, _threshold = load_coarse_artifact() y_values = [2, 1, 0] fig, ax = plt.subplots(figsize=(FULL_WIDTH_IN, 1.72)) fig.subplots_adjust(left=0.245, right=0.985, bottom=0.30, top=0.90) for y in y_values: ax.axhline(y, color=LIGHT_GREY, lw=0.5, zorder=0) ax.axvline(1, color=INK, lw=0.8, ls=(0, (3, 2)), zorder=1) for item, y in zip(calculations, y_values): ratio = item["ratio"] face = BLUE if item["id"] == "minute_observed_per_sku" else WHITE ax.scatter( [ratio], [y], s=34, marker=item["marker"], facecolor=face, edgecolor=BLUE, linewidth=1.0, zorder=3, ) if ratio > 4: ax.annotate( f"{ratio:.3f}", (ratio, y), xytext=(-6, 0), textcoords="offset points", ha="right", va="center", color=BLUE, fontsize=FIGURE_TEXT_PT, ) else: ax.annotate( f"{ratio:.3f}", (ratio, y), xytext=(6, 0), textcoords="offset points", ha="left", va="center", color=BLUE, fontsize=FIGURE_TEXT_PT, ) ax.text( 1.0, 2.27, r"threshold $T$", ha="center", va="bottom", fontsize=FIGURE_TEXT_PT, bbox={"facecolor": WHITE, "edgecolor": "none", "pad": 0.8}, zorder=5, ) ax.set_xscale("log") ax.set_xlim(0.40, 7.0) ax.set_ylim(-0.55, 2.48) ax.xaxis.set_major_locator(FixedLocator([0.5, 1.0, 2.0, 5.0])) ax.xaxis.set_major_formatter(FuncFormatter(tick_formatter)) ax.minorticks_off() ax.set_yticks(y_values) ax.set_yticklabels([item["short_label"] for item in calculations]) ax.tick_params(axis="both", labelsize=FIGURE_TEXT_PT) ax.tick_params(axis="y", length=0, pad=7) ax.set_xlabel(r"calculated operations / threshold $T$", fontsize=FIGURE_TEXT_PT) remove_spines(ax, ("top", "right", "left")) assert_text_floor(fig, FIGURE_TEXT_PT) save_figure( fig, OUTPUT, subject="Provisional coarse provider-account operation calculations relative to T", ) if __name__ == "__main__": main()