13
13
import numpy as np
14
14
15
15
from .. import xmlutils as xml
16
+ from ..filebasedimages import FileBasedImage
16
17
from ..nifti1 import data_type_codes , xform_codes , intent_codes
17
18
from .util import (array_index_order_codes , gifti_encoding_codes ,
18
19
gifti_endian_codes , KIND2FMT )
@@ -190,6 +191,7 @@ def data_tag(dataarray, encoding, datatype, ordering):
190
191
class DataTag (xml .XmlSerializable ):
191
192
def __init__ (self , * args ):
192
193
self .args = args
194
+
193
195
def _to_xml_element (self ):
194
196
return _data_tag_element (* self .args )
195
197
@@ -384,10 +386,15 @@ def metadata(self):
384
386
return self .meta .metadata
385
387
386
388
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 )
388
397
389
- def __init__ (self , meta = None , labeltable = None , darrays = None ,
390
- version = "1.0" ):
391
398
if darrays is None :
392
399
darrays = []
393
400
if meta is None :
@@ -511,7 +518,6 @@ def print_summary(self):
511
518
print (da .print_summary ())
512
519
print ('----end----' )
513
520
514
-
515
521
def _to_xml_element (self ):
516
522
GIFTI = xml .Element ('GIFTI' , attrib = {
517
523
'Version' : self .version ,
@@ -529,3 +535,67 @@ def to_xml(self, enc='utf-8'):
529
535
return b"""<?xml version="1.0" encoding="UTF-8"?>
530
536
<!DOCTYPE GIFTI SYSTEM "http://www.nitrc.org/frs/download.php/115/gifti.dtd">
531
537
""" + 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