{"cells":[{"metadata":{},"cell_type":"markdown","source":"# <u>RANZCR CLiP: Visualize and Understand Dataset</u>\n\n## Contents\n\n1. [Introduction](#1)\n1. [Background knowledge](#2)\n1. [Data overview](#3)\n1. [Visualize x-rays image](#4)\n1. [Visualize x-ray images with annotation](#5)\n1. [Visualize train label](#6)"},{"metadata":{},"cell_type":"markdown","source":"<a id=\"1\"></a> <br>\n# <div class=\"alert alert-block alert-info\">Introduction</div>"},{"metadata":{},"cell_type":"markdown","source":"## Goal\n\nTo create model to detect the presence and position of catheters and lines on chest x-rays,  and categorize a tube that is poorly placed.\n\nDeep learning algorithms may be able to automatically detect malpositioned catheters and lines. Once alerted, clinicians can reposition or remove them to avoid life-threatening complications."},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"import re\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image, ImageDraw\nimport seaborn as sns\nfrom IPython.display import YouTubeVideo","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"--------------"},{"metadata":{},"cell_type":"markdown","source":"<a id=\"2\"></a> <br>\n# <div class=\"alert alert-block alert-info\">Background knowledge</div>\n\nI'll introduce technical words and its' references to need to understand dataset here."},{"metadata":{},"cell_type":"markdown","source":"## Endotracheal Tube\n\nAn endotracheal tube is a flexible plastic tube that is placed through the mouth into the trachea (windpipe) to help a patient breathe. The endotracheal tube is then connected to a ventilator, which delivers oxygen to the lungs. The process of inserting the tube is called endotracheal intubation.\n\nFor more information, see [How an Endotracheal Tube Is Used](https://www.verywellhealth.com/endotracheal-tube-information-2249093)."},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"YouTubeVideo('FtJr7i7ENMY')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Nasogastric Tube\n\nNasogastric tube is a plastic tube through the nose, past the throat, and down into the stomach used in nasogastric intubation medical process. \n\nFor more information, see [Wikipedia - Nasogastric intubation](https://en.wikipedia.org/wiki/Nasogastric_intubation)."},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"YouTubeVideo('Abf3Gd6AaZQ')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Central venous catheter\n\nA central venous catheter (CVC), also known as a central line is a catheter placed into a large vein. It is a form of venous access. Placement of larger catheters in more centrally located veins is often needed in critically ill patients, or in those requiring prolonged intravenous therapies, for more reliable vascular access. \n\nFor more information, see [Wikipedia - Central venous catheter](https://en.wikipedia.org/wiki/Central_venous_catheter)."},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"YouTubeVideo('mTBrCMn86cU')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Swan Ganz Catheter Present\n\nSwan-Ganz catheter is used in Pulmonary artery catheterization (PAC), or right heart catheterization.  In the operation, the insertion of a catheter into a pulmonary artery. Its purpose is diagnostic; it is used to detect heart failure or sepsis, monitor therapy, and evaluate the effects of drugs. \n\nFor more information, see [Wikipedia - Pulmonary artery catheter](https://en.wikipedia.org/wiki/Pulmonary_artery_catheter)."},{"metadata":{"trusted":true},"cell_type":"code","source":"YouTubeVideo('YkN30T6ig30')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"--------------"},{"metadata":{},"cell_type":"markdown","source":"<a id=\"3\"></a> <br>\n# <div class=\"alert alert-block alert-success\">Data overview</div>"},{"metadata":{},"cell_type":"markdown","source":"Let's see given data. there are roughly 7 kinds data."},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"!ls ../input/ranzcr-clip-catheter-line-classification/","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## train.csv\n\ntrain.csv contains image IDs, binary labels, and patient IDs."},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(\"../input/ranzcr-clip-catheter-line-classification/train.csv\")\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.info()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## train_annotations.csv\n\nSegmentation annotations for training samples that have them."},{"metadata":{"trusted":true},"cell_type":"code","source":"train_anno = pd.read_csv(\"../input/ranzcr-clip-catheter-line-classification/train_annotations.csv\")\ntrain_anno.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_anno.info()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"There are all label column values."},{"metadata":{"trusted":true},"cell_type":"code","source":"set(train_anno.label)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## sample_submission.csv\n\nsample_submission.csv is sumple submission file."},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = pd.read_csv(\"../input/ranzcr-clip-catheter-line-classification/sample_submission.csv\")\nsub.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## train, test\n\nThere are chest x-rays images. I'll explain how to visualize them [here](#4)."},{"metadata":{},"cell_type":"markdown","source":"## train_tfrecords, test_tfrecords\n\nSerialized train and test data."},{"metadata":{"trusted":true},"cell_type":"code","source":"! ls ../input/ranzcr-clip-catheter-line-classification/train_tfrecords/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! ls ../input/ranzcr-clip-catheter-line-classification/test_tfrecords/","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<a id=\"4\"></a> <br>\n# <div class=\"alert alert-block alert-success\">Visualize x-rays image</div>\n\nWe can visualize data by matplotlib."},{"metadata":{"trusted":true},"cell_type":"code","source":"train_idx = 5010\nuid = train[[\"StudyInstanceUID\"]].iat[train_idx,0]\ntrain_img = Image.open(f\"../input/ranzcr-clip-catheter-line-classification/train/{uid}.jpg\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(15, 15))\nplt.imshow(train_img)\nplt.title(f\"train_{uid}\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We can see that this is an X-ray image of the chest. We can also see that the right and left sides are marked. We can also see that there are several tubes in the image, too.\n\nBut we can't recognize where the tubing and catheters to detect are, so we will overwrite the annotation data in the next section to confirm."},{"metadata":{},"cell_type":"markdown","source":"<a id=\"5\"></a> <br>\n# <div class=\"alert alert-block alert-success\">Visualize x-ray images with annotation</div>\n\nWe'll extract the annotation data from train_annotations.csv and overwrite the images."},{"metadata":{},"cell_type":"markdown","source":"## Normal sample\n\nFirst, we'll check \"XXX - Normal\" images.\n\nThe data which StudyInstanceUID is '1.2.826.0.1.3680043.8.498.11012190756062412030973253000324820445' is good for example, so I'll extract it."},{"metadata":{"trusted":true},"cell_type":"code","source":"uid = train_anno[\"StudyInstanceUID\"].iloc[5010]\nuid","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"anno_data = train_anno[train_anno[\"StudyInstanceUID\"] == uid]\nanno_data = anno_data.reset_index(drop=True)\nanno_data","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We can find that in this image,  \"NGT - Normal\", \"ETT - Normal\" and \"CVC - Normal\" labeled tubes and catheters are.\n\nWe'll create a utility function to add easy-to-read annotations."},{"metadata":{"trusted":true},"cell_type":"code","source":"def draw_annotaion(structures, im):\n    \"\"\"\n    anatomical_structure: list of points of annotation.\n    im: Pillow  image instance.\n    \"\"\"\n    points = []\n    draw = ImageDraw.Draw(im)\n    for i in range(0, len(structures), 2):\n        points.append(tuple(structures[i:i+2]))\n        \n        draw.line(points, width=50, fill='red')\n    return im","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We will extract the annotation data from each dataframe using regular expressions since the list is stored as a string in the dataframe.\n\nThe extracted annotations will be overwritten on the images."},{"metadata":{"trusted":true},"cell_type":"code","source":"regex = re.compile('\\d+')\nimages = []\nlabels = anno_data[\"label\"].values\nfor i in range(len(anno_data)):\n    match = regex.findall(anno_data[\"data\"].at[i])\n    match = [int(item) for item in match]\n    images.append(draw_annotaion(match, train_img.copy()))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Let's visualize them."},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, axs = plt.subplots(2, 2,  figsize=(15,15))\ncnt = 0\nfor i in range(2):\n    for j in range(2):\n        axs[i][j].imshow(images[cnt])\n        axs[i][j].set_title(f\"{labels[cnt]}\")\n        cnt += 1","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Swan Ganz Catheter Present\n\nWe'll also check Swan-Ganz Catheter."},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"uid = train_anno[\"StudyInstanceUID\"].iloc[170]\nanno_data = train_anno[train_anno[\"StudyInstanceUID\"] == uid]\nanno_data = anno_data.reset_index(drop=True)\nanno_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = Image.open(f\"../input/ranzcr-clip-catheter-line-classification/train/{uid}.jpg\")\n\nmatch = regex.findall(anno_data[\"data\"].at[0])\nmatch = [int(item) for item in match]\n    \nimg_annotated = draw_annotaion(match, img.copy())\nplt.figure(figsize=(8, 8))\nplt.imshow(img_annotated)\nplt.title(\"Swan Ganz Catheter Present\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Abnormal sample\n\nWe will also look at the abnormal data."},{"metadata":{},"cell_type":"markdown","source":"### CVC"},{"metadata":{"trusted":true},"cell_type":"code","source":"uid = train_anno[\"StudyInstanceUID\"].iloc[25]\nanno_data = train_anno[train_anno[\"StudyInstanceUID\"] == uid]\nanno_data = anno_data.reset_index(drop=True)\nanno_data","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"img = Image.open(f\"../input/ranzcr-clip-catheter-line-classification/train/{uid}.jpg\")\n\nmatch = regex.findall(anno_data[\"data\"].at[0])\nmatch = [int(item) for item in match]\n    \nimg_annotated = draw_annotaion(match, img.copy())\nplt.figure(figsize=(8, 8))\nplt.imshow(img_annotated)\nplt.title(\"CVC - Abnormal\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### ETT"},{"metadata":{"trusted":true},"cell_type":"code","source":"uid = train_anno[\"StudyInstanceUID\"].iloc[1562]\nanno_data = train_anno[train_anno[\"StudyInstanceUID\"] == uid]\nanno_data = anno_data.reset_index(drop=True)\nanno_data","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"img = Image.open(f\"../input/ranzcr-clip-catheter-line-classification/train/{uid}.jpg\")\n\nmatch = regex.findall(anno_data[\"data\"].at[0])\nmatch = [int(item) for item in match]\n    \nimg_annotated = draw_annotaion(match, img.copy())\nplt.figure(figsize=(8, 8))\nplt.imshow(img_annotated)\nplt.title(\"ETT - Abnormal\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### NGT"},{"metadata":{"trusted":true},"cell_type":"code","source":"uid = train_anno[\"StudyInstanceUID\"].iloc[1287]\nanno_data = train_anno[train_anno[\"StudyInstanceUID\"] == uid]\nanno_data = anno_data.reset_index(drop=True)\nanno_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = Image.open(f\"../input/ranzcr-clip-catheter-line-classification/train/{uid}.jpg\")\n\nmatch = regex.findall(anno_data[\"data\"].at[0])\nmatch = [int(item) for item in match]\n    \nimg_annotated = draw_annotaion(match, img.copy())\nplt.figure(figsize=(8, 8))\nplt.imshow(img_annotated)\nplt.title(\"NGT - Abnormal\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Borderline sample\n\nWe will also look at the borderline data."},{"metadata":{},"cell_type":"markdown","source":"### ETE"},{"metadata":{"trusted":true},"cell_type":"code","source":"uid = train_anno[\"StudyInstanceUID\"].iloc[129]\nanno_data = train_anno[train_anno[\"StudyInstanceUID\"] == uid]\nanno_data = anno_data.reset_index(drop=True)\nanno_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = Image.open(f\"../input/ranzcr-clip-catheter-line-classification/train/{uid}.jpg\")\n\nmatch = regex.findall(anno_data[\"data\"].at[0])\nmatch = [int(item) for item in match]\n    \nimg_annotated = draw_annotaion(match, img.copy())\nplt.figure(figsize=(8, 8))\nplt.imshow(img_annotated)\nplt.title(\"ETE - Borderline\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### NGT"},{"metadata":{"trusted":true},"cell_type":"code","source":"uid = train_anno[\"StudyInstanceUID\"].iloc[515]\nanno_data = train_anno[train_anno[\"StudyInstanceUID\"] == uid]\nanno_data = anno_data.reset_index(drop=True)\nanno_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = Image.open(f\"../input/ranzcr-clip-catheter-line-classification/train/{uid}.jpg\")\n\nmatch = regex.findall(anno_data[\"data\"].at[2])\nmatch = [int(item) for item in match]\n    \nimg_annotated = draw_annotaion(match, img.copy())\nplt.figure(figsize=(8, 8))\nplt.imshow(img_annotated)\nplt.title(\"NGT - Borderline\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### CVC"},{"metadata":{"trusted":true},"cell_type":"code","source":"uid = train_anno[\"StudyInstanceUID\"].iloc[17940]\nanno_data = train_anno[train_anno[\"StudyInstanceUID\"] == uid]\nanno_data = anno_data.reset_index(drop=True)\nanno_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = Image.open(f\"../input/ranzcr-clip-catheter-line-classification/train/{uid}.jpg\")\n\nmatch = regex.findall(anno_data[\"data\"].at[1])\nmatch = [int(item) for item in match]\n    \nimg_annotated = draw_annotaion(match, img.copy())\nplt.figure(figsize=(8, 8))\nplt.imshow(img_annotated)\nplt.title(\"CVC - Borderline\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Incompletely Imaged sample\n\nIncompletely Imaged is probably an image that the NGtube has protruded from the x-ray image."},{"metadata":{"trusted":true},"cell_type":"code","source":"uid = train_anno[\"StudyInstanceUID\"].iloc[17867]\nanno_data = train_anno[train_anno[\"StudyInstanceUID\"] == uid]\nanno_data = anno_data.reset_index(drop=True)\nanno_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = Image.open(f\"../input/ranzcr-clip-catheter-line-classification/train/{uid}.jpg\")\n\nmatch = regex.findall(anno_data[\"data\"].at[1])\nmatch = [int(item) for item in match]\n    \nimg_annotated = draw_annotaion(match, img.copy())\nplt.figure(figsize=(8, 8))\nplt.imshow(img_annotated)\nplt.title(\"NGT - Incompletely Imaged\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"uid = train_anno[\"StudyInstanceUID\"].iloc[457]\nanno_data = train_anno[train_anno[\"StudyInstanceUID\"] == uid]\nanno_data = anno_data.reset_index(drop=True)\nanno_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = Image.open(f\"../input/ranzcr-clip-catheter-line-classification/train/{uid}.jpg\")\n\nmatch = regex.findall(anno_data[\"data\"].at[2])\nmatch = [int(item) for item in match]\n    \nimg_annotated = draw_annotaion(match, img.copy())\nplt.figure(figsize=(8, 8))\nplt.imshow(img_annotated)\nplt.title(\"NGT - Incompletely Imaged\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<a id=\"6\"></a> <br>\n# <div class=\"alert alert-block alert-success\">Visualize train label</div>\n\nIn train.csv, there are binary labels. From this data, we can determine what treatment has been performed on each StudyInstanceUID image and what state it is in, so we will visualize it."},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"To prepare, define variables and utility functions."},{"metadata":{"trusted":true},"cell_type":"code","source":"ett_cols = [col for col in train.columns if \"ETT\" in col]\nngt_cols = [col for col in train.columns if \"NGT\" in col]\ncvc_cols = [col for col in train.columns if \"CVC\" in col]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def df_transform(df):\n    df_res = pd.DataFrame()\n    for col in df.columns:\n        df_tmp = pd.DataFrame(df[col])\n        df_tmp.columns = [\"data\"]\n        df_tmp[\"type\"] = col\n        df_res = pd.concat([df_res, df_tmp])\n        \n    return df_res","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Breakdown of each treatment performed"},{"metadata":{},"cell_type":"markdown","source":"ETT seems to have been done with about 10% of the training data. Of the data conducted, most seems to be normal, with a little borderline. Abnormal is rarely."},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, axes = plt.subplots(1, 2, figsize=(15,6), gridspec_kw=dict(wspace=0.3, hspace=0.6))\ng1 = sns.countplot(data = df_transform(train[ett_cols]),x=\"data\", ax=axes[0])\ng1.set_title(\"Number of each status of ETT\")\n\ng2 = sns.countplot(data = df_transform(train[ett_cols]),x=\"data\", hue=\"type\", ax=axes[1])\ng2.set_title(\"Number of each status of ETT - detail\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"NGT seems to have been done with about 10% of the training data, too. Borderline and Abnormal are rarely."},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, axes = plt.subplots(1, 2, figsize=(15,6), gridspec_kw=dict(wspace=0.3, hspace=0.6))\ng1 = sns.countplot(data = df_transform(train[ngt_cols]),x=\"data\", ax=axes[0])\ng1.set_title(\"Number of each status of NGT\")\n\ng2 = sns.countplot(data = df_transform(train[ngt_cols]),x=\"data\", hue=\"type\", ax=axes[1])\ng2.set_title(\"Number of each status of NGT - detail\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"CVC seems to have been done with about 30% of the training data, too. Borderline and Abnormal are less common than normal, but we can see that a not insignificant number of cases are occurring."},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, axes = plt.subplots(1, 2, figsize=(15,6), gridspec_kw=dict(wspace=0.3, hspace=0.6))\ng1 = sns.countplot(data = df_transform(train[cvc_cols]),x=\"data\", ax=axes[0])\ng1.set_title(\"Number of each status of NCVCGT\")\n\ng2 = sns.countplot(data = df_transform(train[cvc_cols]),x=\"data\", hue=\"type\", ax=axes[1])\ng2.set_title(\"Number of each status of CVC - detail\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"There seem to be few procedures that use the Swan Ganz Catheter."},{"metadata":{"trusted":true},"cell_type":"code","source":"g = sns.countplot(data = train, x=\"Swan Ganz Catheter Present\")\ng.set_title(\"Number of exists of Swan Ganz Catheter Present\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Is the treatment being performed at the same time?"},{"metadata":{},"cell_type":"markdown","source":"Each record seems to have several tubes and catheters. If the value is 1, there are tubes and catheters. So if we sum the values, we can know the number of them."},{"metadata":{"trusted":true},"cell_type":"code","source":"data_cols = [col for col in train.columns if col not in ['StudyInstanceUID', 'PatientID']]\ng = sns.countplot(np.sum(train[data_cols].values, axis=1))\ng.set_title(\"Number of conditions occurring simultaneously for a PatientID\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Most of the images are of a single procedure, but there are also less than 10000 images of two or more procedures being performed. There are also less than 3000 images of four or more."}],"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}