Skip to content

Commit f2a0467

Browse files
committed
Merge branch 'stephan-master' into main-master
* stephan-master: BF - Writing data array encoding header according to specs ENH - Supporting two types of data array specification strings BF - binary gifti write. Add test BF - Reading binary encoded Gifti without line break
2 parents 435193c + 291fe15 commit f2a0467

File tree

4 files changed

+107
-15
lines changed

4 files changed

+107
-15
lines changed

nibabel/gifti/gifti.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,32 +196,33 @@ def data_tag(dataarray, encoding, datatype, ordering):
196196
else:
197197
ord = 'C'
198198

199-
if encoding == "GIFTI_ENCODING_ASCII":
199+
if encoding == gifti_encoding_codes.giistring[1] or \
200+
encoding == gifti_encoding_codes.specs[1]:
200201
c = BytesIO()
201202
# np.savetxt(c, dataarray, format, delimiter for columns)
202203
np.savetxt(c, dataarray, datatype, ' ')
203204
c.seek(0)
204205
da = c.read()
205206

206-
elif encoding == "GIFTI_ENCODING_B64BIN":
207-
cout = StringIO()
208-
base64.encode(dataarray.tostring(ord), cout)
209-
cout.seek(0)
210-
da = cout.read()
207+
elif encoding == gifti_encoding_codes.giistring[2] or \
208+
encoding == gifti_encoding_codes.specs[2]:
209+
da = base64.encodestring(dataarray.tostring(ord))
211210

212-
elif encoding == "GIFTI_ENCODING_B64GZ":
211+
elif encoding == gifti_encoding_codes.giistring[3] or \
212+
encoding == gifti_encoding_codes.specs[3]:
213213
# first compress
214214
comp = zlib.compress(dataarray.tostring(ord))
215215
da = base64.encodestring(comp)
216216
da = da.decode()
217217

218-
elif encoding == "GIFTI_ENCODING_EXTBIN":
218+
elif encoding == gifti_encoding_codes.giistring[4] or \
219+
encoding == gifti_encoding_codes.specs[4]:
219220
raise NotImplementedError("In what format are the external files?")
220221
da = ''
221222
else:
222223
da = ''
223224

224-
return "<Data>\n"+da+"</Data>\n"
225+
return "<Data>"+da+"</Data>\n"
225226

226227
class GiftiDataArray(object):
227228

@@ -323,7 +324,7 @@ def to_xml(self):
323324

324325
# write data array depending on the encoding
325326
result += data_tag(self.data, \
326-
gifti_encoding_codes.giistring[self.encoding],\
327+
gifti_encoding_codes.specs[self.encoding],\
327328
data_type_codes.fmt[self.datatype],\
328329
self.ind_ord)
329330

@@ -348,7 +349,7 @@ def to_xml_open(self):
348349
array_index_order_codes.label[self.ind_ord], \
349350
str(self.num_dim), \
350351
str(di), \
351-
gifti_encoding_codes.giistring[self.encoding], \
352+
gifti_encoding_codes.specs[self.encoding], \
352353
gifti_endian_codes.giistring[self.endian], \
353354
self.ext_fname,
354355
self.ext_offset,
@@ -364,7 +365,7 @@ def print_summary(self):
364365
print 'ArrayIndexingOrder: ', array_index_order_codes.label[self.ind_ord]
365366
print 'Dimensionality: ', self.num_dim
366367
print 'Dimensions: ', self.dims
367-
print 'Encoding: ', gifti_encoding_codes.giistring[self.encoding]
368+
print 'Encoding: ', gifti_encoding_codes.specs[self.encoding]
368369
print 'Endian: ', gifti_endian_codes.giistring[self.endian]
369370
print 'ExternalFileName: ', self.ext_fname
370371
print 'ExternalFileOffset: ', self.ext_offset

