WCHN / CTseg

Brain CT image segmentation, normalisation, skull-stripping and total brain/intracranial volume computation.
GNU General Public License v3.0
55 stars 17 forks source link

How to use CTseg in python? #9

Closed NeuZhangQiang closed 2 years ago

NeuZhangQiang commented 3 years ago

CTseg is really a good project. Is it available in python? A python package may make the installation easy.

brudfors commented 3 years ago

Hi @NeuZhangQiang

Thank you! No, CTseg is unfortunately not available in Python as it builds on top of the SPM software package, which is implemented in MATLAB. However, I could add a Dockerfile allowing you to run CTseg through Docker. Would you be interested in this?

NeuZhangQiang commented 3 years ago

Thank you for your kindly reply. The matlab code must be run in a compute with matlab software, and it is inconvenient for me. Hope SPM could provide a python package~~~

gllmflndn commented 3 years ago

@NeuZhangQiang A MATLAB software can be compiled using the MATLAB Compiler, see here for more information. Which platform (Windows, Linux, macOS) are you using?

NeuZhangQiang commented 3 years ago

Actually, as a programmer, I am OK for Windows/Linux or matlab/python/c++. But, if I want to cooperate with doctor, I think matlab is complicated. For my algorithm, I think the best solution is that implementing it in python, and convert the python to exe (by pyinstaller). Then, the doctor can directly use the exe in windows. Therefore, I am looking for the python package for CTSeg.

gllmflndn commented 3 years ago

Thank you @NeuZhangQiang for the extra information. It is possible to provide you with a Windows executable that could be used to run CTseg without a MATLAB licence. Would you want it to open the SPM GUI or would you prefer to call CTseg from a command line as in the examples here?

NeuZhangQiang commented 3 years ago

Yes, I want to call CTseg from a command. How can I obtain the executable?

gllmflndn commented 3 years ago

For a Windows platform, you can do the following:

If you then run the spm12.exe executable, it will start the SPM12 GUI and you can access CTSeg from the batch interface. You can also access it from the command line using, e.g.: .\spm12.exe spm.tools.CTSeg --data myCT.nii

NeuZhangQiang commented 3 years ago

Thank you very much, I will try it.

brudfors commented 3 years ago

Add documentation on how to use with Python.

hhtsai-ntu commented 3 years ago

This is an amazing project. Is it possible to call CTseg in one command line on Linux platform?

brudfors commented 3 years ago

Hi @hhtsai-ntu

I am glad you enjoy CTseg.

The maybe simplest way of calling the code from the Linux command line would be:

matlab -batch "addpath('spm'); addpath('spm/toolbox/Shoot'); addpath('spm/toolbox/Longitudinal'); addpath('diffeo-segment'); addpath('CTseg'); spm_CTseg('CT.nii'); exit"

assuming that the matlab command is available in your terminal, and that addpath(...) specifies the correct paths to all required packages.

gllmflndn commented 3 years ago

There are also some convenient wrapper scripts for use at the command line. They would allow you to do:

spm12 spm.tools.CTSeg --data CT.nii

where spm12 is a symbolic link in your PATH to spm12-matlab or spm12-mcr.

@hhtsai-ntu if you need a compiled standalone SPM for Linux that contains CTseg, let me know.

hhtsai-ntu commented 3 years ago

Thank you for the super-fast and useful responses! Yes, I need a compiled standalone SPM for Linux that contains CTseg. Thanks.

gllmflndn commented 3 years ago

@hhtsai-ntu Sorry for the delay, I had missed the GitHub notification. For a Linux standalone, you need to download and install:

hhtsai-ntu commented 3 years ago

Hi @gllmflndn, I have installed "MATLAB Runtime R2020a" and unzip the "spm12_r8055_Linux_R2020a.zip". When I entered the command ./spm12 spm.tools.CTSeg --data CT.nii it showed that

Error using spm_cli (line 56)
Cannot find module spm.tools.CTSeg.

Did I miss any step? Thanks.

gllmflndn commented 3 years ago

Unless I made a mistake during compilation, I think the issue is that CTSeg has been renamed as CTseg so try again with:

 ./spm12 spm.tools.CTseg --data CT.nii
hhtsai-ntu commented 3 years ago

It works! Awesome! Are there any other arguments I can use? Like output path and tissue classes settings. Thanks.

gllmflndn commented 3 years ago

You can list all of the options like this:

./spm12 spm.tools.CTseg --help
Usage: spm12 spm.tools.CTseg [OPTIONS]

Options:
    --data             (*) Select the CT images to segment
    --odir             Segmentations will be written into this directory. If no directory ...
    --tc               The native space option allows you to produce a tissue class image ...
    --def              Deformation fields can be saved to disk, and used by the Deformatio...
    --correct_header   CT images can have messed up orientation matrices in their headers....
    --ss               Produce skull-stripped version, prefixed s_
    --vox              The voxel size of the template space output

    --help             Print usage statement

but you will not currently be able to set some parameters from the command line so I would recommend to create a text file CT_batch.m containing:

matlabbatch{1}.spm.tools.CTseg.data = {'CT.nii'};
matlabbatch{1}.spm.tools.CTseg.odir = {''};
matlabbatch{1}.spm.tools.CTseg.tc = [1 1 1];
matlabbatch{1}.spm.tools.CTseg.def = 1;
matlabbatch{1}.spm.tools.CTseg.correct_header = 0;
matlabbatch{1}.spm.tools.CTseg.ss = 0;
matlabbatch{1}.spm.tools.CTseg.vox = NaN;

and execute it using:

./spm12 batch CT_batch.m
maloneytc commented 2 years ago

Hi @NeuZhangQiang

Thank you! No, CTseg is unfortunately not available in Python as it builds on top of the SPM software package, which is implemented in MATLAB. However, I could add a Dockerfile allowing you to run CTseg through Docker. Would you be interested in this?

I'm interested in running CTSeg in a Docker container if you have a docker file available. Thank you.

brudfors commented 2 years ago

Hi @maloneytc

That should be possible for sure. @gllmflndn would you have time to make a new SPM standalone, with the latest version of CTseg and SPM? I can then make the Dockerfile, and give instructions on how to use it, etc :)

gllmflndn commented 2 years ago

Here is SPM12 +CTseg for MATLAB Runtime R2021b. This Dockerfile could be used after adjustment for the different version numbers and filenames.

brudfors commented 2 years ago

Thank you @gllmflndn!

@maloneytc, please see the Docker section of the CTseg README on how to run using Docker:

https://github.com/WCHN/CTseg#docker

maloneytc commented 2 years ago

This is great, thank you @brudfors and @gllmflndn for your help!