File size: 10,521 Bytes
5c1bb37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b8c78e
5c1bb37
 
4b8c78e
 
 
5c1bb37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b8c78e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c1bb37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b8c78e
 
 
 
 
 
 
 
 
 
 
5c1bb37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from __future__ import annotations

import binascii
import csv
import json
import math
import shutil
import struct
import zlib
from pathlib import Path
from typing import Any, Iterable


REPO_ROOT = Path(__file__).resolve().parents[1]


def ensure_dir(path: str | Path) -> Path:
    out = Path(path)
    out.mkdir(parents=True, exist_ok=True)
    return out


def read_json(path: str | Path) -> Any:
    with Path(path).open("r", encoding="utf-8") as f:
        return json.load(f)


def write_json(path: str | Path, obj: Any) -> None:
    path = Path(path)
    ensure_dir(path.parent)
    with path.open("w", encoding="utf-8") as f:
        json.dump(obj, f, indent=2, sort_keys=True)
        f.write("\n")


def read_jsonl(path: str | Path) -> list[dict[str, Any]]:
    rows: list[dict[str, Any]] = []
    with Path(path).open("r", encoding="utf-8") as f:
        for line_no, line in enumerate(f, start=1):
            line = line.strip()
            if not line:
                continue
            try:
                row = json.loads(line)
            except json.JSONDecodeError as exc:
                raise ValueError(f"Invalid JSONL at {path}:{line_no}: {exc}") from exc
            if not isinstance(row, dict):
                raise ValueError(f"Expected a JSON object at {path}:{line_no}")
            rows.append(row)
    return rows


def write_jsonl(path: str | Path, rows: Iterable[dict[str, Any]]) -> None:
    path = Path(path)
    ensure_dir(path.parent)
    with path.open("w", encoding="utf-8") as f:
        for row in rows:
            f.write(json.dumps(row, sort_keys=True))
            f.write("\n")


def write_csv(path: str | Path, rows: list[dict[str, Any]], fieldnames: list[str] | None = None) -> None:
    path = Path(path)
    ensure_dir(path.parent)
    if fieldnames is None:
        keys: list[str] = []
        for row in rows:
            for key in row:
                if key not in keys:
                    keys.append(key)
        fieldnames = keys
    with path.open("w", encoding="utf-8", newline="") as f:
        writer = csv.DictWriter(f, fieldnames=fieldnames)
        writer.writeheader()
        for row in rows:
            writer.writerow(row)


def _table_fieldnames(rows: list[dict[str, Any]], fieldnames: list[str] | None = None) -> list[str]:
    if fieldnames is not None:
        return fieldnames
    keys: list[str] = []
    for row in rows:
        for key in row:
            if key not in keys:
                keys.append(key)
    return keys


def write_markdown_table(path: str | Path, rows: list[dict[str, Any]], fieldnames: list[str] | None = None) -> None:
    path = Path(path)
    ensure_dir(path.parent)
    fieldnames = _table_fieldnames(rows, fieldnames)
    with path.open("w", encoding="utf-8") as f:
        f.write("| " + " | ".join(fieldnames) + " |\n")
        f.write("| " + " | ".join(["---"] * len(fieldnames)) + " |\n")
        for row in rows:
            f.write("| " + " | ".join(str(row.get(name, "")) for name in fieldnames) + " |\n")


def _latex_escape(value: Any) -> str:
    text = str(value)
    return (
        text.replace("\\", "\\textbackslash{}")
        .replace("&", "\\&")
        .replace("%", "\\%")
        .replace("$", "\\$")
        .replace("#", "\\#")
        .replace("_", "\\_")
        .replace("{", "\\{")
        .replace("}", "\\}")
    )


def write_latex_table(
    path: str | Path,
    rows: list[dict[str, Any]],
    fieldnames: list[str] | None = None,
    caption: str = "Table-ready experiment results.",
    label: str = "tab:cmevs_results",
) -> None:
    path = Path(path)
    ensure_dir(path.parent)
    fieldnames = _table_fieldnames(rows, fieldnames)
    align = "l" * len(fieldnames)
    with path.open("w", encoding="utf-8") as f:
        f.write("\\begin{table}[t]\n")
        f.write("\\centering\n")
        f.write(f"\\caption{{{_latex_escape(caption)}}}\n")
        safe_label = str(label).replace("{", "").replace("}", "")
        f.write(f"\\label{{{safe_label}}}\n")
        f.write(f"\\begin{{tabular}}{{{align}}}\n")
        f.write("\\toprule\n")
        f.write(" & ".join(_latex_escape(name) for name in fieldnames) + " \\\\\n")
        f.write("\\midrule\n")
        for row in rows:
            f.write(" & ".join(_latex_escape(row.get(name, "")) for name in fieldnames) + " \\\\\n")
        f.write("\\bottomrule\n")
        f.write("\\end{tabular}\n")
        f.write("\\end{table}\n")


def copy_file(src: str | Path, dst: str | Path) -> None:
    dst = Path(dst)
    ensure_dir(dst.parent)
    shutil.copy2(src, dst)


def candidate_by_id(candidates: Iterable[dict[str, Any]]) -> dict[str, dict[str, Any]]:
    result = {}
    for row in candidates:
        cid = row.get("candidate_id")
        if not isinstance(cid, str) or not cid.strip():
            raise ValueError("candidate_id must be a nonempty string")
        if cid in result:
            raise ValueError(f"Duplicate candidate_id: {cid}")
        result[cid] = row
    return result


