#!/usr/bin/env python # coding: utf-8 # # AURORA # In this Notebook we will demonstrate how to use the AURORA package to perform segmentation on cancer metastasis in brain MRI. # # --- # ## Getting Started # # ### Setup Colab environment # If you installed the packages and requirments on your own machine, you can skip this section and start from the import section. Otherwise you can follow and execute the tutorial on your browser. In order to start working on the notebook, click on the following button, this will open this page in the Colab environment and you will be able to execute the code on your own. # # # Open In Colab # # # Now that you are visualizing the notebook in Colab, run the next cell to install the packages we will use. There are few things you should follow in order to properly set the notebook up: # 1. Warning: This notebook was not authored by Google. Click on 'Run anyway'. # 1. When the installation commands are done, there might be "Restart runtime" button at the end of the output. Please, click it. # In[ ]: get_ipython().system('pip install brainles_aurora matplotlib') get_ipython().run_line_magic('load_ext', 'autoreload') get_ipython().run_line_magic('autoreload', '2') # By running the next cell you are going to create a folder in your Google Drive. All the files for this tutorial will be uploaded to this folder. After the first execution you might receive some warning and notification, please follow these instructions: # 1. Permit this notebook to access your Google Drive files? Click on 'Yes', and select your account. # Google Drive for desktop wants to access your Google Account. Click on 'Allow'. # 1. At this point, a folder has been created and you can navigate it through the lefthand panel in Colab, you might also have received an email that informs you about the access on your Google Drive # In[ ]: # Create a folder in your Google Drive # from google.colab import drive # drive.mount('/content/drive') # In[ ]: # Don't run this cell if you already cloned the repo # !git clone https://github.com/BrainLesion/tutorials.git # In[ ]: # make files from the repo available in colab import sys COLAB_BASE_PATH = "/content/tutorials/AURORA/" sys.path.insert(0, COLAB_BASE_PATH) # ### Imports # In[18]: from brainles_aurora.inferer import AuroraGPUInferer, AuroraInferer, AuroraInfererConfig import nibabel as nib import numpy as np import utils # local file # --- # ## Data # # AURORA expects *preprocessed* input data as NIfTI file or NumPy Array (*preprocessed* meaning the files should be co-registerend, skullstripped and in SRI-24 space). # # In this example we provide sample data from the [ASNR-MICCAI BraTS Brain Metastasis Challenge](https://www.synapse.org/#!Synapse:syn51156910/wiki/622553), which is already preprocessed in the `AURORA/data` folder in the form of 4 modalities of the same brain (T1, T1C, T2, FLAIR). To get an intuition of the data, one example slice of the 3D scans is visualized below. # # For your own data: # If the data is *not* preprocessed yet, consider using our [BrainLes preprocessing](https://github.com/BrainLesion/preprocessing) package (or its predecessor [BraTS-Toolkit](https://github.com/neuronflow/BraTS-Toolkit)). # # In[28]: utils.visualize_data() # ## Using Aurora # ### Minimal example using default settings and only T1c as input # In[2]: # We first need to create an instance of the AuroraInfererConfig class, which will hold the configuration for the inferer. We can then create an instance of the AuroraInferer class, which will be used to perform the inference. config = AuroraInfererConfig( tta=False, # we disable test time augmentations for a quick demo, should be set to True for better results sliding_window_batch_size=4, # The batch size used for the sliding window inference, decrease if you run out of memory (warning: too small batches might lead to unstable results) ) # Now that we have the configuration we can create an instance of the AuroraInferer class. This class will be used to perform the inference. We can then call the infer method to perform the inference. # If you don-t have a GPU that supports CUDA use the CPU version uncomment this and comment the GPU inferer # inferer = AuroraInferer(config=config) inferer = AuroraGPUInferer( config=config, cuda_devices="0", # optional, if you have multiple GPUs you can specify which one to use ) # The infer method takes the path to the T1c MRI file and the path to the output segmentation file as arguments. The output segmentation file will be created by the infer method and will contain the segmentation of the input T1c MRI. # The example below shows how to perform the inference using a T1c MRI file: _ = inferer.infer( t1c="data/t1c.nii.gz", segmentation_file="output/t1c_segmentation.nii.gz", ) # ## Visualize results # In[30]: utils.visualize_segmentation( modality_file="data/t1c.nii.gz", segmentation_file="output/t1c_segmentation.nii.gz", ) # ### Multiple input modalities and other available outputs # AURORA also supports different combinations of multi-modal MRI files (see manuscript). It will automatically select a suitable model depending on the inputs supplied. # # - Any of the following combination of sequences can be supplied: # - T1-CE + T1 + T2 + T2-FLAIR # - T1-CE only # - T1 only # - T2-FLAIR only # - T1-CE + T2-FLAIR # - T1-CE + T1 # - T1-CE + T1 + T2-FLAIR # - Instead of only saving the final output consisting of one file with 2 labels, additional files with labels for the whole lesion (metastasis + edema) or the metastasis only can also be saved. # - Test-time augmentation can be enabled (tta parameter in config, default = True). Segmentation with TTA will take around 10 times longer than without TTA. # # The example below shows how to perform the inference using multi-modal inputs. # In[4]: config = AuroraInfererConfig() # Use default config # If you don-t have a GPU that supports CUDA use the CPU version: AuroraInferer(config=config) inferer = AuroraGPUInferer( config=config, ) # Use all four input modalities,we also create other outputs and a custom log file _ = inferer.infer( t1="data/t1n.nii.gz", t1c="data/t1c.nii.gz", t2="data/t2w.nii.gz", fla="data/t2f.nii.gz", segmentation_file="output/multi-modal_segmentation.nii.gz", # The unbinarized network outputs for the whole tumor channel (edema + enhancing tumor core + necrosis) channel whole_tumor_unbinarized_floats_file="output/whole_tumor_unbinarized_floats.nii.gz", # The unbinarized network outputs for the metastasis (tumor core) channel metastasis_unbinarized_floats_file="output/metastasis_unbinarized_floats.nii.gz", log_file="output/custom_logfile.log", ) # ### NumPy Inputs/ Outputs # In[5]: config = AuroraInfererConfig() # AuroraInferer(config=config) # If you don-t have a GPU that supports CUDA use the CPU version (uncomment this and comment the GPU inferer) inferer = AuroraGPUInferer(config=config) # we load the nifty data to a numpy array t1_np = nib.load("data/t1n.nii.gz").get_fdata() # we can now use the inferer to perform the inference and obtain again a numpy array containing the segmentation results = inferer.infer(t1=t1_np) print([f"{k} : {v.shape}" for k, v in results.items()]) # # In[ ]: # now we can use the capabilities of numpy without having to re-read a nifti file, for example we could compute the number of metastasis voxels (the volume of the metastasis) as follows: whole_metastasis_voxels = results["segmentation"] > 0 print("metasis volume (including edema)", np.count_nonzero(whole_metastasis_voxels))