|
57 | 57 |
|
58 | 58 | from numpy.testing import assert_almost_equal, assert_array_equal, assert_allclose |
59 | 59 |
|
60 | | -from ..testing import data_path as DATA_PATH, assert_dt_equal |
| 60 | +from ..testing import data_path as DATA_PATH, assert_dt_equal, clear_and_catch_warnings |
61 | 61 |
|
62 | 62 | from ..tmpdirs import InTemporaryDirectory |
63 | 63 |
|
@@ -132,24 +132,38 @@ def validate_asarray(self, pmaker, params): |
132 | 132 | # Shape matches expected shape |
133 | 133 | assert_equal(out.shape, params['shape']) |
134 | 134 |
|
135 | | - def validate_get_scaled(self, pmaker, params): |
| 135 | + def validate_array_interface_with_dtype(self, pmaker, params): |
136 | 136 | # Check proxy returns expected array from asarray |
137 | 137 | prox, fio, hdr = pmaker() |
138 | | - out = prox.get_scaled() |
139 | | - assert_array_equal(out, params['arr_out']) |
140 | | - assert_dt_equal(out.dtype, params['dtype_out']) |
141 | | - # Shape matches expected shape |
142 | | - assert_equal(out.shape, params['shape']) |
| 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 | + context = None |
| 143 | + if np.issubdtype(orig.dtype, np.complexfloating): |
| 144 | + context = clear_and_catch_warnings() |
| 145 | + context.__enter__() |
| 146 | + warnings.simplefilter('ignore', np.ComplexWarning) |
143 | 147 |
|
144 | 148 | for dtype in np.sctypes['float'] + np.sctypes['int'] + np.sctypes['uint']: |
145 | | - out = prox.get_scaled(dtype=dtype) |
| 149 | + # Directly coerce with a dtype |
| 150 | + direct = dtype(prox) |
146 | 151 | # Half-precision is imprecise. Obviously. It's a bad idea, but don't break |
147 | 152 | # the test over it. |
148 | 153 | rtol = 1e-03 if dtype == np.float16 else 1e-05 |
149 | | - assert_allclose(out, params['arr_out'].astype(out.dtype), rtol=rtol, atol=1e-08) |
150 | | - assert_greater_equal(out.dtype, np.dtype(dtype)) |
151 | | - # Shape matches expected shape |
152 | | - assert_equal(out.shape, params['shape']) |
| 154 | + assert_allclose(direct, orig.astype(dtype), rtol=rtol, atol=1e-08) |
| 155 | + assert_dt_equal(direct.dtype, np.dtype(dtype)) |
| 156 | + assert_equal(direct.shape, params['shape']) |
| 157 | + # All three methods should produce equivalent results |
| 158 | + for arrmethod in (np.array, np.asarray, np.asanyarray): |
| 159 | + out = arrmethod(prox, dtype=dtype) |
| 160 | + assert_array_equal(out, direct) |
| 161 | + assert_dt_equal(out.dtype, np.dtype(dtype)) |
| 162 | + # Shape matches expected shape |
| 163 | + assert_equal(out.shape, params['shape']) |
| 164 | + |
| 165 | + if context is not None: |
| 166 | + context.__exit__() |
153 | 167 |
|
154 | 168 | def validate_header_isolated(self, pmaker, params): |
155 | 169 | # Confirm altering input header has no effect |
|
0 commit comments