|
1 | 1 | { |
2 | 2 | "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "id": "view-in-github", |
| 7 | + "colab_type": "text" |
| 8 | + }, |
| 9 | + "source": [ |
| 10 | + "<a href=\"https://colab.research.google.com/github/ImagingDataCommons/IDC-Tutorials/blob/master/notebooks/getting_started/part3_exploring_cohorts.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" |
| 11 | + ] |
| 12 | + }, |
3 | 13 | { |
4 | 14 | "cell_type": "markdown", |
5 | 15 | "metadata": { |
|
23 | 33 | "---\n", |
24 | 34 | "Initial version: Nov 2022\n", |
25 | 35 | "\n", |
26 | | - "Updated: " |
| 36 | + "Updated: May 2023" |
27 | 37 | ] |
28 | 38 | }, |
29 | 39 | { |
|
105 | 115 | "cell_type": "code", |
106 | 116 | "execution_count": null, |
107 | 117 | "metadata": { |
108 | | - "colab": { |
109 | | - "base_uri": "https://localhost:8080/", |
110 | | - "height": 424 |
111 | | - }, |
112 | | - "id": "sVBOII5GP9cw", |
113 | | - "outputId": "fc199d98-c22c-4d88-a89a-962698ddd4ee" |
| 118 | + "id": "sVBOII5GP9cw" |
114 | 119 | }, |
115 | 120 | "outputs": [], |
116 | 121 | "source": [ |
|
160 | 165 | "\n", |
161 | 166 | "While defining your cohort, you may be looking for subsets of collections, patients or studies that meet your search critieria. But when it comes to downloading the cohort, you will always need to get the URLs of the individual files from the cohort.\n", |
162 | 167 | "\n", |
163 | | - "The following cell uses the query from the above to get the list of study identifiers that meet our search critieria, and then selects all of the rows correspoing to the DICOM instances (files)that are included in those studies. The column that contains the URI that can be used to download the file is in the `gcs_url` column. We will also query the `instance_size` column, which we can use to calculate the size of the files corresponding to the cohort.\n", |
| 168 | + "The following cell uses the query from the above to get the list of study identifiers that meet our search critieria, and then selects all of the rows correspoing to the DICOM instances (files)that are included in those studies. The column that contains the URI that can be used to download the file from Google GCS bucket is in the `gcs_url` column, while the location of that same file in Amazon AWS bucket is in the `aws_url` column. We will also query the `instance_size` column, which we can use to calculate the size of the files corresponding to the cohort.\n", |
164 | 169 | "\n", |
165 | 170 | "As we learned in the part 2 of this tutorial series, each row of the IDC metadata table corresponds to a single DICOM file, and the attributes we mentioned above are assigned at the granularity of the individual files.\n", |
166 | 171 | "\n", |
|
171 | 176 | "cell_type": "code", |
172 | 177 | "execution_count": null, |
173 | 178 | "metadata": { |
174 | | - "colab": { |
175 | | - "base_uri": "https://localhost:8080/", |
176 | | - "height": 441 |
177 | | - }, |
178 | | - "id": "D4dkFpEVfDO1", |
179 | | - "outputId": "8a5c6f34-d92f-4d6c-dccf-792819882901" |
| 179 | + "id": "D4dkFpEVfDO1" |
180 | 180 | }, |
181 | 181 | "outputs": [], |
182 | 182 | "source": [ |
|
189 | 189 | "selection_query = \"\"\"\n", |
190 | 190 | "SELECT\n", |
191 | 191 | " gcs_url,\n", |
| 192 | + " aws_url,\n", |
192 | 193 | " instance_size\n", |
193 | 194 | "FROM\n", |
194 | 195 | " bigquery-public-data.idc_current.dicom_all\n", |
|
238 | 239 | "id": "ojgbk_nMhbv0" |
239 | 240 | }, |
240 | 241 | "source": [ |
241 | | - "Now that we have URLs of the individual files, we can use `gsutil` command line tool from Google Cloud SDK to download them. When using Colab, Google Cloud SDK is pre-installed, but if you want to download files to your computer directly you will need to install SDK first! Note that download instructions are documented [here](https://learn.canceridc.dev/data/downloading-data).\n", |
| 242 | + "Now that we have URLs of the individual files, we can use the open source `s5cmd` command line tool to download the files either from the GCS or AWS locations. Note that download instructions are documented [here](https://learn.canceridc.dev/data/downloading-data).\n", |
242 | 243 | "\n", |
243 | | - "To download the files, we will first save GCS URLs into the manifest file, and then pass that manifest to `gsutil`." |
| 244 | + "[s5cmd](https://github.com/peak/s5cmd) is an open source very fast S3 and local filesystem execution tool, which, as experiments showed, is significantly faster than the Google-provided `gsutil`. Let's first install it." |
244 | 245 | ] |
245 | 246 | }, |
246 | 247 | { |
247 | 248 | "cell_type": "code", |
248 | 249 | "execution_count": null, |
249 | 250 | "metadata": { |
250 | | - "colab": { |
251 | | - "base_uri": "https://localhost:8080/" |
252 | | - }, |
253 | | - "id": "8CVLUiMMijjE", |
254 | | - "outputId": "3afc6cb2-5fea-4785-e900-65eaddeffc53" |
| 251 | + "id": "mr-F6YXrWHOm" |
255 | 252 | }, |
256 | 253 | "outputs": [], |
257 | 254 | "source": [ |
258 | | - "selection_df[\"gcs_url\"].to_csv(\"manifest.txt\", header=False, index=False)\n", |
| 255 | + "!wget https://github.com/peak/s5cmd/releases/download/v2.0.0/s5cmd_2.0.0_Linux-64bit.tar.gz && tar zxf s5cmd_2.0.0_Linux-64bit.tar.gz\n", |
259 | 256 | "\n", |
260 | | - "!rm -rf downloaded_cohort_files && mkdir downloaded_cohort_files\n", |
261 | | - "!cat manifest.txt | gsutil -m cp -I downloaded_cohort_files" |
262 | | - ] |
263 | | - }, |
264 | | - { |
265 | | - "cell_type": "markdown", |
266 | | - "metadata": { |
267 | | - "id": "9yJD5pUSjXo0" |
268 | | - }, |
269 | | - "source": [ |
270 | | - "Once the cell above is done, you can expand the left-side \"Files\" panel in the Colab interface and confirm that `downloaded_cohort_files` is not empty." |
| 257 | + "!./s5cmd --help" |
271 | 258 | ] |
272 | 259 | }, |
273 | 260 | { |
274 | 261 | "cell_type": "markdown", |
275 | | - "metadata": { |
276 | | - "id": "70hNEc71T_VZ" |
277 | | - }, |
278 | 262 | "source": [ |
279 | | - "### Faster download using `s5cmd`\n", |
280 | | - "\n", |
281 | | - "[s5cmd](https://github.com/peak/s5cmd) is an open source very fast S3 and local filesystem execution tool, which, as experiments showed, is significantly faster than the Google-provided `gsutil`." |
282 | | - ] |
283 | | - }, |
284 | | - { |
285 | | - "cell_type": "code", |
286 | | - "execution_count": null, |
| 263 | + "In order to use `s5cmd` we need to create the manifest that contains the commands to download individual files. Note that the `gs` bucket prefix for the Google GCS locations is replaced with `s3`. This is because `s5cmd` is a provider-agnostic tool that implements S3 API, and Google GCS buckets support S3 API." |
| 264 | + ], |
287 | 265 | "metadata": { |
288 | | - "colab": { |
289 | | - "base_uri": "https://localhost:8080/" |
290 | | - }, |
291 | | - "id": "mr-F6YXrWHOm", |
292 | | - "outputId": "de9cdb9c-af7a-44ac-8005-3e4076914a0a" |
293 | | - }, |
294 | | - "outputs": [], |
295 | | - "source": [ |
296 | | - "!wget https://github.com/peak/s5cmd/releases/download/v2.0.0/s5cmd_2.0.0_Linux-64bit.tar.gz && tar zxf s5cmd_2.0.0_Linux-64bit.tar.gz\n", |
297 | | - "\n", |
298 | | - "!./s5cmd --help" |
299 | | - ] |
| 266 | + "id": "gdrjpXYM--IH" |
| 267 | + } |
300 | 268 | }, |
301 | 269 | { |
302 | 270 | "cell_type": "code", |
303 | | - "execution_count": 6, |
| 271 | + "execution_count": 3, |
304 | 272 | "metadata": { |
305 | 273 | "id": "gZhsxNbuWXy7" |
306 | 274 | }, |
|
314 | 282 | "\n", |
315 | 283 | "selection_query = \"\"\"\n", |
316 | 284 | "SELECT\n", |
317 | | - " CONCAT(\"cp \",REPLACE(gcs_url, \"gs://\", \"s3://\"), \" .\") as s5cmd_command,\n", |
| 285 | + " CONCAT(\"cp \",REPLACE(gcs_url, \"gs://\", \"s3://\"), \" .\") as s5cmd_gcs_command,\n", |
| 286 | + " CONCAT(\"cp \",aws_url, \" .\") as s5cmd_aws_command,\n", |
318 | 287 | " StudyInstanceUID,\n", |
319 | 288 | "FROM\n", |
320 | 289 | " bigquery-public-data.idc_current.dicom_all\n", |
|
354 | 323 | "\n" |
355 | 324 | ] |
356 | 325 | }, |
| 326 | + { |
| 327 | + "cell_type": "markdown", |
| 328 | + "source": [ |
| 329 | + "Next two cells demonstrate how to generate a manifest and download the files corresponding to your selection from either GCS or AWS buckets." |
| 330 | + ], |
| 331 | + "metadata": { |
| 332 | + "id": "M94EtV6jBnXg" |
| 333 | + } |
| 334 | + }, |
357 | 335 | { |
358 | 336 | "cell_type": "code", |
359 | 337 | "execution_count": null, |
360 | 338 | "metadata": { |
361 | | - "colab": { |
362 | | - "base_uri": "https://localhost:8080/" |
363 | | - }, |
364 | | - "id": "DvFRdvyPWpAE", |
365 | | - "outputId": "8c7d2194-78cd-4ff3-93ff-33546cf51197" |
| 339 | + "id": "DvFRdvyPWpAE" |
366 | 340 | }, |
367 | 341 | "outputs": [], |
368 | 342 | "source": [ |
369 | | - "selection_df[\"s5cmd_command\"].to_csv(\"/content/s5cmd_manifest.txt\", header=False, index=False)\n", |
| 343 | + "# Download files from GCS\n", |
| 344 | + "selection_df[\"s5cmd_gcs_command\"].to_csv(\"/content/s5cmd_gcs_manifest.txt\", header=False, index=False)\n", |
370 | 345 | "\n", |
371 | | - "!rm -rf downloaded_cohort_files && mkdir downloaded_cohort_files\n", |
372 | | - "!cd downloaded_cohort_files && /content/s5cmd --no-sign-request --endpoint-url https://storage.googleapis.com run /content/s5cmd_manifest.txt" |
| 346 | + "!rm -rf gcs_downloaded_cohort_files && mkdir gcs_downloaded_cohort_files\n", |
| 347 | + "!cd gcs_downloaded_cohort_files && /content/s5cmd --no-sign-request --endpoint-url https://storage.googleapis.com run /content/s5cmd_gcs_manifest.txt" |
373 | 348 | ] |
374 | 349 | }, |
| 350 | + { |
| 351 | + "cell_type": "code", |
| 352 | + "source": [ |
| 353 | + "# Download files from AWS\n", |
| 354 | + "selection_df[\"s5cmd_aws_command\"].to_csv(\"/content/s5cmd_aws_manifest.txt\", header=False, index=False)\n", |
| 355 | + "\n", |
| 356 | + "!rm -rf aws_downloaded_cohort_files && mkdir aws_downloaded_cohort_files\n", |
| 357 | + "!cd aws_downloaded_cohort_files && /content/s5cmd --no-sign-request --endpoint-url https://s3.amazonaws.com run /content/s5cmd_aws_manifest.txt" |
| 358 | + ], |
| 359 | + "metadata": { |
| 360 | + "id": "e6BjWfTTC5kS" |
| 361 | + }, |
| 362 | + "execution_count": null, |
| 363 | + "outputs": [] |
| 364 | + }, |
375 | 365 | { |
376 | 366 | "cell_type": "markdown", |
377 | 367 | "metadata": { |
|
394 | 384 | "cell_type": "code", |
395 | 385 | "execution_count": null, |
396 | 386 | "metadata": { |
397 | | - "colab": { |
398 | | - "base_uri": "https://localhost:8080/", |
399 | | - "height": 206 |
400 | | - }, |
401 | | - "id": "bW5ULTmfpa7g", |
402 | | - "outputId": "90d45223-f381-4d3f-e2f5-6eec2b653951" |
| 387 | + "id": "bW5ULTmfpa7g" |
403 | 388 | }, |
404 | 389 | "outputs": [], |
405 | 390 | "source": [ |
|
460 | 445 | "cell_type": "code", |
461 | 446 | "execution_count": null, |
462 | 447 | "metadata": { |
463 | | - "colab": { |
464 | | - "base_uri": "https://localhost:8080/" |
465 | | - }, |
466 | | - "id": "rmEGwPyUqrYr", |
467 | | - "outputId": "548d7f8c-4edc-4504-a671-bfb59c5c9331" |
| 448 | + "id": "rmEGwPyUqrYr" |
468 | 449 | }, |
469 | 450 | "outputs": [], |
470 | 451 | "source": [ |
|
479 | 460 | "source": [ |
480 | 461 | "You can observe that there are two rows for the collection `qin_lung_ct`, which highlights important point that `collection_id` should be treated as a label grouping together both the items released by the original contributors of what initially formed the collection, but also the analysis results of the data in the original collection that might be contributed later. \n", |
481 | 462 | "\n", |
482 | | - "\n", |
| 463 | + "\n", |
483 | 464 | "\n", |
484 | 465 | "In the example above, [`qin_lung_ct` collection ](https://doi.org/10.7937/K9/TCIA.2015.NPGZYZBZ) was complemented by the [segmentations of the lung nodules](https://doi.org/10.7937/K9/TCIA.2015.PF0M9REI), with both images and segmentations becoming part of the same collection, but having distinct DOIs and attribution requirements." |
485 | 466 | ] |
|
512 | 493 | "cell_type": "code", |
513 | 494 | "execution_count": null, |
514 | 495 | "metadata": { |
515 | | - "colab": { |
516 | | - "base_uri": "https://localhost:8080/" |
517 | | - }, |
518 | | - "id": "JVIqa3GWUV64", |
519 | | - "outputId": "0df93427-00b0-43c1-d9b9-1f12676cb49a" |
| 496 | + "id": "JVIqa3GWUV64" |
520 | 497 | }, |
521 | 498 | "outputs": [], |
522 | 499 | "source": [ |
|
527 | 504 | ] |
528 | 505 | }, |
529 | 506 | { |
530 | | - "attachments": {}, |
531 | 507 | "cell_type": "markdown", |
532 | 508 | "metadata": { |
533 | 509 | "id": "-zRHvX1ZK-Dn" |
|
539 | 515 | ] |
540 | 516 | }, |
541 | 517 | { |
542 | | - "attachments": {}, |
543 | 518 | "cell_type": "markdown", |
544 | | - "metadata": {}, |
| 519 | + "metadata": { |
| 520 | + "id": "pvcX4ZhS-OEu" |
| 521 | + }, |
545 | 522 | "source": [ |
546 | 523 | "We can use itkwidgets to view the full 3D model or view slices as well as choose whether or not we'd like to display segmentation label maps with our image data.\n", |
547 | 524 | "\n", |
|
552 | 529 | "cell_type": "code", |
553 | 530 | "execution_count": null, |
554 | 531 | "metadata": { |
555 | | - "colab": { |
556 | | - "base_uri": "https://localhost:8080/" |
557 | | - }, |
558 | | - "id": "Bx9snM-MK-v9", |
559 | | - "outputId": "988ea7e4-8a6c-47fa-ebaf-67d98565bb82" |
| 532 | + "id": "Bx9snM-MK-v9" |
560 | 533 | }, |
561 | 534 | "outputs": [], |
562 | 535 | "source": [ |
|
567 | 540 | }, |
568 | 541 | { |
569 | 542 | "cell_type": "code", |
570 | | - "execution_count": 12, |
| 543 | + "execution_count": null, |
571 | 544 | "metadata": { |
572 | 545 | "id": "o9hS4wamM3jO" |
573 | 546 | }, |
|
580 | 553 | "cell_type": "code", |
581 | 554 | "execution_count": null, |
582 | 555 | "metadata": { |
583 | | - "colab": { |
584 | | - "base_uri": "https://localhost:8080/" |
585 | | - }, |
586 | | - "id": "QEPR3kszK_o-", |
587 | | - "outputId": "8791fea9-0869-4cf6-c49f-6c4f6c53eda7" |
| 556 | + "id": "QEPR3kszK_o-" |
588 | 557 | }, |
589 | 558 | "outputs": [], |
590 | 559 | "source": [ |
|
604 | 573 | }, |
605 | 574 | { |
606 | 575 | "cell_type": "code", |
607 | | - "execution_count": 14, |
| 576 | + "execution_count": null, |
608 | 577 | "metadata": { |
609 | 578 | "id": "Db18CEtoK_Md" |
610 | 579 | }, |
|
616 | 585 | }, |
617 | 586 | { |
618 | 587 | "cell_type": "code", |
619 | | - "execution_count": 15, |
| 588 | + "execution_count": null, |
620 | 589 | "metadata": { |
621 | 590 | "id": "FksVuvkgLbpG" |
622 | 591 | }, |
|
641 | 610 | }, |
642 | 611 | { |
643 | 612 | "cell_type": "code", |
644 | | - "execution_count": 16, |
| 613 | + "execution_count": null, |
645 | 614 | "metadata": { |
646 | 615 | "id": "dtNEiziLLlWv" |
647 | 616 | }, |
|
654 | 623 | }, |
655 | 624 | { |
656 | 625 | "cell_type": "code", |
657 | | - "execution_count": 17, |
| 626 | + "execution_count": null, |
658 | 627 | "metadata": { |
659 | 628 | "id": "Rvf1sEizLwyR" |
660 | 629 | }, |
|
682 | 651 | }, |
683 | 652 | { |
684 | 653 | "cell_type": "code", |
685 | | - "execution_count": 18, |
| 654 | + "execution_count": null, |
686 | 655 | "metadata": { |
687 | 656 | "colab": { |
688 | 657 | "base_uri": "https://localhost:8080/", |
|
1087 | 1056 | }, |
1088 | 1057 | { |
1089 | 1058 | "cell_type": "code", |
1090 | | - "execution_count": 19, |
| 1059 | + "execution_count": null, |
1091 | 1060 | "metadata": { |
1092 | 1061 | "id": "sUi81pCUL5_0" |
1093 | 1062 | }, |
|
1110 | 1079 | }, |
1111 | 1080 | { |
1112 | 1081 | "cell_type": "code", |
1113 | | - "execution_count": 20, |
| 1082 | + "execution_count": null, |
1114 | 1083 | "metadata": { |
1115 | 1084 | "id": "XSxs_YqeL6XW" |
1116 | 1085 | }, |
|
1172 | 1141 | ], |
1173 | 1142 | "metadata": { |
1174 | 1143 | "colab": { |
1175 | | - "provenance": [] |
| 1144 | + "provenance": [], |
| 1145 | + "include_colab_link": true |
1176 | 1146 | }, |
1177 | 1147 | "gpuClass": "standard", |
1178 | 1148 | "kernelspec": { |
|
0 commit comments