def test_nb_edges_increase_monotonically_paths_via_endpoints( get_sample_hypothgraph): hypothgraph = get_sample_hypothgraph source, target = hypoth_conf.generate_rich_endpoints(hypothgraph) sorted_paths_via_endpoints = list( paths.passing_via_endpoints_sorted(hypothgraph, source, target)) # we test by iteratively creating a sub hypothesis graph, and checking that # the two consecutively produced snapshots of the sub hypothesis graph will # contain monotonically increasing number of edges. Moreover, at every time # step the hypothesis graph topology is preserved # we collect the number of produced edges nb_edges_snapshots = [] iterative_sub_hypothgraph = nx.DiGraph() for sorted_path in sorted_paths_via_endpoints: iterative_sub_hypothgraph.add_path(sorted_path) nb_edges_snapshots.append(len(iterative_sub_hypothgraph.edges())) # check monotonicity for previous, current in zip(nb_edges_snapshots, nb_edges_snapshots[1:]): assert current >= previous
def get_sample_configuration(): hypothgraph = sample_graphs.sample_hypothgraph() source, target = hypoth_conf.generate_rich_endpoints(hypothgraph) subgraph = sub_hypothgraph.generate_sub_hypothgraph( hypothgraph, source, target) return hypothgraph, subgraph, source, target
def test_generate_rich_endpoints(get_digraph_with_cycle): hypothgraph = get_digraph_with_cycle min_nb_paths = 4 # just run 100 tests for _ in xrange(100): source, target = hypoth_conf.generate_rich_endpoints(hypothgraph, min_nb_paths) all_simple_paths = list(nx.all_simple_paths(hypothgraph, source, target)) assert len(all_simple_paths) >= min_nb_paths
def test_paths_pass_through_endpoints_are_sorted(get_sample_hypothgraph): hypothgraph = get_sample_hypothgraph source, target = hypoth_conf.generate_rich_endpoints(hypothgraph) sorted_paths_via_endpoints = list( paths.passing_via_endpoints_sorted(hypothgraph, source, target)) # paths are monotonically increasing for previous, current in zip(sorted_paths_via_endpoints, sorted_paths_via_endpoints[1:]): assert len(current) >= len(previous)
def sample_big_and_small(ratio_endpoints_paths=0.5, ratio_on_boundary_paths=0.5): big = sample_graphs.sample_hypothgraph() source, target = hypoth_conf.generate_rich_endpoints(big, min_nb_paths=5) small = sub_hypothgraph.generate_sub_hypothgraph( big, source, target, ratio_endpoints_paths=ratio_endpoints_paths, ratio_on_boundary_paths=ratio_on_boundary_paths, data=True) return big, small, source, target
def test_uniqueness_of_simple_endpoints_paths(get_sample_hypothgraph): hypothgraph = get_sample_hypothgraph source, target = hypoth_conf.generate_rich_endpoints(hypothgraph) endpoints_paths = list(nx.all_simple_paths(hypothgraph, source, target)) nb_paths = len(endpoints_paths) # no path is repeated in all the paths, you cannot just use # `set(all_paths)`, since a path is a list, an unhashable type for path in endpoints_paths: other_paths = [other_path for other_path in endpoints_paths if not other_path == path] # number of other paths should be `nb_paths - 1` assert len(other_paths) == nb_paths-1
def test_ratio_endpoints_paths_sample_hypothgraph(get_sample_hypothgraph): hypothgraph = get_sample_hypothgraph source, target = hypoth_conf.generate_rich_endpoints(hypothgraph) ratio_paths = 0.7 endpoints_paths = list(nx.all_simple_paths(hypothgraph, source, target)) endpoints_ratio_paths = list( paths.take_ratio_endpoints_paths_rand(hypothgraph, source, target, ratio=ratio_paths)) # nb of generated paths is at least the given ratio to the total amount of # paths nb_ratio_paths = len(endpoints_ratio_paths) nb_total_paths = len(endpoints_paths) assert nb_ratio_paths == math.ceil(ratio_paths * nb_total_paths)
def test_uniqueness_of_simple_on_boundary_paths(get_sample_hypothgraph): hypothgraph = get_sample_hypothgraph source, target = hypoth_conf.generate_rich_endpoints(hypothgraph) on_boundary_nodes = boundary.on_boundary(hypothgraph, source, target) on_boundary_paths = list( paths.chain_many_to_many_path_generator(hypothgraph, on_boundary_nodes)) nb_paths = len(on_boundary_paths) # no path is repeated in all the paths, you cannot just use # `set(all_paths)`, since a path is a list, an unhashable type for path in on_boundary_paths: other_paths = [other_path for other_path in on_boundary_paths if not other_path == path] assert len(other_paths) == nb_paths-1
def test_paths_pass_through_endpoints(get_sample_hypothgraph): hypothgraph = get_sample_hypothgraph source, target = hypoth_conf.generate_rich_endpoints(hypothgraph) paths_via_endpoints = list( paths.passing_via_endpoints(hypothgraph, source, target)) for path in paths_via_endpoints: assert source in path assert target in path # generator over all complement paths all_paths = paths.chain_many_to_many_path_generator( hypothgraph, hypothgraph.nodes_iter()) other_paths = (other_path for other_path in all_paths if not other_path in paths_via_endpoints) for other_path in other_paths: assert not (source in other_path and target in other_path)
def test_ratio_on_boundary_paths_sample_hypothgraph(get_sample_hypothgraph): hypothgraph = get_sample_hypothgraph source, target = hypoth_conf.generate_rich_endpoints(hypothgraph) ratio_paths = 0.7 on_boundary_nodes = boundary.on_boundary(hypothgraph, source, target) boundary_paths = list( paths.chain_many_to_many_path_generator(hypothgraph, on_boundary_nodes)) boundary_ratio_paths = list( paths.take_ratio_boundary_paths_rand(hypothgraph, source, target, ratio=ratio_paths)) # nb of generated paths is at least the given ratio to the total amount of # paths nb_ratio_paths = len(boundary_ratio_paths) nb_total_paths = len(boundary_paths) assert nb_ratio_paths == math.ceil(ratio_paths * nb_total_paths)
def get_sample_hypothgraph_and_configurations(): hypothgraph = sample_graphs.sample_hypothgraph() source, target = hypoth_conf.generate_rich_endpoints(hypothgraph) partial_nodes = list( boundary.partial_nodes_boundary_interior(hypothgraph, source, target)) full_nodes = list( boundary.in_boundary_interior(hypothgraph, source, target)) nothing = Hypoth_Conf(source=source, target=target, evidenced_nodes=[]) partial = Hypoth_Conf(source=source, target=target, evidenced_nodes=partial_nodes) full = Hypoth_Conf(source=source, target=target, evidenced_nodes=full_nodes) return { 'hypothgraph': hypothgraph, 'nothing': nothing, 'partial_within': partial, 'full_within': full }