0

Based on the question I asked last time: Applying PageRank to a topic hierarchy tree(using SPARQL query extracted from DBpedia)

As I currently got the PageRank value against the Regulated concept map. Toward the concept "Machine_learning", my currently code is below:

from SPARQLWrapper import SPARQLWrapper, N3
from rdflib import Graph, URIRef, Literal
import networkx as nx
from networkx.readwrite import json_graph
from rdflib.extras.external_graph_libs import rdflib_to_networkx_graph
from rdflib.namespace import Namespace, RDFS, FOAF
import matplotlib.pyplot as plt

#SPARQL query for Regulated SPARQL Query Strategy 
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("""construct { ?child skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child } 
where  { 

{ ?child skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child}  
UNION
{ ?gchild skos:broader/skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child}
}
""")


sparql.setReturnFormat(N3)
results = sparql.query().convert()
g = Graph()
g.parse(data=results, format="n3")
#Undirected graphs will be converted to a directed graph with two directed edges for each undirected edge.
dg = rdflib_to_networkx_graph(g, False, edge_attrs=lambda s,p,o:{})

#Draw regulated concept map
nx.draw(dg)
plt.draw()

#PageRank calculation
p1 = nx.pagerank(dg, alpha=0.85)

#p1 to pr(dict to list)
pr = sorted(p1.items(), key=lambda x:x[1],reverse=True)[:10]

#print sorted ranking
for key,val in pr:
    print(key,val)

There are several questions:

  1. How can I highlight nodes in draw_networkx visualization according to the SPARQL query? For example, I would like to assign the nodes from this query{ ?child skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child} in green and { ?gchild skos:broader/skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child} in red.
  2. Is it possible for me to adjust the nodes size and assign another color for those node according to the PageRank value that calculated above?
#Draw regulated concept map
# nx.draw(dg,pos=nx.spring_layout(dg),node_color='red') # use spring layout
# edges = nx.draw_networkx_edges(dg,pos=nx.spring_layout(dg))

pos = nx.spring_layout(dg)
source_node=copy.copy(pos)
print(source_node)
source_node_list = list(source_node.keys())
# print(source_node_list[0] in nx.spring_layout(dg))
# print(source_node_list)


options = {"node_size": 25, "alpha": 0.85}
graph=nx.draw_networkx_edges(dg, pos=pos, width=1.0, alpha=0.5)
graph=nx.draw_networkx_nodes(dg, pos=pos, nodelist=[source_node_list[1]], node_color="r", **options)
graph=nx.draw_networkx_nodes(dg, pos=pos, nodelist=[source_node_list[0],], node_color="b", **options)
graph=nx.draw_networkx_nodes(dg, pos=pos, nodelist=source_node_list[2:len(source_node_list)-1], node_color="g", **options)

# nx.draw(graph)
# plt.draw()


# nx.draw_networkx_edges(
#     dg,
#     pos,
#     edgelist=[source_node_list[1]],
#     width=8,
#     alpha=0.5,
#     edge_color="r",
# )
# nx.draw_networkx_edges(
#     dg,
#     pos,
#     edgelist=[source_node_list[0]],
#     width=8,
#     alpha=0.5,
#     edge_color="b"
# )

# nx.draw_networkx(dg, pos=nx.spring_layout(dg), node_color='blue',with_labels = False)
# labels=nx.draw_networkx_labels(dg,pos=nx.spring_layout(dg))
# nodes = nx.draw_networkx_nodes(dg,pos=nx.spring_layout(dg))

# nx.draw(dg)
# plt.draw()

Thank you very much in advance.

BBQ
  • 23
  • 4
  • 1
    the question is just about networkx, so you should not add sparql and dbpedia tags. Regarding networkx, did you read the [docs](https://networkx.github.io/documentation/stable/_downloads/networkx_reference.pdf) - or the examples? https://networkx.github.io/documentation/latest/auto_examples/drawing/plot_labels_and_colors.html#sphx-glr-auto-examples-drawing-plot-labels-and-colors-py - I think this should help – UninformedUser Oct 08 '20 at 06:27
  • Thank you for your comment. I read the document, However, the position of networkx drawing nodes is not fixed, I am still confused on changing the color of them. @UninformedUser – BBQ Oct 08 '20 at 08:43
  • I updated that what I tried. Hope that you can let me know if I done something wrong. @UninformedUser – BBQ Oct 08 '20 at 08:45

1 Answers1

0

I think you can pass a dictionary to the node_color parameter of the draw function. If you construct that dictionary such that the keys are the node-names and the values are the colours you want to associate with those node-names, then you should be able to get the formatting you want.

e.g. if you have been able to run some SPARQL to generate a list of nodes you want to be green, and another list that you want to be blue, and assuming you've got a green_list and blue_list pair of lists of these nodenames, then you could construct your dict something like this:

# create the colour specific dictionaries
blue_dict = { n : "blue" for n in blue_list }
green_dict = { n : "green" for n in green_list }
# merge them together into a combined dictionary
known_colour_d = { **blue_dict, **green_dict }
# construct the final dictionary, leaving unknown values with a colour of "orange"
node_colours_d = { n : known_colour_d.get(n, "orange") for n in dg.nodes() }

Ideally, you'd then place node_colours_d into your parameter at draw-time, and it should colour them in for you. From memory, some nx versions prefer colour to be delivered as a list that has the same order as the node-names - but this should work I think for the current version.

Alternately, assuming nx wants a list, then you could replace node_colours_d with node_colours_l to perform the same job, by substituting the following:

node_colours_l = [ known_colour_d.get(n, "orange") for n in dg.nodes() ]

This would create a list containing the colours mapped sequentially to the appearance of each node in the graph and you'd submit this list to the node_color parameter of your draw function.

Thomas Kimber
  • 10,601
  • 3
  • 25
  • 42