FHIRflat Base Class#
- class fhirflat.resources.base.FHIRFlatBase(*, resource_type: str = 'DomainResource', fhir_comments: str | List[str] = None, id: Id = None, implicitRules: Uri = None, _implicitRules: FHIRPrimitiveExtensionType = None, language: Code = None, _language: FHIRPrimitiveExtensionType = None, meta: MetaType = None, contained: List[ResourceType] = None, extension: List[ExtensionType] = None, modifierExtension: List[ExtensionType] = None, text: NarrativeType = None)#
Base class for FHIR resources to add FHIRflat functionality.
- classmethod attr_lists() list[str]#
Attributes which take a list of FHIR types.
- classmethod cleanup(data: dict) dict#
Apply resource-specific changes to references and default values
- classmethod create_fhir_resource(data: str | dict) FHIRFlatBase | ValidationError#
Load data into a dictionary-like structure, then apply resource-specific changes and unpack flattened data like codeableConcepts back into structured data. Creates a FHIR resource from the data.
- classmethod fhir_bulk_import(file: str) FHIRFlatBase | list[FHIRFlatBase]#
Takes a ndjson file containing FHIR resources as json strings and returns a list of populated FHIR resources.
- Parameters:
file – Path to the .ndjson file containing FHIR data
- classmethod fhir_file_to_flat(source_file: str, output_name: str | None = None)#
Converts a .ndjson file of exported FHIR resources to a FHIRflat parquet file.
- Parameters:
source_file – Path to the FHIR resource file.
output_name – Name of the parquet file to be generated, optional, defaults to {resource}.parquet
- classmethod flat_fields() list[str]#
All fields that are present in the FHIRflat representation
- classmethod from_flat(file: str) FHIRFlatBase | list[FHIRFlatBase]#
Takes a FHIRflat parquet file and populates the resource with the data.
- Parameters:
file – Path to the parquet FHIRflat file containing clinical data
- Return type:
FHIRFlatBase or list[FHIRFlatBase]
- classmethod ingest_backbone_elements(mapped_data: Series) Series#
Unflattens ordered lists of data and forms the correct FHIR format which won’t be flattened after ingestion (
*_densecolumns).Extends the flat2fhir.expand_concepts function specifically for data ingestion.
- Parameters:
mapped_data – Pandas series of FHIRflat-like dictionaries ready to be converted to FHIR format.
- classmethod ingest_to_flat(data: DataFrame) DataFrame | None#
Takes a pandas dataframe containing the populated mapping file and a dictionary representing the FHIRflat resource and creates the FHIRflat parquet file. Performs data formatting on the date and coding columns to account for simplifications parquet makes when saving.
- Parameters:
data – Pandas dataframe containing the raw data
- Return type:
A dataframe containing the FHIRflat data.
- to_flat(filename: str | None = None) None | Series#
Generates a FHIRflat parquet file from the resource, or returns a Series
- Parameters:
filename – Name of the parquet file to be generated.
- classmethod validate_fhirflat(df: DataFrame, return_frames: bool = False) tuple[FHIRFlatBase | DataFrame, None | DataFrame]#
Takes a FHIRflat dataframe and validates the data against the FHIR schema. Returns a dataframe of valid resources and a dataframe of the FHIRflat data that produced validation errors, with a validation_error column describing the error.
- Parameters:
df – Pandas dataframe containing the FHIRflat data
return_frames – If True, returns the valid FHIR resources & errors as dataframes, even if only one row is present in the source.
- Returns:
valid_resources – A dataframe containing the valid FHIR resources
errors – A dataframe containing the flat_dict and validation errors.
- Raises:
ValidationError – If a single FHIR resource is present and is invalid.