|
a |
|
b/tests/regression/test_regression.py |
|
|
1 |
import json |
|
|
2 |
from typing import Optional |
|
|
3 |
|
|
|
4 |
from docdeid import Annotation, AnnotationSet |
|
|
5 |
|
|
|
6 |
from deduce import Deduce |
|
|
7 |
|
|
|
8 |
|
|
|
9 |
def regression_test( |
|
|
10 |
model: Deduce, |
|
|
11 |
examples_file: str, |
|
|
12 |
enabled: set[str], |
|
|
13 |
known_failures: Optional[set[int]] = None, |
|
|
14 |
): |
|
|
15 |
if known_failures is None: |
|
|
16 |
known_failures = set() |
|
|
17 |
|
|
|
18 |
with open(examples_file, "rb") as file: |
|
|
19 |
examples = json.load(file)["examples"] |
|
|
20 |
|
|
|
21 |
failures = set() |
|
|
22 |
|
|
|
23 |
for example in examples: |
|
|
24 |
trues = AnnotationSet( |
|
|
25 |
Annotation(**annotation) for annotation in example["annotations"] |
|
|
26 |
) |
|
|
27 |
preds = model.deidentify(text=example["text"], enabled=enabled).annotations |
|
|
28 |
|
|
|
29 |
try: |
|
|
30 |
assert trues == preds |
|
|
31 |
except AssertionError: |
|
|
32 |
failures.add(example["id"]) |
|
|
33 |
|
|
|
34 |
assert failures == known_failures |
|
|
35 |
|
|
|
36 |
|
|
|
37 |
def annotators_from_group(model: Deduce, group: str) -> set[str]: |
|
|
38 |
return {name for name, _ in model.processors[group]}.union({group}) |
|
|
39 |
|
|
|
40 |
|
|
|
41 |
class TestRegression: |
|
|
42 |
def test_regression_name(self, model): |
|
|
43 |
regression_test( |
|
|
44 |
model=model, |
|
|
45 |
examples_file="tests/data/regression_cases/names.json", |
|
|
46 |
enabled=annotators_from_group(model, "names"), |
|
|
47 |
) |
|
|
48 |
|
|
|
49 |
def test_regression_location(self, model): |
|
|
50 |
regression_test( |
|
|
51 |
model=model, |
|
|
52 |
examples_file="tests/data/regression_cases/locations.json", |
|
|
53 |
enabled=annotators_from_group(model, "locations"), |
|
|
54 |
) |
|
|
55 |
|
|
|
56 |
def test_regression_institution(self, model): |
|
|
57 |
regression_test( |
|
|
58 |
model=model, |
|
|
59 |
examples_file="tests/data/regression_cases/institutions.json", |
|
|
60 |
enabled=annotators_from_group(model, "institutions"), |
|
|
61 |
) |
|
|
62 |
|
|
|
63 |
def test_regression_date(self, model): |
|
|
64 |
regression_test( |
|
|
65 |
model=model, |
|
|
66 |
examples_file="tests/data/regression_cases/dates.json", |
|
|
67 |
enabled=annotators_from_group(model, "dates"), |
|
|
68 |
) |
|
|
69 |
|
|
|
70 |
def test_regression_age(self, model): |
|
|
71 |
regression_test( |
|
|
72 |
model=model, |
|
|
73 |
examples_file="tests/data/regression_cases/ages.json", |
|
|
74 |
enabled=annotators_from_group(model, "ages"), |
|
|
75 |
) |
|
|
76 |
|
|
|
77 |
def test_regression_identifier(self, model): |
|
|
78 |
regression_test( |
|
|
79 |
model=model, |
|
|
80 |
examples_file="tests/data/regression_cases/identifiers.json", |
|
|
81 |
enabled=annotators_from_group(model, "identifiers"), |
|
|
82 |
) |
|
|
83 |
|
|
|
84 |
def test_regression_phone(self, model): |
|
|
85 |
regression_test( |
|
|
86 |
model=model, |
|
|
87 |
examples_file="tests/data/regression_cases/phone_numbers.json", |
|
|
88 |
enabled=annotators_from_group(model, "phone_numbers"), |
|
|
89 |
) |
|
|
90 |
|
|
|
91 |
def test_regression_email(self, model): |
|
|
92 |
regression_test( |
|
|
93 |
model=model, |
|
|
94 |
examples_file="tests/data/regression_cases/emails.json", |
|
|
95 |
enabled=annotators_from_group(model, "email_addresses"), |
|
|
96 |
) |
|
|
97 |
|
|
|
98 |
def test_regression_url(self, model): |
|
|
99 |
regression_test( |
|
|
100 |
model=model, |
|
|
101 |
examples_file="tests/data/regression_cases/urls.json", |
|
|
102 |
enabled=annotators_from_group(model, "urls"), |
|
|
103 |
) |