Skip to content

Commit

Permalink
Add types to util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pipliggins committed Aug 5, 2024
1 parent f9fbcdc commit 5eaba4e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
11 changes: 8 additions & 3 deletions fhirflat/flat2fhir.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@
from fhir.resources.quantity import Quantity
from pydantic.v1.error_wrappers import ValidationError

from .util import find_data_class, get_fhirtype, get_local_extension_type, group_keys
from .util import (
find_data_class,
get_fhirtype,
get_local_extension_type,
group_keys,
)


def create_codeable_concept(
old_dict: dict[str, list[str] | str | float | None], name: str
) -> dict[str, list[str]]:
"""Re-creates a codeableConcept structure from the FHIRflat representation."""

# for reading in from ingestion pipeline
# for creating backbone elements
if name + ".code" in old_dict and name + ".system" in old_dict:
raw_codes: str | float | list[str | None] = old_dict.get(name + ".code")
if raw_codes is not None and not isinstance(raw_codes, list):
formatted_code = (
raw_codes if isinstance(raw_codes, str) else str(int(raw_codes))
)
codes = [old_dict[name + ".system"] + "|" + formatted_code]
elif raw_codes is None:
elif not raw_codes:
codes = raw_codes
else:
formatted_codes = [
Expand Down
17 changes: 13 additions & 4 deletions fhirflat/util.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
# Utility functions for FHIRflat
from __future__ import annotations

import datetime
import importlib
import re
from collections.abc import KeysView
from itertools import groupby
from typing import TYPE_CHECKING

import fhir.resources
import numpy as np
import pandas as pd

import fhirflat
from fhirflat.resources import extensions

if TYPE_CHECKING:
from .resources.base import FHIRFlatBase


def group_keys(data_keys: list[str] | KeysView) -> dict[str, list[str]]:
"""
Expand Down Expand Up @@ -79,7 +86,9 @@ def get_local_resource(t: str, case_insensitive: bool = False):
return getattr(fhirflat, a)


def find_data_class(data_class, k):
def find_data_class(
data_class: FHIRFlatBase | list[FHIRFlatBase], k: str
) -> FHIRFlatBase:
"""
Finds the type class for item k within the data class.
Expand Down Expand Up @@ -116,7 +125,7 @@ def find_data_class(data_class, k):
return get_fhirtype(base_class)


def code_or_codeable_concept(col_name, resource):
def code_or_codeable_concept(col_name: str, resource: FHIRFlatBase) -> bool:
search_terms = col_name.split(".")
fhir_type = find_data_class(resource, search_terms[0])

Expand All @@ -138,7 +147,7 @@ def code_or_codeable_concept(col_name, resource):
return code_or_codeable_concept(".".join(search_terms[1:]), fhir_type)


def format_flat(flat_df, resource):
def format_flat(flat_df: pd.DataFrame, resource: FHIRFlatBase) -> pd.DataFrame:
"""
Performs formatting on dates/lists in FHIRflat resources.
"""
Expand Down Expand Up @@ -177,7 +186,7 @@ def format_flat(flat_df, resource):
return flat_df


def condense_codes(row, code_col):
def condense_codes(row: pd.Series, code_col: str) -> pd.Series:
raw_codes = row[(code_col + ".code")]
if isinstance(raw_codes, (str, int, float)) and raw_codes == raw_codes:
formatted_code = (
Expand Down

0 comments on commit 5eaba4e

Please sign in to comment.