Access the 3x3 diffusion tensor matrix of dti images

Hi all,

I have a set of patients’ dti images. How do I extract the 3x3 diffusion tensor matrices per each voxel? Additionally how do I convert the DTI image to nrrd format, can I still use the DWIconvert module even though its not a DWI image?

Thank you

There are a couple ways, via vtk and numpy:

https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#Accessing_Volume_data_as_numpy_array

https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Access_values_in_a_DTI_tensor_volume

If you have the data loaded in Slicer as a diffusion tensor, then saving in nrrd format should just work.

Hi,
Thanks for the response, how do these methods to get the diffusion tensors translate to the slice depth. Per patient I have around 26 images, will the methods give me the tensors for a specified slice depth or for all of them? Additionally, when you run the code given in the links in the interactive python window, where does the information containing the diffusion tensors go? What format is it saved in?

Hi -

You can get all the tensors. The mapping between slices, rows, and columns (IJK) and tensor samples is by the 3D array where each element is a 3x3 tensor. The mapping to patient space is defined by the IJKToRAS transform (and any other applied transforms). The tensors themselves are measured with respect to the Measurement Frame, a 3x3 matrix you can get from the node.

The values are stored in python variables so you can use them to perform further calculations or save them out to a file if you want.

You’re very welcome. Feel free to ask follow ups if needed.

Hi,
So if they save as variables in python would I be able to access them through a jupyter notebook after they saved? How would I save them out as a file (sorry I’m new to this). Additionally, in the links you posted contain nested forloops, how does this work in the python window (since isnt the window just a line by line execution?)

Additionally, if I create a brain mask of each image and then extract the tensors, will this prevent me from getting the skull tensor data (which I dont need).

How can I create brain masks of all my patients and save them to the program so that whenever I come back to continue work I only work on the updated imaged? Is there a simple way I can save this, for example the format given in the DCM window that separates the data per patient and series?

You can have a look at some of the Slicer python tutorials to get an idea of generally how python variables work to represent, save, and restore data.

Yes, you can do loops in the python interactor in Slicer. Accessing a volume that way is shown as an example but usually not efficient.

The numpy array approach is usually better. E.g. if you’ve downloaded DTIBrain from the SampleData module, then you can do this:

>>> a = array('DTIBrain')
>>> a.shape
(85, 144, 144, 3, 3)

If you have a mask like from SlicerDMRI you can also access it as a numpy array.

Again, thank you for the response!
The tutorials helped a lot, but I still couldnt figure out how to save the diffusion tensors as a file. Specifically, I am trying to export all the diffusion tensors to matlab and work with them there. Is there a way to do this?

slicer.util.saveNode should work for diffusion images. You can load the generated .nrrd file into Matlab.

There are great Python packages for all kinds of data processing, including diffusion images. Why don’t you process the data directly in Python?

I agree with @lassoan, you should try to stick with Python if you can.

But if you need to work with matlab, you this might be another option.

Hi,

Thank you again for the replies. I am trying to use matlab to compare to another analysis done in that software. I have been trying save the image data however I keep getting this error. What am I doing wrong? Additionally, if I wanted to save as a .mat file how would i do so since scipy isnt built into slicer? Please let, I am really just trying to extract this data so I can work with it easier in Matlab.
13%20PM

Hi! I’m not sure what your goal is in the code? The code loops through the tensors but doesn’t do anything with the data. The last error is because the save function expects a node, but the code gives it an image data.

hi i am trying to extract the 3x3 diffusion tensors per voxel and save as a .mat file

Hi Andras,
How can I iterate or see values inside the tensor variable which is a vtkfloatarray. is there a way to convert to numpy and then iterate over the array.

Thank you

Regards,
Saima Safdar

Hi Andras,
I have another question. How can I get position values of all the voxels greater than 0 from a volume image.

Thank you

Regards,
Saima Safdar

Hi,
I need to get the tensor values from DTI image. I got the values using the following code but all the values are same. I think the values should be different for each voxel. Could you please tell me if there is something wrong with the following code:

    tensors = slicer.util.array(inputVolume.GetID())
    volumeNode=slicer.util.getNode(inputVolume.GetID())
    imageData=volumeNode.GetImageData()
    tensors = imageData.GetPointData().GetTensors()
    extent = imageData.GetExtent()
    idx = 0
    for k in range(extent[4], extent[5]+1):
      for j in range(extent[2], extent[3]+1):
        for i in range(extent[0], extent[1]+1):
          tensors.GetTuple9(idx)
          idx += 1
    import json
    f = open('/home/saima/Downloads/Damon/Damon/Case131/tensors.txt', 'w')
    for x in range(5300):
        print(tensors.GetTuple(x))
        f.write(json.dumps(tensors.GetTuple(x)))

This was the right way to get the tensors as a numpy array. You can just index it to get the tensor values. You don’t need to use the VTK api.