Skip to content

Commit ec0350c

Browse files
committed
ENH: Improved test coverage.
1 parent 859143b commit ec0350c

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

nibabel/streamlines/tests/test_array_sequence.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ def test_arraysequence_append(self):
152152
seq.append(element)
153153
check_arr_seq(seq, [element])
154154

155+
# Append an empty array.
156+
seq = SEQ_DATA['seq'].copy() # Copy because of in-place modification.
157+
seq.append([])
158+
check_arr_seq(seq, SEQ_DATA['seq'])
159+
155160
# Append an element with different shape.
156161
element = generate_data(nb_arrays=1,
157162
common_shape=SEQ_DATA['seq'].common_shape*2,

nibabel/streamlines/tests/test_tractogram.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from six.moves import zip
1414

1515
from .. import tractogram as module_tractogram
16+
from ..tractogram import is_data_dict, is_lazy_dict
1617
from ..tractogram import TractogramItem, Tractogram, LazyTractogram
1718
from ..tractogram import PerArrayDict, PerArraySequenceDict, LazyDict
1819

@@ -406,14 +407,20 @@ def test_extend(self):
406407
class TestLazyDict(unittest.TestCase):
407408

408409
def test_lazydict_creation(self):
409-
data_dict = LazyDict(DATA['data_per_streamline_func'])
410-
assert_equal(data_dict.keys(), DATA['data_per_streamline_func'].keys())
411-
for k in data_dict.keys():
412-
assert_array_equal(list(data_dict[k]),
413-
list(DATA['data_per_streamline'][k]))
410+
# Different ways of creating LazyDict
411+
lazy_dicts = []
412+
lazy_dicts += [LazyDict(DATA['data_per_streamline_func'])]
413+
lazy_dicts += [LazyDict(**DATA['data_per_streamline_func'])]
414414

415-
assert_equal(len(data_dict),
416-
len(DATA['data_per_streamline_func']))
415+
for data_dict in lazy_dicts:
416+
assert_true(is_lazy_dict(data_dict))
417+
assert_equal(data_dict.keys(), DATA['data_per_streamline_func'].keys())
418+
for k in data_dict.keys():
419+
assert_array_equal(list(data_dict[k]),
420+
list(DATA['data_per_streamline'][k]))
421+
422+
assert_equal(len(data_dict),
423+
len(DATA['data_per_streamline_func']))
417424

418425

419426
class TestTractogramItem(unittest.TestCase):
@@ -470,6 +477,9 @@ def test_tractogram_creation(self):
470477
DATA['data_per_streamline'],
471478
DATA['data_per_point'])
472479

480+
assert_true(is_data_dict(tractogram.data_per_streamline))
481+
assert_true(is_data_dict(tractogram.data_per_point))
482+
473483
# Create a tractogram from another tractogram attributes.
474484
tractogram2 = Tractogram(tractogram.streamlines,
475485
tractogram.data_per_streamline,
@@ -795,6 +805,9 @@ def test_lazy_tractogram_creation(self):
795805
DATA['data_per_streamline_func'],
796806
DATA['data_per_point_func'])
797807

808+
assert_true(is_lazy_dict(tractogram.data_per_streamline))
809+
assert_true(is_lazy_dict(tractogram.data_per_point))
810+
798811
[t for t in tractogram] # Force iteration through tractogram.
799812
assert_equal(len(tractogram), len(DATA['streamlines']))
800813

@@ -910,6 +923,22 @@ def test_lazy_tractogram_apply_affine(self):
910923
tractogram.affine_to_rasmm = None
911924
assert_raises(ValueError, tractogram.to_world)
912925

926+
# But calling apply_affine when affine_to_rasmm is None should work.
927+
tractogram = DATA['lazy_tractogram'].copy()
928+
tractogram.affine_to_rasmm = None
929+
transformed_tractogram = tractogram.apply_affine(affine)
930+
assert_array_equal(transformed_tractogram._affine_to_apply, affine)
931+
assert_true(transformed_tractogram.affine_to_rasmm is None)
932+
check_tractogram(transformed_tractogram,
933+
streamlines=[s*scaling for s in DATA['streamlines']],
934+
data_per_streamline=DATA['data_per_streamline'],
935+
data_per_point=DATA['data_per_point'])
936+
937+
# Calling apply_affine with lazy=False should fail for LazyTractogram.
938+
tractogram = DATA['lazy_tractogram'].copy()
939+
assert_raises(ValueError, tractogram.apply_affine,
940+
affine=np.eye(4), lazy=False)
941+
913942
def test_tractogram_to_world(self):
914943
tractogram = DATA['lazy_tractogram'].copy()
915944
affine = np.random.RandomState(1234).randn(4, 4)

nibabel/streamlines/tractogram.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class PerArrayDict(SliceableDataDict):
7777
7878
This container behaves like a standard dictionary but extends key access to
7979
allow keys for key access to be indices slicing into the contained ndarray
80-
values. The elements must also be ndarrays.
80+
values. The elements must also be ndarrays.
8181
8282
In addition, it makes sure the amount of data contained in those ndarrays
8383
matches the number of streamlines given at the instantiation of this
@@ -200,9 +200,6 @@ def __init__(self, *args, **kwargs):
200200
self.update(**args[0].store) # Copy the generator functions.
201201
return
202202

203-
if isinstance(args[0], SliceableDataDict):
204-
self.update(**args[0])
205-
206203
self.update(dict(*args, **kwargs))
207204

208205
def __getitem__(self, key):

0 commit comments

Comments
 (0)