def type_if_rel(relation_class, rng=None): """ Test if transactions on the type interface propagate correctly to relations and preserve invariants """ T1_N = 2 T2_N = 2 data = np.arange(T1_N * T2_N, dtype=np.float32) data.shape = T1_N, T2_N m = models.AccumModel() r = relation_class([('T1', T1_N), ('T2', T2_N)], data,m) hps = m.create_hps() hps['offset'] = 0.3 r.set_hps(hps) tf_1 = model.DomainInterface(T1_N, {'r' : ('T1', r)}) tf_1.set_hps({'alpha' : 1.0}) tf_2 = model.DomainInterface(T2_N, {'r' : ('T2', r)}) tf_2.set_hps({'alpha' : 1.0}) assert_array_equal(tf_1.get_assignments(), np.ones(T1_N)*model.NOT_ASSIGNED) ### put all T2 into one group, T1 in singletons t2_g1 = tf_2.create_group(rng) assert_equal(len(r.get_all_groups('T2')), 1) for i in range(T2_N): tf_2.add_entity_to_group(t2_g1, i) t1_grps= [tf_1.create_group(rng) for _ in range(T1_N)] [tf_1.add_entity_to_group(g, i) for g, i in zip(t1_grps, range(T1_N))] # total score assert_approx_equal(r.total_score(), np.sum(np.sum(data, axis=1)) + 0.3*T1_N) #Now remove the last entity from the last group in T1 and compute #post pred tf_1.remove_entity_from_group(T1_N -1) for g_i, g in enumerate(t1_grps[:-1]): print "-"*70 score = tf_1.post_pred(g, T1_N-1) est_score_old = np.sum(data[g_i, :]) dp = np.hstack([data[g_i, :], data[T1_N -1, :]]) print dp est_score_new = np.sum(dp) score_delta = est_score_new- est_score_old + util.crp_post_pred(1, T1_N, 1.0) assert_approx_equal(score_delta, score)
def test_crp_post_pred(): assert_approx_equal(util.crp_post_pred(0, 1, 1.0), np.log(1.0)) assert_approx_equal(util.crp_post_pred(0, 1, 10.0), np.log(1.0)) assert_approx_equal(util.crp_post_pred(100, 101, 10.0), np.log(100. / (100 + 10.0)))
def test_crp_post_pred(): assert_approx_equal(util.crp_post_pred(0, 1, 1.0), np.log(1.0)) assert_approx_equal(util.crp_post_pred(0, 1, 10.0), np.log(1.0)) assert_approx_equal(util.crp_post_pred(100, 101, 10.0), np.log(100./(100+10.0)))