Skip to content

Commit 609b4d1

Browse files
committed
simplify the query to get all patients that have clinical data
1 parent b3dbbd3 commit 609b4d1

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

notebooks/clinical_data_intro.ipynb

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"\n",
3131
"Prepared: July 2022\n",
3232
"\n",
33-
"Updated: Dec 2022"
33+
"Updated: May 2023"
3434
]
3535
},
3636
{
@@ -76,7 +76,7 @@
7676
},
7777
{
7878
"cell_type": "code",
79-
"execution_count": null,
79+
"execution_count": 2,
8080
"metadata": {
8181
"id": "o8WdiIiBQwav"
8282
},
@@ -712,7 +712,9 @@
712712
"\n",
713713
"Sometime you may want to know whether specific patient has any clinical data available. One way to do this is to locate the collection that patient belongs to, and then check whether any of the clinical data tables (if any) that are available for that collection have that patient identifier.\n",
714714
"\n",
715-
"Alternatively, we can build a complete list of patients that have clinical data by performing a union on all of the `dicom_patient_id` columns across all of the clinical data tables, which is what we do in the next cell."
715+
"Alternatively, we can build a complete list of patients that have clinical data by performing a union on all of the `dicom_patient_id` columns across all of the clinical data tables, which is what we do in the next cell.\n",
716+
"\n",
717+
"In this query we use the ability of BigQuery to [query multiple tables using a wildcard table](https://cloud.google.com/bigquery/docs/querying-wildcard-tables). Note that here we refer to the specific version of the data, since `idc_current_clinical` dataset contains views, which cannot be queried through prefix."
716718
]
717719
},
718720
{
@@ -725,30 +727,20 @@
725727
"source": [
726728
"import re\n",
727729
"\n",
728-
"all_clinical_tables = column_metadata_df[\"table_name\"].unique()\n",
729-
"query = \"with patients_unionized as (SELECT dicom_patient_id FROM \"+re.sub(\"idc_v[0-9]*_clinical\", \"idc_current_clinical\", all_clinical_tables[0])\n",
730-
"for clinical_table in all_clinical_tables[1:]:\n",
731-
" query = query+\" UNION ALL SELECT dicom_patient_id FROM \"+re.sub(\"idc_v[0-9]*_clinical\", \"idc_current_clinical\", clinical_table)\n",
732-
"\n",
733-
"selection_query = query+\") select distinct(dicom_patient_id) from patients_unionized\"\n",
734-
"\n",
735-
"#print(selection_query)\n",
730+
"selection_query = \"\"\"\n",
731+
"SELECT\n",
732+
" DISTINCT(dicom_patient_id)\n",
733+
"FROM\n",
734+
" `bigquery-public-data.idc_v14_clinical.*`\n",
735+
"WHERE\n",
736+
" _TABLE_SUFFIX NOT IN (\"table_metadata\",\n",
737+
" \"column_metadata\" )\n",
738+
"\"\"\"\n",
736739
"\n",
737740
"selection_result = bq_client.query(selection_query)\n",
738-
"patients_df = selection_result.result().to_dataframe()\n"
739-
]
740-
},
741-
{
742-
"cell_type": "code",
743-
"execution_count": null,
744-
"metadata": {
745-
"id": "Ca63J0HWiXjH"
746-
},
747-
"outputs": [],
748-
"source": [
749-
"patients = patients_df[\"dicom_patient_id\"].unique().tolist()\n",
741+
"patients_df = selection_result.result().to_dataframe()\n",
750742
"\n",
751-
"print(\"\\n\".join(patients))"
743+
"patients_df\n"
752744
]
753745
},
754746
{

0 commit comments

Comments
 (0)