(self)
| 326 | assert_array_equal(wsum, expected_wsum) |
| 327 | |
| 328 | def test_masked_weights(self): |
| 329 | # Test with masked weights. |
| 330 | # (Regression test for https://github.com/numpy/numpy/issues/10438) |
| 331 | a = np.ma.array(np.arange(9).reshape(3, 3), |
| 332 | mask=[[1, 0, 0], [1, 0, 0], [0, 0, 0]]) |
| 333 | weights_unmasked = masked_array([5, 28, 31], mask=False) |
| 334 | weights_masked = masked_array([5, 28, 31], mask=[1, 0, 0]) |
| 335 | |
| 336 | avg_unmasked = average(a, axis=0, |
| 337 | weights=weights_unmasked, returned=False) |
| 338 | expected_unmasked = np.array([6.0, 5.21875, 6.21875]) |
| 339 | assert_almost_equal(avg_unmasked, expected_unmasked) |
| 340 | |
| 341 | avg_masked = average(a, axis=0, weights=weights_masked, returned=False) |
| 342 | expected_masked = np.array([6.0, 5.576271186440678, 6.576271186440678]) |
| 343 | assert_almost_equal(avg_masked, expected_masked) |
| 344 | |
| 345 | # weights should be masked if needed |
| 346 | # depending on the array mask. This is to avoid summing |
| 347 | # masked nan or other values that are not cancelled by a zero |
| 348 | a = np.ma.array([1.0, 2.0, 3.0, 4.0], |
| 349 | mask=[False, False, True, True]) |
| 350 | avg_unmasked = average(a, weights=[1, 1, 1, np.nan]) |
| 351 | |
| 352 | assert_almost_equal(avg_unmasked, 1.5) |
| 353 | |
| 354 | a = np.ma.array([ |
| 355 | [1.0, 2.0, 3.0, 4.0], |
| 356 | [5.0, 6.0, 7.0, 8.0], |
| 357 | [9.0, 1.0, 2.0, 3.0], |
| 358 | ], mask=[ |
| 359 | [False, True, True, False], |
| 360 | [True, False, True, True], |
| 361 | [True, False, True, False], |
| 362 | ]) |
| 363 | |
| 364 | avg_masked = np.ma.average(a, weights=[1, np.nan, 1], axis=0) |
| 365 | avg_expected = np.ma.array([1.0, np.nan, np.nan, 3.5], |
| 366 | mask=[False, True, True, False]) |
| 367 | |
| 368 | assert_almost_equal(avg_masked, avg_expected) |
| 369 | assert_equal(avg_masked.mask, avg_expected.mask) |
| 370 | |
| 371 | |
| 372 | class TestConcatenator: |
nothing calls this directly
no test coverage detected