{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<br>\n\n<br><center><img src=\"https://storage.googleapis.com/kaggle-competitions/kaggle/37333/logos/header.png\" width=100%></center>\n\n<h2 style=\"text-align: center; font-family: Verdana; font-size: 24px; font-style: normal; font-weight: bold; text-decoration: underline; text-transform: none; letter-spacing: 2px; color: #E55CA0; background-color: #ffffff;\">Mayo Clinic - STRIP AI<br><br>How To Interact With Large .tif Files</h2><br>\n<h5 style=\"text-align: center; font-family: Verdana; font-size: 12px; font-style: normal; font-weight: bold; text-decoration: None; text-transform: none; letter-spacing: 1px; color: black; background-color: #ffffff;\">CREATED BY: DARIEN SCHETTLER</h5>\n\n<br>\n\n---\n\n<br>\n\n<center><div class=\"alert alert-block alert-danger\" style=\"margin: 2em; line-height: 1.7em; font-family: Verdana;\">\n    <b style=\"font-size: 18px;\">🛑 &nbsp; WARNING:</b><br><br><b>THIS IS A WORK IN PROGRESS</b><br>\n</div></center>\n\n\n<center><div class=\"alert alert-block alert-warning\" style=\"margin: 2em; line-height: 1.7em; font-family: Verdana;\">\n    <b style=\"font-size: 18px;\">👏 &nbsp; IF YOU FORK THIS OR FIND THIS HELPFUL &nbsp; 👏</b><br><br><b style=\"font-size: 22px; color: darkorange\">PLEASE UPVOTE!</b><br><br>This was a lot of work for me and while it may seem silly, it makes me feel appreciated when others like my work. 😅\n</div></center>\n\n\n","metadata":{}},{"cell_type":"markdown","source":"<br>\n\n**INSTALL REQUIRED LIBRARIES**","metadata":{}},{"cell_type":"code","source":"!sudo apt-get update\n!sudo apt-get -y install libvips-dev\n!pip install pyvips","metadata":{"_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-08T18:56:07.85704Z","iopub.execute_input":"2022-07-08T18:56:07.857354Z","iopub.status.idle":"2022-07-08T18:56:24.197682Z","shell.execute_reply.started":"2022-07-08T18:56:07.857306Z","shell.execute_reply":"2022-07-08T18:56:24.196952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<br>\n\n**IMPORT LIBRARIES**","metadata":{}},{"cell_type":"code","source":"import os\nimport cv2\nimport time\nimport pyvips\nimport openslide\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-07-08T19:04:24.206592Z","iopub.execute_input":"2022-07-08T19:04:24.206973Z","iopub.status.idle":"2022-07-08T19:04:24.433809Z","shell.execute_reply.started":"2022-07-08T19:04:24.206921Z","shell.execute_reply":"2022-07-08T19:04:24.433113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<br>\n\n**OPEN TRAINING DATAFRAME AND ADD IMAGE PATH**","metadata":{}},{"cell_type":"code","source":"# Open the training dataframe and display the initial dataframe\nDATA_DIR = \"/kaggle/input/mayo-clinic-strip-ai\"\nTRAIN_DIR = os.path.join(DATA_DIR, \"train\")\nTRAIN_CSV = os.path.join(DATA_DIR, \"train.csv\")\ntrain_df = pd.read_csv(TRAIN_CSV)\ntrain_df[\"image_path\"] = train_df[\"image_id\"].apply(lambda x: os.path.join(TRAIN_DIR, x+\".tif\"))\nprint(\"\\n... TRAINING DATAFRAME... \\n\")\ndisplay(train_df)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T18:56:24.206177Z","iopub.execute_input":"2022-07-08T18:56:24.206395Z","iopub.status.idle":"2022-07-08T18:56:24.243055Z","shell.execute_reply.started":"2022-07-08T18:56:24.206367Z","shell.execute_reply":"2022-07-08T18:56:24.242498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<br>\n\n**HOW TO SHRINK WSI WITHOUT CRASHING DUE TO OOM RAM**","metadata":{}},{"cell_type":"code","source":"DEMO_PATHS = train_df.sample(5).image_path.tolist()\nDEMO_DOWNSCALE_BY = 20\n\ndef load_scaled_down_slide(image_path, downsample_by=10, to_numpy=True):\n    return pyvips.Image.new_from_file(image_path).resize(1/downsample_by).numpy()\n\nfor DEMO_PATH in DEMO_PATHS:\n    t1 = time.time()\n    DEMO_DOWNSCALED_SLIDE = load_scaled_down_slide(DEMO_PATH, DEMO_DOWNSCALE_BY)\n    print(\"\\ntime: \",time.time()-t1)\n\n    plt.figure(figsize=(20,20))\n    plt.imshow(DEMO_DOWNSCALED_SLIDE)\n    plt.title(f\"{DEMO_PATH.split(DATA_DIR)[-1]}\", fontweight=\"bold\")\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T18:56:42.389276Z","iopub.execute_input":"2022-07-08T18:56:42.389582Z","iopub.status.idle":"2022-07-08T18:58:31.184441Z","shell.execute_reply.started":"2022-07-08T18:56:42.389549Z","shell.execute_reply":"2022-07-08T18:58:31.183329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<br>\n\n**HOW TO VIEW PATCHES - WITHOUT LOADING THE WHOLE IMAGE**","metadata":{}},{"cell_type":"code","source":"DEMO_PATCH_TL_PIXEL = (1_000, 1_000)\nDEMO_PATCH_SHAPE = (3_000,1_500)\n\ndef get_patch(os_obj, tl_pixel, patch_shape):\n    return os_obj.read_region(tl_pixel, 0, patch_shape).convert(\"RGB\")\n\nfor DEMO_PATH in DEMO_PATHS:\n    \n    t1 = time.time()\n    DEMO_OS_OBJ = openslide.open_slide(DEMO_PATH)\n    print(\"demo dimensions: \", DEMO_OS_OBJ.dimensions)\n    if ((DEMO_OS_OBJ.dimensions[0]<(DEMO_PATCH_TL_PIXEL[0]+DEMO_PATCH_SHAPE[0])) or\n        (DEMO_OS_OBJ.dimensions[1]<(DEMO_PATCH_TL_PIXEL[1]+DEMO_PATCH_SHAPE[1]))):\n        DEMO_PATCH_TL_PIXEL = (0,0)\n    DEMO_PATCH = get_patch(DEMO_OS_OBJ, DEMO_PATCH_TL_PIXEL, DEMO_PATCH_SHAPE)\n    print(\"\\ntime: \",time.time()-t1)\n\n    plt.figure(figsize=(10,20))\n    plt.imshow(DEMO_PATCH)\n    plt.title(f\"{DEMO_PATH.split(DATA_DIR)[-1]}\\n{DEMO_PATCH_TL_PIXEL} - {DEMO_PATCH_SHAPE}\", fontweight=\"bold\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T18:58:31.186706Z","iopub.execute_input":"2022-07-08T18:58:31.187013Z","iopub.status.idle":"2022-07-08T18:58:35.923891Z","shell.execute_reply.started":"2022-07-08T18:58:31.186974Z","shell.execute_reply":"2022-07-08T18:58:35.923222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<br>\n\n**RESIZE TRAINING DATA INTO PNG LOW-RESOLUTION PNG DATASET (0.1x)**","metadata":{}},{"cell_type":"code","source":"DWN_SCALE_FACTOR = 10\nos.makedirs(f\"/kaggle/working/mayo-clinic-strip-ai-downscaled-by-{DWN_SCALE_FACTOR}/train\", exist_ok=True)\nos.makedirs(f\"/kaggle/working/mayo-clinic-strip-ai-downscaled-by-{DWN_SCALE_FACTOR}/test\", exist_ok=True)\n\nt1 = time.time()\nfor img_path in train_df.image_path.tolist():\n    dst_path = img_path.replace(\"input/mayo-clinic-strip-ai\", \n                                f\"working/mayo-clinic-strip-ai-downscaled-by-{DWN_SCALE_FACTOR}\")\\\n                        .replace(\".tif\", \n                                 \".png\")\n    tmp_img = load_scaled_down_slide(img_path, downsample_by=10, to_numpy=False)\n    cv2.imwrite(dst_path, tmp_img)\n    \nprint(\"time to resize dataset: \", time.time()-t1)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T18:58:35.925159Z","iopub.execute_input":"2022-07-08T18:58:35.925405Z","iopub.status.idle":"2022-07-08T19:00:36.195307Z","shell.execute_reply.started":"2022-07-08T18:58:35.925376Z","shell.execute_reply":"2022-07-08T19:00:36.193995Z"},"trusted":true},"execution_count":null,"outputs":[]}]}