Skip to main content

The FHIRPath mapping language is a data DSL designed to convert data from QuestionnaireResponse (and not only) to any FHIR Resource.

Project description

fpml (FHIRPathMappingLanguage)

The FHIRPath Mapping Language (FPML) is a data DSL designed to convert data from QuestionnaireResponse (and not only) to any FHIR Resource.

For more details visit the FHIRPathMappingLanguage specification.

Installation

You can install the package from PyPI using the following command:

pip install fpml

API Reference

resolve_template

The resolve_template function processes a given template with a specified FHIR resource, optionally applying a context and additional processing options.

from fpml import resolve_template

result = resolve_template(
    resource,
    template,
    context=None,
    fp_options=None,
    strict=False
)

Arguments:

  • resource (Resource): The input FHIR resource to process.
  • template (Any): The template describing the transformation.
  • context (Optional[Context], optional): Additional context data. Defaults to None.
  • fp_options (Optional[FPOptions], optional): Options for controlling FHIRPath evaluation. Defaults to None.
  • strict (bool, optional): Whether to enforce strict mode. Defaults to False. See more details on strict mode.

Returns:

  • Any: The processed output based on the template.

Raises:

  • FPMLValidationError: If validation of the template or resource fails.

Usage

For the following QuestionnaireResponse resource:

resource = {
    "resourceType": "QuestionnaireResponse",
    "status": "completed",
    "item": [
        {
            "linkId": "name",
            "answer": [
                {
                    "valueString": "Name"
                }
            ]
        }
    ]
}

Here's an example demonstrating how to use the resolve_template function:

from fpml import resolve_template


template = {
    "resourceType": "Patient",
    "name": [
        {
            "text": "{{ item.where(linkId='name').answer.valueString }}"
        }
    ]
}

context = {}

result = resolve_template(resource, template, context)
print(result)

Output:

{'resourceType': 'Patient', 'name': [{'text': 'Name'}]}

Using FHIR data-model

from fpml import resolve_template
from fhirpathpy.models import models


template = {
    "resourceType": "Patient",
    "name": [
        {
            "text": "{{ item.where(linkId='name').answer.value }}"  # <-- according R4 model
        }
    ]
}

context = {}

fp_options = {
    "model": models["r4"]
}

result = resolve_template(resource, template, context, fp_options)
print(result)

Output:

{'resourceType': 'Patient', 'name': [{'text': 'Name'}]}

Using user-defined functions

from fpml import resolve_template


template = {
    "resourceType": "Patient",
    "name": [
        {
            "text": "{{ item.where(linkId='name').answer.valueString.strip() }}"  # <-- Custom function
        }
    ]
}

context = {}

user_invocation_table = {
    "strip": {
        "fn": lambda inputs: [i.strip() for i in inputs],
        "arity": {0: []},
    }
}

fp_options = {
    "userInvocationTable": user_invocation_table
}

result = resolve_template(resource, template, context, fp_options)
print(result)

Output:

{'resourceType': 'Patient', 'name': [{'text': 'Name'}]}

Handling validation errors

from fpml import FPMLValidationError, resolve_template


template = {
    "resourceType": "Patient",
    "name": [
        {
            "text": "{{ item.where( }}"  # <-- Invalid expression
        }
    ]
}

context = {}

try:
    resolve_template(resource, template, context)
except FPMLValidationError as e:
    print(f"Validation error: {e.error_message}")
    print(f"Error path: `{e.error_path}`")

Output:

"Validation error: Cannot evaluate 'item.where(': where wrong arity: got 0"
"Error path: `name.0.text`"

Using strict mode

In strict mode only context variables can be used (starting with percent sigh). Any access of the resource property will raise a validation error.

from fpml import resolve_template


template = {
    "resourceType": "Patient",
    "name": [
        {
            "text": "{{ item.where(linkId='name').answer.valueString }}"  # <-- Accessing resource attribute
        }
    ]
}

context = {} 


resolve_template(resource, template, context, strict=True)

Raises an error:

FPMLValidationError: Cannot evaluate 'item.where(linkId='name').answer.valueString': "Forbidden access to resource property 'item' in strict mode. Use context instead.". Path 'name.0.text'

Meanwhile using context:

from fpml import resolve_template


template = {
    "resourceType": "Patient",
    "name": [
        {
            "text": "{{ %QuestionnaireResponse.item.where(linkId='name').answer.valueString }}"  # <-- Accessing context variable
        }
    ]
}

context = {"QuestionnaireResponse": resource}  # <-- Context 

result = resolve_template(resource, template, context, strict=True)
print(result)

Output:

{'resourceType': 'Patient', 'name': [{'text': 'Name'}]}

Development

Local environment and testing

cd ./python
poetry install

To run tests:

poetry run pytest

Pre-commit hook

In ./python directory:

Run in the shell

autohooks activate

And edit ../.git/hooks/pre-commit replacing the first line with

#!/usr/bin/env -S poetry --project=./python run python

License

This project is licensed under the MIT License.

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

fpml-0.2.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fpml-0.2.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file fpml-0.2.0.tar.gz.

File metadata

  • Download URL: fpml-0.2.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fpml-0.2.0.tar.gz
Algorithm Hash digest
SHA256 04780695a22372f898c0e6b11b4861be640b77e2233e467c3bc28134e05b70ca
MD5 55d281889684c99214a7591b0d3bcddf
BLAKE2b-256 29d3253cb2345119ca825266d903ad69218712ef80d6186a30c8057a2ea8f788

See more details on using hashes here.

Provenance

The following attestation bundles were made for fpml-0.2.0.tar.gz:

Publisher: publish-pypi.yml on beda-software/FHIRPathMappingLanguage

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fpml-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: fpml-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fpml-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ac7e65ebfcfa118e0b11ab846ce9a2fe6ec8e2bb9e3be2eac0f061122dbc9939
MD5 fd0a3d42a174d541fe4a16978bf100bc
BLAKE2b-256 131630bbeba3c16d1fe6dc5364cfd190c1ea6d800d02b1782e3239a058ccb847

See more details on using hashes here.

Provenance

The following attestation bundles were made for fpml-0.2.0-py3-none-any.whl:

Publisher: publish-pypi.yml on beda-software/FHIRPathMappingLanguage

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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