{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":71549,"databundleVersionId":8561470,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport pydicom\nimport matplotlib.pyplot as plt\nimport cv2\nimport glob\nfrom tqdm.auto import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-15T05:38:10.986381Z","iopub.execute_input":"2024-08-15T05:38:10.986831Z","iopub.status.idle":"2024-08-15T05:38:12.135385Z","shell.execute_reply.started":"2024-08-15T05:38:10.986797Z","shell.execute_reply":"2024-08-15T05:38:12.134173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_DIR = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images'\nOUT_DIR = 'images'\nos.makedirs(OUT_DIR, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2024-08-15T05:38:12.137741Z","iopub.execute_input":"2024-08-15T05:38:12.138348Z","iopub.status.idle":"2024-08-15T05:38:12.145289Z","shell.execute_reply.started":"2024-08-15T05:38:12.138307Z","shell.execute_reply":"2024-08-15T05:38:12.143506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_dcm(src_path):\n    dicom_data = pydicom.dcmread(src_path)\n    image = dicom_data.pixel_array\n    image = (image - image.min()) / (image.max() - image.min() +1e-6) * 255\n    image = np.stack([image]*3, axis=-1).astype('uint8')\n    return image","metadata":{"execution":{"iopub.status.busy":"2024-08-15T05:38:12.147177Z","iopub.execute_input":"2024-08-15T05:38:12.147853Z","iopub.status.idle":"2024-08-15T05:38:12.163672Z","shell.execute_reply.started":"2024-08-15T05:38:12.147771Z","shell.execute_reply":"2024-08-15T05:38:12.162436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nall_files = glob.glob(f'{IMG_DIR}/*/*/*.dcm')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T05:38:12.166661Z","iopub.execute_input":"2024-08-15T05:38:12.167810Z","iopub.status.idle":"2024-08-15T05:38:53.742615Z","shell.execute_reply.started":"2024-08-15T05:38:12.167765Z","shell.execute_reply":"2024-08-15T05:38:53.741322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(all_files, columns=['path'])\n# df = df.iloc[:5]","metadata":{"execution":{"iopub.status.busy":"2024-08-15T05:39:37.357595Z","iopub.execute_input":"2024-08-15T05:39:37.358039Z","iopub.status.idle":"2024-08-15T05:39:37.377682Z","shell.execute_reply.started":"2024-08-15T05:39:37.358005Z","shell.execute_reply":"2024-08-15T05:39:37.376426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"studies = []\nseries = []\ninstances = []\n\nfor i, row in tqdm(df.iterrows(), total=len(df)):\n    path = row['path']\n    split = path.split('/')\n    series.append(int(split[-2]))\n    studies.append(int(split[-3]))\n    instances.append(int(split[-1][:-4]))\n    img = read_dcm(path)\n    out_dir = f\"{OUT_DIR}/{split[-3]}/{split[-2]}/\"\n    os.makedirs(out_dir, exist_ok=True)\n    out_path = os.path.join(out_dir, f\"{split[-1][:-4]}.jpg\")\n    cv2.imwrite(out_path, img)\n    \ndf['study_id'] = studies\ndf['series_id'] = series\ndf['instance_number'] = instances","metadata":{"execution":{"iopub.status.busy":"2024-08-15T05:39:37.492095Z","iopub.execute_input":"2024-08-15T05:39:37.492745Z","iopub.status.idle":"2024-08-15T05:39:37.652929Z","shell.execute_reply.started":"2024-08-15T05:39:37.492684Z","shell.execute_reply":"2024-08-15T05:39:37.640386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('all_images.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-08-15T05:40:05.730515Z","iopub.execute_input":"2024-08-15T05:40:05.730965Z","iopub.status.idle":"2024-08-15T05:40:05.744910Z","shell.execute_reply.started":"2024-08-15T05:40:05.730929Z","shell.execute_reply":"2024-08-15T05:40:05.743485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -r -q images.zip images","metadata":{"execution":{"iopub.status.busy":"2024-08-15T05:41:00.333264Z","iopub.execute_input":"2024-08-15T05:41:00.333745Z","iopub.status.idle":"2024-08-15T05:41:03.504268Z","shell.execute_reply.started":"2024-08-15T05:41:00.333690Z","shell.execute_reply":"2024-08-15T05:41:03.502360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rm -rf images","metadata":{"execution":{"iopub.status.busy":"2024-08-15T05:41:10.710607Z","iopub.execute_input":"2024-08-15T05:41:10.711126Z","iopub.status.idle":"2024-08-15T05:41:11.915742Z","shell.execute_reply.started":"2024-08-15T05:41:10.711085Z","shell.execute_reply":"2024-08-15T05:41:11.913926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}