{"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":"code","source":"import os\nimport json\nfrom PIL import Image\nfrom collections import Counter\n\nimport numpy as np\nimport pandas as pd\nimport plotly.express as px\nimport plotly.graph_objects as go\nimport tifffile as tiff\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\n\nimport cv2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-15T11:47:15.959019Z","iopub.execute_input":"2023-06-15T11:47:15.959388Z","iopub.status.idle":"2023-06-15T11:47:17.088378Z","shell.execute_reply.started":"2023-06-15T11:47:15.959351Z","shell.execute_reply":"2023-06-15T11:47:17.087033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tile_meta = pd.read_csv('/kaggle/input/hubmap-hacking-the-human-vasculature/tile_meta.csv')\ntile_meta.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:17.090531Z","iopub.execute_input":"2023-06-15T11:47:17.091290Z","iopub.status.idle":"2023-06-15T11:47:17.145875Z","shell.execute_reply.started":"2023-06-15T11:47:17.091258Z","shell.execute_reply":"2023-06-15T11:47:17.144612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds1 = tile_meta[tile_meta['dataset']==1]\nds1.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:17.147102Z","iopub.execute_input":"2023-06-15T11:47:17.147478Z","iopub.status.idle":"2023-06-15T11:47:17.163915Z","shell.execute_reply.started":"2023-06-15T11:47:17.147449Z","shell.execute_reply":"2023-06-15T11:47:17.162611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds1.describe()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:17.166432Z","iopub.execute_input":"2023-06-15T11:47:17.167720Z","iopub.status.idle":"2023-06-15T11:47:17.192299Z","shell.execute_reply.started":"2023-06-15T11:47:17.167661Z","shell.execute_reply":"2023-06-15T11:47:17.191170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds2 = tile_meta[tile_meta['dataset']==2]\nds2.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:17.193818Z","iopub.execute_input":"2023-06-15T11:47:17.194105Z","iopub.status.idle":"2023-06-15T11:47:17.209654Z","shell.execute_reply.started":"2023-06-15T11:47:17.194081Z","shell.execute_reply":"2023-06-15T11:47:17.208134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds2.describe()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:17.211867Z","iopub.execute_input":"2023-06-15T11:47:17.212235Z","iopub.status.idle":"2023-06-15T11:47:17.235220Z","shell.execute_reply.started":"2023-06-15T11:47:17.212210Z","shell.execute_reply":"2023-06-15T11:47:17.234465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wsi1 = tile_meta[tile_meta['source_wsi']==1]\nwsi1.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:17.236282Z","iopub.execute_input":"2023-06-15T11:47:17.237174Z","iopub.status.idle":"2023-06-15T11:47:17.249895Z","shell.execute_reply.started":"2023-06-15T11:47:17.237145Z","shell.execute_reply":"2023-06-15T11:47:17.248703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wsi1.describe()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:17.251012Z","iopub.execute_input":"2023-06-15T11:47:17.251831Z","iopub.status.idle":"2023-06-15T11:47:17.274290Z","shell.execute_reply.started":"2023-06-15T11:47:17.251802Z","shell.execute_reply":"2023-06-15T11:47:17.273336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zout_ratio = 4 # raw image is huge image, to prevent OOM","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:17.275589Z","iopub.execute_input":"2023-06-15T11:47:17.275833Z","iopub.status.idle":"2023-06-15T11:47:17.281178Z","shell.execute_reply.started":"2023-06-15T11:47:17.275813Z","shell.execute_reply":"2023-06-15T11:47:17.279787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"overall_img = np.zeros(((wsi1['j'].max()+512)//zout_ratio, (wsi1['i'].max()+512)//zout_ratio, 3), dtype=np.uint8)","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:17.284426Z","iopub.execute_input":"2023-06-15T11:47:17.284773Z","iopub.status.idle":"2023-06-15T11:47:17.296082Z","shell.execute_reply.started":"2023-06-15T11:47:17.284745Z","shell.execute_reply":"2023-06-15T11:47:17.294774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for index, row in tqdm(wsi1.iterrows(), total=len(wsi1)):\n    img_path = f'/kaggle/input/hubmap-hacking-the-human-vasculature/train/{row[\"id\"]}.tif' \n    partial_img = tiff.imread(img_path)\n    sy = row['j'] // zout_ratio\n    sx = row['i'] // zout_ratio\n    imsize = partial_img.shape[0] // zout_ratio\n    partial_img = cv2.resize(partial_img, (imsize, imsize))\n    overall_img[sy:sy+imsize, sx:sx+imsize] = partial_img","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:17.297848Z","iopub.execute_input":"2023-06-15T11:47:17.298237Z","iopub.status.idle":"2023-06-15T11:47:29.111435Z","shell.execute_reply.started":"2023-06-15T11:47:17.298206Z","shell.execute_reply":"2023-06-15T11:47:29.109962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(overall_img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:29.112916Z","iopub.execute_input":"2023-06-15T11:47:29.113376Z","iopub.status.idle":"2023-06-15T11:47:33.535584Z","shell.execute_reply.started":"2023-06-15T11:47:29.113310Z","shell.execute_reply":"2023-06-15T11:47:33.534298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(overall_img[10000:10000+512, 2000:2000+512])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:33.537040Z","iopub.execute_input":"2023-06-15T11:47:33.537387Z","iopub.status.idle":"2023-06-15T11:47:33.801059Z","shell.execute_reply.started":"2023-06-15T11:47:33.537359Z","shell.execute_reply":"2023-06-15T11:47:33.800086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tile_meta[tile_meta['id']=='8cb7d2ef7d2c']","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:33.802024Z","iopub.execute_input":"2023-06-15T11:47:33.802352Z","iopub.status.idle":"2023-06-15T11:47:33.817843Z","shell.execute_reply.started":"2023-06-15T11:47:33.802295Z","shell.execute_reply":"2023-06-15T11:47:33.816234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wsi2 = tile_meta[tile_meta['source_wsi']==2]\nwsi2.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:33.819156Z","iopub.execute_input":"2023-06-15T11:47:33.819544Z","iopub.status.idle":"2023-06-15T11:47:33.837689Z","shell.execute_reply.started":"2023-06-15T11:47:33.819511Z","shell.execute_reply":"2023-06-15T11:47:33.836671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"overall_img2 = np.zeros(((wsi2['j'].max()+512)//zout_ratio, (wsi2['i'].max()+512)//zout_ratio, 3), dtype=np.uint8)\noverall_mask2 = np.ones(((wsi2['j'].max()+512)//zout_ratio, (wsi2['i'].max()+512)//zout_ratio, 3), dtype=np.uint8) * 127\noverall_img2.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:33.840081Z","iopub.execute_input":"2023-06-15T11:47:33.840830Z","iopub.status.idle":"2023-06-15T11:47:33.879682Z","shell.execute_reply.started":"2023-06-15T11:47:33.840801Z","shell.execute_reply":"2023-06-15T11:47:33.878677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for index, row in tqdm(wsi2.iterrows(), total=len(wsi2)):\n    img_path = f'/kaggle/input/hubmap-hacking-the-human-vasculature/train/{row[\"id\"]}.tif' \n    partial_img = tiff.imread(img_path)\n    sy = row['j'] // zout_ratio\n    sx = row['i'] // zout_ratio\n    imsize = partial_img.shape[0] // zout_ratio\n    partial_img = cv2.resize(partial_img, (imsize, imsize))\n    overall_img2[sy:sy+imsize, sx:sx+imsize] = partial_img\n    \n    mask_path = f'/kaggle/input/hubmap-making-dataset/train/mask/{row[\"id\"]}_mask.png'\n    partial_mask = cv2.imread(mask_path)\n    \n    partial_mask = cv2.resize(partial_mask, (imsize, imsize))\n    overall_mask2[sy:sy+imsize, sx:sx+imsize] = partial_mask","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:33.881137Z","iopub.execute_input":"2023-06-15T11:47:33.881563Z","iopub.status.idle":"2023-06-15T11:47:48.129123Z","shell.execute_reply.started":"2023-06-15T11:47:33.881524Z","shell.execute_reply":"2023-06-15T11:47:48.128288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(overall_img2)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:48.130458Z","iopub.execute_input":"2023-06-15T11:47:48.130785Z","iopub.status.idle":"2023-06-15T11:47:52.578655Z","shell.execute_reply.started":"2023-06-15T11:47:48.130760Z","shell.execute_reply":"2023-06-15T11:47:52.577213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(overall_mask2)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:52.580472Z","iopub.execute_input":"2023-06-15T11:47:52.580962Z","iopub.status.idle":"2023-06-15T11:47:57.022600Z","shell.execute_reply.started":"2023-06-15T11:47:52.580929Z","shell.execute_reply":"2023-06-15T11:47:57.021447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"problem = overall_img2[19492//zout_ratio-512:19492//zout_ratio+512, 16896//zout_ratio-512:16896//zout_ratio+512]\nprob_gt = overall_mask2[19492//zout_ratio-512:19492//zout_ratio+512, 16896//zout_ratio-512:16896//zout_ratio+512]\nfor y in range(1, 1024, 512//zout_ratio):\n    cv2.line(problem, (0, y), (1024, y), (255, 0, 0), thickness=2, lineType=cv2.LINE_8)\n    cv2.line(prob_gt, (0, y), (1024, y), (255, 0, 0), thickness=2, lineType=cv2.LINE_8)\nfor x in range(1, 1024, 512//zout_ratio):\n    cv2.line(problem, (x, 0), (x, 1024), (255, 0, 0), thickness=2, lineType=cv2.LINE_8)\n    cv2.line(prob_gt, (x, 0), (x, 1024), (255, 0, 0), thickness=2, lineType=cv2.LINE_8)","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:57.024100Z","iopub.execute_input":"2023-06-15T11:47:57.024485Z","iopub.status.idle":"2023-06-15T11:47:57.038201Z","shell.execute_reply.started":"2023-06-15T11:47:57.024457Z","shell.execute_reply":"2023-06-15T11:47:57.036535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(nrows=1, ncols=2,figsize=(20,20))\nax[0].imshow(problem)\nax[1].imshow(prob_gt)\nplt.show()      ","metadata":{"execution":{"iopub.status.busy":"2023-06-15T11:47:57.040249Z","iopub.execute_input":"2023-06-15T11:47:57.040757Z","iopub.status.idle":"2023-06-15T11:47:57.759562Z","shell.execute_reply.started":"2023-06-15T11:47:57.040676Z","shell.execute_reply":"2023-06-15T11:47:57.758090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}