Using sct via python

Hi,
I have so far been using the sct via bash and matlab, but since its coded in python, I am wondering if I can import it as a module into a python IDE? Is anybody using the sct in a python IDE?

Thanks for your help :slight_smile:

Hi alidab,

We don’t provide a python API. It’s an idea we’ve tossed around, but for now the API is as you see it.

You can use subprocess.run to interface python to sct, for example like this:

result = subprocess.run(["sct_analyze_lesion", "-m", mask_fname, "-s", centerline_fname], stdout=subprocess.PIPE, check=True)
sys.stdout.write(result.stdout)

Internally we sometimes represent images with nibabel but we don’t expose this as an API officially. Many of our own internal processes operate on image files, calling out to ANTs, and we don’t have a good way to pass the images in RAM from python to ANTs or otherwise; I think, given that neuroimaging data tends to be very large, we would have limited success trying to do that anyway because it would just lead to running out of RAM sooner than later.

2 Likes