{"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":"# Exploratory Analysis","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn\nfrom matplotlib import style\nimport random","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-23T05:16:29.360899Z","iopub.execute_input":"2022-09-23T05:16:29.361334Z","iopub.status.idle":"2022-09-23T05:16:30.001557Z","shell.execute_reply.started":"2022-09-23T05:16:29.361294Z","shell.execute_reply":"2022-09-23T05:16:30.000497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/sartorius-cell-instance-segmentation/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-09-23T05:16:30.003540Z","iopub.execute_input":"2022-09-23T05:16:30.003980Z","iopub.status.idle":"2022-09-23T05:16:30.789296Z","shell.execute_reply.started":"2022-09-23T05:16:30.003948Z","shell.execute_reply":"2022-09-23T05:16:30.788211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-23T05:16:30.790829Z","iopub.execute_input":"2022-09-23T05:16:30.791824Z","iopub.status.idle":"2022-09-23T05:16:30.815471Z","shell.execute_reply.started":"2022-09-23T05:16:30.791785Z","shell.execute_reply":"2022-09-23T05:16:30.814324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in train.columns:\n    print(f\"Unique values in {col}: {len(train[col].unique())}\")","metadata":{"execution":{"iopub.status.busy":"2022-09-23T05:16:30.817284Z","iopub.execute_input":"2022-09-23T05:16:30.817619Z","iopub.status.idle":"2022-09-23T05:16:30.926860Z","shell.execute_reply.started":"2022-09-23T05:16:30.817589Z","shell.execute_reply":"2022-09-23T05:16:30.925697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### There are 606 labeled images, all having the same shape, pertaining to 3 types.  \n#### There are several annotations for each image (number of annotations > number of images).\n### Note:  \n* #### Cell types aren't classes, as this is not a classification problem but rather an instance segmentation problem.","metadata":{}},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-09-23T05:17:43.854096Z","iopub.execute_input":"2022-09-23T05:17:43.854557Z","iopub.status.idle":"2022-09-23T05:17:43.903702Z","shell.execute_reply.started":"2022-09-23T05:17:43.854512Z","shell.execute_reply":"2022-09-23T05:17:43.902440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### There are no missing values.","metadata":{}},{"cell_type":"code","source":"semi_supervised_folder = \"../input/sartorius-cell-instance-segmentation/train_semi_supervised\"\nss_length = len(os.listdir(semi_supervised_folder))\nprint(f\"There are {ss_length} unlabeled images, useful for semi-supervised learning.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-23T05:17:15.558056Z","iopub.execute_input":"2022-09-23T05:17:15.558442Z","iopub.status.idle":"2022-09-23T05:17:15.566307Z","shell.execute_reply.started":"2022-09-23T05:17:15.558412Z","shell.execute_reply":"2022-09-23T05:17:15.565083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Run-length Encoding of Masks:","metadata":{}},{"cell_type":"code","source":"def id_to_img(img_id):\n    path = \"../input/sartorius-cell-instance-segmentation/train\" + f\"/{img_id}\" + \".png\"\n    return plt.imread(path)","metadata":{"execution":{"iopub.status.busy":"2022-09-23T05:50:13.812735Z","iopub.execute_input":"2022-09-23T05:50:13.813145Z","iopub.status.idle":"2022-09-23T05:50:13.818960Z","shell.execute_reply.started":"2022-09-23T05:50:13.813112Z","shell.execute_reply":"2022-09-23T05:50:13.817585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_img_mask_couple(img_id, width=704, height=520):\n    annots = list(train[train[\"id\"] == img_id][\"annotation\"])\n    mask = np.zeros(shape=(height, width), dtype=float)\n    for ann in annots:\n        a = ann.split()\n        submask = np.zeros(shape=(height * width), dtype=float)\n        start = list(map(int, a[0::2]))\n        n = list(map(int, a[1::2]))\n        end = []\n        for (s, n) in zip(start, n):\n            end.append(s+n-1)\n        for s, e in zip(start, end):\n            submask[s:e] = 1\n        submask = submask.reshape((height, width))\n        mask+=submask\n    img = id_to_img(img_id)\n    return img, mask","metadata":{"execution":{"iopub.status.busy":"2022-09-23T05:53:24.573595Z","iopub.execute_input":"2022-09-23T05:53:24.574059Z","iopub.status.idle":"2022-09-23T05:53:24.585250Z","shell.execute_reply.started":"2022-09-23T05:53:24.574006Z","shell.execute_reply":"2022-09-23T05:53:24.583905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_img_and_mask(img_id):\n    img, mask = get_img_mask_couple(img_id)\n    fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(20,13))\n    axes[0].imshow(img, cmap=\"gray\")\n    axes[1].imshow(mask, cmap=\"binary\")\n    axes[2].imshow(img, cmap=\"cividis\")\n    axes[2].imshow(mask, cmap=\"mako_r\", alpha=0.1)\n    axes[0].set_title(\"image\")\n    axes[1].set_title(\"mask\")\n    axes[2].set_title(\"image & mask\")","metadata":{"execution":{"iopub.status.busy":"2022-09-23T06:09:26.060408Z","iopub.execute_input":"2022-09-23T06:09:26.060829Z","iopub.status.idle":"2022-09-23T06:09:26.068194Z","shell.execute_reply.started":"2022-09-23T06:09:26.060793Z","shell.execute_reply":"2022-09-23T06:09:26.067248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_ids = train[\"id\"].unique()\nchoices = random.choices(all_ids, k=10)\nfor img_id in choices:\n    show_img_and_mask(img_id)","metadata":{"execution":{"iopub.status.busy":"2022-09-23T06:09:27.099174Z","iopub.execute_input":"2022-09-23T06:09:27.100028Z","iopub.status.idle":"2022-09-23T06:09:35.699481Z","shell.execute_reply.started":"2022-09-23T06:09:27.099976Z","shell.execute_reply":"2022-09-23T06:09:35.698139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def img_mask_histograms(img_id):\n    img, mask = get_img_mask_couple(img_id)\n    img_hist, img_bins = np.histogram(img, bins=256, range=(0,1))\n    mask_hist, mask_bins = np.histogram(mask, bins=256)\n    fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(20,13))\n    axes[0,0].imshow(img, cmap=\"gray\")\n    axes[0,1].imshow(mask, cmap=\"binary\")\n    axes[1,0].plot(img_bins[0:-1], img_hist)\n    axes[1,1].plot(mask_bins[0:-1], mask_hist)\n    axes[0,0].set_title(\"image\")\n    axes[0,1].set_title(\"mask\")\n    axes[1,0].set_title(\"image histogram\")\n    axes[1,1].set_title(\"mask_histogram\")","metadata":{"execution":{"iopub.status.busy":"2022-09-23T06:18:26.231776Z","iopub.execute_input":"2022-09-23T06:18:26.232196Z","iopub.status.idle":"2022-09-23T06:18:26.241945Z","shell.execute_reply.started":"2022-09-23T06:18:26.232163Z","shell.execute_reply":"2022-09-23T06:18:26.240909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_ids = train[\"id\"].unique()\nchoices = random.choices(all_ids, k=1)\nfor img_id in choices:\n    img_mask_histograms(img_id)","metadata":{"execution":{"iopub.status.busy":"2022-09-23T06:18:26.548798Z","iopub.execute_input":"2022-09-23T06:18:26.550048Z","iopub.status.idle":"2022-09-23T06:18:27.517341Z","shell.execute_reply.started":"2022-09-23T06:18:26.550000Z","shell.execute_reply":"2022-09-23T06:18:27.516074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}