{"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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport cv2\nimport seaborn \nimport matplotlib.pyplot as plt\nimport gc\nimport queue\nfrom shapely.geometry import Polygon, Point\nimport time","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-15T05:10:01.187224Z","iopub.execute_input":"2023-08-15T05:10:01.187548Z","iopub.status.idle":"2023-08-15T05:10:02.066453Z","shell.execute_reply.started":"2023-08-15T05:10:01.187520Z","shell.execute_reply":"2023-08-15T05:10:02.064747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_folder = \"/kaggle/input/hubmap-hacking-the-human-vasculature/train/\"\ntest_folder = \"/kaggle/input/hubmap-hacking-the-human-vasculature/test/\"","metadata":{"execution":{"iopub.status.busy":"2023-08-15T05:10:02.068532Z","iopub.execute_input":"2023-08-15T05:10:02.068843Z","iopub.status.idle":"2023-08-15T05:10:02.073724Z","shell.execute_reply.started":"2023-08-15T05:10:02.068816Z","shell.execute_reply":"2023-08-15T05:10:02.072234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wsi_fpath = \"/kaggle/input/hubmap-hacking-the-human-vasculature/wsi_meta.csv\"\ntile_fpath = \"/kaggle/input/hubmap-hacking-the-human-vasculature/tile_meta.csv\"\nsample_fpath = \"/kaggle/input/hubmap-hacking-the-human-vasculature/sample_submission.csv\"\npolygon_fpath = \"/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl\"","metadata":{"execution":{"iopub.status.busy":"2023-08-15T05:10:02.075360Z","iopub.execute_input":"2023-08-15T05:10:02.075744Z","iopub.status.idle":"2023-08-15T05:10:02.087725Z","shell.execute_reply.started":"2023-08-15T05:10:02.075716Z","shell.execute_reply":"2023-08-15T05:10:02.086916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json","metadata":{"execution":{"iopub.status.busy":"2023-08-15T05:10:02.088976Z","iopub.execute_input":"2023-08-15T05:10:02.090026Z","iopub.status.idle":"2023-08-15T05:10:02.101964Z","shell.execute_reply.started":"2023-08-15T05:10:02.089973Z","shell.execute_reply":"2023-08-15T05:10:02.100728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(polygon_fpath) as f:\n    polygon = [json.loads(line) for line in f]","metadata":{"execution":{"iopub.status.busy":"2023-08-15T05:10:02.181052Z","iopub.execute_input":"2023-08-15T05:10:02.181490Z","iopub.status.idle":"2023-08-15T05:10:05.168902Z","shell.execute_reply.started":"2023-08-15T05:10:02.181456Z","shell.execute_reply":"2023-08-15T05:10:05.167824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_files = os.listdir(train_folder)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T05:10:05.170590Z","iopub.execute_input":"2023-08-15T05:10:05.170947Z","iopub.status.idle":"2023-08-15T05:10:05.445868Z","shell.execute_reply.started":"2023-08-15T05:10:05.170913Z","shell.execute_reply":"2023-08-15T05:10:05.444673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tile_df = pd.read_csv(tile_fpath)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T05:10:05.447059Z","iopub.execute_input":"2023-08-15T05:10:05.448107Z","iopub.status.idle":"2023-08-15T05:10:05.805937Z","shell.execute_reply.started":"2023-08-15T05:10:05.448070Z","shell.execute_reply":"2023-08-15T05:10:05.804300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wsi_df = pd.read_csv(wsi_fpath)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T05:10:05.809015Z","iopub.execute_input":"2023-08-15T05:10:05.809327Z","iopub.status.idle":"2023-08-15T05:10:05.825550Z","shell.execute_reply.started":"2023-08-15T05:10:05.809300Z","shell.execute_reply":"2023-08-15T05:10:05.824464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df = pd.read_csv(sample_fpath)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T05:10:05.826756Z","iopub.execute_input":"2023-08-15T05:10:05.827045Z","iopub.status.idle":"2023-08-15T05:10:05.837096Z","shell.execute_reply.started":"2023-08-15T05:10:05.827020Z","shell.execute_reply":"2023-08-15T05:10:05.836081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(polygon[0][\"annotations\"])","metadata":{"execution":{"iopub.status.busy":"2023-08-15T05:10:05.838500Z","iopub.execute_input":"2023-08-15T05:10:05.838768Z","iopub.status.idle":"2023-08-15T05:10:05.850995Z","shell.execute_reply.started":"2023-08-15T05:10:05.838744Z","shell.execute_reply":"2023-08-15T05:10:05.849914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Masking_Function:\n    \n    def __init__(self):\n        \n        N = 512\n        self.visited = np.zeros((N, N), dtype = \"bool\")\n        self.polys = []\n        self.labels = []\n        self.startpoints = []\n        self.mask = np.zeros((N, N), dtype = \"int\")\n        self.N = N\n        self.count = 0\n        \n    def read_polygons(self, polygon):\n        \n        for data in polygon[\"annotations\"]:\n\n            ptype = data[\"type\"]\n            crd = data[\"coordinates\"][0]\n            poly = Polygon(crd)\n            \n            if ptype == \"blood_vessel\":\n                label = 1\n            elif ptype == \"glomerulus\":\n                label = 2\n            elif ptype == \"unsure\":\n                label = 3\n            \n            x, y = crd[0]\n            self.polys.append(poly)\n            self.labels.append(label)\n            self.startpoints.append([x, y])\n\n\n\n        \n    def get_neighbours(self, x, y):\n        \n        neigh = []\n        if x+1 < self.N:\n            neigh.append([x + 1, y])\n        if y+1 < self.N:\n            neigh.append([x, y + 1])\n        if x - 1 >= 0:\n            neigh.append([x - 1, y])\n        if y - 1 >= 0:\n            neigh.append([x, y - 1])\n        \n        return neigh\n        \n    def bfs_masking(self, x, y, k):\n        \n        if self.visited[x, y]:\n            return \n        \n        Q = queue.Queue()\n        \n        Q.put([x, y])\n        self.visited[x, y] = True\n \n        while not Q.empty():\n            x, y = Q.get(timeout = 0.01)\n            \n            if self.polys[k].contains(Point(x, y)):\n                self.mask[y, x] = self.labels[k]\n            \n                neigh = self.get_neighbours(x, y)\n            \n                for x, y in neigh:\n                    if not self.visited[x,y]:\n                        self.visited[x, y] = True\n                        Q.put([x, y])        \n        \n    \n    def masking(self):\n        \n        M = len(self.polys)\n        \n        for k in range(M):\n            \n            self.count = 0\n            \n            x, y = self.startpoints[k]\n            self.bfs_masking(x, y, k)\n            neigh = self.get_neighbours(x, y)\n            \n            for x, y in neigh:\n                self.bfs_masking(x, y, k)\n                \n\n              \n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-08-15T05:10:05.853131Z","iopub.execute_input":"2023-08-15T05:10:05.853507Z","iopub.status.idle":"2023-08-15T05:10:05.866848Z","shell.execute_reply.started":"2023-08-15T05:10:05.853476Z","shell.execute_reply":"2023-08-15T05:10:05.865841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NP = len(polygon)\nN = 512\nmask_mat = np.zeros((NP, N, N) , dtype = np.uint8)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T05:11:36.600165Z","iopub.execute_input":"2023-08-15T05:11:36.600517Z","iopub.status.idle":"2023-08-15T05:11:36.605877Z","shell.execute_reply.started":"2023-08-15T05:11:36.600491Z","shell.execute_reply":"2023-08-15T05:11:36.604698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"time1 = time.time()\nfor ID in range(NP):\n    \n    if ID % 50 == 0:\n        print(ID)\n\n    MS = Masking_Function()\n    MS.read_polygons(polygon[ID])\n    MS.masking()\n    mask_mat[ID] = MS.mask\n    \n    \ntime2 = time.time()\n\ntime3 = time2- time1\ntime3 = np.round(time3)\n\nprint(time3, \"sec to develop mask\")","metadata":{"execution":{"iopub.status.busy":"2023-08-15T05:11:36.996265Z","iopub.execute_input":"2023-08-15T05:11:36.996613Z","iopub.status.idle":"2023-08-15T05:12:12.236440Z","shell.execute_reply.started":"2023-08-15T05:11:36.996588Z","shell.execute_reply":"2023-08-15T05:12:12.234789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.save(\"mask_mat.npy\", mask_mat)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plt.imshow(mask_mat[69]*200)","metadata":{"execution":{"iopub.status.busy":"2023-08-11T14:17:21.085723Z","iopub.execute_input":"2023-08-11T14:17:21.086174Z","iopub.status.idle":"2023-08-11T14:17:21.092651Z","shell.execute_reply.started":"2023-08-11T14:17:21.086136Z","shell.execute_reply":"2023-08-11T14:17:21.090865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ID = 69\n#img = cv2.imread(train_folder + polygon[ID][\"id\"] + \".tif\")[:,:,::-1]\n#plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-08-11T14:17:17.110621Z","iopub.execute_input":"2023-08-11T14:17:17.111022Z","iopub.status.idle":"2023-08-11T14:17:17.116412Z","shell.execute_reply.started":"2023-08-11T14:17:17.110991Z","shell.execute_reply":"2023-08-11T14:17:17.115079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plt.imshow(img*mask_mat[80].reshape((N, N, 1)))","metadata":{"execution":{"iopub.status.busy":"2023-08-10T14:45:32.997516Z","iopub.execute_input":"2023-08-10T14:45:32.997905Z","iopub.status.idle":"2023-08-10T14:45:33.224631Z","shell.execute_reply.started":"2023-08-10T14:45:32.997872Z","shell.execute_reply":"2023-08-10T14:45:33.222985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}