/usr/share/pyshared/pebl/network.py is in python-pebl 1.0.2-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | """Classes for representing networks and functions to create/modify them."""
import re
import tempfile
import os
from copy import copy, deepcopy
from itertools import chain
from bisect import insort
from collections import deque
import pydot
import numpy as N
from pebl.util import *
try:
from pebl import _network
except:
_network = None
class EdgeSet(object):
"""
Maintains a set of edges.
Performance characteristics:
- Edge insertion: O(1)
- Edge retrieval: O(n)
Uses adjacency lists but exposes an adjacency matrix interface via the
adjacency_matrix property.
"""
def __init__(self, num_nodes=0):
self._outgoing = [[] for i in xrange(num_nodes)]
self._incoming = [[] for i in xrange(num_nodes)]
def clear(self):
"""Clear the list of edges."""
self.__init__(len(self._outgoing))
def add(self, edge):
"""Add an edge to the list."""
self.add_many([edge])
def add_many(self, edges):
"""Add multiple edges."""
for src,dest in edges:
if dest not in self._outgoing[src]:
insort(self._outgoing[src], dest)
insort(self._incoming[dest], src)
def remove(self, edge):
"""Remove edges from edgelist.
If an edge to be removed does not exist, fail silently (no exceptions).
"""
self.remove_many([edge])
def remove_many(self, edges):
"""Remove multiple edges."""
for src,dest in edges:
try:
self._incoming[dest].remove(src)
self._outgoing[src].remove(dest)
except KeyError, ValueError:
pass
def incoming(self, node):
"""Return list of nodes (as node indices) that have an edge to given node.
The returned list is sorted.
Method is also aliased as parents().
"""
return self._incoming[node]
def outgoing(self, node):
"""Return list of nodes (as node indices) that have an edge from given node.
The returned list is sorted.
Method is also aliased as children().
"""
return self._outgoing[node]
parents = incoming
children = outgoing
def __iter__(self):
"""Iterate through the edges in this edgelist.
Sample usage:
for edge in edgelist:
print edge
"""
for src, dests in enumerate(self._outgoing):
for dest in dests:
yield (src, dest)
def __eq__(self, other):
for out1,out2 in zip(self._outgoing, other._outgoing):
if out1 != out2:
return False
return True
def __hash__(self):
return hash(tuple(tuple(s) for s in self._outgoing))
def __copy__(self):
other = EdgeSet.__new__(EdgeSet)
other._outgoing = [[i for i in lst] for lst in self._outgoing]
other._incoming = [[i for i in lst] for lst in self._incoming]
return other
def as_tuple(self):
return tuple(tuple(s) for s in self._outgoing)
@extended_property
def adjacency_matrix():
"""Set or get edges as an adjacency matrix.
The adjacency matrix is a boolean numpy.ndarray instance.
"""
def fget(self):
size = len(self._outgoing)
adjmat = N.zeros((size, size), dtype=bool)
selfedges = list(self)
if selfedges:
adjmat[unzip(selfedges)] = True
return adjmat
def fset(self, adjmat):
self.clear()
for edge in zip(*N.nonzero(adjmat)):
self.add(edge)
return locals()
@extended_property
def adjacency_lists():
"""Set or get edges as two adjacency lists.
Property returns/accepts two adjacency lists for outgoing and incoming
edges respectively. Each adjacency list if a list of sets.
"""
def fget(self):
return self._outgoing, self._incoming
def fset(self, adjlists):
if len(adjlists) is not 2:
raise Exception("Specify both outgoing and incoming lists.")
# adjlists could be any iterable. convert to list of lists
_outgoing, _incoming = adjlists
self._outgoing = [list(lst) for lst in _outgoing]
self._incoming = [list(lst) for lst in _incoming]
return locals()
def __contains__(self, edge):
"""Check whether an edge exists in the edgelist.
Sample usage:
if (4,5) in edges:
print "edge exists!"
"""
src, dest = edge
try:
return dest in self._outgoing[src]
except IndexError:
return False
def __len__(self):
return sum(len(out) for out in self._outgoing)
class Network(object):
"""A network is a set of nodes and directed edges between nodes"""
#
# Public methods
#
def __init__(self, nodes, edges=None):
"""Creates a Network.
nodes is a list of pebl.data.Variable instances.
edges can be:
* an EdgeSet instance
* a list of edge tuples
* an adjacency matrix (as boolean numpy.ndarray instance)
* string representation (see Network.as_string() for format)
"""
self.nodes = nodes
self.nodeids = range(len(nodes))
# add edges
if isinstance(edges, EdgeSet):
self.edges = edges
elif isinstance(edges, N.ndarray):
self.edges = EdgeSet(len(edges))
self.edges.adjacency_matrix = edges
else:
self.edges = EdgeSet(len(self.nodes))
if isinstance(edges, list):
self.edges.add_many(edges)
elif isinstance(edges, str) and edges:
edges = edges.split(';')
edges = [tuple([int(n) for n in e.split(',')]) for e in edges]
self.edges.add_many(edges)
def is_acyclic(self, roots=None):
"""Uses a depth-first search (dfs) to detect cycles."""
roots = list(roots) if roots else self.nodeids
if _network:
return _network.is_acyclic(self.edges._outgoing, roots, [])
else:
return self.is_acyclic_python(roots)
def is_acyclic_python(self, roots=None):
"""Uses a depth-first search (dfs) to detect cycles."""
def _isacyclic(tovisit, visited):
if tovisit.intersection(visited):
# already visited a node we need to visit. thus, cycle!
return False
for n in tovisit:
# check children for cycles
if not _isacyclic(set(children(n)), visited.union([n])):
return False
# got here without returning false, so no cycles below rootnodes
return True
#---------------
children = self.edges.children
roots = set(roots) if roots else set(range(len(self.nodes)))
return _isacyclic(roots, set())
# TODO: test
def copy(self):
"""Returns a copy of this network."""
newedges = EdgeSet(len(self.nodes))
newedges.adjacency_lists = deepcopy(self.edges.adjacency_lists)
return Network(self.nodes, newedges)
def layout(self, width=400, height=400, dotpath="dot"):
"""Determines network layout using Graphviz's dot algorithm.
width and height are in pixels.
dotpath is the path to the dot application.
The resulting node positions are saved in network.node_positions.
"""
tempdir = tempfile.mkdtemp(prefix="pebl")
dot1 = os.path.join(tempdir, "1.dot")
dot2 = os.path.join(tempdir, "2.dot")
self.as_dotfile(dot1)
try:
os.system("%s -Tdot -Gratio=fill -Gdpi=60 -Gfill=10,10 %s > %s" % (dotpath, dot1, dot2))
except:
raise Exception("Cannot find the dot program at %s." % dotpath)
dotgraph = pydot.graph_from_dot_file(dot2)
nodes = (n for n in dotgraph.get_node_list() if n.get_pos())
self.node_positions = [[int(float(i)) for i in n.get_pos()[1:-1].split(',')] for n in nodes]
def as_string(self):
"""Returns the sparse string representation of network.
If network has edges (2,3) and (1,2), the sparse string representation
is "2,3;1,2".
"""
return ";".join([",".join([str(n) for n in edge]) for edge in list(self.edges)])
def as_dotstring(self):
"""Returns network as a dot-formatted string"""
def node(n, position):
s = "\t\"%s\"" % n.name
if position:
x,y = position
s += " [pos=\"%d,%d\"]" % (x,y)
return s + ";"
nodes = self.nodes
positions = self.node_positions if hasattr(self, 'node_positions') \
else [None for n in nodes]
return "\n".join(
["digraph G {"] +
[node(n, pos) for n,pos in zip(nodes, positions)] +
["\t\"%s\" -> \"%s\";" % (nodes[src].name, nodes[dest].name)
for src,dest in self.edges] +
["}"]
)
def as_dotfile(self, filename):
"""Saves network as a dot file."""
f = file(filename, 'w')
f.write(self.as_dotstring())
f.close()
def as_pydot(self):
"""Returns a pydot instance for the network."""
return pydot.graph_from_dot_data(self.as_dotstring())
def as_image(self, filename, decorator=lambda x: x, prog='dot'):
"""Creates an image (PNG format) for the newtork.
decorator is a function that accepts a pydot graph, modifies it and
returns it. decorators can be used to set visual appearance of
networks (color, style, etc).
prog is the Graphviz program to use (default: dot).
"""
g = self.as_pydot()
g = decorator(g)
g.write_png(filename, prog=prog)
#
# Factory functions
#
def fromdata(data_):
"""Creates a network from the variables in the dataset."""
return Network(data_.variables)
def random_network(nodes, required_edges=[], prohibited_edges=[]):
"""Creates a random network with the given set of nodes.
Can specify required_edges and prohibited_edges to control the resulting
random network.
"""
def _randomize(net, density=None):
n_nodes = len(net.nodes)
density = density or 1.0/n_nodes
max_attempts = 50
for attempt in xrange(max_attempts):
# create an random adjacency matrix with given density
adjmat = N.random.rand(n_nodes, n_nodes)
adjmat[adjmat >= (1.0-density)] = 1
adjmat[adjmat < 1] = 0
# add required edges
for src,dest in required_edges:
adjmat[src][dest] = 1
# remove prohibited edges
for src,dest in prohibited_edges:
adjmat[src][dest] = 0
# remove self-loop edges (those along the diagonal)
adjmat = N.invert(N.identity(n_nodes).astype(bool))*adjmat
# set the adjaceny matrix and check for acyclicity
net.edges.adjacency_matrix = adjmat.astype(bool)
if net.is_acyclic():
return net
# got here without finding a single acyclic network.
# so try with a less dense network
return _randomize(density/2)
# -----------------------
net = Network(nodes)
_randomize(net)
return net
|