Пример #1
0
def test_shortest_simple_paths_mod_signed():
    seg, sng, all_ns = _setup_signed_graph()

    # Add a very long path
    sng.add_edge(('B3', INT_PLUS), ('Z1', INT_PLUS),
                 belief=0.6,
                 weight=-np.log(0.6))

    source = ('B2', INT_PLUS)
    target = ('D1', INT_PLUS)  # D1 upregulated

    # Check that beliafs and weights have been set
    assert sng.edges[(source, ('C1', INT_PLUS))].get('weight', False)
    assert sng.edges[(source, ('C1', INT_PLUS))].get('belief', False)

    # Unweighted search
    expected_paths = {(source, ('C1', INT_PLUS), target),
                      (source, ('C1', INT_MINUS), target)}
    paths = [tuple(p) for p in shortest_simple_paths(sng, source, target)]
    assert len(paths) == 2
    assert set(paths) == expected_paths, 'sets of paths not equal'

    # Weighted search
    source = ('B3', INT_PLUS)
    expected_paths = {
        (source, ('C1', 1), target),
        (source, ('Z1', 0), ('A1', 0), ('B1', 0), ('C1', 0), target),
        (source, ('Z1', 0), ('A1', 1), ('B1', 1), ('C1', 1), target)
    }
    paths = tuple([
        tuple(p)
        for p in shortest_simple_paths(sng, source, target, weight='weight')
    ])
    assert len(paths) == 3
    assert set(paths) == expected_paths, 'sets of paths not equal'
Пример #2
0
def test_shortest_simple_paths_mod_unsigned():
    dg, all_ns = _setup_unsigned_graph()
    dg.add_edge('B1', 'A3', belief=0.7, weight=-np.log(0.7))  # Add long path
    # between
    # B1 and C1
    source, target = 'B1', 'D1'

    # Unweighted searches
    paths = [p for p in shortest_simple_paths(dg, source, target)]
    assert len(paths) == 2
    assert tuple(paths[0]) == ('B1', 'C1', 'D1')
    assert tuple(paths[1]) == ('B1', 'A3', 'B2', 'C1', 'D1')
    assert len([
        p
        for p in shortest_simple_paths(dg, source, target, ignore_nodes={'A3'})
    ]) == 1
    # Test nx.NoPathFound
    try:
        len([
            p for p in shortest_simple_paths(
                dg, source, target, ignore_nodes={'C1'})
        ]) == 0
    except Exception as exc:
        assert isinstance(exc, nx.NetworkXNoPath)

    # Weighted searches
    paths = [
        p for p in shortest_simple_paths(dg, source, target, weight='weight')
    ]
    assert tuple(paths[0]) == ('B1', 'C1', 'D1')
    assert tuple(paths[1]) == ('B1', 'A3', 'B2', 'C1', 'D1')
Пример #3
0
    def find_paths(hashes):
        def ref_counts_function(G, u, v):
            edge_hashes = G.graph['hashes']
            edge_hashes[('A3', 'B1')] = [14]
            try:
                return \
                    len(set(hashes).intersection(set(edge_hashes[(u, v)]))),\
                    sum(hashes)
            except KeyError:
                return 0, sum(hashes)

        return list(
            shortest_simple_paths(G,
                                  source,
                                  target,
                                  hashes=hashes,
                                  ref_counts_function=ref_counts_function))
Пример #4
0
    def count_paths(source, target, hashes):
        def ref_counts_function(G, u, v):
            edge_hashes = G.graph['hashes']
            edge_hashes[('A2', 'B3')] = [14]
            edge_hashes[('B3', 'B1')] = [15]
            try:
                return len(set(hashes).intersection(set(
                    edge_hashes[(u, v)]))), None
            except KeyError:
                return 0, None

        try:
            paths = list(
                shortest_simple_paths(G,
                                      source,
                                      target,
                                      hashes=hashes,
                                      weight='weight',
                                      strict_mesh_id_filtering=True,
                                      ref_counts_function=ref_counts_function))
            return len(paths)
        except nx.NetworkXNoPath:
            return 0