Skip to content

Commit 2f30856

Browse files
committed
RF+DOC - using codecs for writing XML file; docstrings; whitespace
1 parent 94cb828 commit 2f30856

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

nibabel/gifti/gifti.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -473,26 +473,18 @@ def print_summary(self):
473473
print 'DataArray %s:' % i
474474
print da.print_summary()
475475
print '----end----'
476-
476+
477477
def to_xml(self):
478-
478+
""" Return XML corresponding to image content """
479479
res = """<?xml version="1.0" encoding="UTF-8"?>
480480
<!DOCTYPE GIFTI SYSTEM "http://www.nitrc.org/frs/download.php/115/gifti.dtd">
481481
<GIFTI Version="%s" NumberOfDataArrays="%s">\n""" % (self.version, str(self.numDA))
482-
483482
if not self.meta is None:
484483
res += self.meta.to_xml()
485-
486484
if not self.labeltable is None:
487485
res += self.labeltable.to_xml()
488-
489486
for dar in self.darrays:
490487
res += dar.to_xml()
491-
492488
res += "</GIFTI>"
493-
494489
return res
495-
496-
497-
498490

nibabel/gifti/giftiio.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@
1111
##############
1212

1313
import os
14+
import codecs
1415

1516
from . import parse_gifti_fast as gfp
1617

1718
def read(filename):
1819
""" Load a Gifti image from a file
19-
20+
2021
Parameters
2122
----------
2223
filename : string
2324
The Gifti file to open, it has usually ending .gii
24-
25+
2526
Returns
2627
-------
2728
img : GiftiImage
2829
Returns a GiftiImage
29-
3030
"""
3131
if not os.path.isfile(filename):
3232
raise IOError("No such file or directory: '%s'" % filename)
@@ -42,13 +42,16 @@ def write(image, filename):
4242
A GiftiImage instance to store
4343
filename : string
4444
Filename to store the Gifti file to
45-
45+
4646
Returns
4747
-------
4848
None
49-
49+
5050
Notes
5151
-----
52+
We write all files with utf-8 encoding, and specify this at the top of the
53+
XML file with the ``encoding`` attribute.
54+
5255
The Gifti spec suggests using the following suffixes to your
5356
filename when saving each specific type of data:
5457
@@ -72,10 +75,10 @@ def write(image, filename):
7275
Time Series
7376
.topo.gii
7477
Topology
75-
78+
7679
The Gifti file is stored in endian convention of the current machine.
7780
"""
78-
f = open(filename, 'wb')
7981
# Our giftis are always utf-8 encoded - see GiftiImage.to_xml
80-
f.write(image.to_xml().encode('utf-8'))
82+
f = codecs.open(filename, 'wb', encoding='utf-8')
83+
f.write(image.to_xml())
8184
f.close()

0 commit comments

Comments
 (0)