MarcCote / tractconverter

Converter for different neuro-imaging file formats.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Convert from .trk to .tck crashes if anatomy's affine has permutations.

matthieudumont opened this issue · comments

Hi,
I recently generated tracks with some small animal diffusion data and I wanted to convert them from .trk to .tck. Thing is, I get an error in tck.py at line 160.

This happens mainly because my anatomy's affine matrix is a permutation matrix with some zeros on the diagonal and the scaling removal step seems to assume the affine matrix has no zeros on the diagonal.

I already have a quick fix I could submit but I just wanted to confirm it really was a bug and not me using the tool improperly before making a pull request.

Thanks you.

Hi Matthieu,

You could try providing the anatomy to TractConverter.py using the following argument -a anat.nii as it will overwrite information contained in the .trk by the one in the anatomy.

Also, can you paste the output of TractInfo.py script executed on your .trk file.

Anyhow, I would be happy to check your fix, because crashingh sould not be an option ;)
Thanks for the feedback.

Actually this happens precisely when I provide the anatomy as parameter.

The _calcTransform method of tck.py divides the diagonal of the anatomy's affine matrix by the scaling with the following code:

anat = nibabel.load(anatFile)
voxelSize = list(anat.get_header().get_zooms())[:3]

M = anat.get_header().get_best_affine()
idxDiag = np.diag(np.diag(M)) != 0
M[idxDiag] /= voxelSize + [1]

Unfortunatly, if M has some zeros on the diagonal, M[idxDiag] / voxelSize + [1]
crashes because of dimensions mismatch.

I suggest building a scaling matrix and multiply M with it:

anat = nibabel.load(anatFile)
voxelSize = list(anat.get_header().get_zooms())[:3]
scaleMat = np.diag(np.divide(1.0, voxelSize + [1]))
M = anat.get_header().get_best_affine()
M = np.dot(scaleMat, M)

It seems to work for my needs but idk if it was done the other way for a particular reason.

I must confess that part of the code comes from an old version of the FiberNavigator.

I'm curious to know, why is your anatomy has one dimension of 0mm ?

I added your fix thanks => v0.6.7.