{"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 json\nimport glob\nimport random\nimport collections\n\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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-15T13:16:12.968049Z","iopub.execute_input":"2021-07-15T13:16:12.968459Z","iopub.status.idle":"2021-07-15T13:16:14.144494Z","shell.execute_reply.started":"2021-07-15T13:16:12.968366Z","shell.execute_reply":"2021-07-15T13:16:14.143799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv\")\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-15T13:16:24.487276Z","iopub.execute_input":"2021-07-15T13:16:24.487890Z","iopub.status.idle":"2021-07-15T13:16:24.503148Z","shell.execute_reply.started":"2021-07-15T13:16:24.487857Z","shell.execute_reply":"2021-07-15T13:16:24.502444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(5, 5))\nsns.countplot(data=train_df, x=\"MGMT_value\");","metadata":{"execution":{"iopub.status.busy":"2021-07-15T13:16:42.304119Z","iopub.execute_input":"2021-07-15T13:16:42.304530Z","iopub.status.idle":"2021-07-15T13:16:42.463546Z","shell.execute_reply.started":"2021-07-15T13:16:42.304497Z","shell.execute_reply":"2021-07-15T13:16:42.462489Z"},"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\n\n\ndef visualize_sample(\n    brats21id, \n    slice_i,\n    mgmt_value,\n    types=(\"FLAIR\", \"T1w\", \"T1wCE\", \"T2w\")\n):\n    plt.figure(figsize=(16, 5))\n    patient_path = os.path.join(\n        \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/\", \n        str(brats21id).zfill(5),\n    )\n    for i, t in enumerate(types, 1):\n        t_paths = sorted(\n            glob.glob(os.path.join(patient_path, t, \"*\")), \n            key=lambda x: int(x[:-4].split(\"-\")[-1]),\n        )\n    \n        data = load_dicom(t_paths[int(len(t_paths) * slice_i)])\n        plt.subplot(1, 4, i)\n        plt.imshow(data, cmap=\"gray\")\n        plt.title(f\"{t}\", fontsize=16)\n        plt.axis(\"off\")\n\n    plt.suptitle(f\"MGMT_value: {mgmt_value}\", fontsize=16)\n    plt.show()\n# for each  patient taken one dcm file and visualized     \n    ","metadata":{"execution":{"iopub.status.busy":"2021-07-15T13:35:13.925183Z","iopub.execute_input":"2021-07-15T13:35:13.925524Z","iopub.status.idle":"2021-07-15T13:35:13.935528Z","shell.execute_reply.started":"2021-07-15T13:35:13.925494Z","shell.execute_reply":"2021-07-15T13:35:13.934561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in random.sample(range(train_df.shape[0]), 10):\n    _brats21id = train_df.iloc[i][\"BraTS21ID\"]\n    _mgmt_value = train_df.iloc[i][\"MGMT_value\"]\n    visualize_sample(brats21id=_brats21id, mgmt_value=_mgmt_value, slice_i=0.5)","metadata":{"execution":{"iopub.status.busy":"2021-07-15T13:35:14.378638Z","iopub.execute_input":"2021-07-15T13:35:14.379018Z","iopub.status.idle":"2021-07-15T13:35:19.523613Z","shell.execute_reply.started":"2021-07-15T13:35:14.378984Z","shell.execute_reply":"2021-07-15T13:35:19.522543Z"},"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-07-15T13:35:55.852383Z","iopub.execute_input":"2021-07-15T13:35:55.852859Z","iopub.status.idle":"2021-07-15T13:35:55.859852Z","shell.execute_reply.started":"2021-07-15T13:35:55.852709Z","shell.execute_reply":"2021-07-15T13:35:55.858625Z"},"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-07-15T13:36:14.641298Z","iopub.execute_input":"2021-07-15T13:36:14.641816Z","iopub.status.idle":"2021-07-15T13:36:14.648800Z","shell.execute_reply.started":"2021-07-15T13:36:14.641770Z","shell.execute_reply":"2021-07-15T13:36:14.647966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = load_dicom_line(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/FLAIR\")\ncreate_animation(images)","metadata":{"execution":{"iopub.status.busy":"2021-07-15T13:36:26.029688Z","iopub.execute_input":"2021-07-15T13:36:26.030025Z","iopub.status.idle":"2021-07-15T13:36:47.088498Z","shell.execute_reply.started":"2021-07-15T13:36:26.029997Z","shell.execute_reply":"2021-07-15T13:36:47.087645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}