Skip to content

Commit dd9bea3

Browse files
committed
BF+TEST - set data array datatype correctly
1 parent 02310a5 commit dd9bea3

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

nibabel/gifti/gifti.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ def from_array(klass,
224224
intent,
225225
datatype = None,
226226
encoding = "GIFTI_ENCODING_B64GZ",
227-
endian = "GIFTI_ENDIAN_LITTLE",
227+
endian = sys.byteorder,
228228
coordsys = None,
229-
ordering = "RowMajorOrder",
229+
ordering = "C",
230230
meta = None):
231231
""" Creates a new Gifti data array
232232
@@ -235,33 +235,35 @@ def from_array(klass,
235235
darray : ndarray
236236
NumPy data array
237237
intent : string
238-
NIFTI intent codes, see util.intent_codes
239-
datatype : string
240-
NIFTI data type codes, see util.data_type_codes
238+
NIFTI intent code, see nifti1.intent_codes
239+
datatype : None or string, optional
240+
NIFTI data type codes, see nifti1.data_type_codes
241241
If None, the datatype of the NumPy array is taken.
242-
encoding : string, default: GIFTI_ENCODING_B64GZ
243-
Encoding of the data, see util.gifti_encoding_codes
244-
endian : string, default: GIFTI_ENDIAN_LITTLE
245-
The Endianness to store the data array.
246-
Should correspond to the machine endianness.
242+
encoding : string, optionaal
243+
Encoding of the data, see util.gifti_encoding_codes;
244+
default: GIFTI_ENCODING_B64GZ
245+
endian : string, optional
246+
The Endianness to store the data array. Should correspond to the
247+
machine endianness. default: system byteorder
247248
coordsys : GiftiCoordSystem, optional
248249
If None, a identity transformation is taken.
249-
ordering : string, default: RowMajorOrder
250-
The ordering of the array. see util.array_index_order_codes
250+
ordering : string, optional
251+
The ordering of the array. see util.array_index_order_codes;
252+
default: RowMajorOrder - C ordering
251253
meta : None or dict, optional
252254
A dictionary for metadata information. If None, gives empty dict.
253255
254256
Returns
255257
-------
256-
da : instance of class `klass`
258+
da : instance of our own class
257259
"""
258260
if meta is None:
259261
meta = {}
260262
cda = klass(darray)
261263
cda.num_dim = len(darray.shape)
262264
cda.dims = list(darray.shape)
263265
if datatype == None:
264-
cda.datatype = data_type_codes.code[darray.dtype.type]
266+
cda.datatype = data_type_codes.code[darray.dtype]
265267
else:
266268
cda.datatype = data_type_codes.code[datatype]
267269
cda.intent = intent_codes.code[intent]
@@ -271,7 +273,6 @@ def from_array(klass,
271273
cda.coordsys = coordsys
272274
cda.ind_ord = array_index_order_codes.code[ordering]
273275
cda.meta = GiftiMetaData.from_dict(meta)
274-
275276
return cda
276277

277278
def to_xml(self):

nibabel/gifti/tests/test_gifti.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33

44
import numpy as np
55

6-
from ..gifti import GiftiImage
6+
from ...nifti1 import data_type_codes, intent_codes
7+
8+
from ..gifti import GiftiImage, GiftiDataArray
79

810
from numpy.testing import (assert_array_almost_equal,
911
assert_array_equal)
1012

1113
from nose.tools import assert_true, assert_equal, assert_raises
1214

1315

14-
def test_gifti_data():
16+
def test_gifti_image():
1517
# Check that we're not modifying the default empty list in the default
1618
# arguments.
1719
gi = GiftiImage()
@@ -22,3 +24,15 @@ def test_gifti_data():
2224
gi = GiftiImage()
2325
assert_equal(gi.darrays, [])
2426

27+
28+
def test_dataarray():
29+
for dt_code in data_type_codes.value_set():
30+
data_type = data_type_codes.type[dt_code]
31+
if data_type is np.void: # not supported
32+
continue
33+
arr = np.zeros((10,3), dtype=data_type)
34+
da = GiftiDataArray.from_array(arr, 'triangle')
35+
assert_equal(da.datatype, data_type_codes[arr.dtype])
36+
bs_arr = arr.byteswap().newbyteorder()
37+
da = GiftiDataArray.from_array(bs_arr, 'triangle')
38+
assert_equal(da.datatype, data_type_codes[arr.dtype])

0 commit comments

Comments
 (0)