{"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":"import os\nimport random\nimport re\nimport gc\nimport glob\nimport plotly.graph_objects as go\nimport plotly.express as px\n%matplotlib inline\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.models import *\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow import keras\nfrom sklearn.model_selection import train_test_split\nfrom keras.callbacks import EarlyStopping\nfrom keras.callbacks import ModelCheckpoint\nimport pydicom\nfrom tqdm import tqdm\nfrom matplotlib import animation, rc\nfrom IPython.display import HTML\nimport numpy as np \nimport pandas as pd ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-13T23:24:17.179465Z","iopub.execute_input":"2021-08-13T23:24:17.179838Z","iopub.status.idle":"2021-08-13T23:24:20.341091Z","shell.execute_reply.started":"2021-08-13T23:24:17.179753Z","shell.execute_reply":"2021-08-13T23:24:20.340203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Data Visualization\ndf = pd.read_csv(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv\")\npreds = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv')\ndf.drop(index=109,inplace = True)\ndf.drop(index=488,inplace = True)","metadata":{"execution":{"iopub.status.busy":"2021-08-13T23:24:30.479763Z","iopub.execute_input":"2021-08-13T23:24:30.480149Z","iopub.status.idle":"2021-08-13T23:24:30.507891Z","shell.execute_reply.started":"2021-08-13T23:24:30.480118Z","shell.execute_reply":"2021-08-13T23:24:30.507141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.histogram(df, x='MGMT_value')","metadata":{"execution":{"iopub.status.busy":"2021-08-13T23:24:35.164267Z","iopub.execute_input":"2021-08-13T23:24:35.164600Z","iopub.status.idle":"2021-08-13T23:24:36.315476Z","shell.execute_reply.started":"2021-08-13T23:24:35.164569Z","shell.execute_reply":"2021-08-13T23:24:36.314735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_dcm_info(dataset):\n    print(\"Filename.........:\", file_path)\n    print(\"Storage type.....:\", dataset.SOPClassUID)\n    print()\n\n    pat_name = dataset.PatientName\n    display_name = pat_name.family_name + \", \" + pat_name.given_name\n    print(\"Patient's name......:\", display_name)\n    print(\"Patient id..........:\", dataset.PatientID)\n    \n    if 'PixelData' in dataset:\n        rows = int(dataset.Rows)\n        cols = int(dataset.Columns)\n        print(\"Image size.......: {rows:d} x {cols:d}, {size:d} bytes\".format(\n            rows=rows, cols=cols, size=len(dataset.PixelData)))\n        if 'PixelSpacing' in dataset:\n            print(\"Pixel spacing....:\", dataset.PixelSpacing)","metadata":{"execution":{"iopub.status.busy":"2021-08-13T23:24:54.378200Z","iopub.execute_input":"2021-08-13T23:24:54.378533Z","iopub.status.idle":"2021-08-13T23:24:54.384845Z","shell.execute_reply.started":"2021-08-13T23:24:54.378502Z","shell.execute_reply":"2021-08-13T23:24:54.383825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_pixel_array(dataset, figsize=(10,10)):\n    plt.figure(figsize=figsize)\n    plt.imshow(dataset.pixel_array, cmap=plt.cm.bone)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-13T23:24:57.257731Z","iopub.execute_input":"2021-08-13T23:24:57.258131Z","iopub.status.idle":"2021-08-13T23:24:57.264570Z","shell.execute_reply.started":"2021-08-13T23:24:57.258096Z","shell.execute_reply":"2021-08-13T23:24:57.263507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 1\nnum_to_plot = 5\nfor file_name in tqdm(os.listdir('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/FLAIR')):\n    file_path = os.path.join('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/FLAIR', file_name)\n    dataset = pydicom.dcmread(file_path)\n    show_dcm_info(dataset)\n    plot_pixel_array(dataset)    \n    if i >= num_to_plot:\n        break\n    \n    i += 1","metadata":{"execution":{"iopub.status.busy":"2021-08-13T23:25:00.538361Z","iopub.execute_input":"2021-08-13T23:25:00.538708Z","iopub.status.idle":"2021-08-13T23:25:01.518504Z","shell.execute_reply.started":"2021-08-13T23:25:00.538678Z","shell.execute_reply":"2021-08-13T23:25:01.517332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"################################################################################","metadata":{}},{"cell_type":"code","source":"def 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-08-13T23:25:06.723779Z","iopub.execute_input":"2021-08-13T23:25:06.724153Z","iopub.status.idle":"2021-08-13T23:25:06.728800Z","shell.execute_reply.started":"2021-08-13T23:25:06.724121Z","shell.execute_reply":"2021-08-13T23:25:06.728012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import animation, rc\nrc('animation', html='jshtml')\n\n\ndef create_animation(ims):\n    fig = plt.figure(figsize=(6, 6))\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//24)","metadata":{"execution":{"iopub.status.busy":"2021-08-13T23:25:10.599062Z","iopub.execute_input":"2021-08-13T23:25:10.599401Z","iopub.status.idle":"2021-08-13T23:25:10.605721Z","shell.execute_reply.started":"2021-08-13T23:25:10.599370Z","shell.execute_reply":"2021-08-13T23:25:10.604791Z"},"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":{"execution":{"iopub.status.busy":"2021-08-13T23:25:13.877319Z","iopub.execute_input":"2021-08-13T23:25:13.877676Z","iopub.status.idle":"2021-08-13T23:25:13.883767Z","shell.execute_reply.started":"2021-08-13T23:25:13.877645Z","shell.execute_reply":"2021-08-13T23:25:13.882708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = load_dicom_line(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00006/FLAIR\")\ncreate_animation(images)","metadata":{"execution":{"iopub.status.busy":"2021-08-13T23:25:17.116192Z","iopub.execute_input":"2021-08-13T23:25:17.116576Z","iopub.status.idle":"2021-08-13T23:25:22.099181Z","shell.execute_reply.started":"2021-08-13T23:25:17.116544Z","shell.execute_reply":"2021-08-13T23:25:22.098222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**TO BE CONTINUED .....**\n","metadata":{}}]}