{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# UBC-OCEAN","metadata":{}},{"cell_type":"markdown","source":"## Importing Libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom pathlib import Path\nfrom skimage import io\nimport numpy as np\nimport os, glob","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-24T19:05:08.819411Z","iopub.execute_input":"2023-11-24T19:05:08.820152Z","iopub.status.idle":"2023-11-24T19:05:09.910388Z","shell.execute_reply.started":"2023-11-24T19:05:08.820089Z","shell.execute_reply":"2023-11-24T19:05:09.909352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATASET_FOLDER = \"/kaggle/input/UBC-OCEAN/\"\nDATASET_IMAGES = \"/kaggle/input/cancer-subtype-eda-load-wsi-prune-bg/train_thumbnails/\"","metadata":{"execution":{"iopub.status.busy":"2023-11-24T19:05:09.912387Z","iopub.execute_input":"2023-11-24T19:05:09.912849Z","iopub.status.idle":"2023-11-24T19:05:09.917543Z","shell.execute_reply.started":"2023-11-24T19:05:09.912817Z","shell.execute_reply":"2023-11-24T19:05:09.916589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load the train dataset...","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(os.path.join(DATASET_FOLDER, \"train.csv\"))\n\nprint(f\"Dataset/train size: {len(train_df)}\")\ndisplay(train_df.head())","metadata":{"execution":{"iopub.status.busy":"2023-11-24T19:05:09.918772Z","iopub.execute_input":"2023-11-24T19:05:09.919072Z","iopub.status.idle":"2023-11-24T19:05:09.944741Z","shell.execute_reply.started":"2023-11-24T19:05:09.919043Z","shell.execute_reply":"2023-11-24T19:05:09.943826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df.info())","metadata":{"execution":{"iopub.status.busy":"2023-11-24T19:05:09.947499Z","iopub.execute_input":"2023-11-24T19:05:09.948199Z","iopub.status.idle":"2023-11-24T19:05:09.961008Z","shell.execute_reply.started":"2023-11-24T19:05:09.948163Z","shell.execute_reply":"2023-11-24T19:05:09.959888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Class Distribution","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nsns.countplot(x=\"label\", data=train_df)\nplt.title(\"Class Distribution\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T19:05:09.962167Z","iopub.execute_input":"2023-11-24T19:05:09.962451Z","iopub.status.idle":"2023-11-24T19:05:10.249647Z","shell.execute_reply.started":"2023-11-24T19:05:09.962424Z","shell.execute_reply":"2023-11-24T19:05:10.248611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Image Dimensions Distribution","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 6))\nsns.scatterplot(x=\"image_width\", y=\"image_height\", hue=\"label\", data=train_df)\nplt.title(\"Image Dimensions Distribution\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T19:05:10.25133Z","iopub.execute_input":"2023-11-24T19:05:10.252068Z","iopub.status.idle":"2023-11-24T19:05:10.729698Z","shell.execute_reply.started":"2023-11-24T19:05:10.252015Z","shell.execute_reply":"2023-11-24T19:05:10.728927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Class wise Thumbnail Image","metadata":{}},{"cell_type":"code","source":"for lb, dfg in train_df.groupby(\"label\"):\n    fig, axes = plt.subplots(ncols=4, figsize=(16, 4))\n    for i, name in enumerate(dfg[\"image_id\"].sample(4)):\n       \n        img_path = os.path.join(DATASET_FOLDER, \"train_thumbnails\", f\"{name}_thumbnail.png\")\n        \n        if not os.path.isfile(img_path):\n            img_path = os.path.join(DATASET_FOLDER, \"train_images\", f\"{name}.png\")\n            print(f\"Missing thumbnail for {img_path} but img exists {os.path.isfile(img_path)}\")\n            continue\n            \n        axes[i].imshow(plt.imread(img_path))\n        axes[i].set_title(f\"label: *{lb}* for img: {name}\")\n        axes[i].set_axis_off()\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T19:05:10.730885Z","iopub.execute_input":"2023-11-24T19:05:10.731842Z","iopub.status.idle":"2023-11-24T19:05:49.388309Z","shell.execute_reply.started":"2023-11-24T19:05:10.73181Z","shell.execute_reply":"2023-11-24T19:05:49.387483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Tissue Microarray vs. Whole Slide Images","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nsns.countplot(x=\"is_tma\", data=train_df)\nplt.title(\"Tissue Microarray vs. Whole Slide Images\")\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-24T19:05:49.389415Z","iopub.execute_input":"2023-11-24T19:05:49.390446Z","iopub.status.idle":"2023-11-24T19:05:49.635944Z","shell.execute_reply.started":"2023-11-24T19:05:49.39041Z","shell.execute_reply":"2023-11-24T19:05:49.634631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Image Dimensions Boxplot by Class","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 6))\nsns.boxplot(x=\"label\", y=\"image_width\", data=train_df)\nplt.title(\"Image Width Distribution by Class\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T19:05:49.63737Z","iopub.execute_input":"2023-11-24T19:05:49.637811Z","iopub.status.idle":"2023-11-24T19:05:50.087076Z","shell.execute_reply.started":"2023-11-24T19:05:49.637765Z","shell.execute_reply":"2023-11-24T19:05:50.085985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Image Dimensions Boxplot by Class","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 6))\nsns.boxplot(x=\"label\", y=\"image_height\", data=train_df)\nplt.title(\"Image Height Distribution by Class\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T19:05:50.092476Z","iopub.execute_input":"2023-11-24T19:05:50.092831Z","iopub.status.idle":"2023-11-24T19:05:50.429594Z","shell.execute_reply.started":"2023-11-24T19:05:50.092792Z","shell.execute_reply":"2023-11-24T19:05:50.428545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Pairplot","metadata":{}},{"cell_type":"code","source":"sns.pairplot(train_df, hue=\"label\")\nplt.suptitle(\"Pairplot by Class\", y=1.02)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T19:05:50.430858Z","iopub.execute_input":"2023-11-24T19:05:50.431873Z","iopub.status.idle":"2023-11-24T19:05:58.239986Z","shell.execute_reply.started":"2023-11-24T19:05:50.431839Z","shell.execute_reply":"2023-11-24T19:05:58.238929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Distribution of Image Width","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 6))\nsns.histplot(train_df[\"image_width\"], bins=30, kde=True)\nplt.title(\"Distribution of Image Width\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T19:05:58.24154Z","iopub.execute_input":"2023-11-24T19:05:58.241943Z","iopub.status.idle":"2023-11-24T19:05:58.691559Z","shell.execute_reply.started":"2023-11-24T19:05:58.241884Z","shell.execute_reply":"2023-11-24T19:05:58.690785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Distribution of Image Height","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 6))\nsns.histplot(train_df[\"image_height\"], bins=30, kde=True)\nplt.title(\"Distribution of Image Height\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T19:05:58.692709Z","iopub.execute_input":"2023-11-24T19:05:58.693537Z","iopub.status.idle":"2023-11-24T19:05:59.127367Z","shell.execute_reply.started":"2023-11-24T19:05:58.693506Z","shell.execute_reply":"2023-11-24T19:05:59.126301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Distribution of Image Dimensions by Tissue Microarray","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 6))\nsns.scatterplot(x=\"image_width\", y=\"image_height\", hue=\"is_tma\", data=train_df)\nplt.title(\"Distribution of Image Dimensions by Tissue Microarray\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T19:05:59.128859Z","iopub.execute_input":"2023-11-24T19:05:59.130151Z","iopub.status.idle":"2023-11-24T19:05:59.53998Z","shell.execute_reply.started":"2023-11-24T19:05:59.130105Z","shell.execute_reply":"2023-11-24T19:05:59.538842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Violin Plot of Image Dimensions by Class","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(14, 8))\nsns.violinplot(x=\"label\", y=\"image_width\", data=train_df, inner=\"quartile\")\nplt.title(\"Violin Plot of Image Width by Class\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T19:05:59.541438Z","iopub.execute_input":"2023-11-24T19:05:59.541741Z","iopub.status.idle":"2023-11-24T19:05:59.971379Z","shell.execute_reply.started":"2023-11-24T19:05:59.541712Z","shell.execute_reply":"2023-11-24T19:05:59.970102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(14, 8))\nsns.violinplot(x=\"label\", y=\"image_height\", data=train_df, inner=\"quartile\")\nplt.title(\"Violin Plot of Image Height by Class\")\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-24T19:05:59.972698Z","iopub.execute_input":"2023-11-24T19:05:59.97308Z","iopub.status.idle":"2023-11-24T19:06:00.41827Z","shell.execute_reply.started":"2023-11-24T19:05:59.973046Z","shell.execute_reply":"2023-11-24T19:06:00.417185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Pairplot of Image Dimensions by Class","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 10))\nsns.pairplot(train_df, hue=\"label\", vars=[\"image_width\", \"image_height\"])\nplt.suptitle(\"Pairplot of Image Dimensions by Class\", y=1.02)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T19:06:00.41971Z","iopub.execute_input":"2023-11-24T19:06:00.420148Z","iopub.status.idle":"2023-11-24T19:06:02.662602Z","shell.execute_reply.started":"2023-11-24T19:06:00.420108Z","shell.execute_reply":"2023-11-24T19:06:02.661346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Distribution of Image Dimensions by Class and Tissue Microarray","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 8))\nsns.scatterplot(x=\"image_width\", y=\"image_height\", hue=\"label\", style=\"is_tma\", data=train_df)\nplt.title(\"Distribution of Image Dimensions by Class and Tissue Microarray\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T19:06:02.664606Z","iopub.execute_input":"2023-11-24T19:06:02.665305Z","iopub.status.idle":"2023-11-24T19:06:03.230028Z","shell.execute_reply.started":"2023-11-24T19:06:02.665263Z","shell.execute_reply":"2023-11-24T19:06:03.228842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Samples: train_images","metadata":{}},{"cell_type":"code","source":"for label in ['HGSC', 'CC', 'EC', 'LGSC', 'MC']:\n    df_tmp = train_df[train_df['label']==label]\n    image_id_list = list(df_tmp[df_tmp['is_tma']]['image_id'])\n    plt.figure(figsize=(20.0, 6.0))\n    \n    for i in range(len(image_id_list)):\n        image_id = image_id_list[i]\n        plt.subplot(1, 5, i+1)\n        if i == 0:\n            plt.title(f'image_id:{image_id} (TMA)', fontsize=14)\n            plt.ylabel(label, fontsize=14)\n        else:\n            plt.title(f'image_id:{image_id} (TMA)', fontsize=14)\n        io.imshow(f'/kaggle/input/UBC-OCEAN/train_images/{image_id}.png')\n        plt.tick_params(labelbottom=False, labelleft=False, labelright=False, labeltop=False, bottom=False, left=False, right=False, top=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-24T19:06:03.231697Z","iopub.execute_input":"2023-11-24T19:06:03.23246Z","iopub.status.idle":"2023-11-24T19:07:01.965562Z","shell.execute_reply.started":"2023-11-24T19:06:03.232417Z","shell.execute_reply":"2023-11-24T19:07:01.964122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Samples: train_thumbnails","metadata":{}},{"cell_type":"code","source":"for label in ['HGSC', 'CC', 'EC', 'LGSC', 'MC']:\n    df_tmp = train_df[train_df['label']==label]\n    image_id_list = list(df_tmp[~df_tmp['is_tma']]['image_id'].sample(5))\n    plt.figure(figsize=(20.0, 6.0))\n    \n    for i in range(len(image_id_list)):\n        image_id = image_id_list[i]\n        plt.subplot(1, 5, i+1)\n        if i == 0:\n            plt.title(f'image_id:{image_id} (WSI)', fontsize=14)\n            plt.ylabel(label, fontsize=14)\n        else:\n            plt.title(f'image_id:{image_id} (WSI)', fontsize=14)\n            \n        io.imshow(f'/kaggle/input/UBC-OCEAN/train_thumbnails/{image_id}_thumbnail.png')\n        plt.tick_params(labelbottom=False, labelleft=False, labelright=False, labeltop=False, bottom=False, left=False, right=False, top=False)\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-24T19:07:01.967306Z","iopub.execute_input":"2023-11-24T19:07:01.967978Z","iopub.status.idle":"2023-11-24T19:07:31.318103Z","shell.execute_reply.started":"2023-11-24T19:07:01.967936Z","shell.execute_reply":"2023-11-24T19:07:31.317092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Continue...**","metadata":{}}]}