def validate_candidates(candidates: list[dict[str, Any]]) -> None:
    candidate_by_id(candidates)
    if not candidates:
        raise ValueError("Candidate input is empty")
    scenes = set()
    for row in candidates:
        if not isinstance(row.get("valid"), bool):
            raise ValueError("Candidate valid must be a JSON boolean")
        for key in ("source", "scene_id"):
            if not isinstance(row.get(key), str) or not row[key]:
                raise ValueError(f"Candidate {key} must be a nonempty string")
        scenes.add((row["source"], row["scene_id"]))
        position = row.get("position")
        if not isinstance(position, list) or len(position) != 3 or not all(
            isinstance(x, (int, float)) and not isinstance(x, bool) and math.isfinite(x)
            for x in position
        ):
            raise ValueError("Candidate position must contain three finite numbers")
        for key in ("single_view_probe_coverage", "conflict_prior"):
            value = row.get(key)
            if isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value) or not 0 <= value <= 1:
                raise ValueError(f"Candidate {key} must be finite and in [0, 1]")
        for key in ("yaw_deg", "oracle_gain"):
            if key in row and (isinstance(row[key], bool) or not isinstance(row[key], (int, float)) or not math.isfinite(row[key])):
                raise ValueError(f"Candidate {key} must be finite")
        cells = row.get("covered_cells")
        if not isinstance(cells, list) or not all(isinstance(x, str) for x in cells):
            raise ValueError("covered_cells must be an array of strings")
    if len(scenes) != 1:
        raise ValueError("A normalized candidate file must describe one source and scene")


def validate_selection(selected_doc: dict[str, Any], by_id: dict[str, dict[str, Any]]) -> None:
    ids = selected_ids(selected_doc)
    for cid in ids:
        if cid not in by_id:
            raise ValueError(f"Unknown selected candidate: {cid}")
        if not by_id[cid].get("valid", True):
            raise ValueError(f"Selected candidate is invalid: {cid}")
        if selected_doc.get("scene_id") != by_id[cid].get("scene_id"):
            raise ValueError(f"Selected candidate belongs to another scene: {cid}")


def valid_candidates(candidates: Iterable[dict[str, Any]]) -> list[dict[str, Any]]:
    return [row for row in candidates if bool(row.get("valid", True))]


def cell_set(candidate: dict[str, Any]) -> set[str]:
    return {str(cell) for cell in candidate.get("covered_cells", [])}


def universe_cells(candidates: Iterable[dict[str, Any]]) -> set[str]:
    cells: set[str] = set()
    for candidate in candidates:
        if bool(candidate.get("valid", True)):
            cells.update(cell_set(candidate))
    return cells


def selected_ids(selected_doc: dict[str, Any]) -> list[str]:
    if not isinstance(selected_doc, dict) or not isinstance(selected_doc.get("selected_viewpoints"), list):
        raise ValueError("Selection must contain a selected_viewpoints array")
    ids = []
    for row in selected_doc["selected_viewpoints"]:
        cid = row.get("candidate_id") if isinstance(row, dict) else None
        if not isinstance(cid, str) or not cid.strip():
            raise ValueError("Selected candidate_id must be a nonempty string")
        if cid in ids:
            raise ValueError(f"Duplicate selected candidate_id: {cid}")
        ids.append(cid)
    return ids


def safe_div(num: float, den: float) -> float:
    return 0.0 if den == 0 else num / den


def pearson(xs: list[float], ys: list[float]) -> float:
    if len(xs) != len(ys) or len(xs) < 2:
        return float("nan")
    mx = sum(xs) / len(xs)
    my = sum(ys) / len(ys)
    num = sum((x - mx) * (y - my) for x, y in zip(xs, ys))
    vx = sum((x - mx) ** 2 for x in xs)
    vy = sum((y - my) ** 2 for y in ys)
    if vx <= 0.0 or vy <= 0.0:
        return float("nan")
    return num / math.sqrt(vx * vy)


def _png_chunk(kind: bytes, payload: bytes) -> bytes:
    return (
        struct.pack(">I", len(payload))
        + kind
        + payload
        + struct.pack(">I", binascii.crc32(kind + payload) & 0xFFFFFFFF)
    )


def write_solid_png(path: str | Path, width: int, height: int, rgb: tuple[int, int, int]) -> None:
    path = Path(path)
    ensure_dir(path.parent)
    raw = bytearray()
    row = bytes(rgb) * width
    for _ in range(height):
        raw.append(0)
        raw.extend(row)
    ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)
    data = zlib.compress(bytes(raw), level=9)
    with path.open("wb") as f:
        f.write(b"\x89PNG\r\n\x1a\n")
        f.write(_png_chunk(b"IHDR", ihdr))
        f.write(_png_chunk(b"IDAT", data))
        f.write(_png_chunk(b"IEND", b""))


def write_npy_f4(path: str | Path, height: int, width: int, value: float) -> None:
    path = Path(path)
    ensure_dir(path.parent)
    header = "{'descr': '<f4', 'fortran_order': False, 'shape': (%d, %d), }" % (height, width)
    header_bytes = header.encode("latin1")
    prefix_len = 6 + 2 + 2
    padding = 16 - ((prefix_len + len(header_bytes) + 1) % 16)
    header_bytes += b" " * padding + b"\n"
    row = struct.pack("<" + "f" * width, *([float(value)] * width))
    with path.open("wb") as f:
        f.write(b"\x93NUMPY")
        f.write(b"\x01\x00")
        f.write(struct.pack("<H", len(header_bytes)))
        f.write(header_bytes)
        for _ in range(height):
            f.write(row)