{"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 numpy as np\nimport pandas as pd\nimport os, sys\nimport json\nimport glob\nimport random\nimport collections\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":"13111037-145d-4e31-b6f0-cbc80307bc48","_cell_guid":"aa937b9b-dad4-49d8-8cbc-0f933bec7915","collapsed":false,"execution":{"iopub.status.busy":"2021-08-01T04:26:16.045623Z","iopub.execute_input":"2021-08-01T04:26:16.046003Z","iopub.status.idle":"2021-08-01T04:26:16.983705Z","shell.execute_reply.started":"2021-08-01T04:26:16.045971Z","shell.execute_reply":"2021-08-01T04:26:16.982662Z"},"jupyter":{"outputs_hidden":false},"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","metadata":{"_uuid":"d03f050d-18af-4d82-853c-cea9c7932432","_cell_guid":"8675fffa-a6e8-4c1b-a546-f97fbbf0a7f5","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-01T04:27:55.547607Z","iopub.execute_input":"2021-08-01T04:27:55.548035Z","iopub.status.idle":"2021-08-01T04:27:55.564079Z","shell.execute_reply.started":"2021-08-01T04:27:55.547999Z","shell.execute_reply":"2021-08-01T04:27:55.563088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df['MGMT_value'].value_counts())\n\nplt.figure(figsize=(5, 5))\nsns.countplot(data=train_df, x=\"MGMT_value\");","metadata":{"_uuid":"40c2c99a-05c3-4d02-84ae-f7f37d71ea9c","_cell_guid":"38b0c00b-4e87-4ab3-b02c-6c928e20252e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-01T04:54:30.693429Z","iopub.execute_input":"2021-08-01T04:54:30.693826Z","iopub.status.idle":"2021-08-01T04:54:30.819716Z","shell.execute_reply.started":"2021-08-01T04:54:30.693765Z","shell.execute_reply":"2021-08-01T04:54:30.818708Z"},"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        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()","metadata":{"execution":{"iopub.status.busy":"2021-08-01T04:29:27.900335Z","iopub.execute_input":"2021-08-01T04:29:27.900717Z","iopub.status.idle":"2021-08-01T04:29:27.911873Z","shell.execute_reply.started":"2021-08-01T04:29:27.900684Z","shell.execute_reply":"2021-08-01T04:29:27.910751Z"},"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-08-01T05:00:33.657129Z","iopub.execute_input":"2021-08-01T05:00:33.657533Z","iopub.status.idle":"2021-08-01T05:00:38.912812Z","shell.execute_reply.started":"2021-08-01T05:00:33.657496Z","shell.execute_reply":"2021-08-01T05:00:38.911846Z"},"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-01T04:43:21.726310Z","iopub.execute_input":"2021-08-01T04:43:21.726647Z","iopub.status.idle":"2021-08-01T04:43:21.733808Z","shell.execute_reply.started":"2021-08-01T04:43:21.726617Z","shell.execute_reply":"2021-08-01T04:43:21.732714Z"},"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-01T04:43:37.937284Z","iopub.execute_input":"2021-08-01T04:43:37.937795Z","iopub.status.idle":"2021-08-01T04:43:37.945081Z","shell.execute_reply.started":"2021-08-01T04:43:37.937735Z","shell.execute_reply":"2021-08-01T04:43:37.944127Z"},"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-08-01T04:43:44.574618Z","iopub.execute_input":"2021-08-01T04:43:44.575198Z","iopub.status.idle":"2021-08-01T04:44:06.866840Z","shell.execute_reply.started":"2021-08-01T04:43:44.575148Z","shell.execute_reply":"2021-08-01T04:44:06.865850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = load_dicom_line(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T1w\")\ncreate_animation(images)","metadata":{"execution":{"iopub.status.busy":"2021-08-01T05:14:08.449508Z","iopub.execute_input":"2021-08-01T05:14:08.449883Z","iopub.status.idle":"2021-08-01T05:14:10.670296Z","shell.execute_reply.started":"2021-08-01T05:14:08.449845Z","shell.execute_reply":"2021-08-01T05:14:10.669186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = load_dicom_line(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T1wCE\")\ncreate_animation(images)","metadata":{"execution":{"iopub.status.busy":"2021-08-01T05:14:18.070023Z","iopub.execute_input":"2021-08-01T05:14:18.070372Z","iopub.status.idle":"2021-08-01T05:14:24.954929Z","shell.execute_reply.started":"2021-08-01T05:14:18.070342Z","shell.execute_reply":"2021-08-01T05:14:24.953960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = load_dicom_line(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T2w\")\ncreate_animation(images)","metadata":{"execution":{"iopub.status.busy":"2021-08-01T05:14:28.129227Z","iopub.execute_input":"2021-08-01T05:14:28.129716Z","iopub.status.idle":"2021-08-01T05:14:49.113820Z","shell.execute_reply.started":"2021-08-01T05:14:28.129684Z","shell.execute_reply":"2021-08-01T05:14:49.112761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}