def test_topological_order_for_tree(self): D = Leaf(scope=[0]) E = Leaf(scope=[0]) F = Leaf(scope=[0]) B = 0.5 * D + 0.5 * E C = 0.5 * E + 0.5 * F A = 0.5 * B + 0.5 * C A.aname = "A" B.aname = "B" C.aname = "C" D.aname = "D" E.aname = "E" F.aname = "F" result = get_topological_order(A) self.assertEqual(result[0], D) self.assertEqual(result[1], E) self.assertEqual(result[2], F) self.assertEqual(result[3], B) self.assertEqual(result[4], C) self.assertEqual(result[5], A) self.assertEqual(len(result), 6)
def test_topological_order_for_non_tree(self): D = Leaf(scope=[0]) E = Leaf(scope=[0]) F = Leaf(scope=[0]) B = 0.5 * D + 0.5 * E C = 0.5 * E + 0.5 * F H = 0.5 * D + 0.5 * E I = 0.5 * D + 0.5 * E G = 0.5 * H + 0.5 * I A = 0.5 * B + 0.5 * C Z = 0.5 * A + 0.5 * G Z.aname = "Z" A.aname = "A" B.aname = "B" C.aname = "C" D.aname = "D" E.aname = "E" F.aname = "F" G.aname = "G" H.aname = "H" I.aname = "I" result = get_topological_order(Z) self.assertEqual(result[0], D) self.assertEqual(result[1], E) self.assertEqual(result[2], F) self.assertEqual(result[3], B) self.assertEqual(result[4], H) self.assertEqual(result[5], I) self.assertEqual(result[6], C) self.assertEqual(result[7], G) self.assertEqual(result[8], A) self.assertEqual(result[9], Z) self.assertEqual(len(result), 10) layers = get_topological_order_layers(Z) self.assertEqual(set(layers[0]), set([D, E, F])) self.assertEqual(set(layers[1]), set([I, H, B, C])) self.assertEqual(set(layers[2]), set([G, A])) self.assertEqual(set(layers[3]), set([Z]))
def Compress(node): all_parents = get_parents(node) cache = {} for n in get_topological_order(node): params = (n.parameters, tuple(sorted(n.scope))) cached_node = cache.get(params, None) if cached_node is None: cache[params] = n else: for parent, pos in all_parents[n]: parent.children[pos] = cached_node assign_ids(node) val, msg = is_valid(node) assert val, msg return node
def eval_spn_bottom_up(node, eval_functions, all_results=None, debug=False, **args): nodes = get_topological_order(node) if debug: from tqdm import tqdm nodes = tqdm(list(nodes)) if all_results is None: all_results = {} else: all_results.clear() for node_type, func in eval_functions.items(): if "_eval_func" not in node_type.__dict__: node_type._eval_func = [] node_type._eval_func.append(func) node_type._is_leaf = issubclass(node_type, Leaf) leaf_func = eval_functions.get(Leaf, None) tmp_children_list = [] len_tmp_children_list = 0 for n in nodes: try: func = n.__class__._eval_func[-1] n_is_leaf = n.__class__._is_leaf except: if isinstance(n, Leaf) and leaf_func is not None: func = leaf_func n_is_leaf = True else: raise AssertionError( "No lambda function associated with type: %s" % (n.__class__.__name__)) if n_is_leaf: """ Use my code here """ if isinstance(n, CLTree): result = cltree_log_likelihood(n, **args) else: result = func(n, **args) else: len_children = len(n.children) if len_tmp_children_list < len_children: tmp_children_list.extend([None] * len_children) len_tmp_children_list = len(tmp_children_list) for i in range(len_children): ci = n.children[i] tmp_children_list[i] = all_results[ci] result = func(n, tmp_children_list[0:len_children], **args) all_results[n] = result for node_type, func in eval_functions.items(): del node_type._eval_func[-1] if len(node_type._eval_func) == 0: delattr(node_type, "_eval_func") return all_results[node]