daducci / COMMIT

Linear framework to combine tractography and tissue micro-structure estimation with diffusion MRI
Other
45 stars 33 forks source link

Trk loading code should be updated to `nibabel.streamlines` #62

Closed jchoude closed 3 years ago

jchoude commented 5 years ago

The nibabel.trackvis interface is deprecated and will be removed in version 4 of Nibabel.

In its place, it is recommended to use nibabel.streamlines.

You could also use the new dipy.io.streamlines including StatefulTractogram, which provides more space checks.

daducci commented 5 years ago

Hi JC, what do you suggest, shall we use nibabel.streamlines or dipy.io.streamlines? I'm a bit lost.

Also, we made few tries with the nibabel.streamlines interface, and we are puzzled. If we load a tractogram and then re-save it right after, the coordinates are being changed by this operations. For example:

trk_file = nibabel.streamlines.load( 'fibers.trk' )
streamlines_out = trk_file.streamlines
affine = nibabel.streamlines.trk.get_affine_trackvis_to_rasmm( trk_file.header )
tractogram_out = nibabel.streamlines.tractogram.Tractogram( streamlines=streamlines_out, affine_to_rasmm=affine )
nibabel.streamlines.save( tractogram_out, 'fibers__MOD.trk' )

but the two files have different content (a shift). We tried with different affine matrices, e.g. identity, but the problem is not solved. Any clue?

jchoude commented 5 years ago

Hi Alessandro,

I think the simplest way is to use dipy.io.streamlines, since it will keep track of the space for you.

However, in your specific case, without having direct access to the file to validate what I'm saying, the first issue is that the nibabel.streamlines API always loads your streamlines in world (RASMM) space. In this case, since you don't change that space with your other modifications, you should instead use:

tractogram_out = nibabel.streamlines.tractogram.Tractogram( streamlines=streamlines_out, affine_to_rasmm=numpy.eye(4) ),

since your streamlines are already in world space. In your use case, using affine_to_rasmm=affine will apply an additionnal transform. Instead, you should use numpy.eye(4), and then save as

nibabel.streamlines.save( tractogram_out, 'fibers__MOD.trk', header=trk_file.header )

where the header should correctly save them (as long as the content of the original header made sense).

If it still doesn,t work, feel free to write offline with a file and I'll check if something weird is happening.