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]
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 #3
0
def rdflib_to_networkx(rdflib_obj):
    """Given an RDFlib graph object, convert it to a networkx
    graph. 

    Some of the utilities and features of networkx graphs are
    replicated by ComptoxAI's custom graph object, so internal methods
    should be considered first.
    """
    G = rdflib_to_networkx_graph(rdflib_obj)
    return G
Beispiel #4
0
def to_sparse_matrix(file):
    in_file = Path(file)
    in_dir = in_file.parent
    filename = in_file.name.replace(in_file.suffix, '')

    print("Reading RDF graph ...")
    g = Graph()
    g.parse(str(in_file), format="nt")

    print("Converting RDF graph to NetworkX graph ...")
    netx_graph = rdflib_to_networkx_graph(g)
    del g  # free up some memory

    print("Converting NetworkX graph to Scipy sparse matrix format ...")
    graph_matrix = nx.to_scipy_sparse_matrix(netx_graph)
    del netx_graph  # free up some memory

    outfile = f'{str(in_dir)}/{filename}.npz'
    scipy.sparse.save_npz(outfile, graph_matrix)
Beispiel #5
0
    def strip_and_produce_rdf_graph(self, rdf_graph: Graph):
        """
        A function that takes in an rdflib.graph.Graph object
        and transforms it into a datashader holoviews graph.
        Also performs the sparql query on the graph that can be set
        via the 'sparql' parameter
        """

        sparql = self.sparql
        qres = rdf_graph.query(sparql)
        uri_graph = Graph()
        for row in qres:
            uri_graph.add(row)

        new_netx = rdflib_to_networkx_graph(uri_graph)
        original = hv.Graph.from_networkx(new_netx, self._nx_layout,
                                          **self.graph_layout_params)
        output_graph = bundle_graph(original)
        return output_graph