@@ -52,7 +52,7 @@ def metadata(self):
52
52
53
53
def to_xml (self ):
54
54
if len (self .data ) == 0 :
55
- return "<MetaData/>\n "
55
+ return b "<MetaData/>\n "
56
56
res = "<MetaData>\n "
57
57
for ele in self .data :
58
58
nvpair = """<MD>
@@ -61,7 +61,7 @@ def to_xml(self):
61
61
</MD>\n """ % (ele .name , ele .value )
62
62
res = res + nvpair
63
63
res = res + "</MetaData>\n "
64
- return res
64
+ return res . encode ( 'utf-8' )
65
65
66
66
def print_summary (self ):
67
67
print (self .metadata )
@@ -90,7 +90,7 @@ def get_labels_as_dict(self):
90
90
91
91
def to_xml (self ):
92
92
if len (self .labels ) == 0 :
93
- return "<LabelTable/>\n "
93
+ return b "<LabelTable/>\n "
94
94
res = "<LabelTable>\n "
95
95
for ele in self .labels :
96
96
col = ''
@@ -106,7 +106,7 @@ def to_xml(self):
106
106
(str (ele .key ), col , ele .label )
107
107
res = res + lab
108
108
res = res + "</LabelTable>\n "
109
- return res
109
+ return res . encode ( 'utf-8' )
110
110
111
111
def print_summary (self ):
112
112
print (self .get_labels_as_dict ())
@@ -181,7 +181,7 @@ def __init__(self, dataspace=0, xformspace=0, xform=None):
181
181
182
182
def to_xml (self ):
183
183
if self .xform is None :
184
- return "<CoordinateSystemTransformMatrix/>\n "
184
+ return b "<CoordinateSystemTransformMatrix/>\n "
185
185
res = ("""<CoordinateSystemTransformMatrix>
186
186
\t <DataSpace><![CDATA[%s]]></DataSpace>
187
187
\t <TransformedSpace><![CDATA[%s]]></TransformedSpace>\n """
@@ -191,7 +191,7 @@ def to_xml(self):
191
191
res += _arr2txt (self .xform , '%10.6f' )
192
192
res = res + "</MatrixData>\n "
193
193
res = res + "</CoordinateSystemTransformMatrix>\n "
194
- return res
194
+ return res . encode ( 'utf-8' )
195
195
196
196
def print_summary (self ):
197
197
print ('Dataspace: ' , xform_codes .niistring [self .dataspace ])
@@ -200,7 +200,8 @@ def print_summary(self):
200
200
201
201
202
202
def data_tag (dataarray , encoding , datatype , ordering ):
203
- """ Creates the data tag depending on the required encoding """
203
+ """ Creates the data tag depending on the required encoding,
204
+ returns as bytes"""
204
205
import zlib
205
206
ord = array_index_order_codes .npcode [ordering ]
206
207
enclabel = gifti_encoding_codes .label [encoding ]
@@ -215,7 +216,7 @@ def data_tag(dataarray, encoding, datatype, ordering):
215
216
raise NotImplementedError ("In what format are the external files?" )
216
217
else :
217
218
da = ''
218
- return "<Data>" + da + "</Data>\n "
219
+ return ( "<Data>" + da + "</Data>\n " ). encode ( 'utf-8' )
219
220
220
221
221
222
class GiftiDataArray (object ):
@@ -303,21 +304,21 @@ def to_xml(self):
303
304
# fix endianness to machine endianness
304
305
self .endian = gifti_endian_codes .code [sys .byteorder ]
305
306
result = ""
306
- result += self .to_xml_open ()
307
+ result += self .to_xml_open (). decode ( 'utf-8' )
307
308
# write metadata
308
309
if not self .meta is None :
309
- result += self .meta .to_xml ()
310
+ result += self .meta .to_xml (). decode ( 'utf-8' )
310
311
# write coord sys
311
312
if not self .coordsys is None :
312
- result += self .coordsys .to_xml ()
313
+ result += self .coordsys .to_xml (). decode ( 'utf-8' )
313
314
# write data array depending on the encoding
314
315
dt_kind = data_type_codes .dtype [self .datatype ].kind
315
316
result += data_tag (self .data ,
316
317
gifti_encoding_codes .specs [self .encoding ],
317
318
KIND2FMT [dt_kind ],
318
- self .ind_ord )
319
- result = result + self .to_xml_close ()
320
- return result
319
+ self .ind_ord ). decode ( 'utf-8' )
320
+ result = result + self .to_xml_close (). decode ( 'utf-8' )
321
+ return result . encode ( 'utf-8' )
321
322
322
323
def to_xml_open (self ):
323
324
out = """<DataArray Intent="%s"
@@ -331,7 +332,7 @@ def to_xml_open(self):
331
332
di = ""
332
333
for i , n in enumerate (self .dims ):
333
334
di = di + '\t Dim%s=\" %s\" \n ' % (str (i ), str (n ))
334
- return out % (intent_codes .niistring [self .intent ],
335
+ return ( out % (intent_codes .niistring [self .intent ],
335
336
data_type_codes .niistring [self .datatype ],
336
337
array_index_order_codes .label [self .ind_ord ],
337
338
str (self .num_dim ),
@@ -340,10 +341,10 @@ def to_xml_open(self):
340
341
gifti_endian_codes .specs [self .endian ],
341
342
self .ext_fname ,
342
343
self .ext_offset ,
343
- )
344
+ )). encode ( 'utf-8' )
344
345
345
346
def to_xml_close (self ):
346
- return "</DataArray>\n "
347
+ return b "</DataArray>\n "
347
348
348
349
def print_summary (self ):
349
350
print ('Intent: ' , intent_codes .niistring [self .intent ])
@@ -504,10 +505,10 @@ def to_xml(self):
504
505
<GIFTI Version="%s" NumberOfDataArrays="%s">\n """ % (self .version ,
505
506
str (self .numDA ))
506
507
if not self .meta is None :
507
- res += self .meta .to_xml ()
508
+ res += self .meta .to_xml (). decode ( 'utf-8' )
508
509
if not self .labeltable is None :
509
- res += self .labeltable .to_xml ()
510
+ res += self .labeltable .to_xml (). decode ( 'utf-8' )
510
511
for dar in self .darrays :
511
- res += dar .to_xml ()
512
+ res += dar .to_xml (). decode ( 'utf-8' )
512
513
res += "</GIFTI>"
513
- return res
514
+ return res . encode ( 'utf-8' )
0 commit comments