@@ -33,43 +33,41 @@ class GiftiParseError(ExpatError):
33
33
34
34
def read_data_block (encoding , endian , ordering , datatype , shape , data ):
35
35
""" Tries to unzip, decode, parse the funny string data """
36
- ord = array_index_order_codes .npcode [ordering ]
37
36
enclabel = gifti_encoding_codes .label [encoding ]
37
+ dtype = data_type_codes .type [datatype ]
38
38
if enclabel == 'ASCII' :
39
39
# GIFTI_ENCODING_ASCII
40
40
c = StringIO (data )
41
- da = np .loadtxt (c )
42
- da = da .astype (data_type_codes .type [datatype ])
41
+ da = np .loadtxt (c , dtype = dtype )
43
42
return da # independent of the endianness
44
43
45
- elif enclabel == 'B64BIN' :
46
- # GIFTI_ENCODING_B64BIN
47
- dec = base64 .b64decode (data .encode ('ascii' ))
48
- dt = data_type_codes .type [datatype ]
49
- sh = tuple (shape )
50
- newarr = np .frombuffer (dec , dtype = dt )
51
- if len (newarr .shape ) != len (sh ):
52
- newarr = newarr .reshape (sh , order = ord )
53
-
54
- elif enclabel == 'B64GZ' :
55
- # GIFTI_ENCODING_B64GZ
56
- # convert to bytes array for python 3.2
57
- # http://www.diveintopython3.net/strings.html#byte-arrays
58
- dec = base64 .b64decode (data .encode ('ascii' ))
59
- zdec = zlib .decompress (dec )
60
- dt = data_type_codes .type [datatype ]
61
- sh = tuple (shape )
62
- newarr = np .frombuffer (zdec , dtype = dt )
63
- if len (newarr .shape ) != len (sh ):
64
- newarr = newarr .reshape (sh , order = ord )
65
-
66
44
elif enclabel == 'External' :
67
45
# GIFTI_ENCODING_EXTBIN
68
46
raise NotImplementedError ("In what format are the external files?" )
69
47
70
- else :
48
+ elif enclabel not in ( 'B64BIN' , 'B64GZ' ) :
71
49
return 0
72
50
51
+ # Numpy arrays created from bytes objects are read-only.
52
+ # Neither b64decode nor decompress will return bytearrays, and there
53
+ # are not equivalents to fobj.readinto to allow us to pass them, so
54
+ # there is not a simple way to avoid making copies.
55
+ # If this becomes a problem, we should write a decoding interface with
56
+ # a tunable chunk size.
57
+ dec = base64 .b64decode (data .encode ('ascii' ))
58
+ if enclabel == 'B64BIN' :
59
+ # GIFTI_ENCODING_B64BIN
60
+ buff = bytearray (dec )
61
+ else :
62
+ # GIFTI_ENCODING_B64GZ
63
+ buff = bytearray (zlib .decompress (dec ))
64
+ del dec
65
+
66
+ sh = tuple (shape )
67
+ newarr = np .frombuffer (buff , dtype = dtype )
68
+ if len (newarr .shape ) != len (sh ):
69
+ newarr = newarr .reshape (sh , order = array_index_order_codes .npcode [ordering ])
70
+
73
71
# check if we need to byteswap
74
72
required_byteorder = gifti_endian_codes .byteorder [endian ]
75
73
if (required_byteorder in ('big' , 'little' ) and
0 commit comments