"""Analyze a preregistered vertical/horizontal optical-relay field test (Python 3). No observations are bundled. Use --init NEW_DIRECTORY for empty CSV templates. Every number in the CSV slope columns and on the command line is in Eotvos (1 E = 1e-9 s^-2). Each row represents one independent interleaved block, after fitting slopes of signed velocity against surveyed signed area. Use the same area normalization for the relay and reference channels. Retain raw data. Calibration uses a separately calibrated velocity readout at the crossings. Test runs use a held-out local gradiometer for the same two projected gradients. Calibration and test blocks must be disjoint and statistically independent. Rows within either phase must be independent; combine correlated repetitions before using this program. Within-block V/H and reference covariances are retained by taking differences before computing standard errors. The program assumes a stable additive tilt correction in slope units and calibrated gain. External bounds must cover residual bias, transfer errors, unmodeled motion, surveyed-area scale, reference uncertainty of systematic origin, and drift. No bound can be inferred by setting it to zero without calibration. """ import argparse import csv import math from pathlib import Path from statistics import mean, stdev CAL = ["block", "relay_V_E", "relay_H_E", "kinematic_V_E", "kinematic_H_E"] RUN = ["block", "relay_V_E", "relay_H_E", "gravity_V_E", "gravity_H_E"] def read_rows(path, columns): rows = {} with path.open(newline="") as stream: reader = csv.DictReader(stream) if reader.fieldnames != columns: raise ValueError(f"{path}: expected {columns}") for row in reader: key = row["block"].strip() values = [float(row[name]) for name in columns[1:]] if not key or key in rows or not all(map(math.isfinite, values)): raise ValueError(f"Invalid block in {path}: {key!r}") rows[key] = values if len(rows) < 2: raise ValueError(f"{path} needs at least two independent blocks to compute an SE") return rows def se(x): return stdev(x)/math.sqrt(len(x)) def estimate(cal, runs): # Supplied kinematic and gradiometer observations serve different roles. bias = [(v-h)-(a-b) for v,h,a,b in cal] contrast = [v-h for v,h,a,b in runs] reference = [a-b for v,h,a,b in runs] mismatch = [x-y for x,y in zip(contrast, reference)] corrected = mean(contrast)-mean(bias) return (mean(bias), corrected, mean(reference), mean(mismatch)-mean(bias), math.hypot(se(contrast),se(bias)), math.hypot(se(mismatch),se(bias))) def self_test(): # Algebraic fixtures only. An orientation-dependent offset creates a false # positive in an uncorrected relay and disappears after independent calibration. cal = [[30,4,20,2],[32,6,22,4]] test = [[13,3,3,1],[15,5,5,3]] assert estimate(cal,test)==(8,2,2,0,0,0) spurious = [[11,3,1,1],[12,4,2,2]] assert estimate(cal,spurious)[1:4]==(0,0,0) # Fluctuations shared by both test instruments cancel only in agreement error. varied = [[13,3,3,1],[17,5,7,3]] result = estimate(cal,varied) assert result[4] > 0 and result[5] == 0 print("Three algebraic fixtures passed. No physical data were used.") def main(): parser = argparse.ArgumentParser(description=__doc__,formatter_class=argparse.RawDescriptionHelpFormatter) group = parser.add_mutually_exclusive_group(required=True) group.add_argument("--init",type=Path,metavar="NEW_DIRECTORY") group.add_argument("--analyze",type=Path,metavar="DIRECTORY") group.add_argument("--self-test",action="store_true") parser.add_argument("--detection-bias-E",type=float,help="Residual systematic bound on corrected V-H") parser.add_argument("--agreement-bias-E",type=float,help="Total systematic bound on relay-minus-gradiometer V-H") parser.add_argument("--tolerance-E",type=float,help="Preregistered acceptable V-H disagreement") args=parser.parse_args() if args.self_test: return self_test() if args.init is not None: args.init.mkdir(parents=True,exist_ok=False) for name,cols in [("calibration.csv",CAL),("test.csv",RUN)]: with (args.init/name).open("x",newline="") as f: csv.writer(f).writerow(cols) return limits=[args.detection_bias_E,args.agreement_bias_E,args.tolerance_E] if any(x is None or not math.isfinite(x) or x<0 for x in limits) or limits[2]==0: parser.error("Supply both independently supported bias bounds and a positive agreement tolerance in E") cal=read_rows(args.analyze/"calibration.csv",CAL) test=read_rows(args.analyze/"test.csv",RUN) if cal.keys() & test.keys(): parser.error("Calibration and held-out test blocks must have distinct identifiers") b,d,ref,delta,ud,ue=estimate(cal.values(),test.values()) detect = abs(d)>limits[0]+5*ud and d*ref>0 agree = abs(delta)+limits[1]+2*ue<=limits[2] print(f"Independent blocks: {len(cal)} calibration, {len(test)} test") print(f"Tilt correction (V-H): {b:.6g} E") print(f"Corrected relay contrast: {d:.6g} E, SE {ud:.6g} E") print(f"Gradiometer contrast: {ref:.6g} E") print(f"Corrected discrepancy: {delta:.6g} E, SE {ue:.6g} E") print(f"Signed detection criterion: {detect}; agreement within tolerance: {agree}") print("Five and two SE are engineering criteria; coverage depends on the error model.") if __name__=="__main__": main()