Skip to content

Commit

Permalink
Merge pull request #27 from Medical-Event-Data-Standard/v2_release_ca…
Browse files Browse the repository at this point in the history
…ndidate

MEDS v2 Release
  • Loading branch information
EthanSteinberg authored May 4, 2024
2 parents 965b5de + 08b2562 commit a69ec50
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Byte-compiled / optimized / DLL files
_version.py
__pycache__/
*.py[cod]
*$py.class
Expand Down
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@ The Python type signature for the schema is as follows:

Patient = TypedDict('Patient', {
'patient_id': int,
'static_measurements': List[Measurement],
'events': List[Event],
})

Event = TypedDict('Event',{
'time': datetime.datetime,
'measurements': List[Measurement],
})

Measurement = TypedDict('Measurement', {
'time': NotRequired[datetime.datetime],
'code': str,
'text_value': NotRequired[str],
'numeric_value': NotRequired[float],
Expand Down
3 changes: 1 addition & 2 deletions src/meds/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from meds._version import __version__ # noqa

from .schema import (patient_schema, Measurement, Event, Patient, label, Label,
from .schema import (patient_schema, Event, Patient, label, Label,
code_metadata_entry, code_metadata, dataset_metadata,
CodeMetadataEntry, CodeMetadata, DatasetMetadata, birth_code,
death_code)
Expand All @@ -9,7 +9,6 @@
# List all objects that we want to export
_exported_objects = {
'patient_schema': patient_schema,
'Measurement': Measurement,
'Event': Event,
'Patient': Patient,
'label': label,
Expand Down
24 changes: 10 additions & 14 deletions src/meds/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,23 @@
birth_code = "SNOMED/184099003"
death_code = "SNOMED/419620001"


def patient_schema(per_event_metadata_schema=pa.null()):
def patient_schema(per_event_properties_schema=pa.null()):
# Return a patient schema with a particular per event metadata subschema
measurement = pa.struct(
event = pa.struct(
[
("time", pa.timestamp("us")), # Static events will have a null timestamp
("code", pa.string()),
("text_value", pa.string()),
("numeric_value", pa.float32()),
("datetime_value", pa.timestamp("us")),
("metadata", per_event_metadata_schema),
("properties", per_event_properties_schema),
]
)

event = pa.struct([("time", pa.timestamp("us")), ("measurements", pa.list_(measurement))])

patient = pa.schema(
[
("patient_id", pa.int64()),
("static_measurements", pa.list_(measurement)),
("events", pa.list_(event)), # Require ordered by time
("events", pa.list_(event)), # Require ordered by time, nulls must be first
]
)

Expand All @@ -49,20 +46,19 @@ def patient_schema(per_event_metadata_schema=pa.null()):

# Python types for the above schema

Measurement = TypedDict(
"Measurement",
Event = TypedDict(
"Event",
{
"time": NotRequired[datetime.datetime],
"code": str,
"text_value": NotRequired[str],
"numeric_value": NotRequired[float],
"datetime_value": NotRequired[datetime.datetime],
"metadata": NotRequired[Any],
"properties": NotRequired[Any],
},
)

Event = TypedDict("Event", {"time": datetime.datetime, "measurements": List[Measurement]})

Patient = TypedDict("Patient", {"patient_id": int, "static_measurements": List[Measurement], "events": List[Event]})
Patient = TypedDict("Patient", {"patient_id": int, "events": List[Event]})

############################################################

Expand Down
19 changes: 5 additions & 14 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,13 @@ def test_patient_schema():
patient_data = [
{
"patient_id": 123,
"static_measurements": [{
"code": "some_static_code",
"text_value": "example",
"numeric_value": 1.0,
"datetime_value": datetime.datetime(2019, 1, 1, 0, 0, 0),
"metadata": None,
}],
"events": [{ # Nested list for events
"time": datetime.datetime(2020, 1, 1, 12, 0, 0),
"measurements": [{ # Nested list for measurements
"code": "some_code",
"text_value": "Example",
"numeric_value": 10.0,
"datetime_value": datetime.datetime(2020, 1, 1, 12, 0, 0),
"metadata": None
}]
"code": "some_code",
"text_value": "Example",
"numeric_value": 10.0,
"datetime_value": datetime.datetime(2020, 1, 1, 12, 0, 0),
"properties": None
}]
}
]
Expand Down

0 comments on commit a69ec50

Please sign in to comment.