yield parent, n
new = set(g[n]) - enqueued
enqueued |= new
queue.extend([(n, child) for child in new])
Check out how to use generators:
https://wiki.python.org/moin/Generators
40
Writing your own code: network triads
• Extract all unique triangles in a graph with integer node IDs
def get_triangles(g):
nodes = g.nodes()
for n1 in nodes:
neighbors1 = set(g[n1])
for n2 in filter(lambda x: x>n1, nodes):
neighbors2 = set(g[n2])
common = neighbors1 & neighbors2
for n3 in filter(lambda x: x>n2, common):
yield n1, n2, n3
41
Writing your own code: average neighbours’ degree
• Compute the average degree of each node’s neighbours:
• And the more compact version in a single line:
def avg_neigh_degree(g):
data = {}
for n in g.nodes():
if g.degree(n):
data[n] = float(sum(g.degree(i) for i in g[n]))/g.degree(n)
return data
def avg_neigh_degree(g):
return dict((n,float(sum(g.degree(i) for i in g[n]))/ g.degree(n))
for n in g.nodes() if g.degree(n))
42
5. Ready for your own analysis!
43
What you have learnt today
• How to create graphs from scratch, with generators and by loading local data
• How to compute basic network measures, how they are stored in NetworkX and
how to manipulate them with list comprehension
• How to load/store NetworkX data from/to files
• How to use matplotlib to visualize and plot results (useful for final report!)
• How to use and include NetworkX features to design your own algorithms
44
Useful links
●
Code & data used in this lecture:
https://www.dropbox.com/s/uwdzg48629lmic1/stna-
examples.zip
• NodeXL: a graphical front-end that integrates network analysis into Microsoft Office and Excel. (
http://nodexl.codeplex.com
/)
•
Pajek: a program for network analysis for Windows (
http://
pajek.imfm.si/doku.php).
•
Gephi: an interactive visualization and exploration platform (
http://gephi.org
/)
• Power-law Distributions in Empirical Data: tools for fitting heavy-tailed distributions to data (
http://www.santafe.edu/~
aaronc/powerlaws/)
•
GraphViz: graph visualization software (
http://
www.graphviz.org/)
•
Matplotlib: full documentation for the plotting library (
http://
matplotlib.org/)
•
Unfolding Maps: map visualization software in Java (
http://unfoldingmaps.org
/)
45
Questions?
46
E-mail: Petko.Georgiev@cl.cam.ac.uk
Dostları ilə paylaş: |