Skip to content

Commit fc7c33c

Browse files
committed
First draft
1 parent 96d474c commit fc7c33c

File tree

7 files changed

+645
-0
lines changed

7 files changed

+645
-0
lines changed

nibabel/streamlines/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from nibabel.openers import Opener
2+
3+
from nibabel.streamlines.utils import detect_format
4+
5+
6+
def load(fileobj):
7+
''' Load a file of streamlines, return instance associated to file format
8+
9+
Parameters
10+
----------
11+
fileobj : string or file-like object
12+
If string, a filename; otherwise an open file-like object
13+
pointing to a streamlines file (and ready to read from the beginning
14+
of the streamlines file's header)
15+
16+
Returns
17+
-------
18+
obj : instance of ``StreamlineFile``
19+
Returns an instance of a ``StreamlineFile`` subclass corresponding to
20+
the format of the streamlines file ``fileobj``.
21+
'''
22+
fileobj = Opener(fileobj)
23+
streamlines_file = detect_format(fileobj)
24+
return streamlines_file.load(fileobj)

nibabel/streamlines/base_format.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
class StreamlineFile:
3+
@staticmethod
4+
def get_magic_number():
5+
raise NotImplementedError()
6+
7+
@staticmethod
8+
def is_correct_format(cls, fileobj):
9+
raise NotImplementedError()
10+
11+
def get_header(self):
12+
raise NotImplementedError()
13+
14+
def get_streamlines(self, as_generator=False):
15+
raise NotImplementedError()
16+
17+
def get_scalars(self, as_generator=False):
18+
raise NotImplementedError()
19+
20+
def get_properties(self, as_generator=False):
21+
raise NotImplementedError()
22+
23+
@classmethod
24+
def load(cls, fileobj):
25+
raise NotImplementedError()
26+
27+
def save(self, filename):
28+
raise NotImplementedError()
29+
30+
def __iter__(self):
31+
raise NotImplementedError()
32+
33+
34+
class DynamicStreamlineFile(StreamlineFile):
35+
def append(self, streamlines):
36+
raise NotImplementedError()
37+
38+
def __iadd__(self, streamlines):
39+
return self.append(streamlines)

nibabel/streamlines/header.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Field:
2+
NB_STREAMLINES = "nb_streamlines"
3+
STEP_SIZE = "step_size"
4+
METHOD = "method"
5+
NB_SCALARS_PER_POINT = "nb_scalars_per_point"
6+
NB_PROPERTIES_PER_STREAMLINE = "nb_properties_per_streamline"
7+
NB_POINTS = "nb_points"
8+
VOXEL_SIZES = "voxel_sizes"
9+
DIMENSIONS = "dimensions"
10+
MAGIC_NUMBER = "magic_number"
11+
ORIGIN = "origin"
12+
VOXEL_TO_WORLD = "voxel_to_world"
13+
VOXEL_ORDER = "voxel_order"
14+
WORLD_ORDER = "world_order"
15+
ENDIAN = "endian"

nibabel/streamlines/tests/__init__.py

Whitespace-only changes.

nibabel/streamlines/tests/test_trk.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
2+
# vi: set ft=python sts=4 ts=4 sw=4 et:
3+
4+
from pdb import set_trace as dbg
5+
6+
from os.path import join as pjoin, dirname
7+
8+
from numpy.testing import (assert_equal,
9+
assert_almost_equal,
10+
assert_array_equal,
11+
assert_array_almost_equal,
12+
assert_raises)
13+
14+
DATA_PATH = pjoin(dirname(__file__), 'data')
15+
16+
import nibabel as nib
17+
from nibabel.streamlines.header import Field
18+
19+
20+
def test_load_file():
21+
# Test loading empty file
22+
# empty_file = pjoin(DATA_PATH, "empty.trk")
23+
# empty_trk = nib.streamlines.load(empty_file)
24+
25+
# hdr = empty_trk.get_header()
26+
# points = empty_trk.get_points(as_generator=False)
27+
# scalars = empty_trk.get_scalars(as_generator=False)
28+
# properties = empty_trk.get_properties(as_generator=False)
29+
30+
# assert_equal(hdr[Field.NB_STREAMLINES], 0)
31+
# assert_equal(len(points), 0)
32+
# assert_equal(len(scalars), 0)
33+
# assert_equal(len(properties), 0)
34+
35+
# for i in empty_trk: pass # Check if we can iterate through the streamlines.
36+
37+
# Test loading non-empty file
38+
trk_file = pjoin(DATA_PATH, "uncinate.trk")
39+
trk = nib.streamlines.load(trk_file)
40+
41+
hdr = trk.get_header()
42+
points = trk.get_points(as_generator=False)
43+
1/0
44+
scalars = trk.get_scalars(as_generator=False)
45+
properties = trk.get_properties(as_generator=False)
46+
47+
assert_equal(hdr[Field.NB_STREAMLINES] > 0, True)
48+
assert_equal(len(points) > 0, True)
49+
#assert_equal(len(scalars), 0)
50+
#assert_equal(len(properties), 0)
51+
52+
for i in trk: pass # Check if we can iterate through the streamlines.
53+
54+
55+
if __name__ == "__main__":
56+
test_load_file()

0 commit comments

Comments
 (0)