{"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":"My solution for preparing dataset :\n```\nfor each WSI:\n    WSI-->sub_images(1600,1600)\n\nfor each sub_image:\n    resize to (3201,3021)\n    center crop to (2000,2000)\n\nfor each TMA:\n    center crop to (2000,2000)\n```\n\nrelated notebooks:\n\n- [1] https://www.kaggle.com/code/rainfalllove/maybe-a-better-way-to-handle-wsi-files\n- [2] https://www.kaggle.com/code/rainfalllove/maybe-a-better-way-to-handle-tma-in-testset\n","metadata":{}},{"cell_type":"markdown","source":"below are comparations between (2000,2000)TMAs and (2000,2000)WSI sub images after these processing.","metadata":{}},{"cell_type":"code","source":"import os\n\ntma_dir='/kaggle/input/cropped-tmas'\nwsi_dir='/kaggle/input/sub-wsi'\n\nall_tma=os.listdir(tma_dir)\nall_wsi=os.listdir(wsi_dir)","metadata":{"execution":{"iopub.status.busy":"2023-10-24T07:24:15.018752Z","iopub.execute_input":"2023-10-24T07:24:15.019137Z","iopub.status.idle":"2023-10-24T07:24:15.026503Z","shell.execute_reply.started":"2023-10-24T07:24:15.019106Z","shell.execute_reply":"2023-10-24T07:24:15.025756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# resize and crop WSI\n# WSI do the same operation(center_crop) as TMA did\nfrom PIL import Image\ndef center_crop(tma_path, target_size=2000):\n    wsi_name = tma_path.split('/')[-1].split('.')[0]\n    \n    tma_image = Image.open(tma_path)\n    tma_image=tma_image.resize((3201,3021))\n    tma_width, tma_height = tma_image.size\n\n    if isinstance(target_size, int):\n        target_size = (target_size, target_size)\n\n    left = (tma_width - target_size[0]) // 2\n    upper = (tma_height - target_size[1]) // 2\n    right = left + target_size[0]\n    lower = upper + target_size[1]\n\n    # center crop\n    cropped_image = tma_image.crop((left, upper, right, lower))\n\n    return cropped_image\n\nsave_dir='/kaggle/working/wsi'\nif not os.path.exists(save_dir):\n    os.makedirs(save_dir)\n    \nfor wsi in all_wsi[:len(all_tma)]:\n    wsi_path = os.path.join(wsi_dir, wsi)\n    img=center_crop(wsi_path)\n    img.save(os.path.join(save_dir,wsi))","metadata":{"execution":{"iopub.status.busy":"2023-10-24T07:24:15.271895Z","iopub.execute_input":"2023-10-24T07:24:15.272283Z","iopub.status.idle":"2023-10-24T07:24:22.307316Z","shell.execute_reply.started":"2023-10-24T07:24:15.272251Z","shell.execute_reply":"2023-10-24T07:24:22.306201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_wsi_new=os.listdir(save_dir)\nfrom IPython.display import display, Image\nimport PIL\nfor tma, wsi in zip(all_tma, all_wsi_new):\n    tma_path = os.path.join(tma_dir, tma)\n    wsi_path = os.path.join(save_dir, wsi)\n    print(PIL.Image.open(tma_path).size,PIL.Image.open(wsi_path).size)\n    display(Image(filename=tma_path), Image(filename=wsi_path))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-24T07:24:22.309172Z","iopub.execute_input":"2023-10-24T07:24:22.309535Z","iopub.status.idle":"2023-10-24T07:24:23.187162Z","shell.execute_reply.started":"2023-10-24T07:24:22.309506Z","shell.execute_reply":"2023-10-24T07:24:23.186356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}