fhir-kindling


Namefhir-kindling JSON
Version 1.0.3 PyPI version JSON
download
home_pagehttps://migraf.github.io/fhir-kindling/
SummaryPython library to simplify working with FHIR servers and resources.
upload_time2023-09-13 16:54:37
maintainer
docs_urlNone
authorMichael Graf
requires_python>=3.8,<4.0
licenseMIT
keywords python fhir hl7 client medical records healthcare data
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            ![Header](./docs/logo/kindling_header.png)

[![CI](https://github.com/migraf/fhir-kindling/actions/workflows/main_ci.yml/badge.svg?branch=main)](https://github.com/migraf/fhir-kindling/actions/workflows/main_ci.yml)
[![codecov](https://codecov.io/gh/migraf/fhir-kindling/branch/main/graph/badge.svg?token=FKQENFXACB)](https://codecov.io/gh/migraf/fhir-kindling)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Maintainability](https://api.codeclimate.com/v1/badges/3b83aa52724b6e75fc22/maintainability)](https://codeclimate.com/github/migraf/fhir-kindling/maintainability)
![PyPI - Downloads](https://img.shields.io/pypi/dm/fhir_kindling)
![PyPI](https://img.shields.io/pypi/v/fhir_kindling)



Python library for interacting with [HL7 FHIR](http://hl7.org/fhir/) servers and resources. Resource validation and parsing powered by
[pydantic](https://github.com/samuelcolvin/pydantic) and the [fhir.resources](https://github.com/nazrulworld/fhir.resources) library.
Provides a simple interface for synchronous and asynchronous CRUD operations for resources and bundles, 
as well as resource transfer between servers.
Datascience features include flattening of resources and bundles into tabular format (pandas dataframes) and plotting 
methods for resources and bundles can optionally be included with the `ds` extra.

Check out the [documentation](https://migraf.github.io/fhir-kindling/) for more information and a detailed user guide.


Table of Contents
=================

- [Table of Contents](#table-of-contents)
  - [Features](#features)
  - [Installation](#installation)
    - [Extras (optional)](#extras-optional)
  - [Usage](#usage)
    - [Connecting to a FHIR server](#connecting-to-a-fhir-server)
    - [Query resources from the server](#query-resources-from-the-server)
      - [Basic resource query](#basic-resource-query)
      - [Query with filters](#query-with-filters)
      - [Including related resources in the query](#including-related-resources-in-the-query)
      - [Query resources by reference](#query-resources-by-reference)
    - [Add resources to the server](#add-resources-to-the-server)
    - [Deleting/Updating resources](#deletingupdating-resources)
    - [Transfer resources between servers](#transfer-resources-between-servers)
  - [Performance](#performance)
  - [Contributing](#contributing)
    - [Development](#development)
    - [Tests](#tests)
  - [Credits](#credits)

## Features

- Create, Read, Update, Delete resources using a FHIR server's REST API
- Transfer resources between servers while maintaining referential integrity using server-given IDs
- Bundle creation, validation and data management on a FHIR server via the REST API
- Supports Hapi, Blaze and IBM FHIR servers
- CSV serialization of query results
- Synthetic data generation and



<!-- Created by https://github.com/ekalinin/github-markdown-toc -->


## Installation

Install the package using pip:

```shell
pip install fhir-kindling --user
```

### Extras (optional)
Fhir kindling can be used with the following extras:
- `ds` for data science related features, such as flattening of resources into a tabular format

```
pip install fhir-kindling[ds] --user
```

## Usage

### Connecting to a FHIR server

```python
from fhir_kindling import FhirServer

# Connect with basic auth 
basic_auth_server = FhirServer("https://fhir.server/fhir", username="admin", password="admin")
# Connect with static token
token_server = FhirServer("https://fhir.server/fhir", token="your_token")

# Connect using oauth2/oidc
oidc_server = FhirServer("https://fhir.server/fhir", client_id="client_id", client_secret="secret",
                         oidc_provider_url="url")

# Print the server's capability statement
print(basic_auth_server.capabilities)

```

### Query resources from the server

#### Basic resource query

```python
from fhir_kindling import FhirServer
from fhir.resources.patient import Patient

# Connect using oauth2/oidc
oidc_server = FhirServer("https://fhir.server/fhir", client_id="client_id", client_secret="secret",
                         oidc_provider_url="url")

# query all patients on the server
query = oidc_server.query(Patient, output_format="json").all()
print(query.response)

# Query resources based on name of resource
query = oidc_server.query("Patient", output_format="json").all()
print(query.response)
```

#### Query with filters

Filtering the targeted resource is done using the `where` method on the query object. The filter is created by defining
the target field, the comparison operator and the value to compare.

```python
from fhir_kindling import FhirServer

server = FhirServer(api_address="https://fhir.server/fhir")

query = server.query("Patient").where(field="birthDate", operator="gt", value="1980").all()
```

#### Including related resources in the query

Resources that reference or are referenced by resources targeted by the query can be included in the response using
the `include` method on the query object.

```python
# server initialization omitted
# get the patients along with the queried conditions
query_patient_condition = server.query("Condition").include(resource="Condition", reference_param="subject").all()

# get the conditions for a patient
query_patient_condition = server.query("Patient")
query_patient_condition = query_patient_condition.include(resource="Condition", reference_param="subject", reverse=True)
response = query_patient_condition.all()
```

#### Query resources by reference

If you know the id and resource type of the resource you want to query, you can use the `get` method for a single
reference
for a list of references use `get_many`. The passed references should follow the format of `<resource_type>/<id>`.

```python
# server initialization omitted
patient = server.get("Patient/123")

patients = server.get_many(["Patient/123", "Patient/456"])

```

### Add resources to the server

Resources can be added to the server using the `add` method on the server object. Lists of resources can be added using
'add_all'.

```python
from fhir_kindling import FhirServer
from fhir.resources.patient import Patient

# Connect to the server
server = FhirServer(api_address="https://fhir.server/fhir")

# add a single resource
patient = Patient(name=[{"family": "Smith", "given": ["John"]}])
response = server.add(patient)

# add multiple resources
patients = [Patient(name=[{"family": f"Smith_{i}", "given": ["John"]}]) for i in range(10)]
response = server.add_all(patients)
```

### Deleting/Updating resources

Resources can be deleted from the server using the `delete` method on the server object, it takes as input either
references to the resources or the resources itself.  
Similarly the `update` method can be used to update the resources on the server, by passing a list of updated resources.

```python
from fhir_kindling import FhirServer
from fhir.resources.patient import Patient

# Connect to the server
server = FhirServer(api_address="https://fhir.server/fhir")

# add some patients
patients = [Patient(name=[{"family": f"Smith_{i}", "given": ["John"]}]) for i in range(10)]
response = server.add_all(patients)

# change the name of the patients
for patient in response.resources:
    patient.name[0].given[0] = "Jane"

# update the patients on the server
updated_patients = server.update(resources=response.resources)

# delete based on reference
server.delete(references=response.references[:5])
# delete based on resources
server.delete(resources=response.resources[5:])
```

### Transfer resources between servers

Transferring resources between servers is done using the `transfer` method on the server object. Using this method
server assigned ids are used for transfer and referential integrity is maintained.  
This method will also attempt to get all the resources that are referenced by the resources being transferred from the
origin
server and transfer them to the destination server as well.

```python
from fhir_kindling import FhirServer

# initialize the two servers
server_1 = FhirServer(api_address="https://fhir.server/fhir")
server_2 = FhirServer(api_address="https://fhir.server/fhir")

# query some resources from server 1
conditions = server_1.query("Condition").limit(10)
# transfer the resources to server 2
response = server_1.transfer(server_2, resources=conditions.resources)

```

## Performance

This library performs request at least 1.5 times faster than other popular python FHIR libraries.
See [Benchmarks](benchmarks/README.md) for a more detailed description of the benchmarks.
![Query Results](benchmarks/results/query_plot.png)


## Contributing
Contributions are very welcome and greatly appreciated! If you want to contribute to this project, please fork the 
repository and make changes as you'd like. Pull requests are warmly welcome and credit will always be given.

### Development

To set up your environment to develop this package make sure you have [poetry](https://python-poetry.org/) installed and
run the following commands:

Install the dependencies:
```bash
poetry install --with dev --all-extras
```

Install pre-commit hooks:

- Linting: [ruff](https://github.com/charliermarsh/ruff)
- Formatting [black](https://black.readthedocs.io/en/stable/)

```bash
poetry run pre-commit install
```

### Tests
To run the full test suit you need access to two FHIR servers (the second one is used for transfer tests).
You can spin up two servers (one HAPI and one Blaze FHIR) using the compose file in the `testing` directory.
```bash
cd testing
docker compose up
```
The servers will be available at `http://localhost:9090/fhir` and `http://localhost:9091/fhir` respectively.
And the test should be configured to use them via the environment variables `FHIR_API_URL` and `TRANSFER_SERVER_URL` respectively.

Run the tests:
```bash
poetry run pytest
```


## Credits

This package was created with Cookiecutter and
the [audreyr/cookiecutter-pypackage](https://github.com/audreyr/cookiecutter) project template.






            

Raw data

            {
    "_id": null,
    "home_page": "https://migraf.github.io/fhir-kindling/",
    "name": "fhir-kindling",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "python,fhir,hl7,client,medical records,healthcare,data",
    "author": "Michael Graf",
    "author_email": "michael.graf3110@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/56/6a/07fd801c319231beb174c97ee946660ae8627cdfa96cf7fdeaa3a8f2c1fb/fhir_kindling-1.0.3.tar.gz",
    "platform": null,
    "description": "![Header](./docs/logo/kindling_header.png)\n\n[![CI](https://github.com/migraf/fhir-kindling/actions/workflows/main_ci.yml/badge.svg?branch=main)](https://github.com/migraf/fhir-kindling/actions/workflows/main_ci.yml)\n[![codecov](https://codecov.io/gh/migraf/fhir-kindling/branch/main/graph/badge.svg?token=FKQENFXACB)](https://codecov.io/gh/migraf/fhir-kindling)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![Maintainability](https://api.codeclimate.com/v1/badges/3b83aa52724b6e75fc22/maintainability)](https://codeclimate.com/github/migraf/fhir-kindling/maintainability)\n![PyPI - Downloads](https://img.shields.io/pypi/dm/fhir_kindling)\n![PyPI](https://img.shields.io/pypi/v/fhir_kindling)\n\n\n\nPython library for interacting with [HL7 FHIR](http://hl7.org/fhir/) servers and resources. Resource validation and parsing powered by\n[pydantic](https://github.com/samuelcolvin/pydantic) and the [fhir.resources](https://github.com/nazrulworld/fhir.resources) library.\nProvides a simple interface for synchronous and asynchronous CRUD operations for resources and bundles, \nas well as resource transfer between servers.\nDatascience features include flattening of resources and bundles into tabular format (pandas dataframes) and plotting \nmethods for resources and bundles can optionally be included with the `ds` extra.\n\nCheck out the [documentation](https://migraf.github.io/fhir-kindling/) for more information and a detailed user guide.\n\n\nTable of Contents\n=================\n\n- [Table of Contents](#table-of-contents)\n  - [Features](#features)\n  - [Installation](#installation)\n    - [Extras (optional)](#extras-optional)\n  - [Usage](#usage)\n    - [Connecting to a FHIR server](#connecting-to-a-fhir-server)\n    - [Query resources from the server](#query-resources-from-the-server)\n      - [Basic resource query](#basic-resource-query)\n      - [Query with filters](#query-with-filters)\n      - [Including related resources in the query](#including-related-resources-in-the-query)\n      - [Query resources by reference](#query-resources-by-reference)\n    - [Add resources to the server](#add-resources-to-the-server)\n    - [Deleting/Updating resources](#deletingupdating-resources)\n    - [Transfer resources between servers](#transfer-resources-between-servers)\n  - [Performance](#performance)\n  - [Contributing](#contributing)\n    - [Development](#development)\n    - [Tests](#tests)\n  - [Credits](#credits)\n\n## Features\n\n- Create, Read, Update, Delete resources using a FHIR server's REST API\n- Transfer resources between servers while maintaining referential integrity using server-given IDs\n- Bundle creation, validation and data management on a FHIR server via the REST API\n- Supports Hapi, Blaze and IBM FHIR servers\n- CSV serialization of query results\n- Synthetic data generation and\n\n\n\n<!-- Created by https://github.com/ekalinin/github-markdown-toc -->\n\n\n## Installation\n\nInstall the package using pip:\n\n```shell\npip install fhir-kindling --user\n```\n\n### Extras (optional)\nFhir kindling can be used with the following extras:\n- `ds` for data science related features, such as flattening of resources into a tabular format\n\n```\npip install fhir-kindling[ds] --user\n```\n\n## Usage\n\n### Connecting to a FHIR server\n\n```python\nfrom fhir_kindling import FhirServer\n\n# Connect with basic auth \nbasic_auth_server = FhirServer(\"https://fhir.server/fhir\", username=\"admin\", password=\"admin\")\n# Connect with static token\ntoken_server = FhirServer(\"https://fhir.server/fhir\", token=\"your_token\")\n\n# Connect using oauth2/oidc\noidc_server = FhirServer(\"https://fhir.server/fhir\", client_id=\"client_id\", client_secret=\"secret\",\n                         oidc_provider_url=\"url\")\n\n# Print the server's capability statement\nprint(basic_auth_server.capabilities)\n\n```\n\n### Query resources from the server\n\n#### Basic resource query\n\n```python\nfrom fhir_kindling import FhirServer\nfrom fhir.resources.patient import Patient\n\n# Connect using oauth2/oidc\noidc_server = FhirServer(\"https://fhir.server/fhir\", client_id=\"client_id\", client_secret=\"secret\",\n                         oidc_provider_url=\"url\")\n\n# query all patients on the server\nquery = oidc_server.query(Patient, output_format=\"json\").all()\nprint(query.response)\n\n# Query resources based on name of resource\nquery = oidc_server.query(\"Patient\", output_format=\"json\").all()\nprint(query.response)\n```\n\n#### Query with filters\n\nFiltering the targeted resource is done using the `where` method on the query object. The filter is created by defining\nthe target field, the comparison operator and the value to compare.\n\n```python\nfrom fhir_kindling import FhirServer\n\nserver = FhirServer(api_address=\"https://fhir.server/fhir\")\n\nquery = server.query(\"Patient\").where(field=\"birthDate\", operator=\"gt\", value=\"1980\").all()\n```\n\n#### Including related resources in the query\n\nResources that reference or are referenced by resources targeted by the query can be included in the response using\nthe `include` method on the query object.\n\n```python\n# server initialization omitted\n# get the patients along with the queried conditions\nquery_patient_condition = server.query(\"Condition\").include(resource=\"Condition\", reference_param=\"subject\").all()\n\n# get the conditions for a patient\nquery_patient_condition = server.query(\"Patient\")\nquery_patient_condition = query_patient_condition.include(resource=\"Condition\", reference_param=\"subject\", reverse=True)\nresponse = query_patient_condition.all()\n```\n\n#### Query resources by reference\n\nIf you know the id and resource type of the resource you want to query, you can use the `get` method for a single\nreference\nfor a list of references use `get_many`. The passed references should follow the format of `<resource_type>/<id>`.\n\n```python\n# server initialization omitted\npatient = server.get(\"Patient/123\")\n\npatients = server.get_many([\"Patient/123\", \"Patient/456\"])\n\n```\n\n### Add resources to the server\n\nResources can be added to the server using the `add` method on the server object. Lists of resources can be added using\n'add_all'.\n\n```python\nfrom fhir_kindling import FhirServer\nfrom fhir.resources.patient import Patient\n\n# Connect to the server\nserver = FhirServer(api_address=\"https://fhir.server/fhir\")\n\n# add a single resource\npatient = Patient(name=[{\"family\": \"Smith\", \"given\": [\"John\"]}])\nresponse = server.add(patient)\n\n# add multiple resources\npatients = [Patient(name=[{\"family\": f\"Smith_{i}\", \"given\": [\"John\"]}]) for i in range(10)]\nresponse = server.add_all(patients)\n```\n\n### Deleting/Updating resources\n\nResources can be deleted from the server using the `delete` method on the server object, it takes as input either\nreferences to the resources or the resources itself.  \nSimilarly the `update` method can be used to update the resources on the server, by passing a list of updated resources.\n\n```python\nfrom fhir_kindling import FhirServer\nfrom fhir.resources.patient import Patient\n\n# Connect to the server\nserver = FhirServer(api_address=\"https://fhir.server/fhir\")\n\n# add some patients\npatients = [Patient(name=[{\"family\": f\"Smith_{i}\", \"given\": [\"John\"]}]) for i in range(10)]\nresponse = server.add_all(patients)\n\n# change the name of the patients\nfor patient in response.resources:\n    patient.name[0].given[0] = \"Jane\"\n\n# update the patients on the server\nupdated_patients = server.update(resources=response.resources)\n\n# delete based on reference\nserver.delete(references=response.references[:5])\n# delete based on resources\nserver.delete(resources=response.resources[5:])\n```\n\n### Transfer resources between servers\n\nTransferring resources between servers is done using the `transfer` method on the server object. Using this method\nserver assigned ids are used for transfer and referential integrity is maintained.  \nThis method will also attempt to get all the resources that are referenced by the resources being transferred from the\norigin\nserver and transfer them to the destination server as well.\n\n```python\nfrom fhir_kindling import FhirServer\n\n# initialize the two servers\nserver_1 = FhirServer(api_address=\"https://fhir.server/fhir\")\nserver_2 = FhirServer(api_address=\"https://fhir.server/fhir\")\n\n# query some resources from server 1\nconditions = server_1.query(\"Condition\").limit(10)\n# transfer the resources to server 2\nresponse = server_1.transfer(server_2, resources=conditions.resources)\n\n```\n\n## Performance\n\nThis library performs request at least 1.5 times faster than other popular python FHIR libraries.\nSee [Benchmarks](benchmarks/README.md) for a more detailed description of the benchmarks.\n![Query Results](benchmarks/results/query_plot.png)\n\n\n## Contributing\nContributions are very welcome and greatly appreciated! If you want to contribute to this project, please fork the \nrepository and make changes as you'd like. Pull requests are warmly welcome and credit will always be given.\n\n### Development\n\nTo set up your environment to develop this package make sure you have [poetry](https://python-poetry.org/) installed and\nrun the following commands:\n\nInstall the dependencies:\n```bash\npoetry install --with dev --all-extras\n```\n\nInstall pre-commit hooks:\n\n- Linting: [ruff](https://github.com/charliermarsh/ruff)\n- Formatting [black](https://black.readthedocs.io/en/stable/)\n\n```bash\npoetry run pre-commit install\n```\n\n### Tests\nTo run the full test suit you need access to two FHIR servers (the second one is used for transfer tests).\nYou can spin up two servers (one HAPI and one Blaze FHIR) using the compose file in the `testing` directory.\n```bash\ncd testing\ndocker compose up\n```\nThe servers will be available at `http://localhost:9090/fhir` and `http://localhost:9091/fhir` respectively.\nAnd the test should be configured to use them via the environment variables `FHIR_API_URL` and `TRANSFER_SERVER_URL` respectively.\n\nRun the tests:\n```bash\npoetry run pytest\n```\n\n\n## Credits\n\nThis package was created with Cookiecutter and\nthe [audreyr/cookiecutter-pypackage](https://github.com/audreyr/cookiecutter) project template.\n\n\n\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Python library to simplify working with FHIR servers and resources.",
    "version": "1.0.3",
    "project_urls": {
        "Homepage": "https://migraf.github.io/fhir-kindling/",
        "Repository": "https://github.com/migraf/fhir-kindling"
    },
    "split_keywords": [
        "python",
        "fhir",
        "hl7",
        "client",
        "medical records",
        "healthcare",
        "data"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2701e4bfd3f8a8aae4c1b12f35c50d6031c6d5787b714134181256bf21ef1fe6",
                "md5": "06df06fda8b01c4de53e71ae5d450c5a",
                "sha256": "655c75e3684a7345a39b58a5873d2419f40a6658b8ec34d4f8a8a351c856a474"
            },
            "downloads": -1,
            "filename": "fhir_kindling-1.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "06df06fda8b01c4de53e71ae5d450c5a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 83093,
            "upload_time": "2023-09-13T16:54:35",
            "upload_time_iso_8601": "2023-09-13T16:54:35.634405Z",
            "url": "https://files.pythonhosted.org/packages/27/01/e4bfd3f8a8aae4c1b12f35c50d6031c6d5787b714134181256bf21ef1fe6/fhir_kindling-1.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "566a07fd801c319231beb174c97ee946660ae8627cdfa96cf7fdeaa3a8f2c1fb",
                "md5": "096dee9968d9b576fe7af6de40906e9d",
                "sha256": "a9c3b3beb9c2b495b8ecc38ac2080affeb3a981d6130518e09da8ebe9d899317"
            },
            "downloads": -1,
            "filename": "fhir_kindling-1.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "096dee9968d9b576fe7af6de40906e9d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 68268,
            "upload_time": "2023-09-13T16:54:37",
            "upload_time_iso_8601": "2023-09-13T16:54:37.273298Z",
            "url": "https://files.pythonhosted.org/packages/56/6a/07fd801c319231beb174c97ee946660ae8627cdfa96cf7fdeaa3a8f2c1fb/fhir_kindling-1.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-13 16:54:37",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "migraf",
    "github_project": "fhir-kindling",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "lcname": "fhir-kindling"
}
        
Elapsed time: 2.32350s