{"cells":[{"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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Enabling the autocomplete\n%config Completer.use_jedi = False","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# [About The Competition](https://www.kaggle.com/c/rsna-pneumonia-detection-challenge/overview)\n"},{"metadata":{},"cell_type":"markdown","source":"# The Data"},{"metadata":{"trusted":true},"cell_type":"code","source":"detailed_class_info_path = \"../input/rsna-pneumonia-detection-challenge/stage_2_detailed_class_info.csv\"\ntrain_labels_path =  \"../input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv\"\nsample_submission_path = \"../input/rsna-pneumonia-detection-challenge/stage_2_sample_submission.csv\"\n\ntrain_images_path = '../input/rsna-pneumonia-detection-challenge/stage_2_train_images'\ntest_images_path  = '../input/rsna-pneumonia-detection-challenge/stage_2_test_images'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_info_df = pd.read_csv(detailed_class_info_path)\nprint(class_info_df.shape)\nprint(class_info_df.head())\nprint(class_info_df.tail())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(train_labels_path)\nprint(df.shape)\nprint(df.head())\ndf.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df = pd.read_csv(sample_submission_path)\nsubmission_df.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"How we can see the labels follow this pattern:\n- 0 = Normal (Healthy) \n- 1 = Not Normal      "},{"metadata":{"trusted":true},"cell_type":"code","source":"# df['Target'].hist(bins=2)\nclass_info_df.groupby('class').size().plot.bar()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Spliting the dataset**"},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_df, test_df = train_test_split(df, test_size=0.25)\n\ntrain_df = train_df.reset_index()\ntest_df  = test_df.reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport pydicom\nimport cv2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"file = f\"{train_images_path}/{train_df['patientId'][3]}.dcm\"\ndcom = pydicom.dcmread(file)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dcom","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = dcom.pixel_array\n\n# cv2.rectangle(imgcv,(_x1,_y1),(_x2,_y2),(0,255,0),cv2.FILLED)\n# cv2.putText(imgcv,label,(x1,y1),cv2.FONT_HERSHEY_COMPLEX,0.5,(0,0,0),1)\n\nx,y,w,h = train_df[['x','y','width', 'height']].iloc[3,:]\nx,y,w,h = int(x), int(y), int(w), int(h)\n# img = cv2.rectangle(img,(x,y),(x+w,y+h), 0, 1)\n\n\nplt.imshow(img, cmap='bone')\nplt.axis('off')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def dcoms_to_images(dcoms_path, target_size:([int,int]), directory='.'):\n    imgs = []\n    \n    for dcom_path in dcoms_path:\n        file = f\"{directory}/{dcom_path}.dcm\"\n        img  = pydicom.dcmread(file).pixel_array.astype('uint8')\n        \n        img = cv2.resize(img, target_size, interpolation=cv2.INTER_NEAREST)\n        img = np.expand_dims(img,-1)\n        \n        imgs.append(img)\n    return imgs","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"imgs = dcoms_to_images(train_df['patientId'], (512,512), train_images_path)","execution_count":null,"outputs":[]},{"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}