File size: 6,876 Bytes
c9c2c73 7927663 c9c2c73 7927663 c9c2c73 7927663 c9c2c73 4811a82 c9c2c73 4811a82 c9c2c73 4811a82 c9c2c73 4811a82 c9c2c73 4811a82 c9c2c73 4811a82 c9c2c73 7927663 4811a82 c9c2c73 7927663 c9c2c73 7927663 c9c2c73 4811a82 c9c2c73 4811a82 7927663 c9c2c73 4811a82 c9c2c73 4811a82 7927663 c9c2c73 4811a82 c9c2c73 4811a82 c9c2c73 4811a82 c9c2c73 4811a82 c9c2c73 4811a82 c9c2c73 4811a82 c9c2c73 4811a82 c9c2c73 4811a82 c9c2c73 4811a82 7927663 4811a82 7927663 4811a82 7927663 c9c2c73 4811a82 c9c2c73 4811a82 |
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 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 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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# engine/features.py
import json
import numpy as np
import re
from typing import Dict, List, Any
# ------------------------------------------------------------------
# Load schema once
# ------------------------------------------------------------------
_FEATURE_SCHEMA_PATH = "data/feature_schema.json"
with open(_FEATURE_SCHEMA_PATH, "r", encoding="utf-8") as f:
SCHEMA = json.load(f)
FEATURES = SCHEMA["features"]
# ------------------------------------------------------------------
# Helper mappings
# ------------------------------------------------------------------
PNV_MAP = {
"positive": 1.0,
"negative": -1.0,
"variable": 0.5,
"unknown": 0.0,
None: 0.0,
}
SHAPE_MAP = {
"cocci": 1.0,
"rods": 2.0,
"short rods": 2.5,
"spiral": 3.0,
"yeast": 4.0,
"variable": 0.5,
"unknown": 0.0,
}
OXYGEN_MAP = {
"aerobic": 1.0,
"anaerobic": 2.0,
"facultative anaerobe": 3.0,
"microaerophilic": 4.0,
"capnophilic": 5.0,
"unknown": 0.0,
}
# CATEGORY LABELSETS β deterministic, fixed, ML-friendly
CATEGORY_MAPS = {
"Motility Type": [
"none", "tumbling", "peritrichous", "polar", "monotrichous",
"lophotrichous", "amphitrichous", "axial", "gliding"
],
"Pigment": [
"none", "pyocyanin", "pyoverdine", "green", "yellow", "pink",
"red", "orange", "brown", "black", "violet", "cream"
],
"Odor": [
"none", "grape", "fruity", "earthy", "musty", "putrid",
"buttery", "yeasty", "medicinal", "fishy", "almond", "burnt", "mousy"
],
"Colony Pattern": [
"none", "mucoid", "rough", "smooth", "filamentous",
"spreading", "chalky", "corroding", "swarming",
"sticky", "ground-glass", "molar-tooth"
],
"TSI Pattern": [
"unknown", "a/a", "k/a", "k/k",
"k/a+h2s", "a/a+gas"
],
}
# Make fast lookup: label β integer code
CATEGORY_ENCODERS = {
field: {lab: idx for idx, lab in enumerate(labels)}
for field, labels in CATEGORY_MAPS.items()
}
# Temperature flags: a direct binary interpretation
TEMP_FLAGS = {"4c", "25c", "30c", "37c", "42c"}
# ------------------------------------------------------------------
# Normalisation helpers
# ------------------------------------------------------------------
def _norm(x: Any) -> str:
if not x:
return "unknown"
return str(x).strip().lower()
def _map_pnv(x: Any) -> float:
return PNV_MAP.get(_norm(x), 0.0)
def _map_shape(x: Any) -> float:
return SHAPE_MAP.get(_norm(x), 0.0)
def _map_oxygen(x: Any) -> float:
return OXYGEN_MAP.get(_norm(x), 0.0)
def _extract_temperature_flags(value: str):
"""
Convert things like "25//37" β {"25c":1, "37c":1}
"""
flags = {k: 0.0 for k in TEMP_FLAGS}
if not value:
return flags
s = value.lower()
nums = re.findall(r"\b(\d{1,2})\s*c?\b", s)
for n in nums:
key = f"{n}c"
if key in flags:
flags[key] = 1.0
return flags
def _growth_minmax(v: Any):
"""Convert '30//37' β (30,37)."""
if not v:
return (0.0, 0.0)
if not isinstance(v, str):
v = str(v)
m = re.match(r"^\s*(\d+)\s*//\s*(\d+)\s*$", v)
if not m:
return (0.0, 0.0)
return (float(m.group(1)), float(m.group(2)))
def _media_flag(media_field: Any, medium: str) -> float:
if not media_field:
return 0.0
mf = str(media_field).lower()
return 1.0 if medium.lower() in mf else 0.0
# ------------------------------------------------------------------
# CATEGORY mapping helper
# ------------------------------------------------------------------
def _map_category(field: str, value: Any) -> float:
"""
Deterministic integer encoding.
Unknown β 0 (first element)
Multi-values like "yellow; orange" β choose first matching token.
"""
labels = CATEGORY_MAPS.get(field)
if not labels:
return 0.0 # should not happen
enc = CATEGORY_ENCODERS[field]
s = _norm(value)
# Multi-list: pick first match
parts = [p.strip() for p in re.split(r"[;/,]", s) if p.strip()]
for p in parts:
if p in enc:
return float(enc[p])
# No match β return index for "none" or "unknown"
fallback = "none" if "none" in enc else "unknown"
return float(enc.get(fallback, 0))
# ------------------------------------------------------------------
# Main feature extractor
# ------------------------------------------------------------------
def extract_feature_vector(fused_fields: Dict[str, Any]) -> np.ndarray:
"""
Convert fused fields into a fixed-length ML-ready numeric vector.
ORDER must match feature_schema.json exactly.
"""
vec: List[float] = []
growth_temp = fused_fields.get("Growth Temperature")
temp_flags = _extract_temperature_flags(growth_temp)
for f in FEATURES:
name = f["name"]
kind = f["kind"]
value = fused_fields.get(name, "Unknown")
norm = _norm(value)
# ---------------------------
# pnv
# ---------------------------
if kind == "pnv":
vec.append(_map_pnv(norm))
# ---------------------------
# shape
# ---------------------------
elif kind == "shape":
vec.append(_map_shape(norm))
# ---------------------------
# oxygen requirement
# ---------------------------
elif kind == "oxygen":
vec.append(_map_oxygen(norm))
# ---------------------------
# category β integer encoding
# ---------------------------
elif kind == "category":
vec.append(_map_category(name, value))
# ---------------------------
# binary flag (temperatures)
# ---------------------------
elif kind == "binary":
key = name.lower().replace("temperature_", "")
vec.append(temp_flags.get(key, 0.0))
# ---------------------------
# media flag
# ---------------------------
elif kind == "media_flag":
medium = name.replace("Growth", "").strip()
media_field = fused_fields.get("Media Grown On")
vec.append(_media_flag(media_field, medium))
# ---------------------------
# numeric_from_growth_temp (legacy support)
# ---------------------------
elif kind == "numeric_from_growth_temp":
lo, hi = _growth_minmax(growth_temp)
if "min" in name.lower():
vec.append(lo)
elif "max" in name.lower():
vec.append(hi)
else:
vec.append(0.0)
# ---------------------------
# unknown kind
# ---------------------------
else:
vec.append(0.0)
return np.array(vec, dtype=float) |