跳转到主要内容

创建FHIR数据视图以供分析的工具。

项目描述

FHIR Views

简介

FHIR Views是一种定义简单、表格化视图并转换为使用SQL on FHIR约定或其他未来数据源查询的方法。它作为简单的pip install google-fhir-views[r4,bigquery]命令的一部分安装。

FHIR Views有两个主要概念

  • 视图定义,它定义视图创建的字段和标准。它提供了一个Python API以方便使用,但最终视图定义是一组我们将下面探讨的FHIRPath表达式。
  • 视图运行器,它在某些数据源上创建该视图。

例如,让我们创建一个针对1960年之前出生的患者的简单视图

import datetime
from google.fhir.views import bigquery_runner, r4

# Load views based on the base FHIR R4 profile definitions.
views = r4.base_r4()

# Creates a view using the base patient profile.
pats = views.view_of('Patient')

# In this case we interpret the 'current' address as one where period is empty.
# This can be adjusted to meet the needs of a specific dataset.
current = pats.address.where(pats.address.period.empty()).first()

simple_pats = pats.select([
    pats.id.alias('id'),
    pats.gender.alias('gender'),
    pats.birthDate.alias('birthdate'),
    current.line.first().alias('street'),
    current.city.alias('city'),
    current.state.alias('state'),
    current.postalCode.alias('zip')
    ]).where(
       pats.birthDate < datetime.date(1960,1,1))

支持SQL on FHIR v2,上述视图也可以定义为

simple_pats_config = {
    'resource': 'Patient',
    'select': [
        {
            'alias': 'id',
            'path': 'id',
        },
        {
            'alias': 'gender',
            'path': 'gender',
        },
        {
            'alias': 'birthDate',
            'path': 'birthDate',
        },
        {
            'alias': 'street',
            'path': 'address.where(address.period.empty()).first().line.first()',
        },
        {
            'alias': 'city',
            'path': 'address.where(address.period.empty()).first().city',
        },
        {
            'alias': 'state',
            'path': 'address.where(address.period.empty()).first().state',
        },
        {
            'alias': 'zip',
            'path': 'address.where(address.period.empty()).first().postalCode',
        },
    ],
    'where': [
        {'path': 'birthDate < @1960-01-01'},
    ],
}

views = r4.base_r4()
simple_pats = views.from_view_definition(simple_pats_config)

如果在Jupyter笔记本或类似工具中运行上述代码,您会注意到视图构建器支持与给定配置文件中FHIR资源的字段匹配的表格建议。事实上,这只是一个Pythonic的方式构建用于运行器的FHIRPath表达式,通过按Tab键提供建议

tab suggestion image

该构建器对Python用户来说非常方便,但您也可以通过获取视图的字符串表示来直接查看FHIRPath表达式,例如通过运行 print(simple_pats)。注意,每一列和“where”条件都是由FHIRPath表达式定义的,而每一列必须在FHIRPath后附加的alias()函数中定义其名称。

View<http://hl7.org/fhir/StructureDefinition/Patient.select(
  id.alias(id),
  gender.alias(gender),
  birthDate.alias(birthdate),
  address.where(period.empty()).first().line.first().alias(street),
  address.where(period.empty()).first().city.alias(city),
  address.where(period.empty()).first().state.alias(state),
  address.where(period.empty()).first().postalCode.alias(zip)
).where(
  birthDate < @1960-01-01
)>

换句话说,任何runner实现基本上都会使用FHIRPath表达式来选择和过滤底层数据。下面的示例将使用BigQuery runner,它将FHIRPath表达式转换为SQL,但Apache Spark和直接在JSON上的runners也将遵循。这也可以导出为简单的JSON结构,并将其传递给远程服务以评估FHIRPath表达式并为用户生成视图。

现在我们已经定义了一个视图,让我们在真实数据集上运行它。我们将在大查询上运行它。

# Get a BigQuery client. This may require additional authentication to access
# BigQuery, depending on your notebook environment. Typically the client
# and runner are created only once at the start of a notebook.
from google.cloud import bigquery as bq
client = bq.Client()
runner = bigquery_runner.BigQueryRunner(
    client,
    fhir_dataset='bigquery-public-data.fhir_synthea',
    snake_case_resource_tables=True)

runner.to_dataframe(simple_pats, limit = 5)

这将产生这个表格

id 性别 出生日期 街道 城市 邮编
0 6759d2b7-38b4-4798-97c0-d171a53e013a 男性 1916-03-21 659 Bayer Wall Apt 61 波士顿 马萨诸塞州 02108
1 41dbee4d-d355-413f-a040-93ca037fe646 男性 1951-12-05 226 Sipes Ranch Unit 37 Lynnfield 马萨诸塞州 01940
2 e194d708-8989-4e0c-a8e1-eda7351672ce 男性 1947-09-24 638 Pouros Wall Suite 52 Lynnfield 马萨诸塞州 01940
3 4bccdc85-c040-45dd-ada3-a55064439a01 男性 1943-06-20 825 Jakubowski Extension Tewksbury 马萨诸塞州 01876
4 8dca4c3c-d2d5-460f-9168-5f18e5d29b2b 男性 1945-12-13 319 Cronin Light Hubbardston 马萨诸塞州 01452

