Skip to content

From data to models, rules and tables

Start with the generated fixture profile, then the model and data dictionary. These are transparent educational rules, not diagnoses. All comparisons need the same source population, latest versions and as_of clock.

The profile records 68 LOINC 4548-4 A1c observations across 8 clinical-fixture patients, all with % units and final status. The observation-code sections provide corresponding BP, lipid and smoking counts, units and date bounds. The Immunization paths give CVX codes/statuses/dates; Condition and MedicationRequest paths give SNOMED/RxNorm evidence. The Data model page reads those numbers directly from generated JSON rather than maintaining a second set of statistics.

What each path actually does

Way 1 operational: ClinicalMapping parses FHIR; SyncService updates EF entities in SQL Server or SQLite. Patient summary and CDS services query those tables, project ClinicalRecord, form a RiskPatient, and call RiskEngine. GET /api/patients/{id}/summary returns clinical arrays and CDS cards; POST /cds-services/patient-risk returns the same card projection. Cards contain summary, indicator, detail with FHIR source IDs, and source attribution.

Track A lakehouse: bronze_fhir appends original JSON. latest_fhir chooses one current version; silver projects typed columns. The runner reads persisted latest JSON through the same C# mapper and risk engine, inserts risk_rule_results, then SQL publishes gold_risk_flags and gold_outreach. There is no independently implemented SQL PCE or A1c formula to drift from the C# rule. Metabase reads gold; /api/reports/risk groups gold summaries and includes outreach rows.

Way 2 Oystehr: the read-only authenticated Zambda reads Patient/{id} and patient searches for Observation, Condition, MedicationRequest and Immunization via the Oystehr FHIR API. mapping.ts builds a TypeScript risk model; rules.ts implements the rules and CDS-card projection. It does not read the operational SQL or DuckDB database.

Oystehr references below were read with git show origin/wt/WT-dc65/ottehr-track:<path> at commit a2e5bf9faf3a3857c83b0b2e5971e8a0932a2870. That branch was not edited. Its parity harness pins older Way 1 commit 8d1dd5f; it is not proof of unrestricted parity against pass 8. Its fixture tests expect the example flag values below. The branch still reads the first smoking value coding, whereas current Way 1 requires a SNOMED value coding. That difference needs its own branch change and parity run; this style pass must not conceal it.

Eligibility shared by the rule inputs

ClinicalMapping.ToRiskPatient and IsEligible exclude undated/future facts and entered-in-error, cancelled or refuted status. Observations must be final, amended or corrected and use LOINC. Supported quantities must be finite and positive; units are checked by the relevant rule. Results sort by effective time descending, then ordinal resource ID. No missing value becomes zero or false.

Condition/allergy invalid verification overrides clinical status. Diabetes is positive only for active explicit supported SNOMED concepts. BP treatment is positive only for supported RxNorm products with active status or completed within the previous 12 months. Unrelated history does not prove either status negative. Source IDs remain attached to the result.

A1c range

flowchart LR
  source[Observation 4548-4 valueQuantity percent and effective time]
  model[ObservationEntity.Value → RiskPatient.Labs]
  rule[Latest eligible A1c: 5.7 and 6.5 thresholds]
  sql[Way 1 observations → CDS card]
  lake[Track A bronze → silver_observation → gold_risk_flags]
  ehr[Way 2 FHIR API → Zambda rule → CDS card]
  source --> model --> rule
  rule --> sql
  source --> lake
  source --> ehr
Step Exact data and owner
Source/profile Observation.code.coding LOINC 4548-4; valueQuantity.value, .code or .unit %; effectiveDateTime/period start; profile: 68 results, 8 patients
Model ObservationEntity.Code/System/Value/Unit/Effective/Status → ClinicalRecord → RiskPatient.Labs[].Code/Value/Unit/Effective
Rule src/FhirDemo.Core/Helpers/RiskEngine.cs:35: last eligible A1c; >=6.5 diabetes range, >=5.7 prediabetes range, otherwise below threshold. warning starts at 5.7. Missing input emits null-score information card.
Operational patients, observations; summary/CDS endpoint above; card carries the exact value and cited Observation IDs
Lakehouse bronze_fhir.raw_json → silver_observation.code/code_system/value/unit/effective; gold_patient_summary.latest_a1c SQL at src/FhirDemo.Lakehouse/Helpers/LakehouseRunner.cs:117; shared rule → risk_rule_results at line 163 → gold_risk_flags.score/indicator/summary/inputs/as_of at line 171; outreach at line 172
Oystehr ehr-track/risk/index.ts:27 reads patient, line 30 reads observations; mapping.ts:42 maps; rules.ts:65 selects A1c, line 71 emits range flag
Example Ali Herman's latest is 6.270038819212741%, displayed 6.27% → a1c, score 6.270038819212741, warning, “HbA1c: Prediabetes range.”

