コード例 #1
0
ファイル: test_numpy.py プロジェクト: SCarozza/popmon
def test_statistics_1():
    a, w = get_data()
    _means = np.zeros((3, 1, 1, 6))
    _weights = np.zeros((3, 1, 1, 6))
    for i, j, k, l in itertools.product(range(a.shape[0]), range(a.shape[1]),
                                        range(a.shape[2]), range(a.shape[3])):
        _means[i, 0, 0, l] += w[i, j, k, l] * a[i, j, k, l]
        _weights[i, 0, 0, l] += w[i, j, k, l]
    _means /= _weights
    np.testing.assert_allclose(_means,
                               mean(a, w, axis=(1, 2), keepdims=True),
                               rtol=1e-07)
    _stds = np.zeros((3, 1, 1, 6))
    for i, j, k, l in itertools.product(range(a.shape[0]), range(a.shape[1]),
                                        range(a.shape[2]), range(a.shape[3])):
        _stds[i, 0, 0,
              l] += w[i, j, k, l] * (a[i, j, k, l] - _means[i, 0, 0, l])**2
    _stds = np.sqrt(_stds / _weights)
    np.testing.assert_allclose(_stds,
                               std(a, w, axis=(1, 2), keepdims=True),
                               rtol=1e-07)
    _values = np.zeros((3, 6, 4 * 5))
    _weights = np.zeros((3, 6, 4 * 5))
    for i, j, k, l in itertools.product(range(a.shape[0]), range(a.shape[1]),
                                        range(a.shape[2]), range(a.shape[3])):
        _values[i, l, j * a.shape[2] + k] = a[i, j, k, l]
        _weights[i, l, j * a.shape[2] + k] = w[i, j, k, l]

    def get_quantiles(q):
        _quantiles = np.zeros((3, 6))
        for i in range(a.shape[0]):
            for l in range(a.shape[3]):
                isort = np.argsort(_values[i, l])
                v = _values[i, l][isort]
                u = _weights[i, l][isort]
                U = u.cumsum()
                r = (U - 0.5 * u) / U[-1]
                for m in range(1, len(u)):
                    if r[m - 1] <= q and r[m] > q:
                        _quantiles[i, l] = v[m - 1] + (q - r[m - 1]) / (
                            r[m] - r[m - 1]) * (v[m] - v[m - 1])
                        break
        return _quantiles

    np.testing.assert_allclose(get_quantiles(0.1),
                               quantile(a, 0.1, w, axis=(1, 2),
                                        keepdims=False),
                               rtol=1e-07)
    np.testing.assert_allclose(get_quantiles(0.5),
                               quantile(a, 0.5, w, axis=(1, 2),
                                        keepdims=False),
                               rtol=1e-07)
    np.testing.assert_allclose(get_quantiles(0.9),
                               quantile(a, 0.9, w, axis=(1, 2),
                                        keepdims=False),
                               rtol=1e-07)
コード例 #2
0
ファイル: test_numpy.py プロジェクト: SCarozza/popmon
def test_mean_shapes():
    a, w = get_data()
    out = mean(a)
    assert out.ndim == 0
    out = mean(a, w)
    assert out.ndim == 0
    out = mean(a, keepdims=True)
    assert out.shape == (1, 1, 1, 1)
    np.testing.assert_allclose(out.ravel()[0], mean(a), rtol=1e-07)
    out = mean(a, w, keepdims=True)
    assert out.shape == (1, 1, 1, 1)
    np.testing.assert_allclose(out.ravel()[0], mean(a, w), rtol=1e-07)
    out = mean(a, axis=1)
    assert out.shape == (3, 5, 6)
    out = mean(a, w, axis=1)
    assert out.shape == (3, 5, 6)
    out = mean(a, axis=(1, 2))
    assert out.shape == (3, 6)
    np.testing.assert_allclose(out,
                               mean(a, np.ones_like(a), axis=(1, 2)),
                               rtol=1e-07)
    out = mean(a, w, axis=(1, 2))
    assert out.shape == (3, 6)
    out = mean(a, axis=(1, 2), keepdims=True)
    assert out.shape == (3, 1, 1, 6)
    np.testing.assert_allclose(out.ravel(),
                               mean(a, axis=(1, 2)).ravel(),
                               rtol=1e-07)
    out = mean(a, w, axis=(1, 2), keepdims=True)
    assert out.shape == (3, 1, 1, 6)
    np.testing.assert_allclose(out.ravel(),
                               mean(a, w, axis=(1, 2)).ravel(),
                               rtol=1e-07)