コード例 #1
0
def test_relation_numpy_dataview_digest():
    x = np.zeros((2, 3, 4), dtype=np.bool)
    view = relation_numpy_dataview(x)
    view1 = relation_numpy_dataview(x)
    assert_equals(_hexdigest(view), _hexdigest(view1))

    y = np.ones((2, 3, 4), dtype=np.bool)
    view1 = relation_numpy_dataview(y)
    assert_not_equals(_hexdigest(view), _hexdigest(view1))
コード例 #2
0
def test_relation_numpy_dataview_digest():
    x = np.zeros((2, 3, 4), dtype=np.bool)
    view = relation_numpy_dataview(x)
    view1 = relation_numpy_dataview(x)
    assert_equals(_hexdigest(view), _hexdigest(view1))

    y = np.ones((2, 3, 4), dtype=np.bool)
    view1 = relation_numpy_dataview(y)
    assert_not_equals(_hexdigest(view), _hexdigest(view1))
コード例 #3
0
def test_relation_numpy_dataview_digest_masked():
    x = np.zeros((2, 2), dtype=np.bool)
    y = np.zeros((2, 2), dtype=np.bool)
    y[0, 0] = True
    mask = np.zeros((2, 2))
    mask[0, 0] = True
    ma_x = ma.array(x, mask=mask)
    ma_y = ma.array(y, mask=mask)
    view = relation_numpy_dataview(ma_x)
    view1 = relation_numpy_dataview(ma_y)
    assert_equals(_hexdigest(view), _hexdigest(view1))
コード例 #4
0
def test_relation_numpy_dataview_digest_masked():
    x = np.zeros((2, 2), dtype=np.bool)
    y = np.zeros((2, 2), dtype=np.bool)
    y[0, 0] = True
    mask = np.zeros((2, 2))
    mask[0, 0] = True
    ma_x = ma.array(x, mask=mask)
    ma_y = ma.array(y, mask=mask)
    view = relation_numpy_dataview(ma_x)
    view1 = relation_numpy_dataview(ma_y)
    assert_equals(_hexdigest(view), _hexdigest(view1))
コード例 #5
0
def test_relation_numpy_dataview_pickle():
    # not masked, int32
    y = np.random.randint(-10, 10, size=(10, 10))
    view = relation_numpy_dataview(y)
    bstr = pickle.dumps(view)
    view1 = pickle.loads(bstr)
    assert_true((y == view1.toarray()).all())

    # masked, int32
    y = ma.array(y, mask=(np.random.uniform(size=(10, 10)) < 0.5))
    view = relation_numpy_dataview(y)
    bstr = pickle.dumps(view)
    view1 = pickle.loads(bstr)
    assert_true((y == view1.toarray()).all())
コード例 #6
0
def test_relation_numpy_dataview_pickle():
    # not masked, int32
    y = np.random.randint(-10, 10, size=(10, 10))
    view = relation_numpy_dataview(y)
    bstr = pickle.dumps(view)
    view1 = pickle.loads(bstr)
    assert_true((y == view1.toarray()).all())

    # masked, int32
    y = ma.array(y, mask=(np.random.uniform(size=(10, 10)) < 0.5))
    view = relation_numpy_dataview(y)
    bstr = pickle.dumps(view)
    view1 = pickle.loads(bstr)
    assert_true((y == view1.toarray()).all())
コード例 #7
0
def test_dense_vs_sparse():
    # XXX: really belongs in irm test cases, but kernels has a nice cluster
    # enumeration iterator

    r = rng()

    n = 5
    raw = ma.array(
        np.random.choice(np.arange(20), size=(n, n)),
        mask=np.random.choice([False, True], size=(n, n)))

    dense = [relation_numpy_dataview(raw)]
    sparse = [sparse_relation_dataview(_tocsr(raw))]

    domains = [n]
    relations = [((0, 0), gp)]
    defn = irm_definition(domains, relations)

    def score_fn(data):
        def f(assignments):
            s = irm_initialize(defn, data, r=r, domain_assignments=assignments)
            assign = sum(s.score_assignment(i)
                         for i in xrange(len(assignments)))
            likelihood = s.score_likelihood(r)
            return assign + likelihood
        return f

    product_assignments = tuple(map(list, map(permutation_iter, domains)))

    dense_posterior = scores_to_probs(
        np.array(map(score_fn(dense), it.product(*product_assignments))))
    sparse_posterior = scores_to_probs(
        np.array(map(score_fn(sparse), it.product(*product_assignments))))

    assert_1d_lists_almost_equals(dense_posterior, sparse_posterior, places=3)
