-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevaluate-predictions.py
172 lines (141 loc) · 4.8 KB
/
evaluate-predictions.py
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
import argparse
import dataclasses
import logging
from pathlib import Path
from typing import List, Dict, Set
from mdace.cleanup import trim_annotations, merge_adjacent_annotations
from mdace.data import MDACEData, Admission
from mdace.metrics import AllErrorRates
from mdace.text import tokenize
logger = logging.getLogger(Path(__file__).name)
def filter_admissions_by_id(dataset: MDACEData, hadm_ids: Set[int]) -> MDACEData:
return MDACEData(
admissions=[adm for adm in dataset.admissions if adm.hadm_id in hadm_ids]
)
def filter_notes_by_category(
dataset: MDACEData, note_categories: Set[str]
) -> MDACEData:
return MDACEData(
admissions=[
dataclasses.replace(
adm,
notes=[note for note in adm.notes if note.category in note_categories],
)
for adm in dataset.admissions
]
)
def load_hadm_ids(split_file: Path) -> Set[int]:
hadm_ids = set()
with open(split_file, "r") as ifp:
for idx, line in enumerate(ifp):
try:
hadm_ids.add(int(line.strip()))
except ValueError as ve:
if idx > 0:
# header
raise ve
return hadm_ids
def load_grouped_predictions(
dataset_dir: Path,
hadm_ids: Set[int],
target_categories: List[str],
merge_adjacent: bool,
trim_annos: bool,
) -> Dict[int, Admission]:
"""Load dataset, do optional preprocessing/filtering and group evidence annotations by note_id"""
dataset = MDACEData.from_dir(dataset_dir, require_text=True)
dataset = filter_admissions_by_id(dataset, hadm_ids)
if target_categories:
dataset = filter_notes_by_category(dataset, set(target_categories))
if merge_adjacent:
dataset = merge_adjacent_annotations(dataset)
if trim_annos:
dataset = trim_annotations(dataset)
return {adm.hadm_id: adm for adm in dataset.admissions}
def main(args: argparse.Namespace):
hadm_ids = load_hadm_ids(args.split_file)
target_categories = args.note_category
def _load_grouped_predictions(data_dir: Path):
return load_grouped_predictions(
dataset_dir=data_dir,
hadm_ids=hadm_ids,
target_categories=target_categories,
merge_adjacent=args.merge_adjacent,
trim_annos=args.trim_annotations,
)
gold = _load_grouped_predictions(args.gold_dir)
predictions = _load_grouped_predictions(args.predictions_dir)
error_rates = AllErrorRates(tokenize_fn=tokenize)
for hadm_id, actual_evidence in gold.items():
predicted_evidence = predictions.get(hadm_id, [])
if not predicted_evidence and actual_evidence:
logger.debug(
f"No evidence predicted for admission={hadm_id} [{len(actual_evidence):,} actual]"
)
error_rates.observe(actual_evidence, predicted_evidence)
logger.info(error_rates)
md_out = args.md_out # type: Path
if md_out:
md_out.parent.mkdir(parents=True, exist_ok=True)
with open(md_out, "w", encoding="utf8") as ofp:
print(str(error_rates), file=ofp)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--gold-dir",
help="Path to directory containing annotation JSON files",
type=Path,
required=True,
)
parser.add_argument(
"--predictions-dir",
help="Path to directory containing annotation JSON files",
type=Path,
required=True,
)
parser.add_argument(
"--split-file",
help="Path to split CSV file",
type=Path,
required=True,
)
parser.add_argument(
"--note-category",
help="Only consider notes with this category; default considers all",
type=str,
nargs="*",
action="extend",
choices=[
"Case Management",
"Consult",
"Discharge summary",
"ECG",
"General",
"Nursing",
"Nutrition",
"Physician",
"Radiology",
"Rehab Services",
"Respiratory",
"RespiratoryGeneral",
],
)
parser.add_argument(
"--md-out",
help="Write results markdown to this file",
type=Path,
required=False,
)
cleanup = parser.add_argument_group("Clean Up Options")
cleanup.add_argument(
"--merge-adjacent",
action="store_true",
help="Merge annotations that are separated only by non-breaking characters",
)
cleanup.add_argument(
"--trim-annotations",
action="store_true",
help="Trim non-breaking characters from annotations (e.g. punct/whitespace)",
)
logging.basicConfig(level=logging.INFO)
main(parser.parse_args())