{"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 glob\nimport pandas as pd\npd.set_option('display.max_colwidth', -1)  \nimport numpy as np\nfrom tqdm import tqdm\n\nimport matplotlib.pyplot as plt\nimport matplotlib\nmatplotlib.rcParams['animation.html'] = 'jshtml'\nimport seaborn as sns\n\nimport tensorflow as tf\nimport tensorflow.keras as keras\n\nimport pydicom","metadata":{"execution":{"iopub.status.busy":"2021-11-02T02:38:44.501704Z","iopub.execute_input":"2021-11-02T02:38:44.50221Z","iopub.status.idle":"2021-11-02T02:38:52.119469Z","shell.execute_reply.started":"2021-11-02T02:38:44.502165Z","shell.execute_reply":"2021-11-02T02:38:52.118224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/'\n\ndf_train = pd.read_csv(data_dir+'train_labels.csv')\n","metadata":{"execution":{"iopub.status.busy":"2021-11-02T02:38:56.004583Z","iopub.execute_input":"2021-11-02T02:38:56.005198Z","iopub.status.idle":"2021-11-02T02:38:56.043232Z","shell.execute_reply.started":"2021-11-02T02:38:56.005139Z","shell.execute_reply":"2021-11-02T02:38:56.042358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"execution":{"iopub.status.busy":"2021-11-02T02:39:00.691571Z","iopub.execute_input":"2021-11-02T02:39:00.692434Z","iopub.status.idle":"2021-11-02T02:39:00.725057Z","shell.execute_reply.started":"2021-11-02T02:39:00.692375Z","shell.execute_reply":"2021-11-02T02:39:00.724228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\n","metadata":{}},{"cell_type":"markdown","source":"One image reading ","metadata":{}},{"cell_type":"code","source":"data = pydicom.dcmread('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T1w/Image-24.dcm')\ndata","metadata":{"execution":{"iopub.status.busy":"2021-11-01T15:15:08.604383Z","iopub.execute_input":"2021-11-01T15:15:08.605116Z","iopub.status.idle":"2021-11-01T15:15:08.638966Z","shell.execute_reply.started":"2021-11-01T15:15:08.605061Z","shell.execute_reply":"2021-11-01T15:15:08.637925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-11-01T15:48:19.358977Z","iopub.execute_input":"2021-11-01T15:48:19.359511Z","iopub.status.idle":"2021-11-01T15:48:19.363403Z","shell.execute_reply.started":"2021-11-01T15:48:19.359475Z","shell.execute_reply":"2021-11-01T15:48:19.362643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Making all BraTS21ID of same length by adding leading zero if required.\n","metadata":{}},{"cell_type":"code","source":"''' brats = list(train_data[\"BraTS21ID\"])\n        mgmt = list(train_data[\"MGMT_value\"])\n        for b, m in zip(brats, mgmt):\n            self.labels[str(b).zfill(5)] = m'''","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_same_length(patient_id):\n    zeros = 5 - len(str(patient_id))\n    if zeros > 0:\n        prefix = ''.join(['0' for i in range(zeros)])\n    else:\n        return patient_id\n    return prefix+str(patient_id)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T02:39:05.584577Z","iopub.execute_input":"2021-11-02T02:39:05.585177Z","iopub.status.idle":"2021-11-02T02:39:05.591381Z","shell.execute_reply.started":"2021-11-02T02:39:05.585139Z","shell.execute_reply":"2021-11-02T02:39:05.590385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"# Add the full paths for each id for different types of sequences to the csv \n\n\ndf_train['BraTS21ID'] = df_train['BraTS21ID'].apply(make_same_length)\n\n# Add all the paths to the  for easy access\ndf_train['flair'] = df_train['BraTS21ID'].apply(lambda file_id : data_dir+'train/'+file_id+'/FLAIR/')\ndf_train['t1w'] = df_train['BraTS21ID'].apply(lambda file_id : data_dir+'train/'+file_id+'/T1w/')\ndf_train['t1wce'] = df_train['BraTS21ID'].apply(lambda file_id : data_dir+'train/'+file_id+'/T1wCE/')\ndf_train['t2w'] = df_train['BraTS21ID'].apply(lambda file_id : data_dir+'train/'+file_id+'/T2w/')\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-02T02:39:08.758089Z","iopub.execute_input":"2021-11-02T02:39:08.758585Z","iopub.status.idle":"2021-11-02T02:39:08.789626Z","shell.execute_reply.started":"2021-11-02T02:39:08.75854Z","shell.execute_reply":"2021-11-02T02:39:08.788427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.read_csv(data_dir+'sample_submission.csv')\n\ndf_test['BraTS21ID'] = df_test['BraTS21ID'].apply(make_same_length)\n\n# Add all the paths to the df for easy access\ndf_test['flair'] = df_test['BraTS21ID'].apply(lambda file_id : data_dir+'test/'+file_id+'/FLAIR/')\ndf_test['t1w'] = df_test['BraTS21ID'].apply(lambda file_id : data_dir+'test/'+file_id+'/T1w/')\ndf_test['t1wce'] = df_test['BraTS21ID'].apply(lambda file_id : data_dir+'test/'+file_id+'/T1wCE/')\ndf_test['t2w'] = df_test['BraTS21ID'].apply(lambda file_id : data_dir+'test/'+file_id+'/T2w/')\n","metadata":{"execution":{"iopub.status.busy":"2021-11-02T02:39:50.152522Z","iopub.execute_input":"2021-11-02T02:39:50.153996Z","iopub.status.idle":"2021-11-02T02:39:50.173653Z","shell.execute_reply.started":"2021-11-02T02:39:50.153915Z","shell.execute_reply":"2021-11-02T02:39:50.172411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"flair = sorted(glob.glob(f\"{df_train['t1wce'][8]}/*.dcm\"))\nprint(len(flair))","metadata":{"execution":{"iopub.status.busy":"2021-11-02T02:39:53.015203Z","iopub.execute_input":"2021-11-02T02:39:53.015774Z","iopub.status.idle":"2021-11-02T02:39:53.055293Z","shell.execute_reply.started":"2021-11-02T02:39:53.015734Z","shell.execute_reply":"2021-11-02T02:39:53.054447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nimport os\nimport glob\nfrom tqdm import tqdm_notebook as tqdm\nimport random\nimport numpy as np\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import Dataset, DataLoader\nimport torch.nn.functional as F\nfrom torchvision import transforms, utils\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport cv2\n","metadata":{"execution":{"iopub.status.busy":"2021-11-02T02:39:56.069092Z","iopub.execute_input":"2021-11-02T02:39:56.069864Z","iopub.status.idle":"2021-11-02T02:39:57.829098Z","shell.execute_reply.started":"2021-11-02T02:39:56.069809Z","shell.execute_reply":"2021-11-02T02:39:57.827856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef get_image(path, voi_lut=True, fix_monochrome=True,img_size=256):\n    '''\n    Returns the image data as a numpy array.\n    ''' \n    data = pydicom.dcmread(path)\n    if voi_lut:\n        img = apply_voi_lut(data.pixel_array, data)\n    else:\n        img = data.pixel_array\n    # depending on this value, X-ray may look inverted - fix that:\n    if fix_monochrome and data.PhotometricInterpretation == \"MONOCHROME1\":\n        img = np.amax(img) - img\n    img = img - np.min(img)\n    img = img / np.max(img)\n    img = (img * 255).astype(np.uint8)\n    img = cv2.resize(img, (img_size, img_size))\n        \n    return img","metadata":{"execution":{"iopub.status.busy":"2021-11-02T02:40:00.612343Z","iopub.execute_input":"2021-11-02T02:40:00.613Z","iopub.status.idle":"2021-11-02T02:40:00.629386Z","shell.execute_reply.started":"2021-11-02T02:40:00.612926Z","shell.execute_reply":"2021-11-02T02:40:00.626613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"https://www.kaggle.com/xuxu1234/efficientnet3d-for-mri","metadata":{}},{"cell_type":"code","source":"def load_3d_dicom_images(row):\n    \"\"\"\n    we will use some heuristics to choose the slices to avoid any numpy zero matrix (if possible)\n    \"\"\"\n    flair = sorted(glob.glob(f\"{row['flair']}/*.dcm\"))\n    t1w = sorted(glob.glob(f\"{row['t1w']}/*.dcm\"))\n    t1wce = sorted(glob.glob(f\"{row['t1wce']}/*.dcm\"))\n    t2w = sorted(glob.glob(f\"{row['t2w']}/*.dcm\"))\n    img_size=256\n    \n    flair_img = np.array([get_image(a) for a in flair[len(flair)//2 - 25:len(flair)//2 + 25]]).T\n    \n    if flair_img.shape[-1] < 50:\n        n_zero = 50 - flair_img.shape[-1]\n        flair_img = np.concatenate((flair_img, np.zeros((img_size, img_size, n_zero))), axis = -1)\n    #print(flair_img.shape)\n        \n    \n    \n    t1w_img = np.array([get_image(a) for a in t1w[len(t1w)//2 - 25:len(t1w)//2 + 25]]).T\n    if t1w_img.shape[-1] < 50:\n        n_zero = 50 - t1w_img.shape[-1]\n        t1w_img = np.concatenate((t1w_img, np.zeros((img_size, img_size, n_zero))), axis = -1)\n    #print(t1w_img.shape)\n    \n    \n    t1wce_img = np.array([get_image(a) for a in t1wce[len(t1wce)//2 - 25:len(t1wce)//2 + 25]]).T\n    if t1wce_img.shape[-1] < 50:\n        n_zero = 50 - t1wce_img.shape[-1]\n        t1wce_img = np.concatenate((t1wce_img, np.zeros((img_size, img_size, n_zero))), axis = -1)\n    #print(t1wce_img.shape)\n    \n    \n    t2w_img = np.array([get_image(a) for a in t2w[len(t2w)//2 - 25:len(t2w)//2 + 25]]).T\n    if t2w_img.shape[-1] < 50:\n        n_zero = 50 - t2w_img.shape[-1]\n        t2w_img = np.concatenate((t2w_img, np.zeros((img_size, img_size, n_zero))), axis = -1)\n    #print(t2w_img.shape)\n    \n    #return np.concatenate((flair_img, t1w_img, t1wce_img, t2w_img), axis = -1)\n    return (flair_img, t1w_img, t1wce_img, t2w_img)\n    #return flair_img","metadata":{"execution":{"iopub.status.busy":"2021-11-02T03:58:46.375275Z","iopub.execute_input":"2021-11-02T03:58:46.375858Z","iopub.status.idle":"2021-11-02T03:58:46.391176Z","shell.execute_reply.started":"2021-11-02T03:58:46.37582Z","shell.execute_reply":"2021-11-02T03:58:46.390441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#load_3d_dicom_images(df_train.iloc[i])\npaths=[]\nfor i in range(len(df_train)):\n    paths.append(df_train.iloc[i])\n    \n#load_3d_dicom_images(paths[0])           \n               ","metadata":{"execution":{"iopub.status.busy":"2021-11-02T03:29:43.082615Z","iopub.execute_input":"2021-11-02T03:29:43.083144Z","iopub.status.idle":"2021-11-02T03:29:43.175004Z","shell.execute_reply.started":"2021-11-02T03:29:43.08311Z","shell.execute_reply":"2021-11-02T03:29:43.173728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(mri_images)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T03:56:31.187875Z","iopub.execute_input":"2021-11-02T03:56:31.188377Z","iopub.status.idle":"2021-11-02T03:56:31.196699Z","shell.execute_reply.started":"2021-11-02T03:56:31.188304Z","shell.execute_reply":"2021-11-02T03:56:31.195405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" #mri_images = tf.nest.map_structure(load_3d_dicom_images, paths[:40] )\n #print(mri_images[0][1][1][1]  )","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:02:08.372665Z","iopub.execute_input":"2021-11-02T04:02:08.372995Z","iopub.status.idle":"2021-11-02T04:02:08.37775Z","shell.execute_reply.started":"2021-11-02T04:02:08.372965Z","shell.execute_reply":"2021-11-02T04:02:08.376369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"flair_img=[]\nflair_img_label=[]\nt1w_img=[]\nt1w_img_label=[]\nt1wce_img=[]\nt1wce_img_label=[]\nt2w_img=[]\nt2w_img_label=[]\nprint(df_train.iloc[0])\nfor i in range(50):\n    images=load_3d_dicom_images(df_train.iloc[i])\n    flair_img.append(images[0])\n    flair_img_label.append(df_train.iloc[i][1])\n    '''t1w_img.append(images[0])\n    t1w_img_label.append(df_train.iloc[i][1])\n    t1wce_img.append(images[0])\n    t1wce_img_label.append(df_train.iloc[i][1])\n    t2w_img.append(images[0])\n    t2w_img_label.append(df_train.iloc[i][1])'''\n","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:00:47.494987Z","iopub.execute_input":"2021-11-02T04:00:47.495675Z","iopub.status.idle":"2021-11-02T04:02:08.280487Z","shell.execute_reply.started":"2021-11-02T04:00:47.495605Z","shell.execute_reply":"2021-11-02T04:02:08.278958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(50):\n    tf.convert_to_tensor(an_array)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:02:23.708097Z","iopub.execute_input":"2021-11-02T04:02:23.708509Z","iopub.status.idle":"2021-11-02T04:02:23.716841Z","shell.execute_reply.started":"2021-11-02T04:02:23.708474Z","shell.execute_reply":"2021-11-02T04:02:23.715392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain_data=tf.concat(np.array(flair_img),0)\n\nlabel=tf.convert_to_tensor(flair_img_label, dtype=tf.float32)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:44:34.478961Z","iopub.execute_input":"2021-11-02T04:44:34.479538Z","iopub.status.idle":"2021-11-02T04:44:35.323533Z","shell.execute_reply.started":"2021-11-02T04:44:34.4795Z","shell.execute_reply":"2021-11-02T04:44:35.322152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import *\nfrom tensorflow.keras.models import *","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:05:38.405615Z","iopub.execute_input":"2021-11-02T04:05:38.406419Z","iopub.status.idle":"2021-11-02T04:05:38.412943Z","shell.execute_reply.started":"2021-11-02T04:05:38.40636Z","shell.execute_reply":"2021-11-02T04:05:38.411599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def MRIModel():\n    base_model = tf.keras.applications.EfficientNetB0(include_top=False, weights='imagenet')\n    base_model.trainabe = False\n\n    inputs = Input((256, 256, 50))\n    x = Conv2D(3, kernel_size=(3, 3), padding='same', activation='relu')(inputs)\n    x = base_model(x, training=False)\n    flattened_output = GlobalAveragePooling2D()(x)\n    outputs = Dense(1, activation='sigmoid', name=\"lstm_sigmoid\")(flattened_output)\n    return Model(inputs, outputs)\n\ntf.keras.backend.clear_session()\nmodel = MRIModel()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:05:41.022665Z","iopub.execute_input":"2021-11-02T04:05:41.023127Z","iopub.status.idle":"2021-11-02T04:05:44.711146Z","shell.execute_reply.started":"2021-11-02T04:05:41.023089Z","shell.execute_reply":"2021-11-02T04:05:44.710203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Callbacks\nearlystopper = tf.keras.callbacks.EarlyStopping(\n    monitor='val_loss', patience=5, verbose=0, mode='min',\n    restore_best_weights=True\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:10:08.081606Z","iopub.execute_input":"2021-11-02T04:10:08.082067Z","iopub.status.idle":"2021-11-02T04:10:08.08809Z","shell.execute_reply.started":"2021-11-02T04:10:08.082025Z","shell.execute_reply":"2021-11-02T04:10:08.086908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import wandb\nprint('W&B version: ', wandb.__version__)\nfrom wandb.keras import WandbCallback\n\nwandb.login()","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:08:34.475307Z","iopub.execute_input":"2021-11-02T04:08:34.475788Z","iopub.status.idle":"2021-11-02T04:10:04.737149Z","shell.execute_reply.started":"2021-11-02T04:08:34.475751Z","shell.execute_reply":"2021-11-02T04:10:04.7334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.backend.clear_session() \nmodel = MRIModel()\nmodel.compile('adam', 'binary_crossentropy', metrics=['acc'])\n\n\n\n# Train\n_ = model.fit(train_data,label, \n              epochs=10,\n              validation_data=(train_data[:20],label[:20]),\n              callbacks=[earlystopper])\n\n# Evaluate\nloss, acc = model.evaluate((flair_img[:10],flair_img_label[:10]))\n#wandb.log({'Val Accuracy': round(acc, 3)})\n\n","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:50:32.3013Z","iopub.execute_input":"2021-11-02T04:50:32.301831Z","iopub.status.idle":"2021-11-02T04:53:56.939196Z","shell.execute_reply.started":"2021-11-02T04:50:32.301791Z","shell.execute_reply":"2021-11-02T04:53:56.93749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for index, row in df_train.iterrows():\n    #print(row)\n    #=load_3d_dicom_images(path)","metadata":{"execution":{"iopub.status.busy":"2021-11-01T17:29:11.012862Z","iopub.execute_input":"2021-11-01T17:29:11.013273Z","iopub.status.idle":"2021-11-01T17:29:11.020307Z","shell.execute_reply.started":"2021-11-01T17:29:11.013212Z","shell.execute_reply":"2021-11-01T17:29:11.018784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\nload the images","metadata":{}},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-11-01T21:46:08.053011Z","iopub.execute_input":"2021-11-01T21:46:08.053398Z","iopub.status.idle":"2021-11-01T21:46:08.066015Z","shell.execute_reply.started":"2021-11-01T21:46:08.053362Z","shell.execute_reply":"2021-11-01T21:46:08.063847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.loc[[1,20]].head()\ndf_valid = df_train.loc[df_train.iloc[int(len(df_train.index)*0.2):  ].index.tolist()]\ndf_valid.iloc[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-01T22:06:10.091977Z","iopub.execute_input":"2021-11-01T22:06:10.092624Z","iopub.status.idle":"2021-11-01T22:06:10.107689Z","shell.execute_reply.started":"2021-11-01T22:06:10.092554Z","shell.execute_reply":"2021-11-01T22:06:10.106623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''# let's write a simple pytorch dataloader\n\n\nclass BrainTumor(Dataset):\n    def __init__(self, split = \"train\", validation_split = 0.0):\n        # labels    \n        self.split=\"train\"\n        if split == \"valid\":\n            \n            self.idx = df_train.iloc[:int(len(df_train.index)*validation_split)  ].index.tolist() # first 20% as validation\n            self.df = df_train.loc[self.idx]\n        elif split == \"train\":\n            self.idx = df_train.iloc[int(len(df_train.index)*validation_split): ].index.tolist() # last 80% as train\n            self.df=df_train.loc[self.idx]\n        else:\n            self.df = df_test.iloc[int(len(df_test.index)) ].index.tolist()\n    def __len__(self):\n        return len(self.idx )\n    \n    def __getitem__(self, idx):\n        imgs = load_3d_dicom_images(self.df.iloc[idx])\n        #imgs=np.concatenate(imgs, axis = -1)\n        #transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,) * 200, (0.5,) * 200)])\n        #imgs = transform(imgs)\n        label = self.labels[self.ids[idx]]\n        if self.split != \"test\":\n            return torch.tensor(imgs, dtype = torch.float32))\n        else:\n            return torch.tensor(imgs, dtype = torch.float32)\n''","metadata":{"execution":{"iopub.status.busy":"2021-11-01T22:18:23.909989Z","iopub.execute_input":"2021-11-01T22:18:23.910644Z","iopub.status.idle":"2021-11-01T22:18:23.925397Z","shell.execute_reply.started":"2021-11-01T22:18:23.910585Z","shell.execute_reply":"2021-11-01T22:18:23.923658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.iloc[1][1]","metadata":{"execution":{"iopub.status.busy":"2021-11-01T21:51:57.941176Z","iopub.execute_input":"2021-11-01T21:51:57.941779Z","iopub.status.idle":"2021-11-01T21:51:57.948375Z","shell.execute_reply.started":"2021-11-01T21:51:57.941742Z","shell.execute_reply":"2021-11-01T21:51:57.947514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.iloc[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-01T21:33:04.820931Z","iopub.execute_input":"2021-11-01T21:33:04.821431Z","iopub.status.idle":"2021-11-01T21:33:04.83108Z","shell.execute_reply.started":"2021-11-01T21:33:04.821388Z","shell.execute_reply":"2021-11-01T21:33:04.830108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# testing the dataloader\ntrain_dataset = BrainTumor()\ntrain_loader = DataLoader(train_dataset, batch_size=2, shuffle=True, num_workers=8)\n# val_dataset = BrainTumor(split=\"valid\")\n# val_loader = DataLoader(val_dataset, batch_size=2, shuffle=False, num_workers=8)","metadata":{"execution":{"iopub.status.busy":"2021-11-01T22:18:29.227119Z","iopub.execute_input":"2021-11-01T22:18:29.227494Z","iopub.status.idle":"2021-11-01T22:18:29.235359Z","shell.execute_reply.started":"2021-11-01T22:18:29.227462Z","shell.execute_reply":"2021-11-01T22:18:29.234021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader","metadata":{"execution":{"iopub.status.busy":"2021-11-01T22:01:26.221141Z","iopub.execute_input":"2021-11-01T22:01:26.221533Z","iopub.status.idle":"2021-11-01T22:01:26.229205Z","shell.execute_reply.started":"2021-11-01T22:01:26.221496Z","shell.execute_reply":"2021-11-01T22:01:26.227923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img in train_loader:\n    print(img.shape)\n    \n    break","metadata":{"execution":{"iopub.status.busy":"2021-11-01T22:18:32.895561Z","iopub.execute_input":"2021-11-01T22:18:32.895992Z","iopub.status.idle":"2021-11-01T22:18:38.045645Z","shell.execute_reply.started":"2021-11-01T22:18:32.895956Z","shell.execute_reply":"2021-11-01T22:18:38.039977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Displaying one image","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(10, 7))\n\n  \n#data = pydicom.dcmread('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T1w/Image-25.dcm')\nimg1 = get_image('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T1w/Image-25.dcm', True)\nfig.add_subplot(2, 2, 1)\nplt.imshow(img1, cmap='gray')\nplt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2021-11-01T17:36:08.915415Z","iopub.execute_input":"2021-11-01T17:36:08.916005Z","iopub.status.idle":"2021-11-01T17:36:09.005982Z","shell.execute_reply.started":"2021-11-01T17:36:08.91596Z","shell.execute_reply":"2021-11-01T17:36:09.005055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfig = plt.figure(figsize=(10, 7))\n\n  \ndata = pydicom.dcmread('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T1w/Image-1.dcm')\nimg1 = get_image(data)\nfig.add_subplot(2, 2, 1)\nplt.imshow(img1, cmap='gray')\nplt.axis('off')\ndata = pydicom.dcmread('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T1w/Image-20.dcm')\nimg2 = get_image(data)\nfig.add_subplot(2, 2, 2)\nplt.imshow(img2, cmap='gray')\nplt.axis('off')\ndata = pydicom.dcmread('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T1w/Image-24.dcm')\nimg3 = get_image(data)\nfig.add_subplot(2, 2, 3)\nplt.imshow(img3, cmap='gray')\nplt.axis('off')\ndata = pydicom.dcmread('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T1w/Image-30.dcm')\nimg3 = get_image(data)\nfig.add_subplot(2, 2, 4)\nplt.imshow(img3, cmap='gray')\nplt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2021-11-01T16:14:52.315084Z","iopub.execute_input":"2021-11-01T16:14:52.31549Z","iopub.status.idle":"2021-11-01T16:14:52.596811Z","shell.execute_reply.started":"2021-11-01T16:14:52.315457Z","shell.execute_reply":"2021-11-01T16:14:52.59591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data.pixel_array.shape)\nprint(data.pixel_array[300,200])\nprint(np.max(data.pixel_array))","metadata":{"execution":{"iopub.status.busy":"2021-11-01T16:15:01.200591Z","iopub.execute_input":"2021-11-01T16:15:01.200944Z","iopub.status.idle":"2021-11-01T16:15:01.207099Z","shell.execute_reply.started":"2021-11-01T16:15:01.200897Z","shell.execute_reply":"2021-11-01T16:15:01.206312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-11-01T15:17:27.922267Z","iopub.execute_input":"2021-11-01T15:17:27.922612Z","iopub.status.idle":"2021-11-01T15:17:28.251031Z","shell.execute_reply.started":"2021-11-01T15:17:27.92258Z","shell.execute_reply":"2021-11-01T15:17:28.249796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sorted_image_dirs(path: str):\n    '''\n    Sorts the list of image directories by image number in a path\n    '''\n    dirs = glob.glob(path+'*')\n    dirs.sort(key=lambda x: int(x.split('/')[-1].split('-')[-1].split('.')[0]))\n    \n    return dirs\n\n\ndef get_all_images(path: str):\n    '''\n    Returns a list of (non blank) images from a given path (of shape [non_blank_image_count, 512, 512])\n    '''\n    image_dirs = sorted_image_dirs(path)\n    images = []\n    \n    for directory in image_dirs:\n        data = pydicom.dcmread(directory)\n        img = get_image(data)\n        \n        # Exclude the blank images\n        if np.max(img)!=0:\n            images.append(img)\n        else:\n            pass\n    \n    return images\n    \ndef show_animation(images: list):\n    '''\n    Displays an animation from the list of images.\n    \n    set: matplotlib.rcParams['animation.html'] = 'jshtml'\n    \n    '''\n    fig = plt.figure(figsize=(6, 6))\n    plt.axis('off')\n    im = plt.imshow(images[0], cmap='gray')\n    \n    def animate_func(i):\n        im.set_array(images[i])\n        return [im]\n    \n    return matplotlib.animation.FuncAnimation(fig, animate_func, frames = len(images), interval = 20)","metadata":{"execution":{"iopub.status.busy":"2021-10-07T16:50:46.565831Z","iopub.execute_input":"2021-10-07T16:50:46.566246Z","iopub.status.idle":"2021-10-07T16:50:46.57741Z","shell.execute_reply.started":"2021-10-07T16:50:46.566211Z","shell.execute_reply":"2021-10-07T16:50:46.576217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Show FLAIR Sequence of a Patient as Images","metadata":{}},{"cell_type":"code","source":"# A random patient - patient 10\npatient = 10\n\nflair_images = get_all_images(df['flair'][patient])\nprint('No of images:', len(flair_images))\nprint('MGMT: ', df['MGMT_value'][patient])\n\nfig = plt.figure(figsize=(30,30))\n\nc = 1\nfor image in flair_images:\n    ax = fig.add_subplot(len(flair_images)//10+1, 10, c)\n    ax.imshow(image, cmap='gray')\n    c+=1\n    \n    plt.axis('off')\n    \nfig.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2021-10-07T16:45:26.553197Z","iopub.execute_input":"2021-10-07T16:45:26.553618Z","iopub.status.idle":"2021-10-07T16:45:26.591515Z","shell.execute_reply.started":"2021-10-07T16:45:26.553568Z","shell.execute_reply":"2021-10-07T16:45:26.589953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Show Flair Sequence as Animation","metadata":{}},{"cell_type":"code","source":"print('No of images:', len(flair_images))\n\nflair_animation = show_animation(flair_images)\nflair_animation\n# flair_animation.save('./a.mp4')","metadata":{"execution":{"iopub.status.busy":"2021-10-07T01:12:53.431358Z","iopub.execute_input":"2021-10-07T01:12:53.431644Z","iopub.status.idle":"2021-10-07T01:12:58.084982Z","shell.execute_reply.started":"2021-10-07T01:12:53.431616Z","shell.execute_reply":"2021-10-07T01:12:58.084181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Show T1w Sequence as Animation","metadata":{}},{"cell_type":"code","source":"t1w_images = get_all_images(df['t1w'][patient])\n    \nprint('No of images:', len(t1w_images))\nshow_animation(t1w_images)","metadata":{"execution":{"iopub.status.busy":"2021-10-07T16:45:46.86237Z","iopub.execute_input":"2021-10-07T16:45:46.862949Z","iopub.status.idle":"2021-10-07T16:45:46.897447Z","shell.execute_reply.started":"2021-10-07T16:45:46.862912Z","shell.execute_reply":"2021-10-07T16:45:46.895978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"https://www.kaggle.com/v1olet1nor1/rsna-custom-train","metadata":{}},{"cell_type":"markdown","source":"https://www.kaggle.com/trinayanbharadwaj/rsna-miccai-competition-model-building","metadata":{}},{"cell_type":"markdown","source":"Ref: https://www.kaggle.com/arnabs007/part-1-rsna-miccai-btrc-understanding-the-data","metadata":{}}]}