def test_rdflib_to_networkx():
    try:
        import networkx
    except ImportError:
        raise SkipTest("couldn't find networkx")
    from rdflib.extras.external_graph_libs import rdflib_to_networkx_multidigraph
    from rdflib.extras.external_graph_libs import rdflib_to_networkx_digraph
    from rdflib.extras.external_graph_libs import rdflib_to_networkx_graph
    g = Graph()
    a, b, l = URIRef('a'), URIRef('b'), Literal('l')
    p, q = URIRef('p'), URIRef('q')
    edges = [(a, p, b), (a, q, b), (b, p, a), (b, p, l)]
    for t in edges:
        g.add(t)


    mdg = rdflib_to_networkx_multidigraph(g)
    assert len(mdg.edges()) == 4
    assert mdg.has_edge(a, b)
    assert mdg.has_edge(a, b, key=p)
    assert mdg.has_edge(a, b, key=q)

    mdg = rdflib_to_networkx_multidigraph(g, edge_attrs=lambda s, p, o: {})
    assert mdg.has_edge(a, b, key=0)
    assert mdg.has_edge(a, b, key=1)


    dg = rdflib_to_networkx_digraph(g)
    assert dg[a][b]['weight'] == 2
    assert sorted(dg[a][b]['triples']) == [(a, p, b), (a, q, b)]
    assert len(dg.edges()) == 3
    assert dg.size() == 3
    assert dg.size(weight='weight') == 4.0

    dg = rdflib_to_networkx_graph(g, False, edge_attrs=lambda s, p, o:{})
    assert 'weight' not in dg[a][b]
    assert 'triples' not in dg[a][b]


    ug = rdflib_to_networkx_graph(g)
    assert ug[a][b]['weight'] == 3
    assert sorted(ug[a][b]['triples']) == [(a, p, b), (a, q, b), (b, p, a)]
    assert len(ug.edges()) == 2
    assert ug.size() == 2
    assert ug.size(weight='weight') == 4.0

    ug = rdflib_to_networkx_graph(g, False, edge_attrs=lambda s, p, o:{})
    assert 'weight' not in ug[a][b]
    assert 'triples' not in ug[a][b]
Beispiel #2
0
    def show(self):
        from rdflib.extras.external_graph_libs import rdflib_to_networkx_multidigraph
        import networkx as nx
        import matplotlib.pyplot as plt

        plt.figure(figsize=(19, 19))

        G = rdflib_to_networkx_multidigraph(self)

        for node in list(G.nodes):
            if 'Ttl' in node.title():
                G.remove_node(node)

        nx.relabel_nodes(
            G, {node: node.title().split('#')[-1]
                for node in G.nodes},
            copy=False)

        pos = nx.spring_layout(G, iterations=30000, k=1000)
        nx.draw_networkx_edges(G, pos, arrowsize=10)
        nx.draw_networkx_nodes(G, pos, node_size=4000)

        nx.draw_networkx_labels(G, pos)
        nx.draw_networkx_edge_labels(
            G, pos, edge_labels={k[:2]: k[2].split('#')[-1]
                                 for k in G.edges})

        plt.show()
def test_rdflib_to_networkx():
    try:
        import networkx
    except ImportError:
        raise SkipTest("couldn't find networkx")
    from rdflib.extras.external_graph_libs import rdflib_to_networkx_multidigraph
    from rdflib.extras.external_graph_libs import rdflib_to_networkx_digraph
    from rdflib.extras.external_graph_libs import rdflib_to_networkx_graph

    g = Graph()
    a, b, l = URIRef("a"), URIRef("b"), Literal("l")
    p, q = URIRef("p"), URIRef("q")
    edges = [(a, p, b), (a, q, b), (b, p, a), (b, p, l)]
    for t in edges:
        g.add(t)

    mdg = rdflib_to_networkx_multidigraph(g)
    assert len(mdg.edges()) == 4
    assert mdg.has_edge(a, b)
    assert mdg.has_edge(a, b, key=p)
    assert mdg.has_edge(a, b, key=q)

    mdg = rdflib_to_networkx_multidigraph(g, edge_attrs=lambda s, p, o: {})
    assert mdg.has_edge(a, b, key=0)
    assert mdg.has_edge(a, b, key=1)

    dg = rdflib_to_networkx_digraph(g)
    assert dg[a][b]["weight"] == 2
    assert sorted(dg[a][b]["triples"]) == [(a, p, b), (a, q, b)]
    assert len(dg.edges()) == 3
    assert dg.size() == 3
    assert dg.size(weight="weight") == 4.0

    dg = rdflib_to_networkx_graph(g, False, edge_attrs=lambda s, p, o: {})
    assert "weight" not in dg[a][b]
    assert "triples" not in dg[a][b]

    ug = rdflib_to_networkx_graph(g)
    assert ug[a][b]["weight"] == 3
    assert sorted(ug[a][b]["triples"]) == [(a, p, b), (a, q, b), (b, p, a)]
    assert len(ug.edges()) == 2
    assert ug.size() == 2
    assert ug.size(weight="weight") == 4.0

    ug = rdflib_to_networkx_graph(g, False, edge_attrs=lambda s, p, o: {})
    assert "weight" not in ug[a][b]
    assert "triples" not in ug[a][b]
