{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Introduction\nWelcome to the [VinBigData Chest X-ray Abnormalities Detection](https://www.kaggle.com/c/vinbigdata-chest-xray-abnormalities-detection/data) compedition.　　\n\nFirst of all, I would like to mention that I used https://www.kaggle.com/drcapa/chest-x-ray-starter as a reference to create this Notebook.  \n\nThere are some great explanations in past Kaggle Notebooks, so you may want to refer to those as well.\nhttps://www.kaggle.com/zahaviguy/what-are-lung-opacities\n\nI am a beginner in both Kaggle and English, so I would like to grow from now on.  \n\n\n# Basic knowledge of X-ray\nThe degree of absorption of X-rays is defined by the X-rays' wavelength, the thickness of the subject, the density of the subject, and the atoms that make up the subject. When X-rays are irradiated to a living organism, different degrees of blackness (degree of blackness) appear on the X-ray film depending on their absorption. Bones are the whitest part of the human body on film because they absorb X-rays and do not allow them to pass through, while lung fields appear relatively black because the lungs contain a lot of air.   \n**Note that this competition image includes a black and white inverted image in addition to the standard image.**\n"},{"metadata":{},"cell_type":"markdown","source":"# Table of Contents\n\nIn this Notebook, we will briefly discuss the following 15 types.\nI have prioritized short and easy to understand over medical correctness. If there are any incorrect expressions, I would appreciate your comments.\n\n- [1. No finding](#1.-No-finding)\n- [2. Aortic enlargement](#2.-Aortic-enlargement)\n- [3. Atelectasis](#3.-Atelectasis)\n- [4. Calcification](#4.-Calcification)\n- [5. Cardiomegaly](#5.-Cardiomegaly)\n- [6. Consolidation](#6.-Consolidation)\n- [7. ILD](#7.-ILD)\n- [8. Infiltration](#8.-Infiltration)\n- [9. Lung Opacity](#9.-Lung-Opacity)\n- [10. Nodule/Mass](#10.-Nodule/Mass)\n- [11. Other lesion](#11.-Other-lesion)\n- [12. Pleural effusion](#12.-Pleural-effusion)\n- [13. Pleural thickening](#13.-Pleural-thickening)\n- [14. Pneumothorax](#14.-Pneumothorax)\n- [15. Plumonary fibrosis](#15.-Plumonary-fibrosis)"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib\nimport seaborn as sns\nimport pydicom as dicom\nimport cv2\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"path = '/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/'\n#os.listdir(path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"train_data = pd.read_csv(path+'train.csv')\nsamp_subm = pd.read_csv(path+'sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"#print('Number train samples:', len(train_data.index))\n#print('Number test samples:', len(samp_subm.index))","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(1, 1, figsize=(12, 4))\nx = train_data['class_name'].value_counts().keys()\ny = train_data['class_name'].value_counts().values\nax.bar(x, y)\nax.set_xticklabels(x, rotation=90)\nax.set_title('Distribution of the labels')\nplt.grid()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Show Examples"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"def plot_example(idx_list):\n    fig, axs = plt.subplots(1, 3, figsize=(15, 10))\n    fig.subplots_adjust(hspace = .1, wspace=.1)\n    axs = axs.ravel()\n    for i in range(3):\n        image_id = train_data.loc[idx_list[i], 'image_id']\n        data_file = dicom.dcmread(path+'train/'+image_id+'.dicom')\n        img = data_file.pixel_array\n        axs[i].imshow(img, cmap='gray')\n        axs[i].set_title(train_data.loc[idx_list[i], 'class_name'])\n        axs[i].set_xticklabels([])\n        axs[i].set_yticklabels([])\n        if train_data.loc[idx_list[i], 'class_name'] != 'No finding':\n            bbox = [train_data.loc[idx_list[i], 'x_min'],\n                    train_data.loc[idx_list[i], 'y_min'],\n                    train_data.loc[idx_list[i], 'x_max'],\n                    train_data.loc[idx_list[i], 'y_max']]\n            p = matplotlib.patches.Rectangle((bbox[0], bbox[1]),\n                                             bbox[2]-bbox[0],\n                                             bbox[3]-bbox[1],\n                                             ec='r', fc='none', lw=2.)\n            axs[i].add_patch(p)\n            ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 1. No finding\n\n[Back to top](#Table-of-Contents)\n\nThere are no findings on x-ray images.  \nThis is the normal image and is the baseline image needed to differentiate from the abnormal image."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"idx_list = train_data[train_data['class_id']==14][0:3].index.values\nplot_example(idx_list)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 2. Aortic enlargement\n\n[Back to top](#Table-of-Contents)\n\nAortic enlargement is known as a sign of an aortic aneurysm.\nProne to occur in the ascending aorta.\nIn general, the term aneurysm is used when the axial diameter is >5.0 cm for the ascending aorta and >4.0 cm for the descending aorta.  \n(Please compare the below images to the normal images.)"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"idx_list = train_data[train_data['class_id']==0][9:12].index.values\nplot_example(idx_list)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 3. Atelectasis\n[Back to top](#Table-of-Contents)\n\nAtelectasis is a condition where there is no air in part or all of the lungs. And the lungs are collapsed. A common cause of atelectasis is obstruction of the bronchi.  \n\nIn atelectasis, there is an increase in density on chest x-ray (usually whiter; black on black-and-white inversion images).   "},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"idx_list = train_data[train_data['class_id']==1][0:3].index.values\nplot_example(idx_list)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 4. Calcification\n[Back to top](#Table-of-Contents)\n\nMany diseases or conditions can cause calcification on chest x-ray. Calcium (calcification) may be deposited in areas where previous inflammation of the lungs or pleura has healed. Calcium may be deposited in the aorta due to atherosclerosis. Or calcification may occur in mediastinal lymph nodes.  \n\nThe image is characterized by a density similar to that of bone.  "},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"idx_list = train_data[train_data['class_id']==2][0:3].index.values\nplot_example(idx_list)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 5. Cardiomegaly\n[Back to top](#Table-of-Contents)\n\nCardiomegaly can be caused by many conditions, including hypertension, coronary artery disease, infections, inherited disorders, and cardiomyopathies.  \n\nCardiomegaly is usually diagnosed when the ratio of the heart's width to the width of the chest is more than 50%. This diagnostic criterion may be an essential basis for this competition.  \n\n*Comment from readers*  \n**For the criterion heart-to-lung ratio > 0.5 for the diagnosis of cardiomegaly.  \nThis is only valid if the X-ray is performed while the patient is standing. If the patient is sitting or in bed, this criterion cannot be used.\nAn essential point to know it is to detect air in the stomach: if there is no air in it, the patient is not standing. **  \n"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"idx_list = train_data[train_data['class_id']==3][0:3].index.values\nplot_example(idx_list)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 6. Consolidation\n[Back to top](#Table-of-Contents)\n\nConsolidation is officially referred to as air space consolidation. It is a decrease in lung permeability due to infiltration of fluid, cells, or tissue replacing the air-containing spaces in the alveoli. \n\nOn X-rays, the lung field's density is increased, and pulmonary blood vessels are not seen, but black bronchi can be seen in the white background, which is called \"air bronchogram.\" Since air remains in the bronchial tubes, they do not absorb X-rays and appear black, and the black and white are reversed from normal lung fields.  "},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"idx_list = train_data[train_data['class_id']==4][0:3].index.values\nplot_example(idx_list)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 7. ILD\n[Back to top](#Table-of-Contents)\n\nILD stands for \"Interstitial Lung Disease.\"  \nInterstitial lung disease is a general term for many conditions in which the interstitial space is injured. The interstitial space refers to the walls of the alveoli (air sacs in the lungs) and the space around the blood vessels and small airways.\n\nChest radiographic findings include ground-glass opacities (i.e., an area of hazy opacification), linear reticular shadows, and granular shadows.  "},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"idx_list = train_data[train_data['class_id']==5][0:3].index.values\nplot_example(idx_list)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 8. Infiltration\n[Back to top](#Table-of-Contents)\n\nThe infiltration of some fluid component into the alveoli causes an infiltrative shadow (Infiltration).  \n\nIt is difficult to distinguish from consolidation and, in some cases, impossible to distinguish.  \nPlease see https://allnurses.com/consolidation-vs-infiltrate-vs-opacity-t483538/.  "},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"idx_list = train_data[train_data['class_id']==6][0:3].index.values\nplot_example(idx_list)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 9. Lung Opacity\n[Back to top](#Table-of-Contents)\n\nLung opacity is defined as any area in the chest radiograph that is more white than it should be.  \nLung opacity is a loose term.\nPlease see https://www.kaggle.com/zahaviguy/what-are-lung-opacities.\n\nIt is difficult to distinguish from infiltration or consolidation and, in some cases, impossible to distinguish.\nPlease see https://allnurses.com/consolidation-vs-infiltrate-vs-opacity-t483538/.\n\n(Infiltration, consolidation, and opacity will vary depending on who labels them, so I personally question how meaningful it is to distinguish and predict them accurately.)"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"idx_list = train_data[train_data['class_id']==7][0:3].index.values\nplot_example(idx_list)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 10. Nodule/Mass\n[Back to top](#Table-of-Contents)\n\nA nodule/mass is a round shade (typically less than 3 cm in diameter) that appears on a chest X-ray image. It can be seen in primary lung cancer, metastasis from other parts of the body such as colon cancer and kidney cancer, tuberculosis, pulmonary mycosis, non-tuberculous mycobacterium, obsolete pneumonia, and benign tumors."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"idx_list = train_data[train_data['class_id']==8][0:3].index.values\nplot_example(idx_list)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 11. Other lesion\n[Back to top](#Table-of-Contents)\n\nOthers include all abnormalities that do not fall into any other category. This includes bone penetrating images, fractures, and subcutaneous emphysema, etc."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"idx_list = train_data[train_data['class_id']==9][0:3].index.values\nplot_example(idx_list)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 12. Pleural effusion\n[Back to top](#Table-of-Contents)\n\nPleural effusion is the accumulation of water outside the lungs in the chest cavity. The lungs are located in the chest cavity surrounded by the chest wall. The outside of the lungs is covered by a thin membrane called the pleura. The pleura consists of two layers, one on the chest wall (parietal pleura) and the other covering the lungs (visceral pleura). The fluid that accumulates between these layers is called pleural effusion.  \n\nThe findings of pleural effusion vary widely. They also vary depending on whether the radiograph is taken in the upright or supine position.  \nThe most common findings are as follows. **Elevation of the diaphragm on one side, flattening the diaphragm, or blunting the angle between rib and diaphragm (typically more than 30 degrees)**. "},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"idx_list = train_data[train_data['class_id']==10][0:3].index.values\nplot_example(idx_list)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 13. Pleural thickening\n[Back to top](#Table-of-Contents)\n\nThe pleura is the membrane that covers the lungs, and the change in the thickness of the pleura is called pleural thickening. It is often seen in the uppermost part of the lung field (the apex of the lung).  \n\n\"It typically involves the apex of the lung, which is called ‘pulmonary apical cap’. On chest X-rays, the apical cap is an irregular density located at the extreme apex and is less than 5 mm in width\".  \n*Saito, A. et al. Pleural thickening on screening chest X-rays: a single institutional study. Respir Res 20, 138 (2019). https://doi.org/10.1186/s12931-019-1116-9*"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"idx_list = train_data[train_data['class_id']==11][0:3].index.values\nplot_example(idx_list)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 14. Pneumothorax\n[Back to top](#Table-of-Contents)\n\nA pneumothorax is a condition in which air leaks from the lungs and accumulates in the chest cavity. When air leaks and accumulates in the chest, it cannot expand outward like a balloon due to the ribs' presence. Instead, the lungs are pushed by the air and become smaller. In other words, a pneumothorax is a situation where air leaks from the lungs and the lungs become smaller (collapsed).  \n\nIn a chest radiograph of a pneumothorax, the collapsed lung is whiter than normal, and the area where the lung is gone is uniformly black. Besides, the edges of the lung may appear linear.\n\n"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"idx_list = train_data[train_data['class_id']==12][1:4].index.values\nplot_example(idx_list)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 15. Plumonary fibrosis\n[Back to top](#Table-of-Contents)\n\nInflammation of the lung interstitium due to various causes, resulting in thickening and hardening of the walls, fibrosis, and scarring.  \n\nThe fibrotic areas lose their air content, which often results in dense cord shadows or granular shadows."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"idx_list = train_data[train_data['class_id']==13][3:6].index.values\nplot_example(idx_list)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Thank you for reading my Notebook\n(Please feel free to upvote.)"},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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}