这就完成了!现在返回的dataframe包含了一个表格,其中包含查询中描述的示例患者,这些患者是从BigQuery中存储的FHIR数据中提取的。下面的示例将展示更复杂的使用案例,例如将FHIR视图转换为BigQuery虚拟视图或包含来自代码值集的临床内容。

目前我们支持BigQuery runner以消耗BigQuery中的FHIR数据作为我们的数据源,但未来的runners可能支持其他数据存储、FHIR服务器或磁盘上的FHIR批量提取。

与代码值一起工作

对医疗保健数据的最有意义的分析涉及导航临床术语。在某些情况下,这些值集来自像值集权威中心这样的已建立权威机构,在其他时候,它们是为自定义用例定义和维护的。

FHIR Views提供了一个方便的机制来创建和使用此类值集在查询中。以下是一个定义一组表示LDL结果的LOINC代码的示例

LDL_TEST = r4.value_set('urn:example:value_set:ldl').with_codes(
    'http://loinc.org', ['18262-6', '18261-8', '12773-8']).build()

现在我们可以轻松地查询使用FHIRPath memberOf函数的视图中的观察结果

# Creates the base observation view for convenience, typically done once per
# base type in a notebook.
obs = views.view_of('Observation')

ldl_obs = obs.select([
    obs.subject.idFor('Patient').alias('patient'),
    # Below is a Pythonic shorthand -- users could type
    # `obs.value.ofType('Quantity').value` instead for the FHIRPath ofType
    # expression, but the shorthand helps autocompletion
    obs.valueQuantity.value.alias('value'),
    obs.valueQuantity.unit.alias('unit'),
    obs.code.coding.display.first().alias('test'),
    obs.effectiveDateTime.alias('effectiveTime')
    ]).where(obs.code.memberOf(LDL_TEST))

runner.to_dataframe(ldl_obs, limit=5)
患者 单位 测试 有效时间
0 903156da-ca5d-4ec3-ad36-073a9437afe4 153.058 mg/dL 低密度脂蛋白胆固醇 2014-06-20 11:30:15+00:00
1 3d268dce-fed4-4bc7-b156-c78e810c5183 149.379 mg/dL 低密度脂蛋白胆固醇 2013-06-10 16:20:36+00:00
2 fdf7c87b-1c8f-4d09-8d51-e622f747a7c8 88.047 mg/dL 低密度脂蛋白胆固醇 2013-10-07 00:08:45+00:00
3 9007c0ff-a0ad-48dc-adc2-c0908c06fba8 108.145 mg/dL 低密度脂蛋白胆固醇 2016-03-18 10:31:54+00:00
4 cc5a2dd6-37b6-4f15-9da7-53f3b85e3370 64.5849 mg/dL 低密度脂蛋白胆固醇 2012-05-26 13:27:46+00:00

与外部值集和术语服务一起工作

您还可以与外部术语服务定义的值集一起工作。为此,您必须首先创建一个术语服务客户端。

此示例使用NIH的UMLS术语服务。为了访问此术语服务,您需要在此处注册。然后,您应在下面的“your-umls-api-key”位置输入您的API密钥在您的个人资料页面上找到

from google.fhir.r4.terminology import terminology_service_client

tx_client = terminology_service_client.TerminologyServiceClient({
    'http://cts.nlm.nih.gov/fhir/': ('apikey', 'your-umls-api-key'),
})

在对外部定义的值集进行查询之前,您必须首先获取值集定义的代码并将它们写入BigQuery表。您只需要执行此步骤一次。完成此操作后,您将能够在未来的查询中引用您所编写的值集定义。

injury_value_set_url = 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1029.5'
wound_disorder_value_set_url = 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1219.178'
runner.materialize_value_set_expansion((injury_value_set_url, wound_disorder_value_set_url), tx_client)

要查询已保存到BigQuery的外部定义的值集,您只需引用其URL即可。

injury_conds =  cond.select([
    cond.id.alias('id'),
    cond.subject.idFor('Patient').alias('patientId'),
    cond.code.alias('codes')
    ]).where(cond.code.memberOf(injury_value_set_url))

runner.create_database_view(injury_conds, 'injury_conditions')

将FHIR Views保存为BigQuery视图

虽然runner.to_dataframe方便用于检索数据进行本地分析,但在BigQuery本身创建这种扁平化视图通常很有用。它们可以用更简单的SQL轻松查询,或被各种商业智能或其他数据分析工具使用。

因此,BigQueryRunner提供了一个create_database_view方法,它将视图定义转换为BigQuery 视图,然后就可以像使用普通表一样消费它,当底层数据更新时,视图也会更新。以下是一个示例

runner.create_database_view(ldl_obs, 'ldl_observations')

默认情况下,视图是在runner使用的fhir_dataset中创建的,但这并不总是期望的(例如,用户可能希望在他们的独立数据集中进行分析)。因此,在创建runner时通常指定view_dataset作为创建的任何视图的目标。以下是一个示例

runner = bigquery_runner.BigQueryRunner(
    client,
    fhir_dataset='bigquery-public-data.fhir_synthea',
    view_dataset='example_project.diabetic_care_example')

项目详情


下载文件

下载您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

此版本没有可用的源代码分发文件。请参阅生成分发存档的教程

构建分发

google_fhir_views-0.11.0-py3-none-any.whl (65.8 kB 查看散列值)

上传时间 Python 3

由以下组织支持