Beispiel #4
0
def simplify(collapse, blob):
    to_remove = []
    for coll in collapse:
        exclude = set(p for p in coll)
        candidates = [e for e in blob['edges'] if e['pred'] in exclude]
        for c in candidates:
            # make sure we can remove the edges later
            # if they have meta the match will fail
            if 'meta' in c:
                c.pop('meta')

        if candidates:
            edges = [Edge.fromOboGraph(c) for c in candidates]
            g = OntGraph().populate_from_triples(e.asRdf() for e in edges)
            nxg = egl.rdflib_to_networkx_multidigraph(g)
            connected = list(nx.weakly_connected_components(nxg))  # FIXME may not be minimal
            ends = [e.asRdf()[-1] for e in edges if e.p == coll[-1]]
            for c in connected:
                #log.debug('\n' + pformat(c))
                nxgt = nx.MultiDiGraph()
                nxgt.add_edges_from(nxg.edges(c, keys=True))
                ordered_nodes = list(nx.topological_sort(nxgt))
                paths = [p
                         for n in nxgt.nodes()
                         for e in ends
                         for p in list(nx.all_simple_paths(nxgt, n, e))
                         if len(p) == len(coll) + 1]

                for path in sorted(paths):
                    ordered_edges = nxgt.edges(path, keys=True)
                    oe2 = [Edge.fromNx(e) for e in ordered_edges if all([n in path for n in e[:2]])]
                    predicates = [e.p for e in oe2]
                    #log.debug('\n' + pformat(oe2))
                    if predicates == coll: #in collapse:
                        to_remove.extend(zap(path, predicates, oe2, blob))
                    else:  # have to retain this branch to handle cases where the end predicate is duplicated
                        log.error('\n' + pformat(predicates) +
                                    '\n' + pformat(coll))
                        for preds in [coll]:
                            sublist_start = listIn(predicates, preds)
                            if sublist_start is not None:
                                i = sublist_start
                                j = i + len(preds)
                                npath = path[i:j + 1]  # + 1 to include final node
                                oe2 = oe2[i:j]
                                predicates = predicates[i:j]
                                to_remove.extend(zap(npath, predicates, oe2, blob))

    for r in to_remove:
        if r in blob['edges']:
            blob['edges'].remove(r)

    #log.debug('\n' + pformat(blob['edges']))
    return blob  # note that this is in place modification so sort of supruflous
Beispiel #5
0
def makeGraph(rdf_object):
    """
    Make a graph wich is the visual representaton of the RDF object.
    """

    G = rdflib_to_networkx_multidigraph(rdf_object)

    # Plot Networkx instance of RDF Graph
    pos = nx.spring_layout(G, scale=2)
    edge_labels = nx.get_edge_attributes(G, 'r')
    nx.draw_networkx_edge_labels(G, pos, labels=edge_labels)
    nx.draw(G, with_labels=True)
    plt.savefig("test")
Beispiel #6
0
def rdflib_viz(url, ft=None):  #or have it default to ntriples ;'turtle'
    if rdflib_inited == None:
        init_rdflib()
    import rdflib
    from rdflib.extras.external_graph_libs import rdflib_to_networkx_multidigraph
    import networkx as nx
    import matplotlib.pyplot as plt
    g = rdflib.Graph()
    if ft != None:
        result = g.parse(url)  #if didn't do mv, could send in format=
    else:
        result = g.parse(url, format=ft)
    G = rdflib_to_networkx_multidigraph(result)
    #stackoverflow.com/questions/3567018/how-can-i-specify-an-exact-output-size-for-my-networkx-graph
    #plt.figure(3,figsize=(12,12))
    plt.figure(3, figsize=(18, 18))
    # Plot Networkx instance of RDF Graph
    pos = nx.spring_layout(G, scale=2)
    edge_labels = nx.get_edge_attributes(G, 'r')
    nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
    nx.draw(G, with_labels=True)
    #if not in interactive mode for
    plt.show()
