Repository navigation
ENH: Remove dependency on nibabel.trackvis - #64
Conversation
5ac0c52 to
8adff56
Compare
|
Commit 8adff56 makes the Python 3.9, 3.10, and 3.11
That commit checks as well the old NiBabel To avoid missing the code, I've also put it into a gist: I will push a commit removing the checks and the dependency on the old NiBabel |
8adff56 to
c977552
Compare
|
Commit c977552 makes CIs happier:
Python 3.12 I am not sure whether the which I had to borrow from is currently used in
with no assignments. I think Following the That said, @demianw best would be to try the 93ab73f version against the one in here (c977552) with some known, in vivo data with known results. I do not have any known data/known results, so leaving this up to you @demianw if you can afford the time. Otherwise, this is good to go on my end. |
|
Converting back to draft; will check one more thing. |
|
So I converted this back to draft because I wanted to check the solution with a non-identity affine, and as I was suspecting the proposed solution does not work, unfortunately. The affine I used: was one that I created with the following arbitrary values It turns out that debugging
(I changed the IMO this has to do with the trackvis data assuming that the origin of the coordinates is at the center of the voxel: When trying to deal with that and considering what the StatefulTractogram does: inserting the code below in to the gist (see below for the link): The two sets of streamlines (original
I have left the entire modified I have spent some time trying to fix this without success. So @demianw if you happen to have some insight on what is happening, it would be much appreciated. |
|
@MarcCote I hate to bother you, but if anybody might instantly see the issue here, it would be you. |
|
Hey @effigies no problem. I vaguely recall that the old nibabel's trackvis implementation might have not considered that Trackvis (the software, hence the file format) assume (0,0,0) is at the center of the voxel. If I understand correctly the "issue" here is why the transformation (similar to what is done in the StatefulTractogram) does not bring the two tractograms on top of each other? What software are you using to do the visualization? Some software will take into account the metadata contains in the header of the .trk. Lighting up the tractogram's beacon @frheault |
|
Thanks for chiming in @effigies and @MarcCote.
Somehow; I do not fully see where the underlying issue lies. Using the non-identity, arbitrary affine I posted, the
Mi-Brain. |
|
Hello, Sadly, I am not familiar (and/or I forgot) with how the TRK was written in the older version of Babel. The load_tractogram() function assumes a lot of things. It is pretty common when I deal with older file that I need to load the streamline with nib.streamlines.load() and then create a StatefulTractogram This is common because some software saves the TRK assuming the wrong origin but uses software with the same assumption. This is not a problem with the affine (the affine should be identical to the NIFTI that match with the TRK). There is an even worse possibility, sometimes a VOX space shift is made in RASMM (adding 0.5 but when the streamlines are in their final RASMM space) or vice versa (A RASMM shift is made while the streamlines are in VOX). This is more complex and requires a lot of tinkering to fix the TRK so they have no negative coordinates in VOX space and align on top of their matching NIFTI. |
c977552 to
6cb3984
Compare
Make generated test streamline data have positive coordinates: use `rng.random` instead of `rng.standard_normal`. `rng.standard_normal` draws samples from a standard normal distribution (mean=0, stdev=1), instead, `rng.random` draws samples from the half-open interval `[0.0, 1.0)`. Generating positive coordinates allows the streamline data to be written with modern tractography representation tools, such as DIPY's `StatefulTractogram`, and thus enables comparing data across versions and tools more easily.
|
Resuming this. Sorry for the long delay. I wish some migration guide had been written at the time, and all the tinkering involved in these cases could be put into a documented resource that the community could freely and openly access. Based on the conversation, I will then assume that whatever As for the non-identity test, I am contenting myself using an affine that I borrowed from a clinical DWI that I have. After all, all tractography I/O tests in DIPY (at least, those that I considered while working on this patch set) use identity affines. So I will amend the commit before the end of the week after some more testing and mark the PR as ready. |
6cb3984 to
e92fdfe
Compare
Remove dependency on `nibabel.trackvis`: the module was deprecated in
`NiBabel` 2.5.0 and removed in version 4.0.0. So this patch set adapts
the `tractography.trackvis` code so that it uses the modern API.
Add a tolerance when comparing streamline data in `equal_tracts` to
account for some precision loss since data is transformed to `float32`
(vs `float64`) when applying the operations of the `NiBabel` API.
Fixes:
```
tract_querier/tractography/tests/test_tractography.py::test_saveload_trk
site-packages/nibabel/deprecated.py:35:
DeprecationWarning:
The trackvis interface has been deprecated and will be removed in v4.0; please use the 'nibabel.streamlines' interface.
mod = __import__(self._module_name, fromlist=[''])
```
and
```
tract_querier/tractography/trackvis.py:7: in <module>
from nibabel import trackvis
E ImportError: cannot import name 'trackvis' from 'nibabel'
(/opt/hostedtoolcache/Python/3.9.20/x64/lib/python3.9/site-packages/nibabel/__init__.py)
```
and related errors raised for example in:
https://github.com/demianw/tract_querier/actions/runs/12195905001/job/34022456528#step:6:85
and
https://github.com/demianw/tract_querier/actions/runs/12195905001/job/34022456201#step:6:32
Remove the upper version limit for `NiBabel`.
e92fdfe to
813b570
Compare
I wished I would have thought of that. In the original PR nipy/nibabel#391, I aimed to be as consistent as possible with the trackvis.py implementation with proper ground truth .trk files (e.g. https://github.com/nipy/nibabel/blob/master/nibabel/tests/data/simple.trk). Then, given how little information was provided on the specs for the .trk format (https://trackvis.org/docs/?subsect=fileformat), I reverse engineered how original TrackVis (the software) handles the different .trk properties (e.g. affine, dimension, etc). That's probably when ninabel.streamlines diverged a bit from trackvis.py. Also, as François pointed out, given the ambiguous specs for .trk different softwares implemented the format differently. |
|
I have force pushed a new commit with what I believe is the right patch set for this. I have also removed the As said in #64 (comment), I have contented myself with the identity affine matrix for the synthetic streamlines. When visualizing them before this patch set and after, there is the voxel center/corner translation that was expected asusming M-A's recall in #64 (comment) was correct. Now, I did try with a non-identity affine matrix for in-vivo data: took participant So one rotation and translations. The results with the code before this patch set and after are shown below: Pre-/post- (yellow/cyan) patch set overlay: Pre-/post- patch set overlay and the original (pink) left UF available from There is a perfect overlap between the results before this patch set and after (using NiBabel 3.0.0 on both cases), they are located at the expected anatomical location, and overlap with the original tractography data. There is some streamlines that are not extracted by the new patch set due to the mentioned voxel center/corner difference. Thus, my conclusion is that the code before this patch set and after provides the same results, and this PR is ready to be reviewed and merged. |
|
Re #64 (comment) I understand. Thanks @MarcCote.
Trying to load the tractogram at issue ( results in: Have tried removing (cutting) the invalid streamlines ( So I will not try to tinker with this data. I am happy to upload to OSF the So I'd change the test data to avoid this issue @demianw. But the PR is ready on my end. |
|
Hello, I will check this tomorrow. I want to make sure that the affine matrix and other coordinates are well kept |
|
Were the checks successful @demianw ? Thanks. |
|
It's good thanks ! Sorry for the delay |
|
The |
|
Thanks @demianw. I will have a look at what we can do for the testing data as time permits. |









Remove dependency on
nibabel.trackvis: the module was deprecated inNiBabel2.5.0 and removed in version 4.0.0. So this patch set adapts thetractography.trackviscode so that it uses the modern API.Add a tolerance when comparing streamline data in
equal_tractsto account for some precision loss since data is transformed tofloat32(vsfloat64) when applying the operations of theNiBabelAPI.Fixes:
and
and related errors raised for example in:
https://github.com/demianw/tract_querier/actions/runs/12195905001/job/34022456528#step:6:85 and
https://github.com/demianw/tract_querier/actions/runs/12195905001/job/34022456201#step:6:32