{"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":"!conda install gdcm -c conda-forge -y","metadata":{"execution":{"iopub.status.busy":"2021-06-29T06:39:32.425235Z","iopub.execute_input":"2021-06-29T06:39:32.425669Z","iopub.status.idle":"2021-06-29T06:40:41.842285Z","shell.execute_reply.started":"2021-06-29T06:39:32.425586Z","shell.execute_reply":"2021-06-29T06:40:41.841330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nfrom PIL import Image\nimport pandas as pd\nfrom tqdm.auto import tqdm","metadata":{"execution":{"iopub.status.busy":"2021-06-29T06:41:01.467041Z","iopub.execute_input":"2021-06-29T06:41:01.467402Z","iopub.status.idle":"2021-06-29T06:41:01.477701Z","shell.execute_reply.started":"2021-06-29T06:41:01.467367Z","shell.execute_reply":"2021-06-29T06:41:01.476690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\n\ndef read_xray(path,voi_lut=True,fix_monochrome=True):\n    dicom=pydicom.read_file(path)\n    \n    if voi_lut:\n        data=apply_voi_lut(dicom.pixel_array,dicom)\n    \n    else:\n        data=dicom.pixel_array\n        \n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n    \n    data=data-np.min(data)\n    data=data/np.max(data)\n    data=(data*255).astype(np.uint8)\n    \n    return data","metadata":{"execution":{"iopub.status.busy":"2021-06-29T06:41:03.486103Z","iopub.execute_input":"2021-06-29T06:41:03.486593Z","iopub.status.idle":"2021-06-29T06:41:03.803707Z","shell.execute_reply.started":"2021-06-29T06:41:03.486548Z","shell.execute_reply":"2021-06-29T06:41:03.802536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize(array, size, keep_ratio=False, resample=Image.LANCZOS):\n    \n    im = Image.fromarray(array)\n    \n    if keep_ratio:\n        im.thumbnail((size, size), resample)\n    else:\n        im = im.resize((size, size), resample)\n    \n    return im","metadata":{"execution":{"iopub.status.busy":"2021-06-29T06:41:05.083705Z","iopub.execute_input":"2021-06-29T06:41:05.084381Z","iopub.status.idle":"2021-06-29T06:41:05.089323Z","shell.execute_reply.started":"2021-06-29T06:41:05.084345Z","shell.execute_reply":"2021-06-29T06:41:05.088596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('../input/siim-covid19-detection/train_image_level.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-29T06:41:06.066563Z","iopub.execute_input":"2021-06-29T06:41:06.067175Z","iopub.status.idle":"2021-06-29T06:41:06.120317Z","shell.execute_reply.started":"2021-06-29T06:41:06.067142Z","shell.execute_reply":"2021-06-29T06:41:06.119424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '../input/siim-covid19-detection/train/ae3e63d94c13/288554eb6182/e00f9fe0cce5.dcm'\ndicom = pydicom.read_file(path)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T06:41:06.850665Z","iopub.execute_input":"2021-06-29T06:41:06.851039Z","iopub.status.idle":"2021-06-29T06:41:07.158594Z","shell.execute_reply.started":"2021-06-29T06:41:06.851009Z","shell.execute_reply":"2021-06-29T06:41:07.157620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nos.walk()\nreturn 3 tuples: root,dirs,files\n'''","metadata":{"execution":{"iopub.status.busy":"2021-06-29T06:41:11.515094Z","iopub.execute_input":"2021-06-29T06:41:11.515464Z","iopub.status.idle":"2021-06-29T06:41:11.523810Z","shell.execute_reply.started":"2021-06-29T06:41:11.515433Z","shell.execute_reply":"2021-06-29T06:41:11.522639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id=[]\ndimension0=[];dimension1=[]\nsplits=[]\n\n\nfor split in ['train','test']:\n    \n    save_dir=f'/kaggle/tmp/{split}/'\n    \n    os.makedirs(save_dir,exist_ok=True)\n\n    for dirname,_,filenames in tqdm(os.walk(f'../input/siim-covid19-detection/{split}')):\n        for file in filenames:\n            xray=read_xray(os.path.join(dirname,file))\n            im=resize(xray,size=512) ## resize to 512\n            im.save(os.path.join(save_dir,file.replace('dcm','jpg')))\n            \n            image_id.append(file.replace('.dcm',''))\n            dimension0.append(xray.shape[0])\n            dimension1.append(xray.shape[1])\n            splits.append(split)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T06:41:22.611160Z","iopub.execute_input":"2021-06-29T06:41:22.611528Z","iopub.status.idle":"2021-06-29T07:31:11.937129Z","shell.execute_reply.started":"2021-06-29T06:41:22.611498Z","shell.execute_reply":"2021-06-29T07:31:11.936342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n!tar -zcf train.tar.gz -C \"/kaggle/tmp/train/\" .\n!tar -zcf test.tar.gz -C \"/kaggle/tmp/test/\" .","metadata":{"execution":{"iopub.status.busy":"2021-06-29T07:56:52.597633Z","iopub.execute_input":"2021-06-29T07:56:52.597986Z","iopub.status.idle":"2021-06-29T07:57:02.004670Z","shell.execute_reply.started":"2021-06-29T07:56:52.597956Z","shell.execute_reply":"2021-06-29T07:57:02.003472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame.from_dict({'image_id': image_id, 'dim0': dimension0, 'dim1': dimension1, 'split': splits})\ndf.to_csv('meta.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T07:57:29.785896Z","iopub.execute_input":"2021-06-29T07:57:29.786261Z","iopub.status.idle":"2021-06-29T07:57:29.839214Z","shell.execute_reply.started":"2021-06-29T07:57:29.786232Z","shell.execute_reply":"2021-06-29T07:57:29.838188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}