{"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":"# # # !ls /kaggle/input/custom/seg_25d_/seg_25d_mask\n# ! pip install pylibjpeg pylibjpeg-libjpeg pydicom\n# ! pip install -U python-gdcm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-23T06:40:32.985225Z","iopub.execute_input":"2023-05-23T06:40:32.985875Z","iopub.status.idle":"2023-05-23T06:40:32.99072Z","shell.execute_reply.started":"2023-05-23T06:40:32.98583Z","shell.execute_reply":"2023-05-23T06:40:32.989672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.simplefilter(action='ignore')\n\nimport os\nimport re\nimport cv2\nimport pydicom\nimport numpy as np\nfrom glob import glob\nimport nibabel as nib\nfrom tqdm import tqdm\nimport dask.array as da\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:40:32.995668Z","iopub.execute_input":"2023-05-23T06:40:32.996158Z","iopub.status.idle":"2023-05-23T06:40:33.591795Z","shell.execute_reply.started":"2023-05-23T06:40:32.996129Z","shell.execute_reply":"2023-05-23T06:40:33.590799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_patient = [i[:-4] for i in os.listdir(\"../input/rsna-2022-cervical-spine-fracture-detection/segmentations/\")]","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:40:33.592959Z","iopub.execute_input":"2023-05-23T06:40:33.59403Z","iopub.status.idle":"2023-05-23T06:40:33.599755Z","shell.execute_reply.started":"2023-05-23T06:40:33.593997Z","shell.execute_reply":"2023-05-23T06:40:33.598654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#For Segmentation data\ndef load_NIfTI(path):\n    mask = nib.load(path)\n    \n    # Convert to numpy array\n    seg = mask.get_fdata()\n    \n    # Align orientation with images as \n    # NIFTI files consist of segmentation in the sagittal plane, \n    # while the DICOM files are in the axial plane\n    \n    seg = seg[:, ::-1, ::-1].transpose(2, 1, 0)\n    \n    return seg","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:40:33.60259Z","iopub.execute_input":"2023-05-23T06:40:33.602954Z","iopub.status.idle":"2023-05-23T06:40:33.609946Z","shell.execute_reply.started":"2023-05-23T06:40:33.602924Z","shell.execute_reply":"2023-05-23T06:40:33.608428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Example segment image\npath_mask=f\"../input/rsna-2022-cervical-spine-fracture-detection/segmentations/{training_patient[0]}.nii\"\npatient_mask=load_NIfTI(path_mask)\n\npatient_mask.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:40:33.611226Z","iopub.execute_input":"2023-05-23T06:40:33.611657Z","iopub.status.idle":"2023-05-23T06:40:33.755642Z","shell.execute_reply.started":"2023-05-23T06:40:33.611616Z","shell.execute_reply":"2023-05-23T06:40:33.754575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use(\"dark_background\")\nfig, axes = plt.subplots(nrows=3, ncols=6, figsize=(24,12))\nfig.suptitle(f'ID: {training_patient[0]}', weight=\"bold\", size=20)\n\nstart=110\nfor i in range(start,start+18):\n    mask = patient_mask[i]\n    slice_no = i\n\n    # Plot the image\n    x = (i-110) // 6\n    y = (i-110) % 6\n\n    axes[x, y].imshow(mask, cmap='bone')\n    axes[x, y].set_title(f\"Slice: {slice_no}\", fontsize=14, weight='bold')\n    axes[x, y].axis('off')","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:40:33.756868Z","iopub.execute_input":"2023-05-23T06:40:33.757201Z","iopub.status.idle":"2023-05-23T06:40:35.935373Z","shell.execute_reply.started":"2023-05-23T06:40:33.757172Z","shell.execute_reply":"2023-05-23T06:40:35.934558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Loading Scans\ndef atoi(text):\n    return int(text) if text.isdigit() else text\n\ndef natural_keys(text):\n    return [atoi(c) for c in re.split(r'(\\d+)', text)]\n\n# Load the scans in given folder path\ndef load_scan(path):\n\n    dcm_paths = glob(f\"{path}/*\")\n    dcm_paths.sort(key=natural_keys)    \n    patient_scan = [pydicom.dcmread(paths) for paths in dcm_paths]\n    \n    return patient_scan","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:40:35.936492Z","iopub.execute_input":"2023-05-23T06:40:35.937783Z","iopub.status.idle":"2023-05-23T06:40:35.944062Z","shell.execute_reply.started":"2023-05-23T06:40:35.937748Z","shell.execute_reply":"2023-05-23T06:40:35.942875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #Example Scan\npath_scan=f\"../input/rsna-2022-cervical-spine-fracture-detection/train_images/{training_patient[0]}\"\nimage=load_scan(path_scan)","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:40:35.945489Z","iopub.execute_input":"2023-05-23T06:40:35.946022Z","iopub.status.idle":"2023-05-23T06:40:36.40129Z","shell.execute_reply.started":"2023-05-23T06:40:35.94598Z","shell.execute_reply":"2023-05-23T06:40:36.400192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot images\nfig, axes = plt.subplots(nrows=3, ncols=6, figsize=(24,12))\nfig.suptitle(f'ID: {training_patient[0]}', weight=\"bold\", size=20)\n\nstart = 110\nfor i in range(start,start+18):\n    img = image[i].pixel_array\n    slice_no = i\n\n    # Plot the image\n    x = (i-start) // 6\n    y = (i-start) % 6\n\n    axes[x, y].imshow(img, cmap=\"bone\")\n    axes[x, y].set_title(f\"Slice: {slice_no}\", fontsize=14, weight='bold')\n    axes[x, y].axis('off')","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:40:36.402653Z","iopub.execute_input":"2023-05-23T06:40:36.403021Z","iopub.status.idle":"2023-05-23T06:40:39.22665Z","shell.execute_reply.started":"2023-05-23T06:40:36.402989Z","shell.execute_reply":"2023-05-23T06:40:39.225484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_pixels_hu(slices):\n   \n    image = np.stack([cv2.resize(s.pixel_array,(512,512),interpolation = cv2.INTER_NEAREST) for s in slices])\n    \n    # Convert to int16 (from saometimes int16), \n    # should be possible as values should always be low enough (<32k)\n    image = image.astype(np.int16)\n    image = da.from_array(image) #Using Dask to speed up processing\n    \n    # Set outside-of-scan pixels to 0\n    # The intercept is usually -1024, so air is approximately 0\n    image[image <= -1000] = 0\n    \n    # Convert to Hounsfield units (HU) \n        \n    intercept = da.from_array([slices[slice_number].RescaleIntercept for slice_number in range(len(slices))])\n    slope = da.from_array([slices[slice_number].RescaleSlope for slice_number in range(len(slices))])\n    \n    intercept=intercept.reshape((-1,1,1))\n    slope=slope.reshape((-1,1,1))\n    \n    image= slope * image.astype(\"float64\")\n        \n    image+= intercept\n     \n    return image.astype(\"int16\")","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:40:39.227991Z","iopub.execute_input":"2023-05-23T06:40:39.228537Z","iopub.status.idle":"2023-05-23T06:40:39.236791Z","shell.execute_reply.started":"2023-05-23T06:40:39.228488Z","shell.execute_reply":"2023-05-23T06:40:39.235596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient_slice=get_pixels_hu(image)","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:40:39.238247Z","iopub.execute_input":"2023-05-23T06:40:39.238632Z","iopub.status.idle":"2023-05-23T06:40:39.56314Z","shell.execute_reply.started":"2023-05-23T06:40:39.238598Z","shell.execute_reply":"2023-05-23T06:40:39.561583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot images\nfig, axes = plt.subplots(nrows=3, ncols=6, figsize=(24,12))\nfig.suptitle(f'ID: {training_patient[0]}', weight=\"bold\", size=20)\n\nstart = 110\nfor i in range(start,start+18):\n    img = patient_slice[i]\n    slice_no = i\n\n    # Plot the image\n    x = (i-start) // 6\n    y = (i-start) % 6\n\n    axes[x, y].imshow(img, cmap=\"bone\")\n    axes[x, y].set_title(f\"Slice: {slice_no}\", fontsize=14, weight='bold')\n    axes[x, y].axis('off')","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:40:39.564576Z","iopub.execute_input":"2023-05-23T06:40:39.564938Z","iopub.status.idle":"2023-05-23T06:40:52.594662Z","shell.execute_reply.started":"2023-05-23T06:40:39.564907Z","shell.execute_reply":"2023-05-23T06:40:52.592997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MIN_BOUND = 150.0\nMAX_BOUND = 2050.0\n    \ndef normalize(image):\n    image = (image - MIN_BOUND)*255.0 / (MAX_BOUND - MIN_BOUND)\n    image[image>255] = 255.\n    image[image<0] = 255.\n    return image","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:43:05.06717Z","iopub.status.idle":"2023-05-23T06:43:05.067592Z","shell.execute_reply.started":"2023-05-23T06:43:05.06739Z","shell.execute_reply":"2023-05-23T06:43:05.067407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p /kaggle/working/dataset/\n!mkdir -p /kaggle/working/dataset/images\n!mkdir -p /kaggle/working/dataset/masks","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:40:52.60745Z","iopub.execute_input":"2023-05-23T06:40:52.607857Z","iopub.status.idle":"2023-05-23T06:40:55.717782Z","shell.execute_reply.started":"2023-05-23T06:40:52.607822Z","shell.execute_reply":"2023-05-23T06:40:55.716065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = []\ndef Cervical_Train(upto=18):\n    for i in range(upto):\n        patient_ID=training_patient[i]\n\n        patient_seg=load_NIfTI(f\"../input/rsna-2022-cervical-spine-fracture-detection/segmentations/{patient_ID}.nii\")\n\n        patient_scan=load_scan(f\"../input/rsna-2022-cervical-spine-fracture-detection/train_images/{patient_ID}\")\n        patient_hu=get_pixels_hu(patient_scan)\n        patient_hu_normalised=normalize(patient_hu)\n\n        for j in tqdm(range(0,len(patient_seg))):\n\n            img_path = f'/kaggle/working/dataset/images/{patient_ID}_{j}.npy'\n            seg_path = f'/kaggle/working/dataset/masks/{patient_ID}_{j}.npy'\n\n            np.save(img_path, patient_hu_normalised[j].astype(np.uint8))\n            np.save(seg_path, patient_seg[j].astype(np.uint8))\n\n            train_ds.append([img_path, seg_path, patient_ID])","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:40:55.719771Z","iopub.execute_input":"2023-05-23T06:40:55.720162Z","iopub.status.idle":"2023-05-23T06:40:55.728591Z","shell.execute_reply.started":"2023-05-23T06:40:55.720123Z","shell.execute_reply":"2023-05-23T06:40:55.727683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Cervical_Train()","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:43:33.139478Z","iopub.execute_input":"2023-05-23T06:43:33.139895Z","iopub.status.idle":"2023-05-23T06:43:33.145743Z","shell.execute_reply.started":"2023-05-23T06:43:33.139862Z","shell.execute_reply":"2023-05-23T06:43:33.144203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport zipfile\nfrom IPython.display import FileLink, display\n\ndef zip_file(inputdir):\n    \n    zf = zipfile.ZipFile(f\"/kaggle/working/dataset.zip\", \"w\")\n\n    for dirname, subdirs, files in os.walk(f\"/{inputdir}\"):\n        zf.write(dirname)\n        for filename in tqdm(files):\n            zf.write(os.path.join(dirname, filename))\n            \n    zf.close()\n    os.chdir('/kaggle/working/')","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:43:05.052656Z","iopub.status.idle":"2023-05-23T06:43:05.053084Z","shell.execute_reply.started":"2023-05-23T06:43:05.052892Z","shell.execute_reply":"2023-05-23T06:43:05.052911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_file('/kaggle/working/dataset/')","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:43:05.054808Z","iopub.status.idle":"2023-05-23T06:43:05.05522Z","shell.execute_reply.started":"2023-05-23T06:43:05.055034Z","shell.execute_reply":"2023-05-23T06:43:05.055052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:43:05.056404Z","iopub.status.idle":"2023-05-23T06:43:05.056823Z","shell.execute_reply.started":"2023-05-23T06:43:05.05664Z","shell.execute_reply":"2023-05-23T06:43:05.056658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(FileLink('dataset.zip'))","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:43:05.057909Z","iopub.status.idle":"2023-05-23T06:43:05.059487Z","shell.execute_reply.started":"2023-05-23T06:43:05.05927Z","shell.execute_reply":"2023-05-23T06:43:05.059293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink, display","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:43:05.060941Z","iopub.status.idle":"2023-05-23T06:43:05.061351Z","shell.execute_reply.started":"2023-05-23T06:43:05.06116Z","shell.execute_reply":"2023-05-23T06:43:05.061177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/\nFileLink('dataset.zip')","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:43:05.062552Z","iopub.status.idle":"2023-05-23T06:43:05.062959Z","shell.execute_reply.started":"2023-05-23T06:43:05.062761Z","shell.execute_reply":"2023-05-23T06:43:05.062779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2023-05-23T06:43:05.064442Z","iopub.status.idle":"2023-05-23T06:43:05.065103Z","shell.execute_reply.started":"2023-05-23T06:43:05.064899Z","shell.execute_reply":"2023-05-23T06:43:05.064919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}