{"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":"!pip install natsort","metadata":{"execution":{"iopub.status.busy":"2024-08-11T07:20:49.396117Z","iopub.execute_input":"2024-08-11T07:20:49.396562Z","iopub.status.idle":"2024-08-11T07:21:04.213825Z","shell.execute_reply.started":"2024-08-11T07:20:49.396521Z","shell.execute_reply":"2024-08-11T07:21:04.212292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom natsort import natsorted\nimport pandas as pd\nimport numpy as np\nimport pydicom\nimport matplotlib.pyplot as plt\nimport matplotlib.animation as animation\nfrom IPython.display import HTML\nimport matplotlib as rc\n%matplotlib inline\nfrom PIL import Image","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-11T07:21:04.216668Z","iopub.execute_input":"2024-08-11T07:21:04.217071Z","iopub.status.idle":"2024-08-11T07:21:04.227737Z","shell.execute_reply.started":"2024-08-11T07:21:04.217031Z","shell.execute_reply":"2024-08-11T07:21:04.226504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images/1035170868/2484927966/10.dcm'\ndataset = pydicom.read_file(image_path)\nimg = dataset.pixel_array\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T07:21:04.229131Z","iopub.execute_input":"2024-08-11T07:21:04.229538Z","iopub.status.idle":"2024-08-11T07:21:04.679459Z","shell.execute_reply.started":"2024-08-11T07:21:04.229495Z","shell.execute_reply":"2024-08-11T07:21:04.678326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_dir_path = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images/1035170868/2484927966'\nfilepaths = []\nfilenamelists = []\ndcm_imgs = []\n\n# get image datas from directries\nfor dirname, _, filenames in os.walk(image_dir_path):\n    for filename in filenames:\n        # print(dirname, _, filename)\n        filepaths.append(os.path.join(dirname, filename))\n        filenamelists.append(filename)\nfilepaths = natsorted(set(filepaths))\n# filepaths","metadata":{"execution":{"iopub.status.busy":"2024-08-11T07:21:04.681035Z","iopub.execute_input":"2024-08-11T07:21:04.681418Z","iopub.status.idle":"2024-08-11T07:21:04.692794Z","shell.execute_reply.started":"2024-08-11T07:21:04.681384Z","shell.execute_reply":"2024-08-11T07:21:04.691563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for filepath in filepaths:\n    dataset = pydicom.read_file(filepath)\n    img = dataset.pixel_array\n    dcm_imgs.append(img)\nnp_dcm_imgs = np.array(dcm_imgs)\nnp_dcm_imgs.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-11T07:21:04.695891Z","iopub.execute_input":"2024-08-11T07:21:04.696287Z","iopub.status.idle":"2024-08-11T07:21:05.364915Z","shell.execute_reply.started":"2024-08-11T07:21:04.696254Z","shell.execute_reply":"2024-08-11T07:21:05.363680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# save gif image\ndef save_gif_image(imgs, savename ='sample', duration = 1, loop = 1):\n    gif_list = []\n    for i, img in enumerate(imgs):\n        pil_img = Image.fromarray(img)\n        gif_list.append(pil_img)\n    gif_list[0].save(f'{savename}.gif',save_all=True, append_images=gif_list[1:], optimize=True, duration=duration, loop=loop)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T07:21:05.366359Z","iopub.execute_input":"2024-08-11T07:21:05.366743Z","iopub.status.idle":"2024-08-11T07:21:05.374401Z","shell.execute_reply.started":"2024-08-11T07:21:05.366703Z","shell.execute_reply":"2024-08-11T07:21:05.373146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_gif_image(np_dcm_imgs, savename='sample', duration=500)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T07:21:05.376088Z","iopub.execute_input":"2024-08-11T07:21:05.376554Z","iopub.status.idle":"2024-08-11T07:21:05.703900Z","shell.execute_reply.started":"2024-08-11T07:21:05.376512Z","shell.execute_reply":"2024-08-11T07:21:05.702566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HTML(f'<img src = \"./sample.gif\" />')","metadata":{"execution":{"iopub.status.busy":"2024-08-11T07:21:05.705628Z","iopub.execute_input":"2024-08-11T07:21:05.706019Z","iopub.status.idle":"2024-08-11T07:21:05.713437Z","shell.execute_reply.started":"2024-08-11T07:21:05.705981Z","shell.execute_reply":"2024-08-11T07:21:05.712094Z"},"trusted":true},"execution_count":null,"outputs":[]}]}