Datasets:
Create baseline/generate_baseline_predictions.py
Browse files
baseline/generate_baseline_predictions.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import csv
|
| 2 |
+
import os
|
| 3 |
+
from typing import Dict, List
|
| 4 |
+
|
| 5 |
+
K = 1.0
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def compute_surfaces(pressure: float, buffer: float, lag: float, coupling: float) -> Dict[str, float]:
|
| 9 |
+
s1 = buffer - (pressure * coupling) - (K * lag)
|
| 10 |
+
s2 = buffer - (pressure * (coupling ** 2)) - (K * lag)
|
| 11 |
+
s3 = buffer - (pressure * coupling) - (K * (lag ** 2))
|
| 12 |
+
return {
|
| 13 |
+
"baseline_surface": s1,
|
| 14 |
+
"coupling_surface": s2,
|
| 15 |
+
"lag_surface": s3,
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def compute_manifold_margin(pressure: float, buffer: float, lag: float, coupling: float) -> float:
|
| 20 |
+
return min(compute_surfaces(pressure, buffer, lag, coupling).values())
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def load_rows(path: str) -> List[Dict[str, str]]:
|
| 24 |
+
with open(path, newline="", encoding="utf-8") as f:
|
| 25 |
+
return list(csv.DictReader(f))
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def write_predictions(rows: List[Dict[str, str]], output_path: str) -> None:
|
| 29 |
+
with open(output_path, "w", newline="", encoding="utf-8") as f:
|
| 30 |
+
writer = csv.writer(f)
|
| 31 |
+
writer.writerow(["scenario_id", "prediction"])
|
| 32 |
+
|
| 33 |
+
for row in rows:
|
| 34 |
+
pressure = float(row["pressure"])
|
| 35 |
+
buffer = float(row["buffer"])
|
| 36 |
+
lag = float(row["lag"])
|
| 37 |
+
coupling = float(row["coupling"])
|
| 38 |
+
|
| 39 |
+
margin = compute_manifold_margin(pressure, buffer, lag, coupling)
|
| 40 |
+
prediction = 1 if margin >= 0 else 0
|
| 41 |
+
|
| 42 |
+
writer.writerow([row["scenario_id"], prediction])
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def run_split(input_file: str, output_file: str) -> None:
|
| 46 |
+
rows = load_rows(input_file)
|
| 47 |
+
write_predictions(rows, output_file)
|
| 48 |
+
print(f"Predictions written: {output_file}")
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def main() -> None:
|
| 52 |
+
os.makedirs("baseline_predictions", exist_ok=True)
|
| 53 |
+
|
| 54 |
+
splits = {
|
| 55 |
+
"in_domain_test": "data/in_domain_test.csv",
|
| 56 |
+
"distribution_shift": "data/distribution_shift.csv",
|
| 57 |
+
"boundary_trap": "data/boundary_trap.csv",
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
for split_name, input_path in splits.items():
|
| 61 |
+
output_path = f"baseline_predictions/{split_name}_baseline_predictions.csv"
|
| 62 |
+
run_split(input_path, output_path)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
main()
|