Beispiel #1
0
def save_tractogram(fname,
                    streamlines,
                    affine,
                    vox_size=None,
                    shape=None,
                    header=None,
                    reduce_memory_usage=False,
                    tractogram_file=None):
    """ Saves tractogram files (*.trk or *.tck or *.dpy)

    Parameters
    ----------
    fname : str
        output trk filename
    streamlines : list of 2D arrays, generator or ArraySequence
        Each 2D array represents a sequence of 3D points (points, 3).
    affine : array_like (4, 4)
        The mapping from voxel coordinates to streamline points.
    vox_size : array_like (3,), optional
        The sizes of the voxels in the reference image (default: None)
    shape : array, shape (dim,), optional
        The shape of the reference image (default: None)
    header : dict, optional
        Metadata associated to the tractogram file(*.trk). (default: None)
    reduce_memory_usage : {False, True}, optional
        If True, save streamlines in a lazy manner i.e. they will not be kept
        in memory. Otherwise, keep all streamlines in memory until saving.
    tractogram_file : class TractogramFile, optional
        Define tractogram class type (TrkFile vs TckFile)
        Default is None which means auto detect format
    """
    if 'dpy' in os.path.splitext(fname)[1].lower():
        dpw = Dpy(fname, 'w')
        dpw.write_tracks(Streamlines(streamlines))
        dpw.close()
        return

    tractogram_file = tractogram_file or detect_format(fname)
    if tractogram_file is None:
        raise ValueError("Unknown format for 'fname': {}".format(fname))

    if vox_size is not None and shape is not None:
        if not isinstance(header, dict):
            header = {}
        header[Field.VOXEL_TO_RASMM] = affine.copy()
        header[Field.VOXEL_SIZES] = vox_size
        header[Field.DIMENSIONS] = shape
        header[Field.VOXEL_ORDER] = "".join(aff2axcodes(affine))

    if reduce_memory_usage and not callable(streamlines):
        sg = lambda: (s for s in streamlines)
    else:
        sg = streamlines

    tractogram_loader = LazyTractogram if reduce_memory_usage else Tractogram
    tractogram = tractogram_loader(sg)
    tractogram.affine_to_rasmm = affine
    track_file = tractogram_file(tractogram, header=header)
    nib.streamlines.save(track_file, fname)
    def streamlines(self, streamlines):
        """ Modify streamlines. Creating a new object would be less risky.

        Parameters
        ----------
        streamlines : list or ArraySequence (list and deepcopy recommanded)
            Streamlines of the tractogram
        """
        self._tractogram._streamlines = Streamlines(streamlines)
        self.data_per_point = self.data_per_point
        self.data_per_streamline = self.data_per_streamline
        logging.warning('Streamlines has been modified')
Beispiel #3
0
def test_dpy():
    fname = 'test.bin'
    with InTemporaryDirectory():
        dpw = Dpy(fname, 'w')
        A = np.ones((5, 3))
        B = 2 * A.copy()
        C = 3 * A.copy()
        dpw.write_track(A)
        dpw.write_track(B)
        dpw.write_track(C)
        dpw.write_tracks(Streamlines([C, B, A]))

        all_tracks = np.ascontiguousarray(np.vstack([A, B, C, C, B, A]))
        npt.assert_array_equal(all_tracks, dpw.tracks[:])
        dpw.close()

        dpr = Dpy(fname, 'r')
        npt.assert_equal(dpr.version() == u'0.0.1', True)
        T = dpr.read_tracksi([0, 1, 2, 0, 0, 2])
        T2 = dpr.read_tracks()
        npt.assert_equal(len(T2), 6)
        dpr.close()
        npt.assert_array_equal(A, T[0])
        npt.assert_array_equal(C, T[5])