{"cells": [{"metadata": {"_cell_guid": "e47d04c9-d976-49e2-83f2-6fa6854cce53", "collapsed": true, "_uuid": "7c8fbce05cb31280b61e3915edb0e6d5aced80ad"}, "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 numpy as np # linear algebra\n", "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n", "import random\n", "import skimage.io\n", "import matplotlib.pyplot as plt\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."], "cell_type": "code", "execution_count": null, "outputs": []}, {"metadata": {"_cell_guid": "3953def3-5939-4084-8e06-0f7c5b028542", "_uuid": "f3e0b0d7a7af7c0e22f7c46f0b4c54189fcf3e11"}, "source": ["**Calculate mean nuclei size for each image**"], "cell_type": "markdown"}, {"metadata": {"_cell_guid": "58d4a240-48e6-4996-a0d8-8a2c38c7dd61", "collapsed": true, "_uuid": "c02d0a22cd8bf8c63f83c03fbd41212db536b141"}, "source": ["def getNucleiSize(image_id):\n", "    mask_file = \"../input/stage1_train/{}/masks/*.png\".format(image_id)\n", "    masks = skimage.io.imread_collection(mask_file).concatenate()\n", "    num_masks = masks.shape[0]\n", "    nucleiSize = []\n", "    for index in range(0, num_masks):\n", "        nucleiSize.append(np.sum(masks[index] > 128))\n", "    return np.mean(nucleiSize)\n", "\n", "image_ids = check_output([\"ls\", \"../input/stage1_train/\"]).decode(\"utf8\").split()\n", "meanNucleiSizes = []\n", "for image_id in image_ids:\n", "    meanNucleiSizes.append(getNucleiSize(image_id))"], "cell_type": "code", "execution_count": null, "outputs": []}, {"metadata": {"_cell_guid": "ccb1f1fe-74fa-4f86-96a7-44d8f328a232", "_uuid": "273484de7fb5f4f658b44c23abd47207a50493a4"}, "source": ["**Plot hist of nuclei sizes**"], "cell_type": "markdown"}, {"metadata": {"_cell_guid": "67b5351b-3b98-475f-b3da-d683d4d746ac", "collapsed": true, "_uuid": "466a78fb5a7d7d573e47e1d0a13a00721557176a"}, "source": ["print(np.min(meanNucleiSizes), np.median(meanNucleiSizes), np.max(meanNucleiSizes))\n", "plt.hist(meanNucleiSizes, bins=20)"], "cell_type": "code", "execution_count": null, "outputs": []}, {"metadata": {"_cell_guid": "10129ea6-9b29-4229-bde8-0fe226c772dc", "_uuid": "5d5349b03506bc38b0788eaf9609d1f9813f3f6d"}, "source": ["**Show images with min/mean/median/max size**"], "cell_type": "markdown"}, {"metadata": {"_cell_guid": "9b32c7ba-9c7c-458a-bf04-0b6259f37cde", "collapsed": true, "_uuid": "3071e7e780f9a9a17ce0b49b4a662557d99a5049"}, "source": ["def find_nearest(array,value):\n", "    idx = (np.abs(array-value)).argmin()\n", "    return idx\n", "\n", "def read_image_labels(image_id):\n", "    from skimage import morphology\n", "    image_file = \"../input/stage1_train/{}/images/{}.png\".format(image_id,image_id)\n", "    mask_file = \"../input/stage1_train/{}/masks/*.png\".format(image_id)\n", "    image = skimage.io.imread(image_file)\n", "    masks = skimage.io.imread_collection(mask_file).concatenate()    \n", "    height, width, _ = image.shape\n", "    num_masks = masks.shape[0]\n", "    maxValue = np.max(image)\n", "    kernel = np.matrix([[0,0,1,0,0],\n", "                        [0,1,1,1,0],\n", "                        [1,1,1,1,1],\n", "                        [0,1,1,1,0],\n", "                        [0,0,1,0,0] ], dtype=np.bool)\n", "    for index in range(0, num_masks):\n", "        contour = np.logical_xor(masks[index], morphology.binary_erosion(masks[index], kernel) )\n", "#         image[:,:,0]=0\n", "#         image[:,:,2]=0\n", "        image[:,:,1] |= contour.astype(np.uint8)*255\n", "        #image[contour > 0] = maxValue\n", "    return image\n", "\n", "\n", "def showImageWithNucleiSize(nucleiSize):\n", "    print('nuclei size:', nucleiSize)\n", "    i = find_nearest(np.asarray(meanNucleiSizes), nucleiSize)\n", "    image_id = image_ids[i]\n", "    plt.imshow(read_image_labels(image_id) )\n", "    plt.show()\n", "    \n", "print('min size')\n", "showImageWithNucleiSize(np.min(meanNucleiSizes))\n", "print('mean size')\n", "showImageWithNucleiSize(np.mean(meanNucleiSizes))\n", "print('median size')\n", "showImageWithNucleiSize(np.median(meanNucleiSizes))\n", "print('max size')\n", "showImageWithNucleiSize(np.max(meanNucleiSizes))\n", "print('second peak on hist')\n", "showImageWithNucleiSize(1500)"], "cell_type": "code", "execution_count": null, "outputs": []}, {"metadata": {"_cell_guid": "f709ef09-1ccd-48ab-b43a-24a4ed2941b7", "_uuid": "e62452f3e6247e8ed324f81ad7ce4538556fe3c5"}, "source": ["**Show first 10 images sorted by nuclei size**"], "cell_type": "markdown"}, {"metadata": {"_cell_guid": "709f2b30-e31b-45ee-bb1e-20349c64b816", "collapsed": true, "_uuid": "4e8694c204557d062416d9c8a7392bf25e1ef60d"}, "source": ["ids = np.argsort(meanNucleiSizes)[0:10]\n", "for i in ids:\n", "    image_id = image_ids[i]\n", "    print(image_id, ' ', meanNucleiSizes[i])\n", "    plt.imshow(read_image_labels(image_id))\n", "    plt.show()\n", "    "], "cell_type": "code", "execution_count": null, "outputs": []}, {"metadata": {"_cell_guid": "1bd3795d-d885-42a6-8d72-5f7064a3ecd2", "_uuid": "84d79fadd425e3f5479f854af0facec6c54afc21"}, "source": [], "cell_type": "markdown"}], "metadata": {"kernelspec": {"name": "python3", "language": "python", "display_name": "Python 3"}, "language_info": {"name": "python", "nbconvert_exporter": "python", "version": "3.6.4", "pygments_lexer": "ipython3", "mimetype": "text/x-python", "codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py"}}, "nbformat_minor": 1, "nbformat": 4}