{"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-23T03:44:02.073639Z","iopub.execute_input":"2021-06-23T03:44:02.074251Z","iopub.status.idle":"2021-06-23T03:44:38.637210Z","shell.execute_reply.started":"2021-06-23T03:44:02.074194Z","shell.execute_reply":"2021-06-23T03:44:38.636137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nfrom PIL import Image\nimport matplotlib.pylab as plt\n\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nfrom tqdm.auto import tqdm\n\nimport tensorflow as tf\n#import tensorflow_io as tfio\nimport IPython.display as display","metadata":{"execution":{"iopub.status.busy":"2021-06-23T03:44:38.639353Z","iopub.execute_input":"2021-06-23T03:44:38.639719Z","iopub.status.idle":"2021-06-23T03:44:38.646003Z","shell.execute_reply.started":"2021-06-23T03:44:38.639686Z","shell.execute_reply":"2021-06-23T03:44:38.644748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/siim-covid19-detection/train_image_level.csv')\nprint(df.shape)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-23T03:44:38.647536Z","iopub.execute_input":"2021-06-23T03:44:38.647997Z","iopub.status.idle":"2021-06-23T03:44:38.758235Z","shell.execute_reply.started":"2021-06-23T03:44:38.647946Z","shell.execute_reply":"2021-06-23T03:44:38.756898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = '../input/siim-covid19-detection/train/00086460a852/9e8302230c91/65761e66de9f.dcm'\n#dicom = pydicom.read_file(image_path)\ndicom_file = pydicom.dcmread(image_path)\nPatientSex = dicom_file.PatientSex\nPatientSex","metadata":{"execution":{"iopub.status.busy":"2021-06-23T03:44:38.761794Z","iopub.execute_input":"2021-06-23T03:44:38.762117Z","iopub.status.idle":"2021-06-23T03:44:39.267204Z","shell.execute_reply.started":"2021-06-23T03:44:38.762088Z","shell.execute_reply":"2021-06-23T03:44:39.266140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom_file","metadata":{"execution":{"iopub.status.busy":"2021-06-23T03:44:39.270955Z","iopub.execute_input":"2021-06-23T03:44:39.271433Z","iopub.status.idle":"2021-06-23T03:44:39.282396Z","shell.execute_reply.started":"2021-06-23T03:44:39.271390Z","shell.execute_reply":"2021-06-23T03:44:39.281179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = apply_voi_lut(dicom_file.pixel_array, dicom_file)\n\nif dicom_file.PhotometricInterpretation == \"MONOCHROME1\":\n    img = np.amax(img) - img\n    \n    img = img - np.min(img)\n    img = img / np.max(img)\n    img = (img * 255).astype(np.uint8)\n    \n# rescale pixel value between 0 to 255\n#img = (((img - np.min(img))/np.max(img))*255.0).astype(np.uint8)\n\n#print(img.dtype)  #float64\n\nplt.figure(figsize = (6,6))\nplt.imshow(img, cmap=plt.cm.gray)\n#plt.savefig(\"./train/png/\" + image_id + \"\".png\")","metadata":{"execution":{"iopub.status.busy":"2021-06-23T03:44:39.284544Z","iopub.execute_input":"2021-06-23T03:44:39.285118Z","iopub.status.idle":"2021-06-23T03:44:40.159361Z","shell.execute_reply.started":"2021-06-23T03:44:39.285072Z","shell.execute_reply":"2021-06-23T03:44:40.158264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Original from: https://www.kaggle.com/haiyunhu/siim-covid-19-convert-to-jpg-256px/edit\ndef read_xray(path,voi_lut = True, fix_monochrome = True):\n    #image_bytes = pydicom.read_file(path)\n    dicom = pydicom.read_file(path)\n    \n    # VOI LUT (if available by DICOM device) is used to transform raw DICOM data to \n    # \"human-friendly\" view\n    if voi_lut:\n        img = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        img = dicom.pixel_array\n               \n    # depending on this value, X-ray may look inverted - fix that:\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        img = np.amax(img) - img\n        \n    img = (((img - np.min(img))/np.max(img))*255.0).astype(np.uint8)\n    \n    return img\n        \ndef resize(img, size, keep_ratio=False, resample=Image.LANCZOS):\n    img = Image.fromarray(img)  # no shape\n    if keep_ratio:\n        img.thumbnail((size, size), resample)\n    else:\n        img = img.resize((size, size), resample)\n    \n    return img\n    ","metadata":{"execution":{"iopub.status.busy":"2021-06-23T03:44:40.160764Z","iopub.execute_input":"2021-06-23T03:44:40.161356Z","iopub.status.idle":"2021-06-23T03:44:40.170761Z","shell.execute_reply.started":"2021-06-23T03:44:40.161320Z","shell.execute_reply":"2021-06-23T03:44:40.169665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Original from: https://www.kaggle.com/haiyunhu/siim-covid-19-convert-to-jpg-256px/edit\nimage_id = []\ndim0 = []\ndim1 = []\nsplits = []\n\nfor split in ['test', 'train']:\n    save_dir = f'/kaggle/tmp/{split}/'\n    os.makedirs(save_dir, exist_ok=True)\n    \n    for dirname, _, filenames in tqdm(os.walk(f'../input/siim-covid19-detection/{split}')):\n        \n        for file in filenames:\n            #path = os.path.join(dirname, file)\n            #print(path)\n            xray = read_xray(os.path.join(dirname, file))\n            im = resize(xray, size=512)  \n            im.save(os.path.join(save_dir, file.replace('dcm', 'png')))\n\n            image_id.append(file.replace('.dcm', ''))\n            dim0.append(xray.shape[0])\n            dim1.append(xray.shape[1])\n            splits.append(split)","metadata":{"execution":{"iopub.status.busy":"2021-06-23T03:44:40.171831Z","iopub.execute_input":"2021-06-23T03:44:40.172119Z","iopub.status.idle":"2021-06-23T04:41:43.420750Z","shell.execute_reply.started":"2021-06-23T03:44:40.172090Z","shell.execute_reply":"2021-06-23T04:41:43.418824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame.from_dict({'image_id': image_id, 'dim0': dim0, 'dim1': dim1, 'split': splits})\ndf.to_csv('meta.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-23T04:41:43.424530Z","iopub.execute_input":"2021-06-23T04:41:43.424991Z","iopub.status.idle":"2021-06-23T04:41:43.496956Z","shell.execute_reply.started":"2021-06-23T04:41:43.424916Z","shell.execute_reply":"2021-06-23T04:41:43.495730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2021-06-23T04:41:43.498579Z","iopub.execute_input":"2021-06-23T04:41:43.498927Z","iopub.status.idle":"2021-06-23T04:41:43.524654Z","shell.execute_reply.started":"2021-06-23T04:41:43.498896Z","shell.execute_reply":"2021-06-23T04:41:43.523707Z"},"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-23T04:41:43.525886Z","iopub.execute_input":"2021-06-23T04:41:43.526375Z","iopub.status.idle":"2021-06-23T04:42:21.419911Z","shell.execute_reply.started":"2021-06-23T04:41:43.526345Z","shell.execute_reply":"2021-06-23T04:42:21.418671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}