{"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":"!pip install monai[cucim]==0.9.1\n!pip install scipy scikit-image cupy-cuda110","metadata":{"_uuid":"8b397d65-3797-4285-98bd-2b5f021e25ee","_cell_guid":"31fea33e-c959-47c7-84da-b374acd7fbcb","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-05T07:45:49.114059Z","iopub.execute_input":"2022-08-05T07:45:49.114496Z","iopub.status.idle":"2022-08-05T07:46:11.888181Z","shell.execute_reply.started":"2022-08-05T07:45:49.114412Z","shell.execute_reply":"2022-08-05T07:46:11.886972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nfrom monai.transforms import LoadImaged, Compose, GridPatchd\nfrom monai.data.wsi_reader import WSIReader\nfrom monai.data import Dataset, DataLoader\nfrom monai.visualize import matshow3d\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-08-05T07:46:11.890433Z","iopub.execute_input":"2022-08-05T07:46:11.891058Z","iopub.status.idle":"2022-08-05T07:46:18.140374Z","shell.execute_reply.started":"2022-08-05T07:46:11.891015Z","shell.execute_reply":"2022-08-05T07:46:18.139144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_dir = \"../input/mayo-clinic-strip-ai/\"\n\ntrain_img_dir = os.path.join(root_dir, \"train\")\ntest_img_dir = os.path.join(root_dir, \"train\")\n\ntrain_df = pd.read_csv(os.path.join(root_dir, \"train.csv\"))","metadata":{"execution":{"iopub.status.busy":"2022-08-05T07:46:18.14236Z","iopub.execute_input":"2022-08-05T07:46:18.144037Z","iopub.status.idle":"2022-08-05T07:46:18.161004Z","shell.execute_reply.started":"2022-08-05T07:46:18.143986Z","shell.execute_reply":"2022-08-05T07:46:18.160023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_dict = {\"CE\" : 0, \"LAA\": 1}\n\ndef parse_label(x):\n    return label_dict[x]\ntrain_df[\"cls\"] = train_df[\"label\"].apply(lambda x: parse_label(x))","metadata":{"execution":{"iopub.status.busy":"2022-08-05T07:46:18.163729Z","iopub.execute_input":"2022-08-05T07:46:18.164681Z","iopub.status.idle":"2022-08-05T07:46:18.176813Z","shell.execute_reply.started":"2022-08-05T07:46:18.164638Z","shell.execute_reply":"2022-08-05T07:46:18.175914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load an example\n\nthe loaded WSI is divided into several patches (with the specified patch size)","metadata":{}},{"cell_type":"code","source":"example_id = \"026c97_0\"  # select a small .tif file\n\nexample_img_path = os.path.join(train_img_dir, example_id + \".tif\")\nexample_label = train_df[\"cls\"][train_df[\"image_id\"] == example_id]\n\nexample = {\"image\": example_img_path, \"label\": example_label}\npatch_size = (2048, 2048)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T07:46:18.179364Z","iopub.execute_input":"2022-08-05T07:46:18.180297Z","iopub.status.idle":"2022-08-05T07:46:18.187594Z","shell.execute_reply.started":"2022-08-05T07:46:18.180234Z","shell.execute_reply":"2022-08-05T07:46:18.186585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = Compose(\n    [\n        LoadImaged(keys=[\"image\"], reader=WSIReader, backend=\"cucim\", image_only=True), # backend = \"cucim\" or \"openslide\"\n        GridPatchd(\n            keys=[\"image\"],\n            patch_size=patch_size,\n        ),\n    ]\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T07:46:18.189164Z","iopub.execute_input":"2022-08-05T07:46:18.189664Z","iopub.status.idle":"2022-08-05T07:46:18.200559Z","shell.execute_reply.started":"2022-08-05T07:46:18.189629Z","shell.execute_reply":"2022-08-05T07:46:18.199656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = transform(example)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T07:46:18.202304Z","iopub.execute_input":"2022-08-05T07:46:18.202605Z","iopub.status.idle":"2022-08-05T07:46:22.276361Z","shell.execute_reply.started":"2022-08-05T07:46:18.202578Z","shell.execute_reply":"2022-08-05T07:46:22.275311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Visualize","metadata":{}},{"cell_type":"code","source":"# the image has been divided into 30 patches\n\nlen(data)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T07:46:22.278026Z","iopub.execute_input":"2022-08-05T07:46:22.278412Z","iopub.status.idle":"2022-08-05T07:46:22.287592Z","shell.execute_reply.started":"2022-08-05T07:46:22.278365Z","shell.execute_reply":"2022-08-05T07:46:22.286472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"row = 5\ncol = 6\nfig, axs = plt.subplots(row, col, figsize=(6, 6))\n\ncount = 0\nfor i in range(row):\n    for j in range(col):\n        axs[i, j].imshow(data[count][\"image\"].numpy().transpose([1, 2, 0]))\n        count += 1","metadata":{"execution":{"iopub.status.busy":"2022-08-05T07:46:22.289379Z","iopub.execute_input":"2022-08-05T07:46:22.290061Z","iopub.status.idle":"2022-08-05T07:46:43.470907Z","shell.execute_reply.started":"2022-08-05T07:46:22.290025Z","shell.execute_reply":"2022-08-05T07:46:43.469922Z"},"trusted":true},"execution_count":null,"outputs":[]}]}