Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples of proposed FHIR structure for Global.HealthxISARIC #205

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ venv/
ENV/
env.bak/
venv.bak/
isaric-venv

# Spyder project settings
.spyderproject
Expand All @@ -151,6 +152,9 @@ venv.bak/
# Rope project settings
.ropeproject

# VScode project settings
.vscode

# mkdocs documentation
/site

Expand Down
63 changes: 63 additions & 0 deletions fhir/fhir2flat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
Convert FHIR resources as JSON files to FHIRflat CSV files.
"""

import pandas as pd
import json


def expandCoding(df: pd.DataFrame, column_name: str):
"""
Expands a column containing a list of dictionaries with coding information into
multiple columns.
"""

i = df.columns.get_loc(column_name)
df.insert(i + 1, column_name + "_system", df[column_name][0][0]["system"])
df.insert(i + 2, column_name + "_code", df[column_name][0][0]["code"])
df.insert(i + 3, column_name + "_display", df[column_name][0][0]["display"])

df.drop(columns=[column_name], inplace=True)


def condenseReference(df, reference):
"""
Condenses a reference into a string containing the reference type and the reference
id.
"""
df[reference] = df[reference].apply(lambda x: x.split("/")[-1])


def fhir2flat(fhir_file: str, flat_file: str):
"""
Converts a FHIR JSON file into a FHIRflat file.