コード例 #8
0
def test_dense_vs_sparse():
    # XXX: really belongs in irm test cases, but kernels has a nice cluster
    # enumeration iterator

    r = rng()

    n = 5
    raw = ma.array(np.random.choice(np.arange(20), size=(n, n)),
                   mask=np.random.choice([False, True], size=(n, n)))

    dense = [relation_numpy_dataview(raw)]
    sparse = [sparse_relation_dataview(_tocsr(raw))]

    domains = [n]
    relations = [((0, 0), gp)]
    defn = irm_definition(domains, relations)

    def score_fn(data):
        def f(assignments):
            s = irm_initialize(defn, data, r=r, domain_assignments=assignments)
            assign = sum(
                s.score_assignment(i) for i in xrange(len(assignments)))
            likelihood = s.score_likelihood(r)
            return assign + likelihood

        return f

    product_assignments = tuple(map(list, map(permutation_iter, domains)))

    dense_posterior = scores_to_probs(
        np.array(map(score_fn(dense), it.product(*product_assignments))))
    sparse_posterior = scores_to_probs(
        np.array(map(score_fn(sparse), it.product(*product_assignments))))

    assert_1d_lists_almost_equals(dense_posterior, sparse_posterior, places=3)
コード例 #9
0
def test_one_binary_nonconj():
    # 1 domain, 1 binary relation, nonconj
    domains = [3]

    def mk_relations(model):
        return [((0, 0), model)]

    relsize = (domains[0], domains[0])
    data = [
        relation_numpy_dataview(
            ma.array(np.random.choice([False, True], size=relsize),
                     mask=np.random.random(size=relsize) > 0.8))
    ]

    def mkparam():
        return {'p': 0.05}

    params = {0: mkparam()}

    def kernel(s, r):
        assign_resample(s, 10, r)
        theta(s, r, tparams=params)

    _test_convergence(domains, data, mk_relations(bbnc), mk_relations(bb),
                      kernel)
コード例 #10
0
def test_two_binary():
    # 1 domain, 2 binary relations
    domains = [4]

    def mk_relations(model):
        return [((0, 0), model), ((0, 0), model)]

    relsize = (domains[0], domains[0])
    data = [
        relation_numpy_dataview(
            ma.array(np.random.choice([False, True], size=relsize),
                     mask=np.random.choice([False, True], size=relsize))),
        relation_numpy_dataview(
            ma.array(np.random.choice([False, True], size=relsize),
                     mask=np.random.choice([False, True], size=relsize))),
    ]
    _test_convergence(domains, data, mk_relations(bb), mk_relations(bb),
                      assign)
コード例 #11
0
def test_one_binary_one_ternary():
    # 1 domain, 1 binary, 1 ternary
    domains = [4]

    def mk_relations(model):
        return [((0, 0), model), ((0, 0, 0), model)]

    relsize2 = (domains[0], domains[0])
    relsize3 = (domains[0], domains[0], domains[0])
    data = [
        relation_numpy_dataview(
            ma.array(np.random.choice([False, True], size=relsize2),
                     mask=np.random.choice([False, True], size=relsize2))),
        relation_numpy_dataview(
            ma.array(np.random.choice([False, True], size=relsize3),
                     mask=np.random.choice([False, True], size=relsize3))),
    ]
    _test_convergence(domains, data, mk_relations(bb), mk_relations(bb),
                      assign)
コード例 #12
0
def test_two_binary():
    # 1 domain, 2 binary relations
    domains = [4]

    def mk_relations(model):
        return [((0, 0), model), ((0, 0), model)]

    relsize = (domains[0], domains[0])
    data = [
        relation_numpy_dataview(
            ma.array(
                np.random.choice([False, True], size=relsize),
                mask=np.random.choice([False, True], size=relsize))),
        relation_numpy_dataview(
            ma.array(
                np.random.choice([False, True], size=relsize),
                mask=np.random.choice([False, True], size=relsize))),
    ]
    _test_convergence(
        domains, data, mk_relations(bb), mk_relations(bb), assign)
