Update engine/bacteria_identifier.py
Browse files- engine/bacteria_identifier.py +381 -383
engine/bacteria_identifier.py
CHANGED
|
@@ -1,383 +1,381 @@
|
|
| 1 |
-
# engine/bacteria_identifier.py
|
| 2 |
-
# ------------------------------------------------------------
|
| 3 |
-
# Core identification engine
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
from
|
| 10 |
-
|
| 11 |
-
import
|
| 12 |
-
|
| 13 |
-
from
|
| 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 |
-
self.total_fields_evaluated =
|
| 58 |
-
|
| 59 |
-
self.
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
return
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
)
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
return max(
|
| 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 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
return
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
if
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
if
|
| 239 |
-
return
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
return
|
| 284 |
-
|
| 285 |
-
# --------------------
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
# Return top 10
|
| 383 |
-
return results[:10]
|
|
|
|
| 1 |
+
# engine/bacteria_identifier.py
|
| 2 |
+
# ------------------------------------------------------------
|
| 3 |
+
# Core BactAI-D identification engine.
|
| 4 |
+
# - Scores genera from Excel DB (core phenotype fields)
|
| 5 |
+
# - Integrates optional extended-test reasoning
|
| 6 |
+
# - Provides blended confidence and narrative reasoning
|
| 7 |
+
# ------------------------------------------------------------
|
| 8 |
+
|
| 9 |
+
from __future__ import annotations
|
| 10 |
+
|
| 11 |
+
import re
|
| 12 |
+
from dataclasses import dataclass, field
|
| 13 |
+
from typing import Dict, List, Any, Optional, Tuple
|
| 14 |
+
|
| 15 |
+
import pandas as pd
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
from engine.extended_reasoner import score_genera_from_extended
|
| 19 |
+
HAS_EXTENDED_REASONER = True
|
| 20 |
+
except Exception:
|
| 21 |
+
HAS_EXTENDED_REASONER = False
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# ------------------------------------------------------------
|
| 25 |
+
# Helper
|
| 26 |
+
# ------------------------------------------------------------
|
| 27 |
+
|
| 28 |
+
def join_with_and(items: List[str]) -> str:
|
| 29 |
+
if not items:
|
| 30 |
+
return ""
|
| 31 |
+
if len(items) == 1:
|
| 32 |
+
return items[0]
|
| 33 |
+
return ", ".join(items[:-1]) + " and " + items[-1]
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# ------------------------------------------------------------
|
| 37 |
+
# Identification Result
|
| 38 |
+
# ------------------------------------------------------------
|
| 39 |
+
|
| 40 |
+
@dataclass
|
| 41 |
+
class IdentificationResult:
|
| 42 |
+
genus: str
|
| 43 |
+
total_score: int
|
| 44 |
+
matched_fields: List[str] = field(default_factory=list)
|
| 45 |
+
mismatched_fields: List[str] = field(default_factory=list)
|
| 46 |
+
reasoning_factors: Dict[str, Any] = field(default_factory=dict)
|
| 47 |
+
total_fields_evaluated: int = 0
|
| 48 |
+
total_fields_possible: int = 0
|
| 49 |
+
extra_notes: str = ""
|
| 50 |
+
extended_score: float = 0.0 # 0.0–1.0
|
| 51 |
+
extended_explanation: str = ""
|
| 52 |
+
|
| 53 |
+
# ---------- Confidence metrics ----------
|
| 54 |
+
|
| 55 |
+
def confidence_percent(self) -> int:
|
| 56 |
+
"""Confidence based only on tests the user entered."""
|
| 57 |
+
if self.total_fields_evaluated <= 0:
|
| 58 |
+
return 0
|
| 59 |
+
pct = (self.total_score / max(1, self.total_fields_evaluated)) * 100
|
| 60 |
+
return max(0, min(100, int(round(pct))))
|
| 61 |
+
|
| 62 |
+
def true_confidence(self) -> int:
|
| 63 |
+
"""Confidence based on all possible fields in the DB."""
|
| 64 |
+
if self.total_fields_possible <= 0:
|
| 65 |
+
return 0
|
| 66 |
+
pct = (self.total_score / max(1, self.total_fields_possible)) * 100
|
| 67 |
+
return max(0, min(100, int(round(pct))))
|
| 68 |
+
|
| 69 |
+
def blended_confidence_percent(self) -> int:
|
| 70 |
+
"""
|
| 71 |
+
Blend core confidence with extended_score (0–1).
|
| 72 |
+
If no extended signal, return core confidence.
|
| 73 |
+
Simple blend: 70% core, 30% extended signal.
|
| 74 |
+
"""
|
| 75 |
+
core = self.confidence_percent()
|
| 76 |
+
if self.extended_score <= 0:
|
| 77 |
+
return core
|
| 78 |
+
|
| 79 |
+
ext_pct = max(0.0, min(1.0, float(self.extended_score))) * 100.0
|
| 80 |
+
blended = 0.7 * core + 0.3 * ext_pct
|
| 81 |
+
return max(0, min(100, int(round(blended))))
|
| 82 |
+
|
| 83 |
+
# ---------- Reasoning text ----------
|
| 84 |
+
|
| 85 |
+
def reasoning_paragraph(self, ranked_results: Optional[List["IdentificationResult"]] = None) -> str:
|
| 86 |
+
"""Generate a narrative explanation from core matches."""
|
| 87 |
+
if not self.matched_fields and not self.reasoning_factors:
|
| 88 |
+
return "No significant biochemical or morphological matches were found."
|
| 89 |
+
|
| 90 |
+
intro_options = [
|
| 91 |
+
"Based on the observed biochemical and morphological traits,",
|
| 92 |
+
"According to the provided test results,",
|
| 93 |
+
"From the available laboratory findings,",
|
| 94 |
+
"Considering the entered reactions and colony characteristics,",
|
| 95 |
+
]
|
| 96 |
+
|
| 97 |
+
import random
|
| 98 |
+
intro = random.choice(intro_options)
|
| 99 |
+
|
| 100 |
+
highlights = []
|
| 101 |
+
|
| 102 |
+
gram = self.reasoning_factors.get("Gram Stain")
|
| 103 |
+
if gram:
|
| 104 |
+
highlights.append(f"it is **Gram {str(gram).lower()}**")
|
| 105 |
+
|
| 106 |
+
shape = self.reasoning_factors.get("Shape")
|
| 107 |
+
if shape:
|
| 108 |
+
highlights.append(f"with a **{str(shape).lower()}** morphology")
|
| 109 |
+
|
| 110 |
+
catalase = self.reasoning_factors.get("Catalase")
|
| 111 |
+
if catalase:
|
| 112 |
+
highlights.append(f"and **catalase {str(catalase).lower()}** activity")
|
| 113 |
+
|
| 114 |
+
oxidase = self.reasoning_factors.get("Oxidase")
|
| 115 |
+
if oxidase:
|
| 116 |
+
highlights.append(f"and **oxidase {str(oxidase).lower()}** reaction")
|
| 117 |
+
|
| 118 |
+
oxy = self.reasoning_factors.get("Oxygen Requirement")
|
| 119 |
+
if oxy:
|
| 120 |
+
highlights.append(f"which prefers **{str(oxy).lower()}** conditions")
|
| 121 |
+
|
| 122 |
+
if len(highlights) > 1:
|
| 123 |
+
summary = ", ".join(highlights[:-1]) + " and " + highlights[-1]
|
| 124 |
+
else:
|
| 125 |
+
summary = "".join(highlights)
|
| 126 |
+
|
| 127 |
+
# Confidence text (core)
|
| 128 |
+
core_conf = self.confidence_percent()
|
| 129 |
+
if core_conf >= 70:
|
| 130 |
+
confidence_text = "The confidence in this identification is high."
|
| 131 |
+
elif core_conf >= 40:
|
| 132 |
+
confidence_text = "The confidence in this identification is moderate."
|
| 133 |
+
else:
|
| 134 |
+
confidence_text = "The confidence in this identification is low."
|
| 135 |
+
|
| 136 |
+
# Comparison vs other top results
|
| 137 |
+
comparison = ""
|
| 138 |
+
if ranked_results and len(ranked_results) > 1:
|
| 139 |
+
close_others = ranked_results[1:3]
|
| 140 |
+
other_names = [r.genus for r in close_others]
|
| 141 |
+
if other_names:
|
| 142 |
+
if self.total_score >= close_others[0].total_score:
|
| 143 |
+
comparison = (
|
| 144 |
+
f" It is **more likely** than {join_with_and(other_names)} "
|
| 145 |
+
f"based on stronger alignment in {join_with_and(self.matched_fields[:3])}."
|
| 146 |
+
)
|
| 147 |
+
else:
|
| 148 |
+
comparison = (
|
| 149 |
+
f" It is **less likely** than {join_with_and(other_names)} "
|
| 150 |
+
f"due to differences in {join_with_and(self.mismatched_fields[:3])}."
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
return f"{intro} {summary}, the isolate most closely resembles **{self.genus}**. {confidence_text}{comparison}"
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
# ------------------------------------------------------------
|
| 157 |
+
# Bacteria Identifier
|
| 158 |
+
# ------------------------------------------------------------
|
| 159 |
+
|
| 160 |
+
class BacteriaIdentifier:
|
| 161 |
+
"""
|
| 162 |
+
Main engine to match bacterial genus based on biochemical & morphological data.
|
| 163 |
+
"""
|
| 164 |
+
|
| 165 |
+
def __init__(self, db: pd.DataFrame):
|
| 166 |
+
self.db: pd.DataFrame = db.fillna("")
|
| 167 |
+
self.db_columns = list(self.db.columns)
|
| 168 |
+
|
| 169 |
+
# ---------- Field comparison ----------
|
| 170 |
+
|
| 171 |
+
def compare_field(self, db_val: Any, user_val: Any, field_name: str) -> int:
|
| 172 |
+
"""
|
| 173 |
+
Compare one test field between database and user input.
|
| 174 |
+
Returns:
|
| 175 |
+
+1 match
|
| 176 |
+
-1 mismatch
|
| 177 |
+
0 unknown / ignored
|
| 178 |
+
Return -999 to indicate a hard exclusion (stop comparing this genus).
|
| 179 |
+
"""
|
| 180 |
+
if user_val is None:
|
| 181 |
+
return 0
|
| 182 |
+
|
| 183 |
+
user_str = str(user_val).strip()
|
| 184 |
+
if user_str == "" or user_str.lower() == "unknown":
|
| 185 |
+
return 0 # ignore unknown/empty
|
| 186 |
+
|
| 187 |
+
db_str = str(db_val).strip()
|
| 188 |
+
db_l = db_str.lower()
|
| 189 |
+
user_l = user_str.lower()
|
| 190 |
+
|
| 191 |
+
hard_exclusions = {"Gram Stain", "Shape", "Spore Formation"}
|
| 192 |
+
|
| 193 |
+
# Split multi-value fields on ; or / or ,
|
| 194 |
+
db_options = [p.strip().lower() for p in re.split(r"[;/,]", db_str) if p.strip()]
|
| 195 |
+
user_options = [p.strip().lower() for p in re.split(r"[;/,]", user_str) if p.strip()]
|
| 196 |
+
|
| 197 |
+
# "variable" logic: if either is variable, don't penalize
|
| 198 |
+
if "variable" in db_options or "variable" in user_options:
|
| 199 |
+
return 0
|
| 200 |
+
|
| 201 |
+
# Growth Temperature as range "low//high", user enters single numeric or similar
|
| 202 |
+
if field_name == "Growth Temperature":
|
| 203 |
+
try:
|
| 204 |
+
if "//" in db_str:
|
| 205 |
+
low_s, high_s = db_str.split("//", 1)
|
| 206 |
+
low = float(low_s)
|
| 207 |
+
high = float(high_s)
|
| 208 |
+
# user may have given "37//37" or "37" etc.
|
| 209 |
+
if "//" in user_str:
|
| 210 |
+
ut = float(user_str.split("//", 1)[0])
|
| 211 |
+
else:
|
| 212 |
+
ut = float(user_str)
|
| 213 |
+
if low <= ut <= high:
|
| 214 |
+
return 1
|
| 215 |
+
else:
|
| 216 |
+
return -1
|
| 217 |
+
except Exception:
|
| 218 |
+
return 0
|
| 219 |
+
|
| 220 |
+
# Flexible overlap match
|
| 221 |
+
match_found = False
|
| 222 |
+
for u in user_options:
|
| 223 |
+
for d in db_options:
|
| 224 |
+
if not d or not u:
|
| 225 |
+
continue
|
| 226 |
+
if u == d:
|
| 227 |
+
match_found = True
|
| 228 |
+
break
|
| 229 |
+
if u in d or d in u:
|
| 230 |
+
match_found = True
|
| 231 |
+
break
|
| 232 |
+
if match_found:
|
| 233 |
+
break
|
| 234 |
+
|
| 235 |
+
if match_found:
|
| 236 |
+
return 1
|
| 237 |
+
|
| 238 |
+
if field_name in hard_exclusions:
|
| 239 |
+
return -999 # hard mismatch
|
| 240 |
+
return -1
|
| 241 |
+
|
| 242 |
+
# ---------- Next-test suggestions ----------
|
| 243 |
+
|
| 244 |
+
def suggest_next_tests(
|
| 245 |
+
self,
|
| 246 |
+
top_results: List[IdentificationResult],
|
| 247 |
+
user_input: Dict[str, Any],
|
| 248 |
+
max_tests: int = 3,
|
| 249 |
+
) -> List[str]:
|
| 250 |
+
"""
|
| 251 |
+
Suggest tests that best differentiate top matches and haven't
|
| 252 |
+
already been entered or marked 'Unknown' by the user.
|
| 253 |
+
"""
|
| 254 |
+
if not top_results:
|
| 255 |
+
return []
|
| 256 |
+
|
| 257 |
+
# Only consider first 3–5 genera
|
| 258 |
+
top_names = {r.genus for r in top_results[:5]}
|
| 259 |
+
varying_fields: List[str] = []
|
| 260 |
+
|
| 261 |
+
for field in self.db_columns:
|
| 262 |
+
if field == "Genus":
|
| 263 |
+
continue
|
| 264 |
+
|
| 265 |
+
# Skip fields user already filled with a known value
|
| 266 |
+
u_val = user_input.get(field, "")
|
| 267 |
+
if isinstance(u_val, str) and u_val.lower() not in {"", "unknown"}:
|
| 268 |
+
continue
|
| 269 |
+
|
| 270 |
+
# Check if this field differs meaningfully between top genera
|
| 271 |
+
values_for_field = set()
|
| 272 |
+
for _, row in self.db.iterrows():
|
| 273 |
+
g = row.get("Genus", "")
|
| 274 |
+
if g in top_names:
|
| 275 |
+
v = str(row.get(field, "")).strip().lower()
|
| 276 |
+
if v:
|
| 277 |
+
values_for_field.add(v)
|
| 278 |
+
|
| 279 |
+
if len(values_for_field) > 1:
|
| 280 |
+
varying_fields.append(field)
|
| 281 |
+
|
| 282 |
+
# simple deterministic: take first few
|
| 283 |
+
return varying_fields[:max_tests]
|
| 284 |
+
|
| 285 |
+
# ---------- Main identification routine ----------
|
| 286 |
+
|
| 287 |
+
def identify(self, user_input: Dict[str, Any]) -> List[IdentificationResult]:
|
| 288 |
+
"""
|
| 289 |
+
Compare user input to database and rank possible genera.
|
| 290 |
+
Integrates extended signals when available.
|
| 291 |
+
"""
|
| 292 |
+
|
| 293 |
+
results: List[IdentificationResult] = []
|
| 294 |
+
total_fields_possible = len([c for c in self.db_columns if c != "Genus"])
|
| 295 |
+
|
| 296 |
+
# Pre-compute extended scores if extended_reasoner is available
|
| 297 |
+
extended_scores: Dict[str, float] = {}
|
| 298 |
+
extended_explanation: str = ""
|
| 299 |
+
|
| 300 |
+
if HAS_EXTENDED_REASONER:
|
| 301 |
+
try:
|
| 302 |
+
ranked_ext, explanation = score_genera_from_extended(user_input)
|
| 303 |
+
extended_explanation = explanation or ""
|
| 304 |
+
for genus, score in ranked_ext:
|
| 305 |
+
extended_scores[str(genus)] = float(score)
|
| 306 |
+
except Exception:
|
| 307 |
+
extended_scores = {}
|
| 308 |
+
extended_explanation = ""
|
| 309 |
+
|
| 310 |
+
for _, row in self.db.iterrows():
|
| 311 |
+
genus = str(row.get("Genus", "")).strip()
|
| 312 |
+
if not genus:
|
| 313 |
+
continue
|
| 314 |
+
|
| 315 |
+
total_score = 0
|
| 316 |
+
matched_fields: List[str] = []
|
| 317 |
+
mismatched_fields: List[str] = []
|
| 318 |
+
reasoning_factors: Dict[str, Any] = {}
|
| 319 |
+
total_fields_evaluated = 0
|
| 320 |
+
|
| 321 |
+
hard_excluded = False
|
| 322 |
+
|
| 323 |
+
for field in self.db_columns:
|
| 324 |
+
if field == "Genus":
|
| 325 |
+
continue
|
| 326 |
+
|
| 327 |
+
db_val = row.get(field, "")
|
| 328 |
+
user_val = user_input.get(field, "")
|
| 329 |
+
|
| 330 |
+
score = self.compare_field(db_val, user_val, field)
|
| 331 |
+
|
| 332 |
+
if user_val is not None and str(user_val).strip() != "" and str(user_val).strip().lower() != "unknown":
|
| 333 |
+
total_fields_evaluated += 1
|
| 334 |
+
|
| 335 |
+
if score == -999:
|
| 336 |
+
hard_excluded = True
|
| 337 |
+
total_score = -999
|
| 338 |
+
break
|
| 339 |
+
elif score == 1:
|
| 340 |
+
total_score += 1
|
| 341 |
+
matched_fields.append(field)
|
| 342 |
+
reasoning_factors[field] = user_val
|
| 343 |
+
elif score == -1:
|
| 344 |
+
total_score -= 1
|
| 345 |
+
mismatched_fields.append(field)
|
| 346 |
+
|
| 347 |
+
if hard_excluded:
|
| 348 |
+
continue # skip this genus entirely
|
| 349 |
+
|
| 350 |
+
extra_notes = str(row.get("Extra Notes", "")).strip() if "Extra Notes" in row else ""
|
| 351 |
+
|
| 352 |
+
r = IdentificationResult(
|
| 353 |
+
genus=genus,
|
| 354 |
+
total_score=total_score,
|
| 355 |
+
matched_fields=matched_fields,
|
| 356 |
+
mismatched_fields=mismatched_fields,
|
| 357 |
+
reasoning_factors=reasoning_factors,
|
| 358 |
+
total_fields_evaluated=total_fields_evaluated,
|
| 359 |
+
total_fields_possible=total_fields_possible,
|
| 360 |
+
extra_notes=extra_notes,
|
| 361 |
+
)
|
| 362 |
+
|
| 363 |
+
# Attach extended score if available
|
| 364 |
+
if genus in extended_scores:
|
| 365 |
+
r.extended_score = extended_scores[genus]
|
| 366 |
+
r.extended_explanation = extended_explanation
|
| 367 |
+
|
| 368 |
+
results.append(r)
|
| 369 |
+
|
| 370 |
+
# Sort by core score descending
|
| 371 |
+
results.sort(key=lambda r: r.total_score, reverse=True)
|
| 372 |
+
|
| 373 |
+
# Suggest next tests for top few
|
| 374 |
+
if results:
|
| 375 |
+
next_tests = self.suggest_next_tests(results[:5], user_input)
|
| 376 |
+
next_tests_str = ", ".join(next_tests) if next_tests else ""
|
| 377 |
+
for r in results[:5]:
|
| 378 |
+
r.reasoning_factors["next_tests"] = next_tests_str
|
| 379 |
+
|
| 380 |
+
# Return top 10
|
| 381 |
+
return results[:10]
|
|
|
|
|
|