fhir_file: path to FHIR JSON file
flat_file: path to save new FHIRflat CSV file to
"""

# Load JSON data
with open(fhir_file) as json_file:
data = json.load(json_file)

# Flatten JSON and convert to DataFrame
df = pd.json_normalize(data)

# expand all instances of the "coding" list
for coding in df.columns[df.columns.str.endswith("coding")]:
expandCoding(df, coding)

# condense all references
for reference in df.columns[df.columns.str.endswith("reference")]:
condenseReference(df, reference)

# drop static columns
# Can we drop status in all cases? "status" in MedicationAdministration might be
# for indicating "not taken"...
df.drop(columns=["status", "meta.profile"], axis=1, inplace=True)

# save to csv
df.to_csv(flat_file, index=False)


fhir2flat("resource_structure_dev/procedure.json", "flat_procedure.csv")
146 changes: 143 additions & 3 deletions fhir/fhir_export.py → fhir/fhir_export_isaric.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from dataclasses import dataclass
from typing import Optional, Literal, Any

from drug_name_codes import corticosteroid_snomed, antiviral_snomed


@dataclass
class Patient:
Expand All @@ -17,6 +19,7 @@ class Patient:
birthDate: Optional[str] = None
deceasedBoolean: Optional[bool] = None
deceasedDateTime: Optional[str] = None
# outcome here?

def to_json(self) -> dict[str, Any]:
out = {
Expand Down Expand Up @@ -74,14 +77,102 @@ def to_json(self) -> dict[str, Any]:
}


@dataclass
class Medication:
subject: str
visit: str
is_present: bool
snomed_code: int
text: str
datetime: Optional[str] = None
period: Optional[tuple(str, str)] = None

def to_json(self) -> dict[str, Any]:
return {
"resourceType": "MedicationStatement",
"id": str(uuid.uuid4()),
"status": "recorded",
"medication": { # not sure about this structure
"concept": {
"coding": [
{
"system": "http://snomed.info/sct",
"code": str(self.snomed_code),
"display": self.text,
} # could put LOINC code here too
],
},
},
"adherence": {
"code": {
"coding": {
"system": "http://hl7.org/fhir/CodeSystem/medication-statement-adherence",
"code": "taking" if self.is_present else "not-taking",
}
}
},
"subject": {"reference": f"Patient/{self.subject}"},
"encounter": {"reference": f"Encounter/{self.visit}"},
"effectiveTimeDate": self.datetime,
"effectivePeriod": {"start": self.period[0], "end": self.period[1]}
if self.period
else None,
}


@dataclass
class Procedure:
subject: str
visit: str
is_present: bool
snomed_code: int
text: str
datetime: Optional[str] = None
period: Optional[tuple(str, str)] = None

def to_json(self) -> dict[str, Any]:
return {
# "resourceType": "Procedure",
# "id": str(uuid.uuid4()),
# "status": "recorded",
# "medication": {
# "concept": {
# "coding": [
# {
# "system": "http://snomed.info/sct",
# "code": str(self.snomed_code),
# "display": self.text,
# } # could put LOINC code here too
# ],
# },
# },
# "adherence": {
# "code": {
# "coding": {
# "system": "http://hl7.org/fhir/CodeSystem/medication-statement-adherence",
# "code": "taking" if self.is_present else "not-taking",
# }
# }
# },
# "subject": {"reference": f"Patient/{self.subject}"},
# "encounter": {
# "reference": f"Patient/{self.visit}"
# }, # Should this be stored in Patient?
# "effectiveTimeDate": self.datetime,
# "effectivePeriod": {"start": self.period[0], "end": self.period[1]}
# if self.period
# else None,
}


@dataclass
class Observation:
category: Literal[
"social-history",
"vital-signs",
"imaging",
"laboratory",
"procedure",
# "procedure",
"survey",
"exam",
"therapy",
Expand All @@ -98,12 +189,12 @@ def to_json(self) -> dict[str, Any]:


SUBJECT_SNOMED = [
("has_chronic_hematologic_disease", 414022008),
("has_chronic_hematologic_disease", 398983000),
("has_asplenia", 707147002),
("has_tuberculosis", 427099000),
("has_dementia", 52448006),
("has_obesity", 414916001),
("has_rheumatologic_disorder", 363059001),
("has_rheumatologic_disorder", 363059001), # ?
("has_hiv", 86406008),
("has_hypertension", 38341003),
("has_malignant_neoplasm", 363346000),
Expand All @@ -122,6 +213,55 @@ def to_json(self) -> dict[str, Any]:
("has_immunosuppression", 234532001),
]

VISIT_MEDS_SNOMED = [
("treatment_corticosteroid", 788751009),
("treatment_corticosteroid_type", corticosteroid_snomed),
("treatment_antifungal_agent", 373219008),
("treatment_antifungal_agent_type",),
("treatment_antivirals", 372701006),
("treatment_antiviral_type", antiviral_snomed),
("treatment_antibiotics", 346325008),
("treatment_antibiotics_type",),
("treatment_anticoagulation", 372862008),
("treatment_inhaled_nitric_oxide", 6710000),
("treatment_ace_inhibitors", 372733002),
("treatment_arb", 372913009),
("treatment_antimalarial", 373287002),
("treatment_antimalarial_type",),
("treatment_immunosuppressant", 372823004),
("treatment_intravenous_fluids", 118431008),
("treatment_nsaid", 713443004),
("treatment_neuromuscular_blocking_agents", 373295003),
# ("treatment_offlabel",),
("treatment_colchine", 73133000), # ?
("treatment_immunoglobulins",),
("treatment_delirium",),
("treatment_delirium_type",),
("treatment_monoclonal_antibody", 49616005),
# ("treatment_other",),
]

VISIT_PROC_SNOMED = [
("treatment_dialysis", 265764009),
("treatment_ecmo", 233573008),
("treatment_oxygen_therapy", 57485005),
(
"treatment_oxygen_mask_prongs",
371907003,
), # oxygen administration by nasal cannula
("treatment_prone_position", 1240000),
("treatment_invasive_ventilation", 1258985005),
("treatment_noninvasive_ventilation", 428311008),
(
"treatment_high_flow_nasal_cannula",
426854004,
), # equipment rather than treatment - 1259025002 is heated/humidified HFNC
("treatment_cpr", 89666000),
("treatment_respiratory_support",),
("treatment_cardiovascular_support",),
("treatment_pacing", 18590009),
]

SUBJECT_INDICATOR_SNOMED_CODES = dict(SUBJECT_SNOMED)
SNOMED_DISPLAY = dict(
(v, " ".join(k.split("_")[1:]).capitalize())
Expand Down
Loading