{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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"},{"sourceId":8488152,"sourceType":"datasetVersion","datasetId":2725849}],"dockerImageVersionId":30527,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# RSNA2024 One Study Images Slide Show \n","metadata":{"papermill":{"duration":0.006229,"end_time":"2022-12-19T12:04:21.972985","exception":false,"start_time":"2022-12-19T12:04:21.966756","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!pip install -U pylibjpeg pylibjpeg-openjpeg pylibjpeg-libjpeg pydicom python-gdcm","metadata":{"_kg_hide-output":true,"papermill":{"duration":21.850887,"end_time":"2022-12-19T12:04:43.837453","exception":false,"start_time":"2022-12-19T12:04:21.986566","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-22T16:21:08.336049Z","iopub.execute_input":"2024-05-22T16:21:08.337063Z","iopub.status.idle":"2024-05-22T16:21:30.193624Z","shell.execute_reply.started":"2024-05-22T16:21:08.337023Z","shell.execute_reply":"2024-05-22T16:21:30.191952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"    !pip install pylibjpeg --no-index --find-links=file:///kaggle/input/read-dicom-set\n    !pip install pylibjpeg-openjpeg --no-index --find-links=file:///kaggle/input/read-dicom-set\n    !pip install pylibjpeg-libjpeg --no-index --find-links=file:///kaggle/input/read-dicom-set\n    !pip install pydicom --no-index --find-links=file:///kaggle/input/read-dicom-set\n    !pip install python-gdcm --no-index --find-links=file:///kaggle/input/read-dicom-set\n    !pip install dicomsdl --no-index --find-links=file:///kaggle/input/read-dicom-set","metadata":{"execution":{"iopub.status.busy":"2023-08-06T05:37:42.467445Z","iopub.execute_input":"2023-08-06T05:37:42.468038Z","iopub.status.idle":"2023-08-06T05:39:13.370541Z","shell.execute_reply.started":"2023-08-06T05:37:42.468003Z","shell.execute_reply":"2023-08-06T05:39:13.369002Z"}}},{"cell_type":"code","source":"!pip install natsort","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport cv2\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nimport pylibjpeg\nfrom libjpeg import decode\nimport pydicom as dicom\nfrom pydicom import dcmread\nfrom pydicom.data import get_testdata_file\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\nfrom natsort import natsorted","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":true,"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":8.231928,"end_time":"2022-12-19T12:04:52.078417","exception":false,"start_time":"2022-12-19T12:04:43.846489","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-22T16:21:30.195852Z","iopub.execute_input":"2024-05-22T16:21:30.196228Z","iopub.status.idle":"2024-05-22T16:21:40.292335Z","shell.execute_reply.started":"2024-05-22T16:21:30.196195Z","shell.execute_reply":"2024-05-22T16:21:40.291181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Study ID: 100206310\n## Series ID: 1012284084, 1792451510, 2092806862","metadata":{}},{"cell_type":"code","source":"dir0='/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images/100206310/'\npaths1=[]\nfor dirname, _, filenames in os.walk(os.path.join(dir0,'1012284084')):\n    for filename in filenames:\n        paths1+=[(os.path.join(dirname, filename))]\npaths2=[]\nfor dirname, _, filenames in os.walk(os.path.join(dir0,'1792451510')):\n    for filename in filenames:\n        paths2+=[(os.path.join(dirname, filename))]\npaths3=[]\nfor dirname, _, filenames in os.walk(os.path.join(dir0,'2092806862')):\n    for filename in filenames:\n        paths3+=[(os.path.join(dirname, filename))]\n        \npaths1=natsorted(paths1)\npaths2=natsorted(paths2)\npaths3=natsorted(paths3)","metadata":{"_kg_hide-output":true,"papermill":{"duration":73.173455,"end_time":"2022-12-19T12:06:05.261398","exception":false,"start_time":"2022-12-19T12:04:52.087943","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-22T16:21:40.293650Z","iopub.execute_input":"2024-05-22T16:21:40.294369Z","iopub.status.idle":"2024-05-22T16:21:40.316918Z","shell.execute_reply.started":"2024-05-22T16:21:40.294336Z","shell.execute_reply":"2024-05-22T16:21:40.315821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read DICOM and transferred to PNG","metadata":{"papermill":{"duration":0.007921,"end_time":"2022-12-19T12:06:08.008874","exception":false,"start_time":"2022-12-19T12:06:08.000953","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def prepare_dataset(paths):\n    dataset=[]\n\n    for i,path in enumerate(paths):\n        img=dicom.dcmread(path,force=True)\n        pip=img.PhotometricInterpretation\n\n        data=img.pixel_array\n        data=data-np.min(data)\n        if np.max(data) != 0:\n            data=data/np.max(data)\n        data=(data*255).astype(np.uint8)  \n        data=cv2.cvtColor(data,cv2.COLOR_GRAY2RGB)\n        data=cv2.cvtColor(data,cv2.COLOR_BGR2GRAY)\n        data=cv2.resize(data,dsize=(224,224))\n        if pip=='MONOCHROME2':\n            data=255-data\n        ps=path.split('/')[-4:]\n        newfile=ps[-3]+'_'+ps[-2]+'_'+ps[-1][0:-4].zfill(4)+'.png'\n        cv2.imwrite(newfile,data)\n        if data.max()>0:\n            dataset+=[(newfile,data)]\n\n    return dataset\n","metadata":{"papermill":{"duration":28474.854895,"end_time":"2022-12-19T20:00:42.873706","exception":false,"start_time":"2022-12-19T12:06:08.018811","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-22T16:21:40.319221Z","iopub.execute_input":"2024-05-22T16:21:40.319553Z","iopub.status.idle":"2024-05-22T16:21:40.327947Z","shell.execute_reply.started":"2024-05-22T16:21:40.319518Z","shell.execute_reply":"2024-05-22T16:21:40.326709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset1=prepare_dataset(paths1)\ndataset2=prepare_dataset(paths2)\ndataset3=prepare_dataset(paths3)","metadata":{"execution":{"iopub.status.busy":"2024-05-22T16:21:40.329254Z","iopub.execute_input":"2024-05-22T16:21:40.329675Z","iopub.status.idle":"2024-05-22T16:21:42.218217Z","shell.execute_reply.started":"2024-05-22T16:21:40.329643Z","shell.execute_reply":"2024-05-22T16:21:42.217053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images1 = [data[1] for data in dataset1]\nimages2 = [data[1] for data in dataset2]\nimages3 = [data[1] for data in dataset3]\n","metadata":{"execution":{"iopub.status.busy":"2024-05-22T16:21:42.219572Z","iopub.execute_input":"2024-05-22T16:21:42.219902Z","iopub.status.idle":"2024-05-22T16:21:42.225204Z","shell.execute_reply.started":"2024-05-22T16:21:42.219875Z","shell.execute_reply":"2024-05-22T16:21:42.224121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Slide Show","metadata":{"papermill":{"duration":0.010102,"end_time":"2022-12-19T20:00:42.894991","exception":false,"start_time":"2022-12-19T20:00:42.884889","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from matplotlib import animation, rc\nrc('animation', html='jshtml')\n\ndef create_animation(ims):    \n    fig=plt.figure(figsize=(4,4))\n    #im=ims[0]\n    im=plt.imshow(cv2.cvtColor(ims[0],cv2.COLOR_BGR2RGB))\n    text = plt.text(0.05, 0.05, f'Slide {1}', transform=fig.transFigure, fontsize=14, color='blue')\n    def animate_func(i):\n        im.set_array(cv2.cvtColor(ims[i],cv2.COLOR_BGR2RGB))\n        text.set_text(f'Slide {i+1}')        \n        return [im] \n    plt.axis('off')\n    plt.gray()\n    plt.close()    \n\n    return animation.FuncAnimation(fig, animate_func, frames=len(ims), interval=1000//10)","metadata":{"papermill":{"duration":0.064293,"end_time":"2022-12-19T20:00:42.971953","exception":false,"start_time":"2022-12-19T20:00:42.90766","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-22T16:21:42.226785Z","iopub.execute_input":"2024-05-22T16:21:42.227211Z","iopub.status.idle":"2024-05-22T16:21:42.258058Z","shell.execute_reply.started":"2024-05-22T16:21:42.227155Z","shell.execute_reply":"2024-05-22T16:21:42.256748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_animation(images1)","metadata":{"papermill":{"duration":709.991665,"end_time":"2022-12-19T20:12:33.038611","exception":false,"start_time":"2022-12-19T20:00:43.046946","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-22T16:21:42.259673Z","iopub.execute_input":"2024-05-22T16:21:42.260056Z","iopub.status.idle":"2024-05-22T16:21:51.986631Z","shell.execute_reply.started":"2024-05-22T16:21:42.260023Z","shell.execute_reply":"2024-05-22T16:21:51.984785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_animation(images2)","metadata":{"execution":{"iopub.status.busy":"2024-05-22T16:21:51.988614Z","iopub.execute_input":"2024-05-22T16:21:51.989130Z","iopub.status.idle":"2024-05-22T16:21:52.551100Z","shell.execute_reply.started":"2024-05-22T16:21:51.989085Z","shell.execute_reply":"2024-05-22T16:21:52.549642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_animation(images3)","metadata":{"papermill":{"duration":0.693514,"end_time":"2022-12-19T20:12:35.819034","exception":false,"start_time":"2022-12-19T20:12:35.12552","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-22T16:21:52.552404Z","iopub.status.idle":"2024-05-22T16:21:52.552846Z","shell.execute_reply.started":"2024-05-22T16:21:52.552636Z","shell.execute_reply":"2024-05-22T16:21:52.552656Z"},"trusted":true},"execution_count":null,"outputs":[]}]}