{"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":"from pathlib import Path\nfrom glob import glob\nimport pydicom\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport sys","metadata":{"execution":{"iopub.status.busy":"2021-07-14T15:21:05.264473Z","iopub.execute_input":"2021-07-14T15:21:05.264973Z","iopub.status.idle":"2021-07-14T15:21:05.460337Z","shell.execute_reply.started":"2021-07-14T15:21:05.264903Z","shell.execute_reply":"2021-07-14T15:21:05.459149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualization for FLAIR:","metadata":{}},{"cell_type":"code","source":"p = sorted(list(Path('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00006/FLAIR/').glob('*.dcm')), key=lambda path: int(path.stem.rsplit('-')[-1]))","metadata":{"execution":{"iopub.status.busy":"2021-07-14T15:34:02.216612Z","iopub.execute_input":"2021-07-14T15:34:02.217084Z","iopub.status.idle":"2021-07-14T15:34:02.228034Z","shell.execute_reply.started":"2021-07-14T15:34:02.217043Z","shell.execute_reply":"2021-07-14T15:34:02.226879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# credit: https://pydicom.github.io/pydicom/dev/auto_examples/image_processing/reslice.html\n# load the DICOM files\ndef plot_brain_3d(file):\n    files = []\n    for fname in file:\n        files.append(pydicom.dcmread(fname))\n    print(\"file count: {}\".format(len(files)))\n\n    # skip files with no SliceLocation (eg scout views)\n    slices = []\n    skipcount = 0\n    for f in files:\n        if hasattr(f, 'SliceLocation'):\n            slices.append(f)\n        else:\n            skipcount = skipcount + 1\n\n    print(\"skipped, no SliceLocation: {}\".format(skipcount))\n\n    # ensure they are in the correct order\n    slices = sorted(slices, key=lambda s: s.SliceLocation)\n\n    # pixel aspects, assuming all slices are the same\n    ps = slices[0].PixelSpacing\n    ss = slices[0].SliceThickness\n    ax_aspect = ps[1]/ps[0]\n    sag_aspect = ps[1]/ss\n    cor_aspect = ss/ps[0]\n\n    # create 3D array\n    img_shape = list(slices[0].pixel_array.shape)\n    img_shape.append(len(slices))\n    img3d = np.zeros(img_shape)\n\n    # fill 3D array with the images from the files\n    for i, s in enumerate(slices):\n        img2d = s.pixel_array\n        img3d[:, :, i] = img2d\n\n\n    # plot 3 orthogonal slices\n    plt.figure(figsize=(25,25))\n    print(\"\\033[1mBrain CT Scan in three different views (Horizontal, sagittal, coronal)\")\n\n    a1 = plt.subplot(1, 3, 1)\n    plt.imshow(img3d[:, :, img_shape[2]//2], cmap='hot')\n    a1.set_aspect(ax_aspect)\n\n    # plt.figure(figsize=(15,15))\n    a2 = plt.subplot(1, 3, 2)\n    plt.imshow(img3d[:, img_shape[1]//2, :], cmap='hot')\n    a2.set_aspect(sag_aspect)\n\n    # plt.figure(figsize=(15,15))\n    a3 = plt.subplot(1, 3, 3)\n    plt.imshow(img3d[img_shape[0]//2, :, :].T, cmap='hot')\n    a3.set_aspect(cor_aspect)\n\n    # plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T15:46:47.262758Z","iopub.execute_input":"2021-07-14T15:46:47.263220Z","iopub.status.idle":"2021-07-14T15:46:47.278303Z","shell.execute_reply.started":"2021-07-14T15:46:47.263178Z","shell.execute_reply":"2021-07-14T15:46:47.276930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_brain_3d(p)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T15:46:48.807206Z","iopub.execute_input":"2021-07-14T15:46:48.807649Z","iopub.status.idle":"2021-07-14T15:46:50.672704Z","shell.execute_reply.started":"2021-07-14T15:46:48.807612Z","shell.execute_reply":"2021-07-14T15:46:50.671364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"color:red\">Will be updating this notebook with EDA and more visualization<\\p>","metadata":{}}]}