The orngNetwork module provides the functionality to perform network
analysis and layout optimization.
Network
Network class holds network
structure information and supports basic network analysis. Network class is
inherited from orange.GraphAsList. Refer to
orange.GraphAsList for more graph analysis tools. See the orangeom.Pathfinder class
for a way to simplify your network.
Attributes
- coors
- Coordinates for all vertices. They are initialized to random positions. You can modify them manually or use one of the optimization algorithms. Usage:
coors[0][i]
, coors[1][i]
; 0 for x-axis, 1 for y-axis
- items
- ExampleTable with information about vertices. Number of rows should match the number of vertices.
- links
- ExampleTable with information about edges. Number of rows should match the number of edges. Two float attributes named "u" and "v" must be in links table domain to relate the data of an example to an edge. Here, egde is defined by two vertices "u" and "v".
- optimization
- An instance of the NetworkOptimization class. Various network layout optimization methods can be applied to the network through this attribute.
Methods
- Network()
- Default constrctor. Creates a network with one vertex.
- Network(nVertices, nEdges, directedGraph=False)
- Creates a network with n vertices. The nEdges attribute is used to set the number of edge types. It should always be 0, except in some special cases.
- fromDistanceMatrix(matrix, lower, upper, kNN=0)
- Creates edges between vertices with the distance within given threshold.
Matrix should be of orange.SymMatrix type. The number ob objects in a matrix
must match the number of vertices in a network. The kNN parameter can be used to
specify the minimum number of closest vertices to be connected.
- save(file)
- Saves the network to a Pajek (.net) or GML file format. ExampleTables items and links are saved automatically if the value is not None. They are saved to "file_items.tab" and "file_links.tab" files.
Static Methods
- read(file)
- Reads the network from a Pajek (.net) or GML file. Usage:
orngNetwork.Network.read(filename)
NetworkOptimization
NetworkOptimization
is the main class for performing network layout optimization. Network structure is defined in orngNetwork.Network class.
Attributes
- graph
- Holds the Network object.
Methods
- NetworkOptimization()
- Default constrctor.
- NetworkOptimization(network)
- Constructor that takes the Network object.
- random()
- Random layout optimization.
- fruchtermanReingold(steps=100, temperature=1000, coolFactor=default, hiddenNodes=[], weighted=False)
- Fruchterman-Reingold spring layout optimization. Set number of iterations with argument
steps
, start temperature with temperature
(for example: 1000) and set list of hidden nodes with argument hidden_nodes
.
- radialFruchtermanReingold(center, steps=100, temperature=1000)
- Radial Fruchterman-Reingold spring layout optimization. Set center node with attribute
center
, number of iterations with argument steps
and start temperature with temperature
(for example: 1000).
- circularOriginal()
- Circular layout optimization based on original order.
- circularRandom()
- Circular layout optimization based on random order.
- circularCrossingReduction()
- Circular layout optimization (Michael Baur, Ulrik Brandes) with crossing reduction.
Examples
Network constructor and random layout
In our first example we create a Network object with a simple full graph (K5). Vertices are initially placed randomly. Graph is visualized using pylabs matplotlib. NetworkOptimization class is not needed since we do not apply any layout optimization method in this example.
import orngNetwork
from pylab import *
# create graph object of type GraphAsList
# vertices are placed randomly in Network constructor
net = orngNetwork.Network(5, 0)
# set edges
for i in range(4):
for j in range(i + 1, 5):
net[i,j] = 1
# read all edges and plot a line
for u, v in net.getEdges():
x1, y1 = net.coors[0][u], net.coors[1][u]
x2, y2 = net.coors[0][v], net.coors[1][v]
plot([x1, x2], [y1, y2], 'b-')
# read x and y coordinates to Python list
x = net.coors[0]
y = net.coors[1]
# plot vertices
plot(x, y, 'ro')
show()
Executing the above script pops-up a pylab window with the
following graph drawing:
Network layout optimization
This example demonstrates how to optimize network layout using one of included algorithms.
# vertices are placed randomly in NetworkOptimization constructor
networkOptimization = orngNetwork.NetworkOptimization(net)
# optimize verices layout with one of included algorithms
networkOptimization.fruchtermanReingold(100, 1000)
You can use one of the following optimization algorithms:
- .random()
- .fruchtermanReingold(steps, temperature, coolingFactor=Default, hiddenNodes=[], weighted=False)
- .radialFruchtermanReingold(center, steps, temperature)
- .circularOriginal()
- .circularRandom()
- .circularCrossingReduction()
Spring forces layout optimization is the result of the above script:
Reading and saving a network
This example demonstrates reading a network. Network class can read or write Pajek (.net) or GML file format.
# read the network from file
net = orngNetwork.Network.read("K5.net")
# save the network to file
net.save("K5.net")
Visualize a network in NetExplorer widget
This example demonstrates how to display a network in NetExplorer.
net = orngNetwork.Network.read('musicians.net')
net.items = orange.ExampleTable('musicians_items.tab')
ow = OWNetExplorer.OWNetExplorer()
ow.setNetwork(net)
ow.show()