[Neuroimaging] nibabel.trackvis.read error

Matthew Brett matthew.brett at gmail.com
Sat Sep 5 00:18:21 CEST 2015


On Fri, Sep 4, 2015 at 12:27 PM, C.D. Langen <c.langen at erasmusmc.nl> wrote:
> Hi Matthew,
> Thank you for your quick reply. Below are links to two datasets, one
> that failed to be read by nib.trackvis, and one that succeeded. Both can
> be viewed in Trackvis:
> https://dl.dropboxusercontent.com/u/57089115/fail.trk
> https://dl.dropboxusercontent.com/u/57089115/succeed.trk
> Best,
> Carolyn
> On 03-09-15 19:31, Matthew Brett wrote:
>> Hi,
>> On Thu, Sep 3, 2015 at 10:22 AM, C.D. Langen <c.langen at erasmusmc.nl> wrote:
>>> Greetings,
>>> When I try to run the following line of code:
>>> streams, hdr = nib.trackvis.read(os.path.join(subjDir, 'dti.trk'),
>>> points_space='voxel')
>>> I get the following error, but only for a small subset of subjects:
>>>     File
>>> "/cm/shared/apps/python/2.7.6/lib/python2.7/site-packages/nibabel/trackvis.py",
>>> line 223, in read
>>>       streamlines = list(streamlines)
>>>     File
>>> "/cm/shared/apps/python/2.7.6/lib/python2.7/site-packages/nibabel/trackvis.py",
>>> line 202, in track_gen
>>>       buffer = pts_str)
>>> TypeError: buffer is too small for requested array
>>> Someone else had a similar error
>>> (http://mail.scipy.org/pipermail/nipy-devel/2012-March/007272.html)
>>> which they resolved by using nibabel from github. I tried this, but got
>>> the same error.
>>> All subjects' trackvis files were produced in exactly the same way using
>>> Trackvis, so I am not sure why only a few subjects fail while others
>>> succeed. Any ideas?
>>> Thank you in advance for your help in resolving this issue.
>> Thanks for the report - would you mind put the file online somewhere
>> so we can have a look?

What seems to be happening is that the last track in the file is
truncated.  It says that it is 120 points long (n_pts field), but
there is only data in the file for 77 points.

I tried reading the file with this MATLAB toolbox :

>> [header, tracks] = trk_read('fail.trk');
>> tracks(end)

ans =

    nPoints: 120
     matrix: [77x3 single]

>> tracks(end-1)

ans =

    nPoints: 91
     matrix: [91x3 single]

Note that the last track has 120 'nPoints' but only 77 points.  The
previous track has 91 'nPoints' and 91 points, which is what I would
expect.  So I think the file is mal-formed and trackvis is being more
generous than nibabel.  I think nibabel should have a mode where it
passes through this kind of thing.   In the meantime, if you want to
read all but the last shortened track, you could do something like

import nibabel as nib

track_gen, hdr = nib.trackvis.read('fail.trk', as_generator=True)

tracks = []
while True:
        track = next(track_gen)
    except (StopIteration, TypeError):



More information about the Neuroimaging mailing list