{"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 libraries\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimport seaborn as sns\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-27T18:52:14.800106Z","iopub.execute_input":"2022-06-27T18:52:14.800477Z","iopub.status.idle":"2022-06-27T18:52:14.811158Z","shell.execute_reply.started":"2022-06-27T18:52:14.800448Z","shell.execute_reply":"2022-06-27T18:52:14.810188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"###  *In this notebook, we are going to get basic understanding of the csv files and the images provided in the dataset*","metadata":{}},{"cell_type":"code","source":"# PATHS\nTRAIN_IMAGES = \"../input/hubmap-organ-segmentation/train_images/*.tiff\"\nTRAIN_CSV = \"../input/hubmap-organ-segmentation/train.csv\"\nTEST_CSV = \"../input/hubmap-organ-segmentation/test.csv\"","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:11:51.617942Z","iopub.execute_input":"2022-06-27T19:11:51.618285Z","iopub.status.idle":"2022-06-27T19:11:51.623481Z","shell.execute_reply.started":"2022-06-27T19:11:51.618258Z","shell.execute_reply":"2022-06-27T19:11:51.622355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Analyzing csv files","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(TRAIN_CSV)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-27T18:44:35.717863Z","iopub.execute_input":"2022-06-27T18:44:35.719206Z","iopub.status.idle":"2022-06-27T18:44:36.111741Z","shell.execute_reply.started":"2022-06-27T18:44:35.719175Z","shell.execute_reply":"2022-06-27T18:44:36.110865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-27T18:44:51.895850Z","iopub.execute_input":"2022-06-27T18:44:51.896209Z","iopub.status.idle":"2022-06-27T18:44:51.903020Z","shell.execute_reply.started":"2022-06-27T18:44:51.896179Z","shell.execute_reply":"2022-06-27T18:44:51.902456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-06-27T18:44:58.797202Z","iopub.execute_input":"2022-06-27T18:44:58.797616Z","iopub.status.idle":"2022-06-27T18:44:58.818234Z","shell.execute_reply.started":"2022-06-27T18:44:58.797582Z","shell.execute_reply":"2022-06-27T18:44:58.817291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nplt.title(\"Unique values in train columns\")\nunique_counts = train_df[[i for i in train_df.columns if i not in [\"id\",\"rle\"]]].nunique().to_dict()\nax = sns.barplot(list(unique_counts.keys()), list(unique_counts.values()))\nax.bar_label(ax.containers[0])\nplt.plot()","metadata":{"execution":{"iopub.status.busy":"2022-06-27T18:55:04.313374Z","iopub.execute_input":"2022-06-27T18:55:04.313740Z","iopub.status.idle":"2022-06-27T18:55:04.497753Z","shell.execute_reply.started":"2022-06-27T18:55:04.313712Z","shell.execute_reply":"2022-06-27T18:55:04.496600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in [\"organ\",\"data_source\",\"pixel_size\",\"tissue_thickness\",\"sex\"]:\n    print(train_df[i].value_counts())","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:00:13.678026Z","iopub.execute_input":"2022-06-27T19:00:13.678385Z","iopub.status.idle":"2022-06-27T19:00:13.690090Z","shell.execute_reply.started":"2022-06-27T19:00:13.678357Z","shell.execute_reply":"2022-06-27T19:00:13.689102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.title(\"Gender distribution\")\nax = train_df[i].value_counts(\"%\").mul(100).plot.bar()\nax.bar_label(ax.containers[0])","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:04:17.103946Z","iopub.execute_input":"2022-06-27T19:04:17.104282Z","iopub.status.idle":"2022-06-27T19:04:17.228854Z","shell.execute_reply.started":"2022-06-27T19:04:17.104259Z","shell.execute_reply":"2022-06-27T19:04:17.227845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* #### The columns: \"data_source\",\"pixel_size\",\"tissue_thickness\" have only single unique value\n* #### Column Sex has Male and Female\n* #### Column organ has 5 unique values","metadata":{}},{"cell_type":"code","source":"train_df[\"age\"].describe","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:05:00.414864Z","iopub.execute_input":"2022-06-27T19:05:00.415181Z","iopub.status.idle":"2022-06-27T19:05:00.425346Z","shell.execute_reply.started":"2022-06-27T19:05:00.415158Z","shell.execute_reply":"2022-06-27T19:05:00.424403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.title(\"Age distribution\")\nsns.boxplot(x=train_df[\"age\"])","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:08:26.864450Z","iopub.execute_input":"2022-06-27T19:08:26.864801Z","iopub.status.idle":"2022-06-27T19:08:26.994931Z","shell.execute_reply.started":"2022-06-27T19:08:26.864773Z","shell.execute_reply":"2022-06-27T19:08:26.994097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.title(\"Gender wise age distribution\")\nsns.boxplot(x=\"age\", y=\"sex\",data=train_df)","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:08:16.823828Z","iopub.execute_input":"2022-06-27T19:08:16.824188Z","iopub.status.idle":"2022-06-27T19:08:16.977520Z","shell.execute_reply.started":"2022-06-27T19:08:16.824159Z","shell.execute_reply":"2022-06-27T19:08:16.976585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"img_height\"].value_counts() # all are high resolution squared images ","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:10:30.862714Z","iopub.execute_input":"2022-06-27T19:10:30.863116Z","iopub.status.idle":"2022-06-27T19:10:30.872089Z","shell.execute_reply.started":"2022-06-27T19:10:30.863086Z","shell.execute_reply":"2022-06-27T19:10:30.870991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test file: single value\ntest_df = pd.read_csv(TEST_CSV)\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:12:08.404046Z","iopub.execute_input":"2022-06-27T19:12:08.404315Z","iopub.status.idle":"2022-06-27T19:12:08.426781Z","shell.execute_reply.started":"2022-06-27T19:12:08.404294Z","shell.execute_reply":"2022-06-27T19:12:08.425681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n## Visualizing images","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:14:16.513310Z","iopub.execute_input":"2022-06-27T19:14:16.513691Z","iopub.status.idle":"2022-06-27T19:14:16.518577Z","shell.execute_reply.started":"2022-06-27T19:14:16.513663Z","shell.execute_reply":"2022-06-27T19:14:16.517451Z"}}},{"cell_type":"code","source":"#https://www.kaggle.com/code/pestipeti/decoding-rle-masks/notebook\ndef rle2mask(mask_rle, shape=(3000,3000)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (width,height) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0::2], s[1::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape).T","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Original images\")\norgans_df = train_df.groupby(\"organ\").nth(-1).reset_index()\nfig, ax = plt.subplots(1,len(organs_df),figsize=(15,8))\n\nfor _, row in organs_df.iterrows():\n    image = plt.imread(\"../input/hubmap-organ-segmentation/train_images/\"+str(row.id)+\".tiff\")\n    ax[_].imshow(image)\n    ax[_].set_title(row[\"organ\"])","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:45:14.983875Z","iopub.execute_input":"2022-06-27T19:45:14.984219Z","iopub.status.idle":"2022-06-27T19:45:21.508854Z","shell.execute_reply.started":"2022-06-27T19:45:14.984192Z","shell.execute_reply":"2022-06-27T19:45:21.507869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Original images with rle mask\")\n\norgans_df = train_df.groupby(\"organ\").nth(-1).reset_index()\nfig, ax = plt.subplots(1,len(organs_df),figsize=(15,8))\n\nfor _, row in organs_df.iterrows():\n    image = plt.imread(\"../input/hubmap-organ-segmentation/train_images/\"+str(row.id)+\".tiff\")\n    mask = rle2mask(row.rle,shape=(row.img_height,row.img_width))\n    ax[_].imshow(image)\n    ax[_].imshow(mask,alpha=0.2)\n    ax[_].set_title(row[\"organ\"])","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:45:27.149743Z","iopub.execute_input":"2022-06-27T19:45:27.150166Z","iopub.status.idle":"2022-06-27T19:45:33.835501Z","shell.execute_reply.started":"2022-06-27T19:45:27.150131Z","shell.execute_reply":"2022-06-27T19:45:33.834489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### *Adding more EDA and viz* ... ","metadata":{}},{"cell_type":"markdown","source":"## DO UPVOTE PLEASE!","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:50:00.245191Z","iopub.execute_input":"2022-06-27T19:50:00.245603Z","iopub.status.idle":"2022-06-27T19:50:00.250501Z","shell.execute_reply.started":"2022-06-27T19:50:00.245573Z","shell.execute_reply":"2022-06-27T19:50:00.249409Z"}}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}