Skip to content

Commit 562a895

Browse files
committed
comment out viewer cells
modify gitignore for service account keys run notebooks in their own temp folder add a step to create pull request to update colab images list
1 parent 1c1bc7d commit 562a895

File tree

5 files changed

+76
-40
lines changed

5 files changed

+76
-40
lines changed

.github/workflows/test_colab.yml

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@ name: Check Commits and Colab Images
33
on:
44
push:
55
branches: [ "master" ]
6+
paths:
7+
- ".github/workflows/test_colab.yml"
8+
- "notebooks/getting_started/part**.ipynb"
69
pull_request:
710
branches: [ "master" ]
11+
paths:
12+
- ".github/workflows/test_colab.yml"
13+
- "notebooks/getting_started/part**.ipynb"
814
workflow_dispatch:
915
schedule:
1016
- cron: 0 12 */1 * *
1117

1218
jobs:
13-
check_commits_and_images:
19+
check_colab_images:
1420
runs-on: ubuntu-latest
1521
permissions:
1622
contents: write
23+
pull-requests: write
1724

1825
steps:
1926
- name: Checkout code
@@ -28,15 +35,15 @@ jobs:
2835
run: pip install requests==2.31.0 pandas==2.1.1 google-cloud-bigquery==3.12.0 pyarrow==13.0.0 nbformat==5.9.2
2936

3037
- name: Authorize Google Cloud
38+
if: github.event_name != 'pull_request'
3139
uses: google-github-actions/auth@v1
3240
with:
3341
credentials_json: ${{ secrets.SERVICE_ACCOUNT_KEY }}
3442
create_credentials_file: true
3543
export_environment_variables: true
3644

37-
- name: Run check-commits.py and check-colab-images.py, preprocess notebooks scripts
45+
- name: Run check-colab-images.py and preprocess notebooks
3846
run: |
39-
python test/src/check-commits.py
4047
python test/src/check-colab-images.py
4148
python test/src/preProcessNotebooks.py
4249
@@ -60,59 +67,59 @@ jobs:
6067
swap-storage: true
6168

6269
- name: Docker login
70+
if: github.event_name != 'pull_request'
6371
uses: docker/login-action@v3
6472
with:
6573
username: ${{ secrets.DOCKER_USERNAME }}
6674
password: ${{ secrets.DOCKER_PASSWORD }}
6775