コード例 #13
0
def test_one_binary_one_ternary():
    # 1 domain, 1 binary, 1 ternary
    domains = [4]

    def mk_relations(model):
        return [((0, 0), model), ((0, 0, 0), model)]

    relsize2 = (domains[0], domains[0])
    relsize3 = (domains[0], domains[0], domains[0])
    data = [
        relation_numpy_dataview(
            ma.array(
                np.random.choice([False, True], size=relsize2),
                mask=np.random.choice([False, True], size=relsize2))),
        relation_numpy_dataview(
            ma.array(
                np.random.choice(
                    [False, True], size=relsize3),
                mask=np.random.choice([False, True], size=relsize3))),
    ]
    _test_convergence(
        domains, data, mk_relations(bb), mk_relations(bb), assign)
コード例 #14
0
def test_one_binary_nonconj_kernel():
    # 1 domain, 1 binary relation
    domains = [4]

    def mk_relations(model):
        return [((0, 0), model)]

    relsize = (domains[0], domains[0])
    data = [relation_numpy_dataview(
        ma.array(
            np.random.choice([False, True], size=relsize),
            mask=np.random.choice([False, True], size=relsize)))]
    kernel = lambda s, r: assign_resample(s, 10, r)
    _test_convergence(
        domains, data, mk_relations(bb), mk_relations(bb), kernel)
コード例 #15
0
def test_one_binary_nonconj_kernel():
    # 1 domain, 1 binary relation
    domains = [4]

    def mk_relations(model):
        return [((0, 0), model)]

    relsize = (domains[0], domains[0])
    data = [
        relation_numpy_dataview(
            ma.array(np.random.choice([False, True], size=relsize),
                     mask=np.random.choice([False, True], size=relsize)))
    ]
    kernel = lambda s, r: assign_resample(s, 10, r)
    _test_convergence(domains, data, mk_relations(bb), mk_relations(bb),
                      kernel)
コード例 #16
0
def test_one_binary_nonconj():
    # 1 domain, 1 binary relation, nonconj
    domains = [3]

    def mk_relations(model):
        return [((0, 0), model)]

    relsize = (domains[0], domains[0])
    data = [relation_numpy_dataview(
        ma.array(
            np.random.choice([False, True], size=relsize),
            mask=np.random.random(size=relsize) > 0.8))]

    def mkparam():
        return {'p': 0.05}
    params = {0: mkparam()}

    def kernel(s, r):
        assign_resample(s, 10, r)
        theta(s, r, tparams=params)
    _test_convergence(
        domains, data, mk_relations(bbnc), mk_relations(bb), kernel)
コード例 #17
0
def test_relation_numpy_dataview_masked():
    x = np.zeros((2, 3, 4), dtype=np.bool)
    x = ma.masked_array(x, mask=np.ones(x.shape))
    view = relation_numpy_dataview(x)
    assert_is_not_none(view)
    assert_equals(view.shape(), (2, 3, 4))
コード例 #18
0
def test_relation_numpy_dataview_masked():
    x = np.zeros((2, 3, 4), dtype=np.bool)
    x = ma.masked_array(x, mask=np.ones(x.shape))
    view = relation_numpy_dataview(x)
    assert_is_not_none(view)
    assert_equals(view.shape(), (2, 3, 4))
