@@ -264,16 +264,21 @@ def _to_xml_element(self):
264264 return DataTag (dataarray , encoding , datatype , ordering ).to_xml ()
265265
266266
267- def _data_tag_element (dataarray , encoding , datatype , ordering ):
267+ def _data_tag_element (dataarray , encoding , dtype , ordering ):
268268 """ Creates data tag with given `encoding`, returns as XML element
269269 """
270270 import zlib
271- ord = array_index_order_codes .npcode [ordering ]
271+ order = array_index_order_codes .npcode [ordering ]
272272 enclabel = gifti_encoding_codes .label [encoding ]
273273 if enclabel == 'ASCII' :
274- da = _arr2txt (dataarray , datatype )
274+ # XXX Accommodating data_tag API
275+ # On removal (nibabel 4.0) drop str case
276+ da = _arr2txt (dataarray , dtype if isinstance (dtype , str ) else KIND2FMT [dtype .kind ])
275277 elif enclabel in ('B64BIN' , 'B64GZ' ):
276- out = dataarray .tostring (ord )
278+ # XXX Accommodating data_tag API - don't try to fix dtype
279+ if isinstance (dtype , str ):
280+ dtype = dataarray .dtype
281+ out = np .asanyarray (dataarray , dtype ).tostring (order )
277282 if enclabel == 'B64GZ' :
278283 out = zlib .compress (out )
279284 da = base64 .b64encode (out ).decode ()
@@ -456,11 +461,10 @@ def _to_xml_element(self):
456461 if self .coordsys is not None :
457462 data_array .append (self .coordsys ._to_xml_element ())
458463 # write data array depending on the encoding
459- dt_kind = data_type_codes .dtype [self .datatype ].kind
460464 data_array .append (
461465 _data_tag_element (self .data ,
462466 gifti_encoding_codes .specs [self .encoding ],
463- KIND2FMT [ dt_kind ],
467+ data_type_codes . dtype [ self . datatype ],
464468 self .ind_ord ))
465469
466470 return data_array
0 commit comments