Skip to content

Commit 6a40356

Browse files
committed
BF - merge Stephans py3 port changes
1 parent c146254 commit 6a40356

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

nibabel/gifti/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
gifti
1818
"""
1919

20-
from giftiio import read, write
21-
from gifti import *
20+
from .giftiio import read, write
21+
from .gifti import *

nibabel/gifti/gifti.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
#
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
99

10+
import sys
1011
from StringIO import StringIO
12+
if sys.version_info[0] >= 3:
13+
from io import BytesIO
14+
else:
15+
BytesIO = StringIO
1116

1217
import numpy as np
1318

@@ -162,11 +167,11 @@ def to_xml(self):
162167
\t<TransformedSpace><![CDATA[%s]]></TransformedSpace>\n""" % (xform_codes.niistring[self.dataspace], \
163168
xform_codes.niistring[self.xformspace])
164169

165-
e = StringIO()
170+
e = BytesIO()
166171
np.savetxt(e, self.xform, '%10.6f')
167172
e.seek(0)
168173
res = res + "<MatrixData>\n"
169-
res = res + e.read()
174+
res = res + e.read().decode()
170175
e.close()
171176
res = res + "</MatrixData>\n"
172177
res = res + "</CoordinateSystemTransformMatrix>\n"
@@ -192,7 +197,7 @@ def data_tag(dataarray, encoding, datatype, ordering):
192197
ord = 'C'
193198

194199
if encoding == "GIFTI_ENCODING_ASCII":
195-
c = StringIO()
200+
c = BytesIO()
196201
# np.savetxt(c, dataarray, format, delimiter for columns)
197202
np.savetxt(c, dataarray, datatype, ' ')
198203
c.seek(0)
@@ -207,11 +212,8 @@ def data_tag(dataarray, encoding, datatype, ordering):
207212
elif encoding == "GIFTI_ENCODING_B64GZ":
208213
# first compress
209214
comp = zlib.compress(dataarray.tostring(ord))
210-
c = StringIO(comp)
211-
out = StringIO()
212-
base64.encode(c, out)
213-
out.seek(0)
214-
da = out.read()
215+
da = base64.encodestring(comp)
216+
da = da.decode()
215217

216218
elif encoding == "GIFTI_ENCODING_EXTBIN":
217219
raise NotImplementedError("In what format are the external files?")

nibabel/gifti/parse_gifti_fast.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def read_data_block(encoding, endian, ordering, datatype, shape, data):
4141

4242
elif encoding == 2:
4343
# GIFTI_ENCODING_B64BIN
44-
dec = base64.decodestring(data)
44+
dec = base64.decodestring(data.encode('ascii'))
4545
dt = data_type_codes.type[datatype]
4646
sh = tuple(shape)
4747
newarr = np.fromstring(zdec, dtype = dt, sep = '\n', count = c)
@@ -52,7 +52,9 @@ def read_data_block(encoding, endian, ordering, datatype, shape, data):
5252

5353
elif encoding == 3:
5454
# GIFTI_ENCODING_B64GZ
55-
dec = base64.decodestring(data)
55+
# convert to bytes array for python 3.2
56+
# http://diveintopython3.org/strings.html#byte-arrays
57+
dec = base64.decodestring(data.encode('ascii'))
5658
zdec = zlib.decompress(dec)
5759
dt = data_type_codes.type[datatype]
5860
sh = tuple(shape)
@@ -292,7 +294,6 @@ def CharacterDataHandler(self, data):
292294
self.coordsys.xformspace = xform_codes.code[data]
293295
elif self.write_to == 'MatrixData':
294296
# conversion to numpy array
295-
from StringIO import StringIO
296297
c = StringIO(data)
297298
self.coordsys.xform = np.loadtxt(c)
298299
c.close()
@@ -307,7 +308,7 @@ def CharacterDataHandler(self, data):
307308

308309
def parse_gifti_file(fname, buffer_size = 35000000):
309310

310-
datasource = open(fname,'r')
311+
datasource = open(fname,'rb')
311312

312313
parser = ParserCreate()
313314
parser.buffer_text = True

0 commit comments

Comments
 (0)