コード例 #19
0
def test_compare_to_mixture_model():
    r = rng()

    N, D = 4, 5

    Y = np.random.uniform(size=(N, D)) > 0.8
    Y_rec = np.array([tuple(y) for y in Y], dtype=[('', bool)] * D)

    mm_view = rec_numpy_dataview(Y_rec)
    irm_view = relation_numpy_dataview(Y)

    mm_def = mm_definition(N, [bb] * D)
    irm_def = irm_definition([N, D], [((0, 1), bb)])

    perms = list(permutation_iter(N))
    assignment = perms[np.random.randint(0, len(perms))]

    mm_s = mm_initialize(mm_def, mm_view, r=r, assignment=assignment)
    irm_s = irm_initialize(irm_def, [irm_view],
                           r=r,
                           domain_assignments=[
                               assignment,
                               range(D),
                           ])

    def assert_suff_stats_equal():
        assert set(mm_s.groups()) == set(irm_s.groups(0))
        assert irm_s.groups(1) == range(D)
        groups = mm_s.groups()
        for g in groups:
            for i in xrange(D):
                a = mm_s.get_suffstats(g, i)
                b = irm_s.get_suffstats(0, [g, i])
                if b is None:
                    b = {'heads': 0L, 'tails': 0L}
                assert a['heads'] == b['heads'] and a['tails'] == b['tails']

    assert_suff_stats_equal()
    assert_almost_equals(mm_s.score_assignment(),
                         irm_s.score_assignment(0),
                         places=3)

    bound_mm_s = mm_bind(mm_s, mm_view)
    bound_irm_s = irm_bind(irm_s, 0, [irm_view])

    # XXX: doesn't really have to be true, just is true of impl
    assert not bound_mm_s.empty_groups()
    assert not bound_irm_s.empty_groups()

    bound_mm_s.create_group(r)
    bound_irm_s.create_group(r)

    gid_a = bound_mm_s.remove_value(0, r)
    gid_b = bound_irm_s.remove_value(0, r)

    assert gid_a == gid_b
    assert_suff_stats_equal()

    x0, y0 = bound_mm_s.score_value(0, r)
    x1, y1 = bound_irm_s.score_value(0, r)
    assert x0 == x1  # XXX: not really a requirement

    # XXX: should really normalize and then check
    for a, b in zip(y0, y1):
        assert_almost_equals(a, b, places=2)
コード例 #20
0
def test_relation_numpy_dataview():
    x = np.zeros((2, 3, 4), dtype=np.bool)
    view = relation_numpy_dataview(x)
    assert_is_not_none(view)
    assert_equals(view.shape(), (2, 3, 4))
コード例 #21
0
def test_compare_to_mixture_model():
    r = rng()

    N, D = 4, 5

    Y = np.random.uniform(size=(N, D)) > 0.8
    Y_rec = np.array([tuple(y) for y in Y], dtype=[('', bool)] * D)

    mm_view = rec_numpy_dataview(Y_rec)
    irm_view = relation_numpy_dataview(Y)

    mm_def = mm_definition(N, [bb] * D)
    irm_def = irm_definition([N, D], [((0, 1), bb)])

    perms = list(permutation_iter(N))
    assignment = perms[np.random.randint(0, len(perms))]

    mm_s = mm_initialize(mm_def, mm_view, r=r, assignment=assignment)
    irm_s = irm_initialize(irm_def,
                           [irm_view],
                           r=r,
                           domain_assignments=[
                               assignment,
                               range(D),
                           ])

    def assert_suff_stats_equal():
        assert set(mm_s.groups()) == set(irm_s.groups(0))
        assert irm_s.groups(1) == range(D)
        groups = mm_s.groups()
        for g in groups:
            for i in xrange(D):
                a = mm_s.get_suffstats(g, i)
                b = irm_s.get_suffstats(0, [g, i])
                if b is None:
                    b = {'heads': 0L, 'tails': 0L}
                assert a['heads'] == b['heads'] and a['tails'] == b['tails']

    assert_suff_stats_equal()
    assert_almost_equals(
        mm_s.score_assignment(), irm_s.score_assignment(0), places=3)

    bound_mm_s = mm_bind(mm_s, mm_view)
    bound_irm_s = irm_bind(irm_s, 0, [irm_view])

    # XXX: doesn't really have to be true, just is true of impl
    assert not bound_mm_s.empty_groups()
    assert not bound_irm_s.empty_groups()

    bound_mm_s.create_group(r)
    bound_irm_s.create_group(r)

    gid_a = bound_mm_s.remove_value(0, r)
    gid_b = bound_irm_s.remove_value(0, r)

    assert gid_a == gid_b
    assert_suff_stats_equal()

    x0, y0 = bound_mm_s.score_value(0, r)
    x1, y1 = bound_irm_s.score_value(0, r)
    assert x0 == x1  # XXX: not really a requirement

    # XXX: should really normalize and then check
    for a, b in zip(y0, y1):
        assert_almost_equals(a, b, places=2)
コード例 #22
0
def test_relation_numpy_dataview():
    x = np.zeros((2, 3, 4), dtype=np.bool)
    view = relation_numpy_dataview(x)
    assert_is_not_none(view)
    assert_equals(view.shape(), (2, 3, 4))