Skip to content

Commit

Permalink
Add race extension
Browse files Browse the repository at this point in the history
  • Loading branch information
pipliggins committed May 20, 2024
1 parent 234fd09 commit c524efe
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 18 deletions.
2 changes: 1 addition & 1 deletion fhirflat/flat2fhir.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def create_codeable_concept(
"""Re-creates a codeableConcept structure from the FHIRflat representation."""

# for reading in from ingestion pipeline
if (name + ".code" and name + ".system") in old_dict:
if name + ".code" in old_dict and name + ".system" in old_dict:
code = old_dict[name + ".code"]
if isinstance(code, list) and len(code) > 1:
new_dict = {"coding": []}
Expand Down
4 changes: 4 additions & 0 deletions fhirflat/resources/extension_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@ class birthSexType(AbstractType):
__resource_type__ = "birthSex"


class raceType(AbstractType):
__resource_type__ = "Race"


class dateTimeExtensionType(AbstractType):
__resource_type__ = "dateTimeExtension"
5 changes: 5 additions & 0 deletions fhirflat/resources/extension_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def __init__(self):
"Duration": (None, ".extensions"),
"Age": (None, ".extensions"),
"birthSex": (None, ".extensions"),
"Race": (None, ".extensions"),
"dateTimeExtension": (None, ".extensions"),
}

Expand Down Expand Up @@ -230,5 +231,9 @@ def birthsex_validator(v: Union[StrBytes, dict, Path, FHIRAbstractModel]):
return Validators().fhir_model_validator("birthSex", v)


def race_validator(v: Union[StrBytes, dict, Path, FHIRAbstractModel]):
return Validators().fhir_model_validator("Race", v)


def datetimeextension_validator(v: Union[StrBytes, dict, Path, FHIRAbstractModel]):
return Validators().fhir_model_validator("dateTimeExtension", v)
36 changes: 36 additions & 0 deletions fhirflat/resources/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,42 @@ def elements_sequence(cls):
]


class Race(_DataType):
"""
An ISARIC extension collecting data on the race of a patient.
"""

resource_type = Field("Race", const=True)

url = Field("race", const=True, alias="url")

valueCodeableConcept: fhirtypes.CodeableConceptType = Field(
None,
alias="valueCodeableConcept",
title="Value of extension",
description=(
"Value of extension - must be one of a constrained set of the data "
"types (see [Extensibility](extensibility.html) for a list)."
),
# if property is element of this resource.
element_property=True,
element_required=True,
)

@classmethod
def elements_sequence(cls):
"""returning all elements names from
``Extension`` according specification,
with preserving original sequence order.
"""
return [
"id",
"extension",
"url",
"valueCodeableConcept",
]


# ------------------- extension types ------------------------------


Expand Down
34 changes: 17 additions & 17 deletions fhirflat/resources/patient.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from fhir.resources.patient import Patient
from .base import FHIRFlatBase
from .extension_types import (
ageType,
birthSexType,
)
from .extensions import Age, birthSex
from .extension_types import ageType, birthSexType, raceType
from .extensions import Age, birthSex, Race
import orjson

from ..flat2fhir import expand_concepts
Expand All @@ -16,18 +13,20 @@


class Patient(Patient, FHIRFlatBase):
extension: list[Union[ageType, birthSexType, fhirtypes.ExtensionType]] = Field(
None,
alias="extension",
title="Additional content defined by implementations",
description=(
"""
extension: list[Union[ageType, birthSexType, raceType, fhirtypes.ExtensionType]] = (
Field(
None,
alias="extension",
title="Additional content defined by implementations",
description=(
"""
Contains the G.H 'age' and 'birthSex' extensions,
and allows extensions from other implementations to be included."""
),
# if property is element of this resource.
element_property=True,
union_mode="smart",
),
# if property is element of this resource.
element_property=True,
union_mode="smart",
)
)

# attributes to exclude from the flat representation
Expand All @@ -47,9 +46,10 @@ class Patient(Patient, FHIRFlatBase):
def validate_extension_contents(cls, extensions):
age_count = sum(isinstance(item, Age) for item in extensions)
birthsex_count = sum(isinstance(item, birthSex) for item in extensions)
race_count = sum(isinstance(item, Race) for item in extensions)

if age_count > 1 or birthsex_count > 1:
raise ValueError("Age and birthSex can only appear once.")
if age_count > 1 or birthsex_count > 1 or race_count > 1:
raise ValueError("Age, birthSex and Race can only appear once.")

return extensions

Expand Down

0 comments on commit c524efe

Please sign in to comment.