Beispiel #7
0
    def extract_conjunctive_queries(r_graph):
        import networkx as nx
        import rdflib
        from rdflib import URIRef, BNode, Literal
        from rdflib.namespace import RDF
        from rdflib.extras.external_graph_libs import rdflib_to_networkx_multidigraph

        join_nodes = r_graph.subjects(RDF.type,
                                      URIRef("http://www.dfki.de/voc#Join"))

        #        nx_r_graph = rdflib_to_networkx_multidigraph(r_graph, edge_attrs=lambda s, p, o: {'e': r_graph.qname(p)})
        nx_r_graph = rdflib_to_networkx_multidigraph(
            r_graph, edge_attrs=lambda s, p, o: {'e': p})
        #        draw_graph(nx_r_graph)

        nx_conjunctive_query_patterns = list()

        for join_node in join_nodes:
            nx_conjunctive_query_patterns.append(
                nx.subgraph(nx_r_graph,
                            list(nx.dfs_preorder_nodes(nx_r_graph,
                                                       join_node))).copy())

        return nx_conjunctive_query_patterns
Beispiel #8
0
 def materialize_indirect_claim_links(self):
     mdg = rdflib_to_networkx_multidigraph(self._graph)
# TODO Konsolenbeispiele von hier http://lambdamusic.github.io/Ontospy/
# auf lüscho anwenden

import rdflib
from rdflib.extras.external_graph_libs import rdflib_to_networkx_multidigraph
import networkx as nx
import matplotlib.pyplot as plt

url = 'https://www.w3.org/TeamSubmission/turtle/tests/test-30.ttl'

g = rdflib.Graph()
result = g.parse(url, format='turtle')

G = rdflib_to_networkx_multidigraph(result)

