@@ -31,7 +31,7 @@ class GiftiParseError(ExpatError):
3131 """ Gifti-specific parsing error """
3232
3333
34- def read_data_block (darray , fname , data ):
34+ def read_data_block (darray , fname , data , mmap ):
3535 """Parses data from a <Data> element, or loads from an external file.
3636
3737 Parameters
@@ -46,10 +46,25 @@ def read_data_block(darray, fname, data):
4646 data : str or None
4747 Data to parse, or None if data is in an external file
4848
49+ mmap : {True, False, 'c', 'r', 'r+'}
50+ Controls the use of numpy memory mapping for reading data. Only has
51+ an effect when loading GIFTI images with data stored in external files
52+ (``DataArray`` elements with an ``Encoding`` equal to
53+ ``ExternalFileBinary``). If ``False``, do not try numpy ``memmap``
54+ for data array. If one of ``{'c', 'r', 'r+'}``, try numpy ``memmap``
55+ with ``mode=mmap``. A `mmap` value of ``True`` gives the same
56+ behavior as ``mmap='c'``. If the file cannot be memory-mapped, ignore
57+ `mmap` value and read array from file.
58+
4959 Returns
5060 -------
5161 numpy.ndarray containing the parsed data
5262 """
63+ if mmap not in (True , False , 'c' , 'r' , 'r+' ):
64+ raise ValueError ("mmap value should be one of True, False, 'c', "
65+ "'r', 'r+'" )
66+ if mmap is True :
67+ mmap = 'c'
5368 enclabel = gifti_encoding_codes .label [darray .encoding ]
5469 dtype = data_type_codes .type [darray .datatype ]
5570
@@ -114,13 +129,17 @@ def _str2int(in_str):
114129
115130class GiftiImageParser (XmlParser ):
116131
117- def __init__ (self , encoding = None , buffer_size = 35000000 , verbose = 0 ):
132+ def __init__ (self , encoding = None , buffer_size = 35000000 , verbose = 0 ,
133+ mmap = True ):
118134 super (GiftiImageParser , self ).__init__ (encoding = encoding ,
119135 buffer_size = buffer_size ,
120136 verbose = verbose )
121137 # output
122138 self .img = None
123139
140+ # Queried when loading data from <Data> elements - see read_data_block
141+ self .mmap = mmap
142+
124143 # finite state machine stack
125144 self .fsm_state = []
126145
@@ -358,7 +377,8 @@ def flush_chardata(self):
358377 c .close ()
359378
360379 elif self .write_to == 'Data' :
361- self .da .data = read_data_block (self .da , self .fname , data )
380+ self .da .data = read_data_block (self .da , self .fname , data ,
381+ self .mmap )
362382 # update the endianness according to the
363383 # current machine setting
364384 self .endian = gifti_endian_codes .code [sys .byteorder ]
0 commit comments