{"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 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-04-07T15:08:39.208867Z","iopub.execute_input":"2022-04-07T15:08:39.209435Z","iopub.status.idle":"2022-04-07T15:08:39.628102Z","shell.execute_reply.started":"2022-04-07T15:08:39.209339Z","shell.execute_reply":"2022-04-07T15:08:39.627356Z"},"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-04-07T15:14:21.012363Z","iopub.execute_input":"2022-04-07T15:14:21.013065Z","iopub.status.idle":"2022-04-07T15:14:21.018789Z","shell.execute_reply.started":"2022-04-07T15:14:21.013028Z","shell.execute_reply":"2022-04-07T15:14:21.018121Z"},"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-04-07T15:09:00.894867Z","iopub.execute_input":"2022-04-07T15:09:00.895454Z","iopub.status.idle":"2022-04-07T15:09:04.882715Z","shell.execute_reply.started":"2022-04-07T15:09:00.8954Z","shell.execute_reply":"2022-04-07T15:09:04.881956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-06T09:05:52.780214Z","iopub.execute_input":"2022-04-06T09:05:52.780505Z","iopub.status.idle":"2022-04-06T09:05:52.800842Z","shell.execute_reply.started":"2022-04-06T09:05:52.780473Z","shell.execute_reply":"2022-04-06T09:05:52.800188Z"},"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-04-07T15:09:08.711583Z","iopub.execute_input":"2022-04-07T15:09:08.712084Z","iopub.status.idle":"2022-04-07T15:09:30.242827Z","shell.execute_reply.started":"2022-04-07T15:09:08.712046Z","shell.execute_reply":"2022-04-07T15:09:30.242133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dcm_files = os.listdir(dir_train_img)","metadata":{"execution":{"iopub.status.busy":"2022-04-06T09:06:53.866343Z","iopub.execute_input":"2022-04-06T09:06:53.866603Z","iopub.status.idle":"2022-04-06T09:06:54.831725Z","shell.execute_reply.started":"2022-04-06T09:06:53.866574Z","shell.execute_reply":"2022-04-06T09:06:54.830951Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dcm_files[:10]","metadata":{"execution":{"iopub.status.busy":"2022-04-06T09:07:00.838103Z","iopub.execute_input":"2022-04-06T09:07:00.838868Z","iopub.status.idle":"2022-04-06T09:07:00.84709Z","shell.execute_reply.started":"2022-04-06T09:07:00.83882Z","shell.execute_reply":"2022-04-06T09:07:00.846147Z"},"_kg_hide-input":true,"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)\nimage.show()\n# dcm = pydicom.dcmread(os.path.join(dir_train_img, 'ID_000039fa0.dcm'))\n# plt.imshow(dcm.pixel_array, cmap=plt.cm.bone)","metadata":{"execution":{"iopub.status.busy":"2022-04-07T15:16:27.651383Z","iopub.execute_input":"2022-04-07T15:16:27.652151Z","iopub.status.idle":"2022-04-07T15:16:27.672106Z","shell.execute_reply.started":"2022-04-07T15:16:27.652111Z","shell.execute_reply":"2022-04-07T15:16:27.671343Z"},"_kg_hide-input":true,"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-04-07T15:10:14.268876Z","iopub.execute_input":"2022-04-07T15:10:14.269464Z","iopub.status.idle":"2022-04-07T15:10:14.304314Z","shell.execute_reply.started":"2022-04-07T15:10:14.269406Z","shell.execute_reply":"2022-04-07T15:10:14.30362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('./train_files')","metadata":{"execution":{"iopub.status.busy":"2022-04-07T15:22:20.417138Z","iopub.execute_input":"2022-04-07T15:22:20.417388Z","iopub.status.idle":"2022-04-07T15:22:20.42185Z","shell.execute_reply.started":"2022-04-07T15:22:20.41736Z","shell.execute_reply":"2022-04-07T15:22:20.420483Z"},"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)\n    img = Image.open(os.path.join(dir_train_img, f'{img}.png'))\n    img.save(os.path.join('./train_files', f'{img}.png')) #matched image is saved to '/kaggle/working/train_files'","metadata":{"execution":{"iopub.status.busy":"2022-04-06T15:21:12.12091Z","iopub.execute_input":"2022-04-06T15:21:12.121494Z","iopub.status.idle":"2022-04-06T15:24:19.249804Z","shell.execute_reply.started":"2022-04-06T15:21:12.121445Z","shell.execute_reply":"2022-04-06T15:24:19.249094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_files = os.listdir('./train_files')","metadata":{"execution":{"iopub.status.busy":"2022-04-06T15:26:41.349145Z","iopub.execute_input":"2022-04-06T15:26:41.349455Z","iopub.status.idle":"2022-04-06T15:26:41.360584Z","shell.execute_reply.started":"2022-04-06T15:26:41.349417Z","shell.execute_reply":"2022-04-06T15:26:41.359693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(out_files)","metadata":{"execution":{"iopub.status.busy":"2022-04-06T15:28:15.447337Z","iopub.execute_input":"2022-04-06T15:28:15.447986Z","iopub.status.idle":"2022-04-06T15:28:15.452394Z","shell.execute_reply.started":"2022-04-06T15:28:15.447947Z","shell.execute_reply":"2022-04-06T15:28:15.451746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}