Filtering streamlines with dipy target



I’m generating whole-sample streamlines with dipy’s CSD model. I’d like to also filter streamlines by region of interest, but running I’m getting
IndexError: streamline has points that map to negative voxel indices in the helper function _to_voxel_coordinates

Since I’m using the same affine as the DWI data the streamlines were generated from, and since the target mask looks fine relative to the diffusion image, I’m wondering how the negative voxel indices could arise.

I am running target like this:

    from dipy.tracking.utils import target

    # read in streamlines
    from nibabel import trackvis as tv
    streams_in_orig, hdr =
    streams_in = list(streams_in_orig)

    # streams_in is [[array, None, None], ...]
    streams = []
    for s in streams_in:

    target_mask_bool = np.array(target_mask.get_data(), dtype=bool, copy=True)

    target_sl_generator = target(streams, target_mask_bool, affine, include=True)
    target_streamlines = list(target_sl_generator)

where the .trk file was generated as below:

eu = EuDX(peaks.gfa, peaks.peak_indices[..., 0],
                  odf_vertices = sphere.vertices,
streamlines = ((sl, None, None) for sl in eu)

hdr = nib.trackvis.empty_header()
hdr['voxel_size'] = fa_img.get_header().get_zooms()[:3]
hdr['voxel_order'] = 'LAS'
hdr['dim'] = FA.shape[:3]

sl_fname = os.path.abspath('streamline.trk')
nib.trackvis.write(sl_fname, streamlines, hdr, points_space='voxel')

I have the same issue when hdr['voxel_order'] = 'RAS' and when points_space=None.

Any pointers would be appreciated!



I was pointed to Nibabel’s streamlines API, which is implemented in dipy here:

This seemed to resolve the problem. Thanks, Marc!