{"cells":[{"metadata":{},"cell_type":"markdown","source":"# RANZCR CLiP - Catheter and Line Position Challenge"},{"metadata":{},"cell_type":"markdown","source":"![](https://storage.googleapis.com/kaggle-competitions/kaggle/23870/logos/header.png?t=2020-12-01-04-28-05)"},{"metadata":{},"cell_type":"markdown","source":"This is a simple exploratory data analysis for the [RANZCR CLiP - Catheter and Line Position Challenge](https://www.kaggle.com/c/ranzcr-clip-catheter-line-classification).\n\nIn this competition, you’ll detect the presence and position of catheters and lines on chest x-rays. Use machine learning to train and test your model on 40,000 images to categorize a tube that is poorly placed.\n\nThe dataset has been labelled with a set of definitions to ensure consistency with labelling:\n* The normal category includes lines that were appropriately positioned and did not require repositioning.\n* The borderline category includes lines that would ideally require some repositioning but would in most cases still function adequately in their current position.\n* The abnormal category included lines that required immediate repositioning.\n\nIf successful, your efforts may help clinicians save lives. Earlier detection of malpositioned catheters and lines is even more important as COVID-19 cases continue to surge. Many hospitals are at capacity and more patients are in need of these tubes and lines. Quick feedback on catheter and line placement could help clinicians better treat these patients. Beyond COVID-19, detection of line and tube position will ALWAYS be a requirement in many ill hospital patients."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nimport ast","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"ETT_labels = ['ETT - Abnormal', 'ETT - Borderline', 'ETT - Normal']\nNGT_labels = ['NGT - Abnormal', 'NGT - Borderline', 'NGT - Incompletely Imaged', 'NGT - Normal']\nCVC_labels = ['CVC - Abnormal', 'CVC - Borderline', 'CVC - Normal']\nSGC_labels = ['Swan Ganz Catheter Present']","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Train csv\n\n### Columns\n* StudyInstanceUID - unique ID for each image\n* ETT - Abnormal - endotracheal tube placement abnormal\n* ETT - Borderline - endotracheal tube placement borderline abnormal\n* ETT - Normal - endotracheal tube placement normal\n* NGT - Abnormal - nasogastric tube placement abnormal\n* NGT - Borderline - nasogastric tube placement borderline abnormal\n* NGT - Incompletely Imaged - nasogastric tube placement inconclusive due to imaging\n* NGT - Normal - nasogastric tube placement borderline normal\n* CVC - Abnormal - central venous catheter placement abnormal\n* CVC - Borderline - central venous catheter placement borderline abnormal\n* CVC - Normal - central venous catheter placement normal\n* Swan Ganz Catheter Present\n* PatientID - unique ID for each patient in the dataset"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"train = pd.read_csv('../input/ranzcr-clip-catheter-line-classification/train.csv')\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Train annotations\n\nThese are segmentation annotations for training samples that have them. They are included solely as additional information for competitors."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"df_annot = pd.read_csv(\"../input/ranzcr-clip-catheter-line-classification/train_annotations.csv\")\ndf_annot.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# But what are all these names?"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"def image_annotations(label_type, index):\n    uid = df_annot[df_annot.label == label_type].StudyInstanceUID.values[index]\n    row = df_annot[(df_annot.StudyInstanceUID == uid) & (df_annot.label == label_type)]      \n\n    data = np.empty([0, 2])\n    for i in row.data:\n        row_data = np.array(ast.literal_eval(i))\n        data = np.concatenate((data, row_data), axis=0)    \n    \n    image_path = f\"../input/ranzcr-clip-catheter-line-classification/train/{uid}.jpg\"\n    image = cv2.imread(image_path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    \n    fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (10,5))\n    fig.suptitle(label_type, fontsize=20, y=1.05)\n    ax1.imshow(image)\n    ax2.imshow(image)\n    ax2.scatter(data[:, 0], data[:, 1])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 1- Endotracheal Tube Placement (ETT)\n\n> Endotracheal tubes (ETT) are wide-bore plastic tubes that are inserted into the trachea to allow artificial ventilation. Tubes come in a variety of sizes and have a balloon at the tip to ensure that gastric contents are not aspirated into the lungs. Adult tubes are usually approximately 1 cm in diameter. Tubes have a radiopaque strip within them so that they are visible on radiographs."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"label_type = ETT_labels[2]\nimage_annotations(label_type, 4)\nlabel_type = ETT_labels[1]\nimage_annotations(label_type, 0)\nlabel_type = ETT_labels[0]\nimage_annotations(label_type, 8)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 2- Nasogastric Tube Placement (NGT)\n\n> Nasopharyngeal airway tubes are commonly used adjunctory airway devices, primarily utilized as a temporary measure until more stable method of securing the airways (e.g. endotracheal intubation) can be performed. \n1. > \n> Nasopharyngeal airway tubes are made of soft plastic, and are introduced via the nose until the posterior pharynx is reached. As these devices do not trigger a gag reflex, they are particularly useful for alert patients, or in case of difficulty of opening the mouth (e.g. trismus) 1. "},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"label_type = NGT_labels[3]\nimage_annotations(label_type, 5)\nlabel_type = NGT_labels[1]\nimage_annotations(label_type, 1)\nlabel_type = NGT_labels[0]\nimage_annotations(label_type, 5)\nlabel_type = NGT_labels[2]\nimage_annotations(label_type, 0)\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 3- Central Venous Catheter (CVC)\n\n> Central venous catheters (CVC) or lines (CVL) refer to a wide range of central venous access devices but can broadly be divided into four categories. They may be inserted by medical, surgical, anesthetic/ITU, or radiology specialists. "},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"label_type = CVC_labels[2]\nimage_annotations(label_type, 5)\nlabel_type = CVC_labels[1]\nimage_annotations(label_type, 1)\nlabel_type = CVC_labels[0]\nimage_annotations(label_type, 2)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 4- Swan Ganz Catheter Present\n\n> Pulmonary artery catheters (or Swan-Ganz catheters) are balloon flotation catheters that can be inserted simply, quickly, with little training and without fluoroscopic guidance, at the bedside, even in the seriously ill patient. Historically they were widely used to measure right heart hemodynamic indices and pulmonary arterial and capillary wedge pressures. More recently their use has fallen out of favor, due to adverse trial data, however, they still have important niche uses.\n> \n> These catheters should ideally be positioned in the proximal right or left main pulmonary artery. "},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"label_type = SGC_labels[0]\nimage_annotations(label_type, 3)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Distribution of the labels"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"label_counts = train.iloc[:, 1:-1].sum(axis=0).reset_index()\nlabel_counts.columns = ['Type', 'Observations']\nlabel_counts = label_counts\n\nsns.set(style=\"darkgrid\")\nplt.figure(figsize=(12, 8))\nplt.title('Number of observations', fontsize=16)\nax = sns.barplot(data = label_counts, x = 'Type', y = 'Observations', palette=\"viridis\")\nax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha=\"right\")\n\nfor p in ax.patches:\n    height = p.get_height()\n    ax.text(p.get_x() + p.get_width() / 2., height, '{:1.2f}'.format(height), ha=\"center\", weight='regular')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"train_grouped = pd.DataFrame()\n\ntrain_grouped['ETT'] = train[ETT_labels].sum(axis=1)\ntrain_grouped['NGT'] = train[NGT_labels].sum(axis=1)\ntrain_grouped['CVC'] = train[CVC_labels].sum(axis=1)\ntrain_grouped['SGC'] = train[SGC_labels].sum(axis=1)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"sns.set(style=\"darkgrid\")\nchart_color = 'viridis'\ntitle_fontsize = 12\nsuptitle_fontsize = 16\nchart_w, chart_h = 3, 4\n\nfig, (ax11, ax12, ax13) = plt.subplots(1, 3, figsize = (chart_w*3, chart_h))\nfig.suptitle('Endotracheal Tube Placement', fontsize=suptitle_fontsize, y=1.05)\n\na1 = sns.countplot(train[ETT_labels[0]], ax=ax11, palette=chart_color)\na1.set_title(ETT_labels[0], fontsize=title_fontsize, y=1.03)\n\na2 = sns.countplot(train[ETT_labels[1]], ax=ax12, palette=chart_color)\na2.set_title(ETT_labels[1], fontsize=title_fontsize, y=1.03)\n\na3 = sns.countplot(train[ETT_labels[2]], ax=ax13, palette=chart_color)\na3.set_title(ETT_labels[2], fontsize=title_fontsize, y=1.03)\n\nfig, (ax21, ax22, ax23, ax24) = plt.subplots(1, 4, figsize = (chart_w*4, chart_h))\nfig.suptitle('Nasogastric Tube Placement', fontsize=suptitle_fontsize, y=1.05)\n\nb1 = sns.countplot(train[NGT_labels[0]], ax=ax21, palette=chart_color)\nb1.set_title(NGT_labels[0], fontsize=title_fontsize, y=1.03)\n\nb2 = sns.countplot(train[NGT_labels[1]], ax=ax22, palette=chart_color)\nb2.set_title(NGT_labels[1], fontsize=title_fontsize, y=1.03)\n\nb3 = sns.countplot(train[NGT_labels[2]], ax=ax23, palette=chart_color)\nb3.set_title(NGT_labels[2], fontsize=title_fontsize, y=1.03)\n\nb4 = sns.countplot(train[NGT_labels[3]], ax=ax24, palette=chart_color)\nb4.set_title(NGT_labels[3], fontsize=title_fontsize, y=1.03)\n\n\nfig, (ax31, ax32, ax33) = plt.subplots(1, 3, figsize = (chart_w*3, chart_h))\nfig.suptitle('Central Venous Catheter', fontsize=suptitle_fontsize, y=1.05)\n\nc1 = sns.countplot(train[CVC_labels[0]], ax=ax31, palette=chart_color)\nc1.set_title(CVC_labels[0], fontsize=title_fontsize, y=1.03)\n\nc2 = sns.countplot(train[CVC_labels[1]], ax=ax32, palette=chart_color)\nc2.set_title(CVC_labels[1], fontsize=title_fontsize, y=1.03)\n\nc3 = sns.countplot(train[CVC_labels[2]], ax=ax33, palette=chart_color)\nc3.set_title(CVC_labels[2], fontsize=title_fontsize, y=1.03)\n\n\nfig, ax41 = plt.subplots(1, 1, figsize = (chart_w, chart_h))\nfig.suptitle('Swan Ganz Catheter', fontsize=suptitle_fontsize, y=1.05)\n\nd1 = sns.countplot(train[SGC_labels[0]], ax=ax41, palette=chart_color)\nd1.set_title(SGC_labels[0], fontsize=title_fontsize, y=1.03)\n\n\nfor ax in [ax11, ax12, ax13, ax21, ax22, ax23, ax24, ax31, ax32, ax33, ax41]:\n    for p in ax.patches:\n        height = p.get_height()\n        ax.text(p.get_x() + p.get_width() / 2., height, '{:1.2f}'.format(height), ha=\"center\", weight='regular')","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":false,"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"sns.set(style=\"white\")\n\ntrain_grouped_ones = pd.DataFrame()\nfor i in train_grouped.keys():\n    train_grouped_ones[i] = train_grouped[i].map(lambda x: 1 if x != 0 else 0)\n\ncorr = train_grouped_ones.corr()\nmask = np.triu(np.ones_like(corr, dtype=bool))\nfig, ax = plt.subplots(figsize=(11, 9))\nax.set_title('Correlation between categories', fontsize=16)\ncmap = sns.diverging_palette(230, 20, as_cmap=True)\n\n\nsns.heatmap(corr, mask=mask, cmap=cmap, center=0,\n            square=True, linewidths=.5, cbar_kws={\"shrink\": .6}, annot=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# To be continued..."},{"metadata":{},"cell_type":"markdown","source":"# References\n\n* [radiopaedia](https://radiopaedia.org/articles/lines-and-tubes-radiograph)"},{"metadata":{},"cell_type":"markdown","source":"## Hey guys, this is my first notebook, if you have any tips for me to improve, I would love to hear it!"}],"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}