Spaces:
Sleeping
Sleeping
debpc commited on
Commit ·
413f3fa
1
Parent(s): e9f7c71
fix(numeric): support additional confidence interval formats
Browse files- app.py +1 -1
- numeric_core.py +46 -4
- test_numeric_formats_v428.py +107 -0
app.py
CHANGED
|
@@ -13,7 +13,7 @@ logger = setup_logger("app")
|
|
| 13 |
|
| 14 |
app = FastAPI(
|
| 15 |
title="PhytoEvidence Medical Service",
|
| 16 |
-
version="4.2.
|
| 17 |
docs_url="/docs",
|
| 18 |
openapi_url="/openapi.json",
|
| 19 |
)
|
|
|
|
| 13 |
|
| 14 |
app = FastAPI(
|
| 15 |
title="PhytoEvidence Medical Service",
|
| 16 |
+
version="4.2.8",
|
| 17 |
docs_url="/docs",
|
| 18 |
openapi_url="/openapi.json",
|
| 19 |
)
|
numeric_core.py
CHANGED
|
@@ -22,6 +22,16 @@ _DAY_NORM = {
|
|
| 22 |
"month": 30, "months": 30, "mois": 30,
|
| 23 |
}
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
_NUMBER = (
|
| 26 |
r"-?(?:\d+(?:[.,]\d+)?|[.,]\d+)"
|
| 27 |
r"(?:[eE][+-]?\d+)?"
|
|
@@ -40,9 +50,10 @@ _PATTS = [
|
|
| 40 |
re.compile(
|
| 41 |
rf"(?:(?:95\s*%?\s*(?:CI|IC))|"
|
| 42 |
rf"(?:(?:CI|IC)\s*95\s*%?))"
|
| 43 |
-
rf"\s*[:=\[\(]?\s*
|
|
|
|
| 44 |
rf"\s*(?:[-\u2013]|\bto\b|\bà\b|,\s+)\s*"
|
| 45 |
-
rf"({_NUMBER})",
|
| 46 |
re.I,
|
| 47 |
),
|
| 48 |
),
|
|
@@ -89,6 +100,17 @@ _PATTS = [
|
|
| 89 |
re.I,
|
| 90 |
),
|
| 91 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
(
|
| 93 |
"duration",
|
| 94 |
re.compile(
|
|
@@ -100,8 +122,11 @@ _PATTS = [
|
|
| 100 |
(
|
| 101 |
"ratio",
|
| 102 |
re.compile(
|
| 103 |
-
rf"\b(?:OR|RR|HR|SMD|MD
|
| 104 |
-
rf"\s
|
|
|
|
|
|
|
|
|
|
| 105 |
re.I,
|
| 106 |
),
|
| 107 |
),
|
|
@@ -294,6 +319,23 @@ def extract(text: str) -> list[Num]:
|
|
| 294 |
None,
|
| 295 |
)
|
| 296 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
elif kind == "duration":
|
| 298 |
number = Num(
|
| 299 |
"duration",
|
|
|
|
| 22 |
"month": 30, "months": 30, "mois": 30,
|
| 23 |
}
|
| 24 |
|
| 25 |
+
_WORD_NUMBER_NORM = {
|
| 26 |
+
"one": 1, "two": 2, "three": 3, "four": 4,
|
| 27 |
+
"five": 5, "six": 6, "seven": 7, "eight": 8,
|
| 28 |
+
"nine": 9, "ten": 10, "eleven": 11, "twelve": 12,
|
| 29 |
+
"un": 1, "une": 1, "deux": 2, "trois": 3,
|
| 30 |
+
"quatre": 4, "cinq": 5, "six": 6, "sept": 7,
|
| 31 |
+
"huit": 8, "neuf": 9, "dix": 10, "onze": 11,
|
| 32 |
+
"douze": 12,
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
_NUMBER = (
|
| 36 |
r"-?(?:\d+(?:[.,]\d+)?|[.,]\d+)"
|
| 37 |
r"(?:[eE][+-]?\d+)?"
|
|
|
|
| 50 |
re.compile(
|
| 51 |
rf"(?:(?:95\s*%?\s*(?:CI|IC))|"
|
| 52 |
rf"(?:(?:CI|IC)\s*95\s*%?))"
|
| 53 |
+
rf"\s*[,;:=]?\s*[\[\(]?\s*"
|
| 54 |
+
rf"({_NUMBER})\s*%?"
|
| 55 |
rf"\s*(?:[-\u2013]|\bto\b|\bà\b|,\s+)\s*"
|
| 56 |
+
rf"({_NUMBER})\s*%?",
|
| 57 |
re.I,
|
| 58 |
),
|
| 59 |
),
|
|
|
|
| 100 |
re.I,
|
| 101 |
),
|
| 102 |
),
|
| 103 |
+
(
|
| 104 |
+
"duration_word",
|
| 105 |
+
re.compile(
|
| 106 |
+
r"\b(one|two|three|four|five|six|seven|eight|nine|"
|
| 107 |
+
r"ten|eleven|twelve|un|une|deux|trois|quatre|cinq|"
|
| 108 |
+
r"six|sept|huit|neuf|dix|onze|douze)"
|
| 109 |
+
r"(?:\s+|-)\s*"
|
| 110 |
+
r"(weeks?|days?|months?|semaines?|jours?|mois)\b",
|
| 111 |
+
re.I,
|
| 112 |
+
),
|
| 113 |
+
),
|
| 114 |
(
|
| 115 |
"duration",
|
| 116 |
re.compile(
|
|
|
|
| 122 |
(
|
| 123 |
"ratio",
|
| 124 |
re.compile(
|
| 125 |
+
rf"\b(?:OR|RR|HR|SMD|MD|"
|
| 126 |
+
rf"hazard\s+ratio|odds\s+ratio|"
|
| 127 |
+
rf"risk\s+ratio|relative\s+risk)\b"
|
| 128 |
+
rf"\s*(?:[,;:=]|\bis\b|\bwas\b|\bof\b)?"
|
| 129 |
+
rf"\s*({_NUMBER})",
|
| 130 |
re.I,
|
| 131 |
),
|
| 132 |
),
|
|
|
|
| 319 |
None,
|
| 320 |
)
|
| 321 |
|
| 322 |
+
elif kind == "duration_word":
|
| 323 |
+
number = Num(
|
| 324 |
+
"duration",
|
| 325 |
+
"=",
|
| 326 |
+
float(
|
| 327 |
+
_WORD_NUMBER_NORM[
|
| 328 |
+
groups[0].lower()
|
| 329 |
+
]
|
| 330 |
+
)
|
| 331 |
+
* _DAY_NORM.get(
|
| 332 |
+
groups[1].lower(),
|
| 333 |
+
1.0,
|
| 334 |
+
),
|
| 335 |
+
None,
|
| 336 |
+
"days",
|
| 337 |
+
)
|
| 338 |
+
|
| 339 |
elif kind == "duration":
|
| 340 |
number = Num(
|
| 341 |
"duration",
|
test_numeric_formats_v428.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from numeric_core import extract, validate_numeric
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def compact(values):
|
| 5 |
+
return [
|
| 6 |
+
(item.kind, item.lo, item.hi)
|
| 7 |
+
for item in values
|
| 8 |
+
]
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def test_ci_with_comma_after_label():
|
| 12 |
+
assert compact(extract("95% CI, 0.51-0.90")) == [
|
| 13 |
+
("ci", 0.51, 0.90),
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def test_ci_with_percent_on_both_bounds():
|
| 18 |
+
assert compact(
|
| 19 |
+
extract(
|
| 20 |
+
"Difference -6.27% "
|
| 21 |
+
"[95% CI: -33.90%-21.3%]."
|
| 22 |
+
)
|
| 23 |
+
) == [
|
| 24 |
+
("percent", -6.27, None),
|
| 25 |
+
("ci", -33.90, 21.3),
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def test_ci_percent_negative_upper_bound_uses_double_minus():
|
| 30 |
+
assert compact(
|
| 31 |
+
extract("95% CI: -33.90%--21.3%")
|
| 32 |
+
) == [
|
| 33 |
+
("ci", -33.90, -21.3),
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def test_hazard_ratio_long_form():
|
| 38 |
+
assert compact(
|
| 39 |
+
extract(
|
| 40 |
+
"hazard ratio, 0.68; "
|
| 41 |
+
"95% CI, 0.51-0.90"
|
| 42 |
+
)
|
| 43 |
+
) == [
|
| 44 |
+
("ratio", 0.68, None),
|
| 45 |
+
("ci", 0.51, 0.90),
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def test_three_months_written_in_words():
|
| 50 |
+
assert compact(
|
| 51 |
+
extract(
|
| 52 |
+
"The treatment continued for three months."
|
| 53 |
+
)
|
| 54 |
+
) == [
|
| 55 |
+
("duration", 90.0, None),
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def test_hyphenated_three_month_duration():
|
| 60 |
+
assert compact(
|
| 61 |
+
extract("This was a three-month trial.")
|
| 62 |
+
) == [
|
| 63 |
+
("duration", 90.0, None),
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def test_french_duration_written_in_words():
|
| 68 |
+
assert compact(
|
| 69 |
+
extract("Le traitement a duré trois mois.")
|
| 70 |
+
) == [
|
| 71 |
+
("duration", 90.0, None),
|
| 72 |
+
]
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def test_real_percent_ci_matches_exact_claim():
|
| 76 |
+
result = validate_numeric(
|
| 77 |
+
"Group difference -6.27% with "
|
| 78 |
+
"95% CI -33.90 to 21.3.",
|
| 79 |
+
"Responder rates differed by -6.27% "
|
| 80 |
+
"[95% CI: -33.90%-21.3%].",
|
| 81 |
+
rel_tol=0.0,
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
assert result["numeric_ok"] is True
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def test_altered_percent_ci_bound_is_rejected():
|
| 88 |
+
result = validate_numeric(
|
| 89 |
+
"Group difference -6.27% with "
|
| 90 |
+
"95% CI -33.90 to 21.4.",
|
| 91 |
+
"Responder rates differed by -6.27% "
|
| 92 |
+
"[95% CI: -33.90%-21.3%].",
|
| 93 |
+
rel_tol=0.0,
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
assert result["numeric_ok"] is False
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def test_real_hazard_ratio_matches_abbreviated_claim():
|
| 100 |
+
result = validate_numeric(
|
| 101 |
+
"HR 0.68 with 95% CI 0.51 to 0.90.",
|
| 102 |
+
"hazard ratio, 0.68; "
|
| 103 |
+
"95% CI, 0.51-0.90; P = 0.0064.",
|
| 104 |
+
rel_tol=0.0,
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
assert result["numeric_ok"] is True
|