@@ -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
0 commit comments