{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport random as rn\nimport tensorflow as tf\nimport cv2 as cv\nimport glob\nimport pydicom as dicom\n\nfrom sklearn import preprocessing\nfrom sklearn.model_selection import train_test_split \nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D\nfrom tensorflow.keras.layers import MaxPooling2D,AveragePooling2D\nfrom tensorflow.keras.layers import Flatten\nfrom tensorflow.keras.layers import Dense, Dropout,Activation\nfrom tensorflow.keras.optimizers import Adam, SGD\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-04T05:39:40.413500Z","iopub.execute_input":"2022-09-04T05:39:40.413891Z","iopub.status.idle":"2022-09-04T05:39:40.424855Z","shell.execute_reply.started":"2022-09-04T05:39:40.413859Z","shell.execute_reply":"2022-09-04T05:39:40.423166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(42)\nrn.seed(42)\ntf.random.set_seed(42)\nRANDOM_STATE=100\n\nTEST_IMAGES_PATH='../input/rsna-pneumonia-detection-challenge/stage_2_test_images'\nTRAIN_IMAGES_PATH='../input/rsna-pneumonia-detection-challenge/stage_2_train_images'\nTRAIN_LABELS_PATH='../input/pneumoniadetection/stage_2_train_labels.csv'\nDETAILED_CLASS_INFO_PATH='../input/pneumoniadetection/stage_2_detailed_class_info.csv'","metadata":{"execution":{"iopub.status.busy":"2022-09-04T05:39:41.227518Z","iopub.execute_input":"2022-09-04T05:39:41.228377Z","iopub.status.idle":"2022-09-04T05:39:41.238142Z","shell.execute_reply.started":"2022-09-04T05:39:41.228141Z","shell.execute_reply":"2022-09-04T05:39:41.235485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = pd.read_csv(TRAIN_LABELS_PATH)\ndetailed_class = pd.read_csv(DETAILED_CLASS_INFO_PATH)\n# df1 = pd.read_csv('../input/pneumoniadetection/stage_2_sample_submission.csv')\n# train_images_folder = pd.read_csv(TRAIN_IMAGES_PATH)","metadata":{"execution":{"iopub.status.busy":"2022-09-04T05:39:41.938641Z","iopub.execute_input":"2022-09-04T05:39:41.940687Z","iopub.status.idle":"2022-09-04T05:39:42.027791Z","shell.execute_reply.started":"2022-09-04T05:39:41.940602Z","shell.execute_reply":"2022-09-04T05:39:42.025853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_labels.shape, 'is Train labels shape')\nprint(detailed_class.shape, 'is Detailed class shape')","metadata":{"execution":{"iopub.status.busy":"2022-09-04T05:39:42.587978Z","iopub.execute_input":"2022-09-04T05:39:42.588394Z","iopub.status.idle":"2022-09-04T05:39:42.594081Z","shell.execute_reply.started":"2022-09-04T05:39:42.588365Z","shell.execute_reply":"2022-09-04T05:39:42.593326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels['patientId'].value_counts()\n\n# 3239951b-6211-4290-b237-3d9ad17176db     \n# 31764d54-ea3b-434f-bae2-8c579ed13799    \n# 1c44e0a4-4612-438f-9a83-8d5bf919cb67   \n# 8dc8e54b-5b05-4dac-80b9-fa48878621e2 \n# 349f10b4-dc3e-4f3f-b2e4-a5b81448ce87\n\n# train_labels.loc[train_labels['patientId'] == '349f10b4-dc3e-4f3f-b2e4-a5b81448ce87']\n\nprint('There are duplicates. Lets remove duplicates')\n\ntrain_labels.drop_duplicates(subset=['patientId'], keep='last', ignore_index=True, inplace=True) \ndetailed_class.drop_duplicates(subset=['patientId'], keep='last', ignore_index=True, inplace=True) ","metadata":{"execution":{"iopub.status.busy":"2022-09-04T05:39:43.440156Z","iopub.execute_input":"2022-09-04T05:39:43.440592Z","iopub.status.idle":"2022-09-04T05:39:43.495522Z","shell.execute_reply.started":"2022-09-04T05:39:43.440563Z","shell.execute_reply":"2022-09-04T05:39:43.494032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_labels.shape, 'is Train labels new shape')\n","metadata":{"execution":{"iopub.status.busy":"2022-09-04T05:39:44.113723Z","iopub.execute_input":"2022-09-04T05:39:44.114124Z","iopub.status.idle":"2022-09-04T05:39:44.123447Z","shell.execute_reply.started":"2022-09-04T05:39:44.114084Z","shell.execute_reply":"2022-09-04T05:39:44.120423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def map_labels_and_images(train_labels_dv, train_images_path_input):\n    train_labels_dv['image_path']= train_images_path_input +'/'+ train_labels_dv['patientId']+'.dcm'\n    return train_labels_dv;","metadata":{"execution":{"iopub.status.busy":"2022-09-04T05:39:44.727660Z","iopub.execute_input":"2022-09-04T05:39:44.728110Z","iopub.status.idle":"2022-09-04T05:39:44.736327Z","shell.execute_reply.started":"2022-09-04T05:39:44.728078Z","shell.execute_reply":"2022-09-04T05:39:44.734843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def map_details_and_images(train_details_dv, train_images_path_input):\n    train_details_dv['image_path']= train_images_path_input +'/'+ train_details_dv['patientId']+'.dcm'\n    return train_details_dv;","metadata":{"execution":{"iopub.status.busy":"2022-09-04T05:39:45.346467Z","iopub.execute_input":"2022-09-04T05:39:45.347240Z","iopub.status.idle":"2022-09-04T05:39:45.353323Z","shell.execute_reply.started":"2022-09-04T05:39:45.347205Z","shell.execute_reply":"2022-09-04T05:39:45.352332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"detailed_class = map_details_and_images(detailed_class, TRAIN_IMAGES_PATH)","metadata":{"execution":{"iopub.status.busy":"2022-09-04T05:39:45.880980Z","iopub.execute_input":"2022-09-04T05:39:45.881784Z","iopub.status.idle":"2022-09-04T05:39:45.899605Z","shell.execute_reply.started":"2022-09-04T05:39:45.881725Z","shell.execute_reply":"2022-09-04T05:39:45.898145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = map_labels_and_images(train_labels, TRAIN_IMAGES_PATH)","metadata":{"execution":{"iopub.status.busy":"2022-09-04T05:39:46.385451Z","iopub.execute_input":"2022-09-04T05:39:46.386006Z","iopub.status.idle":"2022-09-04T05:39:46.406253Z","shell.execute_reply.started":"2022-09-04T05:39:46.385976Z","shell.execute_reply":"2022-09-04T05:39:46.402567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**preproccesing and visualization**","metadata":{}},{"cell_type":"code","source":"def map_images(_train_labels_df, path):\n    train_labels_df = _train_labels_df.copy()\n#     path = glob.glob(path)\n    labels = []\n    names = []\n    paths = []\n    heights=[]\n    widths=[]\n    \n    for i in range(0, len(train_labels_df)):\n        patientId = train_labels_df.iloc[i]['patientId']\n        file_path = path+'/'+patientId+'.dcm'\n        if ( i ==0 ):\n            print(path)\n            print(patientId)\n            print(path+'/'+patientId+'.dcm')\n        d = dicom.dcmread(file_path)\n        train_labels_df['h'] = d.Rows\n        train_labels_df['w'] = d.Columns\n#         train_labels_df[c] = c\n#         n = cv.imread(path+'/'+patientId+'.dcm')\n#         h, w, c = n.shape\n#         train_labels_df[h] = h\n#         train_labels_df[w] = w\n#         train_labels_df[c] = c\n   \n    return train_labels_df","metadata":{"execution":{"iopub.status.busy":"2022-09-04T05:39:47.403466Z","iopub.execute_input":"2022-09-04T05:39:47.403840Z","iopub.status.idle":"2022-09-04T05:39:47.416889Z","shell.execute_reply.started":"2022-09-04T05:39:47.403812Z","shell.execute_reply":"2022-09-04T05:39:47.414662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tt = map_images(train_labels, TRAIN_IMAGES_PATH)","metadata":{"execution":{"iopub.status.busy":"2022-09-04T05:39:48.069917Z","iopub.execute_input":"2022-09-04T05:39:48.070258Z","iopub.status.idle":"2022-09-04T05:44:32.604494Z","shell.execute_reply.started":"2022-09-04T05:39:48.070231Z","shell.execute_reply":"2022-09-04T05:44:32.602818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tt.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-09-04T05:44:32.607555Z","iopub.execute_input":"2022-09-04T05:44:32.607924Z","iopub.status.idle":"2022-09-04T05:44:32.640135Z","shell.execute_reply.started":"2022-09-04T05:44:32.607895Z","shell.execute_reply":"2022-09-04T05:44:32.638835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tt.describe()","metadata":{"execution":{"iopub.status.busy":"2022-09-04T05:49:31.953392Z","iopub.execute_input":"2022-09-04T05:49:31.953784Z","iopub.status.idle":"2022-09-04T05:49:32.009068Z","shell.execute_reply.started":"2022-09-04T05:49:31.953756Z","shell.execute_reply":"2022-09-04T05:49:32.007628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ddd = dicom.dcmread('../input/rsna-pneumonia-detection-challenge/stage_2_train_images/0004cfab-14fd-4e49-80ba-63a80b6bddd6.dcm')","metadata":{"execution":{"iopub.status.busy":"2022-09-04T06:58:10.665740Z","iopub.execute_input":"2022-09-04T06:58:10.666096Z","iopub.status.idle":"2022-09-04T06:58:10.677240Z","shell.execute_reply.started":"2022-09-04T06:58:10.666069Z","shell.execute_reply":"2022-09-04T06:58:10.676202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ddd","metadata":{"execution":{"iopub.status.busy":"2022-09-04T06:58:13.186349Z","iopub.execute_input":"2022-09-04T06:58:13.186907Z","iopub.status.idle":"2022-09-04T06:58:13.194005Z","shell.execute_reply.started":"2022-09-04T06:58:13.186881Z","shell.execute_reply":"2022-09-04T06:58:13.192922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ddd['Patient\\'s Sex']","metadata":{"execution":{"iopub.status.busy":"2022-09-04T06:58:49.446445Z","iopub.execute_input":"2022-09-04T06:58:49.446778Z","iopub.status.idle":"2022-09-04T06:58:49.476412Z","shell.execute_reply.started":"2022-09-04T06:58:49.446754Z","shell.execute_reply":"2022-09-04T06:58:49.474781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}