{"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-09-26T02:47:41.995684Z","iopub.execute_input":"2024-09-26T02:47:41.996097Z","iopub.status.idle":"2024-09-26T02:47:43.109461Z","shell.execute_reply.started":"2024-09-26T02:47:41.996062Z","shell.execute_reply":"2024-09-26T02:47:43.108238Z"},"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-09-26T02:47:43.111570Z","iopub.execute_input":"2024-09-26T02:47:43.112090Z","iopub.status.idle":"2024-09-26T02:47:43.124630Z","shell.execute_reply.started":"2024-09-26T02:47:43.112055Z","shell.execute_reply":"2024-09-26T02:47:43.123289Z"},"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-09-26T02:47:43.126768Z","iopub.execute_input":"2024-09-26T02:47:43.127145Z","iopub.status.idle":"2024-09-26T02:47:43.134708Z","shell.execute_reply.started":"2024-09-26T02:47:43.127089Z","shell.execute_reply":"2024-09-26T02:47:43.133475Z"},"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-09-26T02:47:44.741764Z","iopub.execute_input":"2024-09-26T02:47:44.742166Z","iopub.status.idle":"2024-09-26T02:48:24.134934Z","shell.execute_reply.started":"2024-09-26T02:47:44.742133Z","shell.execute_reply":"2024-09-26T02:48:24.133784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_series_descriptions.csv')\nl = l[l['series_description']=='Axial T2']\nl","metadata":{"execution":{"iopub.status.busy":"2024-09-26T02:49:19.913923Z","iopub.execute_input":"2024-09-26T02:49:19.914396Z","iopub.status.idle":"2024-09-26T02:49:19.952398Z","shell.execute_reply.started":"2024-09-26T02:49:19.914360Z","shell.execute_reply":"2024-09-26T02:49:19.951290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(all_files, columns=['path'])\n\nindex = []\n\nseries_id_l = l['series_id'].astype(str).to_list()\nfor i in df.T.items():\n    if i[1]['path'].split('/')[-2] in series_id_l:\n        index.append(i[0])\ndf = df.loc[index]\nprint(index[:5])\ndf\n","metadata":{"execution":{"iopub.status.busy":"2024-09-26T03:05:15.382102Z","iopub.execute_input":"2024-09-26T03:05:15.382572Z","iopub.status.idle":"2024-09-26T03:05:31.518541Z","shell.execute_reply.started":"2024-09-26T03:05:15.382536Z","shell.execute_reply":"2024-09-26T03:05:31.517299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(index)","metadata":{"execution":{"iopub.status.busy":"2024-09-26T03:05:50.082122Z","iopub.execute_input":"2024-09-26T03:05:50.083244Z","iopub.status.idle":"2024-09-26T03:05:50.091379Z","shell.execute_reply.started":"2024-09-26T03:05:50.083171Z","shell.execute_reply":"2024-09-26T03:05:50.090029Z"},"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-09-26T03:05:51.716394Z","iopub.execute_input":"2024-09-26T03:05:51.717630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('Axial_T2_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.74491Z","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.33369Z","shell.execute_reply":"2024-08-15T05:41:03.50236Z"},"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":[]}]}