nibabel/gifti/parse_gifti_fast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def read_data_block(encoding, endian, ordering, datatype, shape, data):
4444
dec = base64.decodestring(data.encode('ascii'))
4545
dt = data_type_codes.type[datatype]
4646
sh = tuple(shape)
47-
newarr = np.fromstring(zdec, dtype = dt, sep = '\n', count = c)
47+
newarr = np.fromstring(dec, dtype = dt)
4848
if len(newarr.shape) == len(sh):
4949
return newarr
5050
else:
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE GIFTI SYSTEM "http://www.nitrc.org/frs/download.php/115/gifti.dtd">
3+
<GIFTI Version="1.0" NumberOfDataArrays="2">
4+
<MetaData>
5+
<MD>
6+
<Name><![CDATA[gifticlib-version]]></Name>
7+
<Value><![CDATA[gifti library version 1.08, 8 March, 2010]]></Value>
8+
</MD>
9+
</MetaData>
10+
<LabelTable/>
11+
<DataArray Intent="NIFTI_INTENT_POINTSET"
12+
DataType="NIFTI_TYPE_FLOAT32"
13+
ArrayIndexingOrder="RowMajorOrder"
14+
Dimensionality="2"
15+
Dim0="10"
16+
Dim1="3"
17+
Encoding="GIFTI_ENCODING_B64BIN"
18+
Endian="GIFTI_ENDIAN_LITTLE"
19+
ExternalFileName=""
20+
ExternalFileOffset="">
21+
<MetaData/>
22+
<CoordinateSystemTransformMatrix>
23+
<DataSpace><![CDATA[NIFTI_XFORM_UNKNOWN]]></DataSpace>
24+
<TransformedSpace><![CDATA[NIFTI_XFORM_UNKNOWN]]></TransformedSpace>
25+
<MatrixData>
26+
1.000000 0.000000 0.000000 0.000000
27+
0.000000 1.000000 0.000000 0.000000
28+
0.000000 0.000000 1.000000 0.000000
29+
0.000000 0.000000 0.000000 1.000000
30+
</MatrixData>
31+
</CoordinateSystemTransformMatrix>
32+
<Data>5ywbQ7+UB0NDncRC+VYMQ5QMPkPbfpJCIlwdQ836REPyUKdCNXYrQ8ZvCUMh8ZxCwosUQ5MiwkJv
33+
7YNC/un2QtTv3kLYtoRCFk8HQ4ZJSkOkhhFCFEgyQz6YIkNSARdCYhwyQ4+T5kIvuGRC2tAOQ26k
34+
pUIqLfhB
35+
</Data>
36+
</DataArray>
37+
<DataArray Intent="NIFTI_INTENT_TRIANGLE"
38+
DataType="NIFTI_TYPE_INT32"
39+
ArrayIndexingOrder="RowMajorOrder"
40+
Dimensionality="2"
41+
Dim0="10"
42+
Dim1="3"
43+
Encoding="GIFTI_ENCODING_B64BIN"
44+
Endian="GIFTI_ENDIAN_LITTLE"
45+
ExternalFileName=""
46+
ExternalFileOffset="">
47+
<MetaData/>
48+
<CoordinateSystemTransformMatrix>
49+
<DataSpace><![CDATA[NIFTI_XFORM_UNKNOWN]]></DataSpace>
50+
<TransformedSpace><![CDATA[NIFTI_XFORM_UNKNOWN]]></TransformedSpace>
51+
<MatrixData>
52+
1.000000 0.000000 0.000000 0.000000
53+
0.000000 1.000000 0.000000 0.000000
54+
0.000000 0.000000 1.000000 0.000000
55+
0.000000 0.000000 0.000000 1.000000
56+
</MatrixData>
57+
</CoordinateSystemTransformMatrix>
58+
<Data>AhkAAANGAAACZAAABTcAAAJkAAADRgAAAmQAAAU3AACDEQAAA0YAAEIGAAAFNwAAgxEAAANkAAAC
59+
ZAAABGQAAAJkAAADZAAAAmQAAARkAAACGQAAA2QAAMUNAAAEZAAAYwQAAAJGAACILwAABGQAAIgv
60+
AAACRgAA
61+
</Data>
62+
</DataArray>
63+
</GIFTI>

nibabel/gifti/tests/test_giftiio.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@
3131
DATA_FILE2 = pjoin(IO_DATA_PATH, 'gzipbase64.gii')
3232
DATA_FILE3 = pjoin(IO_DATA_PATH, 'label.gii')
3333
DATA_FILE4 = pjoin(IO_DATA_PATH, 'rh.shape.curv.gii')
34+
DATA_FILE5 = pjoin(IO_DATA_PATH, 'base64bin.gii')
3435

35-
datafiles = [DATA_FILE1, DATA_FILE2, DATA_FILE3, DATA_FILE4]
36-
numda = [2, 1, 1, 1]
36+
datafiles = [DATA_FILE1, DATA_FILE2, DATA_FILE3, DATA_FILE4, DATA_FILE5]
37+
numda = [2, 1, 1, 1, 2]
3738

3839
DATA_FILE1_darr1 = np.array(
3940
[[-16.07201 , -66.187515, 21.266994],
@@ -65,6 +66,28 @@
6566
[-0.45624232],
6667
[-0.31101292]], dtype=np.float32)
6768

69+
DATA_FILE5_darr1 = np.array([[ 155.17539978, 135.58103943, 98.30715179],
70+
[ 140.33973694, 190.0491333 , 73.24776459],
71+
[ 157.3598938 , 196.97969055, 83.65809631],
72+
[ 171.46174622, 137.43661499, 78.4709549 ],
73+
[ 148.54592896, 97.06752777, 65.96373749],
74+
[ 123.45701599, 111.46841431, 66.3571167 ],
75+
[ 135.30892944, 202.28720093, 36.38148499],
76+
[ 178.28155518, 162.59469604, 37.75128937],
77+
[ 178.11087036, 115.28820038, 57.17986679],
78+
[ 142.81582642, 82.82115173, 31.02205276]], dtype=np.float32)
79+
80+
81+
DATA_FILE5_darr2 = np.array([[ 6402, 17923, 25602],
82+
[14085, 25602, 17923],
83+
[25602, 14085, 4483],
84+
[17923, 1602, 14085],
85+
[ 4483, 25603, 25602],
86+
[25604, 25602, 25603],
87+
[25602, 25604, 6402],
88+
[25603, 3525, 25604],
89+
[ 1123, 17922, 12168],
90+
[25604, 12168, 17922]], dtype=np.int32)
6891

6992
def test_read_ordering():
7093
# DATA_FILE1 has an expected darray[0].data shape of (3,3). However if we
@@ -116,6 +139,11 @@ def test_dataarray3():
116139
def test_dataarray4():
117140
img4 = gi.read(DATA_FILE4)
118141
assert_array_almost_equal(img4.darrays[0].data[:10], DATA_FILE4_darr1)
142+
143+
def test_dataarray5():
144+
img3 = gi.read(DATA_FILE5)
145+
assert_array_almost_equal(img3.darrays[0].data, DATA_FILE5_darr1)
146+
assert_array_almost_equal(img3.darrays[1].data, DATA_FILE5_darr2)
119147

120148
def test_readwritedata():
121149
img = gi.read(DATA_FILE2)

0 commit comments

Comments
 (0)