{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":13451,"databundleVersionId":1188070,"sourceType":"competition"}],"dockerImageVersionId":30177,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport os\nfrom PIL import Image\nimport pydicom \nimport cv2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-03T10:43:08.205433Z","iopub.execute_input":"2022-11-03T10:43:08.205789Z","iopub.status.idle":"2022-11-03T10:43:08.606389Z","shell.execute_reply.started":"2022-11-03T10:43:08.205697Z","shell.execute_reply":"2022-11-03T10:43:08.605654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Input\n\ndir_csv = '../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection'\ndir_train_img = '../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train'","metadata":{"execution":{"iopub.status.busy":"2022-11-03T10:43:08.608115Z","iopub.execute_input":"2022-11-03T10:43:08.608382Z","iopub.status.idle":"2022-11-03T10:43:08.612364Z","shell.execute_reply.started":"2022-11-03T10:43:08.608347Z","shell.execute_reply":"2022-11-03T10:43:08.611506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(os.path.join(dir_csv, 'stage_2_train.csv'))","metadata":{"execution":{"iopub.status.busy":"2022-11-03T10:43:08.614292Z","iopub.execute_input":"2022-11-03T10:43:08.614912Z","iopub.status.idle":"2022-11-03T10:43:12.854861Z","shell.execute_reply.started":"2022-11-03T10:43:08.614876Z","shell.execute_reply":"2022-11-03T10:43:12.85411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-03T10:43:12.856721Z","iopub.execute_input":"2022-11-03T10:43:12.857958Z","iopub.status.idle":"2022-11-03T10:43:12.875519Z","shell.execute_reply.started":"2022-11-03T10:43:12.857922Z","shell.execute_reply":"2022-11-03T10:43:12.874762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[['ID', 'Image', 'Diagnosis']] = train['ID'].str.split('_', expand=True)\ntrain = train[['Image', 'Diagnosis', 'Label']]\ntrain.drop_duplicates(inplace=True)\ntrain = train.pivot(index='Image', columns='Diagnosis', values='Label').reset_index()\ntrain['Image'] = 'ID_' + train['Image']\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-03T10:43:12.876756Z","iopub.execute_input":"2022-11-03T10:43:12.877015Z","iopub.status.idle":"2022-11-03T10:43:33.379102Z","shell.execute_reply.started":"2022-11-03T10:43:12.87698Z","shell.execute_reply":"2022-11-03T10:43:33.378364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dcm_files = os.listdir(dir_train_img)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T10:43:33.380247Z","iopub.execute_input":"2022-11-03T10:43:33.380691Z","iopub.status.idle":"2022-11-03T10:44:09.613318Z","shell.execute_reply.started":"2022-11-03T10:43:33.380656Z","shell.execute_reply":"2022-11-03T10:44:09.612566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dcm_files[:10]","metadata":{"execution":{"iopub.status.busy":"2022-11-03T10:44:09.614749Z","iopub.execute_input":"2022-11-03T10:44:09.615258Z","iopub.status.idle":"2022-11-03T10:44:09.621921Z","shell.execute_reply.started":"2022-11-03T10:44:09.61522Z","shell.execute_reply":"2022-11-03T10:44:09.621042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# image = Image.open(os.path.join(dir_train_img, 'ID_000039fa0.dcm'))\n# plt.show(img)\ndcm = pydicom.dcmread(os.path.join(dir_train_img, 'ID_000039fa0.dcm'))\nplt.imshow(dcm.pixel_array, cmap=plt.cm.bone)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T10:44:09.623399Z","iopub.execute_input":"2022-11-03T10:44:09.624113Z","iopub.status.idle":"2022-11-03T10:44:09.931107Z","shell.execute_reply.started":"2022-11-03T10:44:09.624069Z","shell.execute_reply":"2022-11-03T10:44:09.930394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train = train[:10000]\nnew_train.to_csv('train.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T10:44:09.932163Z","iopub.execute_input":"2022-11-03T10:44:09.933038Z","iopub.status.idle":"2022-11-03T10:44:09.956376Z","shell.execute_reply.started":"2022-11-03T10:44:09.93299Z","shell.execute_reply":"2022-11-03T10:44:09.955752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('./train_files')","metadata":{"execution":{"iopub.status.busy":"2022-11-03T10:44:09.958732Z","iopub.execute_input":"2022-11-03T10:44:09.958978Z","iopub.status.idle":"2022-11-03T10:44:09.963299Z","shell.execute_reply.started":"2022-11-03T10:44:09.958943Z","shell.execute_reply":"2022-11-03T10:44:09.962401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img in new_train.Image.to_list():\n    dcm = pydicom.dcmread(os.path.join(dir_train_img, f'{img}.dcm'))\n    dcm.save_as(os.path.join('./train_files', f'{img}.dcm'), write_like_original=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T10:44:09.964838Z","iopub.execute_input":"2022-11-03T10:44:09.965086Z","iopub.status.idle":"2022-11-03T10:47:00.496551Z","shell.execute_reply.started":"2022-11-03T10:44:09.965053Z","shell.execute_reply":"2022-11-03T10:47:00.495751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_files = os.listdir('./train_files')","metadata":{"execution":{"iopub.status.busy":"2022-11-03T10:47:00.49777Z","iopub.execute_input":"2022-11-03T10:47:00.498048Z","iopub.status.idle":"2022-11-03T10:47:00.508547Z","shell.execute_reply.started":"2022-11-03T10:47:00.498011Z","shell.execute_reply":"2022-11-03T10:47:00.507855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(out_files)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T10:47:00.510089Z","iopub.execute_input":"2022-11-03T10:47:00.510534Z","iopub.status.idle":"2022-11-03T10:47:00.518464Z","shell.execute_reply.started":"2022-11-03T10:47:00.510498Z","shell.execute_reply":"2022-11-03T10:47:00.517727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}