{"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":"code","source":"!pip install dicom2nifti","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-09-30T19:45:21.690215Z","iopub.execute_input":"2021-09-30T19:45:21.690489Z","iopub.status.idle":"2021-09-30T19:45:28.032556Z","shell.execute_reply.started":"2021-09-30T19:45:21.690452Z","shell.execute_reply":"2021-09-30T19:45:28.031749Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dicom to nifti","metadata":{}},{"cell_type":"markdown","source":"Here we are basically converting a series of dicom files into a nifti file with each layer of the MRI scans stacked on top of each other ","metadata":{}},{"cell_type":"code","source":"import dicom2nifti","metadata":{"execution":{"iopub.status.busy":"2021-09-30T19:47:25.173117Z","iopub.execute_input":"2021-09-30T19:47:25.173444Z","iopub.status.idle":"2021-09-30T19:47:37.668299Z","shell.execute_reply.started":"2021-09-30T19:47:25.17341Z","shell.execute_reply":"2021-09-30T19:47:37.667549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport nibabel as nib\n#import itk\n#import itkwidgets\nfrom ipywidgets import interact, interactive, IntSlider, ToggleButtons\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nsns.set_style('darkgrid')\nimport matplotlib.pyplot as plt\nfrom matplotlib import cm\nimport matplotlib.animation as anim\nfrom IPython.display import Image as show_gif","metadata":{"execution":{"iopub.status.busy":"2021-09-30T19:47:49.049967Z","iopub.execute_input":"2021-09-30T19:47:49.050749Z","iopub.status.idle":"2021-09-30T19:47:49.060396Z","shell.execute_reply.started":"2021-09-30T19:47:49.050687Z","shell.execute_reply":"2021-09-30T19:47:49.059564Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ImageToGIF:\n    \"\"\"Create GIF without saving image files.\"\"\"\n    def __init__(self,\n                 size=(600, 400), \n                 xy_text=(80, 10),\n                 dpi=100, \n                 cmap='CMRmap'):\n\n        self.fig = plt.figure()\n        self.fig.set_size_inches(size[0] / dpi, size[1] / dpi)\n        self.xy_text = xy_text\n        self.cmap = cmap\n        \n        self.ax = self.fig.add_axes([0, 0, 1, 1])\n        self.ax.set_xticks([])\n        self.ax.set_yticks([])\n        self.images = []\n \n    def add(self, *args, label, with_mask=True):\n        \n        image = args[0]\n        mask = args[-1]\n        plt.set_cmap(self.cmap)\n        plt_img = self.ax.imshow(image, animated=True)\n        if with_mask:\n            plt_mask = self.ax.imshow(np.ma.masked_where(mask == False, mask),\n                                      alpha=0.7, animated=True)\n\n        plt_text = self.ax.text(*self.xy_text, label, color='red')\n        to_plot = [plt_img, plt_mask, plt_text] if with_mask else [plt_img, plt_text]\n        self.images.append(to_plot)\n        plt.close()\n \n    def save(self, filename, fps):\n        animation = anim.ArtistAnimation(self.fig, self.images)\n        animation.save(filename, writer='imagemagick', fps=fps)\n        \n        ","metadata":{"execution":{"iopub.status.busy":"2021-09-30T19:47:50.888164Z","iopub.execute_input":"2021-09-30T19:47:50.888945Z","iopub.status.idle":"2021-09-30T19:47:50.900039Z","shell.execute_reply.started":"2021-09-30T19:47:50.888903Z","shell.execute_reply":"2021-09-30T19:47:50.899197Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# T2w","metadata":{}},{"cell_type":"markdown","source":"Now let's see how the MRI scans look like, we will be starting with T2w","metadata":{}},{"cell_type":"code","source":"dicom2nifti.convert_directory(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T2w\", \"./\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_filename = \"./5_t2w.nii.gz\"","metadata":{"execution":{"iopub.status.busy":"2021-09-30T20:00:27.429784Z","iopub.execute_input":"2021-09-30T20:00:27.430579Z","iopub.status.idle":"2021-09-30T20:00:27.437063Z","shell.execute_reply.started":"2021-09-30T20:00:27.43054Z","shell.execute_reply":"2021-09-30T20:00:27.436246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_img = nib.load(sample_filename)\nsample_img = np.asanyarray(sample_img.dataobj)\n#print(\"img shape ->\", sample_img.shape)\nheight, width, depth= sample_img.shape\nprint(f\"The image object has the following dimensions: height: {height}, width:{width}, depth:{depth}\")","metadata":{"execution":{"iopub.status.busy":"2021-09-30T20:00:29.714563Z","iopub.execute_input":"2021-09-30T20:00:29.715111Z","iopub.status.idle":"2021-09-30T20:00:30.390068Z","shell.execute_reply.started":"2021-09-30T20:00:29.715073Z","shell.execute_reply":"2021-09-30T20:00:30.3891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here we will picking a random layer of the MRI scan","metadata":{}},{"cell_type":"code","source":"maxval = 409\ni = np.random.randint(0, maxval)\nprint(f\"Plotting Layer {i}\")\nplt.imshow(sample_img[:, :, i], cmap='gray')\nplt.axis('off');","metadata":{"execution":{"iopub.status.busy":"2021-09-30T20:02:30.646885Z","iopub.execute_input":"2021-09-30T20:02:30.647215Z","iopub.status.idle":"2021-09-30T20:02:30.867315Z","shell.execute_reply.started":"2021-09-30T20:02:30.647163Z","shell.execute_reply":"2021-09-30T20:02:30.865768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This is a gif of the MRI scans","metadata":{}},{"cell_type":"code","source":"sample_data_gif = ImageToGIF()\nlabel = sample_filename.replace('/', '.').split('.')[-2]\nfilename = f'{label}_3d_2d.gif'\n\nfor i in range(sample_img.shape[0]):\n    image = np.rot90(sample_img[i])\n    #mask = np.clip(np.rot90(sample_mask[i]), 0, 1)\n    sample_data_gif.add(image,label=f'{label}_{str(i)}')\n \nsample_data_gif.save(filename, fps=15)\nshow_gif(filename, format='png')","metadata":{"execution":{"iopub.status.busy":"2021-09-30T19:48:44.427234Z","iopub.execute_input":"2021-09-30T19:48:44.427819Z","iopub.status.idle":"2021-09-30T19:50:05.5095Z","shell.execute_reply.started":"2021-09-30T19:48:44.42778Z","shell.execute_reply":"2021-09-30T19:50:05.508678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# FLAIR","metadata":{}},{"cell_type":"markdown","source":"Next let's see how the FLAIR view of the MRI scans look like ","metadata":{}},{"cell_type":"code","source":"dicom2nifti.convert_directory(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/FLAIR\", \"./\")","metadata":{"execution":{"iopub.status.busy":"2021-09-30T19:50:57.317798Z","iopub.execute_input":"2021-09-30T19:50:57.318139Z","iopub.status.idle":"2021-09-30T19:51:10.349885Z","shell.execute_reply.started":"2021-09-30T19:50:57.318104Z","shell.execute_reply":"2021-09-30T19:51:10.349116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_filename = \"./4_flair.nii.gz\"","metadata":{"execution":{"iopub.status.busy":"2021-09-30T19:59:56.363162Z","iopub.execute_input":"2021-09-30T19:59:56.363916Z","iopub.status.idle":"2021-09-30T19:59:56.368694Z","shell.execute_reply.started":"2021-09-30T19:59:56.363876Z","shell.execute_reply":"2021-09-30T19:59:56.367796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_img = nib.load(sample_filename)\nsample_img = np.asanyarray(sample_img.dataobj)\n#print(\"img shape ->\", sample_img.shape)\nheight, width, depth= sample_img.shape\nprint(f\"The image object has the following dimensions: height: {height}, width:{width}, depth:{depth}\")","metadata":{"execution":{"iopub.status.busy":"2021-09-30T19:59:59.763485Z","iopub.execute_input":"2021-09-30T19:59:59.763809Z","iopub.status.idle":"2021-09-30T20:00:00.42529Z","shell.execute_reply.started":"2021-09-30T19:59:59.763771Z","shell.execute_reply":"2021-09-30T20:00:00.424325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"maxval = 400\ni = np.random.randint(0, maxval)\nprint(f\"Plotting Layer {i}\")\nplt.imshow(sample_img[:, :, i], cmap='gray')\nplt.axis('off');","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_data_gif = ImageToGIF()\nlabel = sample_filename.replace('/', '.').split('.')[-2]\nfilename = f'{label}_3d_2d_2.gif'\n\nfor i in range(sample_img.shape[0]):\n    image = np.rot90(sample_img[i])\n    #mask = np.clip(np.rot90(sample_mask[i]), 0, 1)\n    sample_data_gif.add(image,label=f'{label}_{str(i)}')\n \nsample_data_gif.save(filename, fps=15)\nshow_gif(filename, format='png')","metadata":{"execution":{"iopub.status.busy":"2021-09-30T19:52:44.779642Z","iopub.execute_input":"2021-09-30T19:52:44.780393Z","iopub.status.idle":"2021-09-30T19:54:22.311394Z","shell.execute_reply.started":"2021-09-30T19:52:44.780353Z","shell.execute_reply":"2021-09-30T19:54:22.310436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Hope this notebook was helpful to show you how our data looks like and what our model will exactly be seeing ","metadata":{}}]}