{"cells": [{"outputs": [], "metadata": {"_uuid": "0e1ae50376ebf43af9c1e561baf08f149a823693", "_cell_guid": "b609641b-9bb7-42f6-9add-aeb93a070a6a"}, "cell_type": "code", "source": ["# This Python 3 environment comes with many helpful analytics libraries installed\n", "# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n", "# For example, here's several helpful packages to load in \n", "\n", "import os\n", "import numpy as np # linear algebra\n", "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n", "import matplotlib.pyplot as plt\n", "import skimage.io\n", "import seaborn as sns\n", "\n", "# Input data files are available in the \"../input/\" directory.\n", "# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n", "\n", "from subprocess import check_output\n", "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n", "\n", "# Any results you write to the current directory are saved as output."], "execution_count": 1}, {"outputs": [], "metadata": {"_uuid": "2de1f288bb778274c175c6ffdf7a1341acef558f", "collapsed": true, "_cell_guid": "63eab86d-5dae-454b-8cd2-334411ddd7f4"}, "cell_type": "code", "source": ["input_dir = '../input'\n", "train_dir = '../input/stage1_train'"], "execution_count": 2}, {"outputs": [], "metadata": {"_uuid": "0c485cc0777f4d06fb44a2a26a6909670c3a531e", "collapsed": true, "_cell_guid": "8d71941e-1289-4020-a664-1e78d29a5eb3"}, "cell_type": "code", "source": ["df_labels = pd.read_csv(os.path.join(input_dir, 'stage1_train_labels.csv'))"], "execution_count": 3}, {"metadata": {"_uuid": "233fff71f8a4c0ce4f2a16ad8fec427e00b92a43", "_cell_guid": "ab41b361-7d3a-4015-865e-e15f0eae7814"}, "cell_type": "markdown", "source": ["# visualize random image"]}, {"outputs": [], "metadata": {"_uuid": "33c924a69e08ecc94493068676daa385073aea60", "collapsed": true, "_cell_guid": "59fc58a6-019c-4175-9639-4580a1076857"}, "cell_type": "code", "source": ["def show_images(image_ids):\n", "  plt.close('all')\n", "  fig, ax = plt.subplots(nrows=len(image_ids),ncols=3, figsize=(50,50))\n", "\n", "  for image_idx, image_id in enumerate(image_ids):\n", "    image_path = os.path.join(train_dir, image_id, 'images', '{}.png'.format(image_id))\n", "    mask_paths = os.path.join(train_dir, image_id, 'masks', '*.png')\n", "  \n", "    image = skimage.io.imread(image_path)\n", "    masks = skimage.io.imread_collection(mask_paths).concatenate()\n", "    mask = np.zeros(image.shape[:2], np.uint16)\n", "    for mask_idx in range(masks.shape[0]):\n", "      mask[masks[mask_idx] > 0] = mask_idx + 1\n", "    other = mask == 0\n", "    \n", "    if len(image_ids) > 1:\n", "      ax[image_idx, 0].imshow(image)\n", "      ax[image_idx, 1].imshow(mask)\n", "      ax[image_idx, 2].imshow(np.expand_dims(other, axis=2) * image)\n", "    else:\n", "      ax[0].imshow(image)\n", "      ax[1].imshow(mask)\n", "      ax[2].imshow(np.expand_dims(other, axis=2) * image)\n"], "execution_count": 4}, {"outputs": [], "metadata": {"_uuid": "1359cc7b9f85f6ed2f169e9e2e1b3c2b7b7e45d5", "_cell_guid": "454fbfc6-8e44-45e8-a7ec-9c99e062a121"}, "cell_type": "code", "source": ["sample_image = df_labels.sample(n=1).iloc[0]\n", "show_images([sample_image['ImageId']])"], "execution_count": 5}, {"metadata": {"_uuid": "64bfa526112772272489add66d6dbe2428910565", "_cell_guid": "35d08899-df30-418f-b392-ba3e9fdd3a38"}, "cell_type": "markdown", "source": ["# Statistics"]}, {"outputs": [], "metadata": {"_uuid": "422aec74655fedd46f0947d18f166e3be335746b", "collapsed": true, "_cell_guid": "f1a3a2c4-edb9-438e-8f14-ef0e14fbaa56"}, "cell_type": "code", "source": ["def get_nuclei_sizes():\n", "  image_ids = list(df_labels.drop_duplicates(subset='ImageId')['ImageId'])\n", "  def nuclei_size_stats(image_id):\n", "    mask_paths = os.path.join(train_dir, image_id, 'masks', '*.png')\n", "    masks = skimage.io.imread_collection(mask_paths).concatenate()\n", "    masks = (masks > 0).astype(np.uint16)\n", "    nuclei_sizes = np.sum(masks, axis=(1,2))\n", "    return {'nuclei_size_min': np.min(nuclei_sizes),\n", "            'nuclei_size_max': np.max(nuclei_sizes),\n", "            'nuclei_size_mean': np.mean(nuclei_sizes),\n", "            'nuclei_size_std': np.std(nuclei_sizes)}\n", "  return pd.DataFrame.from_dict({image_id: nuclei_size_stats(image_id) for image_id in image_ids}, orient='index')"], "execution_count": 6}, {"outputs": [], "metadata": {"_uuid": "e1b444b1f6c6ee8b0750084ec94460826b284cc3", "collapsed": true, "_cell_guid": "1af77a55-6eb9-4676-a0f3-19ade5015346"}, "cell_type": "code", "source": ["df_nuclei_sizes = get_nuclei_sizes()"], "execution_count": 7}, {"outputs": [], "metadata": {"_uuid": "e83f3b87c314febff60c8da5f1d3157a82f3fdcb", "collapsed": true, "_cell_guid": "d557194d-df90-4039-90e7-60f51af7bd69"}, "cell_type": "code", "source": ["def plot_stats(df):\n", "  fig, axs = plt.subplots(nrows=3, ncols=2, figsize=(64,64))\n", "  def plot_with_set_font_size(key, ax):\n", "    p = sns.distplot(df_stats[key], kde=False, rug=False, ax=ax)\n", "    p.tick_params(labelsize=50)\n", "    p.set_xlabel(key, fontsize=50)\n", "  \n", "  plot_with_set_font_size('mask_counts', axs[0,0])\n", "  plot_with_set_font_size('nuclei_size_min', axs[1,0])\n", "  plot_with_set_font_size('nuclei_size_max', axs[1,1])\n", "  plot_with_set_font_size('nuclei_size_mean', axs[2,0])\n", "  plot_with_set_font_size('nuclei_size_std', axs[2,1])\n"], "execution_count": 8}, {"outputs": [], "metadata": {"_uuid": "d9648b2ed5f8e17c1695e97c7e365325a8548b50", "scrolled": false, "_cell_guid": "bba26ca7-1804-4a51-9688-8cc955f226a1"}, "cell_type": "code", "source": ["df_mask_counts = df_labels.groupby(['ImageId']).count()\n", "df_mask_counts.columns = ['mask_counts']\n", "df_stats = df_mask_counts.join(df_nuclei_sizes)\n", "\n", "display(df_stats.describe())\n", "plot_stats(df_stats)"], "execution_count": 9}, {"metadata": {"_uuid": "ad7b93e7e1642a0e425f7caccb8293a50f2a965f", "_cell_guid": "3bfdc852-ad92-4e50-827d-b957df24b0b4"}, "cell_type": "markdown", "source": ["## lower percentile of mask counts"]}, {"outputs": [], "metadata": {"_uuid": "c93f1aef8a712d4b191f78b74f98686bc0a2f0f8", "_cell_guid": "9e282937-4741-4246-84fc-fb0486855b76"}, "cell_type": "code", "source": ["lower_percentile_stats = df_stats.query('mask_counts < 15.25')\n", "display(lower_percentile_stats.describe())\n", "plot_stats(lower_percentile_stats)\n"], "execution_count": 10}, {"outputs": [], "metadata": {}, "cell_type": "code", "source": ["samples = lower_percentile_stats.sample(n=5)\n", "display(samples)\n", "show_images(list(samples.index))"], "execution_count": 11}, {"metadata": {"_uuid": "4257da28879e4845a2670b087b7ff480f522aa57", "_cell_guid": "d3a640f4-255a-4c92-aac6-e76d3378b56b"}, "cell_type": "markdown", "source": ["## middle percentile of mask counts"]}, {"outputs": [], "metadata": {"_uuid": "a3b324e4588b3952018baa0ad2e1b365f8e8caf8", "_cell_guid": "e29a024f-0365-4fe2-8d9b-0b4e6e02c693"}, "cell_type": "code", "source": ["middle_percentile_stats = df_stats.query('mask_counts > 15.25 and mask_counts < 54.0')\n", "display(middle_percentile_stats.describe())\n", "plot_stats(middle_percentile_stats)"], "execution_count": 12}, {"outputs": [], "metadata": {"_uuid": "a863565bdd674ca9f8f9cedb895111ce97bb1274", "scrolled": false, "_cell_guid": "4f4e84da-ebb5-4c63-abed-5b0d854f4e5f"}, "cell_type": "code", "source": ["samples = middle_percentile_stats.sample(n=5)\n", "display(samples)\n", "show_images(list(samples.index))"], "execution_count": 13}, {"metadata": {"_uuid": "68c8365f0080aa33b141be87d47da1a50e974447", "_cell_guid": "d3203d32-73f8-4ec5-908b-aa133e7dca2f"}, "cell_type": "markdown", "source": ["## upper percentile of mask counts"]}, {"outputs": [], "metadata": {"_uuid": "20bb7e3871133dde9e214a087c8783164866eec7", "_cell_guid": "a08b7755-0607-49cc-826a-a213bfdcc104"}, "cell_type": "code", "source": ["upper_percentile_stats = df_stats.query('mask_counts > 54.0')\n", "display(upper_percentile_stats.describe())\n", "plot_stats(upper_percentile_stats)"], "execution_count": 14}, {"outputs": [], "metadata": {"_uuid": "a8a0a72113311f9ed72fceae5ae652324a90aa03", "_cell_guid": "639dc3e0-7b19-44ce-8ca6-0a94d230ccf3"}, "cell_type": "code", "source": ["samples = upper_percentile_stats.sample(n=5)\n", "display(samples)\n", "show_images(list(samples.index))"], "execution_count": 15}], "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"nbconvert_exporter": "python", "version": "3.6.4", "codemirror_mode": {"name": "ipython", "version": 3}, "pygments_lexer": "ipython3", "name": "python", "file_extension": ".py", "mimetype": "text/x-python"}}, "nbformat": 4, "nbformat_minor": 1}