Example #1
0
 def testReal(self):
     m1 = _getMapping()
     m2 = resample(m1, pxPerDeg=15, method='mean')
     m2.checkPlateCarree()
     m3 = resample(m2, arcsecPerPx=100, method='nearest')
     m3.checkPlateCarree()
     
     assert_array_approx_equal(_bbToArray(m2.boundingBox), _bbToArray(m1.boundingBox), 1)
     assert_array_approx_equal(_bbToArray(m3.boundingBox), _bbToArray(m1.boundingBox), 1)
    def test_joint_estmator_point(self):
        X = array([[0, 1], [1, 0], [1, 1]])
        y = array([[0, 1], [1, 0], [1, 0]])

        assert_array_approx_equal(
            joint_estimator_point(X, y), [[.5, 0], [.25, .25]])
        assert_array_approx_equal(
            joint_estimator_point(csr_matrix(X), csr_matrix(y)),
            [[.5, 0], [.25, .25]])
    def test_joint_estmator_point(self):
        X = array([[0, 1], [1, 0], [1, 1]])
        y = array([[0, 1], [1, 0], [1, 0]])

        assert_array_approx_equal(joint_estimator_point(X, y),
                                  [[.5, 0], [.25, .25]])
        assert_array_approx_equal(
            joint_estimator_point(csr_matrix(X), csr_matrix(y)),
            [[.5, 0], [.25, .25]])
    def test_pointwise_mutual_information(self):
        X = array([[0, 1], [1, 0], [1, 1]])
        y = array([[0, 1], [1, 0], [1, 0]])

        assert_array_approx_equal(pointwise_mutual_information(X, y),
                                  [0.1178, 0.1178],
                                  decimal=3)
        assert_array_approx_equal(pointwise_mutual_information(
            csr_matrix(X), csr_matrix(y)), [0.1178, 0.1178],
                                  decimal=3)
    def test_mutual_information(self):
        X = array([[0, 1],
                   [1, 0],
                   [1, 1]])
        y = array([[0, 1],
                   [1, 0],
                   [1, 0]])

        assert_array_approx_equal(mutual_information(X, y), [-0.37489, -0.605939], decimal=3)
        assert_array_approx_equal(mutual_information(csr_matrix(X), csr_matrix(y)), [-0.37489, -0.605939], decimal=3)
Example #6
0
    def _testReal(self):
        m1 = _getMapping()
        m2 = resample(m1, pxPerDeg=15, method='mean')
        m2.checkPlateCarree()
        m3 = resample(m2, arcsecPerPx=100, method='nearest')
        m3.checkPlateCarree()

        assert_array_approx_equal(_bbToArray(m2.boundingBox),
                                  _bbToArray(m1.boundingBox), 1)
        assert_array_approx_equal(_bbToArray(m3.boundingBox),
                                  _bbToArray(m1.boundingBox), 1)
    def test_mutual_information(self):
        X = array([[0, 1], [1, 0], [1, 1]])
        y = array([[0, 1], [1, 0], [1, 0]])

        assert_array_approx_equal(mutual_information(X, y),
                                  [-0.37489, -0.605939],
                                  decimal=3)
        assert_array_approx_equal(mutual_information(csr_matrix(X),
                                                     csr_matrix(y)),
                                  [-0.37489, -0.605939],
                                  decimal=3)
    def test_pointwise_mutual_information(self):
        X = array([[0, 1],
                   [1, 0],
                   [1, 1]])
        y = array([[0, 1],
                   [1, 0],
                   [1, 0]])

        assert_array_approx_equal(pointwise_mutual_information(X, y), [0.1178, 0.1178], decimal=3)
        assert_array_approx_equal(pointwise_mutual_information(csr_matrix(X), csr_matrix(y)),
                                  [0.1178, 0.1178], decimal=3)
    def test_joint_estimator_full(self):
        X = array([[0, 1], [1, 0], [1, 1]])
        y = array([[0, 1], [1, 0], [1, 0]])

        assert_array_approx_equal(
            joint_estimator_full(X, y),
            [[[.1667, .0], [.0833, .0833]], [[.0, .1667], [.0833, .0833]],
             [[.0, .0833], [.0833, .0]], [[.0833, .0], [.0, .0833]]],
            decimal=3)
        assert_array_approx_equal(
            joint_estimator_full(csr_matrix(X), csr_matrix(y)),
            [[[.1667, .0], [.0833, .0833]], [[.0, .1667], [.0833, .0833]],
             [[.0, .0833], [.0833, .0]], [[.0833, .0], [.0, .0833]]],
            decimal=3)
