Skip to content

Commit fde94f1

Browse files
committed
Merge pull request #365 from bcipolli/xmlbasedimages
RF: Use XmlSerializable, XmlParser for Gifti load/save
2 parents 853f5fb + 5c850da commit fde94f1

File tree

4 files changed

+240
-149
lines changed

4 files changed

+240
-149
lines changed

nibabel/gifti/gifti.py

Lines changed: 60 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class GiftiMetaData(xml.XmlSerializable):
2929
the list self.data """
3030
def __init__(self, nvpair=None):
3131
self.data = []
32-
if not nvpair is None:
32+
if nvpair is not None:
3333
self.data.append(nvpair)
3434

3535
@classmethod
@@ -296,7 +296,7 @@ def from_array(klass,
296296
cda.intent = intent_codes.code[intent]
297297
cda.encoding = gifti_encoding_codes.code[encoding]
298298
cda.endian = gifti_endian_codes.code[endian]
299-
if not coordsys is None:
299+
if coordsys is not None:
300300
cda.coordsys = coordsys
301301
cda.ind_ord = array_index_order_codes.code[ordering]
302302
cda.meta = GiftiMetaData.from_dict(meta)
@@ -371,7 +371,7 @@ def print_summary(self):
371371
print('Endian: ', gifti_endian_codes.specs[self.endian])
372372
print('ExternalFileName: ', self.ext_fname)
373373
print('ExternalFileOffset: ', self.ext_offset)
374-
if not self.coordsys is None:
374+
if self.coordsys is not None:
375375
print('----')
376376
print('Coordinate System:')
377377
print(self.coordsys.print_summary())
@@ -386,14 +386,44 @@ def metadata(self):
386386
return self.meta.metadata
387387

388388

389-
class GiftiImage(FileBasedImage, xml.XmlSerializable):
389+
class GiftiImage(xml.XmlSerializable, FileBasedImage):
390+
"""
391+
The Gifti spec suggests using the following suffixes to your
392+
filename when saving each specific type of data:
393+
394+
.gii
395+
Generic GIFTI File
396+
.coord.gii
397+
Coordinates
398+
.func.gii
399+
Functional
400+
.label.gii
401+
Labels
402+
.rgba.gii
403+
RGB or RGBA
404+
.shape.gii
405+
Shape
406+
.surf.gii
407+
Surface
408+
.tensor.gii
409+
Tensors
410+
.time.gii
411+
Time Series
412+
.topo.gii
413+
Topology
414+
415+
The Gifti file is stored in endian convention of the current machine.
416+
"""
390417
valid_exts = ('.gii',)
391418
files_types = (('image', '.gii'),)
392419

393420
def __init__(self, header=None, extra=None, file_map=None, meta=None,
394421
labeltable=None, darrays=None, version="1.0"):
395-
FileBasedImage.__init__(self, header=header, extra=extra,
396-
file_map=file_map)
422+
super(GiftiImage, self).__init__(header=header, extra=extra,
423+
file_map=file_map)
424+
# placed here temporarily for git diff purposes
425+
from .parse_gifti_fast import GiftiImageParser
426+
GiftiImage.parser = GiftiImageParser
397427

398428
if darrays is None:
399429
darrays = []
@@ -501,7 +531,7 @@ def getArraysFromIntent(self, intent):
501531

502532
def print_summary(self):
503533
print('----start----')
504-
print('Source filename: ', self.filename)
534+
print('Source filename: ', self.get_filename())
505535
print('Number of data arrays: ', self.numDA)
506536
print('Version: ', self.version)
507537
if self.meta is not None:
@@ -536,22 +566,6 @@ def to_xml(self, enc='utf-8'):
536566
<!DOCTYPE GIFTI SYSTEM "http://www.nitrc.org/frs/download.php/115/gifti.dtd">
537567
""" + xml.XmlSerializable.to_xml(self, enc)
538568

539-
@classmethod
540-
def from_file_map(klass, file_map):
541-
""" Load a Gifti image from a file_map
542-
543-
Parameters
544-
file_map : string
545-
546-
Returns
547-
-------
548-
img : GiftiImage
549-
Returns a GiftiImage
550-
"""
551-
from .parse_gifti_fast import parse_gifti_file
552-
return parse_gifti_file(
553-
fptr=file_map['image'].get_prepare_fileobj('rb'))
554-
555569
def to_file_map(self, file_map=None):
556570
""" Save the current image to the specified file_map
557571
@@ -562,40 +576,31 @@ def to_file_map(self, file_map=None):
562576
Returns
563577
-------
564578
None
565-
566-
Notes
567-
-----
568-
We write all files with utf-8 encoding, and specify this at the top of
569-
the XML file with the ``encoding`` attribute.
570-
571-
The Gifti spec suggests using the following suffixes to your
572-
filename when saving each specific type of data:
573-
574-
.gii
575-
Generic GIFTI File
576-
.coord.gii
577-
Coordinates
578-
.func.gii
579-
Functional
580-
.label.gii
581-
Labels
582-
.rgba.gii
583-
RGB or RGBA
584-
.shape.gii
585-
Shape
586-
.surf.gii
587-
Surface
588-
.tensor.gii
589-
Tensors
590-
.time.gii
591-
Time Series
592-
.topo.gii
593-
Topology
594-
595-
The Gifti file is stored in endian convention of the current machine.
596579
"""
597-
# Our giftis are always utf-8 encoded - see GiftiImage.to_xml
598580
if file_map is None:
599581
file_map = self.file_map
600582
f = file_map['image'].get_prepare_fileobj('wb')
601583
f.write(self.to_xml())
584+
585+
@classmethod
586+
def from_file_map(klass, file_map, buffer_size=35000000):
587+
""" Load a Gifti image from a file_map
588+
589+
Parameters
590+
file_map : string
591+
592+
Returns
593+
-------
594+
img : GiftiImage
595+
Returns a GiftiImage
596+
"""
597+
parser = klass.parser(buffer_size=buffer_size)
598+
parser.parse(fptr=file_map['image'].get_prepare_fileobj('rb'))
599+
img = parser.img
600+
return img
601+
602+
@classmethod
603+
def from_filename(klass, filename, buffer_size=35000000):
604+
file_map = klass.filespec_to_file_map(filename)
605+
img = klass.from_file_map(file_map, buffer_size=buffer_size)
606+
return img

0 commit comments

Comments
 (0)