Skip to content

FIX: Coerce data types on writing GIFTI DataArrays #806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions nibabel/gifti/gifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,21 @@ def _to_xml_element(self):
return DataTag(dataarray, encoding, datatype, ordering).to_xml()


def _data_tag_element(dataarray, encoding, datatype, ordering):
def _data_tag_element(dataarray, encoding, dtype, ordering):
""" Creates data tag with given `encoding`, returns as XML element
"""
import zlib
ord = array_index_order_codes.npcode[ordering]
order = array_index_order_codes.npcode[ordering]
enclabel = gifti_encoding_codes.label[encoding]
if enclabel == 'ASCII':
da = _arr2txt(dataarray, datatype)
# XXX Accommodating data_tag API
# On removal (nibabel 4.0) drop str case
da = _arr2txt(dataarray, dtype if isinstance(dtype, str) else KIND2FMT[dtype.kind])
elif enclabel in ('B64BIN', 'B64GZ'):
out = dataarray.tostring(ord)
# XXX Accommodating data_tag API - don't try to fix dtype
if isinstance(dtype, str):
dtype = dataarray.dtype
out = np.asanyarray(dataarray, dtype).tostring(order)
if enclabel == 'B64GZ':
out = zlib.compress(out)
da = base64.b64encode(out).decode()
Expand Down Expand Up @@ -462,11 +467,10 @@ def _to_xml_element(self):
if self.coordsys is not None:
data_array.append(self.coordsys._to_xml_element())
# write data array depending on the encoding
dt_kind = data_type_codes.dtype[self.datatype].kind
data_array.append(
_data_tag_element(self.data,
gifti_encoding_codes.specs[self.encoding],
KIND2FMT[dt_kind],
data_type_codes.dtype[self.datatype],
self.ind_ord))

return data_array
Expand Down
18 changes: 18 additions & 0 deletions nibabel/gifti/tests/test_gifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from nibabel.testing import clear_and_catch_warnings
from .test_parse_gifti_fast import (DATA_FILE1, DATA_FILE2, DATA_FILE3,
DATA_FILE4, DATA_FILE5, DATA_FILE6)
import itertools


def test_gifti_image():
Expand Down Expand Up @@ -400,3 +401,20 @@ def test_data_array_round_trip():
gio = GiftiImage.from_file_map(fmap)
vertices = gio.darrays[0].data
assert_array_equal(vertices, verts)


def test_darray_dtype_coercion_failures():
dtypes = (np.uint8, np.int32, np.int64, np.float32, np.float64)
encodings = ('ASCII', 'B64BIN', 'B64GZ')
for data_dtype, darray_dtype, encoding in itertools.product(dtypes,
dtypes,
encodings):
da = GiftiDataArray(np.arange(10).astype(data_dtype),
encoding=encoding,
intent='NIFTI_INTENT_NODE_INDEX',
datatype=darray_dtype)
gii = GiftiImage(darrays=[da])
gii_copy = GiftiImage.from_bytes(gii.to_bytes())
da_copy = gii_copy.darrays[0]
assert_equal(np.dtype(da_copy.data.dtype), np.dtype(darray_dtype))
assert_array_equal(da_copy.data, da.data)