{"cells":[{"metadata":{"_uuid":"6d0bfc3283470d3a22a2925bd64b2d114234a7e8"},"cell_type":"markdown","source":"# Do you Know About what is Pneumonia?\n\nPneumonia is an infection that inflames the air sacs in one or both lungs. The air sacs may fill with fluid or pus (purulent material), causing cough with phlegm or pus, fever, chills, and difficulty breathing. A variety of organisms, including bacteria, viruses and fungi, can cause pneumonia.\n\nPneumonia can range in seriousness from mild to life-threatening. It is most serious for infants and young children, people older than age 65, and people with health problems or weakened immune systems.\n\n<img src=\"https://lungdiseasenews.com/wp-content/uploads/2015/11/shutterstock_318078170.jpg\" alt=\"Pneumonia\" height=\"500\" width=\"750\">\n\n\n## How Is Pneumonia Diagnosed?\n\nPhysical exam: Your doctor will listen to your lungs with a stethoscope. If you have pneumonia, your lungs may make crackling, bubbling, and rumbling sounds when you inhale. You also may be wheezing, and it may be hard to hear sounds of breathing in some areas of your chest.\n\n## What is Symptoms of Pneumonia?\n\n* Chest pain when you breathe or cough\n* Confusion or changes in mental awareness (in adults age 65 and older)\n* Cough, which may produce phlegm\n* Fatigue\n* Fever, sweating and shaking chills\n* Lower than normal body temperature (in adults older than age 65 and people with weak immune systems)\n* Nausea, vomiting or diarrhea\n* Shortness of breath\n\n# Let Dig out the Data and Find Solutions of  Pneumonia"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","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\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\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\nimport pydicom\n\nimport os\nprint(os.listdir(\"../input\"))\nfrom os import listdir\nfrom os.path import isfile, join\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7ee064355cc2fe8dc1467b89b2ffdf3da8f28854"},"cell_type":"code","source":"\n### plot packages\nimport plotly.offline as py\npy.init_notebook_mode(connected=True)\nfrom plotly.offline import init_notebook_mode, iplot\nimport plotly.figure_factory as ff\nimport matplotlib as plt\nimport plotly.graph_objs as go\nimport plotly.tools as tls\n%matplotlib inline\nimport cufflinks as cf\ncf.go_offline()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train_images_dir = '../input/stage_1_train_images/'\ntrain_images = [f for f in listdir(train_images_dir) if isfile(join(train_images_dir, f))]\ntest_images_dir = '../input/stage_1_test_images/'\ntest_images = [f for f in listdir(test_images_dir) if isfile(join(test_images_dir, f))]\nprint('5 Training images', train_images[:5]) # Print the first 5","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a181e9948e508b22c6255b0934e011fbcfdb7d12"},"cell_type":"code","source":"train_label = pd.read_csv(\"../input/stage_1_train_labels.csv\")\nclass_info = pd.read_csv(\"../input/stage_1_detailed_class_info.csv\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"53607b88996133ab9e98e63ac4436fc9e98ef1da"},"cell_type":"markdown","source":"## Road Map of Exploration\n* Class Count of stage_1_detailed\n* Target Variable\n* Preprocessing Function \n* All Case Images of No_Lung_Opacity / Normal /Lung_Opacity\n* Filter the all_class of No_Lung_Opacity / Normal /Lung_Opacity\n* Only Lung_Opacity Image Insights\n* Only Not_Normal_Opacity Image Insights\n* Only Normal_Opacity Image Insights"},{"metadata":{"trusted":true,"_uuid":"178377dbac8bdd086664b0b278c54accae99a304"},"cell_type":"code","source":"### plot packages\nimport plotly.offline as py\npy.init_notebook_mode(connected=True)\nfrom plotly.offline import init_notebook_mode, iplot\nimport plotly.figure_factory as ff\nimport matplotlib as plt\nimport plotly.graph_objs as go\nimport plotly.tools as tls\n%matplotlib inline\nimport cufflinks as cf\ncf.go_offline()\n\ntrace1 = go.Bar(\n            x=class_info['class'].value_counts().index,\n            y=class_info['class'].value_counts().values,\n        marker=dict(\n            color='rgba(222,45,38,0.8)',\n        )\n    )\n\ndata = [trace1]\nlayout = go.Layout(\n    title = 'Class Count of stage_1_detailed'\n)\n\nfig = go.Figure(data=data, layout=layout)\npy.iplot(fig, filename='grouped-bar')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"20789e4bd3c180a056d6fc6dc28900749dafe8c3"},"cell_type":"code","source":"trace1  = go.Bar(\n            x=train_label['Target'].value_counts().index,\n            y=train_label['Target'].value_counts().values,\n         marker=dict(\n                color='rgba(222,99,38,0.8)',\n            )\n    )\n\ndata = [trace1]\nlayout = go.Layout(\n    title = 'Target Variable'\n)\n\nfig = go.Figure(data=data, layout=layout)\npy.iplot(fig, filename='basic-bar')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ed4505026c60a5944edc12cff3171b8a71094bb1"},"cell_type":"markdown","source":"### Preprocessing Function \n"},{"metadata":{"trusted":true,"_uuid":"81c592b9c5c3570e83daea927887b3b3a0e265d9"},"cell_type":"code","source":"# Forked from `https://www.kaggle.com/peterchang77/exploratory-data-analysis`\ndef parse_data(df):\n    \n    # --- Define lambda to extract coords in list [y, x, height, width]\n    extract_box = lambda row: [row['y'], row['x'], row['height'], row['width']]\n\n    parsed = {}\n    for n, row in df.iterrows():\n        # --- Initialize patient entry into parsed \n        pid = row['patientId']\n        if pid not in parsed:\n            parsed[pid] = {\n                'dicom': '../input/stage_1_train_images/%s.dcm' % pid,\n                'label': row['Target'],\n                'boxes': []}\n\n        # --- Add box if opacity is present\n        if parsed[pid]['label'] == 1:\n            parsed[pid]['boxes'].append(extract_box(row))\n\n    return parsed\n\nparsed = parse_data(train_label)\n\ndef draw(data):\n    \"\"\"\n    Method to draw single patient with bounding box(es) if present \n\n    \"\"\"\n    # --- Open DICOM file\n    d = pydicom.read_file(data['dicom'])\n    im = d.pixel_array\n\n    # --- Convert from single-channel grayscale to 3-channel RGB\n    im = np.stack([im] * 3, axis=2)\n\n    # --- Add boxes with random color if present\n    for box in data['boxes']:\n        #rgb = np.floor(np.random.rand(3) * 256).astype('int')\n        rgb = [255, 251, 204] # Just use yellow\n        im = overlay_box(im=im, box=box, rgb=rgb, stroke=15)\n\n    plt.imshow(im, cmap=plt.cm.gist_gray)\n    plt.axis('off')\n\ndef overlay_box(im, box, rgb, stroke=2):\n    \"\"\"\n    Method to overlay single box on image\n\n    \"\"\"\n    # --- Convert coordinates to integers\n    box = [int(b) for b in box]\n    \n    # --- Extract coordinates\n    y1, x1, height, width = box\n    y2 = y1 + height\n    x2 = x1 + width\n\n    im[y1:y1 + stroke, x1:x2] = rgb\n    im[y2:y2 + stroke, x1:x2] = rgb\n    im[y1:y2, x1:x1 + stroke] = rgb\n    im[y1:y2, x2:x2 + stroke] = rgb\n\n    return im","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8151f62c7f3f06903bb513ffe98fa0206b48effe"},"cell_type":"markdown","source":"### All Case Images of No_Lung_Opacity / Normal /Lung_Opacity"},{"metadata":{"trusted":true,"_uuid":"abb311c3926d54b1a248e4db5bd6fe6b89a539a6"},"cell_type":"code","source":"import matplotlib.pylab as plt\nplt.style.use('default')\nfig=plt.figure(figsize=(25, 25))\ncolumns = 3; rows = 3\nfor i in range(1, columns*rows +1):\n    fig.add_subplot(rows, columns, i)\n    draw(parsed[train_label['patientId'].unique()[i]])\n    fig.add_subplot","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"99cd5aa2f78051a49ea85cb05ee7a772e0ab9625"},"cell_type":"markdown","source":"### Filter the all_class of No_Lung_Opacity / Normal /Lung_Opacity"},{"metadata":{"trusted":true,"_uuid":"c6c091d5ec97b100abb58c1954a4611da0563313"},"cell_type":"code","source":"opacity = class_info \\\n    .loc[class_info['class'] == 'Lung Opacity'] \\\n    .reset_index()\nnot_normal = class_info \\\n    .loc[class_info['class'] == 'No Lung Opacity / Not Normal'] \\\n    .reset_index()\nnormal = class_info \\\n    .loc[class_info['class'] == 'Normal'] \\\n    .reset_index()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3057e3c478b76ea408e8063684d61e92745fa8d9"},"cell_type":"markdown","source":"### Only Lung_Opacity Image Insights\n\n#### Community-acquired pneumonia is the most common type of pneumonia. It occurs outside of hospitals or other health care facilities. It may be caused by:\n\n* Bacteria - The most common cause of bacterial pneumonia in the U.S. is Streptococcus pneumoniae. This type of pneumonia can occur on its own or after you've had a cold or the flu. It may affect one part (lobe) of the lung, a condition called lobar pneumonia.\n* Bacteria like organisms -  Mycoplasma pneumoniae also can cause pneumonia. It typically produces milder symptoms than do other types of pneumonia. Walking pneumonia is an informal name given to this type of pneumonia, which typically isn't severe enough to require bed rest.\n* Fungi - This type of pneumonia is most common in people with chronic health problems or weakened immune systems, and in people who have inhaled large doses of the organisms. The fungi that cause it can be found in soil or bird droppings and vary depending upon geographic location.\n* Viruses -  Some of the viruses that cause colds and the flu can cause pneumonia. Viruses are the most common cause of pneumonia in children younger than 5 years. Viral pneumonia is usually mild. But in some cases it can become very serious.\n"},{"metadata":{"trusted":true,"_uuid":"47fb7cfa48bd318f895122af191fda1be5feea83"},"cell_type":"code","source":"plt.style.use('default')\nfig=plt.figure(figsize=(25, 25))\ncolumns = 3; rows = 3\nfor i in range(1, columns*rows +1):\n    fig.add_subplot(rows, columns, i)\n    draw(parsed[opacity['patientId'].unique()[i]])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"32251311f9487aa3742b44f5f45e7721532e91d6"},"cell_type":"markdown","source":"### Only Not_Normal_Opacity Image Insights\n\n#### Normal factors include:\n\n* Being hospitalized. You're at greater risk of pneumonia if you're in a hospital intensive care unit, especially if you're on a machine that helps you breathe (a ventilator).\n* Chronic disease. You're more likely to get pneumonia if you have asthma, chronic obstructive pulmonary disease (COPD) or heart disease.\n* Smoking. Smoking damages your body's natural defenses against the bacteria and viruses that cause pneumonia.\n* Weakened or suppressed immune system. People who have HIV/AIDS, who've had an organ transplant, or who receive chemotherapy or long-term steroids are at risk.\n"},{"metadata":{"trusted":true,"_uuid":"3eb828ab1018919ca2bb3b75979f8b60e8d7fd2d"},"cell_type":"code","source":"plt.style.use('default')\nfig=plt.figure(figsize=(25, 25))\ncolumns = 3; rows = 3\nfor i in range(1, columns*rows +1):\n    fig.add_subplot(rows, columns, i)\n    draw(parsed[not_normal['patientId'].loc[i]])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"969c2acaa89b46686156f48fea421f5979fbdc33"},"cell_type":"markdown","source":"### Only Normal_Opacity Image Insights"},{"metadata":{"trusted":true,"_uuid":"46c06e8f93eaaa686b89aa80a57c75df26451f93"},"cell_type":"code","source":"plt.style.use('default')\nfig=plt.figure(figsize=(25, 25))\ncolumns = 3; rows = 3\nfor i in range(1, columns*rows +1):\n    fig.add_subplot(rows, columns, i)\n    draw(parsed[normal['patientId'].loc[i]])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"182c0a81e007c4cbea33a07c82c39eff63d9f77a"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}