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

Hello, I have just started using SCT and I have seen that now there are python APIs. I tried to make them work but the cannot be imported and some init files are empty. I’ve noticed the note on the lack of stability on these APIs, so I was wondering whether using subrocess was the only way to integrate SCT in python also right now.
Thank you in advance!

1 Like

Hi @iricchi! Thank you for asking – this is valuable feedback. :slightly_smiling_face:

You are correct. The Python API is not available at the moment.

Right now, SCT is not structured to be installable via pip, so many of the API modules are inaccessible (as you have encountered). We are currently working to address this, and tracking progress in Issue #1526 in our GitHub repository.

I will make a note to update the documentation to be clearer that the Python API cannot be used, so thank you again for pointing this out.

Kind regards,
Joshua

2 Likes