From 83c7c7f1d3919b080d403ccc2251208b3f5e5be9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Trevi=C3=B1o=20Cervantes?= Date: Fri, 23 Oct 2026 00:33:53 -0600 Subject: [PATCH] fix column matcher false-positive on shared table prefixes; round float durations for int columns --- webapi/imports/mapping/column_matcher.py | 18 ++++++++++++++++-- webapi/imports/schemas/coercion.py | 8 ++++++++ webapi/imports/schemas/tea/eeg_estudio.py | 7 ++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/webapi/imports/mapping/column_matcher.py b/webapi/imports/mapping/column_matcher.py index 9996370..871ee1c 100644 --- a/webapi/imports/mapping/column_matcher.py +++ b/webapi/imports/mapping/column_matcher.py @@ -139,9 +139,23 @@ class ColumnMatcher: if normalized_source == unprefixed_target: return _UNPREFIXED, "unprefixed" # Not every field has a table prefix (`hemoglobina_g_dl`), so words - # are compared against both the full and the unprefixed name. + # are compared against both the full and the unprefixed name. When + # the target *does* have real content beyond its prefix ("eeg" in + # "eeg_estudio"), that prefix is dropped from the full-name words + # first: every column in the table shares it, so a source word + # matching only that (e.g. "eeg_id" ~ "eeg_estudio", both just + # carrying "eeg") is not real evidence of a relationship. When the + # target has no content beyond its prefix (it isn't really a + # "prefix_field" name, e.g. "hemoglobina_g_dl"), nothing is dropped. + unprefixed_target_words = _words(unprefixed_target) + prefix_token = normalized_target.split("_", 1)[0] + target_words_full = ( + [w for w in _words(normalized_target) if w != prefix_token] + if unprefixed_target_words + else _words(normalized_target) + ) source_words = _words(normalized_source) - words = max(_word_score(source_words, _words(normalized_target)), _word_score(source_words, _words(unprefixed_target))) + words = max(_word_score(source_words, target_words_full), _word_score(source_words, unprefixed_target_words)) if words >= _WORD_FLOOR: # 0.5..0.7 by word overlap, plus a tiny whole-string tiebreak so # "fecha_reporte" prefers bx_fecha_reporte over another one-word overlap. diff --git a/webapi/imports/schemas/coercion.py b/webapi/imports/schemas/coercion.py index aaebfeb..41f2a65 100644 --- a/webapi/imports/schemas/coercion.py +++ b/webapi/imports/schemas/coercion.py @@ -31,6 +31,14 @@ def optional_str(v: Any) -> Any: return text or None +def rounded_int(v: Any) -> Any: + """A minutes/count field stored as an `int` column, whose source may + report a float (e.g. a duration measured to tenths of a minute).""" + if v is None or (isinstance(v, str) and v.strip() == ""): + return None + return round(float(v)) + + _DAY_FIRST_FORMATS = ("%d/%m/%Y", "%d-%m-%Y", "%d.%m.%Y", "%Y/%m/%d") diff --git a/webapi/imports/schemas/tea/eeg_estudio.py b/webapi/imports/schemas/tea/eeg_estudio.py index 49134ce..274b050 100644 --- a/webapi/imports/schemas/tea/eeg_estudio.py +++ b/webapi/imports/schemas/tea/eeg_estudio.py @@ -17,7 +17,7 @@ from core.tea.enums import ( TipoGrafoelemento, ) from webapi.imports.schemas.base import ImportSchema -from webapi.imports.schemas.coercion import edf_file, Catalog, CatalogValue, coerce_date, optional_str +from webapi.imports.schemas.coercion import edf_file, Catalog, CatalogValue, coerce_date, optional_str, rounded_int _FUENTE_INTERPRETACION_EEG_CATALOG = Catalog(FuenteInterpretacionEeg) _ESTADO_REGISTRO_EEG_CATALOG = Catalog(EstadoRegistroEeg) @@ -84,6 +84,11 @@ class EegEstudioImportSchema(ImportSchema): def _coerce_dates(cls, v): return coerce_date(v) + @field_validator("eeg_duracion_min", mode="before") + @classmethod + def _coerce_duracion_min(cls, v): + return rounded_int(v) + @field_validator("eeg_estudio", mode="before") @classmethod def _valid_edf(cls, v):