Skip to content

Commit 0324bce

Browse files
committed
Merge pull request #10 from matthew-brett/suggestion-for-per-array-dicts
Suggestion: simplify constructor for sliceable dicts
2 parents 8952c23 + 2dd2d4a commit 0324bce

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

nibabel/streamlines/tractogram.py

+24-19
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,16 @@ class SliceableDataDict(collections.MutableMapping):
2525
This container behaves like a standard dictionary but extends key access to
2626
allow keys for key access to be indices slicing into the contained ndarray
2727
values.
28+
29+
Parameters
30+
----------
31+
\*args :
32+
\*\*kwargs :
33+
Positional and keyword arguments, passed straight through the ``dict``
34+
constructor.
2835
"""
2936
def __init__(self, *args, **kwargs):
3037
self.store = dict()
31-
# Use the 'update' method to set the keys.
32-
if len(args) == 1:
33-
if args[0] is None:
34-
return
35-
36-
if isinstance(args[0], SliceableDataDict):
37-
self.update(**args[0])
38-
return
39-
4038
self.update(dict(*args, **kwargs))
4139

4240
def __getitem__(self, key):
@@ -47,9 +45,9 @@ def __getitem__(self, key):
4745

4846
# Try to interpret key as an index/slice for every data element, in
4947
# which case we perform (maybe advanced) indexing on every element of
50-
# the dictionnary.
48+
# the dictionary.
5149
idx = key
52-
new_dict = type(self)(None)
50+
new_dict = type(self)()
5351
try:
5452
for k, v in self.items():
5553
new_dict[k] = v[idx]
@@ -81,8 +79,18 @@ class PerArrayDict(SliceableDataDict):
8179
In addition, it makes sure the amount of data contained in those ndarrays
8280
matches the number of streamlines given at the instantiation of this
8381
instance.
82+
83+
Parameters
84+
----------
85+
nb_elements : None or int, optional
86+
Number of elements per value in each key, value pair or None for not
87+
specified.
88+
\*args :
89+
\*\*kwargs :
90+
Positional and keyword arguments, passed straight through the ``dict``
91+
constructor.
8492
"""
85-
def __init__(self, nb_elements, *args, **kwargs):
93+
def __init__(self, nb_elements=None, *args, **kwargs):
8694
self.nb_elements = nb_elements
8795
super(PerArrayDict, self).__init__(*args, **kwargs)
8896

@@ -105,7 +113,7 @@ def __setitem__(self, key, value):
105113
self.store[key] = value
106114

107115

108-
class PerArraySequenceDict(SliceableDataDict):
116+
class PerArraySequenceDict(PerArrayDict):
109117
""" Dictionary for which key access can do slicing on the values.
110118
111119
This container behaves like a standard dictionary but extends key access to
@@ -116,10 +124,6 @@ class PerArraySequenceDict(SliceableDataDict):
116124
sequences matches the number of elements given at the instantiation
117125
of the instance.
118126
"""
119-
def __init__(self, nb_elements, *args, **kwargs):
120-
self.nb_elements = nb_elements
121-
super(PerArraySequenceDict, self).__init__(*args, **kwargs)
122-
123127
def __setitem__(self, key, value):
124128
value = ArraySequence(value)
125129

@@ -285,7 +289,8 @@ def data_per_streamline(self):
285289

286290
@data_per_streamline.setter
287291
def data_per_streamline(self, value):
288-
self._data_per_streamline = PerArrayDict(len(self.streamlines), value)
292+
self._data_per_streamline = PerArrayDict(
293+
len(self.streamlines), {} if value is None else value)
289294

290295
@property
291296
def data_per_point(self):
@@ -294,7 +299,7 @@ def data_per_point(self):
294299
@data_per_point.setter
295300
def data_per_point(self, value):
296301
self._data_per_point = PerArraySequenceDict(
297-
self.streamlines.nb_elements, value)
302+
self.streamlines.nb_elements, {} if value is None else value)
298303

299304
@property
300305
def affine_to_rasmm(self):

nibabel/streamlines/trk.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ def get_affine_trackvis_to_rasmm(header):
9393
The streamlines in a trackvis file are in 'voxelmm' space, where the
9494
coordinates refer to the corner of the voxel.
9595
96-
Compute the # affine matrix that will bring them back to RAS+ mm space,
97-
where the coordinates refer to the center of the voxel.
96+
Compute the affine matrix that will bring them back to RAS+ mm space, where
97+
the coordinates refer to the center of the voxel.
9898
9999
Parameters
100100
----------

0 commit comments

Comments
 (0)