Example #10
0
    def test_npmi(self):
        X = array([[0, 1],
                   [1, 0],
                   [1, 1]])
        y = array([[0, 1],
                   [1, 0],
                   [1, 0]])

        transformer = TermWeightTransformer(method='npmi')
        transformer.fit(X, y)

        assert_array_approx_equal(transformer._weights, [0.1699, 0.0850], decimal=3)
        assert_array_approx_equal(transformer.transform(X), array([[0., 0.0850],
                                                                   [0.1700, 0.],
                                                                   [0.1700, 0.0850]]),
                                  decimal=3)

        transformer = TermWeightTransformer(method='npmi')
        X = csr_matrix(X)
        y = csr_matrix(y)
        transformer.fit(X, y)
        newX = transformer.transform(X)

        assert_array_approx_equal(transformer._weights, [0.1700, 0.0850], decimal=3)
        assert_true(issparse(newX))
        assert_array_approx_equal(newX.todense(), array([[0., 0.0850],
                                                         [0.1700, 0.],
                                                         [0.1700, 0.0850]]),
                                  decimal=3)
    def test_joint_estimator_full(self):
        X = array([[0, 1], [1, 0], [1, 1]])
        y = array([[0, 1], [1, 0], [1, 0]])

        assert_array_approx_equal(
            joint_estimator_full(X, y),
            [[[.1667, .0], [.0833, .0833]], [[.0, .1667], [.0833, .0833]],
             [[.0, .0833], [.0833, .0]], [[.0833, .0], [.0, .0833]]],
            decimal=3)
        assert_array_approx_equal(
            joint_estimator_full(csr_matrix(X), csr_matrix(y)),
            [[[.1667, .0], [.0833, .0833]], [[.0, .1667], [.0833, .0833]],
             [[.0, .0833], [.0833, .0]], [[.0833, .0], [.0, .0833]]],
            decimal=3)
Example #12
0
def test_auto_guide(auto_class, init_loc_fn, num_particles):
    latent_dim = 3

    def model(obs):
        a = numpyro.sample("a", Normal(0, 1))
        return numpyro.sample("obs", Bernoulli(logits=a), obs=obs)

    obs = Bernoulli(0.5).sample(random.PRNGKey(0), (10, latent_dim))

    rng_key = random.PRNGKey(0)
    guide_key, stein_key = random.split(rng_key)
    inner_guide = auto_class(model, init_loc_fn=init_loc_fn())

    with handlers.seed(rng_seed=guide_key), handlers.trace() as inner_guide_tr:
        inner_guide(obs)

    steinvi = SteinVI(
        model,
        auto_class(model, init_loc_fn=init_loc_fn()),
        Adam(1.0),
        Trace_ELBO(),
        RBFKernel(),
        num_particles=num_particles,
    )
    state = steinvi.init(stein_key, obs)
    init_params = steinvi.get_params(state)

    for name, site in inner_guide_tr.items():
        if site.get("type") == "param":
            assert name in init_params
            inner_param = site
            init_value = init_params[name]
            expected_shape = (num_particles, *np.shape(inner_param["value"]))
            assert init_value.shape == expected_shape
            if "auto_loc" in name or name == "b":
                assert np.alltrue(init_value != np.zeros(expected_shape))
                assert np.unique(init_value).shape == init_value.reshape(
                    -1).shape
            elif "scale" in name:
                assert_array_approx_equal(init_value,
                                          np.full(expected_shape, 0.1))
            else:
                assert_array_approx_equal(init_value,
                                          np.full(expected_shape, 0.0))
Example #13
0
 def _testPoleBug(self):
     m1 = _getMapping()
     m2 = resample(m1, arcsecPerPx=100, method='mean')
     assert_array_approx_equal(_bbToArray(m2.boundingBox),
                               _bbToArray(m1.boundingBox), 1)
Example #14
0
 def testPoleBug(self):
     m1 = _getMapping()
     m2 = resample(m1, arcsecPerPx=100, method='mean')
     assert_array_approx_equal(_bbToArray(m2.boundingBox), _bbToArray(m1.boundingBox), 1)