{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.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":52254,"databundleVersionId":6863140,"sourceType":"competition"}],"dockerImageVersionId":30615,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\nimport os\nimport cv2\nimport glob\nimport pydicom\nimport zipfile\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport nibabel as nib\nimport PIL.Image as Image\nfrom pathlib import Path\nimport cv2\nfrom tqdm import tqdm\nfrom joblib import Parallel, delayed\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-20T12:39:55.186905Z","iopub.execute_input":"2023-12-20T12:39:55.187476Z","iopub.status.idle":"2023-12-20T12:39:55.194728Z","shell.execute_reply.started":"2023-12-20T12:39:55.187442Z","shell.execute_reply":"2023-12-20T12:39:55.193493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def standardize_pixel_array(dcm: pydicom.dataset.FileDataset) -> np.ndarray:\n    # Correct DICOM pixel_array if PixelRepresentation == 1.\n    pixel_array = dcm.pixel_array\n    if dcm.PixelRepresentation == 1:\n        bit_shift = dcm.BitsAllocated - dcm.BitsStored\n        dtype = pixel_array.dtype \n        pixel_array = (pixel_array << bit_shift).astype(dtype) >>  bit_shift\n\n    intercept = float(dcm.RescaleIntercept)\n    slope = float(dcm.RescaleSlope)\n    center = int(dcm.WindowCenter)\n    width = int(dcm.WindowWidth)\n    low = center - width / 2\n    high = center + width / 2    \n    \n    pixel_array = (pixel_array * slope) + intercept\n    pixel_array = np.clip(pixel_array, low, high)\n\n    return pixel_array","metadata":{"execution":{"iopub.status.busy":"2023-12-20T12:39:57.053047Z","iopub.execute_input":"2023-12-20T12:39:57.05341Z","iopub.status.idle":"2023-12-20T12:39:57.060581Z","shell.execute_reply.started":"2023-12-20T12:39:57.053381Z","shell.execute_reply":"2023-12-20T12:39:57.059546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/\"\n\nprint('Number of training patients :', len(os.listdir(TRAIN_PATH)))","metadata":{"execution":{"iopub.status.busy":"2023-12-20T12:39:57.251563Z","iopub.execute_input":"2023-12-20T12:39:57.252411Z","iopub.status.idle":"2023-12-20T12:39:57.611449Z","shell.execute_reply.started":"2023-12-20T12:39:57.252366Z","shell.execute_reply":"2023-12-20T12:39:57.610367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process(patient, size=512, save_folder=\"\", data_path=\"\"):\n    p = sorted(os.listdir(data_path + patient))\n    os.mkdir(f\"/kaggle/working/train_images/{patient}\")\n    for study in p:\n        os.mkdir(f\"/kaggle/working/train_images/{patient}/{study}\")\n        imgs = {}\n        s = sorted(os.listdir(data_path + f\"{patient}/{study}\"))\n        s.sort(key=len)\n        \n        for f in s:\n            dicom = pydicom.dcmread(os.path.join(data_path + f\"{patient}/{study}\",f))\n\n            pos_z = dicom[(0x20, 0x32)].value[-1]\n\n            img = standardize_pixel_array(dicom)\n            img = (img - img.min()) / (img.max() - img.min() + 1e-6)\n\n            if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n                img = 1 - img\n\n            imgs[pos_z] = img\n            \n        for i, k in enumerate(imgs.keys()):\n            print(k)\n            img = imgs[k]\n\n            if size is not None:\n                img = cv2.resize(img, (size, size))\n\n            if isinstance(save_folder, str):\n                cv2.imwrite(save_folder + f\"train_images/{patient}/{study}/{i+1}.png\", (img * 255).astype(np.uint8))\n            else:\n                im = cv2.imencode('.png', (img * 255).astype(np.uint8))[1]\n                save_folder.writestr(f'train_images/{patient}/{study}/{i+1}.png', im)","metadata":{"execution":{"iopub.status.busy":"2023-12-20T12:39:57.613418Z","iopub.execute_input":"2023-12-20T12:39:57.614091Z","iopub.status.idle":"2023-12-20T12:39:57.624613Z","shell.execute_reply.started":"2023-12-20T12:39:57.614055Z","shell.execute_reply":"2023-12-20T12:39:57.623609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patients = sorted(os.listdir(TRAIN_PATH))\n\n# Chunking\npatients = (\n#     patients[:400],\n#     patients[400:  800],\n#     patients[800:  1200],\n#     patients[1200: 1600],\n#     patients[1600: 2000],\n#     patients[2000: 2400],\n#     patients[2400: 2800],\n    patients[:100],\n)\npatients = patients[0] # [:100]  # subsample","metadata":{"execution":{"iopub.status.busy":"2023-12-20T12:39:57.625818Z","iopub.execute_input":"2023-12-20T12:39:57.626173Z","iopub.status.idle":"2023-12-20T12:39:57.640566Z","shell.execute_reply.started":"2023-12-20T12:39:57.626137Z","shell.execute_reply":"2023-12-20T12:39:57.639729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#p_dir = str(TRAIN_PATH).split('/')[-1]\n!mkdir train_images\nfor patient in tqdm(patients):\n    process(patient, size=None, save_folder='/kaggle/working/', data_path=TRAIN_PATH)","metadata":{"execution":{"iopub.status.busy":"2023-12-20T12:39:57.77766Z","iopub.execute_input":"2023-12-20T12:39:57.778028Z","iopub.status.idle":"2023-12-20T12:41:01.229624Z","shell.execute_reply.started":"2023-12-20T12:39:57.777997Z","shell.execute_reply":"2023-12-20T12:41:01.228116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-12-20T05:52:06.137542Z","iopub.execute_input":"2023-12-20T05:52:06.138278Z","iopub.status.idle":"2023-12-20T05:52:07.072996Z","shell.execute_reply.started":"2023-12-20T05:52:06.138247Z","shell.execute_reply":"2023-12-20T05:52:07.071701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}