{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# load Data and liberaies\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport pydicom\nimport seaborn as sns\nimport sklearn\nimport os,glob\nbase = \"../input/osic-pulmonary-fibrosis-progression/\"\nprint(os.listdir(base))\ntrain = pd.read_csv(base + \"train.csv\")\ntest  = pd.read_csv(base + \"test.csv\")\nsub = pd.read_csv(base + \"sample_submission.csv\")\nprint(\"train shape: \" , train.shape , \"test shape: \",test.shape,\"submision shape: \",sub.shape)\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# train data Describtion","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.DataFrame(columns=[\"Patient\",\"Weeks\",\"FVC\",\"Percent\",\"Age\",\"Sex\",\"SmokingStatus\"])\nc=0\nfor p,d in train.groupby([\"Patient\"]):\n    df.loc[c,[\"Patient\",\"Age\",\"Sex\",\"SmokingStatus\"]] = d[[\"Patient\",\"Age\",\"Sex\",\"SmokingStatus\"]].drop_duplicates().values\n    df.loc[c,\"Weeks\"] = d['Weeks'].values\n    df.loc[c,\"FVC\"] = d['FVC'].values\n    df.loc[c,\"Percent\"] = d['Percent'].values\n    c+=1\nprint(df.shape)\ndf[\"Age\"]=df['Age'].astype(\"int\")\ndf.head()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(df[\"Age\"].describe())\ndf['Age'].hist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.catplot(x='Sex',kind=\"count\",data=df,hue=\"SmokingStatus\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.catplot(x='Sex',y=\"Age\",data=df,hue=\"SmokingStatus\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig,ax = plt.subplots(nrows=6,ncols=5,figsize=(20,30))\ni,j=0,0\nfor file in glob.glob(base+\"train/\"+\"ID00007637202177411956430/\"+\"*.dcm\"):\n    ax[j][i].imshow(pydicom.dcmread(file).pixel_array, cmap=plt.cm.bone)\n    ax[j][i].set_title(file.split(\"/\")[-1])\n    i+=1\n    if i==5:\n        i=0\n        j+=1\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Forced Vital capacity (FVC) test","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from IPython.display import IFrame, YouTubeVideo\nYouTubeVideo('K7bFxiHCwxM',width=600, height=400)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# domain understanding","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from IPython.display import IFrame, YouTubeVideo\nYouTubeVideo('YGAO7ted0UU',width=600, height=400)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# The aim of this competition is to predict a patient’s severity of decline in lung function based on a CT scan of their lungs","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"files = glob.glob(base+\"train/\"+\"ID00007637202177411956430/\"+\"*.dcm\")\nprint(files[0])\nimage = pydicom.dcmread(files[0])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Multiple .dcm files represent different slices of a single CT scan which is acquired at Week 0. CT scans produce 3D volumes for each scan, those volumes consist of 2D slices and each slice is a .dcm file. Every directory has different number of slices in osic-pulmonary-fibrosis-progression/train. Those number of slices are between 12 and 1018 with a median of 98. Total number of slices adds up to 33026 for 176 patients","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# reference https://www.kaggle.com/gunesevitan/osic-pulmonary-fibrosis-progression-eda#5.-DICOM-Files\ndef load_scan(patient_name):\n    \n    patient_directory = [pydicom.dcmread(f'../input/osic-pulmonary-fibrosis-progression/train/{patient_name}/{s}') for s in os.listdir(f'../input/osic-pulmonary-fibrosis-progression/train/{patient_name}')]\n    patient_directory.sort(key=lambda s: float(s.ImagePositionPatient[2]))\n    patient_slices = np.zeros((len(patient_directory), patient_directory[0].Rows, patient_directory[0].Columns))\n\n    for i, s in enumerate(patient_directory):\n        patient_slices[i] = s.pixel_array\n            \n    return patient_slices\n\npatient = 'ID00228637202259965313869'\npatient_slices = load_scan(patient)\nprint(f'Patient {patient} CT scan is loaded - Volume Shape: {patient_slices.shape}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.animation as animation\nfrom IPython.display import HTML\n\nfig = plt.figure(figsize=(7, 7))\n\nims = []\nfor i in patient_slices:\n    im = plt.imshow(i, animated=True, cmap=plt.cm.bone)\n    plt.axis('off')\n    ims.append([im])\n\nani = animation.ArtistAnimation(fig, ims, interval=25, blit=False, repeat_delay=1000)\nHTML(ani.to_html5_video())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(test)\nnew = pd.DataFrame([i for i in sub['Patient_Week'].str.split(\"_\")],columns=[\"Patient\",\"Week\"])\nfor g in new.groupby(\"Patient\"):\n    print(g[1][\"Week\"].unique())\n    print(len(g[1][\"Week\"].unique()))\n    break","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for g in train.groupby(\"Patient\"):\n    if g[0]==\"ID00007637202177411956430\":\n        print(g[1][\"Weeks\"].unique())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"# coming soon next","execution_count":null},{"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}