{"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 os\n\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom tqdm.notebook import tqdm","metadata":{"_kg_hide-input":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%config Completer.use_jedi = False\nsns.set_theme(style=\"whitegrid\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_annot_path = '../input/hubmap-kidney-segmentation/train'\ndset_info_path = '../input/hubmap-kidney-segmentation/HuBMAP-20-dataset_information.csv'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dset_info = pd.read_csv(dset_info_path)\ndset_info.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"glomerulu_dict = {'filename':[],\n                   'width_img':[],\n                   'height_img':[],\n                   'width_seg':[],\n                   'height_seg':[],\n                   'area':[],\n                   'perimeter':[],\n                   'center_x':[],\n                   'center_y':[],\n                   'w/h':[],\n                   }\ncols = ['width_pixels', 'height_pixels',\n        'glomerulus_segmentation_file']\n\nfor w_img, h_img, json_filename in tqdm(dset_info[cols].values):\n    \n    json_path = os.path.join(root_annot_path, json_filename)\n    if not os.path.exists(json_path):\n        continue\n    \n    with open(json_path) as json_file:\n        data = json.load(json_file)\n    \n    for glomerulu_info in data:\n        contour = glomerulu_info['geometry']['coordinates']\n        contour = np.array(contour, dtype=np.float32)\n        area = cv2.contourArea(contour)\n        perimeter = cv2.arcLength(contour, True)\n\n        xmin, ymin = contour[0].min(axis=0)\n        xmax, ymax = contour[0].max(axis=0)\n\n        width = xmax - xmin\n        height = ymax - ymin\n\n        center_x = np.mean([xmax, xmin])\n        center_y = np.mean([ymax, ymin])\n        \n        glomerulu_dict['filename'].append(json_filename.split('.')[0])\n        glomerulu_dict['width_img'].append(w_img)\n        glomerulu_dict['height_img'].append(h_img)\n        glomerulu_dict['width_seg'].append(width)\n        glomerulu_dict['height_seg'].append(height)\n        glomerulu_dict['w/h'].append(width/height)\n        glomerulu_dict['area'].append(area)\n        glomerulu_dict['perimeter'].append(perimeter)\n        glomerulu_dict['center_x'].append(center_x)\n        glomerulu_dict['center_y'].append(center_y)\n                ","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"glomerulu_df = pd.DataFrame(glomerulu_dict)\nglomerulu_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"glomerulu_df.info()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"glomerulu_df.describe()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sorted_filenames = glomerulu_df.filename.value_counts().index\nsorted_filenames","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,5))\nplt.xticks(rotation=45)\nplt.title('Number of glomerulus for each image')\nsns.countplot(data=glomerulu_df, x='filename', order=sorted_filenames);","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nfor filename, w, h in dset_info[['image_file', 'width_pixels', 'height_pixels']].values:\n    plt.text(w, h, filename, fontsize=10)\nsns.scatterplot(data=dset_info, x=\"width_pixels\", y=\"height_pixels\");","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,5))\nplt.xticks(rotation=45)\nplt.title('Segment width')\nsns.boxplot(data=glomerulu_df, x='filename', y='width_seg', order=sorted_filenames);","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,5))\nplt.xticks(rotation=45)\nplt.title('Segment height')\nsns.boxplot(data=glomerulu_df, x='filename', y='height_seg', order=sorted_filenames);","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.jointplot(data=glomerulu_df, x=\"width_seg\", y=\"height_seg\");","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,5))\nplt.xticks(rotation=45)\nplt.title('Width/height')\nsns.boxplot(data=glomerulu_df, x='filename', y='w/h', order=sorted_filenames);","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,6))\nplt.subplots_adjust(hspace=.5)\n\nplt.subplot(2,1,1)\nplt.title('Width/height')\nsns.histplot(data=glomerulu_df, x='w/h');\n\nplt.subplot(2,1,2)\nplt.xlim(1.5,2.45)\nplt.ylim(0, 20)\nplt.title('Width/height > 1.5')\nsns.histplot(data=glomerulu_df, x='w/h');","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(glomerulu_df['w/h']>1.5).sum()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.jointplot(data=glomerulu_df, x=\"center_x\", y=\"center_y\");","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,5))\nplt.xticks(rotation=45)\nplt.title('Area')\nsns.boxplot(data=glomerulu_df, x='filename', y='area', order=sorted_filenames);","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,6))\nplt.title('Area')\nsns.histplot(data=glomerulu_df, x='area');","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}