Skip to content

Commit 45a4967

Browse files
committed
TEST: Validate dataobj.__array__(dtype)
1 parent b53c9eb commit 45a4967

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
@@ -131,6 +131,27 @@ def validate_asarray(self, pmaker, params):
131131
# Shape matches expected shape
132132
assert_equal(out.shape, params['shape'])
133133

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

0 commit comments

Comments
 (0)