A1c trend

flowchart LR
  source[Three dated Observation 4548-4 values]
  model[ObservationEntity → ordered LabFact inputs]
  rule[Oldest less than middle less than newest]
  sql[Way 1 observations → trend CDS card]
  lake[Track A bronze → silver_observation → gold trend flag]
  ehr[Way 2 FHIR API → rules.ts trend]
  source --> model --> rule --> sql
  source --> lake
  source --> ehr
Step Exact data and owner
Source/profile Same 68 profiled A1c observations; the latest three eligible facts are selected per patient, not three arbitrary rows
Model ObservationEntity.Value/Effective/Id → RiskPatient.Labs; ordinal ID breaks equal-time ties
Rule src/FhirDemo.Core/Helpers/RiskEngine.cs:43: three strictly rising values → score 1/warning; three otherwise → 0/info; one or two → null/info. With no A1c, only the missing-A1c flag is emitted, not a trend row.
Operational patients, observations; summary/CDS cards[].detail lists the three dated sources
Lakehouse Same bronze/silver columns; C# risk loop at LakehouseRunner.cs:163; SQL gold_risk_flags at 171 publishes flag a1c_trend; gold_outreach.warning_count/reasons includes its warning
Oystehr ehr-track/risk/rules.ts:70 computes strict increase; line 72 emits the trend flag
Example Ali's oldest→newest displayed values 5.90 → 6.22 → 6.27% → score 1, warning. Comparisons use unrounded values.

Repeated elevated blood pressure

flowchart LR
  source[LOINC BP panel and systolic diastolic components]
  model[ObservationEntity.Systolic Diastolic → BpFact]
  rule[At least 2 of last 3: systolic 140 or diastolic 90]
  sql[Way 1 observations → hypertension card]
  lake[Track A bronze → silver BP columns → gold flag]
  ehr[Way 2 FHIR API → rules.ts BP]
  source --> model --> rule --> sql
  source --> lake
  source --> ehr
Step Exact data and owner
Source/profile Observation panel LOINC 55284-4 or 85354-9; components 8480-6/8462-4; quantity code mm[Hg] or unit mmHg. Generated observation-code profiles show panel counts and units; component paths retain component code/system frequencies.
Model ObservationEntity.Systolic/Diastolic/Effective → RiskPatient.BloodPressures → BpFact
Rule src/FhirDemo.Core/Helpers/RiskEngine.cs:48: latest three complete positive finite pairs; count systolic >=140 OR diastolic >=90. At least two high readings → warning. Fewer than two complete readings → null score.
Operational patients, observations; card cites each complete reading used
Lakehouse silver_observation.systolic/diastolic SQL at LakehouseRunner.cs:62; gold_patient_summary.latest_systolic/latest_diastolic at 117 selects a complete pair; shared rule at 163 → flag hypertension in gold at 171 → outreach
Oystehr ehr-track/risk/mapping.ts:31 component mapper; rules.ts:74 latest complete pairs and lines 75–76 high-count/card
Example Ali's last three display 124/88, 117/87, 118/75 mmHg → score 0, info. Adalberto's display 168/89, 162/119, 194/94 → score 3, warning.

Flu care gap

flowchart LR
  source[Completed CVX flu Immunization with occurrence time]
  model[ImmunizationEntity → VaccineFact]
  rule[Inclusive previous 12 months at as_of]
  sql[Way 1 immunizations → flu gap card]
  lake[Track A bronze → silver_immunization → gold status and flag]
  ehr[Way 2 FHIR API → rules.ts flu gap]
  source --> model --> rule --> sql
  source --> lake
  source --> ehr
