# number-trace: check the numbers in AI-written text against your source tables

Version 0.1.0. Licence for the code: MIT (see the end of this file).

A small script that runs on your own machine. You give it the source tables (as text or CSV) and a piece of written text. It lists every number in the text that it cannot find in the source, lists numbers it found only by rounding or by a percent/proportion conversion, and lists strong or interpretive phrases in the text that the source does not contain.

- Offline. No network calls, no packages to install. Your data stays on your machine.
- Two implementations with the same behaviour and the same report: `number_trace.R` (base R) and `number_trace.py` (Python standard library).
- No SAS version is included. No SAS is available where this was built, so a macro could not be run, and an untested macro is not offered.

## Run it

```
Rscript number_trace.R  --source example/source_tables.txt --text example/text_planted_errors.txt
python  number_trace.py --source example/source_tables.txt --text example/text_planted_errors.txt
```

Options: `--source` takes one or more files; `--ignore 0.05,0.15` accepts numbers you know are not in the source (thresholds, for example); `--no-rounding`; `--no-percent`; `--verbose` lists every matched number and where it matched; `--out report.txt` writes the report to a file. Exit status is 0 if nothing was flagged, 1 if something was, 2 on a usage or file error.

## What counts as "found"

A number in the text is found if some number in the source matches it as one of:

1. exactly (same value, same decimals), ignoring leading zeros (`.05` = `0.05`), thousands commas (`1,234`) and minus-sign variants;
2. rounding: the text number has fewer decimals than a source number and equals it rounded half away from zero (`24` from `24.1`, `0.96` from `0.960`);
3. percent versus proportion: `75.6%` in the text with `0.756` in the source, or `0.756` in the text with `75.6` in the source.

Numbers that look like labels are ignored: `Table 14-3.01`, `Slide 4`, `p.43`, footnote marks such as `[1]`, and heading numbers at the start of a line (`11.1 Primary`). The phrase check is a fixed list of words and phrases (for example "statistically significant", "clinically meaningful", "well tolerated", "no new safety signals", "demonstrate", "most", "few", "none", "related to"). A phrase is reported only if the same pattern does not occur anywhere in the source.

## Tested results (2026-10-21)

Source for all runs: the CDISC pilot CSR Section 14 excerpt (`example/source_tables.txt`, Tables 14-1.01, 14-3.01, 14-3.02, 14-5.01, 14-5.02). R 4.6.1 and Python 3.14.3 produced byte-identical reports for all five distinct runs (checked with `cmp`). The reports are in `expected/`.

| Run | Numbers in text | Found | Not found | Phrases flagged |
|---|---|---|---|---|
| (a) correct passage: `text_correct.txt` (the good output of the csr-safety-efficacy page) | 174 | 173 | 1 | 3 |
| (b) planted errors: `text_planted_errors.txt` | 173 | 168 | 5 | 6 |
| (c1) csr-safety-efficacy, good output | same text as (a) | | | |
| (c2) csr-safety-efficacy, lazy output: `csr_lazy.txt` | 14 | 5 | 9 | 14 |
| (c3) study-results-deck-from-tfls, good output: `deck_good.txt` | 229 | 229 | 0 | 4 |
| (c4) study-results-deck-from-tfls, lazy output: `deck_lazy.txt` | 5 | 5 | 0 | 9 |

### False positives (flagged, but nothing wrong)

- Numbers, correct passage: 1 of 174. The text says "no p-value below 0.05"; 0.05 is a threshold the writer supplied, not in the tables. Use `--ignore 0.05` for this. One more number (0.15, also a threshold) was listed under "found only by conversion" because the source has a count of 15; it is not an error either.
- Numbers, good deck: 0 of 229.
- Phrases, correct passage: 3 of 3 are false positives. "improvement" and "worsening" appear in a sentence saying no such statement is made; "due to" appears in a list of things the tables do not contain. The tool matches words and does not read negation.
- Phrases, good deck: 4 of 4 are false positives ("confirm" twice, "benefit" and "due to", all inside lists of what the tables do not contain).
- So 7 of 7 phrase flags on the two good passages are false positives. Treat phrase flags as prompts to read the sentence, not as findings.

### False negatives (a wrong or unsupported item not reported under "not found")

Planted-errors passage, 9 planted number errors: 5 reported as not found (243 for 234; p=0.059 for 0.569; 65.0% for 56.0%; 76.5% for 75.6%; p=0.234 for 0.243). 4 not reported as not found:

