(self)
| 289 | assert_equal(actual, desired) |
| 290 | |
| 291 | def test_weight_and_input_dims_different(self): |
| 292 | # this test mirrors a test for np.average() |
| 293 | # in lib/test/test_function_base.py |
| 294 | y = np.arange(12).reshape(2, 2, 3) |
| 295 | w = np.array([0., 0., 1., .5, .5, 0., 0., .5, .5, 1., 0., 0.])\ |
| 296 | .reshape(2, 2, 3) |
| 297 | |
| 298 | m = np.full((2, 2, 3), False) |
| 299 | yma = np.ma.array(y, mask=m) |
| 300 | subw0 = w[:, :, 0] |
| 301 | |
| 302 | actual = average(yma, axis=(0, 1), weights=subw0) |
| 303 | desired = masked_array([7., 8., 9.], mask=[False, False, False]) |
| 304 | assert_almost_equal(actual, desired) |
| 305 | |
| 306 | m = np.full((2, 2, 3), False) |
| 307 | m[:, :, 0] = True |
| 308 | m[0, 0, 1] = True |
| 309 | yma = np.ma.array(y, mask=m) |
| 310 | actual = average(yma, axis=(0, 1), weights=subw0) |
| 311 | desired = masked_array( |
| 312 | [np.nan, 8., 9.], |
| 313 | mask=[True, False, False]) |
| 314 | assert_almost_equal(actual, desired) |
| 315 | |
| 316 | m = np.full((2, 2, 3), False) |
| 317 | yma = np.ma.array(y, mask=m) |
| 318 | |
| 319 | subw1 = w[1, :, :] |
| 320 | actual = average(yma, axis=(1, 2), weights=subw1) |
| 321 | desired = masked_array([2.25, 8.25], mask=[False, False]) |
| 322 | assert_almost_equal(actual, desired) |
| 323 | |
| 324 | # here the weights have the wrong shape for the specified axes |
| 325 | with pytest.raises( |
| 326 | ValueError, |
| 327 | match="Shape of weights must be consistent with " |
| 328 | "shape of a along specified axis"): |
| 329 | average(yma, axis=(0, 1, 2), weights=subw0) |
| 330 | |
| 331 | with pytest.raises( |
| 332 | ValueError, |
| 333 | match="Shape of weights must be consistent with " |
| 334 | "shape of a along specified axis"): |
| 335 | average(yma, axis=(0, 1), weights=subw1) |
| 336 | |
| 337 | # swapping the axes should be same as transposing weights |
| 338 | actual = average(yma, axis=(1, 0), weights=subw0) |
| 339 | desired = average(yma, axis=(0, 1), weights=subw0.T) |
| 340 | assert_almost_equal(actual, desired) |
| 341 | |
| 342 | def test_onintegers_with_mask(self): |
| 343 | # Test average on integers with mask |
nothing calls this directly
no test coverage detected