6876
- name: Pull from GCP and Push Docker image to Docker Hub
69-
if: env.RESULT == 'true'
77+
if: github.event_name != 'pull_request' && env.RESULT == 'true'
7078
run: |
7179
docker pull us-docker.pkg.dev/colab-images/public/runtime:latest
7280
docker tag us-docker.pkg.dev/colab-images/public/runtime:latest imagingdatacommons/idc-testing-colab:latest
7381
docker push imagingdatacommons/idc-testing-colab:latest
7482
7583
- name: Pull Docker image from Docker Hub
76-
if: env.RESULT == 'false'
84+
if: github.event_name == 'pull_request' || env.RESULT == 'false'
7785
run: |
7886
docker pull imagingdatacommons/idc-testing-colab:latest
7987
8088
- name: Copy Google Cloud credentials to Docker container
89+
if: github.event_name != 'pull_request'
8190
run: |
8291
CREDENTIALS_FILE_PATH="${{ env.GOOGLE_APPLICATION_CREDENTIALS }}"
8392
CREDENTIALS_FILE_NAME=$(basename "$CREDENTIALS_FILE_PATH")
8493
GOOGLE_APPLICATION_CREDENTIALS="/content/$CREDENTIALS_FILE_NAME"
8594
echo "GOOGLE_APPLICATION_CREDENTIALS=$GOOGLE_APPLICATION_CREDENTIALS" >> $GITHUB_ENV
8695
8796
- name: Run notebook with papermill
97+
if: github.event_name != 'pull_request'
8898
run: |
8999
for nb in part1_prerequisites part2_searching_basics part3_exploring_cohorts; do
100+
# Create a new directory for each notebook
101+
sudo mkdir -p "temp/${nb}"
102+
90103
docker run -d --name colab -v "$(pwd):/content" -e GOOGLE_APPLICATION_CREDENTIALS="${{ env.GOOGLE_APPLICATION_CREDENTIALS }}" imagingdatacommons/idc-testing-colab:latest
91104
docker exec -t colab /bin/bash -c "pip install papermill==2.4.0"
92-
docker exec -t colab /bin/bash -c "set -o xtrace && set -o errexit && set -o pipefail && set -o nounset && set +o errexit && cd content/ && papermill --log-level='DEBUG' /content/notebooks/getting_started/${nb}.ipynb /content/test/outputs/${nb}_papermill_output.ipynb && set -o errexit && ls -A"
93-
#docker exec -t colab /bin/bash -c "jupyter nbconvert --to html --ExtractOutputPreprocessor.enabled=False /content/test/outputs/output_${nb}.ipynb"
105+
106+
# Change the working directory to the new directory
107+
docker exec -t colab /bin/bash -c "set -o xtrace && set -o errexit && set -o pipefail && set -o nounset && set +o errexit && cd /content/temp/${nb} && papermill --log-level='DEBUG' /content/notebooks/getting_started/${nb}.ipynb /content/test/outputs/${nb}_papermill_output.ipynb && set -o errexit && ls -A"
108+
94109
docker stop colab
95110
docker rm colab
111+
sudo rm -r "temp/${nb}"
96112
done
97-
98-
# skip commit
99-
#- name: Commit changes
100-
# if: ${{ github.event_name != 'pull_request' }}
101-
# uses: stefanzweifel/git-auto-commit-action@v4
102-
# with:
103-
# commit_message: 'Check colab env'
104-
# file_pattern: 'test/*.csv test/outputs/*.ipynb'
105-
# branch: 'master'
106-
107-
#- name: Check output notebooks for errors
108-
# run: |
109-
# for nb in part1_prerequisites part2_searching_basics part3_exploring_cohorts; do
110-
# if grep -q '"name": "stderr"\|"status": "failed"' test/outputs/output_${nb}.ipynb; then
111-
# echo "Error messages found in the ${nb} notebook output:"
112-
# cat test/outputs/output_${nb}.ipynb
113-
# exit 1
114-
# else
115-
# echo "No errors found in the ${nb} notebook output."
116-
# fi
117-
# done
118-
# exit $EXIT_CODE
113+
114+
- name: Create Pull Request to update the colab image metadata database
115+
if: github.event_name != 'pull_request'
116+
uses: peter-evans/create-pull-request@v5
117+
with:
118+
title: Update colab images list
119+
body: Update colab-images-list.csv so that when a colab image is updated, we do not keep pulling colab docker image from gcp and pushing to dockerhub everytime
120+
base: master
121+
branch: update-colab-images-list
122+
add-paths: |
123+
test/colab-images-list.csv
124+
env:
125+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ __pycache__/
55
# Pycharm project files
66
.idea
77
.idea/*
8+
9+
10+
gha-creds-**.json

test/colab-images-list.csv

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
date,tag,sha256,docker_pull_tag,docker_pull_sha256_tag
2-
,latest,sha256:4a26494c9c92ab4d0515e0715d79dfecbe8cfacb9b86fcfd55bc0274cb89530d,us-docker.pkg.dev/colab-images/public/runtime:latest,us-docker.pkg.dev/colab-images/public/runtime@sha256:4a26494c9c92ab4d0515e0715d79dfecbe8cfacb9b86fcfd55bc0274cb89530d
2+
,latest,sha256:9634a9786cde027a5c115a2a2df31aebe0c9ef9a4a06951cb32bcf6edec84ef7,us-docker.pkg.dev/colab-images/public/runtime:latest,us-docker.pkg.dev/colab-images/public/runtime@sha256:9634a9786cde027a5c115a2a2df31aebe0c9ef9a4a06951cb32bcf6edec84ef7
33
20230515,release-colab-20230515-060150-RC00,sha256:3a8fc58f7e81b96dc59a2fb48b7973802f59fdd634fb538569228d830a7e76a9,us-docker.pkg.dev/colab-images/public/runtime:release-colab-20230515-060150-RC00,us-docker.pkg.dev/colab-images/public/runtime@sha256:3a8fc58f7e81b96dc59a2fb48b7973802f59fdd634fb538569228d830a7e76a9
44
20230622,release-colab-20230622-060123-RC01,sha256:7dac57e02aae4e83aab349563190a71bdd07374e1365f53bd6a50280046c6091,us-docker.pkg.dev/colab-images/public/runtime:release-colab-20230622-060123-RC01,us-docker.pkg.dev/colab-images/public/runtime@sha256:7dac57e02aae4e83aab349563190a71bdd07374e1365f53bd6a50280046c6091
55
20230711,release-colab-20230711-060203-RC00,sha256:53dc33f450cd162d8a42c5aff02d50ac24eb9fc68be77f0374614ad07247e9cd,us-docker.pkg.dev/colab-images/public/runtime:release-colab-20230711-060203-RC00,us-docker.pkg.dev/colab-images/public/runtime@sha256:53dc33f450cd162d8a42c5aff02d50ac24eb9fc68be77f0374614ad07247e9cd
66
20230803,release-colab-20230803-060151-RC00,sha256:ae8a5bf22a84c67fb4b35aa4b1f19dac94b01b56a97c5c7bb15db57552e8d38c,us-docker.pkg.dev/colab-images/public/runtime:release-colab-20230803-060151-RC00,us-docker.pkg.dev/colab-images/public/runtime@sha256:ae8a5bf22a84c67fb4b35aa4b1f19dac94b01b56a97c5c7bb15db57552e8d38c
77
20230921,release-colab_20230921-060057_RC00,sha256:4a26494c9c92ab4d0515e0715d79dfecbe8cfacb9b86fcfd55bc0274cb89530d,us-docker.pkg.dev/colab-images/public/runtime:release-colab_20230921-060057_RC00,us-docker.pkg.dev/colab-images/public/runtime@sha256:4a26494c9c92ab4d0515e0715d79dfecbe8cfacb9b86fcfd55bc0274cb89530d
8+
20231214,release-colab_20231214-060137_RC00,sha256:9634a9786cde027a5c115a2a2df31aebe0c9ef9a4a06951cb32bcf6edec84ef7,us-docker.pkg.dev/colab-images/public/runtime:release-colab_20231214-060137_RC00,us-docker.pkg.dev/colab-images/public/runtime@sha256:9634a9786cde027a5c115a2a2df31aebe0c9ef9a4a06951cb32bcf6edec84ef7

test/src/check-colab-images.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def main():
4848
if existing_sha256 != current_sha256:
4949
print("SHA256 digest has changed!")
5050
# Perform actions if the digest has changed, like updating GitHub Actions.
51-
df.to_csv('database.csv', index=False) # Save the new DataFrame as 'database.csv'
51+
df.to_csv('test/colab-images-list.csv', index=False) # Save the new DataFrame as 'database.csv'
5252
print("Updated the database CSV.")
5353
result = True
5454
else:

test/src/preProcessNotebooks.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import subprocess
22
import nbformat
33

4+
45
def modify_notebook(file_name, project_id, query1, query2):
56
# Load the notebook
67
with open(file_name) as f:
@@ -10,26 +11,50 @@ def modify_notebook(file_name, project_id, query1, query2):
1011
for cell in nb.cells:
1112
if cell.cell_type == "code":
1213
# Replace the line if it exists
13-
cell.source = cell.source.replace('my_ProjectID = "" #@param {type:"string"}', f"#this project_id is injected for testing\nmy_ProjectID='{project_id}'")
14+
cell.source = cell.source.replace(
15+
'my_ProjectID = "" #@param {type:"string"}',
16+
f"#this project_id is injected for testing\nmy_ProjectID='{project_id}'",
17+
)
1418
# Comment out another line
15-
cell.source = cell.source.replace('auth.authenticate_user()', '#while testing, the authentication is handled by using application default credentials\n#auth.authenticate_user()')
16-
cell.source = cell.source.replace('REPLACE THIS TEXT WITH YOUR QUERY!', query1)
17-
cell.source = cell.source.replace('# write the selection criteria under this line!', query2)
19+
cell.source = cell.source.replace(
20+
"auth.authenticate_user()",
21+
"#while testing, the authentication is handled by using application default credentials\n#auth.authenticate_user()",
22+
)
23+
cell.source = cell.source.replace(
24+
"REPLACE THIS TEXT WITH YOUR QUERY!", query1
25+
)
26+
cell.source = cell.source.replace(
27+
"# write the selection criteria under this line!", query2
28+
)
29+
cell.source = (
30+
"# " + cell.source.replace("\n", "\n# ")
31+
if cell.source.lower().startswith("viewer")
32+
else cell.source
33+
)
1834

1935
# Write the notebook back to disk
20-
with open(file_name, 'w') as f:
36+
with open(file_name, "w") as f:
2137
nbformat.write(nb, f)
2238

39+
2340
# Command to run
2441
files = [
2542
"notebooks/getting_started/part1_prerequisites.ipynb",
2643
"notebooks/getting_started/part2_searching_basics.ipynb",
27-
"notebooks/getting_started/part3_exploring_cohorts.ipynb"
44+
"notebooks/getting_started/part3_exploring_cohorts.ipynb",
2845
]
2946

3047
for file_name in files:
3148

32-
project_id = 'idc-external-025'
33-
query1 = '#this query is injected for testing\n SELECT DISTINCT collection_tumorLocation FROM `bigquery-public-data.idc_current.dicom_all` ' if 'part2' in file_name else ''
34-
query2 = '#this query is injected for testing\n MODALITY=\'MR\' AND collection_tumorLocation=\'Lung\'' if 'part2' in file_name else ''
49+
project_id = "idc-external-025"
50+
query1 = (
51+
"#this query is injected for testing\n SELECT DISTINCT collection_tumorLocation FROM `bigquery-public-data.idc_current.dicom_all` "
52+
if "part2" in file_name
53+
else ""
54+
)
55+
query2 = (
56+
"#this query is injected for testing\n MODALITY='MR' AND collection_tumorLocation='Lung'"
57+
if "part2" in file_name
58+
else ""
59+
)
3560
modify_notebook(file_name, project_id, query1, query2)

0 commit comments

Comments
 (0)