vanandrew/brainextractor

Problem with running your code

BNM999 opened this issue · 7 comments

image
Please explain why this error is coming and how to fix it. I am using Google Collab and after installing the brainextractor and cloning your github this error comes

You are getting this error because you are trying to invoke the brainextractor command line script as python code (which doesn't make sense to do). Since you are trying to use brainextractor as a python library, what you want to do instead is initialize a BrainExtractor object on your input image, then invoke the methods on this object to run the brain extraction and save out the mask (This is essentially what the command-line brainextractor script is doing on the backend, albeit with a bunch of argparse stuff to gather user input from the command line)

Here's a quick example of what you probably want to do:

# import the nibabel library so we can read in a nifti image
import nibabel as nib
# import the BrainExtractor class
from brainextractor import BrainExtractor

# read in the image file first
input_img = nib.load("/content/MNI.nii.gz")

# create a BrainExtractor object using the input_img as input
# we just use the default arguments here, but look at the
# BrainExtractor class in the code for the full argument list
bet = BrainExtractor(img=input_img)

# run the brain extraction
# this will by default run for 1000 iterations
# I recommend looking at the run method to see how it works
bet.run()

# save the computed mask out to file
bet.save_mask("/content/MN.nii.gz")

You can look at:

class BrainExtractor:
"""
Implemenation of the FSL Brain Extraction Tool
This class takes in a Nifti1Image class and generates
the brain surface and mask.
"""
def __init__(
self,
img: nib.Nifti1Image,
t02t: float = 0.02,
t98t: float = 0.98,
bt: float = 0.5,
d1: float = 20.0, # mm
d2: float = 10.0, # mm
rmin: float = 3.33, # mm
rmax: float = 10.0, # mm
):
for all the arguments to initialize the BrainExtractor class, as well as the arguments to it's run method here:
def run(self, iterations: int = 1000, deformation_path: str = None):

Thanks alot for your reply and guidance. could tell me why is the details of the MRI are white with the structures inside replaced by a full white color?

I'm guessing you're asking where the extracted brain image is? It is almost trivial to make one yourself, which is why the code doesn't do it for you.

The code is setup to output a brain mask, where 1 denotes a voxel that is classified as brain and 0 is not. So in order to get the extracted brain image, just do an element-wise multiplication between the input and the brain mask.

Continuing from the example I posted, here would be the code snippet:

# load brain mask that was saved out previously
brain_mask = nib.load("/content/MN.nii.gz")

# input_img was the original image we loaded in before
# we multiply the input and the mask to get the brain extracted image
brain_extracted = input_img.get_fdata() * brain_mask.get_fdata()

# we create a new nifti image to save this data out to
# we use the affine information from the input_img
# we also use the same header information from input_img
nib.Nifti1Image(brain_extracted, input_img.affine, input_img.header).to_filename("/content/MNI_bet.nii.gz")

You Rock! Thanks.
One last request if you may, how do you propose, I could apply this code to 33 nii files in subfolders from sample0001 to sample0033 using python code.

That's more of a basic python question, so I'm going to leave that for you to google 🙂. You'll learn it better rather than me giving you the answer here.

To give you a hint though: for loops + see this stackoverflow answer

Hello vanandrew,
So was able to extract and process my nifiti files using your code however some of the brain seems to be removed too, as i can see in the brainextractor main page, you have listed how to modify the fractional intensity using the terminal. Could you help me with a "python" code that i need to add custom fractional intensity value.
much appreciated,
thanks, you code has helped me alot in my project.

Hello vanandrew, So was able to extract and process my nifiti files using your code however some of the brain seems to be removed too, as i can see in the brainextractor main page, you have listed how to modify the fractional intensity using the terminal. Could you help me with a "python" code that i need to add custom fractional intensity value. much appreciated, thanks, you code has helped me alot in my project.
no worries , i figured it out by :
bet = BrainExtractor(img=input_img**,bt=0.2**)
#bt value was the fractional intensity threshold value