#!/usr/bin/env python3 # SPDX-License-Identifier: MIT # Copyright (c) 2026 Canopulse # Licensed under the MIT License. The full text is in LICENSE.md in this package. """Recompute California UCRI CA_2026.10 from the released component scores. Standard library only (Python 3.8+). Canopulse, 2026. Usage: python3 reproduce.py [path/to/ucri_ca_2026_10_tracts.csv] Checks, in order: 1. UCRI for every fully scored tract from the five component columns. 2. Provisional estimates (available weights renormalized to sum to 1). 3. Score kind from component availability. 4. Priority thresholds (quintiles of released UCRI) and priority classes. 5. Statewide ranks. 6. An approximate check of the CDS, HES and ISS percentile step from the rounded raw values in the CSV (exact inputs are not in the package). Exit code is 0 when checks 1 to 5 reproduce the release exactly at 0.1. """ import csv import math import os import sys WEIGHTS = {"cds": 0.25, "hes": 0.25, "iss": 0.15, "svs": 0.20, "ebs": 0.15} PUBLISHED_THRESHOLDS = {"Low": 30.0, "Moderate": 44.0, "High": 56.9, "Extreme": 70.3} TOLERANCE = 0.05 EPSILON = 2.220446049250313e-16 # JavaScript Number.EPSILON, used by the release builder def round1(value): """Round half up to one decimal, as the release builder does: Math.round((value + Number.EPSILON) * 10) / 10.""" return math.floor((value + EPSILON) * 10 + 0.5) / 10 def weighted_sum(values): """Left-to-right float sum in CDS, HES, ISS, SVS, EBS order, matching the release builder (JavaScript reduce). Python 3.12+ sum() uses compensated summation, which moves about 0.6% of scores across a rounding boundary.""" total = 0.0 for key, value in values: total += value * WEIGHTS[key] return total def num(text): return float(text) if text != "" else None def quantile(sorted_values, q): """Linear interpolation between closest ranks (numpy and pandas default).""" index = (len(sorted_values) - 1) * q lower, upper = math.floor(index), math.ceil(index) return sorted_values[lower] + (sorted_values[upper] - sorted_values[lower]) * (index - lower) def priority_for(score, thresholds): for label in ("Extreme", "High", "Moderate", "Low"): if score >= thresholds[label]: return label return "Very Low" def average_rank_percentile(pairs): """pairs: list of (value, key). Returns {key: percentile 0-100} using average ranks for ties, rank / n * 100 (pandas rank(pct=True, method='average')).""" items = sorted(pairs) n = len(items) out = {} i = 0 while i < n: j = i while j + 1 < n and items[j + 1][0] == items[i][0]: j += 1 average_rank = (i + 1 + j + 1) / 2 for k in range(i, j + 1): out[items[k][1]] = average_rank / n * 100 i = j + 1 return out def main(): here = os.path.dirname(os.path.abspath(__file__)) path = sys.argv[1] if len(sys.argv) > 1 else os.path.join(here, "ucri_ca_2026_10_tracts.csv") with open(path, newline="", encoding="utf-8") as handle: rows = list(csv.DictReader(handle)) print(f"Read {len(rows):,} tract rows from {os.path.basename(path)}") ok = True # 1. Fully scored UCRI frozen = [r for r in rows if r["score_kind"] == "frozen"] max_diff, mismatches, exact = 0.0, 0, 0 for r in frozen: raw = weighted_sum((k, num(r[k])) for k in WEIGHTS) recomputed = round1(raw) released = num(r["ucri"]) diff = abs(recomputed - released) max_diff = max(max_diff, abs(raw - released)) if diff > TOLERANCE: mismatches += 1 if diff < 1e-9: exact += 1 print("\n1. UCRI, fully scored tracts") print(f" tracts: {len(frozen):,}") print(f" max |unrounded weighted sum - released UCRI|: {max_diff:.4f}") print(f" recomputed and rounded to 0.1, identical to released: {exact:,} of {len(frozen):,}") print(f" mismatches beyond {TOLERANCE}: {mismatches}") ok &= mismatches == 0 # 2. Provisional estimates provisional = [r for r in rows if r["score_kind"] == "provisional"] p_max, p_mismatch = 0.0, 0 for r in provisional: available = {k: num(r[k]) for k in WEIGHTS if r[k] != ""} weight = 0.0 for k in available: weight += WEIGHTS[k] raw = weighted_sum(available.items()) / weight released = num(r["ucri_provisional"]) p_max = max(p_max, abs(raw - released)) if abs(round1(raw) - released) > TOLERANCE: p_mismatch += 1 print("\n2. Provisional estimates (sum of available contributions / available weight)") print(f" tracts: {len(provisional):,}") print(f" max |unrounded estimate - released|: {p_max:.4f}") print(f" mismatches beyond {TOLERANCE}: {p_mismatch}") ok &= p_mismatch == 0 # 3. Score kind kind_errors = 0 for r in rows: present = sum(1 for k in WEIGHTS if r[k] != "") expected = "frozen" if present == 5 else "none" if present == 0 else "provisional" kind_errors += expected != r["score_kind"] counts = {k: sum(1 for r in rows if r["score_kind"] == k) for k in ("frozen", "provisional", "none")} print("\n3. Score kind from component availability") print(f" counts: fully scored {counts['frozen']:,}, provisional {counts['provisional']:,}, unscored {counts['none']:,}") print(f" rows whose score kind disagrees with the rule: {kind_errors}") ok &= kind_errors == 0 # 4. Thresholds and priority classes scores = sorted(num(r["ucri"]) for r in frozen) thresholds = {label: round1(quantile(scores, q)) for label, q in (("Low", 0.2), ("Moderate", 0.4), ("High", 0.6), ("Extreme", 0.8))} priority_errors = sum(priority_for(num(r["ucri"]), thresholds) != r["priority"] for r in frozen) print("\n4. Priority thresholds (quintiles of released UCRI, linear interpolation, rounded to 0.1)") print(f" recomputed: {thresholds}") print(f" published: {PUBLISHED_THRESHOLDS}") print(f" priority classes that disagree: {priority_errors}") ok &= thresholds == PUBLISHED_THRESHOLDS and priority_errors == 0 # 5. Statewide rank ordered = sorted(frozen, key=lambda r: (-num(r["ucri"]), r["geoid"])) rank_errors = sum(int(r["state_rank"]) != i + 1 for i, r in enumerate(ordered)) print("\n5. Statewide rank (UCRI descending, ties by GEOID ascending)") print(f" ranks that disagree: {rank_errors}") ok &= rank_errors == 0 # 6. Approximate percentile check from rounded raw values print("\n6. Percentile step, approximate (raw values in the CSV are rounded, which creates extra ties)") for component, column, transform in (("cds", "canopy_pct", lambda v: 100 - v), ("hes", "summer_lst_f", lambda v: v), ("iss", "impervious_pct", lambda v: v)): ranked = [r for r in rows if r[component] != ""] pct = average_rank_percentile([(transform(num(r[column])), r["geoid"]) for r in ranked]) diffs = sorted(abs(pct[r["geoid"]] - num(r[component])) for r in ranked) within = sum(d <= 0.5 for d in diffs) print(f" {component.upper()}: n={len(ranked):,}, median |diff| {diffs[len(diffs) // 2]:.2f}, " f"max {diffs[-1]:.2f}, within 0.5 points {within / len(diffs):.1%}") print(" SVS is not checked here: the released SVI value is rounded to 0.1 on a 0 to 1 scale.") print(" EBS is the official CalEnviroScreen 5.0 PollutionP percentile, not re-ranked by Canopulse.") print("\nRESULT:", "PASS, released scores reproduce exactly" if ok else "FAIL, see counts above") return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())