MCPcopy
hub / github.com/numpy/numpy / test_masked_weights

Method test_masked_weights

numpy/ma/tests/test_extras.py:416–457  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

414 assert_array_equal(wsum, expected_wsum)
415
416 def test_masked_weights(self):
417 # Test with masked weights.
418 # (Regression test for https://github.com/numpy/numpy/issues/10438)
419 a = np.ma.array(np.arange(9).reshape(3, 3),
420 mask=[[1, 0, 0], [1, 0, 0], [0, 0, 0]])
421 weights_unmasked = masked_array([5, 28, 31], mask=False)
422 weights_masked = masked_array([5, 28, 31], mask=[1, 0, 0])
423
424 avg_unmasked = average(a, axis=0,
425 weights=weights_unmasked, returned=False)
426 expected_unmasked = np.array([6.0, 5.21875, 6.21875])
427 assert_almost_equal(avg_unmasked, expected_unmasked)
428
429 avg_masked = average(a, axis=0, weights=weights_masked, returned=False)
430 expected_masked = np.array([6.0, 5.576271186440678, 6.576271186440678])
431 assert_almost_equal(avg_masked, expected_masked)
432
433 # weights should be masked if needed
434 # depending on the array mask. This is to avoid summing
435 # masked nan or other values that are not cancelled by a zero
436 a = np.ma.array([1.0, 2.0, 3.0, 4.0],
437 mask=[False, False, True, True])
438 avg_unmasked = average(a, weights=[1, 1, 1, np.nan])
439
440 assert_almost_equal(avg_unmasked, 1.5)
441
442 a = np.ma.array([
443 [1.0, 2.0, 3.0, 4.0],
444 [5.0, 6.0, 7.0, 8.0],
445 [9.0, 1.0, 2.0, 3.0],
446 ], mask=[
447 [False, True, True, False],
448 [True, False, True, True],
449 [True, False, True, False],
450 ])
451
452 avg_masked = np.ma.average(a, weights=[1, np.nan, 1], axis=0)
453 avg_expected = np.ma.array([1.0, np.nan, np.nan, 3.5],
454 mask=[False, True, True, False])
455
456 assert_almost_equal(avg_masked, avg_expected)
457 assert_equal(avg_masked.mask, avg_expected.mask)
458
459
460class TestConcatenator:

Callers

nothing calls this directly

Calls 4

averageFunction · 0.90
assert_almost_equalFunction · 0.90
assert_equalFunction · 0.90
reshapeMethod · 0.80

Tested by

no test coverage detected