1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
.. AUTO-GENERATED FILE -- DO NOT EDIT!
.. _example_file_formats:
=====================
File Format Friendly
=====================
Overview
========
Read :ref:`faq`
::
import numpy as np
from dipy.data import get_data
from nibabel import trackvis
read trackvis
::
fname=get_data('fornix')
print(fname)
streams,hdr=trackvis.read(fname)
tracks=[s[0] for s in streams]
quick way use numpy.save
::
tracks_np=np.array(tracks,dtype=np.object)
np.save('fornix.npy',tracks_np)
it is good practice to remove what is not necessary any more
::
del tracks_np
tracks2=list(np.load('fornix.npy'))
huge datasets use dipy.io.dpy
* direct indexing from the disk
* memory usage always low
* extendable
::
from dipy.io.dpy import Dpy
dpw=Dpy('fornix.dpy','w')
write many tracks at once
::
dpw.write_tracks(tracks2)
write one track
::
dpw.write_track(tracks2[0]*6)
or one track each time
::
for t in tracks:
dpw.write_track(t*3)
dpw.close()
read tracks directly from the disk using their indices
::
dpr=Dpy('fornix.dpy','r')
some_tracks=dpr.read_tracksi([0,10,20,30,100])
dpr.close()
Number of tracks in before and after
::
print(len(tracks))
print(len(some_tracks))
.. admonition:: Example source code
You can download :download:`the full source code of this example <./file_formats.py>`.
This same script is also included in the dipy source distribution under the
:file:`doc/examples/` directory.
|