1313import numpy as np
1414
1515from .. import xmlutils as xml
16+ from ..filebasedimages import FileBasedImage
1617from ..nifti1 import data_type_codes , xform_codes , intent_codes
1718from .util import (array_index_order_codes , gifti_encoding_codes ,
1819 gifti_endian_codes , KIND2FMT )
@@ -190,6 +191,7 @@ def data_tag(dataarray, encoding, datatype, ordering):
190191 class DataTag (xml .XmlSerializable ):
191192 def __init__ (self , * args ):
192193 self .args = args
194+
193195 def _to_xml_element (self ):
194196 return _data_tag_element (* self .args )
195197
@@ -384,10 +386,15 @@ def metadata(self):
384386 return self .meta .metadata
385387
386388
387- class GiftiImage (xml .XmlSerializable ):
389+ class GiftiImage (FileBasedImage , xml .XmlSerializable ):
390+ valid_exts = ('.gii' ,)
391+ files_types = (('image' , '.gii' ),)
392+
393+ def __init__ (self , header = None , extra = None , file_map = None , meta = None ,
394+ labeltable = None , darrays = None , version = "1.0" ):
395+ FileBasedImage .__init__ (self , header = header , extra = extra ,
396+ file_map = file_map )
388397
389- def __init__ (self , meta = None , labeltable = None , darrays = None ,
390- version = "1.0" ):
391398 if darrays is None :
392399 darrays = []
393400 if meta is None :
@@ -511,7 +518,6 @@ def print_summary(self):
511518 print (da .print_summary ())
512519 print ('----end----' )
513520
514-
515521 def _to_xml_element (self ):
516522 GIFTI = xml .Element ('GIFTI' , attrib = {
517523 'Version' : self .version ,
@@ -529,3 +535,67 @@ def to_xml(self, enc='utf-8'):
529535 return b"""<?xml version="1.0" encoding="UTF-8"?>
530536<!DOCTYPE GIFTI SYSTEM "http://www.nitrc.org/frs/download.php/115/gifti.dtd">
531537""" + xml .XmlSerializable .to_xml (self , enc )
538+
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+
555+ def to_file_map (self , file_map = None ):
556+ """ Save the current image to the specified file_map
557+
558+ Parameters
559+ ----------
560+ file_map : string
561+
562+ Returns
563+ -------
564+ 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.
596+ """
597+ # Our giftis are always utf-8 encoded - see GiftiImage.to_xml
598+ if file_map is None :
599+ file_map = self .file_map
600+ f = file_map ['image' ].get_prepare_fileobj ('wb' )
601+ f .write (self .to_xml ())
0 commit comments