{"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nfrom tqdm import tqdm\nimport pydicom as dicom\nimport nibabel as nib\nimport os\nimport pandas as pd\nfrom matplotlib import pyplot as plt\nimport torch.nn.functional as F\nimport torch","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-07T16:33:18.522533Z","iopub.execute_input":"2023-09-07T16:33:18.522933Z","iopub.status.idle":"2023-09-07T16:33:22.491749Z","shell.execute_reply.started":"2023-09-07T16:33:18.522904Z","shell.execute_reply":"2023-09-07T16:33:22.490807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_3D_scans(folder, downsample_rate=1): \n    filenames = os.listdir(folder)\n    filenames = [int(filename.split('.')[0]) for filename in filenames]\n    filenames = sorted(filenames)\n    filenames = [str(filename) + '.jpeg' for filename in filenames]\n        \n    volume = []\n    for filename in filenames:\n        filepath = os.path.join(folder, filename)\n        \n        image =plt.imread(filepath)\n        \"\"\"\n        ds = dicom.dcmread(filepath)\n        image = ds.pixel_array\n        \n        if ds.PixelRepresentation == 1:\n            bit_shift = ds.BitsAllocated - ds.BitsStored\n            dtype = pixel_array.dtype \n            new_array = (pixel_array << bit_shift).astype(dtype) >>  bit_shift\n            image = pydicom.pixel_data_handlers.util.apply_modality_lut(new_array, image)\n\n        if ds.PhotometricInterpretation == \"MONOCHROME1\":\n            image = 1 - image\n        \n        \n        # find rescale params\n        if (\"RescaleIntercept\" in ds) and (\"RescaleSlope\" in ds):\n            intercept = float(ds.RescaleIntercept)\n            slope = float(ds.RescaleSlope)\n        \n    \n        # find clipping params\n        center = int(ds.WindowCenter)\n        width = int(ds.WindowWidth)\n        low = center - width / 2\n        high = center + width / 2    \n        \n        \n        image = (image * slope) + intercept\n        image = np.clip(image, low, high)\n\n        image = (image / np.max(image) * 255).astype(np.int16)\n        \"\"\"\n        \n        image = image[::downsample_rate, ::downsample_rate]\n        volume.append( image )\n    \n    volume = np.stack(volume, axis=0)\n    return volume","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:48:19.334326Z","iopub.execute_input":"2023-09-07T16:48:19.334774Z","iopub.status.idle":"2023-09-07T16:48:19.345946Z","shell.execute_reply.started":"2023-09-07T16:48:19.33474Z","shell.execute_reply":"2023-09-07T16:48:19.344846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DS_RATE = 1","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:33:22.506525Z","iopub.execute_input":"2023-09-07T16:33:22.506869Z","iopub.status.idle":"2023-09-07T16:33:22.522602Z","shell.execute_reply.started":"2023-09-07T16:33:22.506841Z","shell.execute_reply":"2023-09-07T16:33:22.521417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filepath = '/kaggle/input/rsna-2023-atd-reduced-256-5mm/reduced_256_tickness_5/10004/21057'\nvolume = create_3D_scans(filepath, downsample_rate=DS_RATE)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:48:21.833852Z","iopub.execute_input":"2023-09-07T16:48:21.83516Z","iopub.status.idle":"2023-09-07T16:48:23.74766Z","shell.execute_reply.started":"2023-09-07T16:48:21.835111Z","shell.execute_reply":"2023-09-07T16:48:23.746582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tempfile import TemporaryFile\n\nfolderpath = '/kaggle/input/rsna-2023-atd-reduced-256-5mm/reduced_256_tickness_5/'\noutput_file_prefix = \"part\"\n\nscans_3d = {}\n\npatient_ids = []\nscan_ids = []\nscan_size = []\nscan_width = []\nscan_height = []\npart = []\n\npart_counter = 1\n\nfor _, patients, _ in os.walk(folderpath):\n    num_patients = len(patients)\n    for i, patient_id in enumerate(patients):\n        patient_dir = os.path.join(folderpath, patient_id)\n        for _, scans, _ in os.walk(patient_dir):\n            for scan_id in scans:\n                scan_dir = os.path.join(patient_dir, scan_id)\n                volume = create_3D_scans(scan_dir, downsample_rate=DS_RATE)\n                \n                scans_3d[f\"{scan_id}\"] = volume\n                size, height, width = volume.shape\n                \n                patient_ids.append(patient_id)\n                scan_ids.append(scan_id)\n                scan_size.append(size)\n                scan_width.append(width)\n                scan_height.append(height)\n                part.append(f\"{output_file_prefix}_{part_counter}\")\n                \n        if i == int(num_patients/2):\n            np.savez(f\"{output_file_prefix}_{part_counter}.npz\", **scans_3d, allow_pickle=True)\n            print(f\"SAVED FILE: {output_file_prefix}_{part_counter}.npz\")\n            scans_3d = {}\n            part_counter += 1\n    \nnp.savez(f\"{output_file_prefix}_{part_counter}.npz\", **scans_3d, allow_pickle=True)\nprint(f\"SAVED FILE: {output_file_prefix}_{part_counter}.npz\")\nscans_3d = {}\n            ","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:40:56.231586Z","iopub.execute_input":"2023-09-07T16:40:56.232095Z","iopub.status.idle":"2023-09-07T16:41:20.971649Z","shell.execute_reply.started":"2023-09-07T16:40:56.23206Z","shell.execute_reply":"2023-09-07T16:41:20.969377Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#data = np.load(\"part1.npz\", allow_pickle=True)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:36:00.109282Z","iopub.execute_input":"2023-09-07T16:36:00.11092Z","iopub.status.idle":"2023-09-07T16:36:00.131484Z","shell.execute_reply.started":"2023-09-07T16:36:00.110869Z","shell.execute_reply":"2023-09-07T16:36:00.13015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dataset = list(data.keys())","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:36:00.133325Z","iopub.execute_input":"2023-09-07T16:36:00.133892Z","iopub.status.idle":"2023-09-07T16:36:00.141117Z","shell.execute_reply.started":"2023-09-07T16:36:00.13384Z","shell.execute_reply":"2023-09-07T16:36:00.139915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plt.imshow(data[\"61834/51596\"][6], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:36:00.142666Z","iopub.execute_input":"2023-09-07T16:36:00.143944Z","iopub.status.idle":"2023-09-07T16:36:00.583312Z","shell.execute_reply.started":"2023-09-07T16:36:00.14391Z","shell.execute_reply":"2023-09-07T16:36:00.581703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shapes = pd.DataFrame({\n    \"patient_ids\": patient_ids,\n    \"scan_ids\": scan_ids,\n    \"size\": scan_size,\n    \"width\": scan_width,\n    \"height\": scan_height,\n    \"location\": part\n})\n\nshapes.to_csv(\"shapes.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:36:00.585066Z","iopub.execute_input":"2023-09-07T16:36:00.585444Z","iopub.status.idle":"2023-09-07T16:36:00.623487Z","shell.execute_reply.started":"2023-09-07T16:36:00.585413Z","shell.execute_reply":"2023-09-07T16:36:00.622159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shapes","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:36:00.629257Z","iopub.execute_input":"2023-09-07T16:36:00.629685Z","iopub.status.idle":"2023-09-07T16:36:00.663284Z","shell.execute_reply.started":"2023-09-07T16:36:00.629654Z","shell.execute_reply":"2023-09-07T16:36:00.66197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shapes[shapes[\"size\"] == 1]","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:36:00.664935Z","iopub.execute_input":"2023-09-07T16:36:00.665275Z","iopub.status.idle":"2023-09-07T16:36:00.684546Z","shell.execute_reply.started":"2023-09-07T16:36:00.665246Z","shell.execute_reply":"2023-09-07T16:36:00.683187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counts = shapes.groupby(by=[\"size\"]).count().sort_values(by=[\"size\"])\ncounts","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:36:00.686458Z","iopub.execute_input":"2023-09-07T16:36:00.686952Z","iopub.status.idle":"2023-09-07T16:36:00.720038Z","shell.execute_reply.started":"2023-09-07T16:36:00.68691Z","shell.execute_reply":"2023-09-07T16:36:00.718553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counts[counts[\"patient_ids\"] > 2][\"patient_ids\"].plot()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:36:00.721925Z","iopub.execute_input":"2023-09-07T16:36:00.722303Z","iopub.status.idle":"2023-09-07T16:36:01.003714Z","shell.execute_reply.started":"2023-09-07T16:36:00.722269Z","shell.execute_reply":"2023-09-07T16:36:01.001979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counts[\"patient_ids\"].plot(kind=\"bar\", xticks=counts.index.values[::10])","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:36:01.005959Z","iopub.execute_input":"2023-09-07T16:36:01.006502Z","iopub.status.idle":"2023-09-07T16:36:01.45039Z","shell.execute_reply.started":"2023-09-07T16:36:01.006443Z","shell.execute_reply":"2023-09-07T16:36:01.448614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv(\"/kaggle/input/rsna-2023-abdominal-trauma-detection/train.csv\")\nlabels[\"bowel\"] = (labels.bowel_healthy == 1).astype(int)\nlabels[\"extravasation\"] = (labels.extravasation_healthy == 1).astype(int)\n\nlabels = labels.drop(columns=[\"bowel_healthy\", \"bowel_injury\", \"extravasation_healthy\", \"extravasation_injury\"])\nlabels = labels.set_index(\"patient_id\")\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:39:14.096668Z","iopub.execute_input":"2023-09-07T16:39:14.097335Z","iopub.status.idle":"2023-09-07T16:39:14.130364Z","shell.execute_reply.started":"2023-09-07T16:39:14.097299Z","shell.execute_reply":"2023-09-07T16:39:14.129006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:38:41.439405Z","iopub.execute_input":"2023-09-07T16:38:41.439932Z","iopub.status.idle":"2023-09-07T16:38:41.448997Z","shell.execute_reply.started":"2023-09-07T16:38:41.439895Z","shell.execute_reply":"2023-09-07T16:38:41.447075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.loc[10004]","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:37:47.940362Z","iopub.execute_input":"2023-09-07T16:37:47.940821Z","iopub.status.idle":"2023-09-07T16:37:47.952737Z","shell.execute_reply.started":"2023-09-07T16:37:47.940788Z","shell.execute_reply":"2023-09-07T16:37:47.950703Z"},"trusted":true},"execution_count":null,"outputs":[]}],"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"}}