Implement the following plan: Phase 3: Value and Unit Normalization Context Phase 2…
- User
Implement the following plan:
Phase 3: Value and Unit Normalization
Context
Phase 2 (Expression Trees) is complete and verified (119/119 tests passing). The
atomic_criteriatable storesunit_textas raw free-text strings (e.g., "mg/dL", "%", "years") but has nounit_concept_idcolumn. Per gap_closure_plan.md Phase 3 (Gap 7), we need a static UCUM lookup mapping ~50 common clinical trial units to OMOPunit_concept_idvalues, plus optional categorical value normalization ("positive"/"negative" → SNOMED qualifier concepts).This is a lightweight phase — no LLM calls needed, pure deterministic YAML-based lookup.
Files to Create
File Description .../protocol_processor/config/ucum_mappings.yaml~50 unit entries + ~6 value entries with UCUM codes and OMOP concept IDs .../protocol_processor/tools/unit_normalizer.pynormalize_unit()andnormalize_value()— YAML lookup with alias expansionservices/api-service/alembic/versions/43_01_add_unit_value_concept_ids.pyMigration: add unit_concept_idandvalue_concept_idcolumns toatomic_criteria.../tests/test_unit_normalizer.py~22 tests for normalizer (canonical, alias, case, whitespace, None, unrecognized) Files to Modify
File Change libs/shared/src/shared/models.pyAdd unit_concept_id: int | Noneandvalue_concept_id: int | NonetoAtomicCriterion.../tools/structure_builder.pyImport normalizer, call in _create_atomic_from_mapping().../tests/test_phase2_e2e.pyAdd TestPhase3UnitNormalizationclass + update column assertion.../config/__init__.pyUpdate docstring to mention ucum_mappings.yaml Implementation Steps
Step 1: UCUM Mappings YAML (
config/ucum_mappings.yaml)Static YAML with two sections:
— list of ~50 entries, each with:
Assistant
I'll start by reading the key files I need to understand before implementing. Let me explore the codebase in parallel.
Assistant
Now let me read the existing migration and terminology_router for patterns.
Assistant
Good, I have a clear picture of the codebase. Let me implement all the steps. I'll start with the files that can be created independently in parallel.
Assistant
Now let me create the UCUM mappings YAML and the unit normalizer in parallel.
Assistant
Now add the model fields and create the migration. Also update the config
__init__.pyand integrate into structure_builder — all in parallel since they're independent edits.Assistant
Wait, I introduced a duplicate import. Let me fix that — the original import block already had the schemas import.
Assistant
I see the duplicate. Let me fix it.
Assistant
Now integrate the normalizer calls into
_create_atomic_from_mapping():Assistant
Now create the migration file:
Assistant
Now create the test files. Let me write both test files in parallel.
Assistant
Now update
test_phase2_e2e.py— add the Phase 3 test class and update the column assertion:Assistant
Now add the Phase 3 test class at the end of the file:
Assistant
All files are created/modified. Let me run the tests now.
Assistant
30/30 passed. Now the e2e tests:
Assistant
22/22 passed. Now run the full test suite:
Assistant
154/154 tests passing. Now run lint and type checks:
Assistant
Three lint issues — let me fix them all.
Assistant
The import block needs a blank line removed. Let me fix.
Assistant
Hmm, that looks correct to me. The isort rule might want no blank line between
__future__and the third-party imports here. Let me just run the auto-fix:Assistant
Ruff is clean. The mypy
yamlstubs issue — let me check if the other tools that use yaml have the same issue or if there's a project-wide config handling this.Assistant
Same pre-existing issue on
terminology_router.py— the yaml stubs error is a project-wide baseline issue, not something introduced by this change. Let me also re-run the full test suite to confirm everything is still green after the ruff auto-fix:Assistant
All 154 tests passing, ruff clean, mypy clean (pre-existing yaml stubs issue only).
Assistant
Phase 3 is fully implemented. Here's a summary of what was done:
Phase 3: Value and Unit Normalization — Complete
Files Created (4)
File Description config/ucum_mappings.yaml~50 UCUM unit entries + 6 categorical value mappings with OMOP concept IDs tools/unit_normalizer.pynormalize_unit()andnormalize_value()— YAML lookup with@lru_cache, case-insensitive, alias expansionalembic/versions/43_01_add_unit_value_concept_ids.pyMigration adding unit_concept_idandvalue_concept_idtoatomic_criteriatests/test_unit_normalizer.py30 tests covering canonical, alias, case, whitespace, None, empty, unrecognized Files Modified (4)
- User
Can you run a few inclusion/exclusion criteria through the backend and see that it properly catches the entities, relations, values and units? Check that everything is working.
Assistant
Let me explore the pipeline to understand how to invoke it end-to-end.
Assistant
Good, I have a full picture of the pipeline. The full pipeline requires Gemini API, ToolUniverse, MedGemma, etc. — but I can write and run an integration test that feeds realistic clinical criteria (as field_mappings, which is what the ground node produces) through the structure builder and verify everything lands correctly in the DB.
Let me write a comprehensive integration script.
Assistant
I miscounted — there are 12 recognized units, not 11. The first two tests passed perfectly. Let me fix the assertion counts.
Assistant
All 3 integration tests pass. Let me also run ruff and the full suite to confirm no regressions.
Assistant
157/157 tests passing, ruff clean. Here's what the integration test verified:
Integration Test Results
Ran 12 realistic clinical trial criteria (15 total field_mappings) through
build_expression_tree→ SQLite DB, checking every field:# Criterion Unit unit_concept_id value_concept_id 1 HbA1c >= 6.5% and <= 10% %8554 — 2 Age >= 18 years and <= 75 years years9448 — 3 eGFR >= 30 mL/min/1.73m2 mL/min/1.73m2720870 — - User
I think we should add units and unit_concept_ids for booleans (positive/negative) and some kind of unit concept for ECOG. For example in SNOMED there is a concept for each level. ECOG observation is bound to LOINC 89247-1 for ECOG performance. We should reference both LOINC answer list, SNOMED CT ECOG grade concepts somewhere. Consider schema's and where it's appropriate to map this.
Assistant
This is a meaningful design decision — ordinal scales like ECOG need entity-context-aware value mapping, which is fundamentally different from the current context-free
normalize_value(). Let me plan this properly.Assistant
Good exploration. Now I have a clear picture of the current design. Let me clarify the user's intent on a key point before designing the plan.
Assistant
The user wants me to research OMOP standards for ordinal scales. Let me do that research and also check how Karnofsky/ECOG are represented in OMOP CDM.
Assistant
Good, I have solid research results. Let me now design the plan with the Plan agent.
Assistant
Excellent research complete. Let me now write the final plan incorporating all three user messages: the initial request, the "custom unit" suggestion, and the "any unitless scoring system" clarification.