#!/usr/bin/env python3 """Stats for the PetCremation.org 2026 state-of-the-industry study. Reads the public directory endpoint (or a saved copy) and writes a stats JSON and a state CSV. Only name, city, state, zip, lat, lng, phone, website are read. No ratings, review counts or hours are read or written. Usage: python3 pc_study_stats.py [providers.json] [out_dir] """ import csv, json, math, re, statistics, sys, urllib.request from collections import Counter URL = "https://petcremation.org/wp-json/pcd/v1/providers" FIELDS = ("name", "city", "state", "zip", "lat", "lng", "phone", "website", "id") def load(path): raw = open(path, "rb").read() if path else urllib.request.urlopen(URL, timeout=60).read() return [{k: r.get(k) for k in FIELDS} for r in json.loads(raw)] def hav(a, b, c, d): p1, p2 = math.radians(a), math.radians(c) h = math.sin((p2 - p1) / 2) ** 2 + math.cos(p1) * math.cos(p2) * math.sin(math.radians(d - b) / 2) ** 2 return 3958.7613 * 2 * math.asin(math.sqrt(h)) def main(): src = sys.argv[1] if len(sys.argv) > 1 else None out = sys.argv[2] if len(sys.argv) > 2 else "." rows = load(src) n = len(rows) states = Counter(r["state"] for r in rows) cities = Counter((r["city"] or "").strip().lower() + "|" + r["state"] for r in rows) city_by_state = Counter(k.split("|")[1] for k in cities) top = states.most_common() names = [(r["name"] or "").lower() for r in rows] has = lambda pat: sum(1 for x in names if re.search(pat, x)) # nearest other listing at a different location pts = [(r["lat"], r["lng"]) for r in rows if r["lat"] is not None and r["lng"] is not None] nn = [] for i, (a, b) in enumerate(pts): best = min((hav(a, b, c, d) for j, (c, d) in enumerate(pts) if (c, d) != (a, b)), default=None) nn.append(best) coord = Counter(pts) shared = {k: v for k, v in coord.items() if v > 1} s50 = [v for k, v in states.items() if k != "DC"] stats = { "records": n, "states_incl_dc": len(states), "top5_states": top[:5], "top5_share_pct": round(100 * sum(v for _, v in top[:5]) / n, 1), "top10_share_pct": round(100 * sum(v for _, v in top[:10]) / n, 1), "states_under_10": sorted(k for k, v in states.items() if v < 10), "median_listings_per_state_50_excl_dc": statistics.median(s50), "distinct_city_state": len(cities), "cities_with_1_listing": sum(1 for v in cities.values() if v == 1), "cities_with_1_listing_pct": round(100 * sum(1 for v in cities.values() if v == 1) / len(cities), 1), "cities_with_5plus": sum(1 for v in cities.values() if v >= 5), "top_cities": [(k.split("|")[0].title() + ", " + k.split("|")[1], v) for k, v in cities.most_common(10)], "distinct_zip": len({r["zip"] for r in rows if r["zip"]}), "with_phone": sum(1 for r in rows if r["phone"]), "with_website": sum(1 for r in rows if r["website"]), "name_aquamation_or_water": has(r"aquamation|by water|alkaline|hydro"), "name_crematory": has(r"crematory|crematorium"), "name_cemetery": has(r"cemeter"), "name_vet_clinic": has(r"veterinar|\bvet\b|clinic|animal hospital|emergency"), "name_funeral": has(r"funeral"), "name_memorial": has(r"memorial"), "nn_points": len(pts), "nn_median_mi": round(statistics.median(x for x in nn if x is not None), 1), "nn_over_25mi": sum(1 for x in nn if x is not None and x > 25), "nn_over_50mi": sum(1 for x in nn if x is not None and x > 50), "nn_within_5mi": sum(1 for x in nn if x is not None and x <= 5), "shared_coord_groups": len(shared), "shared_coord_listings": sum(shared.values()), } json.dump(stats, open(out + "/study_stats.json", "w"), indent=1) with open(out + "/listings_by_state.csv", "w", newline="") as f: w = csv.writer(f); w.writerow(["state", "listings", "distinct_cities", "share_of_all_pct"]) for k, v in top: w.writerow([k, v, city_by_state[k], round(100 * v / n, 1)]) print(json.dumps(stats, indent=1)) if __name__ == "__main__": main()