Skip to content

Commit 8348d1e

Browse files
committed
Addressed @matthew-brett's comments
1 parent 0b4a375 commit 8348d1e

File tree

4 files changed

+30
-36
lines changed

4 files changed

+30
-36
lines changed

nibabel/streamlines/tests/test_tractogram.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
import warnings
55

6-
from nibabel.testing import assert_arrays_equal, check_iteration
6+
from nibabel.testing import assert_arrays_equal
77
from nibabel.testing import clear_and_catch_warnings
88
from nose.tools import assert_equal, assert_raises, assert_true
99
from numpy.testing import assert_array_equal, assert_array_almost_equal
@@ -110,7 +110,7 @@ def check_tractogram(tractogram,
110110
streamlines = list(streamlines)
111111
assert_equal(len(tractogram), len(streamlines))
112112
assert_arrays_equal(tractogram.streamlines, streamlines)
113-
assert_true(check_iteration(tractogram))
113+
[t for t in tractogram] # Force iteration through tractogram.
114114

115115
assert_equal(len(tractogram.data_per_streamline), len(data_per_streamline))
116116
for key in data_per_streamline.keys():
@@ -556,7 +556,7 @@ def test_lazy_tractogram_creation(self):
556556
DATA['data_per_streamline_func'],
557557
DATA['data_per_point_func'])
558558

559-
assert_true(check_iteration(tractogram))
559+
[t for t in tractogram] # Force iteration through tractogram.
560560
assert_equal(len(tractogram), len(DATA['streamlines']))
561561

562562
# Generator functions get re-called and creates new iterators.
@@ -627,7 +627,7 @@ def test_lazy_tractogram_len(self):
627627
tractogram = LazyTractogram(DATA['streamlines_func'])
628628

629629
assert_true(tractogram._nb_streamlines is None)
630-
check_iteration(tractogram) # Force iteration through tractogram.
630+
[t for t in tractogram] # Force iteration through tractogram.
631631
assert_equal(tractogram._nb_streamlines, len(DATA['streamlines']))
632632
# This should *not* produce a warning.
633633
assert_equal(len(tractogram), len(DATA['streamlines']))

nibabel/streamlines/trk.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ def get_affine_trackvis_to_rasmm(header):
100100
affine = np.dot(scale, affine)
101101

102102
# TrackVis considers coordinate (0,0,0) to be the corner of the
103-
# voxel whereas streamlines returned assume (0,0,0) to be the
104-
# center of the voxel. Thus, streamlines are shifted of half a voxel.
103+
# voxel whereas streamlines returned assumes (0,0,0) to be the
104+
# center of the voxel. Thus, streamlines are shifted by half a voxel.
105105
offset = np.eye(4)
106106
offset[:-1, -1] -= 0.5
107107
affine = np.dot(offset, affine)
@@ -132,21 +132,24 @@ def encode_value_in_name(value, name, max_name_len=20):
132132
""" Encodes a value in the last two bytes of a string.
133133
134134
If `value` is one, then there is no encoding and the last two bytes
135-
are left untouched. This function also verify that the length of name is
136-
less than `max_name_len`.
135+
are left untouched. Otherwise, the byte before the last will be
136+
set to \x00 and the last byte will correspond to the value.
137+
138+
This function also verifies that the length of name is less
139+
than `max_name_len`.
137140
138141
Parameters
139142
----------
140-
value : int
141-
Integer value to encode.
142-
name : str
143+
value : byte
144+
Integer value between 0 and 255 to encode.
145+
name : bytes
143146
Name in which the last two bytes will serve to encode `value`.
144147
max_name_len : int, optional
145148
Maximum length name can have.
146149
147150
Returns
148151
-------
149-
encoded_name : str
152+
encoded_name : bytes
150153
Name containing the encoded value.
151154
"""
152155

@@ -161,9 +164,10 @@ def encode_value_in_name(value, name, max_name_len=20):
161164
).format(name, max_name_len - 2)
162165
raise ValueError(msg)
163166

