{"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":"!pip install pydicom\n!pip install -U pylibjpeg pylibjpeg-openjpeg pylibjpeg-libjpeg\n!pip install pylibjpeg-libjpeg","metadata":{"execution":{"iopub.status.busy":"2023-03-25T20:38:44.151332Z","iopub.execute_input":"2023-03-25T20:38:44.151794Z","iopub.status.idle":"2023-03-25T20:39:20.630046Z","shell.execute_reply.started":"2023-03-25T20:38:44.151753Z","shell.execute_reply":"2023-03-25T20:39:20.628179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport sys\nfrom glob import glob\nimport cv2\nimport re\nimport gc\nimport pydicom as dicom\nimport nibabel as nib\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-25T20:39:20.63283Z","iopub.execute_input":"2023-03-25T20:39:20.633321Z","iopub.status.idle":"2023-03-25T20:39:20.641129Z","shell.execute_reply.started":"2023-03-25T20:39:20.633276Z","shell.execute_reply":"2023-03-25T20:39:20.639631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_list = [\n    '1.2.826.0.1.3680043.1363',\n    '1.2.826.0.1.3680043.4202',\n    '1.2.826.0.1.3680043.26979',\n    '1.2.826.0.1.3680043.30067'\n]","metadata":{"execution":{"iopub.status.busy":"2023-03-25T20:39:20.643225Z","iopub.execute_input":"2023-03-25T20:39:20.644071Z","iopub.status.idle":"2023-03-25T20:39:20.65483Z","shell.execute_reply.started":"2023-03-25T20:39:20.64401Z","shell.execute_reply":"2023-03-25T20:39:20.653394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom(inpath: str, size: int=512):\n    paths = sorted(inpath, key=lambda p: int(os.path.splitext(os.path.basename(p))[0]))\n    \n    imgs = []\n    for path in paths:\n        img = dicom.read_file(path)\n        img = img.pixel_array\n        img = cv2.resize(img, (size, size), interpolation=cv2.INTER_LINEAR)\n        img = img - np.min(img)\n        img = img / (np.max(img) + 1e-7)\n        img = (img * 255).astype(np.uint8)\n        imgs.append(img)\n        \n    return imgs","metadata":{"execution":{"iopub.status.busy":"2023-03-25T20:39:20.656807Z","iopub.execute_input":"2023-03-25T20:39:20.657333Z","iopub.status.idle":"2023-03-25T20:39:20.668243Z","shell.execute_reply.started":"2023-03-25T20:39:20.657276Z","shell.execute_reply":"2023-03-25T20:39:20.666832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_nii(path, size=512):\n    segment = nib.load(path).get_fdata() # convert to numpy array\n    # conversion from axial to sagittal view\n    seg = segment[:, ::-1, ::-1].transpose(1, 0, 2)\n    shape = seg.shape\n    seg = cv2.resize(seg, (size, size), interpolation=cv2.INTER_LINEAR)\n    seg = seg.astype(np.uint16) * 255\n    mask = np.asarray(seg)\n\n    # image orientation: (width, height, channel)\n    return mask.transpose(1, 2, 0)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T20:39:20.670181Z","iopub.execute_input":"2023-03-25T20:39:20.670672Z","iopub.status.idle":"2023-03-25T20:39:20.680318Z","shell.execute_reply.started":"2023-03-25T20:39:20.670631Z","shell.execute_reply":"2023-03-25T20:39:20.678774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    os.mkdir('train_images')\n    os.mkdir('segmentations')\nexcept: pass\n\ntrain_df = pd.read_csv('/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train.csv')\nfor idx in range(len(train_df)):\n    uid = train_df.loc[idx]['StudyInstanceUID']\n    if uid in save_list:\n        image_path = glob(f'/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train_images/{uid}/*.dcm')\n        mask_path = f'/kaggle/input/rsna-2022-cervical-spine-fracture-detection/segmentations/{uid}.nii'\n        \n        image = load_dicom(image_path)\n        mask = load_nii(mask_path)\n        \n        for idx in range(len(image)):\n            image_save_path = f'/kaggle/working/train_images/{uid}_{idx}.npz'\n            np.savez_compressed(image_save_path, image[idx])\n            \n            mask_save_path = f'/kaggle/working/segmentations/{uid}_{idx}.npz'\n            np.savez_compressed(mask_save_path, mask[idx])","metadata":{"execution":{"iopub.status.busy":"2023-03-25T20:39:20.687475Z","iopub.execute_input":"2023-03-25T20:39:20.688602Z","iopub.status.idle":"2023-03-25T20:40:11.930782Z","shell.execute_reply.started":"2023-03-25T20:39:20.688552Z","shell.execute_reply":"2023-03-25T20:40:11.929135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\n\n# compress 'prep_seg' dir as a zip file\noutput_filename = 'train_images'\ndir_name = 'train_images'\nshutil.make_archive(output_filename, 'zip', dir_name)\npath = '/kaggle/working/train_images'\n\n\n# remove all files in prep_seg directory\nfor file_name in os.listdir(path):\n    file = path + '/' + file_name\n    if os.path.isfile(file):\n        os.remove(file)\n# delete uncompressed directory\nos.rmdir('train_images')\n\n\n# compress 'prep_seg' dir as a zip file\noutput_filename = 'segmentations'\ndir_name = 'segmentations'\nshutil.make_archive(output_filename, 'zip', dir_name)\npath = '/kaggle/working/segmentations'\n\n# remove all files in prep_seg directory\nfor file_name in os.listdir(path):\n    file = path + '/' + file_name\n    if os.path.isfile(file):\n        os.remove(file)\n# delete uncompressed directory\nos.rmdir('segmentations')","metadata":{"execution":{"iopub.status.busy":"2023-03-25T20:40:11.932938Z","iopub.execute_input":"2023-03-25T20:40:11.933423Z","iopub.status.idle":"2023-03-25T20:40:17.840227Z","shell.execute_reply.started":"2023-03-25T20:40:11.933348Z","shell.execute_reply":"2023-03-25T20:40:17.838838Z"},"trusted":true},"execution_count":null,"outputs":[]}]}