{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sb\nimport pydicom\nimport os\nimport glob\nimport imageio\nfrom IPython.display import Image\n\nplt.rcParams[\"figure.figsize\"] = (10,5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_dir = '../input/osic-pulmonary-fibrosis-progression/'\nos.listdir(base_dir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_path = base_dir + 'train/'\ntest_path = base_dir + 'test/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(base_dir+'train.csv')\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.describe()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Sex"},{"metadata":{"trusted":true},"cell_type":"code","source":"sb.countplot(data = train_df, x=\"Sex\")\nplt.title(\"Sex distribution\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sb.boxplot(data=train_df, x = 'Sex', y = 'Age')\nplt.title('Sex distribution based on Age')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Age"},{"metadata":{"trusted":true},"cell_type":"code","source":"sb.countplot(train_df['Age'])\nplt.title('Age distribution')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sb.kdeplot(train_df.loc[train_df['Sex'] == 'Male', 'Age'], label = 'Male',shade=True)\nsb.kdeplot(train_df.loc[train_df['Sex'] == 'Female', 'Age'], label = 'Female',shade=True)\nplt.xlabel('Age (years)'); plt.ylabel('Density') \nplt.title('Distribution of Ages')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### SmokingStatus"},{"metadata":{"trusted":true},"cell_type":"code","source":"sb.countplot(data = train_df, x=\"SmokingStatus\")\nplt.title(\"Smoking status distribution\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sb.countplot(data = train_df, x=\"SmokingStatus\", hue='Sex')\nplt.title('Smoking Status distribution based on Sex')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### FVC: Forced Vital Count"},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\nsb.scatterplot(data = train_df, x=\"FVC\", y=\"Percent\", hue='Age')\nplt.title('FVC vs Percent')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sb.scatterplot(data = train_df, x=\"FVC\", y=\"Age\", hue='Sex')\nplt.title('FVC vs Age')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sb.scatterplot(data = train_df, x=\"FVC\", y=\"Weeks\", hue='SmokingStatus')\nplt.title('FVC vs Weeks')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Percent"},{"metadata":{"trusted":true},"cell_type":"code","source":"sb.distplot(train_df['Percent'])\nplt.title('Percent distribution')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sb.violinplot(data=train_df, x='Percent', y='SmokingStatus', hue = 'Sex')\nplt.title('Percent vs Smoking status')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Weeks"},{"metadata":{"trusted":true},"cell_type":"code","source":"sb.scatterplot(data = train_df, x=\"Weeks\", y=\"Age\", hue = \"Sex\")\nplt.title('Weeks vs Age')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"patient_id_1 = train_df.Patient[0]\npatient_1 = train_df[train_df.Patient == patient_id_1]\npatient_1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"patient_1.plot(x='Weeks',y='FVC')\nplt.title('Weeks vs FVC for 1 patient')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"patient_1.plot(x='Weeks',y='Percent')\nplt.title('Weeks vs Percent for 1 patient')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Correlation Matrix"},{"metadata":{"trusted":true},"cell_type":"code","source":"sb.heatmap(train_df.corr(), cmap = 'RdYlBu_r')\nplt.title('Correlation Matrix')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Analysis on Image"},{"metadata":{"trusted":true},"cell_type":"code","source":"patient_1_path = train_path + patient_id_1 +'/'\nimg_paths = [\n        f for f in glob.glob(\n            os.path.join(patient_1_path, '**')\n        )]\nimg_paths = sorted(img_paths, key=lambda i: int(os.path.splitext(os.path.basename(i))[0]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, axs = plt.subplots(5,6, figsize = (12,12))\naxs = axs.flatten()\nfor image_path,axis in zip(img_paths,axs):\n    img = pydicom.dcmread(image_path)\n    axis.imshow(img.pixel_array)\nfig.suptitle('Lung Images over Weeks', fontweight='bold')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"images = []\nfor image_path in img_paths:\n    images.append(pydicom.dcmread(image_path).pixel_array)\nimageio.mimsave(\"/tmp/gif.gif\", images, duration=0.0001)\nImage(filename=\"/tmp/gif.gif\", format='png')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n","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}