{"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":"# Short notebook to help you visualize the polygons data on images, enjoy.","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport json\nimport matplotlib.pyplot as plt\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2023-06-10T19:50:26.541091Z","iopub.execute_input":"2023-06-10T19:50:26.541487Z","iopub.status.idle":"2023-06-10T19:50:26.546544Z","shell.execute_reply.started":"2023-06-10T19:50:26.541456Z","shell.execute_reply":"2023-06-10T19:50:26.545577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading data","metadata":{}},{"cell_type":"code","source":"polygon_df = pd.read_json(path_or_buf=\"/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl\", lines=True)\npolygon_df","metadata":{"execution":{"iopub.status.busy":"2023-06-10T19:50:26.873321Z","iopub.execute_input":"2023-06-10T19:50:26.874060Z","iopub.status.idle":"2023-06-10T19:50:29.783746Z","shell.execute_reply.started":"2023-06-10T19:50:26.874017Z","shell.execute_reply":"2023-06-10T19:50:29.782562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Splitting data into separate columns","metadata":{}},{"cell_type":"code","source":"df_flattened = polygon_df.explode('annotations')\ntemp_df = df_flattened['annotations'].apply(pd.Series)\npoly_df = pd.concat([polygon_df['id'], temp_df], axis=1)\npoly_df.reset_index(inplace=True, drop=True)\npoly_df","metadata":{"execution":{"iopub.status.busy":"2023-06-10T19:50:29.785627Z","iopub.execute_input":"2023-06-10T19:50:29.786396Z","iopub.status.idle":"2023-06-10T19:50:36.847170Z","shell.execute_reply.started":"2023-06-10T19:50:29.786364Z","shell.execute_reply":"2023-06-10T19:50:36.846068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plotting sample base image","metadata":{}},{"cell_type":"code","source":"sample_image_name = poly_df['id'][0]\nsample_img_path = \"/kaggle/input/hubmap-hacking-the-human-vasculature/train/\" + sample_image_name + \".tif\"\nsample_img_path","metadata":{"execution":{"iopub.status.busy":"2023-06-10T19:50:36.848696Z","iopub.execute_input":"2023-06-10T19:50:36.849042Z","iopub.status.idle":"2023-06-10T19:50:36.856676Z","shell.execute_reply.started":"2023-06-10T19:50:36.849006Z","shell.execute_reply":"2023-06-10T19:50:36.855530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_image = Image.open(sample_img_path)\nplt.imshow(base_image)\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-10T19:50:36.859202Z","iopub.execute_input":"2023-06-10T19:50:36.859625Z","iopub.status.idle":"2023-06-10T19:50:37.113933Z","shell.execute_reply.started":"2023-06-10T19:50:36.859596Z","shell.execute_reply":"2023-06-10T19:50:37.112714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_img(polygon_data, image_name):\n    \"\"\"\n    Opens image from a path, reads data about polygons correlated to said image, draws it and then plots new image\n    \"\"\"\n    \n    img_data = polygon_data.query('id == @image_name')\n    img_data.reset_index(inplace=True, drop=True)\n    \n    image_path = \"/kaggle/input/hubmap-hacking-the-human-vasculature/train/\" + image_name + \".tif\"\n    image = Image.open(image_path)\n    \n    for idx, row in img_data.iterrows():\n        if row['type'] == 'glomerulus':\n            color = 'green'\n        elif row['type'] == 'blood_vessel':\n            color = 'red'\n        else:\n            color = 'yellow'\n\n        sublist = row['coordinates'][0]\n        x = []\n        y = []\n\n        for datapoint in sublist:\n            x.append(datapoint[0])\n            y.append(datapoint[1])\n\n        plt.scatter(x, y, s=0)\n        plt.fill(x, y, color, alpha=0.5)\n        \n    plt.imshow(image)\n    \nimage_name = poly_df['id'][0]\nplot_img(poly_df, image_name)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T19:50:37.115279Z","iopub.execute_input":"2023-06-10T19:50:37.115600Z","iopub.status.idle":"2023-06-10T19:50:37.814817Z","shell.execute_reply.started":"2023-06-10T19:50:37.115574Z","shell.execute_reply":"2023-06-10T19:50:37.810447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_name = poly_df['id'][260]\nplot_img(poly_df, image_name)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T19:50:37.816523Z","iopub.execute_input":"2023-06-10T19:50:37.817809Z","iopub.status.idle":"2023-06-10T19:50:38.614143Z","shell.execute_reply.started":"2023-06-10T19:50:37.817763Z","shell.execute_reply":"2023-06-10T19:50:38.612927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_name = poly_df['id'][400]\nplot_img(poly_df, image_name)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T19:50:38.615683Z","iopub.execute_input":"2023-06-10T19:50:38.616131Z","iopub.status.idle":"2023-06-10T19:50:39.679409Z","shell.execute_reply.started":"2023-06-10T19:50:38.616091Z","shell.execute_reply":"2023-06-10T19:50:39.678313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_name = poly_df['id'][6200]\nplot_img(poly_df, image_name)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T19:50:39.681285Z","iopub.execute_input":"2023-06-10T19:50:39.681911Z","iopub.status.idle":"2023-06-10T19:50:40.383888Z","shell.execute_reply.started":"2023-06-10T19:50:39.681869Z","shell.execute_reply":"2023-06-10T19:50:40.382903Z"},"trusted":true},"execution_count":null,"outputs":[]}]}