{"cells":[{"metadata":{},"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"%matplotlib inline\nimport os\n\nimport numpy as np\nimport openslide\nfrom matplotlib import pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"images_dir = '/kaggle/input/prostate-cancer-grade-assessment/train_images'\nimages_filenames = os.listdir(images_dir)\nlen(images_filenames)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class PatchSlicer:\n    height: int\n    width: int\n\n    def __init__(self, path_to_wsi: str, step_size: int = 256):\n        self.path_to_wsi = path_to_wsi\n        self.step_size = step_size\n        self.patch_size = (self.step_size, self.step_size)\n        self.x = 0\n        self.y = 0\n\n    def patch_generator(self):\n        with openslide.OpenSlide(self.path_to_wsi) as wsi :\n            self.width = wsi.level_dimensions[0][0]\n            self.height = wsi.level_dimensions[0][1]\n            while self.y + self.step_size < self.height:\n                while self.x + self.step_size < self.width:\n                    coords = (self.x, self.y)\n                    yield wsi.read_region(coords, 0, self.patch_size), coords\n                    self.x += self.step_size\n                self.x = 0\n                self.y += self.step_size","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"step_size = 512\nimage_index = 0\nps = PatchSlicer(os.path.join(images_dir, images_filenames[image_index]), step_size)\nps_generator = ps.patch_generator()\n\nmean_pixel_value = []\ncoords = []\nfor patch, coord in ps_generator:\n    mean_pixel_value.append(np.array(patch).mean())\n    coords.append(coord)\nlen(coords)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"xy = zip(mean_pixel_value, coords)\nimage = openslide.OpenSlide(os.path.join(images_dir, images_filenames[image_index]))\ncols = 20\nrows = 20\noffset = 0\nplt.figure(figsize=(20, 20))\nplt.subplots_adjust(wspace=0, hspace=0)\n\nfor chart, (_, coord) in enumerate(sorted(xy)[offset:], 1):\n    ax = plt.subplot(rows, cols, chart)\n    ax.imshow(np.array(image.read_region(coord, 0, (step_size, step_size))))\n    ax.axis('off')\n    if chart == cols * rows:\n        break","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}