# genet / test_networkx.py

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 #!/usr/bin/env python ################################################################################ # $Id$ ################################################################################ # Copyright (c) 2008 Santiago Paya Miralta # # This file is part of mine. # # Mine is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ################################################################################ import pylab as P import networkx as NX from test_data import * ################################################################################ # Level 00 # G00 = NX.XGraph(name='Level 00', multiedges=True, selfloops=True) for r in db.query("SELECT * FROM gn_00_edges").dictresult(): G00.add_edges_from([(r['idna'], r['idnb'], r['id'])]) # May be there are isolated nodes. for r in db.query("SELECT * FROM gn_00_nodes").dictresult(): G00.add_node(r['id']) print 'G00 nodes:', G00.nodes() print 'G00 edges:', G00.edges() pos = NX.spring_layout(G00) NX.draw_networkx(G00, pos) P.show() P.savefig("genet_L0.png") print "Wrote genet_L0.png" ################################################################################ # Level 1 # G101 = NX.XGraph(name='Infrastructure', multiedges=True, selfloops=True) for e in genet.EL01: print e.idnA.id, e.idnB.id, e.id G101.add_edge(e.idnA.id, e.idnB.id, e.id) # May be there are isolated nodes. for n in genet.NL01: print n.id G101.add_node(n.id) print 'G101 nodes:', G101.nodes() print 'G101 edges:', G101.edges() pos = NX.spring_layout(G101) NX.draw_networkx(G101, pos) P.show() P.savefig("genet_L101.png") print "Wrote genet_L101.png" ################################################################################ # Fancy draw & DOT # G102 = NX.XGraph(name='Infrastructure', multiedges=True, selfloops=True) for e in genet.EL01: print e.idnA.id, e.idnB.id, e.id G102.add_edges_from([(e.idnA.id, e.idnB.id, e)]) # May be there are isolated nodes. for n in genet.NL01: print n.id G102.add_node(n.id) pos = NX.spring_layout(G102) NX.draw_networkx_nodes(G102, pos, node_color='g', node_size=500) NX.draw_networkx_edges(G102, pos, width=8.0, alpha=0.5, edge_color='g') labels={} labels[G102.nodes()[0]]='$\pi$' labels[G102.nodes()[1]]='$\mu$' labels[G102.nodes()[2]]='$\epsilon$' NX.draw_networkx_labels(G102, pos, labels=labels, font_color='w', font_family='sans-serif', font_size=12) P.show() P.savefig("genet_L102.png") print "Wrote genet_L102.png" """ With the dot file can do in the system: $neato -Tpng file.dot > file.png$ eog file.png """ NX.write_dot(G102,"genet_L102.dot") print "Wrote genet_L102.dot" ################################################################################ # Paths # """ The Dijkstra algorithms seems not to support multiedge graphs, because the NX._Graph.get_edge() function returns a list. Line 438 in /var/lib/python-support/python2.5/networkx/path.py, function single_source_dijkstra(G,source,target), should be: ... if G.multiedges: vw_dist = dist[v] + reduce(min, G.get_edge(v,w)) else: vw_dist = dist[v] + G.get_edge(v,w) ... or may be defining an appropiate function get_edge(v, w) depending on the multiedges property before the loooop. Example: >>> l = [0.23, 7, 0.1] >>> print reduce(min, l) 0.1 """ G103 = NX.XGraph(name='Infrastructure', multiedges=False, selfloops=True) for e in genet.EL01: # When creating the graph with edges (dont worry about isolated nodes) # the information passed is the edges' weight. print e.idnA.id, e.idnB.id, e.id G103.add_edge(e.idnA.id, e.idnB.id, e.iddown.len) paths = NX.single_source_shortest_path_length(G103, G103.nodes()[0], 1000) print paths paths = NX.single_source_shortest_path(G103, G103.nodes()[0], 1000) print paths path = NX.shortest_path(G103, G103.nodes()[0], G103.nodes()[NX.number_of_nodes(G103)-1]) print path paths = NX.single_source_dijkstra_path_length(G103, G103.nodes()[0]) print paths print 'The', G103.edges()[0], 'weight is:', G103.edges()[0][2] paths = NX.single_source_dijkstra_path(G103, G103.nodes()[0]) print paths
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.