llmvalidate 0.5.0__tar.gz → 0.6.0__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.
- {llmvalidate-0.5.0/src/llmvalidate.egg-info → llmvalidate-0.6.0}/PKG-INFO +39 -18
- {llmvalidate-0.5.0 → llmvalidate-0.6.0}/pyproject.toml +1 -1
- {llmvalidate-0.5.0 → llmvalidate-0.6.0}/readme.md +39 -18
- {llmvalidate-0.5.0 → llmvalidate-0.6.0}/src/llmvalidate/validation.py +36 -14
- {llmvalidate-0.5.0 → llmvalidate-0.6.0/src/llmvalidate.egg-info}/PKG-INFO +39 -18
- {llmvalidate-0.5.0 → llmvalidate-0.6.0}/LICENSE +0 -0
- {llmvalidate-0.5.0 → llmvalidate-0.6.0}/setup.cfg +0 -0
- {llmvalidate-0.5.0 → llmvalidate-0.6.0}/src/llmvalidate/__init__.py +0 -0
- {llmvalidate-0.5.0 → llmvalidate-0.6.0}/src/llmvalidate/structured.py +0 -0
- {llmvalidate-0.5.0 → llmvalidate-0.6.0}/src/llmvalidate/utils.py +0 -0
- {llmvalidate-0.5.0 → llmvalidate-0.6.0}/src/llmvalidate.egg-info/SOURCES.txt +0 -0
- {llmvalidate-0.5.0 → llmvalidate-0.6.0}/src/llmvalidate.egg-info/dependency_links.txt +0 -0
- {llmvalidate-0.5.0 → llmvalidate-0.6.0}/src/llmvalidate.egg-info/requires.txt +0 -0
- {llmvalidate-0.5.0 → llmvalidate-0.6.0}/src/llmvalidate.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: llmvalidate
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Summary: Oncoshot LLM validation framework
|
|
5
5
|
License: MIT
|
|
6
6
|
Project-URL: Homepage, https://github.com/Oncoshot/llm-validation-framework
|
|
@@ -24,20 +24,26 @@ Dynamic: license-file
|
|
|
24
24
|
|
|
25
25
|
# LLM Validation Framework
|
|
26
26
|
|
|
27
|
-
[](https://github.com/Oncoshot/llm-validation-framework/actions)
|
|
28
28
|
[](https://pypi.org/project/llmvalidate/)
|
|
29
29
|
[](https://pypi.org/project/llmvalidate/)
|
|
30
|
-
[](https://github.com/Oncoshot/llm-validation-framework/blob/master/LICENSE)
|
|
31
|
+
[](https://github.com/Oncoshot/llm-validation-framework/stargazers)
|
|
32
32
|
|
|
33
33
|
A comprehensive Python framework for evaluating LLM-extracted structured data against ground truth labels. Supports binary classification, scalar values, and list fields with detailed performance metrics, confidence-based evaluation, and statistical uncertainty quantification via non-parametric bootstrap confidence intervals.
|
|
34
34
|
|
|
35
|
+
## 📄 Paper
|
|
36
|
+
|
|
37
|
+
The methodology behind this framework is described in a medRxiv preprint:
|
|
38
|
+
[medRxiv 2026.05.18.26353541](https://www.medrxiv.org/content/10.64898/2026.05.18.26353541v1) — DOI: [10.64898/2026.05.18.26353541](https://doi.org/10.64898/2026.05.18.26353541)
|
|
39
|
+
|
|
35
40
|
## ✨ Key Features
|
|
36
41
|
|
|
37
42
|
- **Multi-field validation** - Binary (True/False), scalar (single values), and list (multiple values) data types
|
|
43
|
+
- **Coded field support** - Score a value together with its code (ICD-10 / ICD-O-3, RxNorm) as two independent facets
|
|
38
44
|
- **Partial labeling support** - Handle datasets where different cases have labels for different subsets of fields
|
|
39
45
|
- **Dual usage modes** - Validate pre-computed results OR run live LLM inference with validation
|
|
40
|
-
- **Comprehensive metrics** - Precision, recall, F1/F2, accuracy, specificity with
|
|
46
|
+
- **Comprehensive metrics** - Precision, recall, F1/F2, accuracy, specificity, with micro and macro aggregation where applicable
|
|
41
47
|
- **Confidence analysis** - Automatic performance breakdown by confidence levels
|
|
42
48
|
- **Statistical uncertainty** - Non-parametric bootstrap confidence intervals for all performance metrics
|
|
43
49
|
- **Production ready** - Parallel processing, intelligent caching, detailed progress tracking
|
|
@@ -60,14 +66,14 @@ python runme.py
|
|
|
60
66
|
|
|
61
67
|
Processes the included [samples.csv](samples.csv) (14 test cases covering all validation scenarios) and outputs timestamped results to `validation_results/samples/`:
|
|
62
68
|
|
|
63
|
-
- **[Results CSV](validation_results/samples/2026-
|
|
64
|
-
- **[Metrics CSV](validation_results/samples/2026-
|
|
65
|
-
- **[CI Metrics CSV](validation_results/samples/2026-
|
|
69
|
+
- **[Results CSV](validation_results/samples/2026-07-13%2001-12-18%20results.csv)** - Row-by-row comparison with confusion matrix counts and item-level details
|
|
70
|
+
- **[Metrics CSV](validation_results/samples/2026-07-13%2001-12-18%20metrics.csv)** - Aggregated performance statistics with confidence breakdowns
|
|
71
|
+
- **[CI Metrics CSV](validation_results/samples/2026-07-13%2001-12-18%20CI%20metrics.csv)** - Confidence intervals for metrics
|
|
66
72
|
|
|
67
73
|
| Rows | Field Type | Test Scenarios |
|
|
68
74
|
|------|------------|----------------|
|
|
69
75
|
| **1-4** | Binary (`Has metastasis`) | True Positive, True Negative, False Positive, False Negative |
|
|
70
|
-
| **5-9** | Scalar (`Diagnosis`, `Histology`) | Correct, incorrect, missing, spurious, and empty extractions |
|
|
76
|
+
| **5-9** | Scalar (`Diagnosis`, `Histology`) | Correct, incorrect, missing, spurious, and correct-empty (TN) extractions |
|
|
71
77
|
| **10-14** | List (`Treatment Drugs`, `Test Results`) | Perfect match, spurious items, missing items, correct empty, mixed results |
|
|
72
78
|
|
|
73
79
|
## 📊 Usage Modes
|
|
@@ -77,7 +83,7 @@ When you have LLM predictions in `Res: {Field Name}` columns:
|
|
|
77
83
|
|
|
78
84
|
```python
|
|
79
85
|
import pandas as pd
|
|
80
|
-
from
|
|
86
|
+
from llmvalidate import validate
|
|
81
87
|
|
|
82
88
|
df = pd.read_csv("data.csv", index_col="Patient ID")
|
|
83
89
|
# df must contain: "Field Name" and "Res: Field Name" columns
|
|
@@ -93,8 +99,8 @@ results_df, metrics_df = validate(
|
|
|
93
99
|
### Mode 2: Live LLM Inference + Validation
|
|
94
100
|
|
|
95
101
|
```python
|
|
96
|
-
from
|
|
97
|
-
from
|
|
102
|
+
from llmvalidate.structured import StructuredResult, StructuredGroup, StructuredField
|
|
103
|
+
from llmvalidate.utils import flatten_structured_result
|
|
98
104
|
|
|
99
105
|
def llm_callback(row, i, raw_text_column_name):
|
|
100
106
|
raw_text = row[raw_text_column_name]
|
|
@@ -164,6 +170,14 @@ with the `-value` / `-code` label columns.
|
|
|
164
170
|
- **`null/empty/NaN`** = Field not labeled/evaluated (supports partial labeling where different cases may have labels for different field subsets)
|
|
165
171
|
- **Lists** - Can be Python lists `["a", "b"]` or stringified `"['a', 'b']"` (auto-converted)
|
|
166
172
|
|
|
173
|
+
The `"-"`-vs-empty distinction above applies to **label** columns only. In **prediction** (`Res:`)
|
|
174
|
+
columns there is no "not evaluated" state: `"-"`, `""`, `null`/`NaN`, whitespace-only strings and
|
|
175
|
+
`[]` are all treated identically as "nothing extracted" (scoring Mis against a labeled value, or
|
|
176
|
+
TN when the label is `"-"`). We still recommend emitting `"-"` uniformly, to keep predictions
|
|
177
|
+
symmetric with labels. **Exception — binary fields:** predictions must be an explicit
|
|
178
|
+
`True`/`False`; an empty prediction is "not True / not False", so it scores FN against a `True`
|
|
179
|
+
label and **FP** against a `False` label.
|
|
180
|
+
|
|
167
181
|
### Partial Labeling Support
|
|
168
182
|
The framework supports partial labeling scenarios where:
|
|
169
183
|
- Not every case needs labels for every field
|
|
@@ -174,7 +188,7 @@ The framework supports partial labeling scenarios where:
|
|
|
174
188
|
|
|
175
189
|
## 📈 Output Files
|
|
176
190
|
|
|
177
|
-
|
|
191
|
+
A `validate()` run generates two timestamped CSV files (a third, CI metrics, is added when you also run `bootstrap_CI` — see the demo above):
|
|
178
192
|
|
|
179
193
|
### 1. Results CSV (`YYYY-MM-DD HH-MM-SS results.csv`)
|
|
180
194
|
**Row-level analysis** with detailed per-case metrics:
|
|
@@ -188,7 +202,7 @@ The framework generates two timestamped CSV files for each validation run:
|
|
|
188
202
|
- `TP/FP/FN/TN: {Field}` - Confusion matrix counts (1 or 0 per row)
|
|
189
203
|
|
|
190
204
|
**Non-Binary Fields:**
|
|
191
|
-
- `Cor/
|
|
205
|
+
- `Cor/Mis/Spu: {Field}` - Item counts per row (Inc and TN are None for list fields)
|
|
192
206
|
- `Cor/Inc/Mis/Spu: {Field} items` - Actual item lists
|
|
193
207
|
- `Precision/Recall/F1/F2: {Field}` - Per-row metrics (list fields only)
|
|
194
208
|
|
|
@@ -208,9 +222,14 @@ The framework generates two timestamped CSV files for each validation run:
|
|
|
208
222
|
|
|
209
223
|
**Binary Metrics:** `TP`, `TN`, `FP`, `FN`, `precision`, `recall`, `F1/F2`, `accuracy`, `specificity`
|
|
210
224
|
|
|
211
|
-
**Non-Binary Metrics:** `cor`, `inc`, `mis`, `spu`, `precision/recall/F1/F2 (micro
|
|
225
|
+
**Non-Binary Metrics:** `cor`, `inc`, `mis`, `spu`, `TN`, `precision/recall/F1/F2 (micro and macro)`, `specificity`
|
|
226
|
+
|
|
227
|
+
Applicability: `cor`, `mis`, `spu` and the `(micro)` precision/recall/F-scores apply to all non-binary fields; `inc`, `TN` and `specificity` are meaningful for **scalar fields only**; the `(macro)` metrics are averages of the per-row metrics and exist for **list fields only**. `accuracy` and `specificity` carry no `(micro)`/`(macro)` tag: they are only ever computed one way (pooled counts), since a per-row version would be a 0/1 indicator whose average equals the pooled value.
|
|
212
228
|
|
|
213
229
|
## ⚡ Performance Metrics Explained
|
|
230
|
+
|
|
231
|
+

|
|
232
|
+
|
|
214
233
|
### Binary Classification Metrics
|
|
215
234
|
|
|
216
235
|
For fields with True/False values (e.g., "Has metastasis"):
|
|
@@ -241,6 +260,7 @@ For scalar and list fields (e.g., "Diagnosis", "Treatment Drugs"):
|
|
|
241
260
|
| **Missing (Mis)** | Items present in label but not extracted | (Same example) → Mis=1 (DrugB missing) |
|
|
242
261
|
| **Spurious (Spu)** | Items extracted but not in label | Label: `["DrugA"]`, Prediction: `["DrugA", "DrugC"]` → Spu=1 |
|
|
243
262
|
| **Incorrect (Inc)** | Wrong values for scalar fields | Label: `"Cancer"`, Prediction: `"Diabetes"` → Inc=1 |
|
|
263
|
+
| **True Negative (TN)** | Scalar fields only: field correctly left empty | Label: `"-"`, Prediction: `""`/`"-"` → TN=1 |
|
|
244
264
|
|
|
245
265
|
#### Structured Extraction Formulas
|
|
246
266
|
|
|
@@ -248,8 +268,9 @@ For scalar and list fields (e.g., "Diagnosis", "Treatment Drugs"):
|
|
|
248
268
|
|--------|---------|---------|
|
|
249
269
|
| **Precision** | `Cor / (Cor + Spu + Inc)` | Of all extracted items, how many were correct? |
|
|
250
270
|
| **Recall** | `Cor / (Cor + Mis + Inc)` | Of all labeled items, how many were correctly extracted? |
|
|
271
|
+
| **Specificity** | `TN / (TN + Spu)` | Scalar fields only: of all cases labeled as having no information, how many were correctly left empty? |
|
|
251
272
|
|
|
252
|
-
**Note:**
|
|
273
|
+
**Note:** Inc and TN (and therefore specificity) are defined only for scalar fields. For list fields, extracted items are always classified as correct, missing, or spurious — Inc stays empty and no TN column is emitted.
|
|
253
274
|
|
|
254
275
|
The following formulas apply to both binary classification and structured extraction metrics:
|
|
255
276
|
|
|
@@ -266,7 +287,7 @@ The framework includes statistical confidence interval estimation using non-para
|
|
|
266
287
|
|
|
267
288
|
### Usage
|
|
268
289
|
```python
|
|
269
|
-
from
|
|
290
|
+
from llmvalidate import bootstrap_CI
|
|
270
291
|
|
|
271
292
|
# After running validation to get results_df
|
|
272
293
|
ci_results = bootstrap_CI(
|
|
@@ -347,7 +368,7 @@ pip install -r requirements.txt
|
|
|
347
368
|
pytest
|
|
348
369
|
|
|
349
370
|
# Run with coverage reporting
|
|
350
|
-
pytest --cov=
|
|
371
|
+
pytest --cov=llmvalidate
|
|
351
372
|
|
|
352
373
|
# Run specific test modules
|
|
353
374
|
pytest tests/validate_test.py # Core validation logic
|
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
# LLM Validation Framework
|
|
2
2
|
|
|
3
|
-
[](https://github.com/Oncoshot/llm-validation-framework/actions)
|
|
4
4
|
[](https://pypi.org/project/llmvalidate/)
|
|
5
5
|
[](https://pypi.org/project/llmvalidate/)
|
|
6
|
-
[](https://github.com/Oncoshot/llm-validation-framework/blob/master/LICENSE)
|
|
7
|
+
[](https://github.com/Oncoshot/llm-validation-framework/stargazers)
|
|
8
8
|
|
|
9
9
|
A comprehensive Python framework for evaluating LLM-extracted structured data against ground truth labels. Supports binary classification, scalar values, and list fields with detailed performance metrics, confidence-based evaluation, and statistical uncertainty quantification via non-parametric bootstrap confidence intervals.
|
|
10
10
|
|
|
11
|
+
## 📄 Paper
|
|
12
|
+
|
|
13
|
+
The methodology behind this framework is described in a medRxiv preprint:
|
|
14
|
+
[medRxiv 2026.05.18.26353541](https://www.medrxiv.org/content/10.64898/2026.05.18.26353541v1) — DOI: [10.64898/2026.05.18.26353541](https://doi.org/10.64898/2026.05.18.26353541)
|
|
15
|
+
|
|
11
16
|
## ✨ Key Features
|
|
12
17
|
|
|
13
18
|
- **Multi-field validation** - Binary (True/False), scalar (single values), and list (multiple values) data types
|
|
19
|
+
- **Coded field support** - Score a value together with its code (ICD-10 / ICD-O-3, RxNorm) as two independent facets
|
|
14
20
|
- **Partial labeling support** - Handle datasets where different cases have labels for different subsets of fields
|
|
15
21
|
- **Dual usage modes** - Validate pre-computed results OR run live LLM inference with validation
|
|
16
|
-
- **Comprehensive metrics** - Precision, recall, F1/F2, accuracy, specificity with
|
|
22
|
+
- **Comprehensive metrics** - Precision, recall, F1/F2, accuracy, specificity, with micro and macro aggregation where applicable
|
|
17
23
|
- **Confidence analysis** - Automatic performance breakdown by confidence levels
|
|
18
24
|
- **Statistical uncertainty** - Non-parametric bootstrap confidence intervals for all performance metrics
|
|
19
25
|
- **Production ready** - Parallel processing, intelligent caching, detailed progress tracking
|
|
@@ -36,14 +42,14 @@ python runme.py
|
|
|
36
42
|
|
|
37
43
|
Processes the included [samples.csv](samples.csv) (14 test cases covering all validation scenarios) and outputs timestamped results to `validation_results/samples/`:
|
|
38
44
|
|
|
39
|
-
- **[Results CSV](validation_results/samples/2026-
|
|
40
|
-
- **[Metrics CSV](validation_results/samples/2026-
|
|
41
|
-
- **[CI Metrics CSV](validation_results/samples/2026-
|
|
45
|
+
- **[Results CSV](validation_results/samples/2026-07-13%2001-12-18%20results.csv)** - Row-by-row comparison with confusion matrix counts and item-level details
|
|
46
|
+
- **[Metrics CSV](validation_results/samples/2026-07-13%2001-12-18%20metrics.csv)** - Aggregated performance statistics with confidence breakdowns
|
|
47
|
+
- **[CI Metrics CSV](validation_results/samples/2026-07-13%2001-12-18%20CI%20metrics.csv)** - Confidence intervals for metrics
|
|
42
48
|
|
|
43
49
|
| Rows | Field Type | Test Scenarios |
|
|
44
50
|
|------|------------|----------------|
|
|
45
51
|
| **1-4** | Binary (`Has metastasis`) | True Positive, True Negative, False Positive, False Negative |
|
|
46
|
-
| **5-9** | Scalar (`Diagnosis`, `Histology`) | Correct, incorrect, missing, spurious, and empty extractions |
|
|
52
|
+
| **5-9** | Scalar (`Diagnosis`, `Histology`) | Correct, incorrect, missing, spurious, and correct-empty (TN) extractions |
|
|
47
53
|
| **10-14** | List (`Treatment Drugs`, `Test Results`) | Perfect match, spurious items, missing items, correct empty, mixed results |
|
|
48
54
|
|
|
49
55
|
## 📊 Usage Modes
|
|
@@ -53,7 +59,7 @@ When you have LLM predictions in `Res: {Field Name}` columns:
|
|
|
53
59
|
|
|
54
60
|
```python
|
|
55
61
|
import pandas as pd
|
|
56
|
-
from
|
|
62
|
+
from llmvalidate import validate
|
|
57
63
|
|
|
58
64
|
df = pd.read_csv("data.csv", index_col="Patient ID")
|
|
59
65
|
# df must contain: "Field Name" and "Res: Field Name" columns
|
|
@@ -69,8 +75,8 @@ results_df, metrics_df = validate(
|
|
|
69
75
|
### Mode 2: Live LLM Inference + Validation
|
|
70
76
|
|
|
71
77
|
```python
|
|
72
|
-
from
|
|
73
|
-
from
|
|
78
|
+
from llmvalidate.structured import StructuredResult, StructuredGroup, StructuredField
|
|
79
|
+
from llmvalidate.utils import flatten_structured_result
|
|
74
80
|
|
|
75
81
|
def llm_callback(row, i, raw_text_column_name):
|
|
76
82
|
raw_text = row[raw_text_column_name]
|
|
@@ -140,6 +146,14 @@ with the `-value` / `-code` label columns.
|
|
|
140
146
|
- **`null/empty/NaN`** = Field not labeled/evaluated (supports partial labeling where different cases may have labels for different field subsets)
|
|
141
147
|
- **Lists** - Can be Python lists `["a", "b"]` or stringified `"['a', 'b']"` (auto-converted)
|
|
142
148
|
|
|
149
|
+
The `"-"`-vs-empty distinction above applies to **label** columns only. In **prediction** (`Res:`)
|
|
150
|
+
columns there is no "not evaluated" state: `"-"`, `""`, `null`/`NaN`, whitespace-only strings and
|
|
151
|
+
`[]` are all treated identically as "nothing extracted" (scoring Mis against a labeled value, or
|
|
152
|
+
TN when the label is `"-"`). We still recommend emitting `"-"` uniformly, to keep predictions
|
|
153
|
+
symmetric with labels. **Exception — binary fields:** predictions must be an explicit
|
|
154
|
+
`True`/`False`; an empty prediction is "not True / not False", so it scores FN against a `True`
|
|
155
|
+
label and **FP** against a `False` label.
|
|
156
|
+
|
|
143
157
|
### Partial Labeling Support
|
|
144
158
|
The framework supports partial labeling scenarios where:
|
|
145
159
|
- Not every case needs labels for every field
|
|
@@ -150,7 +164,7 @@ The framework supports partial labeling scenarios where:
|
|
|
150
164
|
|
|
151
165
|
## 📈 Output Files
|
|
152
166
|
|
|
153
|
-
|
|
167
|
+
A `validate()` run generates two timestamped CSV files (a third, CI metrics, is added when you also run `bootstrap_CI` — see the demo above):
|
|
154
168
|
|
|
155
169
|
### 1. Results CSV (`YYYY-MM-DD HH-MM-SS results.csv`)
|
|
156
170
|
**Row-level analysis** with detailed per-case metrics:
|
|
@@ -164,7 +178,7 @@ The framework generates two timestamped CSV files for each validation run:
|
|
|
164
178
|
- `TP/FP/FN/TN: {Field}` - Confusion matrix counts (1 or 0 per row)
|
|
165
179
|
|
|
166
180
|
**Non-Binary Fields:**
|
|
167
|
-
- `Cor/
|
|
181
|
+
- `Cor/Mis/Spu: {Field}` - Item counts per row (Inc and TN are None for list fields)
|
|
168
182
|
- `Cor/Inc/Mis/Spu: {Field} items` - Actual item lists
|
|
169
183
|
- `Precision/Recall/F1/F2: {Field}` - Per-row metrics (list fields only)
|
|
170
184
|
|
|
@@ -184,9 +198,14 @@ The framework generates two timestamped CSV files for each validation run:
|
|
|
184
198
|
|
|
185
199
|
**Binary Metrics:** `TP`, `TN`, `FP`, `FN`, `precision`, `recall`, `F1/F2`, `accuracy`, `specificity`
|
|
186
200
|
|
|
187
|
-
**Non-Binary Metrics:** `cor`, `inc`, `mis`, `spu`, `precision/recall/F1/F2 (micro
|
|
201
|
+
**Non-Binary Metrics:** `cor`, `inc`, `mis`, `spu`, `TN`, `precision/recall/F1/F2 (micro and macro)`, `specificity`
|
|
202
|
+
|
|
203
|
+
Applicability: `cor`, `mis`, `spu` and the `(micro)` precision/recall/F-scores apply to all non-binary fields; `inc`, `TN` and `specificity` are meaningful for **scalar fields only**; the `(macro)` metrics are averages of the per-row metrics and exist for **list fields only**. `accuracy` and `specificity` carry no `(micro)`/`(macro)` tag: they are only ever computed one way (pooled counts), since a per-row version would be a 0/1 indicator whose average equals the pooled value.
|
|
188
204
|
|
|
189
205
|
## ⚡ Performance Metrics Explained
|
|
206
|
+
|
|
207
|
+

|
|
208
|
+
|
|
190
209
|
### Binary Classification Metrics
|
|
191
210
|
|
|
192
211
|
For fields with True/False values (e.g., "Has metastasis"):
|
|
@@ -217,6 +236,7 @@ For scalar and list fields (e.g., "Diagnosis", "Treatment Drugs"):
|
|
|
217
236
|
| **Missing (Mis)** | Items present in label but not extracted | (Same example) → Mis=1 (DrugB missing) |
|
|
218
237
|
| **Spurious (Spu)** | Items extracted but not in label | Label: `["DrugA"]`, Prediction: `["DrugA", "DrugC"]` → Spu=1 |
|
|
219
238
|
| **Incorrect (Inc)** | Wrong values for scalar fields | Label: `"Cancer"`, Prediction: `"Diabetes"` → Inc=1 |
|
|
239
|
+
| **True Negative (TN)** | Scalar fields only: field correctly left empty | Label: `"-"`, Prediction: `""`/`"-"` → TN=1 |
|
|
220
240
|
|
|
221
241
|
#### Structured Extraction Formulas
|
|
222
242
|
|
|
@@ -224,8 +244,9 @@ For scalar and list fields (e.g., "Diagnosis", "Treatment Drugs"):
|
|
|
224
244
|
|--------|---------|---------|
|
|
225
245
|
| **Precision** | `Cor / (Cor + Spu + Inc)` | Of all extracted items, how many were correct? |
|
|
226
246
|
| **Recall** | `Cor / (Cor + Mis + Inc)` | Of all labeled items, how many were correctly extracted? |
|
|
247
|
+
| **Specificity** | `TN / (TN + Spu)` | Scalar fields only: of all cases labeled as having no information, how many were correctly left empty? |
|
|
227
248
|
|
|
228
|
-
**Note:**
|
|
249
|
+
**Note:** Inc and TN (and therefore specificity) are defined only for scalar fields. For list fields, extracted items are always classified as correct, missing, or spurious — Inc stays empty and no TN column is emitted.
|
|
229
250
|
|
|
230
251
|
The following formulas apply to both binary classification and structured extraction metrics:
|
|
231
252
|
|
|
@@ -242,7 +263,7 @@ The framework includes statistical confidence interval estimation using non-para
|
|
|
242
263
|
|
|
243
264
|
### Usage
|
|
244
265
|
```python
|
|
245
|
-
from
|
|
266
|
+
from llmvalidate import bootstrap_CI
|
|
246
267
|
|
|
247
268
|
# After running validation to get results_df
|
|
248
269
|
ci_results = bootstrap_CI(
|
|
@@ -323,7 +344,7 @@ pip install -r requirements.txt
|
|
|
323
344
|
pytest
|
|
324
345
|
|
|
325
346
|
# Run with coverage reporting
|
|
326
|
-
pytest --cov=
|
|
347
|
+
pytest --cov=llmvalidate
|
|
327
348
|
|
|
328
349
|
# Run specific test modules
|
|
329
350
|
pytest tests/validate_test.py # Core validation logic
|
|
@@ -359,4 +380,4 @@ llm-validation-framework/
|
|
|
359
380
|
|
|
360
381
|
## 📄 License
|
|
361
382
|
|
|
362
|
-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
383
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
@@ -75,10 +75,11 @@ def compare_results_all(df, fields, parents={}, comparison_callback=None, raw_te
|
|
|
75
75
|
|
|
76
76
|
Returns pandas data frame same as df but with added columns:
|
|
77
77
|
'Cor: ' for Correct
|
|
78
|
-
'Inc: ' for Incorrect
|
|
78
|
+
'Inc: ' for Incorrect (scalar fields; for list fields only when 'parents' is provided)
|
|
79
79
|
'Mis: ' for Missing
|
|
80
80
|
'Spu: ' for Spurious
|
|
81
|
-
'Par: ' for Partial
|
|
81
|
+
'Par: ' for Partial (only when 'parents' is provided)
|
|
82
|
+
'TN: ' for True Negative (scalar fields only; not defined for list fields)
|
|
82
83
|
"""
|
|
83
84
|
|
|
84
85
|
fields = [f for f in fields if f in df] #ignore fields which are not in columns (they may be added later by comparison_callback)
|
|
@@ -88,6 +89,16 @@ def compare_results_all(df, fields, parents={}, comparison_callback=None, raw_te
|
|
|
88
89
|
for field in fields:
|
|
89
90
|
binary_fields[field] = pd.api.types.is_bool_dtype(pd.api.types.infer_dtype(df[field].dropna()))
|
|
90
91
|
|
|
92
|
+
# Determine which fields are list fields (any label or result value is a list).
|
|
93
|
+
# TN is only defined for scalar fields, so list fields never get a 'TN: ' column.
|
|
94
|
+
list_fields = {}
|
|
95
|
+
for field in fields:
|
|
96
|
+
is_list = df[field].map(type).eq(list).any()
|
|
97
|
+
res_col = 'Res: ' + field
|
|
98
|
+
if not is_list and res_col in df.columns:
|
|
99
|
+
is_list = df[res_col].map(type).eq(list).any()
|
|
100
|
+
list_fields[field] = is_list
|
|
101
|
+
|
|
91
102
|
# Create a list to store modified rows
|
|
92
103
|
modified_rows = []
|
|
93
104
|
# Iterate over the rows of the DataFrame
|
|
@@ -134,6 +145,23 @@ def compare_results_all(df, fields, parents={}, comparison_callback=None, raw_te
|
|
|
134
145
|
del res['Partial'] #partial appear only if parent match
|
|
135
146
|
del res_items['Partial']
|
|
136
147
|
|
|
148
|
+
if not list_fields[field]:
|
|
149
|
+
# Scalar field: report per-case TN so true negatives (field correctly
|
|
150
|
+
# left empty) are visible row by row. TN = 1 if the case was labeled
|
|
151
|
+
# as "No information" (label '-') AND the prediction is empty.
|
|
152
|
+
# None (undefined) when the case was not labeled for this field.
|
|
153
|
+
# TN is not defined for list fields (see get_metrics).
|
|
154
|
+
if res['Correct'] is None:
|
|
155
|
+
row['TN: ' + field] = None
|
|
156
|
+
#row['TN: ' + field + ' items'] = None
|
|
157
|
+
else:
|
|
158
|
+
TN_case = 1 if (is_scalar_empty(normalize(expected))
|
|
159
|
+
and is_scalar_empty(normalize(actual))) else 0
|
|
160
|
+
row['TN: ' + field] = TN_case
|
|
161
|
+
# a TN case has no extracted item: the matched "item" is the
|
|
162
|
+
# no-information marker itself therefore pointless to add
|
|
163
|
+
# row['TN: ' + field + ' items'] = ['-'] if TN_case else []
|
|
164
|
+
|
|
137
165
|
# Iterate over the key-value pairs in the result dictionary
|
|
138
166
|
for key, value in res.items():
|
|
139
167
|
# Create a new column in the DataFrame using the key
|
|
@@ -264,7 +292,7 @@ def get_metrics(res_df, fields):
|
|
|
264
292
|
'labeled cases': labeled_cases,
|
|
265
293
|
'field-present cases': exceptions_no,
|
|
266
294
|
'TP': None, 'TN': None, 'FP': None, 'FN': None,
|
|
267
|
-
'precision (micro)': None, 'recall (micro)': None, 'F1 score (micro)': None, 'F2 score (micro)': None, 'accuracy
|
|
295
|
+
'precision (micro)': None, 'recall (micro)': None, 'F1 score (micro)': None, 'F2 score (micro)': None, 'accuracy': None, 'specificity': None,
|
|
268
296
|
'cor': None, 'inc': None, 'mis': None, 'spu': None, 'par': None,
|
|
269
297
|
'precision (macro)': None, 'recall (macro)': None, 'F1 score (macro)': None, 'F2 score (macro)': None
|
|
270
298
|
})
|
|
@@ -330,6 +358,7 @@ def get_metrics(res_df, fields):
|
|
|
330
358
|
mis = field_df['Mis: ' + field].sum()
|
|
331
359
|
spu = field_df['Spu: ' + field].sum()
|
|
332
360
|
par = field_df['Par: ' + field].sum() if 'Par: ' + field in field_df else None
|
|
361
|
+
TN = field_df['TN: ' + field].sum() if 'TN: ' + field in field_df else None
|
|
333
362
|
|
|
334
363
|
if 'Precision: ' + field in field_df:
|
|
335
364
|
# if Precision present it means that it is a list field
|
|
@@ -337,15 +366,8 @@ def get_metrics(res_df, fields):
|
|
|
337
366
|
recall_macro = field_df['Recall: ' + field].mean()
|
|
338
367
|
f1_score_macro = field_df['F1 score: ' + field].mean()
|
|
339
368
|
f2_score_macro = field_df['F2 score: ' + field].mean()
|
|
340
|
-
|
|
341
|
-
# TN is not defined for list values (e.g. label ['apple'] and prediction ['orange', 'banana'] will produce SPU=2)
|
|
342
|
-
TN_field = None
|
|
343
|
-
else:
|
|
344
|
-
# scalar field
|
|
345
|
-
TN_field = labeled_cases - field_present_cases - spu
|
|
346
|
-
|
|
347
369
|
precision_micro, recall_micro, f1_score_micro, f2_score_micro, specificity_micro = calculate_metrics(
|
|
348
|
-
cor, par or 0, inc or 0, spu, mis,
|
|
370
|
+
cor, par or 0, inc or 0, spu, mis, TN
|
|
349
371
|
)
|
|
350
372
|
|
|
351
373
|
# Append computed row as a dictionary
|
|
@@ -362,8 +384,8 @@ def get_metrics(res_df, fields):
|
|
|
362
384
|
'recall (micro)': recall_micro,
|
|
363
385
|
'F1 score (micro)': f1_score_micro,
|
|
364
386
|
'F2 score (micro)': f2_score_micro,
|
|
365
|
-
'accuracy
|
|
366
|
-
'specificity
|
|
387
|
+
'accuracy': accuracy_micro,
|
|
388
|
+
'specificity': specificity_micro,
|
|
367
389
|
'cor': cor,
|
|
368
390
|
'inc': inc,
|
|
369
391
|
'mis': mis,
|
|
@@ -613,7 +635,7 @@ def _reorder_result_columns(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
613
635
|
res_cols_all = [c for c in cols if c.startswith('Res: ')]
|
|
614
636
|
|
|
615
637
|
metric_prefixes_binary_counts = ["TP: ", "FP: ", "FN: ", "TN: "]
|
|
616
|
-
metric_prefixes_non_binary_counts = ["Cor: ", "Inc: ", "Mis: ", "Spu: ", "Par: "]
|
|
638
|
+
metric_prefixes_non_binary_counts = ["Cor: ", "Inc: ", "Mis: ", "Spu: ", "Par: ", "TN: "]
|
|
617
639
|
metric_prefixes = ["Precision: ", "Recall: ", "Accuracy: ", "F1 score: ", "F2 score: ", "Specificity: "]
|
|
618
640
|
|
|
619
641
|
metric_prefixes_all = metric_prefixes_binary_counts + metric_prefixes_non_binary_counts + metric_prefixes
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: llmvalidate
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Summary: Oncoshot LLM validation framework
|
|
5
5
|
License: MIT
|
|
6
6
|
Project-URL: Homepage, https://github.com/Oncoshot/llm-validation-framework
|
|
@@ -24,20 +24,26 @@ Dynamic: license-file
|
|
|
24
24
|
|
|
25
25
|
# LLM Validation Framework
|
|
26
26
|
|
|
27
|
-
[](https://github.com/Oncoshot/llm-validation-framework/actions)
|
|
28
28
|
[](https://pypi.org/project/llmvalidate/)
|
|
29
29
|
[](https://pypi.org/project/llmvalidate/)
|
|
30
|
-
[](https://github.com/Oncoshot/llm-validation-framework/blob/master/LICENSE)
|
|
31
|
+
[](https://github.com/Oncoshot/llm-validation-framework/stargazers)
|
|
32
32
|
|
|
33
33
|
A comprehensive Python framework for evaluating LLM-extracted structured data against ground truth labels. Supports binary classification, scalar values, and list fields with detailed performance metrics, confidence-based evaluation, and statistical uncertainty quantification via non-parametric bootstrap confidence intervals.
|
|
34
34
|
|
|
35
|
+
## 📄 Paper
|
|
36
|
+
|
|
37
|
+
The methodology behind this framework is described in a medRxiv preprint:
|
|
38
|
+
[medRxiv 2026.05.18.26353541](https://www.medrxiv.org/content/10.64898/2026.05.18.26353541v1) — DOI: [10.64898/2026.05.18.26353541](https://doi.org/10.64898/2026.05.18.26353541)
|
|
39
|
+
|
|
35
40
|
## ✨ Key Features
|
|
36
41
|
|
|
37
42
|
- **Multi-field validation** - Binary (True/False), scalar (single values), and list (multiple values) data types
|
|
43
|
+
- **Coded field support** - Score a value together with its code (ICD-10 / ICD-O-3, RxNorm) as two independent facets
|
|
38
44
|
- **Partial labeling support** - Handle datasets where different cases have labels for different subsets of fields
|
|
39
45
|
- **Dual usage modes** - Validate pre-computed results OR run live LLM inference with validation
|
|
40
|
-
- **Comprehensive metrics** - Precision, recall, F1/F2, accuracy, specificity with
|
|
46
|
+
- **Comprehensive metrics** - Precision, recall, F1/F2, accuracy, specificity, with micro and macro aggregation where applicable
|
|
41
47
|
- **Confidence analysis** - Automatic performance breakdown by confidence levels
|
|
42
48
|
- **Statistical uncertainty** - Non-parametric bootstrap confidence intervals for all performance metrics
|
|
43
49
|
- **Production ready** - Parallel processing, intelligent caching, detailed progress tracking
|
|
@@ -60,14 +66,14 @@ python runme.py
|
|
|
60
66
|
|
|
61
67
|
Processes the included [samples.csv](samples.csv) (14 test cases covering all validation scenarios) and outputs timestamped results to `validation_results/samples/`:
|
|
62
68
|
|
|
63
|
-
- **[Results CSV](validation_results/samples/2026-
|
|
64
|
-
- **[Metrics CSV](validation_results/samples/2026-
|
|
65
|
-
- **[CI Metrics CSV](validation_results/samples/2026-
|
|
69
|
+
- **[Results CSV](validation_results/samples/2026-07-13%2001-12-18%20results.csv)** - Row-by-row comparison with confusion matrix counts and item-level details
|
|
70
|
+
- **[Metrics CSV](validation_results/samples/2026-07-13%2001-12-18%20metrics.csv)** - Aggregated performance statistics with confidence breakdowns
|
|
71
|
+
- **[CI Metrics CSV](validation_results/samples/2026-07-13%2001-12-18%20CI%20metrics.csv)** - Confidence intervals for metrics
|
|
66
72
|
|
|
67
73
|
| Rows | Field Type | Test Scenarios |
|
|
68
74
|
|------|------------|----------------|
|
|
69
75
|
| **1-4** | Binary (`Has metastasis`) | True Positive, True Negative, False Positive, False Negative |
|
|
70
|
-
| **5-9** | Scalar (`Diagnosis`, `Histology`) | Correct, incorrect, missing, spurious, and empty extractions |
|
|
76
|
+
| **5-9** | Scalar (`Diagnosis`, `Histology`) | Correct, incorrect, missing, spurious, and correct-empty (TN) extractions |
|
|
71
77
|
| **10-14** | List (`Treatment Drugs`, `Test Results`) | Perfect match, spurious items, missing items, correct empty, mixed results |
|
|
72
78
|
|
|
73
79
|
## 📊 Usage Modes
|
|
@@ -77,7 +83,7 @@ When you have LLM predictions in `Res: {Field Name}` columns:
|
|
|
77
83
|
|
|
78
84
|
```python
|
|
79
85
|
import pandas as pd
|
|
80
|
-
from
|
|
86
|
+
from llmvalidate import validate
|
|
81
87
|
|
|
82
88
|
df = pd.read_csv("data.csv", index_col="Patient ID")
|
|
83
89
|
# df must contain: "Field Name" and "Res: Field Name" columns
|
|
@@ -93,8 +99,8 @@ results_df, metrics_df = validate(
|
|
|
93
99
|
### Mode 2: Live LLM Inference + Validation
|
|
94
100
|
|
|
95
101
|
```python
|
|
96
|
-
from
|
|
97
|
-
from
|
|
102
|
+
from llmvalidate.structured import StructuredResult, StructuredGroup, StructuredField
|
|
103
|
+
from llmvalidate.utils import flatten_structured_result
|
|
98
104
|
|
|
99
105
|
def llm_callback(row, i, raw_text_column_name):
|
|
100
106
|
raw_text = row[raw_text_column_name]
|
|
@@ -164,6 +170,14 @@ with the `-value` / `-code` label columns.
|
|
|
164
170
|
- **`null/empty/NaN`** = Field not labeled/evaluated (supports partial labeling where different cases may have labels for different field subsets)
|
|
165
171
|
- **Lists** - Can be Python lists `["a", "b"]` or stringified `"['a', 'b']"` (auto-converted)
|
|
166
172
|
|
|
173
|
+
The `"-"`-vs-empty distinction above applies to **label** columns only. In **prediction** (`Res:`)
|
|
174
|
+
columns there is no "not evaluated" state: `"-"`, `""`, `null`/`NaN`, whitespace-only strings and
|
|
175
|
+
`[]` are all treated identically as "nothing extracted" (scoring Mis against a labeled value, or
|
|
176
|
+
TN when the label is `"-"`). We still recommend emitting `"-"` uniformly, to keep predictions
|
|
177
|
+
symmetric with labels. **Exception — binary fields:** predictions must be an explicit
|
|
178
|
+
`True`/`False`; an empty prediction is "not True / not False", so it scores FN against a `True`
|
|
179
|
+
label and **FP** against a `False` label.
|
|
180
|
+
|
|
167
181
|
### Partial Labeling Support
|
|
168
182
|
The framework supports partial labeling scenarios where:
|
|
169
183
|
- Not every case needs labels for every field
|
|
@@ -174,7 +188,7 @@ The framework supports partial labeling scenarios where:
|
|
|
174
188
|
|
|
175
189
|
## 📈 Output Files
|
|
176
190
|
|
|
177
|
-
|
|
191
|
+
A `validate()` run generates two timestamped CSV files (a third, CI metrics, is added when you also run `bootstrap_CI` — see the demo above):
|
|
178
192
|
|
|
179
193
|
### 1. Results CSV (`YYYY-MM-DD HH-MM-SS results.csv`)
|
|
180
194
|
**Row-level analysis** with detailed per-case metrics:
|
|
@@ -188,7 +202,7 @@ The framework generates two timestamped CSV files for each validation run:
|
|
|
188
202
|
- `TP/FP/FN/TN: {Field}` - Confusion matrix counts (1 or 0 per row)
|
|
189
203
|
|
|
190
204
|
**Non-Binary Fields:**
|
|
191
|
-
- `Cor/
|
|
205
|
+
- `Cor/Mis/Spu: {Field}` - Item counts per row (Inc and TN are None for list fields)
|
|
192
206
|
- `Cor/Inc/Mis/Spu: {Field} items` - Actual item lists
|
|
193
207
|
- `Precision/Recall/F1/F2: {Field}` - Per-row metrics (list fields only)
|
|
194
208
|
|
|
@@ -208,9 +222,14 @@ The framework generates two timestamped CSV files for each validation run:
|
|
|
208
222
|
|
|
209
223
|
**Binary Metrics:** `TP`, `TN`, `FP`, `FN`, `precision`, `recall`, `F1/F2`, `accuracy`, `specificity`
|
|
210
224
|
|
|
211
|
-
**Non-Binary Metrics:** `cor`, `inc`, `mis`, `spu`, `precision/recall/F1/F2 (micro
|
|
225
|
+
**Non-Binary Metrics:** `cor`, `inc`, `mis`, `spu`, `TN`, `precision/recall/F1/F2 (micro and macro)`, `specificity`
|
|
226
|
+
|
|
227
|
+
Applicability: `cor`, `mis`, `spu` and the `(micro)` precision/recall/F-scores apply to all non-binary fields; `inc`, `TN` and `specificity` are meaningful for **scalar fields only**; the `(macro)` metrics are averages of the per-row metrics and exist for **list fields only**. `accuracy` and `specificity` carry no `(micro)`/`(macro)` tag: they are only ever computed one way (pooled counts), since a per-row version would be a 0/1 indicator whose average equals the pooled value.
|
|
212
228
|
|
|
213
229
|
## ⚡ Performance Metrics Explained
|
|
230
|
+
|
|
231
|
+

|
|
232
|
+
|
|
214
233
|
### Binary Classification Metrics
|
|
215
234
|
|
|
216
235
|
For fields with True/False values (e.g., "Has metastasis"):
|
|
@@ -241,6 +260,7 @@ For scalar and list fields (e.g., "Diagnosis", "Treatment Drugs"):
|
|
|
241
260
|
| **Missing (Mis)** | Items present in label but not extracted | (Same example) → Mis=1 (DrugB missing) |
|
|
242
261
|
| **Spurious (Spu)** | Items extracted but not in label | Label: `["DrugA"]`, Prediction: `["DrugA", "DrugC"]` → Spu=1 |
|
|
243
262
|
| **Incorrect (Inc)** | Wrong values for scalar fields | Label: `"Cancer"`, Prediction: `"Diabetes"` → Inc=1 |
|
|
263
|
+
| **True Negative (TN)** | Scalar fields only: field correctly left empty | Label: `"-"`, Prediction: `""`/`"-"` → TN=1 |
|
|
244
264
|
|
|
245
265
|
#### Structured Extraction Formulas
|
|
246
266
|
|
|
@@ -248,8 +268,9 @@ For scalar and list fields (e.g., "Diagnosis", "Treatment Drugs"):
|
|
|
248
268
|
|--------|---------|---------|
|
|
249
269
|
| **Precision** | `Cor / (Cor + Spu + Inc)` | Of all extracted items, how many were correct? |
|
|
250
270
|
| **Recall** | `Cor / (Cor + Mis + Inc)` | Of all labeled items, how many were correctly extracted? |
|
|
271
|
+
| **Specificity** | `TN / (TN + Spu)` | Scalar fields only: of all cases labeled as having no information, how many were correctly left empty? |
|
|
251
272
|
|
|
252
|
-
**Note:**
|
|
273
|
+
**Note:** Inc and TN (and therefore specificity) are defined only for scalar fields. For list fields, extracted items are always classified as correct, missing, or spurious — Inc stays empty and no TN column is emitted.
|
|
253
274
|
|
|
254
275
|
The following formulas apply to both binary classification and structured extraction metrics:
|
|
255
276
|
|
|
@@ -266,7 +287,7 @@ The framework includes statistical confidence interval estimation using non-para
|
|
|
266
287
|
|
|
267
288
|
### Usage
|
|
268
289
|
```python
|
|
269
|
-
from
|
|
290
|
+
from llmvalidate import bootstrap_CI
|
|
270
291
|
|
|
271
292
|
# After running validation to get results_df
|
|
272
293
|
ci_results = bootstrap_CI(
|
|
@@ -347,7 +368,7 @@ pip install -r requirements.txt
|
|
|
347
368
|
pytest
|
|
348
369
|
|
|
349
370
|
# Run with coverage reporting
|
|
350
|
-
pytest --cov=
|
|
371
|
+
pytest --cov=llmvalidate
|
|
351
372
|
|
|
352
373
|
# Run specific test modules
|
|
353
374
|
pytest tests/validate_test.py # Core validation logic
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|