forked from ChelseaKR/permit-bearings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconformance_evaluation_cli.py
More file actions
178 lines (157 loc) · 6.21 KB
/
Copy pathconformance_evaluation_cli.py
File metadata and controls
178 lines (157 loc) · 6.21 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""Validate, predict, or score a bounded held-out scanner evaluation.
The committed plan is ``not_run``. ``predict`` intentionally has no answer-key
argument; ``score`` requires a separately frozen prediction receipt.
"""
from __future__ import annotations
import argparse
import json
import sys
from collections.abc import Sequence
from pathlib import Path
from .conformance_evaluation import (
generate_blind_predictions,
load_answer_key,
load_case_set,
load_evaluation_manifest,
load_predictions,
load_result,
score_predictions,
write_json_exclusive,
)
ROOT = Path(__file__).resolve().parents[2]
DEFAULT_MANIFEST = Path("data/conformance/evaluations/heldout-v1/manifest.json")
def _parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="permit_pathways.conformance_evaluation_cli",
description=(
"Validate the unrun plan or create bounded raw scanner-evaluation "
"receipts. This does not determine compliance or legal accuracy."
),
)
parser.add_argument("--root", type=Path, default=ROOT)
commands = parser.add_subparsers(dest="command", required=True)
validate = commands.add_parser("validate-plan")
validate.add_argument("--manifest", type=Path, default=DEFAULT_MANIFEST)
predict = commands.add_parser(
"predict",
help="freeze binary scanner observations without loading an answer key",
)
predict.add_argument("--manifest", type=Path, default=DEFAULT_MANIFEST)
predict.add_argument("--cases", type=Path, required=True)
predict.add_argument("--output", type=Path, required=True)
predict.add_argument("--generated-at", required=True)
predict.add_argument("--repository-commit-sha", required=True)
score = commands.add_parser(
"score",
help="score a frozen prediction receipt against a reviewed answer key",
)
score.add_argument("--manifest", type=Path, default=DEFAULT_MANIFEST)
score.add_argument("--cases", type=Path, required=True)
score.add_argument("--answer-key", type=Path, required=True)
score.add_argument("--predictions", type=Path, required=True)
score.add_argument("--output", type=Path, required=True)
score.add_argument("--scored-at", required=True)
score.add_argument("--repository-commit-sha", required=True)
validate_result = commands.add_parser(
"validate-result",
help="reload and recompute a completed raw-count result receipt",
)
validate_result.add_argument("--manifest", type=Path, default=DEFAULT_MANIFEST)
validate_result.add_argument("--cases", type=Path, required=True)
validate_result.add_argument("--answer-key", type=Path, required=True)
validate_result.add_argument("--predictions", type=Path, required=True)
validate_result.add_argument("--result", type=Path, required=True)
return parser
def _under_root(root: Path, path: Path) -> Path:
return path if path.is_absolute() else root / path
def _emit(payload: dict[str, object]) -> None:
sys.stdout.write(json.dumps(payload, indent=2, sort_keys=True) + "\n")
def main(argv: Sequence[str] | None = None) -> int:
args = _parser().parse_args(argv)
root = args.root.resolve()
try:
manifest_path = _under_root(root, args.manifest)
manifest = load_evaluation_manifest(manifest_path, root)
if args.command == "validate-plan":
_emit(
{
"active_check_count": len(manifest.check_ids),
"evaluation_id": manifest.evaluation_id,
"execution_status": "not_run",
"result_present": False,
"supports_accuracy_claim": False,
}
)
return 0
cases_path = _under_root(root, args.cases)
cases = load_case_set(cases_path, manifest)
if args.command == "predict":
output = _under_root(root, args.output)
predictions = generate_blind_predictions(
manifest,
cases,
root,
generated_at=args.generated_at,
repository_commit_sha=args.repository_commit_sha,
)
receipt_sha = write_json_exclusive(output, predictions.payload)
_emit(
{
"evaluation_id": manifest.evaluation_id,
"machine_abstain": 0,
"output": str(output),
"predictions_sha256": receipt_sha,
"reference_labels_loaded": False,
}
)
return 0
answer_key = load_answer_key(
_under_root(root, args.answer_key), manifest, cases
)
predictions = load_predictions(
_under_root(root, args.predictions), manifest, cases, root
)
if args.command == "validate-result":
receipt = load_result(
_under_root(root, args.result),
manifest,
cases,
answer_key,
predictions,
)
_emit(
{
"evaluation_id": manifest.evaluation_id,
"result_sha256": receipt.raw_sha256,
"status": receipt.payload["status"],
"supports_accuracy_claim": False,
}
)
return 0
result = score_predictions(
manifest,
cases,
answer_key,
predictions,
scored_at=args.scored_at,
repository_commit_sha=args.repository_commit_sha,
)
output = _under_root(root, args.output)
receipt_sha = write_json_exclusive(output, result)
_emit(
{
"evaluation_id": manifest.evaluation_id,
"output": str(output),
"result_sha256": receipt_sha,
"status": result["status"],
"supports_accuracy_claim": False,
}
)
return 0
except (OSError, ValueError) as error:
print(
f"conformance evaluation: invalid input or output: {error}", file=sys.stderr
)
return 2
if __name__ == "__main__":
raise SystemExit(main())