167+
name = name.ljust(max_name_len, '\x00')
164168
if value > 1:
165169
# Use the last two bytes of `name` to store `value`.
166-
name = (asbytes(name[:18].ljust(18, '\x00')) + b'\x00' +
170+
name = (asbytes(name[:max_name_len - 2]) + b'\x00' +
167171
np.array(value, dtype=np.int8).tostring())
168172

169173
return name
@@ -183,10 +187,10 @@ class TrkReader(object):
183187
----
184188
TrackVis (so its file format: TRK) considers the streamline coordinate
185189
(0,0,0) to be in the corner of the voxel whereas NiBabel's streamlines
186-
internal representation (Voxel space) assume (0,0,0) to be in the
190+
internal representation (Voxel space) assumes (0,0,0) to be in the
187191
center of the voxel.
188192
189-
Thus, streamlines are shifted of half a voxel on load and are shifted
193+
Thus, streamlines are shifted by half a voxel on load and are shifted
190194
back on save.
191195
"""
192196
def __init__(self, fileobj):
@@ -461,10 +465,10 @@ class TrkFile(TractogramFile):
461465
----
462466
TrackVis (so its file format: TRK) considers the streamline coordinate
463467
(0,0,0) to be in the corner of the voxel whereas NiBabel's streamlines
464-
internal representation (Voxel space) assume (0,0,0) to be in the
468+
internal representation (Voxel space) assumes (0,0,0) to be in the
465469
center of the voxel.
466470
467-
Thus, streamlines are shifted of half a voxel on load and are shifted
471+
Thus, streamlines are shifted by half a voxel on load and are shifted
468472
back on save.
469473
"""
470474

@@ -515,8 +519,9 @@ def is_correct_format(cls, fileobj):
515519
otherwise returns False.
516520
"""
517521
with Opener(fileobj) as f:
518-
magic_number = f.read(5)
519-
f.seek(-5, os.SEEK_CUR)
522+
magic_len = len(cls.MAGIC_NUMBER)
523+
magic_number = f.read(magic_len)
524+
f.seek(-magic_len, os.SEEK_CUR)
520525
return magic_number == cls.MAGIC_NUMBER
521526

522527
@classmethod

nibabel/streamlines/utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import numpy as np
21
import nibabel
32

43

@@ -9,7 +8,7 @@ def get_affine_from_reference(ref):
98
---------
109
ref : str or :class:`Nifti1Image` object or ndarray shape (4, 4)
1110
If str then it's the filename of reference file that will be loaded
12-
using :func:nibabel.load in order to obtain the affine.
11+
using :func:`nibabel.load` in order to obtain the affine.
1312
If :class:`Nifti1Image` object then the affine is obtained from it.
1413
If ndarray shape (4, 4) then it's the affine.
1514
@@ -18,14 +17,15 @@ def get_affine_from_reference(ref):
1817
affine : ndarray (4, 4)
1918
Transformation matrix mapping voxel space to RAS+mm space.
2019
"""
21-
if type(ref) is np.ndarray:
20+
if hasattr(ref, 'affine'):
21+
return ref.affine
22+
23+
if hasattr(ref, 'shape'):
2224
if ref.shape != (4, 4):
2325
msg = "`ref` needs to be a numpy array with shape (4, 4)!"
2426
raise ValueError(msg)
2527

2628
return ref
27-
elif hasattr(ref, 'affine'):
28-
return ref.affine
2929

3030
# Assume `ref` is the name of a neuroimaging file.
3131
return nibabel.load(ref).affine

nibabel/testing/__init__.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,8 @@ def assert_allclose_safely(a, b, match_nans=True, rtol=1e-5, atol=1e-8):
6565
assert_true(np.allclose(a, b, rtol=rtol, atol=atol))
6666

6767

68-
def check_iteration(iterable):
69-
""" Checks that an object can be iterated through without errors. """
70-
try:
71-
for _ in iterable:
72-
pass
73-
except:
74-
return False
75-
76-
return True
77-
78-
7968
def assert_arrays_equal(arrays1, arrays2):
80-
""" Checks that two iterables yielding arrays are equals. """
69+
""" Check two iterables yield the same sequence of arrays. """
8170
for arr1, arr2 in zip_longest(arrays1, arrays2, fillvalue=None):
8271
assert_false(arr1 is None or arr2 is None)
8372
assert_array_equal(arr1, arr2)

0 commit comments

Comments
 (0)