-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_sir_csv.py
More file actions
160 lines (139 loc) · 5.8 KB
/
Copy pathbuild_sir_csv.py
File metadata and controls
160 lines (139 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python3
"""Build relational CSVs from SIR extracted JSON files.
Produces:
<output-dir>/sir_records.csv — one row per SirRecord
<output-dir>/violations.csv — one row per possible_violation
"""
import argparse
import csv
import json
from pathlib import Path
SIR_RECORDS_FIELDS = [
"record_uid",
"batch",
"source_file",
"record_index",
"model",
"generated_at_utc",
"sir_id",
"report_date",
"incident_date",
"location_details",
"where_clear",
"location_text_raw",
"country_or_area",
"location_type",
"precision_level",
"geocodable",
"geocodable_query",
"lat",
"lon",
"uncertainty_note",
"dead_confirmed",
"injured_confirmed",
"missing_confirmed",
"dead_possible_min",
"dead_possible_max",
"possible_violations_count",
"context_note",
"libyan_coast_guard_involved",
"evidence_quote",
"confidence",
"evidence_pages",
]
VIOLATIONS_FIELDS = [
"record_uid",
"sir_id",
"source_file",
"violation_index",
"violation_name",
"legal_basis",
"assessment",
]
def build_csvs(input_dir: Path, output_dir: Path) -> None:
output_dir.mkdir(parents=True, exist_ok=True)
json_files = sorted(input_dir.glob("**/*.extracted.json"))
if not json_files:
print(f"No .extracted.json files found in {input_dir}")
return
record_uid = 0
records_written = 0
violations_written = 0
with open(output_dir / "sir_records.csv", "w", newline="", encoding="utf-8") as rf, \
open(output_dir / "violations.csv", "w", newline="", encoding="utf-8") as vf:
rw = csv.DictWriter(rf, fieldnames=SIR_RECORDS_FIELDS)
vw = csv.DictWriter(vf, fieldnames=VIOLATIONS_FIELDS)
rw.writeheader()
vw.writeheader()
for json_path in json_files:
batch = json_path.parent.name
try:
with open(json_path, encoding="utf-8") as fh:
data = json.load(fh)
except json.JSONDecodeError as exc:
print(f"WARN: skipping {json_path} ({exc})")
continue
source_file = data.get("source_file", "")
model = data.get("model", "")
generated_at_utc = data.get("generated_at_utc", "")
for rec_idx, rec in enumerate(data.get("records", [])):
record_uid += 1
violations = rec.get("possible_violations") or []
evidence_pages = rec.get("evidence_pages") or []
rw.writerow({
"record_uid": record_uid,
"batch": batch,
"source_file": source_file,
"record_index": rec_idx,
"model": model,
"generated_at_utc": generated_at_utc,
"sir_id": rec.get("sir_id", ""),
"report_date": rec.get("report_date", ""),
"incident_date": rec.get("incident_date", ""),
"location_details": rec.get("location_details", ""),
"where_clear": rec.get("where_clear", ""),
"location_text_raw": rec.get("location_text_raw", ""),
"country_or_area": rec.get("country_or_area", ""),
"location_type": rec.get("location_type", ""),
"precision_level": rec.get("precision_level", ""),
"geocodable": rec.get("geocodable", ""),
"geocodable_query": rec.get("geocodable_query", ""),
"lat": rec.get("lat", ""),
"lon": rec.get("lon", ""),
"uncertainty_note": rec.get("uncertainty_note", ""),
"dead_confirmed": rec.get("dead_confirmed", ""),
"injured_confirmed": rec.get("injured_confirmed", ""),
"missing_confirmed": rec.get("missing_confirmed", ""),
"dead_possible_min": rec.get("dead_possible_min", ""),
"dead_possible_max": rec.get("dead_possible_max", ""),
"possible_violations_count": len(violations),
"context_note": rec.get("context_note", ""),
"libyan_coast_guard_involved": rec.get("libyan_coast_guard_involved", ""),
"evidence_quote": rec.get("evidence_quote", ""),
"confidence": rec.get("confidence", ""),
"evidence_pages": ",".join(str(p) for p in evidence_pages),
})
records_written += 1
for v_idx, v in enumerate(violations):
vw.writerow({
"record_uid": record_uid,
"sir_id": rec.get("sir_id", ""),
"source_file": source_file,
"violation_index": v_idx,
"violation_name": v.get("violation_name", ""),
"legal_basis": v.get("legal_basis", ""),
"assessment": v.get("assessment", ""),
})
violations_written += 1
print(f"Written {records_written} records → {output_dir / 'sir_records.csv'}")
print(f"Written {violations_written} violations → {output_dir / 'violations.csv'}")
def main() -> None:
parser = argparse.ArgumentParser(description="Build relational CSVs from SIR extracted JSON files.")
parser.add_argument("--input-dir", default="analysis_output", type=Path,
help="Directory containing .extracted.json files (default: analysis_output)")
parser.add_argument("--output-dir", default="output_csv", type=Path,
help="Directory for output CSVs (default: output_csv)")
args = parser.parse_args()
build_csvs(args.input_dir, args.output_dir)
if __name__ == "__main__":
main()