Help importing and segmenting MRI with CAT12 from Python

Dear Brainstorm Team,

I’m currently working on a project where I need to import an MRI file (.nii / .nii.gz), segment it with CAT12, and then import the segmented data back into Brainstorm.

I’m calling Brainstorm functions from Python using the MATLAB engine (matlab.engine), so I’m not opening the GUI.
I tried importing the MRI using import_mri(), but it didn’t work.

Now I’m attempting to download SPM12 and CAT12 from Python to perform the segmentation manually before importing it back into Brainstorm. However, I’d like to know if there is an easier or recommended way to import and segment the MRI directly within the Brainstorm pipeline.

Any guidance or example scripts would be greatly appreciated.

Thank you very much for your help!

Hello @claudiarodgadea,

It is possible to use Brainstorm without the GUI, this is by generating a Matlab script, and execute it. Details to do so are described in this tutorial:

The script import_mri() only does that, to import a MRI volume into the Brainstorm DB, the process that you are looking is process_segment_cat12.m

The process process_segment_cat12.m is called in these two examples:

In addition, check this other forum post:

Thank you for your help. However, after using the process you recommended and following the examples, I’m still encountering an issue. Although the MRI appears to be imported and segmented correctly, I can’t find any of the resulting files in my folder, only the brainstormsubject.mat file. I have been unsuccessfully trying to export the results from Brainstorm to Python, in case they were saved within Brainstorm’s internal database, or at least to verify that they are stored there and save them in a variable for later use.

Here is my code. Hopefully, you can help me find a way to solve this problem. Thank you again for your assistance.

Anatomy volumes (MRI, CT, PET) and surfaces (cortex, scalp, etc) are stored in the Brainstorm DB as indicated in here:

In Matlab this filesnames can be retrieved with:

sSubject = bst_get('Subject', subject_name);
mriFileNames = file_fullpath({sSubject.Anatomy.FileName});
surfileNames = file_fullpath({sSubject.Surface.FileName});

Then you can just load them as, this will return the structures described in the links above

mriMat = in_mri_bst(mri_file_name);
surfMat = in_tess_bst(mri_file_name);

:light_bulb: Double-check your calls to bst_get, in the shared code: SubjectByName does not exist, and the MriFile takes as argument the the relative path to the MRI file (obtained with file_short(), not the Subject file)

Thank you