Esempio n. 1
0
def tractography_to_trackvis_file(filename,
                                  tractography,
                                  affine=None,
                                  image_dimensions=None):
    trk_header = trackvis.empty_header()

    if affine is not None:
        pass
    elif hasattr(tractography, 'affine'):
        affine = tractography.affine
    else:
        raise ValueError("Affine transform has to be provided")

    trackvis.aff_to_hdr(affine, trk_header, True, True)
    trk_header['origin'] = 0.
    if image_dimensions is not None:
        trk_header['dim'] = image_dimensions
    elif hasattr(tractography, 'image_dimensions'):
        trk_header['dim'] = tractography.image_dimensions
    else:
        raise ValueError("Image dimensions needed to save a trackvis file")

    orig_data = tractography.tracts_data()
    data = {}
    for k, v in orig_data.items():
        if not isinstance(v[0], numpy.ndarray):
            continue
        if (v[0].ndim > 1 and any(d > 1 for d in v[0].shape[1:])):
            warn("Scalar data %s ignored as trackvis "
                 "format does not handle multivalued data" % k)
        else:
            data[k] = v

    #data_new = {}
    # for k, v in data.iteritems():
    #    if (v[0].ndim > 1 and v[0].shape[1] > 1):
    #        for i in xrange(v[0].shape[1]):
    #            data_new['%s_%02d' % (k, i)] = [
    #                v_[:, i] for v_ in v
    #            ]
    #    else:
    #       data_new[k] = v
    trk_header['n_count'] = len(tractography.tracts())
    trk_header['n_properties'] = 0
    trk_header['n_scalars'] = len(data)

    if len(data) > 10:
        raise ValueError('At most 10 scalars permitted per point')

    trk_header['scalar_name'][:len(data)] = numpy.array([n[:20] for n in data],
                                                        dtype='|S20')
    trk_tracts = []

    for i, sl in enumerate(tractography.tracts()):
        scalars = None
        if len(data) > 0:
            scalars = numpy.vstack([
                data[k.decode('utf8')][i].squeeze()
                for k in trk_header['scalar_name'][:len(data)]
            ]).T

        trk_tracts.append((sl, scalars, None))

    trackvis.write(filename, trk_tracts, trk_header, points_space='rasmm')
Esempio n. 2
0
    if not(np.allclose(line[0], line[-1])):
        # new_line = [line[0]] # default line
        new_line = [line[0] + normal * normal_length] # added normal step outside
        for index in range(len(line)):
            if not(np.allclose(line[index], new_line[-1], atol=epsilon)):
                new_line += [line[index]]
        new_line = np.array(new_line)
        new_lines += [new_line]
    """
    new_lines += [np.array(line)]

del lines


hdr = nib.trackvis.empty_header()
trackvis.aff_to_hdr(img_affine, hdr, True, True)
"""
pixels_shape = [img.shape[0], img.shape[1], img.shape[2]]
hdr['voxel_size'] = img_zoom
hdr['voxel_order'] = 'LAS'
hdr['dim'] = pixels_shape
hdr['n_count'] = len(new_lines)
"""



streamlines = []
for fibre in list(new_lines):
    streamlines += [(fibre, None, None)]

nib.trackvis.write(trk_filename, streamlines, hdr, points_space='rasmm')
Esempio n. 3
0
def tractography_to_trackvis_file(filename, tractography, affine=None, image_dimensions=None):
    trk_header = trackvis.empty_header()

    if affine is not None:
        pass
    elif hasattr(tractography, 'affine'):
        affine = tractography.affine
    else:
        raise ValueError("Affine transform has to be provided")

    trackvis.aff_to_hdr(affine, trk_header, True, True)
    trk_header['origin'] = 0.
    if image_dimensions is not None:
        trk_header['dim'] = image_dimensions
    elif hasattr(tractography, 'image_dimensions'):
        trk_header['dim'] = tractography.image_dimensions
    else:
        raise ValueError("Image dimensions needed to save a trackvis file")

    orig_data = tractography.tracts_data()
    data = {}
    for k, v in orig_data.items():
        if not isinstance(v[0], numpy.ndarray):
            continue
        if (v[0].ndim > 1 and any(d > 1 for d in v[0].shape[1:])):
            warn(
                "Scalar data %s ignored as trackvis "
                "format does not handle multivalued data" % k
            )
        else:
            data[k] = v

    #data_new = {}
    # for k, v in data.iteritems():
    #    if (v[0].ndim > 1 and v[0].shape[1] > 1):
    #        for i in xrange(v[0].shape[1]):
    #            data_new['%s_%02d' % (k, i)] = [
    #                v_[:, i] for v_ in v
    #            ]
    #    else:
    #       data_new[k] = v
    trk_header['n_count'] = len(tractography.tracts())
    trk_header['n_properties'] = 0
    trk_header['n_scalars'] = len(data)

    if len(data) > 10:
        raise ValueError('At most 10 scalars permitted per point')

    trk_header['scalar_name'][:len(data)] = numpy.array(
        [n[:20] for n in data],
        dtype='|S20'
    )
    trk_tracts = []

    for i, sl in enumerate(tractography.tracts()):
        scalars = None
        if len(data) > 0:
            scalars = numpy.vstack([
                data[k.decode('utf8')][i].squeeze()
                for k in trk_header['scalar_name'][:len(data)]
            ]).T

        trk_tracts.append((sl, scalars, None))

    trackvis.write(filename, trk_tracts, trk_header, points_space='rasmm')