{"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":"# Cervical Spine Fracture Dicom Image Animation","metadata":{}},{"cell_type":"code","source":"import os\nimport json\nimport glob\nimport random\nimport collections\nimport numpy as np\nimport pandas as pd\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom matplotlib import animation, rc\nrc('animation', html='jshtml')","metadata":{"papermill":{"duration":1.459643,"end_time":"2021-08-07T06:06:30.409049","exception":false,"start_time":"2021-08-07T06:06:28.949406","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-10T18:08:52.287481Z","iopub.execute_input":"2022-09-10T18:08:52.288091Z","iopub.status.idle":"2022-09-10T18:08:52.298439Z","shell.execute_reply.started":"2022-09-10T18:08:52.288029Z","shell.execute_reply":"2022-09-10T18:08:52.297387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scans = !ls ../input/rsna-2022-cervical-spine-fracture-detection/train_images/* -d\nprint(len(scans))\nprint(scans[0:3])","metadata":{"papermill":{"duration":1.288441,"end_time":"2021-08-07T06:06:31.707604","exception":false,"start_time":"2021-08-07T06:06:30.419163","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-10T18:08:52.300516Z","iopub.execute_input":"2022-09-10T18:08:52.301403Z","iopub.status.idle":"2022-09-10T18:08:53.341151Z","shell.execute_reply.started":"2022-09-10T18:08:52.301365Z","shell.execute_reply":"2022-09-10T18:08:53.3397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Select StudyInstanceUID","metadata":{}},{"cell_type":"code","source":"UID0='1.2.826.0.1.3680043.10051' # select UID\nt=0\nfor i,scan in enumerate(scans):\n    if UID0 in scan:\n        t=i\nprint(t)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T18:08:53.343045Z","iopub.execute_input":"2022-09-10T18:08:53.343468Z","iopub.status.idle":"2022-09-10T18:08:53.35242Z","shell.execute_reply.started":"2022-09-10T18:08:53.343427Z","shell.execute_reply":"2022-09-10T18:08:53.351043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files=os.listdir(scans[t])\nPath=pd.DataFrame(columns=['file','path','order'])\nPath['file']=files\nPath['path']=Path['file'].apply(lambda x:os.path.join(scans[t],x))\nPath['order']=Path['file'].apply(lambda x:('000'+x[0:-4])[-4:])\nPath=Path.sort_values('order',ascending=True)\ndisplay(Path)\npaths=Path['path'].tolist()\nprint(paths[0:5])","metadata":{"execution":{"iopub.status.busy":"2022-09-10T18:08:53.354264Z","iopub.execute_input":"2022-09-10T18:08:53.355373Z","iopub.status.idle":"2022-09-10T18:08:53.39377Z","shell.execute_reply.started":"2022-09-10T18:08:53.355327Z","shell.execute_reply":"2022-09-10T18:08:53.392492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom(path):\n    dicom=pydicom.read_file(path)\n    data=dicom.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    return data","metadata":{"papermill":{"duration":0.018097,"end_time":"2021-08-07T06:06:31.76883","exception":false,"start_time":"2021-08-07T06:06:31.750733","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-10T18:08:53.398105Z","iopub.execute_input":"2022-09-10T18:08:53.398593Z","iopub.status.idle":"2022-09-10T18:08:53.404706Z","shell.execute_reply.started":"2022-09-10T18:08:53.398549Z","shell.execute_reply":"2022-09-10T18:08:53.403911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom_line(path):\n    t_paths = sorted(\n        glob.glob(os.path.join(path, \"*\")), \n        key=lambda x: int(x[:-4].split(\"-\")[-1]),\n    )\n    images = []\n    for filename in t_paths:\n        data = load_dicom(filename)\n        if data.max() == 0:\n            continue\n        images.append(data)\n        \n    return images","metadata":{"papermill":{"duration":0.019458,"end_time":"2021-08-07T06:06:31.797959","exception":false,"start_time":"2021-08-07T06:06:31.778501","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-10T18:08:53.405967Z","iopub.execute_input":"2022-09-10T18:08:53.411811Z","iopub.status.idle":"2022-09-10T18:08:53.419586Z","shell.execute_reply.started":"2022-09-10T18:08:53.411777Z","shell.execute_reply":"2022-09-10T18:08:53.418413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_animation(ims):    \n    fig=plt.figure(figsize=(6,6))\n    plt.axis('off')\n    im=plt.imshow(ims[0],cmap=\"gray\")\n    plt.close()\n\n    def animate_func(i):\n        im.set_array(ims[i])\n        return [im]\n\n    return animation.FuncAnimation(fig, animate_func, frames=len(ims), interval=1000//10)","metadata":{"papermill":{"duration":0.018869,"end_time":"2021-08-07T06:06:31.826405","exception":false,"start_time":"2021-08-07T06:06:31.807536","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-10T18:08:53.421904Z","iopub.execute_input":"2022-09-10T18:08:53.422495Z","iopub.status.idle":"2022-09-10T18:08:53.429993Z","shell.execute_reply.started":"2022-09-10T18:08:53.422462Z","shell.execute_reply":"2022-09-10T18:08:53.428942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images=[]\nfor i in range(len(paths)):\n    images+=[load_dicom(paths[i])]","metadata":{"papermill":{"duration":1.801185,"end_time":"2021-08-07T06:06:33.665777","exception":false,"start_time":"2021-08-07T06:06:31.864592","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-10T18:08:53.432126Z","iopub.execute_input":"2022-09-10T18:08:53.432492Z","iopub.status.idle":"2022-09-10T18:08:54.60893Z","shell.execute_reply.started":"2022-09-10T18:08:53.432461Z","shell.execute_reply":"2022-09-10T18:08:54.607686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_animation(images)","metadata":{"papermill":{"duration":4.21995,"end_time":"2021-08-07T06:06:37.89574","exception":false,"start_time":"2021-08-07T06:06:33.67579","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-10T18:08:54.610626Z","iopub.execute_input":"2022-09-10T18:08:54.61098Z","iopub.status.idle":"2022-09-10T18:09:12.964021Z","shell.execute_reply.started":"2022-09-10T18:08:54.610947Z","shell.execute_reply":"2022-09-10T18:09:12.962971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### bounding_boxes","metadata":{}},{"cell_type":"code","source":"box=pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/train_bounding_boxes.csv')\ndir0='../input/rsna-2022-cervical-spine-fracture-detection/train_images/'\nbox['path']=box['slice_number'].apply(lambda x : os.path.join(dir0,UID0,str(x)+'.dcm'))\ndisplay(box)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T18:09:12.965435Z","iopub.execute_input":"2022-09-10T18:09:12.965761Z","iopub.status.idle":"2022-09-10T18:09:13.033094Z","shell.execute_reply.started":"2022-09-10T18:09:12.965731Z","shell.execute_reply":"2022-09-10T18:09:13.032297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"box0=box[box['StudyInstanceUID']==UID0]\ndisplay(box0)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T18:09:13.034775Z","iopub.execute_input":"2022-09-10T18:09:13.035121Z","iopub.status.idle":"2022-09-10T18:09:13.056015Z","shell.execute_reply.started":"2022-09-10T18:09:13.035087Z","shell.execute_reply":"2022-09-10T18:09:13.05522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def draw_box(i):\n    \n    path=box0.loc[i,'path']\n    num=box0.loc[i,'slice_number']\n    image=load_dicom(path)\n    \n    H,W=image.shape[0],image.shape[1]\n    #print(H,W)\n\n    x=box0.iloc[i,1]\n    y=box0.iloc[i,2]\n    w=box0.iloc[i,3] \n    h=box0.iloc[i,4]\n    \n    x0=(x-w/2).astype(int)\n    y0=(y-h/2).astype(int)\n    x1=(x+w/2).astype(int)\n    y1=(y+h/2).astype(int)\n    \n    #print((x0,y0),(x1,y1))\n    \n    cv2.rectangle(image,(x0,y0),(x1,y1),(0,255,0),2)                  \n    #cv2.imwrite('/kaggle/working/'+str(num)+'.png',image)        \n    #plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n    #plt.show() \n        \n    return image","metadata":{"execution":{"iopub.status.busy":"2022-09-10T18:09:13.057332Z","iopub.execute_input":"2022-09-10T18:09:13.058231Z","iopub.status.idle":"2022-09-10T18:09:13.070561Z","shell.execute_reply.started":"2022-09-10T18:09:13.058195Z","shell.execute_reply":"2022-09-10T18:09:13.069408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images2=[]\nfor i in range(len(box0)):\n    images2+=[draw_box(i)]","metadata":{"papermill":{"duration":0.070945,"end_time":"2021-08-07T06:06:38.038627","exception":false,"start_time":"2021-08-07T06:06:37.967682","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-10T18:09:13.072091Z","iopub.execute_input":"2022-09-10T18:09:13.072639Z","iopub.status.idle":"2022-09-10T18:09:32.059865Z","shell.execute_reply.started":"2022-09-10T18:09:13.072607Z","shell.execute_reply":"2022-09-10T18:09:32.058516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_animation(images2)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T18:09:32.064002Z","iopub.execute_input":"2022-09-10T18:09:32.064414Z","iopub.status.idle":"2022-09-10T18:09:33.663862Z","shell.execute_reply.started":"2022-09-10T18:09:32.06437Z","shell.execute_reply":"2022-09-10T18:09:33.662969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}