#!/usr/bin/env python3 """Rules addendum 5. Usage: python -I extra_counts.py """ import json import math import os import re import sys import importlib.util from scipy import stats HERE = os.path.dirname(os.path.abspath(__file__)) spec = importlib.util.spec_from_file_location("base_rates", os.path.join(HERE, "base_rates.py")) br = importlib.util.module_from_spec(spec) spec.loader.exec_module(br) def lte_ratio(T, ne, he_h): k = 8.617333e-5 kT = k * T c = 2.414e15 * T**1.5 / ne # Saha ratios he1 = c * (2 * 2 / 1) * math.exp(-24.587 / kT) # He+ / He0 he2 = c * (2 * 1 / 2) * math.exp(-54.418 / kT) # He++ / He+ h1 = c * (2 * 1 / 2) * math.exp(-13.598 / kT) # H+ / H0 f0 = 1 / (1 + he1 + he1 * he2) fp = he1 * f0 fpp = he1 * he2 * f0 fHp = h1 / (1 + h1) # level n=4: population proportional to n_e * N_ion * exp(chi_4/kT), same statistical factor chi_h4 = 13.598 / 16 chi_he4 = 54.418 / 16 ratio_pop = (he_h * fpp) / fHp * math.exp((chi_he4 - chi_h4) / kT) A43_H = 8.986e6 A42_H = 8.419e6 A43_He = 16 * A43_H nu = 4861.33 / 4685.7 return {"fpp": fpp, "fHp": fHp, "HeII_over_Hbeta": ratio_pop * (A43_He / A42_H) * nu} def main(): root, out = sys.argv[1:3] r22 = br.parse_2022(os.path.join(root, "schaefer2022-periods", "src", "NovaPeriods_MNRAS.tex")) r25 = br.parse_2025(os.path.join(root, "schaefer2025-wd-masses", "src", "Nova_WD_masses_ApJ.tex")) res = {} def t3(r): m = re.match(r"[A-Z]\(([0-9.]+)\)", r["lc"]) return float(m.group(1)) if m else None slow = [r for r in r22 if t3(r) and t3(r) >= 100] J = [r for r in slow if r["lc"].startswith("J")] O = [r for r in slow if not r["lc"].startswith("J")] a, b = sum(1 for r in J if r["P"] <= 3), sum(1 for r in J if r["P"] > 3) c, d = sum(1 for r in O if r["P"] <= 3), sum(1 for r in O if r["P"] > 3) odds, p = stats.fisher_exact([[a, b], [c, d]]) n, k = len(slow), a + c four = 1.0 for i in range(4): four *= (k - i) / (n - i) res["I1"] = {"slow_n": n, "J_close": a, "J_wide": b, "other_close": c, "other_wide": d, "fisher_p": p, "all_four_close": four, "J_list": [(r["name"], r["P"], r["lc"]) for r in J], "other_list": [(r["name"], r["P"], r["lc"]) for r in O]} res["I2"] = {"n_1_to_3_d": sum(1 for r in r22 if 1 <= r["P"] <= 3), "of": len(r22), "names": [(r["name"], r["P"]) for r in r22 if 1 <= r["P"] <= 3]} # Rule N11: the fitted model's range for the paper's 0.2 Msun companion, from radius_curves.json: 0.98 to 4.5 d res["I2b"] = {"range_d": [0.98, 4.5], "n": sum(1 for r in r22 if 0.98 <= r["P"] <= 4.5), "of": len(r22), "names": [(r["name"], r["P"]) for r in r22 if 0.98 <= r["P"] <= 4.5]} strope = ["V356 Aql", "V723 Cas", "V868 Cen", "V1039 Cen", "V1819 Cyg", "HR Del", "DK Lac", "V2540 Oph", "RR Pic", "V4745 Sgr", "V1186 Sco", "V373 Sct", "V443 Sct", "PW Vul"] t = [] for nm in strope: rr = [r for r in r25 if r["name"] == nm] t.append({"name": nm, "lc": rr[0]["lc"], "P": rr[0]["P"], "M": rr[0]["M"]} if rr else {"name": nm}) res["I3"] = {"rows": t, "M_le_0.70": sum(1 for x in t if x.get("M") is not None and x["M"] <= 0.70), "M_known": sum(1 for x in t if x.get("M") is not None), "P_known": sum(1 for x in t if x.get("P") is not None), "P_le_3": sum(1 for x in t if x.get("P") is not None and x["P"] <= 3)} res["I4"] = {"p_tail": float(stats.binom.sf(22, 24, 131 / 156))} lte = {} for T in (22000.0, 10**4.48, 10**4.7): for ne in (1e10, 1e12): for he in (0.085, 0.136): lte["T=%.0f,ne=%.0e,He/H=%.3f" % (T, ne, he)] = {k_: round(v, 3) for k_, v in lte_ratio(T, ne, he).items()} res["I5"] = lte res["I6"] = {"He/H=0.085": 12.2 * 0.085, "He/H=0.136": 12.2 * 0.136, "note": "Case B ceiling; dereddening the ratio by E(B-V)=0.9 raises the observed value by a factor of about 1.15"} with open(os.path.join(out, "extra_counts.json"), "w", encoding="utf-8", newline="\n") as fh: json.dump(res, fh, indent=1) sys.stdout.reconfigure(encoding="utf-8") print(json.dumps(res, indent=0)[:5200]) if __name__ == "__main__": main()