Python IO for DWI nrrd

Quick question: Is there a way to use Slicer’s python package outside of Slicer or an alternative way to read DWI NRRD files in python? I am trying to load/save NAMIC style DWI NRRD files in python (pynrrd does not interpret the DWI specific fields)

Any help is much appreciated

Hi @styner - I don’t know if this is possible for your workflow, but the PythonSlicer executable is a vanilla python that almost all packages can pip install to. So if you use that as your base you can use any parts of Slicer along with the rest of the python stack. I tend to do that instead of virtualenvs or other ways of isolating python environments.

If that doesn’t work for you maybe we can think of other ideas.

Thanks Steve, That’s certainly one possibility, but it also makes it significantly harder for certain uses. E.g. we do a lot of our deep learning via anaconda. So, I would have to customize Anaconda to PythonSlicer instead of its Python version. Not necessarily the best option also if I would like to later update my anaconda distribution.

When using other DWI formats (such as NIFTI, which is IMO inferior to NRRD when it comes to DWI/DTI), I have the option to use nibabel (e.g. for a multi-shell sparse facile model from the DWI in dipy). I would like such a solution where I can use nibabel for DWI in NIFTI and another package for Slicer-style DWI in NRRD.


We have it on our wish list to modularize the slicer libraries into python packages, but there’s a lot of cmake work for that to happen.

Another option for your case would be to use these converters:

That converter may indeed solve quite a bit of our problem, as we can read the DWI NRRD data and convert it to a nifti internal data structure (no need to write it to disk) and then use it with any NIFTI based package (such as dipy). And conversely, we can do the same to get the NIFTI into NRRD data structure if needed.

We were thinking about a conversion solution via DWIConvert, but this python based converter would be better as we don’t have to use disk space for the conversion (less space needs and much faster).


Is there anything specific in your Anaconda environment that you think you could not pip-install into Slicer’s Python environment?

Another option is to run certain Python functions in Slicer’s Python environment - the same way as you do it when you need to use multiple virtual Python environments for other reasons (e.g., because there are conflicting requirements between various Python package versions).