{"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":"### Data Overview :\n\n#### train.csv - IDs and masks for all training objects. None of this metadata is provided for the test set.\n\n* id - unique identifier for object\n\n* annotation - run length encoded pixels for the identified neuronal cell\n\n* width - source image width\n\n* height - source image height\n\n* cell_type - the cell line\n\n* plate_time - time plate was created\n\n* sample_date - date sample was created\n\n* sample_id - sample identifier\n\n* elapsed_timedelta - time since first image taken of sample\n\n* train - train images in PNG format\n\n* test - test images in PNG format. Only a few test set images are available for download; the remainder can only be accessed by your notebooks when you submit.\n\n\n","metadata":{"id":"Nl4uyiJLU8ZV"}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport cv2\nimport imageio","metadata":{"id":"I4CfUa4Bhu7-","execution":{"iopub.status.busy":"2021-10-23T18:16:41.081728Z","iopub.execute_input":"2021-10-23T18:16:41.082134Z","iopub.status.idle":"2021-10-23T18:16:42.299381Z","shell.execute_reply.started":"2021-10-23T18:16:41.082023Z","shell.execute_reply":"2021-10-23T18:16:42.298218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/sartorius-cell-instance-segmentation/train.csv\")","metadata":{"id":"dgnsQTa1jfvN","execution":{"iopub.status.busy":"2021-10-23T18:16:42.301351Z","iopub.execute_input":"2021-10-23T18:16:42.301677Z","iopub.status.idle":"2021-10-23T18:16:42.946376Z","shell.execute_reply.started":"2021-10-23T18:16:42.301628Z","shell.execute_reply":"2021-10-23T18:16:42.945069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"id":"rSJiXbPLjplk","outputId":"fbeb1aa6-e5c3-4fe2-be87-3efc2528019b","execution":{"iopub.status.busy":"2021-10-23T18:16:42.94804Z","iopub.execute_input":"2021-10-23T18:16:42.948399Z","iopub.status.idle":"2021-10-23T18:16:42.990508Z","shell.execute_reply.started":"2021-10-23T18:16:42.948351Z","shell.execute_reply":"2021-10-23T18:16:42.989088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"id":"HvjhzdmnkL21","outputId":"1a24233f-7bc1-4d05-9178-fe766d83808b","execution":{"iopub.status.busy":"2021-10-23T18:16:42.99423Z","iopub.execute_input":"2021-10-23T18:16:42.994736Z","iopub.status.idle":"2021-10-23T18:16:43.194203Z","shell.execute_reply.started":"2021-10-23T18:16:42.994683Z","shell.execute_reply":"2021-10-23T18:16:43.193212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Unique values in DataFrame : \")\nfor col in df.columns:\n    print(col + \" : \" + str(len(df[col].unique())))","metadata":{"id":"plzzKyIwqwe9","outputId":"d8208280-9105-4ce8-8aa5-f2e582fa37cb","execution":{"iopub.status.busy":"2021-10-23T18:16:43.195605Z","iopub.execute_input":"2021-10-23T18:16:43.196043Z","iopub.status.idle":"2021-10-23T18:16:43.301114Z","shell.execute_reply.started":"2021-10-23T18:16:43.195964Z","shell.execute_reply":"2021-10-23T18:16:43.300064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Null value in dataframe :- \",df.isnull().any().sum())","metadata":{"id":"LXyKBuwCdUT2","outputId":"486f7e71-1653-473b-fee6-21cc2933b320","execution":{"iopub.status.busy":"2021-10-23T18:16:43.302849Z","iopub.execute_input":"2021-10-23T18:16:43.303387Z","iopub.status.idle":"2021-10-23T18:16:43.360476Z","shell.execute_reply.started":"2021-10-23T18:16:43.303339Z","shell.execute_reply":"2021-10-23T18:16:43.359722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x= \"cell_type\", data= df)\nplt.title(\"Cell type counting\")\nplt.show()","metadata":{"id":"3HFUkHzN6-qG","outputId":"d14bec69-5c34-4001-e1da-7191a36a1eeb","execution":{"iopub.status.busy":"2021-10-23T18:16:43.361848Z","iopub.execute_input":"2021-10-23T18:16:43.362324Z","iopub.status.idle":"2021-10-23T18:16:43.670056Z","shell.execute_reply.started":"2021-10-23T18:16:43.362284Z","shell.execute_reply":"2021-10-23T18:16:43.668868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ref: https://www.kaggle.com/inversion/run-length-decoding-quick-start\n\ndef rle_decode(mask_rle, shape, color=1):\n\n    s = mask_rle.split()\n    \n    starts = list(map(lambda x: int(x) - 1, s[0::2]))\n    \n    lengths = list(map(int, s[1::2]))\n    ends = [x + y for x, y in zip(starts, lengths)]\n    \n    img = np.zeros((shape[0] * shape[1], shape[2]), dtype=np.float32)\n            \n    for start, end in zip(starts, ends):\n        img[start : end] = color\n    \n    return img.reshape(shape)","metadata":{"id":"2ge8DweC8cXE","execution":{"iopub.status.busy":"2021-10-23T18:16:43.671624Z","iopub.execute_input":"2021-10-23T18:16:43.671912Z","iopub.status.idle":"2021-10-23T18:16:43.681032Z","shell.execute_reply.started":"2021-10-23T18:16:43.671876Z","shell.execute_reply":"2021-10-23T18:16:43.679672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_masks(image_id, colors=True):\n    \n    labels = df[df[\"id\"] == image_id][\"annotation\"].tolist()\n\n    if colors:\n        mask = np.zeros((520, 704, 3))\n        for label in labels:\n            mask += rle_decode(label, shape=(520, 704, 3), color=np.random.rand(3))\n    else:\n        mask = np.zeros((520, 704, 1))\n        for label in labels:\n            mask += rle_decode(label, shape=(520, 704, 1))\n    mask = mask.clip(0, 1)\n\n    image = cv2.imread(f\"../input/sartorius-cell-instance-segmentation/train/{image_id}.png\")\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n    plt.figure(figsize=(15, 31))\n    plt.subplot(3, 1, 1)\n    plt.imshow(image)\n    plt.title('Input image')\n    plt.axis(\"off\")\n    plt.subplot(3, 1, 2)\n    plt.imshow(image)\n    plt.imshow(mask, alpha=0.5)\n    plt.title('Input image with mask')\n    plt.axis(\"off\")\n    plt.subplot(3, 1, 3)\n    plt.imshow(mask)\n    plt.title('Only mask')\n    plt.axis(\"off\")\n    \n    plt.show();","metadata":{"id":"jMP-tBWiIqyk","execution":{"iopub.status.busy":"2021-10-23T18:16:48.671591Z","iopub.execute_input":"2021-10-23T18:16:48.671903Z","iopub.status.idle":"2021-10-23T18:16:48.68423Z","shell.execute_reply.started":"2021-10-23T18:16:48.671871Z","shell.execute_reply":"2021-10-23T18:16:48.683193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# shsy5y cell type\nplot_masks(\"0030fd0e6378\")","metadata":{"id":"g8gGwIFXIrXg","outputId":"5e6788f6-1f8a-4529-c74b-0a264abf01ee","execution":{"iopub.status.busy":"2021-10-23T18:16:49.861894Z","iopub.execute_input":"2021-10-23T18:16:49.862209Z","iopub.status.idle":"2021-10-23T18:16:51.563914Z","shell.execute_reply.started":"2021-10-23T18:16:49.862175Z","shell.execute_reply":"2021-10-23T18:16:51.562878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cort cell type\nplot_masks(\"ffdb3cc02eef\")","metadata":{"id":"-_5L7zZpI4dZ","outputId":"928dd12d-3eee-4743-9706-55001ee7bfcc","execution":{"iopub.status.busy":"2021-10-23T18:16:51.566015Z","iopub.execute_input":"2021-10-23T18:16:51.566487Z","iopub.status.idle":"2021-10-23T18:16:52.760987Z","shell.execute_reply.started":"2021-10-23T18:16:51.566444Z","shell.execute_reply":"2021-10-23T18:16:52.759855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# astro cell type\nplot_masks(\"0140b3c8f445\")","metadata":{"id":"BmqXIKZlPf4G","outputId":"5904c80f-645d-40f2-a30e-ab278bcade96","execution":{"iopub.status.busy":"2021-10-23T18:16:52.762516Z","iopub.execute_input":"2021-10-23T18:16:52.762859Z","iopub.status.idle":"2021-10-23T18:16:53.938088Z","shell.execute_reply.started":"2021-10-23T18:16:52.762815Z","shell.execute_reply":"2021-10-23T18:16:53.937325Z"},"trusted":true},"execution_count":null,"outputs":[]}]}