Skip to content

Commit

Permalink
Show combined patient series (#2318)
Browse files Browse the repository at this point in the history
* Upgrade related_patient_columns_to_records to do a "full join"
* Add failing test

* fixes #2301

---------

Co-authored-by: Dave Evans <[email protected]>
  • Loading branch information
madwort and evansd authored Dec 13, 2024
1 parent 9ca5a7e commit 932e6d5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
5 changes: 4 additions & 1 deletion ehrql/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ def related_columns_to_records(columns):


def related_patient_columns_to_records(columns):
for patient_id in columns[0].patient_to_value.keys():
patients_all = set()
for c in columns:
patients_all.update(c.patient_to_value.keys())
for patient_id in sorted(patients_all):
record = {"patient_id": patient_id}
for i, column in enumerate(columns, start=1):
record[f"series_{i}"] = render_value(column[patient_id], convert_null=True)
Expand Down
36 changes: 33 additions & 3 deletions tests/unit/test_debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import pytest

from ehrql import create_dataset, show
from ehrql.debugger import activate_debug_context, elements_are_related_series, render
from ehrql.debugger import (
activate_debug_context,
elements_are_related_series,
related_patient_columns_to_records,
render,
)
from ehrql.query_engines.in_memory_database import PatientColumn
from ehrql.tables import EventFrame, PatientFrame, Series, table

Expand All @@ -19,7 +24,7 @@ def date_serializer(obj):
def test_show(capsys):
expected_output = textwrap.dedent(
"""
Show line 27:
Show line 32:
'Hello'
"""
).strip()
Expand All @@ -32,7 +37,7 @@ def test_show(capsys):
def test_show_with_label(capsys):
expected_output = textwrap.dedent(
"""
Show line 40: Number
Show line 45: Number
14
"""
).strip()
Expand Down Expand Up @@ -61,6 +66,31 @@ def test_render_multiple_variables():
assert render(12, "Hello") == expected_output


def test_related_patient_columns_to_records_full_join():
c1 = PatientColumn.parse(
"""
1 | 101
2 | 102
4 | 104
"""
)
c2 = PatientColumn.parse(
"""
1 | 201
2 | 202
3 | 203
"""
)
r = list(related_patient_columns_to_records([c1, c2]))
r_expected = [
{"patient_id": 1, "series_1": 101, "series_2": 201},
{"patient_id": 2, "series_1": 102, "series_2": 202},
{"patient_id": 3, "series_1": "", "series_2": 203},
{"patient_id": 4, "series_1": 104, "series_2": ""},
]
assert r == r_expected


def test_render_formatted_table():
expected_output = textwrap.dedent(
"""
Expand Down

0 comments on commit 932e6d5

Please sign in to comment.