Step Exact data and owner
Source/profile Immunization.vaccineCode.coding CVX; occurrenceDateTime; completed status. Generated Immunization profile lists all captured codes/statuses and date range. Supported flu codes: 140, 141, 150, 155, 158, 161, 166, 171, 185, 186, 197, 205.
Model ImmunizationEntity.Code/System/Effective/Status → RiskPatient.Vaccines[].Code/Occurred/Status
Rule src/FhirDemo.Core/Helpers/RiskEngine.cs:57: completed supported CVX occurrence in inclusive [as_of.AddMonths(-12), as_of] → score 0/info; otherwise 1/warning. Dataset absence is not proof of no vaccination elsewhere.
Operational patients, immunizations; card reports care gap and cites available eligible flu records
Lakehouse Bronze → silver_immunization.code/code_system/status/effective; independent recorded-dose SQL gold_immunization_status.has_recent_flu at LakehouseRunner.cs:128; shared C# flag → gold flu_gap at 171; outreach counts its warning
Oystehr ehr-track/risk/rules.ts:83 eligible flu vaccines; previousYear at line 25 clamps leap day to match .NET; lines 84–85 emit gap
Example Ali has historical flu records but none in the prior 12 months at the committed snapshot clock → score 1, warning.

ASCVD

flowchart LR
  source[Demographics lipids BP smoking diabetes treatment evidence]
  model[Patient Observation Condition Medication → RiskPatient]
  rule[Complete explicit inputs → 2013 PCE or null]
  sql[Way 1 tables → ASCVD CDS card]
  lake[Track A bronze latest JSON → shared PCE → gold ASCVD]
  ehr[Way 2 FHIR API → mapping.ts → PCE]
  source --> model --> rule --> sql
  source --> lake
  source --> ehr
Step Exact data and owner
Source/profile Patient full birthDate/gender/explicit race; LOINC 2093-3 total cholesterol and 2085-9 HDL in mg/dL; supported BP systolic; LOINC 72166-2 with SNOMED smoking value; active SNOMED diabetes 44054006/46635009; RxNorm BP products 197361/316049/897718. Profile paths expose availability; no race or status is invented to fill gaps.
Model PatientEntity.BirthDate/Sex/Race, observation quantities/components/ValueCode, condition and medication code/status → RiskPatient.Age/Sex/Race/Labs/BloodPressures/Smoker/Diabetes/BpTreated
Rule src/FhirDemo.Core/Helpers/RiskEngine.cs:74 validates age 40–79, supported sex/race coefficients, positive finite lipids/SBP, explicit booleans. Line 105: 100 * (1 - S0 ^ exp(sum - mean)); coefficients at line 110; warning at >=20%. Missing any required input returns null.
Formula detail sum = Age*ln(age) + AgeSquared*ln(age)^2 + Total*ln(TC) + AgeTotal*ln(age)*ln(TC) + Hdl*ln(HDL) + AgeHdl*ln(age)*ln(HDL) + chosenBp*ln(SBP) + chosenAgeBp*ln(age)*ln(SBP) + Smoker*smoker + AgeSmoker*ln(age)*smoker + Diabetes*diabetes. Treated status chooses BP coefficients.
Operational patients, observations, conditions, medications; CDS card lists missing fields or calculated percent plus source IDs and historical race-coefficient caveat
Lakehouse Bronze latest JSON → same mapper/PCE at LakehouseRunner.cs:163; silver columns document projection but do not independently supply all PCE inputs. gold_risk_flags stores score/inputs; gold_outreach.ascvd_percent at 172 preserves null and only orders known scores after warning count.
Oystehr ehr-track/risk/mapping.ts:42 builds inputs; rules.ts:39 validates; line 56 computes same PCE; line 80 calls it. Mapping difference and older pinned parity limitation are stated above.
Example Ali has missing explicit race coefficient, BP-treated and diabetes status → null, info. Adalberto lacks explicit race coefficient and diabetes status → null, info. No prediction is invented.

Evidence and scope of parity

The Data model page's values come from the committed synthetic patient snapshot and linked fixtures. Ali's AllergyIntolerance and A1c are traced through all layers in DATA-MODEL.md. The five displayed expected flags match the Ottehr branch's fixtures.test.ts, including its null ASCVD result. That is fixture evidence, not a live cloud execution.

tests/FhirDemo.Tests/FixtureRiskParityTests.cs compares every persisted operational patient flag with DuckDB gold: flag membership, nullable score, indicator, summary, detail, source IDs and clock. It explicitly replays both the top-level clinical fixture directory and the Ottehr subdirectory into the test lakehouse. The default lakehouse command loads only 10 top-level clinical patients, while operational sync includes the 2 Ottehr patients. This population difference is existing behavior, not hidden by the test.

Pass7LakehouseParityTests also checks latest A1c and complete BP values across every top-level fixture patient and adverse mapping cases. Actual test output belongs in VERIFY.md. A passing fixture comparison establishes agreement for that input set and clock; it does not establish clinical validity or guarantee equivalence for every possible FHIR resource.