{"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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-22T20:20:49.705157Z","iopub.execute_input":"2023-05-22T20:20:49.705562Z","iopub.status.idle":"2023-05-22T20:20:49.711673Z","shell.execute_reply.started":"2023-05-22T20:20:49.705518Z","shell.execute_reply":"2023-05-22T20:20:49.710436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nfrom PIL import Image\nfrom matplotlib import pyplot as plt\n%matplotlib inline\n\nfrom skimage import draw","metadata":{"execution":{"iopub.status.busy":"2023-05-22T20:29:23.140502Z","iopub.execute_input":"2023-05-22T20:29:23.141080Z","iopub.status.idle":"2023-05-22T20:29:23.342069Z","shell.execute_reply.started":"2023-05-22T20:29:23.141041Z","shell.execute_reply":"2023-05-22T20:29:23.340439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\n\nwith open('../input/hubmap-hacking-the-human-vasculature/polygons.jsonl', 'r') as json_file:\n    json_labels = list(json_file)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T20:22:00.068610Z","iopub.execute_input":"2023-05-22T20:22:00.069149Z","iopub.status.idle":"2023-05-22T20:22:00.482473Z","shell.execute_reply.started":"2023-05-22T20:22:00.069112Z","shell.execute_reply":"2023-05-22T20:22:00.481278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"json_labels = [json.loads(i) for i in json_labels]\nlen(json_labels)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T20:23:23.335414Z","iopub.execute_input":"2023-05-22T20:23:23.335872Z","iopub.status.idle":"2023-05-22T20:23:23.343805Z","shell.execute_reply.started":"2023-05-22T20:23:23.335813Z","shell.execute_reply":"2023-05-22T20:23:23.342426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\n","metadata":{"execution":{"iopub.status.busy":"2023-05-22T20:58:25.579335Z","iopub.execute_input":"2023-05-22T20:58:25.579751Z","iopub.status.idle":"2023-05-22T20:58:25.586074Z","shell.execute_reply.started":"2023-05-22T20:58:25.579720Z","shell.execute_reply":"2023-05-22T20:58:25.584756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_mask(datapoint):\n    # Image.open(image)\n    for annot in datapoint['annotations']:\n        cords = annot['coordinates']\n        if annot['type']==\"blood_vessel\":\n            c=\"r\"\n        elif annot['type']==\"glomerulus\":\n            c=\"b\"\n        elif annot['type']==\"unsure\":\n            c=\"gray\"\n#         print(annot['type'], c)\n        x, y = np.array([i[0] for i in cords[0]]), np.asarray([i[1] for i in cords[0]])\n\n        x_offset = x - np.min(x)\n        y_offset = np.max(y) - y\n        actimage = np.array(Image.open(image_path))\n        image = draw.polygon2mask(\n                (512, 512),\n                np.stack((y_offset, x_offset), axis=1)\n                )\n\n        plt.scatter(x, y, s=0)\n        plt.fill(x, y, c, alpha=0.5)\n    plt.imshow(actimage, interpolation='nearest', cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-05-22T20:55:58.285046Z","iopub.execute_input":"2023-05-22T20:55:58.285472Z","iopub.status.idle":"2023-05-22T20:55:58.297268Z","shell.execute_reply.started":"2023-05-22T20:55:58.285439Z","shell.execute_reply":"2023-05-22T20:55:58.296120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):\n    datapoint = json_labels[i]\n    image_path = os.path.join(\"../input/hubmap-hacking-the-human-vasculature/train/\", f\"{datapoint['id']}.tif\")\n    plot_mask(datapoint)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-22T21:02:39.894957Z","iopub.execute_input":"2023-05-22T21:02:39.895401Z","iopub.status.idle":"2023-05-22T21:02:42.895457Z","shell.execute_reply.started":"2023-05-22T21:02:39.895367Z","shell.execute_reply":"2023-05-22T21:02:42.894263Z"},"trusted":true},"execution_count":null,"outputs":[]}]}