{"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":"A very simple example of a generator for patches extraction from a WSI. \nPatches are sorted by mean pixel value. ","metadata":{}},{"cell_type":"code","source":"%matplotlib inline\nimport os\nimport pandas as  pd\nimport numpy as np\nimport openslide\nfrom matplotlib import pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-10T20:58:33.313034Z","iopub.execute_input":"2022-08-10T20:58:33.313577Z","iopub.status.idle":"2022-08-10T20:58:34.001361Z","shell.execute_reply.started":"2022-08-10T20:58:33.31354Z","shell.execute_reply":"2022-08-10T20:58:34.000512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_dir = '/kaggle/input/prostate-cancer-grade-assessment/train_images'\nimages_filenames = os.listdir(images_dir)\nlen(images_filenames)","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2022-08-10T20:58:37.39419Z","iopub.execute_input":"2022-08-10T20:58:37.394518Z","iopub.status.idle":"2022-08-10T20:58:37.407011Z","shell.execute_reply.started":"2022-08-10T20:58:37.394484Z","shell.execute_reply":"2022-08-10T20:58:37.40573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Location of the training images\ndata_dir = '/kaggle/input/prostate-cancer-grade-assessment/train_images'\nmask_dir = '/kaggle/input/prostate-cancer-grade-assessment/train_label_masks'\n\n# Location of training labels\ntrain_labels = pd.read_csv('/kaggle/input/prostate-cancer-grade-assessment/train.csv').set_index('image_id')","metadata":{"execution":{"iopub.status.busy":"2022-08-10T20:58:39.489471Z","iopub.execute_input":"2022-08-10T20:58:39.489809Z","iopub.status.idle":"2022-08-10T20:58:39.534086Z","shell.execute_reply.started":"2022-08-10T20:58:39.489773Z","shell.execute_reply":"2022-08-10T20:58:39.533087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"slide = openslide.OpenSlide(os.path.join(data_dir, '005e66f06bce9c2e49142536caf2f6ee.tiff'))","metadata":{"execution":{"iopub.status.busy":"2022-08-10T20:59:12.987253Z","iopub.execute_input":"2022-08-10T20:59:12.987746Z","iopub.status.idle":"2022-08-10T20:59:13.019909Z","shell.execute_reply.started":"2022-08-10T20:59:12.987712Z","shell.execute_reply":"2022-08-10T20:59:13.019185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"slide_props = slide.properties\nprint(slide_props)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-10T20:59:36.109538Z","iopub.execute_input":"2022-08-10T20:59:36.110165Z","iopub.status.idle":"2022-08-10T20:59:36.115181Z","shell.execute_reply.started":"2022-08-10T20:59:36.110126Z","shell.execute_reply":"2022-08-10T20:59:36.114455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get slide dimensions for the level 0 - max resolution level\nslide_dims = slide.dimensions\nprint(slide_dims)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T21:00:03.934387Z","iopub.execute_input":"2022-08-10T21:00:03.93488Z","iopub.status.idle":"2022-08-10T21:00:03.940212Z","shell.execute_reply.started":"2022-08-10T21:00:03.934844Z","shell.execute_reply":"2022-08-10T21:00:03.939189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dims = slide.level_dimensions\nnum_levels = len(dims)\nprint(\"Number of levels in this image are:\", num_levels)\n\nprint(\"Dimensions of various levels in this image are:\", dims)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T21:03:06.231747Z","iopub.execute_input":"2022-08-10T21:03:06.232088Z","iopub.status.idle":"2022-08-10T21:03:06.241365Z","shell.execute_reply.started":"2022-08-10T21:03:06.232052Z","shell.execute_reply":"2022-08-10T21:03:06.239815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"factors = slide.level_downsamples\nprint(\"Each level is downsampled by an amount of: \", factors)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T21:03:45.564272Z","iopub.execute_input":"2022-08-10T21:03:45.564934Z","iopub.status.idle":"2022-08-10T21:03:45.570191Z","shell.execute_reply.started":"2022-08-10T21:03:45.564895Z","shell.execute_reply":"2022-08-10T21:03:45.569261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from openslide.deepzoom import DeepZoomGenerator","metadata":{"execution":{"iopub.status.busy":"2022-08-10T21:11:17.09678Z","iopub.execute_input":"2022-08-10T21:11:17.097082Z","iopub.status.idle":"2022-08-10T21:11:17.101604Z","shell.execute_reply.started":"2022-08-10T21:11:17.097052Z","shell.execute_reply":"2022-08-10T21:11:17.100271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Generate object for tiles using the DeepZoomGenerator\ntiles = DeepZoomGenerator(slide, tile_size=1000, overlap=0, limit_bounds=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T21:13:12.803292Z","iopub.execute_input":"2022-08-10T21:13:12.803615Z","iopub.status.idle":"2022-08-10T21:13:12.808295Z","shell.execute_reply.started":"2022-08-10T21:13:12.80358Z","shell.execute_reply":"2022-08-10T21:13:12.80761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#The tiles object also contains data at many levels. \n#To check the number of levels\nprint(\"The number of levels in the tiles object are: \", tiles.level_count)\n\nprint(\"The dimensions of data in each level are: \", tiles.level_dimensions)\nprint(\"Total number of tiles = : \", tiles.tile_count)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T21:13:14.05636Z","iopub.execute_input":"2022-08-10T21:13:14.05691Z","iopub.status.idle":"2022-08-10T21:13:14.063637Z","shell.execute_reply.started":"2022-08-10T21:13:14.05687Z","shell.execute_reply":"2022-08-10T21:13:14.062635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#How many tiles at a specific level?\nlevel_num = tiles.level_count-2\nprint(\"Tiles shape at level \", level_num, \" is: \", tiles.level_tiles[level_num])\nprint(\"This means there are \", tiles.level_tiles[level_num][0]*tiles.level_tiles[level_num][1], \" total tiles in this level\")","metadata":{"execution":{"iopub.status.busy":"2022-08-10T21:13:16.598349Z","iopub.execute_input":"2022-08-10T21:13:16.598718Z","iopub.status.idle":"2022-08-10T21:13:16.606947Z","shell.execute_reply.started":"2022-08-10T21:13:16.598681Z","shell.execute_reply":"2022-08-10T21:13:16.605779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"###### Saving each tile to local directory\ncols, rows = tiles.level_tiles[level_num]\n\nimport os\ntile_dir = \"./\"\nfor row in range(rows):\n    for col in range(cols):\n        tile_name = os.path.join(tile_dir, '%d_%d' % (col, row))\n        print(\"Now saving tile with title: \", tile_name)\n        temp_tile = tiles.get_tile(level_num, (col, row))\n        temp_tile_RGB = temp_tile.convert('RGB')\n        temp_tile_np = np.array(temp_tile_RGB)\n        plt.imsave(tile_name + \".png\", temp_tile_np)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T21:15:33.008738Z","iopub.execute_input":"2022-08-10T21:15:33.009044Z","iopub.status.idle":"2022-08-10T21:17:42.5324Z","shell.execute_reply.started":"2022-08-10T21:15:33.009014Z","shell.execute_reply":"2022-08-10T21:17:42.531328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}