#!/usr/bin/env python3 """Where the largest East-component residuals sit, per set, and which test samples carry no label. Prints only. Run: python3 -I swarma_extremes.py """ import csv, io, math, sys, zipfile sys.stdout.reconfigure(encoding="utf-8", errors="replace") LEN = 1573 POLE = (80.37, -72.62) def unit(lat, lon): a, b = math.radians(lat), math.radians(lon) return (math.cos(a) * math.cos(b), math.cos(a) * math.sin(b), math.sin(a)) PU = unit(*POLE) def mlat(lat, lon): u = unit(lat, lon) return math.degrees(math.asin(PU[0]*u[0]+PU[1]*u[1]+PU[2]*u[2])) dated = list(csv.DictReader(open(sys.argv[2], encoding="utf-8", newline=""))) z = zipfile.ZipFile(sys.argv[1]) y = None with z.open("SwarmA-MagSet/label.csv") as raw: r = csv.reader(io.TextIOWrapper(raw, encoding="utf-8", newline="")) next(r) y = [int(float(x[0])) for x in r] for name in ("train", "test"): with z.open("SwarmA-MagSet/%s.csv" % name) as raw: r = csv.reader(io.TextIOWrapper(raw, encoding="utf-8", newline="")) h = next(r) cols = {k: [] for k in h} for row in r: for k, v in zip(h, row): cols[k].append(float(v)) rows = [d for d in dated if d["set"] == name] # per sample max |res_BY|, and where tops = [] for s in range(len(cols["Kp"]) // LEN): a = s * LEN seg = cols["res_BY"][a:a+LEN] i = max(range(LEN), key=lambda k: abs(seg[k])) tops.append((abs(seg[i]), s, i, seg[i], cols["Latitude"][a+i], cols["Longitude"][a+i], mlat(cols["Latitude"][a+i], cols["Longitude"][a+i]), cols["Kp"][a+i], cols["Dst"][a+i], rows[s]["t_first_row_utc"], rows[s]["direction"])) tops.sort(reverse=True) print(name, "largest |res_BY| by sample:") for t in tops[:6]: print(" |res_BY|=%.1f sample %d row %d value %.1f lat %.1f lon %.1f mlat %.1f Kp %.3f Dst %.1f start %s %s" % t) n = sum(1 for t in tops if t[0] > 50) print(" samples with max |res_BY| > 50 nT:", n, "of", len(tops), "| > 20 nT:", sum(1 for t in tops if t[0] > 20)) mabs = [abs(mlat(a, b)) for a, b in zip(cols["Latitude"], cols["Longitude"])] print(" share of rows with |mlat| > 50:", round(sum(1 for m in mabs if m > 50) / len(mabs), 4), "max |mlat|", round(max(mabs), 1)) if name == "test": hi = [i for i, m in enumerate(mabs) if m > 50] lo = [i for i, m in enumerate(mabs) if m <= 50] print(" labelled share |mlat|>50:", round(sum(y[i] for i in hi) / len(hi), 4), "share of labels there:", round(sum(y[i] for i in hi) / sum(y), 4), "| <=50:", round(sum(y[i] for i in lo) / len(lo), 4)) print(" no labels:" if name == "test" else "", [s for s in range(len(cols["Kp"]) // LEN) if name == "test" and sum(y[s*LEN:(s+1)*LEN]) == 0])