Skip to content

Commit 5b76c8e

Browse files
committed
TEST: Validate dataobj.__array__(dtype)
1 parent ccd7453 commit 5b76c8e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

nibabel/tests/test_proxy_api.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,27 @@ def validate_asarray(self, pmaker, params):
132132
# Shape matches expected shape
133133
assert_equal(out.shape, params['shape'])
134134

135+
def validate_array_interface_with_dtype(self, pmaker, params):
136+
# Check proxy returns expected array from asarray
137+
prox, fio, hdr = pmaker()
138+
orig = np.array(prox, dtype=None)
139+
assert_array_equal(orig, params['arr_out'])
140+
assert_dt_equal(orig.dtype, params['dtype_out'])
141+
142+
for dtype in np.sctypes['float'] + np.sctypes['int'] + np.sctypes['uint']:
143+
# Directly coerce with a dtype
144+
direct = dtype(prox)
145+
assert_almost_equal(direct, orig.astype(dtype))
146+
assert_dt_equal(direct.dtype, np.dtype(dtype))
147+
assert_equal(direct.shape, params['shape'])
148+
# All three methods should produce equivalent results
149+
for arrmethod in (np.array, np.asarray, np.asanyarray):
150+
out = arrmethod(prox, dtype=dtype)
151+
assert_array_equal(out, direct)
152+
assert_dt_equal(out.dtype, np.dtype(dtype))
153+
# Shape matches expected shape
154+
assert_equal(out.shape, params['shape'])
155+
135156
def validate_get_scaled(self, pmaker, params):
136157
# Check proxy returns expected array from asarray
137158
prox, fio, hdr = pmaker()

0 commit comments

Comments
 (0)