Skip to content

Commit 6aa94a9

Browse files
committed
Added support for Python 3
1 parent ed12031 commit 6aa94a9

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

nibabel/streamlines/compact_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def extend(self, elements):
110110
lengths = elements._lengths
111111
else:
112112
self._data = np.concatenate([self._data] + list(elements), axis=0)
113-
lengths = map(len, elements)
113+
lengths = list(map(len, elements))
114114

115115
idx = self._offsets[-1] + self._lengths[-1] if len(self) > 0 else 0
116116
self._lengths.extend(lengths)

nibabel/streamlines/tests/test_compact_list.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class TestCompactList(unittest.TestCase):
1717
def setUp(self):
1818
rng = np.random.RandomState(42)
1919
self.data = [rng.rand(rng.randint(10, 50), 3) for _ in range(10)]
20-
self.lengths = map(len, self.data)
20+
self.lengths = list(map(len, self.data))
2121
self.clist = CompactList(self.data)
2222

2323
def test_creating_empty_compactlist(self):
@@ -31,7 +31,7 @@ def test_creating_empty_compactlist(self):
3131
def test_creating_compactlist_from_list(self):
3232
rng = np.random.RandomState(42)
3333
data = [rng.rand(rng.randint(10, 50), 3) for _ in range(10)]
34-
lengths = map(len, data)
34+
lengths = list(map(len, data))
3535

3636
clist = CompactList(data)
3737
assert_equal(len(clist), len(data))
@@ -54,7 +54,7 @@ def test_creating_compactlist_from_list(self):
5454
def test_creating_compactlist_from_generator(self):
5555
rng = np.random.RandomState(42)
5656
data = [rng.rand(rng.randint(10, 50), 3) for _ in range(10)]
57-
lengths = map(len, data)
57+
lengths = list(map(len, data))
5858

5959
gen = (e for e in data)
6060
clist = CompactList(gen)
@@ -78,7 +78,7 @@ def test_creating_compactlist_from_generator(self):
7878
def test_creating_compactlist_from_compact_list(self):
7979
rng = np.random.RandomState(42)
8080
data = [rng.rand(rng.randint(10, 50), 3) for _ in range(10)]
81-
lengths = map(len, data)
81+
lengths = list(map(len, data))
8282

8383
clist = CompactList(data)
8484
clist2 = CompactList(clist)
@@ -152,7 +152,7 @@ def test_compactlist_extend(self):
152152
rng = np.random.RandomState(1234)
153153
shape = self.clist.shape
154154
new_data = [rng.rand(rng.randint(10, 50), *shape) for _ in range(5)]
155-
lengths = map(len, new_data)
155+
lengths = list(map(len, new_data))
156156
clist.extend(new_data)
157157
assert_equal(len(clist), len(self.clist)+len(new_data))
158158
assert_array_equal(clist._offsets[-len(new_data):],
@@ -187,7 +187,7 @@ def test_compactlist_getitem(self):
187187
assert_array_equal(self.clist[i], e)
188188

189189
# Get multiple items (this will create a view).
190-
clist_view = self.clist[range(len(self.clist))]
190+
clist_view = self.clist[list(range(len(self.clist)))]
191191
assert_true(clist_view is not self.clist)
192192
assert_true(clist_view._data is self.clist._data)
193193
assert_true(clist_view._offsets is not self.clist._offsets)

nibabel/streamlines/trk.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from nibabel.affines import apply_affine
1515
from nibabel.openers import Opener
16+
from nibabel.py3k import asbytes, asstr
1617
from nibabel.volumeutils import (native_code, swapped_code)
1718

1819
from .compact_list import CompactList
@@ -276,7 +277,7 @@ def write(self, tractogram):
276277
property_name = k
277278
if nb_values > 1:
278279
# Use the last to bytes of the name to store the nb of values associated to this data_for_streamline.
279-
property_name = k[:18].ljust(18, '\x00') + '\x00' + np.array(nb_values, dtype=np.int8).tostring()
280+
property_name = asbytes(k[:18].ljust(18, '\x00')) + b'\x00' + np.array(nb_values, dtype=np.int8).tostring()
280281

281282
self.header['property_name'][i] = property_name
282283

@@ -298,7 +299,7 @@ def write(self, tractogram):
298299
scalar_name = k
299300
if nb_values > 1:
300301
# Use the last to bytes of the name to store the nb of values associated to this data_for_streamline.
301-
scalar_name = k[:18].ljust(18, '\x00') + '\x00' + np.array(nb_values, dtype=np.int8).tostring()
302+
scalar_name = asbytes(k[:18].ljust(18, '\x00')) + b'\x00' + np.array(nb_values, dtype=np.int8).tostring()
302303

303304
self.header['scalar_name'][i] = scalar_name
304305

@@ -314,7 +315,7 @@ def write(self, tractogram):
314315
# If the voxel order implied by the affine does not match the voxel
315316
# order in the TRK header, change the orientation.
316317
# voxel (affine) -> voxel (header)
317-
header_ornt = self.header[Field.VOXEL_ORDER]
318+
header_ornt = asstr(self.header[Field.VOXEL_ORDER])
318319
affine_ornt = "".join(nib.orientations.aff2axcodes(self.header[Field.VOXEL_TO_RASMM]))
319320
header_ornt = nib.orientations.axcodes2ornt(header_ornt)
320321
affine_ornt = nib.orientations.axcodes2ornt(affine_ornt)
@@ -589,7 +590,7 @@ def load(cls, fileobj, lazy_load=False):
589590
# If the voxel order implied by the affine does not match the voxel
590591
# order in the TRK header, change the orientation.
591592
# voxel (header) -> voxel (affine)
592-
header_ornt = trk_reader.header[Field.VOXEL_ORDER]
593+
header_ornt = asstr(trk_reader.header[Field.VOXEL_ORDER])
593594
affine_ornt = "".join(nib.orientations.aff2axcodes(trk_reader.header[Field.VOXEL_TO_RASMM]))
594595
header_ornt = nib.orientations.axcodes2ornt(header_ornt)
595596
affine_ornt = nib.orientations.axcodes2ornt(affine_ornt)
@@ -601,12 +602,12 @@ def load(cls, fileobj, lazy_load=False):
601602
# voxel -> rasmm
602603
affine = np.dot(trk_reader.header[Field.VOXEL_TO_RASMM], affine)
603604

604-
605605
# Find scalars and properties name
606606
data_per_point_slice = {}
607607
if trk_reader.header[Field.NB_SCALARS_PER_POINT] > 0:
608608
cpt = 0
609609
for scalar_name in trk_reader.header['scalar_name']:
610+
scalar_name = asstr(scalar_name)
610611
if len(scalar_name) == 0:
611612
continue
612613

@@ -626,6 +627,7 @@ def load(cls, fileobj, lazy_load=False):
626627
if trk_reader.header[Field.NB_PROPERTIES_PER_STREAMLINE] > 0:
627628
cpt = 0
628629
for property_name in trk_reader.header['property_name']:
630+
property_name = asstr(property_name)
629631
if len(property_name) == 0:
630632
continue
631633

0 commit comments

Comments
 (0)