{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-30T06:14:37.615669Z","iopub.execute_input":"2021-09-30T06:14:37.615944Z","iopub.status.idle":"2021-09-30T06:14:37.620063Z","shell.execute_reply.started":"2021-09-30T06:14:37.615916Z","shell.execute_reply":"2021-09-30T06:14:37.619506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install fastai==2.5.1","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:14:37.621281Z","iopub.execute_input":"2021-09-30T06:14:37.621550Z","iopub.status.idle":"2021-09-30T06:15:24.649973Z","shell.execute_reply.started":"2021-09-30T06:14:37.621522Z","shell.execute_reply":"2021-09-30T06:15:24.649062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\nimport fastai\nimport random\nimport PIL","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:24.651752Z","iopub.execute_input":"2021-09-30T06:15:24.651980Z","iopub.status.idle":"2021-09-30T06:15:27.245074Z","shell.execute_reply.started":"2021-09-30T06:15:24.651952Z","shell.execute_reply":"2021-09-30T06:15:27.244234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mri_type = 'T1w'\nmin_subset = 16\npatients_seq_folder = [patient/mri_type for patient in Path('../input/rsna-miccai-png/train').ls() if (patient/mri_type).exists() and len((patient/mri_type).ls()) > min_subset]","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:27.246261Z","iopub.execute_input":"2021-09-30T06:15:27.246496Z","iopub.status.idle":"2021-09-30T06:15:45.934864Z","shell.execute_reply.started":"2021-09-30T06:15:27.246470Z","shell.execute_reply":"2021-09-30T06:15:45.933885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patients_seq_folder[:5]","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:45.936600Z","iopub.execute_input":"2021-09-30T06:15:45.936844Z","iopub.status.idle":"2021-09-30T06:15:45.944631Z","shell.execute_reply.started":"2021-09-30T06:15:45.936807Z","shell.execute_reply":"2021-09-30T06:15:45.943798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv')\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:45.945756Z","iopub.execute_input":"2021-09-30T06:15:45.946421Z","iopub.status.idle":"2021-09-30T06:15:45.988229Z","shell.execute_reply.started":"2021-09-30T06:15:45.946386Z","shell.execute_reply":"2021-09-30T06:15:45.987517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def open_image(fname, size=224):\n    img = PIL.Image.open(fname)\n    img = img.resize((size, size))\n    t = torch.Tensor(np.array(img))\n    return t.float()/255.0","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:45.989250Z","iopub.execute_input":"2021-09-30T06:15:45.989940Z","iopub.status.idle":"2021-09-30T06:15:45.994700Z","shell.execute_reply.started":"2021-09-30T06:15:45.989906Z","shell.execute_reply":"2021-09-30T06:15:45.993568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SeqImage(fastuple):\n    def show(self, ctx=None, **kwargs):\n        *imgs, label = self\n        if not isinstance(imgs[0], Tensor):\n            imgs = [tensor(img).permute(2,0,1) for img in imgs]\n        img_cat = torch.cat(imgs, dim=2)\n        return show_image(img_cat, figsize=(20,20), title=label, ctx=ctx, **kwargs)","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:45.996055Z","iopub.execute_input":"2021-09-30T06:15:45.996504Z","iopub.status.idle":"2021-09-30T06:15:46.008961Z","shell.execute_reply.started":"2021-09-30T06:15:45.996467Z","shell.execute_reply":"2021-09-30T06:15:46.007983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"splits = RandomSplitter()(patients_seq_folder)","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:46.010350Z","iopub.execute_input":"2021-09-30T06:15:46.011104Z","iopub.status.idle":"2021-09-30T06:15:46.039572Z","shell.execute_reply.started":"2021-09-30T06:15:46.011067Z","shell.execute_reply":"2021-09-30T06:15:46.038683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_folders, valid_folders = L(patients_seq_folder)[splits[0]], L(patients_seq_folder)[splits[1]]","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:46.040955Z","iopub.execute_input":"2021-09-30T06:15:46.041187Z","iopub.status.idle":"2021-09-30T06:15:46.046137Z","shell.execute_reply.started":"2021-09-30T06:15:46.041159Z","shell.execute_reply":"2021-09-30T06:15:46.045069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files_test = train_folders[0].ls()[:16]","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:46.048971Z","iopub.execute_input":"2021-09-30T06:15:46.049211Z","iopub.status.idle":"2021-09-30T06:15:46.057456Z","shell.execute_reply.started":"2021-09-30T06:15:46.049187Z","shell.execute_reply":"2021-09-30T06:15:46.056633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs_label = [PILImage.create(file) for file in files_test]\nimgs_label.append(0)","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:46.058879Z","iopub.execute_input":"2021-09-30T06:15:46.059220Z","iopub.status.idle":"2021-09-30T06:15:46.201119Z","shell.execute_reply.started":"2021-09-30T06:15:46.059126Z","shell.execute_reply":"2021-09-30T06:15:46.200307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s = SeqImage(imgs_label)","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:46.202248Z","iopub.execute_input":"2021-09-30T06:15:46.202490Z","iopub.status.idle":"2021-09-30T06:15:46.209171Z","shell.execute_reply.started":"2021-09-30T06:15:46.202462Z","shell.execute_reply":"2021-09-30T06:15:46.208272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tst = Resize(224)(s)\ntst = ToTensor()(tst)\ntst.show();","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:46.211034Z","iopub.execute_input":"2021-09-30T06:15:46.211744Z","iopub.status.idle":"2021-09-30T06:15:46.551696Z","shell.execute_reply.started":"2021-09-30T06:15:46.211694Z","shell.execute_reply":"2021-09-30T06:15:46.550808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SeqTransform(Transform):\n    def encodes(self, folder):\n        files = folder.ls()\n        files = sorted(random.sample(files, min_subset), key=lambda path: int(path.stem.split('-')[1]))\n        imgs = [PILImage.create(file) for file in files]\n        label = labels[labels['BraTS21ID']==int((folder).parent.name)]['MGMT_value'].values[0]\n        return SeqImage(*imgs, label)","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:46.553662Z","iopub.execute_input":"2021-09-30T06:15:46.554334Z","iopub.status.idle":"2021-09-30T06:15:46.561657Z","shell.execute_reply.started":"2021-09-30T06:15:46.554288Z","shell.execute_reply":"2021-09-30T06:15:46.560930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tfm = SeqTransform()","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:46.563239Z","iopub.execute_input":"2021-09-30T06:15:46.563616Z","iopub.status.idle":"2021-09-30T06:15:46.575793Z","shell.execute_reply.started":"2021-09-30T06:15:46.563579Z","shell.execute_reply":"2021-09-30T06:15:46.574752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tls = TfmdLists(patients_seq_folder, tfm, splits=splits)","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:46.577056Z","iopub.execute_input":"2021-09-30T06:15:46.577328Z","iopub.status.idle":"2021-09-30T06:15:46.689306Z","shell.execute_reply.started":"2021-09-30T06:15:46.577298Z","shell.execute_reply":"2021-09-30T06:15:46.688573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_at(tls.valid, 3);","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:46.690390Z","iopub.execute_input":"2021-09-30T06:15:46.690638Z","iopub.status.idle":"2021-09-30T06:15:47.093538Z","shell.execute_reply.started":"2021-09-30T06:15:46.690611Z","shell.execute_reply":"2021-09-30T06:15:47.092651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = tls.dataloaders(after_item=[Resize(224), ToTensor], \n                      after_batch=[IntToFloatTensor])","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:47.094700Z","iopub.execute_input":"2021-09-30T06:15:47.094949Z","iopub.status.idle":"2021-09-30T06:15:47.257275Z","shell.execute_reply.started":"2021-09-30T06:15:47.094913Z","shell.execute_reply":"2021-09-30T06:15:47.256662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@typedispatch\ndef show_batch(x:SeqImage, y, samples, ctxs=None, max_n=6, nrows=None, ncols=1, figsize=None, **kwargs):\n    if figsize is None: figsize = (ncols*6, max_n//ncols * 3)\n    if ctxs is None: ctxs = get_grid(min(x[0].shape[0], max_n), nrows=None, ncols=ncols, figsize=figsize)\n    for index,ctx in enumerate(ctxs): \n        imgs_ls = [x[i][index] for i in range(min_subset)]\n        label = int(x[-1][index])\n        SeqImage(*imgs_ls, label).show(ctx=ctx)","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:47.258618Z","iopub.execute_input":"2021-09-30T06:15:47.259073Z","iopub.status.idle":"2021-09-30T06:15:47.266860Z","shell.execute_reply.started":"2021-09-30T06:15:47.259032Z","shell.execute_reply":"2021-09-30T06:15:47.266252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b = dls.one_batch()","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:47.268070Z","iopub.execute_input":"2021-09-30T06:15:47.268576Z","iopub.status.idle":"2021-09-30T06:15:57.473381Z","shell.execute_reply.started":"2021-09-30T06:15:47.268535Z","shell.execute_reply":"2021-09-30T06:15:57.472508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch(figsize=(20,20))","metadata":{"execution":{"iopub.status.busy":"2021-09-30T06:15:57.474613Z","iopub.execute_input":"2021-09-30T06:15:57.474864Z","iopub.status.idle":"2021-09-30T06:16:10.323200Z","shell.execute_reply.started":"2021-09-30T06:15:57.474836Z","shell.execute_reply":"2021-09-30T06:16:10.322545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"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":[]}]}