<jupyter_start><jupyter_text>Using `ibis` to connect to MIMIC IV on Google BigQueryEnvironments in Google Colaboratory are not persistent. If we use any software that is not part of teh Google Python Colaboratory environment, we must install it during each session.We are going to be using Ibis, so this must be installed.<jupyter_code>!pip install ibis-framework[bigquery]
import ibis
from google_auth_oauthlib import flow
import os<jupyter_output><empty_output><jupyter_text>Authenticate using `google.colab`<jupyter_code>from google.colab import auth
auth.authenticate_user()
print('Authenticated')<jupyter_output><empty_output><jupyter_text>This is copied straight from the Google documentation.If you haven't verified the app with Google you get some security warnings you need to click through.<jupyter_code># TODO: Uncomment the line below to set the `launch_browser` variable.
launch_browser = True
#
# The `launch_browser` boolean variable indicates if a local server is used
# as the callback URL in the auth flow. A value of `True` is recommended,
# but a local server does not work if accessing the application remotely,
# such as over SSH or from a remote Jupyter notebook.

appflow = flow.InstalledAppFlow.from_client_secrets_file(
    secret, scopes=["https://www.googleapis.com/auth/bigquery"]
)

if launch_browser:
    appflow.run_local_server()
else:
    appflow.run_console()

credentials = appflow.credentials<jupyter_output>Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=351306700747-5t1ni8s29h7mug06vls19q52pbma4f8e.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fbigquery&state=kJe2M31F9LoWG1JZVokDk4RMjuXHXh&access_type=offline<jupyter_text>Pass the credentials to the `ibis` `connect` function.<jupyter_code>conn = ibis.bigquery.connect(
    project_id="mimic4-bq",
    dataset_id='physionet-data.mimic_core')<jupyter_output><empty_output><jupyter_text>Once we connect we can list all the databases we have access to<jupyter_code>conn.list_databases()<jupyter_output><empty_output><jupyter_text>Since I connected to `mimic_core`, I can list the tables in this database<jupyter_code>conn.list_tables()
patients = conn.table("patients")
patients.schema()<jupyter_output><empty_output><jupyter_text>And do queries<jupyter_code>patients.execute(limit=20)
conn.table("admissions").execute(limit=20)
conn.table("transfers").execute(limit=20)<jupyter_output><empty_output><jupyter_text>I can connect to another databasae<jupyter_code>conn2 = ibis.bigquery.connect(
    project_id="mimic4-bq",
    dataset_id='physionet-data.mimic_derived',
    credentials=credentials)
conn2.list_tables()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09081119885139986, 'fraction_numerical': 0.020459440057430008, 'mean_word_length': 7.628482972136223, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '118417', 'license': 'MIT', 'n_tokens_mistral': 973, 'n_tokens_neox': 934, 'n_words': 219, 'path': 'notebooks/connect_to_mimiciv_colab.ipynb', 'repo_name': 'chapmanbe/google', 'size': '2786'}