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
581 stars 66 forks source link

SPARQL Query Visualization #179

Closed pebbie closed 3 years ago

pebbie commented 3 years ago

Hi, Thank you for this nice work!

I am suggesting adding the ability to visualize the SPARQL query (the graph pattern) to help in the tutorials. I wrote this small library a year ago. I notice that kglab uses pyvis so I refactor the code a bit today so it can be used in pyvis. This is available in the example notebook.

Cheers

louisguitton commented 3 years ago

HI @pebbie , that's excellent!

I was able to run your notebook with this

pip install pygraphviz jupyterlab pyvis networkx

and this query

#endpoint: https://sparql.rhea-db.org/sparql
PREFIX rh:<http://rdf.rhea-db.org/>

SELECT ?reaction ?reactionEquation
WHERE {
VALUES ?status { rh:Approved rh:Preliminary }
    ?reaction rdfs:subClassOf rh:Reaction .
    ?reaction rh:status ?status .    
    ?reaction rh:equation ?reactionEquation .
}
ORDER BY ?reaction

gets visualised into this

Would you feel like adapting your code and contributing this in a PR ? pyvis nbetworkx rdflib are already part of the requirements and I think we could cut out pygraphviz then right?

pebbie commented 3 years ago

Sure, I'll see if I can provide into networkx graph directly without using pygraphviz.

Do you have any ideas/suggestions on how or where this should be integrated with kglab?

pebbie commented 3 years ago

so i managed to adapt the code to generate the graph without using pygraphviz and adding some default style. pyvis_sparqlgpviz

any suggestion on how to integrate this?

ceteri commented 3 years ago

@pebbie wonderful!! So glad to see :)

Perhaps as a simple approach to integration, if you'd like to add a PR for this as a utility function in https://github.com/DerwenAI/kglab/blob/main/kglab/util.py then we could refactor into the classes.

@louisguitton @Ankush-Chander: How about, this could fit well within the PyVis-related methods of SubgraphTensor https://github.com/DerwenAI/kglab/blob/main/kglab/subg.py#L452 ?

louisguitton commented 3 years ago

Yeah as a first step, I would vouch for "parking" this code either in the util.py file or even in a standalone module sparql_viz.py or something like that.