- a dropped minus sign (`1.0` for `-1.0`): it matched by rounding to a different source value (`0.960`). It does appear in the "found only by rounding" list, so a reader checking that list would see it;
- two arms' values in swapped order (the erythema row reversed): every number exists in the source, in the wrong arm. Not detectable by design;
- one adverse-event row given another row's values (dizziness given the sinus bradycardia numbers): not detectable by design;
- a wrong rounded value (`25` for `24.4`): `25` exists in the source as a count and as `25.0%`.

Also in that passage, 3 of 3 inserted interpretive phrases ("clinically meaningful", "improvement", "well tolerated") were flagged. The other 3 phrase flags in that run are the same false positives as in the correct passage.

Lazy CSR text, 12 invented numeric claims (-3.2, -0.8, -2.4, -3.9, -0.9, p=0.002, 42%, 28%, p=0.01, 150, 68%, 61%): 9 reported as not found, 3 missed. Missed: `p=0.002` (that value exists in an unrelated adverse-event row), `28%` (28 exists as a count in the populations table, and the tool does not require the percent sign to match a count), and `p=0.01` (matches `0.007` by rounding; it is listed under "found only by rounding"). The other two numbers found (`24` from "Week 24" and `95%` from "95% CI") are labels, not claims. This text also has 14 phrase flags, which are real unsupported claims ("statistically significant", "safe and well tolerated", "no new safety signals").

Lazy deck outline: the tool finds no unsupported numbers because the text contains almost none (it uses placeholders). It flags 9 phrases, all real unsupported claims ("Statistically significant and clinically meaningful", "Well Tolerated", "No new safety signals"). The number check alone passes the lazy deck; the phrase list is what catches it.

Good deck: the number check passes all 229 numbers. The by-hand read recorded in `content/flagships/VERIFICATION_REPORT_study-results-deck-from-tfls.md` found no wrong number but two wrong statements about footnotes. This tool cannot see those.

These are small samples, one source excerpt, and passages written for this project. Do not read the counts as general accuracy rates.

## Limits

- It proves a number appears somewhere in the source. It does not prove the number is the right one, or in the right row, column, arm or table, or that the surrounding words are correct.
- Short numbers match by chance. The report counts single-digit integers found exactly (14 in the correct passage); treat those as weak support.
- Rounding and percent/proportion matching loosen the check, and a wrong number can match a different source value this way. Review the "found only by rounding or percent/proportion" section by eye, or use `--no-rounding` and `--no-percent` for a strict pass.
- A wrong number that happens to exist elsewhere in the source passes.
- It does not compute anything. A derived number (a percent worked out from n/N, a difference, a sum) is reported as not found unless the source prints it.
- It does not check the reverse: source values the text never mentions.
- The phrase list is fixed and English only, matches words without understanding negation, and will miss unsupported claims worded differently.
- Source and text are read as plain text. Numbers inside images, scanned PDFs or Word tables must be converted to text first. Word and PDF files are not read.
- Number extraction is a pattern: dates, identifiers, version numbers and doses will appear as numbers. Use `--ignore` for known extras.
- A clean report is not a pass. It is a first filter before a person reads the text against the tables.
- Not validated software. Not a substitute for your QC process.

## Example data

`example/source_tables.txt` is an excerpt of Section 14 of the CDISCPILOT01 clinical study report (CDISC SDTM/ADaM Pilot Project, https://github.com/cdisc-org/sdtm-adam-pilot-project): Tables 14-1.01, 14-3.01, 14-3.02, 14-5.01 (selected rows) and 14-5.02. It is a condensed, re-typed excerpt: numbers are copied from the CSR tables, but table titles are shortened, some footnotes are abridged and two truncated labels are expanded, so it is not the original wording. Attribution to CDISC is required, and CDISC's terms ask for no modification of the material and no sale; check those terms against the original report before reusing the example. The study is a repurposed mock submission with fictional dates, not a real filing.

The texts in `example/` are the outputs recorded in `content/flagships/csr-safety-efficacy-text-from-tables.json` and `content/flagships/study-results-deck-from-tfls.json` (good and lazy), and `text_planted_errors.txt`. That file is the correct passage with 11 hand edits: 9 number errors, 2 harmless roundings (`24.1` to `24` and `0.960` to `0.96`), and one sentence replaced by a sentence with 3 unsupported phrases. Two of the edits are in one place (`24` and `25` in the same line).

## Files

- `number_trace.R`, `number_trace.py`: the tool.
- `example/`: source tables and test texts.
- `expected/`: the report each run produced (identical from R and Python).
- `PAGE.md`: copy for a tool page.

## Licence

The code in this folder is released under the MIT licence.

Copyright (c) 2026 StatAgent

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

The example data in `example/source_tables.txt` is CDISC material and is not covered by this licence.
