BigQuery¶
Requires pip install "ehr2rl[bigquery]". See the
BigQuery guide for prerequisites, costs, and limits.
Loading a dataset¶
- ehr2rl.bigquery.load_mimiciv_bigquery_dataset(client, cohort, preset, itemid_map, action_config)¶
Load a bounded MIMIC-IV cohort from BigQuery into an
EHRDataset.Validates the itemid map against live labels, then queries admissions, the preset’s chartevents and labevents, and inputevents for medication actions. Every query goes through
client’s budget guard.- Parameters:
client (
GuardedBigQueryClient) – Guarded client that runs every query.cohort (
BigQueryCohort) – Cohort definition.preset (
FeaturePreset) – Concepts to use as state features.itemid_map (
ItemIdMap) – Itemid map for features and medications. All of its itemids are validated.action_config (
ActionConfig) – Bins and windows for medication actions.
- Returns:
One trajectory per admission, with metadata
died,feature_names,action_names,action_sizes, andprovenance.- Return type:
- Raises:
EHRValidationError – If the map’s labels differ from MIMIC-IV or the preset names a missing concept.
BudgetExceededError – If any query’s dry run exceeds the byte cap.
BigQueryAuthError – If any query fails.
Cohorts¶
- class ehr2rl.bigquery.CohortCriteria(cohort_size=25, min_age=None, max_age=None, admission_types=(), min_icu_los_hours=None, max_icu_los_hours=None)¶
Filters that define a bounded ICU cohort.
Bounds are inclusive.
Noneor an empty tuple means no filter.- Parameters:
cohort_size (
int) – Maximum number of ICU stays. The first stays bysubject_id,hadm_id, andstay_idare taken, not a random sample.min_age (
int|None) – Minimum MIMIC-IVanchor_agein years.max_age (
int|None) – Maximum MIMIC-IVanchor_agein years.admission_types (
tuple[str,...]) – Allowedadmission_typevalues, such as"EW EMER.".min_icu_los_hours (
float|None) – Minimum ICU length of stay in hours.max_icu_los_hours (
float|None) – Maximum ICU length of stay in hours.
- Raises:
ValueError – If
cohort_sizeis not positive or a bound is negative.
- class ehr2rl.bigquery.BigQueryCohort(criteria, mimic_version='3_1')¶
Build bounded MIMIC-IV v3.1 SQL queries for a cohort.
Every query is restricted to the cohort’s ICU stays, and event queries keep only events during those stays.
- Parameters:
criteria (
CohortCriteria) – Cohort filters.mimic_version (
str) – Must be"3_1".
- Raises:
ValueError – If
mimic_versionis not"3_1".
- vitals_sql(itemids)¶
Return SQL for chartevents rows of the given itemids during cohort stays.
- labs_sql(itemids)¶
Return SQL for labevents rows of the given itemids during cohort stays.
Guarded execution¶
- class ehr2rl.bigquery.GuardedBigQueryClient(client, maximum_bytes_billed, cache_dir=None, query_timeout=None, disable_retries=False)¶
Run BigQuery queries only after a dry-run budget check.
- Parameters:
client (
Any) – Agoogle.cloud.bigquery.Client.maximum_bytes_billed (
int) – Per-query byte cap. A query whose dry run exceeds it is not run, and the cap is also set on each job so BigQuery enforces it server-side.cache_dir (
str|Path|None) – Directory for cached results, created if missing. Results are not cached whenNone.query_timeout (
float|None) – Seconds to wait for each API call and result download. Must be positive.disable_retries (
bool) – Turn off the Google client’s automatic retries.
- Raises:
ValueError – If
maximum_bytes_billedorquery_timeoutis not positive.
- query_dataframe(sql, *, cache_key_parts=())¶
Run
sqlafter cache and dry-run checks and return its results.- Parameters:
- Returns:
The results and execution metadata.
- Return type:
- Raises:
BudgetExceededError – If the dry-run estimate exceeds
maximum_bytes_billed.BigQueryAuthError – If the dry run or query fails for any reason, including credentials, permissions, invalid SQL, and timeouts.
- class ehr2rl.bigquery.QueryResult(dataframe, job_id, bytes_processed, query_hash)¶
A query’s results plus BigQuery execution metadata.
Errors¶
- exception ehr2rl.bigquery.BigQueryError¶
Base error for ehr2rl BigQuery operations.
- exception ehr2rl.bigquery.BudgetExceededError¶
Raised before execution when a dry run exceeds the configured budget.
- exception ehr2rl.bigquery.BigQueryAuthError¶
Raised when a BigQuery dry run or query fails.
Despite the name, this covers every failure from the BigQuery client, including invalid SQL, timeouts, and network errors, not only authentication and permissions.