Skip to main content

Transformation utility to translate data formats into FHIR

Project description

Python FHIR converter, fastish, most nuts and bolts included, ready for production

License PyPi - Version Python versions


Provides a python native version of FHIR-Converter written in C#.

The key features are:

  • Fastish: Leverages Cython where possible
  • Move fast: Designed to be extensibile. Use the thin rendering API or leverage the builtin parts
  • Easy: Designed to be easy to use, extend and deploy.
  • Robust: Get production-ready code.

Limitations:

  • Only CDA->FHIR is currently builtin. Additional work is needed to implement the filters, etc to support FHIR->FHIR and HL7v2->FHIR and back.
  • Python-liquid requires a comma between parameters to filters. This does not appear to be a restriction with DotLiquid. As a result templates brought to this environment may need commas added.

Built on the back of:

Table of Contents

Install

Install Python FHIR Converter using Pipenv:

$ pipenv install -u python-fhir-converter

Or pip:

$ pip install python-fhir-converter

Links

Basic Usage

Source can be found here

from functools import partial
from pathlib import Path

from fhir_converter.loaders import get_file_system_loader
from fhir_converter.renderers import (
    CcdaRenderer,
    get_environment,
    render_files_to_dir,
    render_to_dir,
)
from fhir_converter.utils import mkdir_if_not_exists

templates_dir, sample_data_dir, data_out_dir = (
    Path("data/templates/ccda"),
    Path("data/sample/ccda"),
    Path("data/out"),
)

from_file = sample_data_dir.joinpath("CCD.ccda")
mkdir_if_not_exists(data_out_dir)

# Render the file to string using the rendering defaults indenting the output
with open(from_file) as xml_in:
    # indent is provided, any other kwargs supported by dump may be provided
    print(CcdaRenderer().render_fhir_string("CCD", xml_in, indent=1))

# Create a renderer that will load the user defined templates into the rendering env
renderer = CcdaRenderer(
    get_environment(loader=get_file_system_loader(search_path=templates_dir))
)

# Render the file to the output directory using the default CCD template
render_to_dir(
    render=partial(renderer.render_fhir, "CCD"),
    from_file=from_file,
    to_dir=data_out_dir,
)

# Render all of the sample files to the output directory using the user defined
# pampi (problems, allergies, meds, procedures, immunizations) template
render_files_to_dir(
    render=partial(renderer.render_fhir, "pampi"),
    from_dir=sample_data_dir,
    to_dir=data_out_dir,
    path_filter=lambda p: p.suffix in (".ccda", ".xml"),
)

Command line interface

The package comes with a CLI interface that can be invoked either by the script name fhir_converter_cli or as python module python -m fhir_converter. The CLI allows you to transform a single file or an entire directory.

fhir_converter_cli  --from-file  ./data/sample/ccda/CCD.ccda --to-dir ./data/out --template-name CCD
---------------------------------------------------------------
RENDER SUCCESS
---------------------------------------------------------------
Total time: 0.14s
Finished at: 2024-01-11 10:49:44.182033
Final Memory: 32M
---------------------------------------------------------------

fhir-converter % fhir_converter_cli --template-dir ./data/templates/ccda --from-dir ./data/sample/ccda --to-dir ./data/out --template-name pampi
---------------------------------------------------------------
RENDER SUCCESS
---------------------------------------------------------------
Total time: 0.32s
Finished at: 2024-01-11 10:49:44.182033
Final Memory: 37M
---------------------------------------------------------------

Benchmark

You can run the benchmark from the root of the source tree. Test rig is a 14-inch, 2021 Macbook Pro with the binned M1 PRO not in low power mode.

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        3    0.000    0.000   16.998    5.666 ../scripts/benchmark.py:75(render_samples)
       22    0.003    0.000   16.997    0.773 ../fhir-converter/fhir_converter/renderers.py:187(render_files_to_dir)
      484    0.002    0.000   16.968    0.035 ../fhir-converter/fhir_converter/renderers.py:220(render_to_dir)
      484    0.010    0.000   16.842    0.035 ../fhir-converter/fhir_converter/renderers.py:93(render_fhir)
      484    0.003    0.000   14.674    0.030 ../fhir-converter/fhir_converter/renderers.py:117(render_to_fhir)

The test fixture profiles the converter using a single thread. The samples are rendered using all of the builtin templates along with the handful of user defined templates. The percall time is relative to the rendering template being used, the number of files being rendered (there is some warm up) and the size of the files to be rendered. In a 60 minute period in similar conditions a little over 100K CDA documents could be rendered into FHIR bundles. Note: including the original CDA document in the bundle as a DocumentReference adds noticable overhead to the render. Omitting this via a user defined template is recommended if this is not required for your usecase.

Related Projects

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_fhir_converter-0.0.16.tar.gz (65.9 kB view hashes)

Uploaded Source

Built Distribution

python_fhir_converter-0.0.16-py3-none-any.whl (349.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page