{"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 json\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tifffile as tiff\nfrom glob import glob\nfrom path import Path\nfrom collections import defaultdict","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-26T00:27:32.406773Z","iopub.execute_input":"2023-05-26T00:27:32.407217Z","iopub.status.idle":"2023-05-26T00:27:32.683511Z","shell.execute_reply.started":"2023-05-26T00:27:32.407183Z","shell.execute_reply":"2023-05-26T00:27:32.682617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Set parameters","metadata":{}},{"cell_type":"code","source":"# File path settings\nBASE_DIR = Path('/kaggle/input/hubmap-hacking-the-human-vasculature')\n\ntrain_paths = glob(f'{BASE_DIR}/train/*')\ntest_paths = glob(f'{BASE_DIR}/test/*')\npolygons_path = f'{BASE_DIR}/polygons.jsonl'\n\nprint(f'Num of Trains: {len(train_paths)}')\nprint(f'Num of Tests: {len(test_paths)}')","metadata":{"execution":{"iopub.status.busy":"2023-05-26T00:27:32.685758Z","iopub.execute_input":"2023-05-26T00:27:32.686250Z","iopub.status.idle":"2023-05-26T00:27:33.105112Z","shell.execute_reply.started":"2023-05-26T00:27:32.686206Z","shell.execute_reply":"2023-05-26T00:27:33.103765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## EDA","metadata":{}},{"cell_type":"markdown","source":"### Check annotations","metadata":{}},{"cell_type":"code","source":"polygons = pd.read_json(polygons_path, orient='records', lines=True)\npolygons","metadata":{"execution":{"iopub.status.busy":"2023-05-26T00:27:33.111412Z","iopub.execute_input":"2023-05-26T00:27:33.114654Z","iopub.status.idle":"2023-05-26T00:27:38.368704Z","shell.execute_reply.started":"2023-05-26T00:27:33.114588Z","shell.execute_reply":"2023-05-26T00:27:38.367538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(polygons)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T00:27:38.371995Z","iopub.execute_input":"2023-05-26T00:27:38.372605Z","iopub.status.idle":"2023-05-26T00:27:38.379164Z","shell.execute_reply.started":"2023-05-26T00:27:38.372569Z","shell.execute_reply":"2023-05-26T00:27:38.378053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The number of training data is 7033, while the number of polygons is 1633, which means not all data have annotations.","metadata":{}},{"cell_type":"code","source":"# chack the polygon which has vessel annotation\ncoordinates_list = defaultdict(list)\nnum_vessel = 0\nfor i in range(len(polygons)):\n    annotations = polygons.loc[i].annotations\n    id_ = polygons.loc[i].id\n    for annotation in annotations:\n        if annotation['type'] == 'blood_vessel':\n            coordinates_list[id_].append(annotation['coordinates'])\n            num_vessel += 1\nprint(f'Total number of vessel labels: {num_vessel}')\nprint(f'The number of images which have vessel labels: {len(coordinates_list.keys())}')","metadata":{"execution":{"iopub.status.busy":"2023-05-26T00:27:38.381199Z","iopub.execute_input":"2023-05-26T00:27:38.382077Z","iopub.status.idle":"2023-05-26T00:27:38.604700Z","shell.execute_reply.started":"2023-05-26T00:27:38.382033Z","shell.execute_reply":"2023-05-26T00:27:38.603205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freqs_vessel = []\nfor value in coordinates_list.values():\n    freqs_vessel.append(len(value))\nplt.hist(freqs_vessel, bins=30)\nplt.xlabel('Number of vessel labels')\nplt.ylabel('freqency')","metadata":{"execution":{"iopub.status.busy":"2023-05-26T00:27:38.606819Z","iopub.execute_input":"2023-05-26T00:27:38.607309Z","iopub.status.idle":"2023-05-26T00:27:38.928966Z","shell.execute_reply.started":"2023-05-26T00:27:38.607266Z","shell.execute_reply":"2023-05-26T00:27:38.927731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"In this competition, our target is predicting the blood_vessel's mask.  \nSo, we count the number of annotations which have blood_vessel labels.  \nFrom the result, the number of annotations which have blood_vessel labels is 1622. Therefore 11 polygons have no blood_vessel labels.  \nOn the other hand, the total of blood_vessel labels is 16054. So a polygon has one or more blood_vessel labels.","metadata":{}},{"cell_type":"markdown","source":"### Chack the images and anotation masks","metadata":{}},{"cell_type":"code","source":"def transform_coordinate_into_mask(coordinates):\n    \"\"\"\n    transform coordinate into mask image\n    \n    parameters\n    ----------\n    coordinates: list\n        coordinate of mask\n    \n    returns\n    ----------\n    mask: numpy.array\n        image of mask\n    \"\"\"\n    mask = np.zeros(shape=(512, 512))\n    \n    for coordinate in coordinates:\n        for axis in coordinate[0]:\n            mask[axis[1], axis[0]] = 1\n    \n    return mask","metadata":{"execution":{"iopub.status.busy":"2023-05-26T00:27:38.930214Z","iopub.execute_input":"2023-05-26T00:27:38.930562Z","iopub.status.idle":"2023-05-26T00:27:38.937335Z","shell.execute_reply.started":"2023-05-26T00:27:38.930533Z","shell.execute_reply":"2023-05-26T00:27:38.936123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training images\nfor i, items in enumerate(coordinates_list.items()):\n    image = tiff.imread(f'{BASE_DIR}/train/{items[0]}.tif')\n    mask = transform_coordinate_into_mask(items[1])\n    \n    fig, ax = plt.subplots(1, 2)\n    ax[0].imshow(image)\n    ax[1].imshow(mask)\n    plt.show()\n    if i == 10:\n        break\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-26T00:27:38.938844Z","iopub.execute_input":"2023-05-26T00:27:38.939217Z","iopub.status.idle":"2023-05-26T00:27:43.634539Z","shell.execute_reply.started":"2023-05-26T00:27:38.939187Z","shell.execute_reply":"2023-05-26T00:27:43.633394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test image\nimage = tiff.imread(test_paths)\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T00:47:46.926079Z","iopub.execute_input":"2023-05-26T00:47:46.926575Z","iopub.status.idle":"2023-05-26T00:47:47.334402Z","shell.execute_reply.started":"2023-05-26T00:47:46.926541Z","shell.execute_reply":"2023-05-26T00:47:47.333264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}