Skip to main content

fhir4ds

The fhir4ds package is the unified entry point for the toolkit. It orchestrates sub-packages to provide a seamless high-level API for FHIR data science.

1. At a Glance

The most common entry points are exposed directly at the package root:

from fhir4ds import (
create_connection, # Get a pre-configured connection
register, # Register UDFs on an existing connection
evaluate_measure, # End-to-end quality measure evaluation
generate_view_sql, # ViewDefinition → SQL
)

2. Function Reference

create_connection

create_connection(database=":memory:", *, allow_unsigned_extensions=True, register_udfs=True, **kwargs) -> duckdb.DuckDBPyConnection

The recommended way to start. Returns a DuckDB connection with all FHIRPath and CQL extensions pre-registered.

ParameterTypeDefaultDescription
databasestr":memory:"Path to the DuckDB file.
allow_unsigned_extensionsboolTrueAllow loading unsigned (dev-build) DuckDB extensions.
register_udfsboolTrueAutomatically register all UDFs on the connection.
valueset_cachedictNonePre-expanded terminology map for in_valueset.
**kwargsAny-Additional arguments passed to duckdb.connect().
import fhir4ds
con = fhir4ds.create_connection()

register

register(con, *, valueset_cache=None) -> dict

Prepares an existing DuckDB connection for FHIR4DS tasks. Prefers C++ extensions but falls back to Python UDFs.

ParameterTypeDescription
conduckdb.DuckDBPyConnectionThe connection to prepare.
valueset_cachedict(Optional) Pre-expanded terminology map.

Returns: dict showing which C++ extensions were successfully loaded.


evaluate_measure

evaluate_measure(library_path, conn, *, output_columns=None, parameters=None) -> duckdb.DuckDBPyRelation

The primary API for quality measure evaluation. Translates CQL to SQL and executes it in one step.

ParameterTypeDescription
library_pathstrPath to the CQL library file.
connduckdb.DuckDBPyConnectionThe database connection.
output_columnsdict(Optional) Mapping of SQL column names to CQL definition names.
parametersdict(Optional) Key-value pairs for CQL parameters (e.g. Measurement Period).
result = fhir4ds.evaluate_measure(
"CMS165.cql",
con,
parameters={"Measurement Period": ("2025-01-01", "2025-12-31")}
)
df = result.df()

generate_view_sql

generate_view_sql(view_definition) -> str

Generates optimized DuckDB SQL from a declarative SQL-on-FHIR v2 ViewDefinition.

ParameterTypeDescription
view_definition`dictstr`
sql = fhir4ds.generate_view_sql(my_view_json)
df = con.execute(sql).df()

3. Sub-Package Access

Advanced users can access the underlying specialized modules directly:

  • fhir4ds.fhirpath: Low-level expression evaluation.
  • fhir4ds.cql: Advanced translator and ELM handling.
  • fhir4ds.viewdef: ViewDefinition parser and AST access.
  • fhir4ds.dqm: Detailed audit evidence orchestration.