{"cells":[{"metadata":{},"cell_type":"markdown","source":"<img src=\"https://hubmapconsortium.org/wp-content/uploads/2019/01/HuBMAP-Retina-Logo-Color.png\" alt=\"Human Biomolecular Atlas Program\" />"},{"metadata":{},"cell_type":"markdown","source":"<div align=\"center\"><h1 style=\"font-size: 4em\"> <b>Hacking the kidney</b></h1></div>\n<p style=\"font-size:1.2em\"> If you feel this is something new and fresh, and it added some value to you, please consider <font color='orange'>upvoting</font> 😄</p>\n<p style=\"font-size:1.2em\"><font color=\"red\">NOTE</font> due to the size of the images, trying to read and show different images may consume all the memory and the session will automatically restart</p>"},{"metadata":{},"cell_type":"markdown","source":"## <font size='8' color='blue'>Contents</font> \n\n\n<p>\n    <ul style=\"font-size:1.2em\">\n        <li><font color='blue'>Introduction</font></li>\n        <li><font color='blue'>Importing the necessary libraries</font></li>\n        <li><font color='blue'>The Data</font></li>\n        <ul>\n            <li><font color='blue'>Analyzing images and contours</font></li>\n            <li><font color='blue'>Analyzing patient data</font></li>\n        </ul>\n        <li>Work in progress...</li>\n    </ul>  \n</p>"},{"metadata":{},"cell_type":"markdown","source":"<h2 style=\"font-size: 2em\"> 1. <a id=\"Introduction\">Introduction </a> 🏕️</h2>"},{"metadata":{},"cell_type":"markdown","source":"<div>\n    <p style=\"font-size:1.2em\"> Our kidneys remove wastes and extra fluid from your body, they also remove the waste products generated by the chemical reactions carried out in our cells, and maintain a healthy balance of water, salts and minerals in our blood. This balance is essential for the other tissues to be in a good state and work normally.\n        At a gross level, the kidney can be divide in the following parts:</p>\n        \n   <img src=\"https://i.pinimg.com/originals/d7/7e/f1/d77ef1baf5ee73d0153cf60002e55b46.jpg\" alt=\"Parts of the Kidney\" width=\"500\" height=\"600\"/>\n    \n   <p> <ul style=\"font-size:1.2em\">\n        <li><font color='black'>Renal Artery</font>: branches off of the aorta bringing waste-filled blood into the kidney </li>\n        <li><font color='black'>Renal Vein</font>: removes the filtered blood from the kidneys to the inferior vena cava</li> \n        <li><font color='black'>Ureter</font>: collects filtrate and urine from the renal pelvis and takes it to the bladder</li>\n        <li><font color='black'>Renal Pelvis</font>: that functions as a funnel for urine flowing to the ureter.</li>\n        <li><font color='black'>nephron</font>: the functional unit of the kidney.\n            <ul>\n                <li><font color='black'>renal corpuscle</font>: <font style=\"text-decoration: underline;\">Located whithin the <a href=\"https://en.wikipedia.org/wiki/Renal_cortex\">cortex</a></font>, consists of a tuft of capillaries called <a href=\"https://en.wikipedia.org/wiki/Glomerulus_(kidney)\">glomerulus</a> and an encompassing <a href=\"https://en.wikipedia.org/wiki/Bowman%27s_capsule\">Bowman's capsule</a>\n                <li><font color='black'>renal tubule</font>: The renal tubule extends from the corpuscle, in the <a href=\"https://en.wikipedia.org/wiki/Renal_cortex\">cortex</a>, and descends into to the <a href=\"https://en.wikipedia.org/wiki/Renal_medulla\">Medulla</a>\n            </ul>\n        <li><font color='black'>Parenchyma</font>: The parenchyma is the functional parts of an organ </li>\n        <ul >\n            <li><a href=\"https://en.wikipedia.org/wiki/Renal_medulla\">Medulla</a>: It contains the renal corpuscles and part of the renal tubules</li>\n            <li><a href=\"https://en.wikipedia.org/wiki/Renal_medulla\">Cortex</a>: It contains some structures of teh nephrone, such as the vasa rectae, the venulae rectae, the medullary capillary plexus, the loop of Henle, and the collecting tubule.</li>\n        </ul>\n    </ul>\n    </p>\n</div>"},{"metadata":{},"cell_type":"markdown","source":"<div>\n    <p style=\"font-size:1.2em\">\n        In the following images we can see the anatomicla part that constitutes the glomerulus, a cartoon representation (Left), and an image of how it would actually look on a <a src=\"https://en.wikipedia.org/wiki/Periodic_acid%E2%80%93Schiff_stain\">Periodic acid–Schiff</a> (PAS) image (Right), where we can apreciate the <a href=\"https://en.wikipedia.org/wiki/Glomerulus_(kidney)\">glomerulus</a> envolved by the <a href=\"https://en.wikipedia.org/wiki/Bowman%27s_capsule\">Bowman's capsule</a>\n    </p>\n  <div>\n      <div>\n        <img src=\"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTTVybo7lfOBNkxidBcNej1tfQXojCNiB9X6Q&usqp=CAU\" alt=\"Snow\" style=\"float: right; width: 50%; padding: 5px;\">\n      </div>\n      <div>\n        <img src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/Bowman%27s_capsule_and_glomerulus.svg/250px-Bowman%27s_capsule_and_glomerulus.svg.png\" alt=\"Forest\" style=\"float: left; width: 50%; padding: 10px 100px; transform: rotate(-90deg);\">\n      </div>\n  </div>\n  \n</div>"},{"metadata":{},"cell_type":"markdown","source":"<div>\n    <p style=\"font-size:1.2em\"> \n        At a more granular level, we can notice that the <a href=\"https://en.wikipedia.org/wiki/Glomerulus_(kidney)\">glomerulus</a> which is the structure of interest for <a href=\"https://www.kaggle.com/c/hubmap-kidney-segmentation/overview\">this challenge</a>, is a component of the renal corpuscle, which itself is a component of a nephron, that is found within the <a href=\"https://en.wikipedia.org/wiki/Renal_medulla\">Cortex</a>. The <a href=\"https://en.wikipedia.org/wiki/Glomerulus_(kidney)\">glomerulus</a> is important because it represents the  component where the filtrartion of the blood begins, in it, the hydrostatic pressure provides the force for ultrafiltration.\n    </p>\n    <img src=\"https://blog.cambridgecoaching.com/hs-fs/hubfs/eden.png?width=665&height=540&name=eden.png\" alt=\"Detailed parts of the Kidney\" width=\"500\" height=\"600\"/>\n    <p></p>\n    <p style=\"font-size:1.2em\">Here is a good video I found, if you want to learn more about the glomerulus and glomerular filtration</p>\n</div>\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"from IPython.display import HTML\nHTML('<center><iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/SVqSqPOcahY\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe></center>')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<h2 style=\"font-size: 2em\"> 2. <a id=\"Data\">Importing the necessary libraries</a> 📗</h2>"},{"metadata":{"trusted":true},"cell_type":"code","source":"%%capture\npip install seaborn --upgrade","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport seaborn as sns\nimport pandas as pd\nimport sys\nimport matplotlib.pyplot as plt\nimport os\nimport cv2\nimport json\nimport pprint\n\n#color\nfrom colorama import Fore, Back, Style","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<h2 style=\"font-size: 2em\"> 3. <a id=\"Data\">The Data </a> 🏕️</h2>"},{"metadata":{},"cell_type":"markdown","source":"<h3 style=\"font-size: 1.5em\"> 3.1 <a id=\"Data\">Analyzing images and contours</a></h3>"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"def rle2mask(mask_rle, shape):\n    # https://www.kaggle.com/paulorzp/rle-functions-run-lenght-encode-decode\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\n\ndef mask_to_contours(image, mask_layer, thickness):\n    \"\"\" converts a mask to contours using OpenCV and draws them on the image\"\"\"\n    contours, hierarchy = cv2.findContours(mask_layer.astype('uint8'), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n    image = cv2.drawContours(image, contours, -1, color=(255,0,0),thickness=thickness)\n    return image","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"#Data paths\nimg_path = \"../input/hubmap-kidney-segmentation/train/0486052bb.tiff\"\ntrain_path = \"../input/hubmap-kidney-segmentation/train.csv\"\nimg_id = img_path.split(\"/\")[-1].split(\".\")[0]\nstruct_path = \"../input/hubmap-kidney-segmentation/train/\" + img_id + \"-anatomical-structure.json\"\nglm_path = \"../input/hubmap-kidney-segmentation/train/\" + img_id + \".json\"\n\n#Read the image an generate the binary mask from the RL encoded data\norg_img = cv2.imread(img_path)\nimg = np.copy(org_img)\ndf_train = pd.read_csv(train_path)\nmask = rle2mask(\n    df_train[df_train[\"id\"] == img_id][\"encoding\"].values[0], \n    (img.shape[1], img.shape[0])\n)\n\n#Merge image with glomeruli mask \nimg = mask_to_contours(img, mask, thickness=-1)\n\n#Extract contours of anathomical structures from json file\nfile = open(struct_path)\nstructures_json = json.load(file)\n\n#Merge the image with the contours of the anathomical structures \nfor structure in structures_json:\n    my_class = structure['properties']['classification']['name']\n    if my_class == 'Medulla': #Note that Medulla will be delimited by a green contour\n        A = np.asarray(structure['geometry']['coordinates'])\n        A = [np.swapaxes(A,0,1)]\n        img = cv2.drawContours(img, A, -1, color=(0,255,0),thickness=50)\n    else: #Note that Cortex will be delimited by a blue contour\n        A = np.asarray(structure['geometry']['coordinates'])\n        A = [np.swapaxes(A,0,1)]\n        img = cv2.drawContours(img, A, -1, color=(0,0,255),thickness=50)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.gcf()\nfig.set_size_inches(15,12)\nplt.imshow(img)\nplt.grid(False)\nprint(\"Notice that the\",Fore.GREEN, \"Medulla\",Style.RESET_ALL ,\"is delimited by the\", Fore.GREEN,\"green\",Style.RESET_ALL,\"line, and the\" ,Fore.BLUE, \"Cortex\",Style.RESET_ALL , \"is delimited by a\", Fore.BLUE,\"blue\",Style.RESET_ALL,\"line\")\nprint(\"The\",Fore.RED, \"red\",Style.RESET_ALL ,\"dots correspond to the\",Fore.RED, \"glomerulli\",Style.RESET_ALL )\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"file = open(glm_path)\nmy_json = json.load(file)\nprint(\"This JSON file has\", Fore.RED,len(my_json), Style.RESET_ALL ,\"elements.\")\nprint(\"Each element represent a\", Fore.RED + \"glomerulus\", Style.RESET_ALL)\nprint(\"The first element is printed below\")\nprint(\"\\n\")\npprint.pprint(my_json[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#We can zoom-in on multiple regions of the image where we can find glomeruli\nseeds_y = [18000, 9000, 3000, 2000]\nseeds_x = [8000, 4000, 19500, 11000]\nglm_img = np.copy(org_img)\nf, axs = plt.subplots(2,2, figsize=(18,18))\nfor i, ax in enumerate(axs.flat):\n    a,b = seeds_y[i], seeds_x[i]\n    A = mask_to_contours(glm_img[a:a+1000, b:b+1000], mask[a:a+1000, b:b+1000], thickness=5)\n    ax.imshow(A)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#We can zoom-in on multiple regions of the image but delimited to the Medulla\nseeds_y = [10000, 10000, 11000, 11000]\nseeds_x = [15000, 16000, 15000, 16000]\nf, axs = plt.subplots(2,2, figsize=(18,18))\nfor i, ax in enumerate(axs.flat):\n    a,b = seeds_y[i], seeds_x[i]\n    A = mask_to_contours(glm_img[a:a+1000, b:b+1000], mask[a:a+1000, b:b+1000], thickness=5)\n    ax.imshow(A)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<h3 style=\"font-size: 1.5em\"> 3.2 <a id=\"Data\">Analyzing patient data</a></h3>"},{"metadata":{"trusted":true},"cell_type":"code","source":"my_df = pd.read_csv(\"../input/hubmap-kidney-segmentation/HuBMAP-20-dataset_information.csv\")\nmy_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.style.use('seaborn')\n\nRC = my_df['race'].value_counts()\nSX = my_df['sex'].value_counts()\nLT = my_df['laterality'].value_counts()\n\ncolors = ['#008fd5', '#fc4f30']\nexplode = (0, 0.1)\nexplode2 = (0.2, 0)\n\nf = plt.gcf()\nf.set_size_inches(16,6)\nplt.subplot(1,3,1)\n\nplt.pie(RC, startangle=90, colors=colors, \n        wedgeprops={'edgecolor': 'black'}, autopct='%1.f%%', \n        explode=explode, shadow=True)\nplt.title(\"Race\")\nplt.legend(loc='best', labels=RC.keys(), fontsize='medium')\n\nplt.subplot(1,3,2)\nplt.pie(SX, startangle=90, colors=colors, \n        wedgeprops={'edgecolor': 'black'}, autopct='%1.f%%', \n        explode=explode, shadow=True)\nplt.title(\"Sex\")\nplt.legend(loc='best', labels=SX.keys(), fontsize='medium')\n\nplt.subplot(1,3,3)\nplt.pie(LT, startangle=90, colors=colors, \n        wedgeprops={'edgecolor': 'black'}, autopct='%1.f%%', \n        explode=explode, shadow=True)\nplt.title(\"Laterality\")\nplt.legend(loc='best', labels=LT.keys(), fontsize='medium')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.gcf()\nfig.set_size_inches(18,13)\nplt.subplot(3,4,1)\nsns.histplot(x=\"age\",data=my_df)\nplt.subplot(3,4,2)\nsns.histplot(x=\"weight_kilograms\",data=my_df)\nplt.subplot(3,4,3)\nsns.histplot(x=\"height_centimeters\",data=my_df)\nplt.subplot(3,4,4)\nsns.histplot(x=\"bmi_kg/m^2\",data=my_df)\n\nplt.subplot(3,4,5)\nsns.histplot(x=\"age\", hue=\"sex\", data=my_df, palette=sns.color_palette()[0:4:2])\nplt.subplot(3,4,6)\nsns.histplot(x=\"weight_kilograms\", hue=\"sex\",data=my_df, palette=sns.color_palette()[0:4:2])\nplt.subplot(3,4,7)\nsns.histplot(x=\"height_centimeters\", hue=\"sex\", data=my_df, palette=sns.color_palette()[0:4:2])\nplt.subplot(3,4,8)\nsns.histplot(x=\"bmi_kg/m^2\", hue=\"sex\", data=my_df, palette=sns.color_palette()[0:4:2])\n\nplt.subplot(3,4,9)\nsns.histplot(x=\"age\", hue=\"race\", data=my_df, palette=sns.color_palette()[0:4:2])\nplt.subplot(3,4,10)\nsns.histplot(x=\"weight_kilograms\", hue=\"race\",data=my_df, palette=sns.color_palette()[0:4:2])\nplt.subplot(3,4,11)\nsns.histplot(x=\"height_centimeters\", hue=\"race\",data=my_df, palette=sns.color_palette()[0:4:2])\nplt.subplot(3,4,12)\nsns.histplot(x=\"bmi_kg/m^2\", hue=\"race\",data=my_df, palette=sns.color_palette()[0:4:2])\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<center><img src=\"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRRP_n04OUHDX7B1pmwX2U-JlK1BqOdKEcDdA&usqp=CAU\" width=\"500\" height=\"600\" alt=\"Work in progress|please return later\" /></center>"}],"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":4,"nbformat_minor":4}