{"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 # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport glob\nimport pydicom\nimport matplotlib.pyplot as plt\nimport os\nimport seaborn as sns \nfrom matplotlib import animation, rc\nrc('animation', html='jshtml')","metadata":{"_uuid":"2b632bcd-d890-4f8d-8652-dc3b46445e36","_cell_guid":"c10a3006-0d4e-4b6c-a26b-9773dbb999d2","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-02T06:09:19.327166Z","iopub.execute_input":"2021-08-02T06:09:19.327975Z","iopub.status.idle":"2021-08-02T06:09:19.335228Z","shell.execute_reply.started":"2021-08-02T06:09:19.327923Z","shell.execute_reply":"2021-08-02T06:09:19.334345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv\")\nsample = pd.read_csv(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv\")\n\ntrain","metadata":{"execution":{"iopub.status.busy":"2021-08-02T06:09:19.336680Z","iopub.execute_input":"2021-08-02T06:09:19.337104Z","iopub.status.idle":"2021-08-02T06:09:19.364348Z","shell.execute_reply.started":"2021-08-02T06:09:19.337070Z","shell.execute_reply":"2021-08-02T06:09:19.363633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data visualization","metadata":{}},{"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","metadata":{"execution":{"iopub.status.busy":"2021-08-02T06:09:19.366143Z","iopub.execute_input":"2021-08-02T06:09:19.366461Z","iopub.status.idle":"2021-08-02T06:09:19.371397Z","shell.execute_reply.started":"2021-08-02T06:09:19.366428Z","shell.execute_reply":"2021-08-02T06:09:19.370515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_animation(ims, num, mgmt, img_type):\n    fig = plt.figure(figsize=(6, 6))\n    plt.axis('off')\n    plt.title(\"Num: \" + str(num) + \", MGMT: \" + str(mgmt) + \", Image type: \" + img_type)\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, len(ims), interval = 1000//24)","metadata":{"_uuid":"41b39218-e50a-4514-9686-91af973e1c13","_cell_guid":"6457ce71-1ec4-4c3e-b308-6cdf9a95c5e2","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-02T06:09:19.373155Z","iopub.execute_input":"2021-08-02T06:09:19.373742Z","iopub.status.idle":"2021-08-02T06:09:19.383736Z","shell.execute_reply.started":"2021-08-02T06:09:19.373706Z","shell.execute_reply":"2021-08-02T06:09:19.382926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom_line(root_path, num, img_type):\n    path = root_path + str(num).zfill(5) + \"/\" + img_type\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: continue\n        images.append(data)\n        \n    return images","metadata":{"execution":{"iopub.status.busy":"2021-08-02T06:09:19.384992Z","iopub.execute_input":"2021-08-02T06:09:19.385407Z","iopub.status.idle":"2021-08-02T06:09:19.393028Z","shell.execute_reply.started":"2021-08-02T06:09:19.385369Z","shell.execute_reply":"2021-08-02T06:09:19.392182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_path = \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/\"\nnum = 30\nimg_type = \"FLAIR\"  # \"FLAIR\", \"T1w\", \"T1wCE\", \"T2w\"\nimages = load_dicom_line(root_path, train.iloc[num][\"BraTS21ID\"], img_type)\ncreate_animation(images, train.iloc[num][\"BraTS21ID\"], train.iloc[num][\"MGMT_value\"], img_type)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T06:09:19.394381Z","iopub.execute_input":"2021-08-02T06:09:19.394809Z","iopub.status.idle":"2021-08-02T06:09:28.225713Z","shell.execute_reply.started":"2021-08-02T06:09:19.394771Z","shell.execute_reply":"2021-08-02T06:09:28.224769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\n\nScans = [\"FLAIR\",\"T1w\",\"T1wCE\",\"T2w\"]\ntrain_path = \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/\"\nallres = []\n\nfor index, num in tqdm(enumerate(train[\"BraTS21ID\"])):\n    scannum = []\n    for i in range(4):\n        scan = Scans[i]\n        path = train_path + str(num).zfill(5)\n        folderpath = os.path.join(path,scan)\n        filelist = os.listdir(folderpath)\n        scannum.append(len(filelist))\n    \n    allres.append([train[\"BraTS21ID\"].iloc[index],train[\"MGMT_value\"].iloc[index],\n                   scannum[0], scannum[1], scannum[2], scannum[3]])\n        \nallnumdf = pd.DataFrame(allres)\nallnumdf.columns = [\"BraTS21Id\", \"MGMT_value\",\"FLAIR\", \"T1w\", \"T1wCE\", \"T2w\"]\nallnumdf","metadata":{"execution":{"iopub.status.busy":"2021-08-02T06:09:28.227127Z","iopub.execute_input":"2021-08-02T06:09:28.227464Z","iopub.status.idle":"2021-08-02T06:09:29.579013Z","shell.execute_reply.started":"2021-08-02T06:09:28.227427Z","shell.execute_reply":"2021-08-02T06:09:29.578028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(5, 5))\nsns.countplot(data=allnumdf, x=\"MGMT_value\");","metadata":{"execution":{"iopub.status.busy":"2021-08-02T06:09:29.580394Z","iopub.execute_input":"2021-08-02T06:09:29.580756Z","iopub.status.idle":"2021-08-02T06:09:29.767185Z","shell.execute_reply.started":"2021-08-02T06:09:29.580718Z","shell.execute_reply":"2021-08-02T06:09:29.766198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax =plt.subplots(2,2, figsize=(10, 10))\n\nsns.distplot(allnumdf[\"FLAIR\"], ax=ax[0][0])\nsns.distplot(allnumdf[\"T1w\"], ax=ax[0][1])\nsns.distplot(allnumdf[\"T1wCE\"], ax=ax[1][0])\nsns.distplot(allnumdf[\"T2w\"], ax=ax[1][1])\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T06:09:29.771808Z","iopub.execute_input":"2021-08-02T06:09:29.773756Z","iopub.status.idle":"2021-08-02T06:09:30.486213Z","shell.execute_reply.started":"2021-08-02T06:09:29.773711Z","shell.execute_reply":"2021-08-02T06:09:30.485413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmpdf = allnumdf[allnumdf[\"MGMT_value\"]==1].reset_index(drop=True)\ntmpdf","metadata":{"execution":{"iopub.status.busy":"2021-08-02T06:09:30.487558Z","iopub.execute_input":"2021-08-02T06:09:30.488051Z","iopub.status.idle":"2021-08-02T06:09:30.504281Z","shell.execute_reply.started":"2021-08-02T06:09:30.488013Z","shell.execute_reply":"2021-08-02T06:09:30.503560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}