@@ -31,7 +31,7 @@ class GiftiParseError(ExpatError):
31
31
""" Gifti-specific parsing error """
32
32
33
33
34
- def read_data_block (darray , fname , data ):
34
+ def read_data_block (darray , fname , data , mmap ):
35
35
"""Parses data from a <Data> element, or loads from an external file.
36
36
37
37
Parameters
@@ -46,10 +46,25 @@ def read_data_block(darray, fname, data):
46
46
data : str or None
47
47
Data to parse, or None if data is in an external file
48
48
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
+
49
59
Returns
50
60
-------
51
61
numpy.ndarray containing the parsed data
52
62
"""
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'
53
68
enclabel = gifti_encoding_codes .label [darray .encoding ]
54
69
dtype = data_type_codes .type [darray .datatype ]
55
70
@@ -114,13 +129,17 @@ def _str2int(in_str):
114
129
115
130
class GiftiImageParser (XmlParser ):
116
131
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 ):
118
134
super (GiftiImageParser , self ).__init__ (encoding = encoding ,
119
135
buffer_size = buffer_size ,
120
136
verbose = verbose )
121
137
# output
122
138
self .img = None
123
139
140
+ # Queried when loading data from <Data> elements - see read_data_block
141
+ self .mmap = mmap
142
+
124
143
# finite state machine stack
125
144
self .fsm_state = []
126
145
@@ -358,7 +377,8 @@ def flush_chardata(self):
358
377
c .close ()
359
378
360
379
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 )
362
382
# update the endianness according to the
363
383
# current machine setting
364
384
self .endian = gifti_endian_codes .code [sys .byteorder ]
0 commit comments