{"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":"#Is that a Homer Simpson's Brain MRI scan? \n\n![](https://slideplayer.com/7099373/24/images/slide_1.jpg)slideplayer.com","metadata":{}},{"cell_type":"markdown","source":"#Code by stpete_ishii https://www.kaggle.com/stpeteishii/siim-acr-dicom-slide-show/notebook?select=stage_2_images","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')\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2021-07-28T23:28:44.639681Z","iopub.execute_input":"2021-07-28T23:28:44.640092Z","iopub.status.idle":"2021-07-28T23:28:46.056827Z","shell.execute_reply.started":"2021-07-28T23:28:44.640005Z","shell.execute_reply":"2021-07-28T23:28:46.055807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by stpete_ishii https://www.kaggle.com/stpeteishii/siim-acr-dicom-slide-show/notebook?select=stage_2_images\n\nscans = !ls ../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00688/T2w/ -d\nprint(len(scans))\nprint(scans[0])","metadata":{"execution":{"iopub.status.busy":"2021-07-28T23:33:38.964826Z","iopub.execute_input":"2021-07-28T23:33:38.965258Z","iopub.status.idle":"2021-07-28T23:33:39.000167Z","shell.execute_reply.started":"2021-07-28T23:33:38.965226Z","shell.execute_reply":"2021-07-28T23:33:38.998861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by stpete_ishii https://www.kaggle.com/stpeteishii/siim-acr-dicom-slide-show/notebook?select=stage_2_images\n\nfiles=os.listdir('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00688/T2w')\nprint(len(files))\nn=len(files)\nN=[]\nfor i in range(n//10):\n    N+=[i*10]","metadata":{"execution":{"iopub.status.busy":"2021-07-28T23:34:47.773790Z","iopub.execute_input":"2021-07-28T23:34:47.774174Z","iopub.status.idle":"2021-07-28T23:34:47.861238Z","shell.execute_reply.started":"2021-07-28T23:34:47.774139Z","shell.execute_reply":"2021-07-28T23:34:47.860064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by stpete_ishii https://www.kaggle.com/stpeteishii/siim-acr-dicom-slide-show/notebook?select=stage_2_images\n\n\ndef load_dicom(path):\n    dicom=pydicom.read_file(path,force=True)\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":{"execution":{"iopub.status.busy":"2021-07-28T23:35:06.658961Z","iopub.execute_input":"2021-07-28T23:35:06.659413Z","iopub.status.idle":"2021-07-28T23:35:06.666386Z","shell.execute_reply.started":"2021-07-28T23:35:06.659362Z","shell.execute_reply":"2021-07-28T23:35:06.665045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by stpete_ishii https://www.kaggle.com/stpeteishii/siim-acr-dicom-slide-show/notebook?select=stage_2_images\n\ndef load_dicom_line(path):\n    t_paths = sorted(\n        glob.glob(os.path.join(path,\"*\")), \n        key=lambda x: x[:-4].split(\"_\")[-1],\n    )\n    images = []\n    for filename in tqdm(np.array(t_paths)[N]):\n        data = load_dicom(filename)\n        if data.max() == 0:\n            continue\n        images.append(data)\n        \n    return images","metadata":{"execution":{"iopub.status.busy":"2021-07-28T23:35:23.866463Z","iopub.execute_input":"2021-07-28T23:35:23.867116Z","iopub.status.idle":"2021-07-28T23:35:23.874424Z","shell.execute_reply.started":"2021-07-28T23:35:23.867062Z","shell.execute_reply":"2021-07-28T23:35:23.873196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by stpete_ishii https://www.kaggle.com/stpeteishii/siim-acr-dicom-slide-show/notebook?select=stage_2_images\n\ndef create_animation(ims):\n    fig=plt.figure(figsize=(7,7))\n    plt.axis('off')\n    im=plt.imshow(ims[0],cmap=\"gray\")\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)","metadata":{"execution":{"iopub.status.busy":"2021-07-28T23:35:41.506657Z","iopub.execute_input":"2021-07-28T23:35:41.507293Z","iopub.status.idle":"2021-07-28T23:35:41.513229Z","shell.execute_reply.started":"2021-07-28T23:35:41.507212Z","shell.execute_reply":"2021-07-28T23:35:41.512409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = load_dicom_line(scans[0])","metadata":{"execution":{"iopub.status.busy":"2021-07-28T23:36:03.116033Z","iopub.execute_input":"2021-07-28T23:36:03.116662Z","iopub.status.idle":"2021-07-28T23:36:03.780744Z","shell.execute_reply.started":"2021-07-28T23:36:03.116599Z","shell.execute_reply":"2021-07-28T23:36:03.779669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by stpete_ishii https://www.kaggle.com/stpeteishii/siim-acr-dicom-slide-show/notebook?select=stage_2_images\n\ncreate_animation(images)","metadata":{"execution":{"iopub.status.busy":"2021-07-28T23:36:46.483468Z","iopub.execute_input":"2021-07-28T23:36:46.483922Z","iopub.status.idle":"2021-07-28T23:36:48.532092Z","shell.execute_reply.started":"2021-07-28T23:36:46.483881Z","shell.execute_reply":"2021-07-28T23:36:48.531056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Thank you stpete_ishii for the script  https://www.kaggle.com/stpeteishii/siim-acr-dicom-slide-show/notebook?select=stage_2_images","metadata":{}}]}