Skip to main content

FHIR Query

Query any FHIR api using SQL to get tabular datasets.

Usage

import fhir2dataset as query
sql_query = """
SELECT Patient.name.family, Patient.address.city
FROM Patient
WHERE Patient.birthdate = 2000-01-01 AND Patient.gender = 'female'
"""
query.sql(sql_query)
100%|██████████| 1000/1000 [00:01<00:00, 16it/s]

    Patient.name.family        Patient.address.city
--------------------------------------------
0   Mozart                     Paris
1   Chopin                     London
2   Listz                      Vienna
...

FHIR Query is still under active development, feedback and contributions are welcome!

Installation

pip install fhir2dataset

From source

After cloning this repository, you can install the required dependencies

pip install -r requirements.txt
npm install --prefix ./fhir2dataset/tools/metadata

Check that the version of antlr4 is 4.8: npm view antlr4 version. If not, run cd fhir2dataset/metadata && npm install antlr4@4.8.0.

Getting started

There are two possible ways to enter the query: as a SQL query or as a JSON config file

SQL query

You can define SQL queries of the following form:

SELECT alias_1.a, alias_1.b, alias_2.a
FROM Resource_1 AS alias_1
INNER JOIN Resource_2 AS alias_2 ON alias_1.d = alias_2
WHERE alias_1.c = value_1
AND alias_2.d = value_2

Important note: Attributes in the SELECT clause should be valid fhir paths, while attributes in the WHERE clause should be valid search parameters.

Note that we only support a subset of SQL keywords.

By default, FHIR Query will use the HAPI FHIR Api. But you can use your own api using the following syntax:

import fhir2dataset as query

sql_query = "SELECT ..."

query.sql(
    sql_query=sql_query,
    fhir_api_url="https://api.awesome.fhir.org/baseR4/",
    token="<my token>"
)

To have more infos about the execution, you can enable logging:

import logging
logging.basicConfig(level=logging.INFO)

JSON config file

You can also use JSON configuration files.

from fhir2dataset.query import Query
from fhir2dataset.fhirrules_getter import FHIRRules

fhir_api_url = 'http://hapi.fhir.org/baseR4/'
fhir_rules = FHIRRules(fhir_api_url=fhir_api_url)
query = Query(fhir_api_url, fhir_rules=fhir_rules)

config.json:

{
  "select": {
    "alias_1": ["a", "b"],
    "alias_2": ["a"]
  },
  "from": {
    "alias_1": "Resource_1",
    "alias_2": "Resource_2"
  },
  "join": {
    "inner": {
      "alias_1": {
        "d": "alias_2"
      }
    }
  },
  "where": {
    "alias_1": {
      "c": "value_1"
    },
    "alias_2": {
      "d": "value_2"
    }
  }
}
with open('/path/to/config.json') as json_file:
    config = json.load(json_file)

query = Query(fhir_api_url=fhir_api_url, token=token)
query = query.from_config(config)
query.execute()

For extended usage, you can refer to this tutorial and then this Jupyter Notebook

More Examples

Check out examples of queries and how they are transformed in call to the FHIR api!

Contributing

The following commands on a terminal and in your virtual environment allow you to do some minimal local testing before each commit:

pip install -r requirements-dev.txt
pre-commit install

If you ever want to delete them you just have to do:

pre-commit clean

Release files for fhir2dataset 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fhir2dataset 0.1.6
File Size Uploaded
fhir2dataset-0.1.6.tar.gz 2.1 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for fhir2dataset 0.1.6
File Interpreter ABI Platform
fhir2dataset-0.1.6-py3-none-any.whl Python 3 none any Details

Total release size: 5.3 MB

Release files / fhir2dataset-0.1.6.tar.gz

Download URL fhir2dataset-0.1.6.tar.gz
Size 2.1 MB
Tags Source
SHA-256 checksum
How to use checksums
1b8fac5bb727a34eaac2db5f8e35fd4b11f31e14e95072633d4a1adc2093ffaf
BLAKE2b-256 checksum
How to use checksums
19d0462c8169180a178324f2a328776e5b329e114da9a90c333f8eb6274d179a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.3

Release files / fhir2dataset-0.1.6-py3-none-any.whl

Download URL fhir2dataset-0.1.6-py3-none-any.whl
Size 3.2 MB
Tags Python 3
SHA-256 checksum
How to use checksums
e18cad556f6fc98b68038797b3ba21413c753e2e601cdf1ae87fb13c70505399
BLAKE2b-256 checksum
How to use checksums
c615db31bfa6bf6bb9497e06c3fa4f59cc0009167d40eb2a76f26a57c3c8a629
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.1.6 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page