{"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":"# install pull request version of torchio https://github.com/fepegar/torchio/pull/683\n!pip install git+https://github.com/laynr/torchio.git@681-add-plot_volume-indices-parameter","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:09:17.084985Z","iopub.execute_input":"2021-10-08T16:09:17.085648Z","iopub.status.idle":"2021-10-08T16:09:28.750118Z","shell.execute_reply.started":"2021-10-08T16:09:17.085606Z","shell.execute_reply":"2021-10-08T16:09:28.749285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import imageio\nimport pandas as pd\nimport torchio as tio\nfrom pathlib import Path\nimport multiprocessing as mp\nfrom tqdm.notebook import tqdm\nimport matplotlib.pyplot as plt\nfrom torch.utils.data import random_split, DataLoader\n\ndims = (280, 280, 264)\nplt.rcParams[\"figure.figsize\"] = (12, 10)\n\nout_dir      = Path.cwd()\ndata_dir     = Path('/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification')\ntraining_dir = data_dir / 'train'","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:09:28.751783Z","iopub.execute_input":"2021-10-08T16:09:28.752019Z","iopub.status.idle":"2021-10-08T16:09:28.761400Z","shell.execute_reply.started":"2021-10-08T16:09:28.751992Z","shell.execute_reply":"2021-10-08T16:09:28.760778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get patients\ndef get_patients(patients_dir, demo=False):\n    dir_list = training_dir.glob('*')\n    patients = [x.name for x in dir_list if x.is_dir()]\n    \n    if demo:\n        patients = patients[:2]\n\n    # Remove cases the competion host said to exclude \n    # https://www.kaggle.com/c/rsna-miccai-brain-tumor-radiogenomic-classification/discussion/262046\n    if '00109' in patients: patients.remove('00109')\n    if '00123' in patients: patients.remove('00123')\n    if '00709' in patients: patients.remove('00709')\n        \n    return patients\n\npatients = get_patients(training_dir, demo=True)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:09:28.762859Z","iopub.execute_input":"2021-10-08T16:09:28.763242Z","iopub.status.idle":"2021-10-08T16:09:28.995285Z","shell.execute_reply.started":"2021-10-08T16:09:28.763199Z","shell.execute_reply":"2021-10-08T16:09:28.994442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create dataset with synchronized MRIs\ndef data_preparation(patients):\n    subjects  = []\n    labels_df = pd.read_csv(data_dir / 'train_labels.csv', index_col=0)\n    # loop thru patients\n    for patient in patients:\n        # get label for patient\n        label = labels_df._get_value(int(patient), 'MGMT_value')\n        # create subject object for each patient\n        subject = tio.Subject(\n            BraTS21ID=patient,\n            MGMT_value=label,\n            FLAIR=tio.ScalarImage(training_dir / patient / 'FLAIR',),\n            T1w=tio.ScalarImage(training_dir / patient / 'T1w',),\n            T1wCE=tio.ScalarImage(training_dir / patient / 'T1wCE',),\n            T2w=tio.ScalarImage(training_dir / patient / 'T2w',),\n         )\n        # add subject object to subjects list\n        subjects.append(subject)\n\n    # preprocessing transforms\n    preprocessing_transforms = tio.Compose([\n        tio.ToCanonical(),\n        tio.Resample(1, image_interpolation='bspline'),\n        tio.Resample('T1w', image_interpolation='nearest'),\n        tio.CropOrPad(dims),\n    ])\n        \n\n    # create datasets from transformed subjects\n    dataset = tio.SubjectsDataset(subjects, transform=preprocessing_transforms)\n    print(f'patients :{len(dataset)}')\n    \n    return dataset\n\ndataset = data_preparation(patients) ","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:09:28.997057Z","iopub.execute_input":"2021-10-08T16:09:28.997301Z","iopub.status.idle":"2021-10-08T16:09:29.018505Z","shell.execute_reply.started":"2021-10-08T16:09:28.997273Z","shell.execute_reply":"2021-10-08T16:09:29.017921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create slices\ndef preprocess_dataset(dataset, out_dir, parallel=True):\n    if parallel:\n        loader = DataLoader(\n            dataset,\n            num_workers=mp.cpu_count(),\n            collate_fn=lambda x: x[0],\n        )\n        iterable = loader\n    else:\n        iterable = dataset\n        \n    for subject in tqdm(iterable):\n        slices_dir = out_dir / f'{subject[\"BraTS21ID\"]}' / 'slices' \n        slices_dir.mkdir(parents=True, exist_ok=True)\n        for x in range(dims[2]):\n            filename = slices_dir / f'{x:03d}_{subject[\"BraTS21ID\"]}_{subject[\"MGMT_value\"]}.png'\n            subject.plot(reorient=False, indices= (x,x,x), output_path=filename, show=False)   \n\npreprocess_dataset(dataset, out_dir, parallel=True)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:09:29.019347Z","iopub.execute_input":"2021-10-08T16:09:29.019561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create gifs\ndef create_gifs(patients, out_dir):\n    image_paths = []\n    for patient in patients:\n        slices_dir = out_dir / f'{patient}' / 'slices'\n        gif_dir   = out_dir / f'{patient}' / 'gif'\n        gif_dir.mkdir(parents=True, exist_ok=True)\n    \n        slices = slices_dir.glob('*')\n        filenames = [x for x in slices if x.is_file()]\n        filenames.sort()\n\n        images = []\n        for filename in filenames:\n            images.append(imageio.imread(filename))\n        imageio.mimsave(gif_dir / f'{patient}.gif', images)\n        \n        image_paths.append(gif_dir / f'{patient}.gif')\n        return image_paths\n                \nimage_paths = create_gifs(patients, out_dir)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display gifs\nfrom IPython.display import Image\ndef display_gifs(image_paths):\n    for image_path in image_paths:\n        display(Image(image_path))\n        \ndisplay_gifs(image_paths)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}