{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-27T09:02:47.463448Z","iopub.execute_input":"2023-08-27T09:02:47.464648Z","iopub.status.idle":"2023-08-27T09:02:57.347093Z","shell.execute_reply.started":"2023-08-27T09:02:47.464600Z","shell.execute_reply":"2023-08-27T09:02:57.345002Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%bash\npip install pandas\npip install pillow\n","metadata":{"execution":{"iopub.status.busy":"2023-08-27T09:03:51.331719Z","iopub.execute_input":"2023-08-27T09:03:51.332584Z","iopub.status.idle":"2023-08-27T09:04:17.563492Z","shell.execute_reply.started":"2023-08-27T09:03:51.332541Z","shell.execute_reply":"2023-08-27T09:04:17.562646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport json\nfrom PIL import Image\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-08-27T09:04:41.049548Z","iopub.execute_input":"2023-08-27T09:04:41.049984Z","iopub.status.idle":"2023-08-27T09:04:41.055271Z","shell.execute_reply.started":"2023-08-27T09:04:41.049954Z","shell.execute_reply":"2023-08-27T09:04:41.054304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_dir = \"/kaggle/input/hubmap-hacking-the-human-vasculature\"","metadata":{"execution":{"iopub.status.busy":"2023-08-27T09:05:45.075492Z","iopub.execute_input":"2023-08-27T09:05:45.076044Z","iopub.status.idle":"2023-08-27T09:05:45.081981Z","shell.execute_reply.started":"2023-08-27T09:05:45.076005Z","shell.execute_reply":"2023-08-27T09:05:45.080775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd {root_dir}","metadata":{"execution":{"iopub.status.busy":"2023-08-27T09:06:07.183968Z","iopub.execute_input":"2023-08-27T09:06:07.184379Z","iopub.status.idle":"2023-08-27T09:06:07.191939Z","shell.execute_reply.started":"2023-08-27T09:06:07.184349Z","shell.execute_reply":"2023-08-27T09:06:07.190664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load Metadata:\nLoad the metadata from tile_meta.csv and wsi_meta.csv files. This will help you understand information about each tile and the corresponding whole slide images.","metadata":{}},{"cell_type":"code","source":"tile_metadata = pd.read_csv('tile_meta.csv')\nwsi_metadata = pd.read_csv('wsi_meta.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-27T09:06:08.898607Z","iopub.execute_input":"2023-08-27T09:06:08.899070Z","iopub.status.idle":"2023-08-27T09:06:08.944199Z","shell.execute_reply.started":"2023-08-27T09:06:08.899036Z","shell.execute_reply":"2023-08-27T09:06:08.942951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load JSON Annotations:\nLoad the JSON annotations from polygons.jsonl file. This will give you information about the polygonal segmentation masks.","metadata":{}},{"cell_type":"code","source":"annotations = []\nwith open('polygons.jsonl', 'r') as f:\n    for line in f:\n        annotations.append(json.loads(line))\n","metadata":{"execution":{"iopub.status.busy":"2023-08-27T09:06:38.409667Z","iopub.execute_input":"2023-08-27T09:06:38.410076Z","iopub.status.idle":"2023-08-27T09:06:43.273438Z","shell.execute_reply.started":"2023-08-27T09:06:38.410046Z","shell.execute_reply":"2023-08-27T09:06:43.272261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Explore Data:\nNow you can start exploring the loaded data. For example, you can print the first few rows of the metadata and annotations to understand the structure of the data.","metadata":{}},{"cell_type":"code","source":"print(\"Tile Metadata:\")\ntile_metadata.head()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-08-27T10:05:15.034598Z","iopub.execute_input":"2023-08-27T10:05:15.035084Z","iopub.status.idle":"2023-08-27T10:05:15.051545Z","shell.execute_reply.started":"2023-08-27T10:05:15.035052Z","shell.execute_reply":"2023-08-27T10:05:15.050281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"\\nWSI Metadata:\")\nwsi_metadata.head()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-08-27T10:05:34.143154Z","iopub.execute_input":"2023-08-27T10:05:34.143615Z","iopub.status.idle":"2023-08-27T10:05:34.161893Z","shell.execute_reply.started":"2023-08-27T10:05:34.143582Z","shell.execute_reply":"2023-08-27T10:05:34.160977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"\\nAnnotations:\")\nprint(f\"#annotations: {len(annotations)}\")\nprint(annotations[:2])  # Print the first 2 annotations for demonstration","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-08-27T10:06:43.287990Z","iopub.execute_input":"2023-08-27T10:06:43.288402Z","iopub.status.idle":"2023-08-27T10:06:43.298061Z","shell.execute_reply.started":"2023-08-27T10:06:43.288374Z","shell.execute_reply":"2023-08-27T10:06:43.296601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_files = os.listdir('train')\ntest_files = os.listdir('test')","metadata":{"execution":{"iopub.status.busy":"2023-08-27T10:02:52.417771Z","iopub.execute_input":"2023-08-27T10:02:52.418228Z","iopub.status.idle":"2023-08-27T10:02:52.429630Z","shell.execute_reply.started":"2023-08-27T10:02:52.418187Z","shell.execute_reply":"2023-08-27T10:02:52.428327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_files[:5]","metadata":{"execution":{"iopub.status.busy":"2023-08-27T09:10:43.524564Z","iopub.execute_input":"2023-08-27T09:10:43.524972Z","iopub.status.idle":"2023-08-27T09:10:43.534560Z","shell.execute_reply.started":"2023-08-27T09:10:43.524931Z","shell.execute_reply":"2023-08-27T09:10:43.533270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_files[:5]","metadata":{"execution":{"iopub.status.busy":"2023-08-27T10:03:05.144761Z","iopub.execute_input":"2023-08-27T10:03:05.145198Z","iopub.status.idle":"2023-08-27T10:03:05.153523Z","shell.execute_reply.started":"2023-08-27T10:03:05.145165Z","shell.execute_reply":"2023-08-27T10:03:05.152148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"#training samples: {len(train_files)}\")","metadata":{"execution":{"iopub.status.busy":"2023-08-27T10:04:09.350710Z","iopub.execute_input":"2023-08-27T10:04:09.351145Z","iopub.status.idle":"2023-08-27T10:04:09.357725Z","shell.execute_reply.started":"2023-08-27T10:04:09.351115Z","shell.execute_reply":"2023-08-27T10:04:09.356246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"#testing samples: {len(test_files)}\")","metadata":{"execution":{"iopub.status.busy":"2023-08-27T10:04:28.449069Z","iopub.execute_input":"2023-08-27T10:04:28.449479Z","iopub.status.idle":"2023-08-27T10:04:28.456025Z","shell.execute_reply.started":"2023-08-27T10:04:28.449452Z","shell.execute_reply":"2023-08-27T10:04:28.454798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Visualize Images and Annotations:\nYou can choose a tile ID and visualize the corresponding image along with its annotations to get a visual understanding.","metadata":{}},{"cell_type":"code","source":"def visualize_tile(tile_id):\n    # Get metadata for the specific tile\n    tile_info = tile_metadata[tile_metadata['id'] == tile_id].iloc[0]\n    \n    # Construct the image path\n    image_path = f'train/{tile_info[\"id\"]}.tif'\n    \n    # Open the image using PIL\n    image = Image.open(image_path)\n\n    # Create a figure for plotting\n    plt.figure(figsize=(8, 8))\n    \n    # Display the image\n    plt.imshow(image)\n    \n    # Set the title of the plot\n    plt.title(f'Tile ID: {tile_info[\"id\"]}')\n    \n    # Turn off axis ticks\n    plt.axis('off')\n\n    # Iterate through annotations and plot masks in red\n    for ann in annotations:\n        if ann['id'] == tile_info['id']:\n            for mask in ann['annotations']:\n                coords = mask['coordinates']\n                # Reshape coordinates and extract x and y\n                reshaped_coords = np.reshape(coords, (-1, 2))\n                x_coords = reshaped_coords[:, 0]\n                y_coords = reshaped_coords[:, 1]\n                plt.plot(x_coords, y_coords, color='red')\n    \n    # Display the plot\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-27T09:24:23.852854Z","iopub.execute_input":"2023-08-27T09:24:23.853264Z","iopub.status.idle":"2023-08-27T09:24:23.864350Z","shell.execute_reply.started":"2023-08-27T09:24:23.853236Z","shell.execute_reply":"2023-08-27T09:24:23.862711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Let's visualize single Image and Mask","metadata":{}},{"cell_type":"code","source":"# Visualize a tile with its annotations\ntile_id_to_visualize = tile_metadata['id'].iloc[2]\nvisualize_tile(tile_id_to_visualize)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T09:24:34.587169Z","iopub.execute_input":"2023-08-27T09:24:34.588076Z","iopub.status.idle":"2023-08-27T09:24:35.099696Z","shell.execute_reply.started":"2023-08-27T09:24:34.588038Z","shell.execute_reply":"2023-08-27T09:24:35.097974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Visualize multiple images and masks","metadata":{}},{"cell_type":"code","source":"# Iterate over a range of tile IDs and visualize them\nfor idx in range(2, 7):  # Change the range as needed\n    tile_id_to_visualize = tile_metadata['id'].iloc[idx]\n    visualize_tile(tile_id_to_visualize)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-27T09:30:51.033578Z","iopub.execute_input":"2023-08-27T09:30:51.034066Z","iopub.status.idle":"2023-08-27T09:30:53.951377Z","shell.execute_reply.started":"2023-08-27T09:30:51.034017Z","shell.execute_reply":"2023-08-27T09:30:53.950405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission_df = pd.read_csv('sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-27T10:10:39.560569Z","iopub.execute_input":"2023-08-27T10:10:39.561473Z","iopub.status.idle":"2023-08-27T10:10:39.581652Z","shell.execute_reply.started":"2023-08-27T10:10:39.561426Z","shell.execute_reply":"2023-08-27T10:10:39.580526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-27T10:10:48.975728Z","iopub.execute_input":"2023-08-27T10:10:48.976142Z","iopub.status.idle":"2023-08-27T10:10:48.987244Z","shell.execute_reply.started":"2023-08-27T10:10:48.976112Z","shell.execute_reply":"2023-08-27T10:10:48.986037Z"},"trusted":true},"execution_count":null,"outputs":[]}]}