DerwenAI / kglab

Graph Data Science: an abstraction layer in Python for building knowledge graphs, integrated with popular graph libraries – atop Pandas, NetworkX, RAPIDS, RDFlib, pySHACL, PyVis, morph-kgc, pslpython, pyarrow, etc.
https://derwen.ai/docs/kgl/
MIT License
574 stars 65 forks source link

Can't find an easy path to generating a visual of the query results #281

Closed dmoore247 closed 1 year ago

dmoore247 commented 1 year ago

I'm submitting a

Current Behaviour:

Can't find an easy path to generating a visual of the query results My graph is large at 86,000 triples, this is more than I want to visualize.

How can I get quickly from a sparql query to a visual of the query results?

Expected Behaviour:

I've been trying to figure out how to chain operations, like creating a graph from a query result, visual from results etc. Does something already exist?

How about something like this?

kg = kglab.KnowledgeGraph().load_rdf("aws.ttl")
sparql = """
SELECT  ?s ?p ?o
WHERE {
    ?s ?p ?o
}
LIMIT 1000
"""
kg2 = kg.sparql_query_as_kg(sparql)
subgraph = kglab.SubgraphTensor(kg2)
pyvis_graph = subgraph.build_pyvis_graph(notebook=False)
displayHTML(pyvis_graph.generate_html())

or

...
res = kg.sparql_query(sparql)
kg2 = kglab.KnowledgeGraph(results=res)
subgraph = kglab.SubgraphTensor(kg2)
pyvis_graph = subgraph.build_pyvis_graph(notebook=False)
displayHTML(pyvis_graph.generate_html())
Mec-iS commented 1 year ago

Hi, thanks for using kglab. For large subgraphs I would suggest using networkx for visualization as demonstrated in this example.

It is possible to load a subgraph generated by a query:

import networkx as nx

subgraph = kglab.SubgraphMatrix(kg, sparql)
nx_graph = subgraph.build_nx_graph(nx.DiGraph(), bipartite=True)

And plot it:

nx.draw(nx_graph, node_color=color, edge_color="gray", with_labels=True)
plt.show()