{"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":"markdown","source":"# Setup and helper functions","metadata":{}},{"cell_type":"code","source":"import os\nimport re\nimport csv\nimport cv2\nimport random\nimport shutil\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom matplotlib.patches import Rectangle\n\n#import pydicom as dicom\n\nimport nibabel as nib","metadata":{"execution":{"iopub.status.busy":"2023-05-05T15:08:46.456049Z","iopub.execute_input":"2023-05-05T15:08:46.456765Z","iopub.status.idle":"2023-05-05T15:08:47.033754Z","shell.execute_reply.started":"2023-05-05T15:08:46.456618Z","shell.execute_reply":"2023-05-05T15:08:47.032147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from glob import glob\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-05-05T15:08:47.036975Z","iopub.execute_input":"2023-05-05T15:08:47.037701Z","iopub.status.idle":"2023-05-05T15:08:47.045307Z","shell.execute_reply.started":"2023-05-05T15:08:47.037634Z","shell.execute_reply":"2023-05-05T15:08:47.043791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save Images for YOLOv5 training\nI found it easier to save the CT slices as jpegs for training Ultralytics YOLO.","metadata":{}},{"cell_type":"code","source":"# patients with segmentation data\nniis = os.listdir('/kaggle/input/rsna-2022-cervical-spine-fracture-detection/segmentations')\ntrain_df = pd.read_csv('/kaggle/input/3-channel-preprocessed-dataset/3_channel_train_df1.csv')\n#pts = [re.search('(1.2.826.0.1.3680043.)([0-9]*)(?=.nii)', filename).group(0) for filename in niis]\npts = np.unique(list(train_df['StudyInstanceUID']))\n#pts = np.unique([each_img[:-4].split('_')[0] for each_img in os.listdir('/kaggle/input/3-channel-preprocessed-dataset')])\n\n# Save slices corresponding to each yolo_coord txt file\ntrain_images = '/kaggle/input/3-channel-preprocessed-dataset/prep_train'\n\nyolo_slices = os.path.join(os.getcwd(), 'yolo_slices')\nif not os.path.exists(yolo_slices):\n    os.mkdir(yolo_slices)","metadata":{"scrolled":true,"_kg_hide-input":false,"_kg_hide-output":false,"execution":{"iopub.status.busy":"2023-05-05T10:50:31.162719Z","iopub.execute_input":"2023-05-05T10:50:31.163402Z","iopub.status.idle":"2023-05-05T10:50:31.460355Z","shell.execute_reply.started":"2023-05-05T10:50:31.163358Z","shell.execute_reply":"2023-05-05T10:50:31.458014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for pt in tqdm(pts):\n    slice_nums_ = len(glob(f'/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train_images/{pt}/*'))\n    slice_nums = [int(re.search('([0-9]*)', str(filename)).group(0)) for filename in range(slice_nums_)]\n    slice_nums = sorted(slice_nums)\n    pt_CT = os.path.join(train_images, str(pt)) \n    \n    for slice_ in slice_nums:\n        uid_id = f'{pt}_{slice_}'\n        img_path = f'{pt_CT}_{slice_}'\n        img = np.load(f'{img_path}.npz')['arr_0']\n\n        imgs_savepath = os.path.join(yolo_slices, f\"{str(pt)}_{slice_}.jpg\")\n        cv2.imwrite(imgs_savepath, img)","metadata":{"execution":{"iopub.status.busy":"2023-05-05T10:50:33.109205Z","iopub.execute_input":"2023-05-05T10:50:33.109648Z","iopub.status.idle":"2023-05-05T11:06:24.632905Z","shell.execute_reply.started":"2023-05-05T10:50:33.109612Z","shell.execute_reply":"2023-05-05T11:06:24.631069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## train 2","metadata":{}},{"cell_type":"code","source":"# patients with segmentation data\nniis = os.listdir('/kaggle/input/rsna-2022-cervical-spine-fracture-detection/segmentations')\ntrain_df = pd.read_csv('/kaggle/input/3-channel-preprocessed-dataset/3_channel_train_df2.csv')\n#pts = [re.search('(1.2.826.0.1.3680043.)([0-9]*)(?=.nii)', filename).group(0) for filename in niis]\npts = np.unique(list(train_df['StudyInstanceUID']))\n#pts = np.unique([each_img[:-4].split('_')[0] for each_img in os.listdir('/kaggle/input/3-channel-preprocessed-dataset')])\n\n# Save slices corresponding to each yolo_coord txt file\ntrain_images = '/kaggle/input/3-channel-preprocessed-dataset/prep_train2'\n\nyolo_slices = os.path.join(os.getcwd(), 'yolo_slices')\nif not os.path.exists(yolo_slices):\n    os.mkdir(yolo_slices)","metadata":{"execution":{"iopub.status.busy":"2023-05-05T11:06:24.635899Z","iopub.execute_input":"2023-05-05T11:06:24.637053Z","iopub.status.idle":"2023-05-05T11:06:24.945699Z","shell.execute_reply.started":"2023-05-05T11:06:24.636998Z","shell.execute_reply":"2023-05-05T11:06:24.944509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for pt in tqdm(pts):\n    slice_nums_ = len(glob(f'/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train_images/{pt}/*'))\n    slice_nums = [int(re.search('([0-9]*)', str(filename)).group(0)) for filename in range(slice_nums_)]\n    slice_nums = sorted(slice_nums)\n    pt_CT = os.path.join(train_images, str(pt)) \n    \n    for slice_ in slice_nums:\n        uid_id = f'{pt}_{slice_}'\n        img_path = f'{pt_CT}_{slice_}'\n        img = np.load(f'{img_path}.npz')['arr_0']\n\n        imgs_savepath = os.path.join(yolo_slices, f\"{str(pt)}_{slice_}.jpg\")\n        cv2.imwrite(imgs_savepath, img)","metadata":{"execution":{"iopub.status.busy":"2023-05-05T11:06:24.94719Z","iopub.execute_input":"2023-05-05T11:06:24.947659Z","iopub.status.idle":"2023-05-05T11:24:41.801272Z","shell.execute_reply.started":"2023-05-05T11:06:24.947612Z","shell.execute_reply":"2023-05-05T11:24:41.800082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## train 5","metadata":{}},{"cell_type":"code","source":"# patients with segmentation data\nniis = os.listdir('/kaggle/input/rsna-2022-cervical-spine-fracture-detection/segmentations')\ntrain_df = pd.read_csv('/kaggle/input/3-channel-preprocessed-dataset/3_channel_train_df5.csv')\n#pts = [re.search('(1.2.826.0.1.3680043.)([0-9]*)(?=.nii)', filename).group(0) for filename in niis]\npts = np.unique(list(train_df['StudyInstanceUID']))\n#pts = np.unique([each_img[:-4].split('_')[0] for each_img in os.listdir('/kaggle/input/3-channel-preprocessed-dataset')])\n\n# Save slices corresponding to each yolo_coord txt file\ntrain_images = '/kaggle/input/3-channel-preprocessed-dataset/prep_train5'\n\nyolo_slices = os.path.join(os.getcwd(), 'yolo_slices')\nif not os.path.exists(yolo_slices):\n    os.mkdir(yolo_slices)","metadata":{"execution":{"iopub.status.busy":"2023-05-05T11:24:41.804266Z","iopub.execute_input":"2023-05-05T11:24:41.804641Z","iopub.status.idle":"2023-05-05T11:24:42.219661Z","shell.execute_reply.started":"2023-05-05T11:24:41.804607Z","shell.execute_reply":"2023-05-05T11:24:42.218455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for pt in tqdm(pts):\n    slice_nums_ = len(glob(f'/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train_images/{pt}/*'))\n    slice_nums = [int(re.search('([0-9]*)', str(filename)).group(0)) for filename in range(slice_nums_)]\n    slice_nums = sorted(slice_nums)\n    pt_CT = os.path.join(train_images, str(pt)) \n    \n    for slice_ in slice_nums:\n        uid_id = f'{pt}_{slice_}'\n        img_path = f'{pt_CT}_{slice_}'\n        img = np.load(f'{img_path}.npz')['arr_0']\n\n        imgs_savepath = os.path.join(yolo_slices, f\"{str(pt)}_{slice_}.jpg\")\n        cv2.imwrite(imgs_savepath, img)","metadata":{"execution":{"iopub.status.busy":"2023-05-05T11:24:42.220926Z","iopub.execute_input":"2023-05-05T11:24:42.221271Z","iopub.status.idle":"2023-05-05T11:44:25.729466Z","shell.execute_reply.started":"2023-05-05T11:24:42.22124Z","shell.execute_reply":"2023-05-05T11:44:25.728203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## train 3","metadata":{}},{"cell_type":"code","source":"# patients with segmentation data\nniis = os.listdir('/kaggle/input/rsna-2022-cervical-spine-fracture-detection/segmentations')\ntrain_df = pd.read_csv('/kaggle/input/3-channel-preprocessed-dataset/3_channel_train_df3.csv')\n#pts = [re.search('(1.2.826.0.1.3680043.)([0-9]*)(?=.nii)', filename).group(0) for filename in niis]\npts = np.unique(list(train_df['StudyInstanceUID']))\n#pts = np.unique([each_img[:-4].split('_')[0] for each_img in os.listdir('/kaggle/input/3-channel-preprocessed-dataset')])\n\n# Save slices corresponding to each yolo_coord txt file\ntrain_images = '/kaggle/input/3-channel-preprocessed-dataset/prep_train3'\n\nyolo_slices = os.path.join(os.getcwd(), 'yolo_slices')\nif not os.path.exists(yolo_slices):\n    os.mkdir(yolo_slices)","metadata":{"execution":{"iopub.status.busy":"2023-05-05T15:09:12.844101Z","iopub.execute_input":"2023-05-05T15:09:12.84462Z","iopub.status.idle":"2023-05-05T15:09:13.080631Z","shell.execute_reply.started":"2023-05-05T15:09:12.844581Z","shell.execute_reply":"2023-05-05T15:09:13.079229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for pt in tqdm(pts):\n    slice_nums_ = len(glob(f'/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train_images/{pt}/*'))\n    slice_nums = [int(re.search('([0-9]*)', str(filename)).group(0)) for filename in range(slice_nums_)]\n    slice_nums = sorted(slice_nums)\n    pt_CT = os.path.join(train_images, str(pt)) \n    \n    for slice_ in slice_nums:\n        uid_id = f'{pt}_{slice_}'\n        img_path = f'{pt_CT}_{slice_}'\n        img = np.load(f'{img_path}.npz')['arr_0']\n\n        imgs_savepath = os.path.join(yolo_slices, f\"{str(pt)}_{slice_}.jpg\")\n        cv2.imwrite(imgs_savepath, img)","metadata":{"execution":{"iopub.status.busy":"2023-05-05T15:09:13.296242Z","iopub.execute_input":"2023-05-05T15:09:13.296703Z","iopub.status.idle":"2023-05-05T15:24:16.225203Z","shell.execute_reply.started":"2023-05-05T15:09:13.296666Z","shell.execute_reply":"2023-05-05T15:24:16.22269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## train 4","metadata":{}},{"cell_type":"code","source":"# patients with segmentation data\nniis = os.listdir('/kaggle/input/rsna-2022-cervical-spine-fracture-detection/segmentations')\ntrain_df = pd.read_csv('/kaggle/input/3-channel-preprocessed-dataset/3_channel_train_df4.csv')\n#pts = [re.search('(1.2.826.0.1.3680043.)([0-9]*)(?=.nii)', filename).group(0) for filename in niis]\npts = np.unique(list(train_df['StudyInstanceUID']))\n#pts = np.unique([each_img[:-4].split('_')[0] for each_img in os.listdir('/kaggle/input/3-channel-preprocessed-dataset')])\n\n# Save slices corresponding to each yolo_coord txt file\ntrain_images = '/kaggle/input/3-channel-preprocessed-dataset/prep_train4'\n\nyolo_slices = os.path.join(os.getcwd(), 'yolo_slices')\nif not os.path.exists(yolo_slices):\n    os.mkdir(yolo_slices)","metadata":{"execution":{"iopub.status.busy":"2023-05-05T15:25:50.887178Z","iopub.execute_input":"2023-05-05T15:25:50.887805Z","iopub.status.idle":"2023-05-05T15:25:51.133715Z","shell.execute_reply.started":"2023-05-05T15:25:50.887751Z","shell.execute_reply":"2023-05-05T15:25:51.132663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for pt in tqdm(pts):\n    slice_nums_ = len(glob(f'/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train_images/{pt}/*'))\n    slice_nums = [int(re.search('([0-9]*)', str(filename)).group(0)) for filename in range(slice_nums_)]\n    slice_nums = sorted(slice_nums)\n    pt_CT = os.path.join(train_images, str(pt)) \n    \n    for slice_ in slice_nums:\n        uid_id = f'{pt}_{slice_}'\n        img_path = f'{pt_CT}_{slice_}'\n        img = np.load(f'{img_path}.npz')['arr_0']\n\n        imgs_savepath = os.path.join(yolo_slices, f\"{str(pt)}_{slice_}.jpg\")\n        cv2.imwrite(imgs_savepath, img)","metadata":{"execution":{"iopub.status.busy":"2023-05-05T15:25:51.357979Z","iopub.execute_input":"2023-05-05T15:25:51.358703Z","iopub.status.idle":"2023-05-05T15:40:49.863955Z","shell.execute_reply.started":"2023-05-05T15:25:51.358655Z","shell.execute_reply":"2023-05-05T15:40:49.862675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\n\noutput_filename = 'yolo_slices'\ndir_name = 'yolo_slices'\nshutil.make_archive(output_filename, 'zip', dir_name)\n\npath = '/kaggle/working/yolo_slices'\nfor file_name in os.listdir(path):\n    file = path + '/' + file_name\n    if os.path.isfile(file):\n        os.remove(file)\nos.rmdir('yolo_slices')","metadata":{"execution":{"iopub.status.busy":"2023-05-05T15:40:49.866163Z","iopub.execute_input":"2023-05-05T15:40:49.866573Z","iopub.status.idle":"2023-05-05T15:47:05.659312Z","shell.execute_reply.started":"2023-05-05T15:40:49.866529Z","shell.execute_reply":"2023-05-05T15:47:05.658005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}