onelook 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
onelook-0.1.2/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 onelook contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
onelook-0.1.2/PKG-INFO ADDED
@@ -0,0 +1,593 @@
1
+ Metadata-Version: 2.4
2
+ Name: onelook
3
+ Version: 0.1.2
4
+ Summary: Seal a blind test set, record the one run made against it, and check later whether a published number still corresponds to that code and that data.
5
+ Author: onelook contributors
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/RealAoss/onelook
8
+ Project-URL: Issues, https://github.com/RealAoss/onelook/issues
9
+ Project-URL: Changelog, https://github.com/RealAoss/onelook/blob/main/CHANGELOG.md
10
+ Keywords: evaluation,test set,reproducibility,metrics
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Classifier: Topic :: Scientific/Engineering
18
+ Classifier: Topic :: Software Development :: Testing
19
+ Requires-Python: >=3.10
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ Dynamic: license-file
23
+
24
+ # onelook
25
+
26
+ A small tool that keeps accuracy numbers honest. It seals a blind test set,
27
+ records the one run made against it together with a fingerprint of the code
28
+ that was measured, and can afterwards tell anyone whether a published number
29
+ still corresponds to that code and that data.
30
+
31
+ Python 3.10 or later, standard library only.
32
+
33
+ ```
34
+ pip install git+https://github.com/RealAoss/onelook
35
+ ```
36
+
37
+ ## The problem
38
+
39
+ You build a matcher, a classifier or a heuristic. You have a test set. You
40
+ run it, look at the misses, fix a few, run it again. After some rounds of
41
+ this the number is good and it goes into the README as "accuracy on the test
42
+ set". By then the test set has been used for tuning, and the number says
43
+ more about how well the code was fitted to those examples than about how it
44
+ will do on new ones.
45
+
46
+ The remedy is a matter of discipline, not of software: tune on one set,
47
+ measure once on another that you have not looked at, and then leave the code
48
+ alone. onelook does not make anyone follow that. It makes the protocol cheap
49
+ to follow, and it makes departures from it visible:
50
+
51
+ - the blind set is sealed by hash, so editing it afterwards shows;
52
+ - every run against it is recorded, the first as `blind` and every later one
53
+ as `repeat`, and none is ever removed;
54
+ - each run stores a fingerprint of the code that was measured, so "the code
55
+ has changed since the number was taken" is a fact anyone can check;
56
+ - the report for the README is generated from those records and says what
57
+ they say, including the unflattering cases.
58
+
59
+ ## Two-minute walkthrough
60
+
61
+ <!-- walkthrough:begin -->
62
+
63
+ A matcher that decides whether two company names mean the same company, and
64
+ a blind set of eight labelled pairs written by someone who has not seen the
65
+ matcher.
66
+
67
+ <!-- file: matcher.py -->
68
+ ```python
69
+ def score(a, b):
70
+ """Share of words the two names have in common."""
71
+ x, y = set(a.lower().split()), set(b.lower().split())
72
+ return len(x & y) / len(x | y)
73
+ ```
74
+
75
+ <!-- file: blind.csv -->
76
+ ```
77
+ id,a,b,gold
78
+ 1,Alder Corp,ALDER Corp,1
79
+ 2,Alder Corp,Birch Corp,0
80
+ 3,Cedar Inc,Cedar Incorporated,1
81
+ 4,Dogwood,Dogwood LLC,1
82
+ 5,Elm Street Ltd,Elm Street Ltd,1
83
+ 6,Fir Industries,Gum Enterprises,0
84
+ 7,Hazel,Hazel Search,0
85
+ 8,Ivy Co,Ivy Company,1
86
+ ```
87
+
88
+ The evaluation script is yours. onelook only runs it. This one uses the
89
+ scoring helpers that come with onelook, prints every miss, and writes the
90
+ numbers to a JSON file.
91
+
92
+ <!-- file: evaluate.py -->
93
+ ```python
94
+ import csv
95
+ import json
96
+
97
+ from onelook import metrics
98
+ from matcher import score
99
+
100
+ THRESHOLD = 0.5 # chosen on the dev set, before the blind set was written
101
+
102
+ with open("blind.csv", newline="", encoding="utf-8") as f:
103
+ items = [
104
+ metrics.Item(gold=row["gold"] == "1", score=score(row["a"], row["b"]), id=row["id"])
105
+ for row in csv.DictReader(f)
106
+ ]
107
+
108
+ result = metrics.score_at(items, THRESHOLD)
109
+ misses = metrics.errors(items, THRESHOLD)
110
+ for item in misses.false_positives:
111
+ print("false positive: pair", item.id)
112
+ for item in misses.false_negatives:
113
+ print("false negative: pair", item.id)
114
+
115
+ with open("metrics.json", "w", encoding="utf-8") as f:
116
+ json.dump(
117
+ {"n": result.n, "precision": result.precision, "recall": result.recall, "f1": result.f1},
118
+ f,
119
+ )
120
+ ```
121
+
122
+ Seal the blind set and say which files are the engine:
123
+
124
+ ```console
125
+ $ onelook seal blind blind.csv --note "written by a colleague who has not seen matcher.py"
126
+ sealed 'blind' version 1: 1 file, 8 items
127
+ set hash 9294ed6a5fe6bf594c51ee762f1f2ad0fa62f5e557c018ecc61d4cb06c6fd31c (newlines: lf)
128
+ 4c5f2c90f1f7901802d82475982f0a62e964c015e24d976fa005ab7d1392bf5f blind.csv
129
+ $ onelook fingerprint matcher.py --save
130
+ 22326629048b37c8eb2e05ce993df32b4bb2bded3d38eeaba758084456cb461d
131
+ 1 file; include: matcher.py; ignore: .git, __pycache__, *.pyc; newlines: lf
132
+ saved as the engine in onelook.json
133
+ ```
134
+
135
+ Both commands wrote to `onelook.json`, the ledger. Commit it. Now make the
136
+ one run:
137
+
138
+ ```console
139
+ $ onelook run blind --metrics metrics.json -- python evaluate.py
140
+ false positive: pair 7
141
+ false negative: pair 3
142
+ false negative: pair 8
143
+ onelook: recorded run 1 of set 'blind' (version 1) as the blind run
144
+ onelook: engine 22326629048b37c8eb2e05ce993df32b4bb2bded3d38eeaba758084456cb461d, exit code 0
145
+ $ onelook status
146
+ ok blind (v1): sealed files intact; 1 run (blind); engine matches the blind run
147
+ ```
148
+
149
+ The paragraph and table for the README:
150
+
151
+ ```console
152
+ $ onelook report
153
+ <!-- onelook:begin blind -->
154
+ Sealed set `blind`: 1 file, 8 items, sealed 2026-11-24 (note: written by a colleague who has not seen matcher.py). The sealed files are unchanged since sealing (set hash `9294ed6a5fe6`). It has been run once, on 2026-11-24; that run is the blind run. Blind-run numbers: n 8, precision 0.75, recall 0.6, f1 0.6667. The engine fingerprint at the blind run was `22326629048b`; the engine files in this checkout match it. onelook records which files and which code each run measured; it cannot show that the set was unseen while the code was written.
155
+
156
+ | run | label | date | exit | engine | n | precision | recall | f1 |
157
+ | --- | --- | --- | --- | --- | --- | --- | --- | --- |
158
+ | 1 | blind | 2026-11-24 | 0 | `22326629048b` | 8 | 0.75 | 0.6 | 0.6667 |
159
+
160
+ <!-- onelook:end blind -->
161
+ ```
162
+
163
+ Now the thing the protocol forbids. Having seen the misses, change the
164
+ matcher so that `Inc` matches `Incorporated`:
165
+
166
+ <!-- file: matcher.py -->
167
+ ```python
168
+ def score(a, b):
169
+ """Share of words that match, where a word also matches its abbreviation."""
170
+ x, y = a.lower().split(), b.lower().split()
171
+ same = sum(1 for w in x if any(v.startswith(w) or w.startswith(v) for v in y))
172
+ return same / max(len(x), len(y))
173
+ ```
174
+
175
+ <!-- exit: 1 -->
176
+ ```console
177
+ $ onelook status
178
+ FAIL blind (v1): sealed files intact; 1 run (blind); engine CHANGED since the blind run
179
+ 1 of 1 set no longer holds
180
+ ```
181
+
182
+ `status` exits with 1, which is what a CI job needs. Running the evaluation
183
+ again is allowed, but it is recorded for what it is:
184
+
185
+ ```console
186
+ $ onelook run blind --metrics metrics.json -- python evaluate.py
187
+ false positive: pair 2
188
+ false positive: pair 7
189
+ onelook: recorded run 2 of set 'blind' (version 1) as a repeat; it is not blind, and the blind run stays on record
190
+ onelook: the engine has changed since the blind run
191
+ onelook: engine 6ef60a912005a344cd31725b7327b6d2bc90315961c3397ce71bb812ab5acacd, exit code 0
192
+ $ onelook report
193
+ <!-- onelook:begin blind -->
194
+ Sealed set `blind`: 1 file, 8 items, sealed 2026-11-24 (note: written by a colleague who has not seen matcher.py). The sealed files are unchanged since sealing (set hash `9294ed6a5fe6`). It has been run twice; only run 1, on 2026-11-24, is blind. Blind-run numbers: n 8, precision 0.75, recall 0.6, f1 0.6667. The latest run is run 2 of the same set, a repeat, and is not blind: n 8, precision 0.7143, recall 1, f1 0.8333; the first-run numbers were n 8, precision 0.75, recall 0.6, f1 0.6667. The engine fingerprint at the blind run was `22326629048b`; the engine files in this checkout have a different fingerprint (`6ef60a912005`), so the blind-run numbers describe earlier code, not this checkout. The engine was changed after the blind run: run 2 measured different code. onelook records which files and which code each run measured; it cannot show that the set was unseen while the code was written.
195
+
196
+ | run | label | date | exit | engine | n | precision | recall | f1 |
197
+ | --- | --- | --- | --- | --- | --- | --- | --- | --- |
198
+ | 1 | blind | 2026-11-24 | 0 | `22326629048b` | 8 | 0.75 | 0.6 | 0.6667 |
199
+ | 2 | repeat | 2026-11-24 | 0 | `6ef60a912005` | 8 | 0.7143 | 1 | 0.8333 |
200
+
201
+ <!-- onelook:end blind -->
202
+ ```
203
+
204
+ The F1 went up. The report still leads with the first number, and says why
205
+ the second one is not comparable. Put the report into the README and anyone
206
+ who clones the repository can check it:
207
+
208
+ ```console
209
+ $ onelook report --write README.md
210
+ README.md: appended a block for 'blind'
211
+ $ onelook verify
212
+ ok ledger is internally consistent (1 sealed set, 2 run records)
213
+ ok set 'blind' (version 1): sealed files match their recorded hashes
214
+ note set 'blind' (version 1): engine differs from the blind run's fingerprint
215
+ note set 'blind' (version 1): 1 repeat run on record
216
+ ok README.md: the report for 'blind' matches the ledger and the files here
217
+ verified: the README's report is accurate for this checkout
218
+ ```
219
+
220
+ `verify` succeeded because the README now says what is true; the two `note`
221
+ lines show what that is. Had the README still carried the first report,
222
+ `verify` would have exited with 1 and printed the lines that differ.
223
+
224
+ "The report leads with the first number" is as far as the tool goes. The
225
+ second number is a repeat on tuned code, and the honest way to get a new
226
+ blind number for the new matcher is a new blind set.
227
+
228
+ <!-- walkthrough:end -->
229
+
230
+ ## Commands
231
+
232
+ All commands read `onelook.json` in the current directory; `--ledger FILE`
233
+ names another one. Paths in the ledger are relative to the ledger's
234
+ directory.
235
+
236
+ ### `onelook seal NAME FILES...`
237
+
238
+ Records, for each file: its path, SHA-256, size, and the number of items if
239
+ the format is recognised (`.csv` and `.tsv`: rows after the header;
240
+ `.jsonl` and `.ndjson`: non-blank lines; `.json`: length of a top-level
241
+ array). Also the time and an optional `--note`, for example who wrote the
242
+ set. `FILES` may be files, directories or globs.
243
+
244
+ - Sealing the same content again under the same name does nothing.
245
+ - Sealing different content under an existing name is refused.
246
+ - `--new-version` seals it as version 2 (3, ...). Earlier versions and their
247
+ runs stay in the ledger, and the report mentions them.
248
+ - `--exact` hashes the bytes as they are. The default treats CRLF as LF, so
249
+ that a checkout on Windows with line-ending conversion still verifies.
250
+
251
+ ### `onelook fingerprint [PATHS...]`
252
+
253
+ Prints the fingerprint of the engine: one hash over the paths and contents
254
+ of the files that make up the code being measured.
255
+
256
+ - With `PATHS` (files, directories or globs) it hashes those. `--save`
257
+ stores them in the ledger as the engine. `--ignore PATTERN` (repeatable)
258
+ leaves files out; `.git`, `__pycache__` and `*.pyc` are always in the
259
+ saved ignore list. `--exact` hashes bytes as they are.
260
+ - Without `PATHS` it uses the engine saved in the ledger.
261
+ - `--list` prints every file with its hash, which is how you find out what
262
+ changed.
263
+
264
+ The engine configuration is the `engine` object in `onelook.json` and can
265
+ be edited by hand. Include everything that can change the number: code,
266
+ thresholds, prompt files, model weights. Do not include files the
267
+ evaluation writes.
268
+
269
+ ### `onelook run NAME -- COMMAND...`
270
+
271
+ 1. Re-hashes the sealed files. If they differ from the seal, it stops and
272
+ records nothing.
273
+ 2. Computes the engine fingerprint.
274
+ 3. Runs `COMMAND` in the current directory, without a shell, and captures
275
+ standard output and error together while passing them through.
276
+ `--shell` joins the words with spaces and hands them to the system shell.
277
+ On Windows, a `.bat` or `.cmd` file is always run by `cmd.exe`, which
278
+ would interpret characters such as `&`, `|` and `>` in the arguments, so
279
+ onelook refuses to run one without `--shell`; pass `--shell` only if you
280
+ accept that `cmd.exe` interprets the command line, and quote the
281
+ arguments yourself.
282
+ 4. Reads `--metrics FILE` if given: a JSON object the command wrote.
283
+ 5. Appends a run record: time, run number, label, set hash, engine
284
+ fingerprint and configuration, command, exit code, duration, metrics, a
285
+ SHA-256 of the output and its last 20 lines (`--tail N` changes that;
286
+ `--tail 0` keeps none, which you want if the output quotes a set that is
287
+ not public).
288
+
289
+ Run 1 against a sealed set is labelled `blind`. Every later run against the
290
+ same set is labelled `repeat`. If a later run measured different engine
291
+ files from run 1, the record says so. When the engine configuration was
292
+ edited after run 1 (for example a different ignore list), the two
293
+ fingerprints are not comparable, so the code of the later run is
294
+ fingerprinted again with run 1's configuration and that is compared; the
295
+ record and the report state the configuration change separately. Records
296
+ are appended and never rewritten or removed.
297
+
298
+ A run is recorded whatever the command's exit status, because a command
299
+ that crashed halfway may still have shown you results. That includes a run
300
+ you interrupt (Ctrl-C) once the command has started: the command is
301
+ stopped, the run is recorded with status `interrupted` and keeps its number
302
+ and label (an interrupted run 1 is the blind run), and `onelook run` exits
303
+ with 130. If you want to check
304
+ that the script starts at all, try it on the dev set first. `onelook run`
305
+ exits with the command's status, or with 1 if the command succeeded but the
306
+ metrics file was missing or not a JSON object. If the metrics file exists
307
+ but the command did not modify it, the metrics are recorded with a warning.
308
+
309
+ ### `onelook status`
310
+
311
+ One line per sealed set: are the sealed files intact, how many runs, does
312
+ the engine still match the blind run. Exits 0 if everything holds and 1 if
313
+ a sealed set has changed or the engine differs from the one the blind run
314
+ measured. `--strict` also exits 1 when a set has repeat runs or its blind
315
+ run exited with a non-zero status.
316
+
317
+ The engine comparison uses the engine configuration stored in the blind
318
+ run's record, not the current one, so narrowing the configuration later
319
+ does not make changed code look unchanged.
320
+
321
+ ### `onelook report [NAME...]`
322
+
323
+ Prints a paragraph and a table per sealed set, in Markdown, wrapped in
324
+ `<!-- onelook:begin NAME -->` and `<!-- onelook:end NAME -->` comments.
325
+ `--format json` prints the same facts as JSON (`--format md` is the
326
+ default). `--write FILE` puts the blocks into a Markdown file, replacing
327
+ blocks that are already there.
328
+
329
+ The report has no generation date in it; it depends only on the ledger and
330
+ the files, so it can be regenerated and compared.
331
+
332
+ ### `onelook verify`
333
+
334
+ For a reader who has cloned the repository. It
335
+
336
+ 1. checks that the ledger is internally consistent (run numbers in order,
337
+ run 1 and only run 1 labelled `blind`, set hashes that follow from the
338
+ file hashes, flags that agree with the fingerprints);
339
+ 2. re-hashes every sealed file and re-fingerprints the engine;
340
+ 3. finds the onelook blocks in `README.md` (or `--readme FILE`) and compares
341
+ each with the report regenerated from the ledger and the files present.
342
+
343
+ It exits 0 when the README's report is exactly what onelook would write
344
+ now, and 1 otherwise, printing the differing lines. A README that correctly
345
+ says "the engine has changed" verifies; one that implies it has not is
346
+ refuted. If the README has no onelook block, `verify` falls back to the
347
+ test `status` makes. Naming a file with `--readme` that has no block is a
348
+ failure.
349
+
350
+ ### Exit status
351
+
352
+ | status | meaning |
353
+ | --- | --- |
354
+ | 0 | done, and everything that was checked holds |
355
+ | 1 | a check does not hold (`status`, `verify`), or metrics could not be read (`run`) |
356
+ | 2 | usage error, or onelook refused: no ledger, unknown set, changed sealed set, re-seal without `--new-version`, a Windows `.bat`/`.cmd` command without `--shell` |
357
+ | 130 | `onelook run` was interrupted; the run was recorded as interrupted |
358
+ | other | from `onelook run`: the exit status of your command |
359
+
360
+ ## Exactly what is hashed
361
+
362
+ **File hash.** SHA-256 of the file's bytes. In the default newline mode
363
+ `lf`, every CRLF pair (bytes `0D 0A`) is replaced by one LF (`0A`) before
364
+ hashing; a CR not followed by LF is left alone. In mode `exact` nothing is
365
+ replaced. The recorded size is the number of bytes hashed. The `lf` mode is
366
+ applied to every file, binary ones included; use `exact` if a `0D 0A`
367
+ sequence inside a binary file must count.
368
+
369
+ **Path.** Relative to the directory containing `onelook.json`, with `/` as
370
+ the separator on every operating system, in the case the file system
371
+ reports. No Unicode normalisation is applied.
372
+
373
+ **Set hash and engine fingerprint.** Both are the same construction over a
374
+ list of files: SHA-256 of
375
+
376
+ ```
377
+ onelook-fingerprint-v1 LF
378
+ then, for each file in ascending order of path (by Unicode code point):
379
+ path in UTF-8, one NUL byte, the 64 lowercase hex digits of the file hash, LF
380
+ ```
381
+
382
+ So a fingerprint changes when a file's content changes, when a file is
383
+ added, removed or renamed, and when content moves between files. It does not
384
+ change with the operating system's path separator, with CRLF versus LF (in
385
+ `lf` mode), with file times or permissions, or with where the project
386
+ directory is.
387
+
388
+ **Which files.** A sealed set is the list of files found at sealing time.
389
+ The patterns given to `seal` are kept too: a file that matches them later
390
+ but is not in the list counts as added. The engine is whatever the `include`
391
+ patterns select at the moment of hashing, minus the `ignore` patterns. The
392
+ ledger file itself is never included. Directory symlinks are not followed.
393
+
394
+ **Patterns.** `*` matches within one path segment, `?` one character,
395
+ `[abc]` a character class, `[!abc]` its negation, and a segment that is
396
+ exactly `**` any number of segments, including none. A pattern selects a
397
+ file if it matches the file's path or a directory above it, so `src`
398
+ selects everything under `src`. Include patterns are anchored at the ledger
399
+ directory. An ignore pattern without `/` is compared with every segment, so
400
+ `__pycache__` and `*.pyc` apply at any depth. Backslashes in patterns are
401
+ read as `/`. Matching is case-sensitive on every operating system.
402
+
403
+ **What is not hashed.** The Python version, installed packages, environment
404
+ variables, random seeds, data the engine downloads, and anything outside the
405
+ engine patterns. If one of those can change the number, put it in a file and
406
+ put the file in the engine.
407
+
408
+ ## Report wording
409
+
410
+ The report is assembled from sentences chosen by what the ledger and the
411
+ files show. These are the cases; the walkthrough above shows two complete
412
+ reports.
413
+
414
+ | situation | the report says |
415
+ | --- | --- |
416
+ | sealed, never run | "No run has been recorded against this set, so there is no number to report." |
417
+ | one run | "It has been run once, on DATE; that run is the blind run. Blind-run numbers: ..." |
418
+ | several runs | "It has been run 3 times; only run 1, on DATE, is blind. Blind-run numbers: ... The latest run is run 3 of the same set, a repeat, and is not blind: ...; the first-run numbers were ..." |
419
+ | blind run failed | "The blind run exited with code 1." and, if there are none, "The blind run recorded no metrics." |
420
+ | engine unchanged | "The engine fingerprint at the blind run was `...`; the engine files in this checkout match it." |
421
+ | engine changed since | "...; the engine files in this checkout have a different fingerprint (`...`), so the blind-run numbers describe earlier code, not this checkout." |
422
+ | a repeat measured changed code | "The engine was changed after the blind run: runs 2-3 measured different code." |
423
+ | only the engine configuration changed | "The engine configuration (which files count as the engine) was edited after the blind run: run 2 used a different configuration; compared with the blind run's configuration, run 2 measured the same engine files as the blind run." |
424
+ | blind run interrupted | "The blind run was interrupted before the command finished." |
425
+ | sealed files changed | "The sealed files have changed since sealing (changed: ...), so the recorded numbers do not describe the files in this checkout." |
426
+ | set was re-sealed | "Version 1 of this set was sealed DATE and run twice before it was replaced." |
427
+
428
+ Every report ends with the same sentence: "onelook records which files and
429
+ which code each run measured; it cannot show that the set was unseen while
430
+ the code was written."
431
+
432
+ Metrics are taken from the metrics JSON as written. Nested objects are
433
+ flattened to dotted keys, lists are left out, and floats are shown to four
434
+ significant digits.
435
+
436
+ ## Scoring helpers
437
+
438
+ `onelook.metrics` is a small library for binary decisions. You do not have
439
+ to use it; `onelook run` accepts any command and any metrics JSON.
440
+
441
+ ```python
442
+ >>> from onelook import metrics
443
+ >>> pairs = [(True, 0.9), (True, 0.4), (False, 0.6), (False, 0.1)]
444
+ >>> items = [metrics.Item(gold=gold, score=score) for gold, score in pairs]
445
+
446
+ >>> result = metrics.score_at(items, 0.5) # positive when score >= 0.5
447
+ >>> result.confusion
448
+ Confusion(tp=1, fp=1, fn=1, tn=1)
449
+ >>> result.precision, result.recall, result.f1, result.accuracy
450
+ (0.5, 0.5, 0.5, 0.5)
451
+
452
+ >>> [item.score for item in metrics.errors(items, 0.5).all] # every miss
453
+ [0.6, 0.4]
454
+
455
+ >>> low, high = metrics.wilson_interval(81, 263) # 95% by default
456
+ >>> round(low, 4), round(high, 4)
457
+ (0.2553, 0.3662)
458
+ ```
459
+
460
+ A baseline should be compared at its best threshold, not at yours.
461
+ `best_f1_threshold` finds it, and remembers which data it was found on:
462
+
463
+ ```python
464
+ >>> from onelook import metrics
465
+ >>> pairs = [(True, 0.9), (True, 0.4), (False, 0.6), (False, 0.1)]
466
+ >>> items = [metrics.Item(gold=gold, score=score) for gold, score in pairs]
467
+ >>> choice = metrics.best_f1_threshold(items)
468
+ >>> choice.threshold, round(choice.result.f1, 3)
469
+ (0.4, 0.8)
470
+ >>> choice.result.threshold_tuned_on_same_data
471
+ True
472
+ >>> choice.result.warning
473
+ 'threshold was chosen on the same data it is reported on; the result is the best any threshold reaches on this data, not an estimate for unseen data'
474
+
475
+ >>> other = [metrics.Item(gold=True, score=0.5), metrics.Item(gold=False, score=0.45)]
476
+ >>> metrics.score_at(other, choice).threshold_tuned_on_same_data
477
+ False
478
+ ```
479
+
480
+ Pass the `choice` object, not `choice.threshold`: a bare number carries no
481
+ record of where it came from, and the flag is then `None`. The flag and the
482
+ warning are included in `result.to_dict()`, so they reach the metrics file
483
+ and the report.
484
+
485
+ Details:
486
+
487
+ - Undefined values are `None`, not 0: precision when nothing is predicted
488
+ positive, recall when there are no gold positives, F1 when both, accuracy
489
+ on empty input. `wilson_interval(0, 0)` is `None`.
490
+ - `best_f1_threshold` tries each distinct score as a threshold. Items with
491
+ equal scores always fall on the same side. If several thresholds give the
492
+ same F1, the highest is returned. With no gold positives the threshold is
493
+ `None`.
494
+ - `wilson_interval` is the Wilson score interval without continuity
495
+ correction. `result.interval("precision" | "recall" | "accuracy")` applies
496
+ it; there is none for F1, which is not a simple proportion.
497
+ - `metrics.load_items(path)` reads `.csv` (with a header) or `.jsonl` with
498
+ the fields `gold`, `score` and/or `pred`, and optionally `id` and `text`.
499
+ `metrics.score_predictions(items)` scores by `pred` instead of a
500
+ threshold.
501
+
502
+ ## Library
503
+
504
+ The functions behind the commands can be called directly. Relative paths
505
+ are relative to the ledger's directory.
506
+
507
+ ```python
508
+ >>> import sys
509
+ >>> from onelook import Ledger, seal, fingerprint, record_run, status
510
+ >>> ledger = Ledger.load("onelook.json", create=True)
511
+ >>> seal(ledger, "blind", ["blind.csv"], note="written by a colleague").created
512
+ True
513
+ >>> engine = ledger.set_engine(["matcher.py"])
514
+ >>> ledger.save()
515
+ >>> fingerprint(ledger.root, ["matcher.py"]).hash == fingerprint(ledger.root, ["*.py"], ignore=["evaluate.py"]).hash
516
+ True
517
+ >>> run = record_run(ledger, "blind", [sys.executable, "evaluate.py"], metrics_file="metrics.json")
518
+ >>> run.record["run"], run.record["label"], run.record["metrics"]["n"]
519
+ (1, 'blind', 8)
520
+ >>> status(ledger).ok
521
+ True
522
+ >>> again = record_run(ledger, "blind", [sys.executable, "evaluate.py"], metrics_file="metrics.json")
523
+ >>> again.record["run"], again.record["label"]
524
+ (2, 'repeat')
525
+ ```
526
+
527
+ Also exported: `check_set`, `check_ledger`, `report`, `render_markdown`,
528
+ `verify`.
529
+
530
+ ## What it does not do
531
+
532
+ - **It cannot know whether you looked.** A run labelled `blind` means "the
533
+ first run recorded in this ledger against these exact files". If the
534
+ author read the blind set, tuned against a copy of it, or evaluated
535
+ without `onelook run`, the ledger looks the same. The label records a
536
+ claim; it does not prove it.
537
+ - **It cannot stop anyone from editing the ledger.** `onelook.json` is a
538
+ plain file. `verify` catches inconsistent edits, such as a renumbered run
539
+ or a repeat relabelled as blind. A careful edit, or a deleted and
540
+ recreated ledger, is invisible to onelook. The ledger is tamper-evident
541
+ only together with version control history: commit it after sealing and
542
+ after the blind run, and a reader can see when each record appeared and
543
+ whether any was later changed.
544
+ - **A number from a repeat run is not blind**, however it was obtained and
545
+ whatever the reason for the repeat. onelook will report it, labelled.
546
+ - **It does not check your evaluation.** It runs your command and stores
547
+ the JSON the command wrote. Whether the script reads the sealed files,
548
+ and whether its arithmetic is right, is up to you.
549
+ - **It does not capture the environment.** See "What is not hashed".
550
+ - **It does not judge the set.** Too small, too easy, unrepresentative, or
551
+ overlapping with the dev set: none of that is visible to a hash.
552
+
553
+ ## Protocol
554
+
555
+ The discipline onelook is built around. The tool helps with the items
556
+ marked with the command that covers them; the rest is on you.
557
+
558
+ 1. **Keep a dev set for tuning.** Look at it as much as you like. Do not
559
+ publish its numbers as test results.
560
+ 2. **Have the blind set written by someone who has not seen the
561
+ implementation**, or write it before the implementation exists. Whoever
562
+ knows how the engine works will, without meaning to, write cases it
563
+ handles.
564
+ 3. **Seal it before anything is run on it** (`onelook seal`), and commit
565
+ the ledger.
566
+ 4. **Fix every choice beforehand**: thresholds, prompts, preprocessing.
567
+ Declare the files that hold them as the engine
568
+ (`onelook fingerprint --save`).
569
+ 5. **Run the blind set once** (`onelook run`). Commit the ledger again.
570
+ 6. **Freeze the engine afterwards.** If it must change, the old number no
571
+ longer describes it (`onelook status` will say so), and a new number
572
+ needs a new blind set: a re-run on the old one is a repeat.
573
+ 7. **Give baselines their best threshold**
574
+ (`metrics.best_f1_threshold`), so that your method is not flattered by a
575
+ badly configured comparison, and say that the baseline's number is tuned
576
+ on the reported set.
577
+ 8. **List every miss** (`metrics.errors`). A reader learns more from the
578
+ false positives and false negatives than from the third decimal.
579
+ 9. **Report what happened** (`onelook report`), including repeats and
580
+ changes, and let readers check it (`onelook verify`).
581
+
582
+ ## Development
583
+
584
+ ```
585
+ python -m unittest discover -s tests -v
586
+ ```
587
+
588
+ The tests need no network and no installation; they include this README's
589
+ walkthrough and examples. See [CONTRIBUTING.md](CONTRIBUTING.md).
590
+
591
+ ## License
592
+
593
+ MIT. See [LICENSE](LICENSE).