import omophub
client = omophub.OMOPHub()
# Lab results from EHR (via FHIR Observation resources, parsed by your code)
local_labs = [
{"local_code": "Cr_Serum", "display": "Creatinine, Serum", "value": 1.2, "unit": "mg/dL"},
{"local_code": "Creat_Blood", "display": "Blood Creatinine", "value": 106, "unit": "umol/L"},
{"local_code": "Kidney_Fx_Creat", "display": "Creatinine Level", "value": 0.9, "unit": "mg/dL"},
]
print("Normalizing local lab codes for sidecar app...\n")
standardized = []
for lab in local_labs:
display = lab["display"]
print(f" '{lab['local_code']}' ({display})")
# Search OMOPHub using the human-readable display name
# (OMOPHub cannot resolve proprietary local codes directly)
try:
# Try basic search first
results = client.search.basic(
display,
vocabulary_ids=["LOINC"],
domain_ids=["Measurement"],
page_size=3,
)
candidates = results.get("concepts", []) if results else []
# If basic search misses, try fuzzy search
if not candidates:
fuzzy_results = client.search.fuzzy(display)
fuzzy_list = (
fuzzy_results if isinstance(fuzzy_results, list)
else fuzzy_results.get("concepts", [])
) if fuzzy_results else []
candidates = [
c for c in fuzzy_list
if c.get("vocabulary_id") == "LOINC" and c.get("domain_id") == "Measurement"
][:3]
if candidates:
best = candidates[0]
loinc_id = best["concept_id"]
loinc_name = best.get("concept_name", "Unknown")
loinc_code = best.get("concept_code", "N/A")
print(f" -> {loinc_name} (OMOP: {loinc_id}, LOINC: {loinc_code})")
standardized.append({
"original_code": lab["local_code"],
"value": lab["value"],
"unit": lab["unit"],
"loinc_concept_id": loinc_id,
"loinc_name": loinc_name,
})
else:
print(f" -> No LOINC match found (may need manual mapping)")
except omophub.APIError as e:
print(f" -> API error: {e.message}")
# All three local codes should now map to the same LOINC creatinine concept
print(f"\n--- Standardized for App Display ---")
unique_loinc = set()
for s in standardized:
unique_loinc.add(s["loinc_concept_id"])
print(f" {s['loinc_name']}: {s['value']} {s['unit']} (from '{s['original_code']}')")
print(f"\nUnique LOINC concepts: {len(unique_loinc)}")
if len(unique_loinc) == 1:
print("All local codes resolved to the same LOINC concept - ready for trend display.")
else:
print("Multiple LOINC concepts found - review mappings for consistency.")