Read/Write streamline files


DIPY can read and write many different file formats. In this example we give a short introduction on how to use it for loading or saving streamlines.

Read Frequently Asked Questions

import numpy as np
from import get_fnames
from import load_trk, save_trk
from dipy.tracking.streamline import Streamlines
  1. Read/write streamline files with DIPY.
fname = get_fnames('fornix')

# Read Streamlines
streams, hdr = load_trk(fname)
streamlines = Streamlines(streams)

# Save Streamlines
save_trk("my_streamlines.trk", streamlines=streamlines, affine=np.eye(4))
  1. We also work on our HDF5 based file format which can read/write massive datasets (as big as the size of your free disk space). With Dpy we can support
  • direct indexing from the disk
  • memory usage always low
  • extensions to include different arrays in the same file

Here is a simple example.

from import Dpy
dpw = Dpy('fornix.dpy', 'w')

Write many streamlines at once.


Write one track


or one track each time.

for t in streamlines:


Read streamlines directly from the disk using their indices

dpr = Dpy('fornix.dpy', 'r')
some_streamlines = dpr.read_tracksi([0, 10, 20, 30, 100])


Example source code

You can download the full source code of this example. This same script is also included in the dipy source distribution under the doc/examples/ directory.