Skip to content

Commit 4a7168c

Browse files
committed
change model.py to be compatible with old and new regex
1 parent 67c8d14 commit 4a7168c

File tree

3 files changed

+63
-36
lines changed

3 files changed

+63
-36
lines changed

edsnlp/pipes/ner/tnm/model.py

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,21 @@ class Metastasis(TnmEnum):
7979

8080

8181
class TNM(pydantic.BaseModel):
82-
prefix: Optional[Prefix] = None
83-
tumour: Optional[Tumour] = None
84-
tumour_specification: Optional[Specification] = None
82+
tumour_prefix: Optional[str] = None
83+
tumour: Optional[str] = None
84+
tumour_specification: Optional[str] = None
8585
tumour_suffix: Optional[str] = None
86-
node: Optional[Node] = None
87-
node_specification: Optional[Specification] = None
86+
node_prefix: Optional[str] = None
87+
node: Optional[str] = None
88+
node_specification: Optional[str] = None
8889
node_suffix: Optional[str] = None
89-
metastasis: Optional[Metastasis] = None
90-
resection_completeness: Optional[int] = None
90+
metastasis_prefix: Optional[str] = None
91+
metastasis: Optional[str] = None
92+
metastasis_specification: Optional[str] = None
93+
pleura: Optional[str] = None
94+
resection: Optional[str] = None
95+
resection_specification: Optional[str] = None
96+
resection_loc: Optional[str] = None
9197
version: Optional[str] = None
9298
version_year: Optional[int] = None
9399

@@ -112,32 +118,43 @@ def validate_year(cls, v):
112118
def norm(self) -> str:
113119
norm = []
114120

115-
if self.prefix is not None:
116-
norm.append(str(self.prefix))
121+
if self.tumour_prefix:
122+
norm.append(f"{self.tumour_prefix or ''}")
117123

118-
if (
119-
(self.tumour is not None)
120-
| (self.tumour_specification is not None)
121-
| (self.tumour_suffix is not None)
122-
):
123-
norm.append(f"T{str(self.tumour or '')}")
124-
norm.append(f"{str(self.tumour_specification or '')}")
125-
norm.append(f"{str(self.tumour_suffix or '')}")
126-
127-
if (
128-
(self.node is not None)
129-
| (self.node_specification is not None)
130-
| (self.node_suffix is not None)
131-
):
132-
norm.append(f"N{str(self.node or '')}")
133-
norm.append(f"{str(self.node_specification or '')}")
134-
norm.append(f"{str(self.node_suffix or '')}")
124+
if self.tumour:
125+
norm.append(f"T{self.tumour}")
126+
if self.tumour_specification:
127+
norm.append(f"{self.tumour_specification or ''}")
128+
if self.tumour_suffix:
129+
norm.append(f"{self.tumour_suffix or ''}")
130+
131+
if self.node_prefix:
132+
norm.append(f"{self.node_prefix or ''}")
135133

136-
if self.metastasis is not None:
134+
if self.node:
135+
norm.append(f"N{self.node}")
136+
if self.node_specification:
137+
norm.append(f"{self.node_specification or ''}")
138+
if self.node_suffix:
139+
norm.append(f"{self.node_suffix or ''}")
140+
141+
if self.metastasis_prefix:
142+
norm.append(f"{self.metastasis_prefix or ''}")
143+
144+
if self.metastasis:
137145
norm.append(f"M{self.metastasis}")
146+
if self.metastasis_specification:
147+
norm.append(f"{self.metastasis_specification or ''}")
148+
149+
if self.pleura:
150+
norm.append(f"PL{self.pleura}")
138151

139-
if self.resection_completeness is not None:
140-
norm.append(f"R{self.resection_completeness}")
152+
if self.resection:
153+
norm.append(f"R{self.resection}")
154+
if self.resection_specification:
155+
norm.append(f"{self.resection_specification or ''}")
156+
if self.resection_loc:
157+
norm.append(f"{self.resection_loc or ''}")
141158

142159
if self.version is not None and self.version_year is not None:
143160
norm.append(f" ({self.version.upper()} {self.version_year})")
@@ -182,14 +199,21 @@ def dict(
182199
set_keys = set(d.keys())
183200
for k in set_keys.intersection(
184201
{
185-
"prefix",
202+
"tumour_prefix",
186203
"tumour",
187-
"node",
188-
"metastasis",
189204
"tumour_specification",
190-
"node_specification",
191205
"tumour_suffix",
206+
"node_prefix",
207+
"node",
208+
"node_specification",
192209
"node_suffix",
210+
"metastasis_prefix",
211+
"metastasis",
212+
"metastasis_specification",
213+
"pleura",
214+
"resection",
215+
"resection_specification",
216+
"resection_loc",
193217
}
194218
):
195219
v = d[k]

edsnlp/pipes/ner/tnm/patterns.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
prefix_pattern = r"(?P<tumour_prefix>[cpPyraum]p?)"
2-
tumour_pattern = r"T\s?(?P<tumour>([0-4o]|is))?(?P<tumour_specification>[abcdx]|mi)?"
2+
tumour_pattern = (
3+
r"T\s?(?P<tumour>([0-4o]|is|[Xx]))?(?P<tumour_specification>[abcdx]|mi)?"
4+
)
35
tumour_pattern += r"(?:\((?P<tumour_suffix>[^()]{1,10})\))?"
46
node_pattern = r"(\s{,2}\/?\s{,2}([cpPyraum]p?)?\s{,2}N\s?(?P<node>[0-3o]|x)"
57
node_pattern += (
68
r"(?P<node_specification>[abcdx]|mi)?(?:\((?P<node_suffix>[^()]{1,10})\))?)"
79
)
810

911
metastasis_pattern = (
10-
r"(\s{,2}\/?\s{,2}([cpPyraum]p?)?\s{,2}M\s?(?P<metastasis>([01o]|x))x?)" # noqa: E501
12+
r"(\s{,2}\/?\s{,2}([cpPyraum]p?)?\s{,2}M\s?(?P<metastasis>([01o]|x))x?)"
1113
)
1214
resection_pattern = r"(\s{,2}\/?\s{,2}R\s?(?P<resection>[012]))"
1315

tests/pipelines/ner/test_tnm.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from edsnlp.pipes.ner.tnm.patterns import tnm_pattern
12
from edsnlp.utils.examples import parse_example
23

34
examples = [
@@ -22,7 +23,7 @@
2223

2324

2425
def test_scores(blank_nlp):
25-
blank_nlp.add_pipe("eds.tnm")
26+
blank_nlp.add_pipe("eds.tnm", config=dict(pattern=tnm_pattern))
2627

2728
for example in examples:
2829
text, entities = parse_example(example=example)

0 commit comments

Comments
 (0)