def all_undirected_forest_components(n): """ Enumerate undirected forests with n nodes. """ # First get all the nondecreasing sequences that add to n sequences = nondecreasing_sequences_adding_to(n) # Now for each sequence get all the forests return flatmap(functools.partial(nondet_map, all_undirected_trees), sequences)
def test_sample_string_for_tree(): t = nx.DiGraph([(0, 1), (1, 2), (2, 3)]) nomi_samples = [ immutably(sample_string_for_tree)(t, [2]) for _ in range(1000) ] mi_samples = [ # MI 1 immutably(sample_string_for_tree)(t, [0, 1]) for _ in range(1000) ] # When self-information is high, samples become sparser, so more samples # are needed to get a distribution with the right MI. mi_samples2 = [ # MI 1 immutably(sample_string_for_tree)(t, [2, 1]) for _ in range(10000) ] mi_samples3 = [ # MI 2 immutably(sample_string_for_tree)(t, [0, 2]) for _ in range(1000) ] assert len(set(mi_samples)) < len(set(nomi_samples)) def is_close(x, y, tol): return abs(x - y) < tol import rfutils.entropy nomi_mi = rfutils.entropy.mutual_information( Counter(rfutils.flatmap(lambda s: rfutils.sliding(s, 2), nomi_samples))) assert is_close(nomi_mi, 0, 0.01) mi_mi = rfutils.entropy.mutual_information( Counter(rfutils.flatmap(lambda s: rfutils.sliding(s, 2), mi_samples))) assert is_close(mi_mi, 1, 0.01) mi2_mi = rfutils.entropy.mutual_information( Counter(rfutils.flatmap(lambda s: rfutils.sliding(s, 2), mi_samples2))) assert is_close(mi2_mi, 1, 0.01) mi3_mi = rfutils.entropy.mutual_information( Counter(rfutils.flatmap(lambda s: rfutils.sliding(s, 2), mi_samples3))) assert is_close(mi3_mi, 2, 0.1)
def bind(self, f): return Amb(rfutils.flatmap(f, self.values))
def all_head_final_trees(n): return filter( tree_is_head_final, flatmap(directed_trees, all_undirected_trees(n)) )
def all_directed_trees(n): """ Generate all rooted directed trees with n nodes. There are n ^ (n - 1) such trees. """ return flatmap(directed_trees, all_undirected_trees(n))
def all_directed_forest_components(n): forests = all_undirected_forest_components(n) return flatmap(functools.partial(nondet_map, directed_trees), forests)
def main(): langs = sorted(corpora.sud_langs) rfutils.write_dicts(sys.stdout, rfutils.flatmap(run, langs))