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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
04780695a22372f898c0e6b11b4861be640b77e2233e467c3bc28134e05b70ca
|
|
| MD5 |
55d281889684c99214a7591b0d3bcddf
|
|
| BLAKE2b-256 |
29d3253cb2345119ca825266d903ad69218712ef80d6186a30c8057a2ea8f788
|
Provenance
The following attestation bundles were made for fpml-0.2.0.tar.gz:
Publisher:
publish-pypi.yml on beda-software/FHIRPathMappingLanguage
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fpml-0.2.0.tar.gz -
Subject digest:
04780695a22372f898c0e6b11b4861be640b77e2233e467c3bc28134e05b70ca - Sigstore transparency entry: 256552621
- Sigstore integration time:
-
Permalink:
beda-software/FHIRPathMappingLanguage@5f55ea5aab4a6acf44106d0ad524d6df96546609 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/beda-software
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@5f55ea5aab4a6acf44106d0ad524d6df96546609 -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac7e65ebfcfa118e0b11ab846ce9a2fe6ec8e2bb9e3be2eac0f061122dbc9939
|
|
| MD5 |
fd0a3d42a174d541fe4a16978bf100bc
|
|
| BLAKE2b-256 |
131630bbeba3c16d1fe6dc5364cfd190c1ea6d800d02b1782e3239a058ccb847
|
Provenance
The following attestation bundles were made for fpml-0.2.0-py3-none-any.whl:
Publisher:
publish-pypi.yml on beda-software/FHIRPathMappingLanguage
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fpml-0.2.0-py3-none-any.whl -
Subject digest:
ac7e65ebfcfa118e0b11ab846ce9a2fe6ec8e2bb9e3be2eac0f061122dbc9939 - Sigstore transparency entry: 256552634
- Sigstore integration time:
-
Permalink:
beda-software/FHIRPathMappingLanguage@5f55ea5aab4a6acf44106d0ad524d6df96546609 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/beda-software
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@5f55ea5aab4a6acf44106d0ad524d6df96546609 -
Trigger Event:
push
-
Statement type: