Skip to content

Commit 2838c49

Browse files
committed
FIX: Coerce data types on writing GIFTI DataArrays
Includes a hack to keep the old data_tag API functional
1 parent b021b2d commit 2838c49

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

nibabel/gifti/gifti.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,16 +270,21 @@ def _to_xml_element(self):
270270
return DataTag(dataarray, encoding, datatype, ordering).to_xml()
271271

272272

273-
def _data_tag_element(dataarray, encoding, datatype, ordering):
273+
def _data_tag_element(dataarray, encoding, dtype, ordering):
274274
""" Creates data tag with given `encoding`, returns as XML element
275275
"""
276276
import zlib
277-
ord = array_index_order_codes.npcode[ordering]
277+
order = array_index_order_codes.npcode[ordering]
278278
enclabel = gifti_encoding_codes.label[encoding]
279279
if enclabel == 'ASCII':
280-
da = _arr2txt(dataarray, datatype)
280+
# XXX Accommodating data_tag API
281+
# On removal (nibabel 4.0) drop str case
282+
da = _arr2txt(dataarray, dtype if isinstance(dtype, str) else KIND2FMT[dtype.kind])
281283
elif enclabel in ('B64BIN', 'B64GZ'):
282-
out = dataarray.tostring(ord)
284+
# XXX Accommodating data_tag API - don't try to fix dtype
285+
if isinstance(dtype, str):
286+
dtype = dataarray.dtype
287+
out = np.asanyarray(dataarray, dtype).tostring(order)
283288
if enclabel == 'B64GZ':
284289
out = zlib.compress(out)
285290
da = base64.b64encode(out).decode()
@@ -462,11 +467,10 @@ def _to_xml_element(self):
462467
if self.coordsys is not None:
463468
data_array.append(self.coordsys._to_xml_element())
464469
# write data array depending on the encoding
465-
dt_kind = data_type_codes.dtype[self.datatype].kind
466470
data_array.append(
467471
_data_tag_element(self.data,
468472
gifti_encoding_codes.specs[self.encoding],
469-
KIND2FMT[dt_kind],
473+
data_type_codes.dtype[self.datatype],
470474
self.ind_ord))
471475

472476
return data_array

nibabel/gifti/tests/test_gifti.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ def test_data_array_round_trip():
403403
assert_array_equal(vertices, verts)
404404

405405

406-
def test_type_coercion():
406+
def test_darray_dtype_coercion_failures():
407407
dtypes = (np.uint8, np.int32, np.int64, np.float32, np.float64)
408408
encodings = ('ASCII', 'B64BIN', 'B64GZ')
409409
for data_dtype, darray_dtype, encoding in itertools.product(dtypes,

0 commit comments

Comments
 (0)