# Plot Networkx instance of RDF Graph
pos = nx.spring_layout(G, scale=2)
edge_labels = nx.get_edge_attributes(G, 'r')
nx.draw_networkx_edge_labels(G, pos, labels=edge_labels)
nx.draw(G, with_labels=True)
plt.show()
Beispiel #10
0
def search():
    # test namespace
    BS = rdflib.Namespace('https://w3id.org/def/basicsemantics-owl#')
    # test graph
    csv_test_graph = rdflib.Graph('IOMemory', rdflib.BNode())
    # create graph
    csv_graph = rdflib.Graph('IOMemory', rdflib.BNode())
    print(request.data)
    print('in search method ---')
    print(str(request.form.get("searchx1")))
    class_labels = request.form.getlist('dropdown')
    print((class_labels))
    print(dictOfWordsFromCSV.get(class_labels[0]))
    gene = []
    for i in range(0, len(class_labels)):
        print(class_labels_dict.get(str(request.form.get("search" + str(i + 1)))))
        # gene.append((request.form.get("search" + str(i + 1)), class_labels[i]))
        # g.add((rdflib.URIRef(class_labels_dict.get(str(request.form.get("search" + str(i + 1))))),
        #       rdflib.Literal(class_labels[i]), rdflib.Literal(request.form.get("search" + str(i + 1)))))
        if i < 2:
            csv_graph.add(
                (rdflib.Literal(request.form.get("search" + str(i + 1))), BS[request.form.get("prop" + str(i + 1))],
                 rdflib.Literal(dictOfWordsFromCSV.get(class_labels[i])[0])))
            csv_graph.add(
                (rdflib.Literal(request.form.get("search" + str(i + 1))), BS[request.form.get("prop" + str(i + 1))],
                 rdflib.Literal(class_labels[i])))
        if i >= 2 and i < 9:
            csv_graph.add((rdflib.Literal(request.form.get("search" + str(i + 1))), OWL.hasValue,
                           rdflib.Literal(dictOfWordsFromCSV.get(class_labels[i])[0])))
            csv_graph.add((rdflib.Literal(request.form.get("search" + str(i + 1))), OWL.hasUnit,
                           rdflib.Literal(dictOfWordsFromCSV.get(class_labels[i])[1])))
            csv_graph.add((rdflib.Literal(request.form.get("search" + str(i + 1))), OWL.hasIdentifier,
                           rdflib.Literal(class_labels[i])))
        # owl = OWL.str(request.form.get("prop1"))

        # test graph add data request.form.get("search" + str(i + 1))
        # csv_test_graph.add((rdflib.URIRef(request.form.get("search" + str(i + 1))),
        #                     BS[request.form.get("prop" + str(i + 1))],
        #                     rdflib.Literal(request.form.get("searchx" + str(
        #                         i + 1)))))  # rdflib.RDF.type(str(request.form.get("prop" + str(i + 1))))

    for j in range(7):
        csv_graph.add((rdflib.Literal('column ' + str(j)), OWL.hasLabel,
                       rdflib.Literal(dictOfWordsHeaders[j][1])))
        csv_graph.add((rdflib.Literal('column ' + str(j)), OWL.hasIndex,
                       rdflib.Literal(dictOfWordsHeaders[j][0])))
        csv_graph.add((rdflib.Literal('column ' + str(j)), OWL.hasUnit,
                       rdflib.Literal(dictOfWordsHeaders[j][2])))

    print('----------graph value----------------')
    print(csv_test_graph.serialize(format="turtle").decode())
    # The turtle format has the purpose of being more readable for humans.
    print(csv_graph.serialize(format="turtle").decode())
    print(rdflib.Literal(request.form.get("search" + str(0))))
    # for s, p, o in g:
    #   print(s, p, o)
    gg = (request.form.get('search1'))
    # _Gene = request.form['inputGene']
    # _gene = str(gene)
    print('changed')
    # rint(gene, csv_values.get('prob'))
    selectValue = request.form.get('dropdown')
    ss = request.form.getlist('dropdown')
    print(ss)
    print(selectValue, gene)

    G = rdflib_to_networkx_multidigraph(csv_graph)
    # Plot Networkx instance of RDF Graph
    pos = nx.spring_layout(G, scale=2)
    edge_labels = nx.get_edge_attributes(G, 'r')
    nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
    nx.draw(G, with_labels=True)
    plt.savefig('./rdf_triple.png')

    # save file
    csv_graph.serialize("./test.ttl", format="turtle")
    # flash('RDF file successfully created')
    return csv_graph  # jsonify(g_test.serialize(format="turtle").decode())
Beispiel #11
0
def convert_to_a_graph(rdf_graph):
    multi_di_graph = rdflib_to_networkx_multidigraph(rdf_graph)
    directed = multi_di_graph.is_directed()
    strict = nx.number_of_selfloops(
        multi_di_graph) == 0 and not multi_di_graph.is_multigraph()
    a_graph = AGraph(name=multi_di_graph.name,
                     strict=strict,
                     directed=directed)

    a_graph.graph_attr.update(
        multi_di_graph.graph.get(
            "graph", {
                'label': 'Network Map',
                'fontsize': '16',
                'fontcolor': 'white',
                'bgcolor': '#333333',
                'rankdir': 'BT',
                'overlap': 'prism',
                'splines': 'true'
            }))
    a_graph.node_attr.update(
        multi_di_graph.graph.get(
            "node", {
                'fontname': 'Helvetica',
                'fontcolor': 'white',
                'color': '#006699',
                'style': 'filled',
                'fillcolor': '#006699',
            }))
    a_graph.edge_attr.update(
        multi_di_graph.graph.get(
            "edge", {
                'style': 'dashed',
                'color': 'green',
                'arrowhead': 'open',
                'fontname': 'Courier',
                'fontsize': '14',
                'fontcolor': 'white',
            }))

    a_graph.graph_attr.update((k, v) for k, v in multi_di_graph.graph.items()
                              if k not in ("graph", "node", "edge"))

    for n, node_data in multi_di_graph.nodes(data=True):
        a_graph.add_node(n)
        a = a_graph.get_node(n)
        a.attr.update({k: str(v) for k, v in node_data.items()})

    if multi_di_graph.is_multigraph():
        for u, v, key, edge_data in multi_di_graph.edges(data=True, keys=True):
            str_edge_data = {
                k: str(v)
                for k, v in edge_data.items() if k != "key"
            }
            a_graph.add_edge(u, v, headlabel=str(key))
            a = a_graph.get_edge(u, v)
            a.attr.update(str_edge_data)

    a_graph.layout()

    return a_graph