__init__.py 18.4 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
#! /usr/bin/env python
2
# -*- coding: utf-8 -*-
Tiago Peixoto's avatar
Tiago Peixoto committed
3
#
4
5
# graph_tool -- a general graph manipulation python module
#
Tiago Peixoto's avatar
Tiago Peixoto committed
6
# Copyright (C) 2007-2011 Tiago de Paula Peixoto <tiago@skewed.de>
Tiago Peixoto's avatar
Tiago Peixoto committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#
# This program 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 3 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, see <http://www.gnu.org/licenses/>.

21
"""
22
23
``graph_tool.draw`` - Graph drawing and layout
----------------------------------------------
24
25
26
27

Summary
+++++++

28
29
30
Layout algorithms
=================

31
32
33
.. autosummary::
   :nosignatures:

Tiago Peixoto's avatar
Tiago Peixoto committed
34
   sfdp_layout
35
   fruchterman_reingold_layout
36
37
   arf_layout
   random_layout
38
39
40
41
42
43
44
45
46


Graph drawing
=============

.. autosummary::
   :nosignatures:

   graph_draw
Tiago Peixoto's avatar
Tiago Peixoto committed
47
   graphviz_draw
48

49
50
51
52
53
54
55
56
57
58
59
60

Low-level graph drawing
^^^^^^^^^^^^^^^^^^^^^^^

.. autosummary::
   :nosignatures:

   cairo_draw
   interactive_window
   GraphWidget
   GraphWindow

61
62
Contents
++++++++
63
64
"""

Tiago Peixoto's avatar
Tiago Peixoto committed
65
66
67
68
69
70
71
72
from .. import GraphView, _check_prop_vector, group_vector_property, \
     ungroup_vector_property, infect_vertex_property, _prop
from .. topology import max_cardinality_matching, max_independent_vertex_set, \
    label_components,  pseudo_diameter
from .. community import condensation_graph
from .. stats import label_parallel_edges
import numpy.random
from numpy import sqrt
73
74
import sys
import warnings
75
76
77

from .. dl_import import dl_import
dl_import("import libgraph_tool_layout")
78

79

Tiago Peixoto's avatar
Tiago Peixoto committed
80
81
82
83
__all__ = ["graph_draw", "graphviz_draw", "fruchterman_reingold_layout",
           "arf_layout", "sfdp_layout", "random_layout",
           "interactive_window", "cairo_draw", "GraphWidget",
           "GraphWindow"]
84

Tiago Peixoto's avatar
Tiago Peixoto committed
85

86
def random_layout(g, shape=None, pos=None, dim=2):
87
88
89
90
    r"""Performs a random layout of the graph.

    Parameters
    ----------
91
    g : :class:`~graph_tool.Graph`
92
        Graph to be used.
93
    shape : tuple or list (optional, default: ``None``)
Tiago Peixoto's avatar
Tiago Peixoto committed
94
95
96
97
        Rectangular shape of the bounding area. The size of this parameter must
        match `dim`, and each element can be either a pair specifying a range,
        or a single value specifying a range starting from zero. If None is
        passed, a square of linear size :math:`\sqrt{N}` is used.
98
    pos : :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
99
        Vector vertex property maps where the coordinates should be stored.
100
    dim : int (optional, default: ``2``)
101
102
103
104
        Number of coordinates per vertex.

    Returns
    -------
105
106
107
    pos : :class:`~graph_tool.PropertyMap`
        A vector-valued vertex property map with the coordinates of the
        vertices.
108
109
110
111

    Notes
    -----
    This algorithm has complexity :math:`O(V)`.
Tiago Peixoto's avatar
Tiago Peixoto committed
112
113
114
115
116
117
118
119
120
121
122

    Examples
    --------
    >>> from numpy.random import seed
    >>> seed(42)
    >>> g = gt.random_graph(100, lambda: (3, 3))
    >>> shape = [[50, 100], [1, 2], 4]
    >>> pos = gt.random_layout(g, shape=shape, dim=3)
    >>> pos[g.vertex(0)].a
    array([ 86.59969709,   1.31435598,   0.64651486])

123
124
    """

125
    if pos == None:
Tiago Peixoto's avatar
Tiago Peixoto committed
126
127
        pos = g.new_vertex_property("vector<double>")
    _check_prop_vector(pos, name="pos")
128

Tiago Peixoto's avatar
Tiago Peixoto committed
129
    pos = ungroup_vector_property(pos, range(0, dim))
130
131

    if shape == None:
Tiago Peixoto's avatar
Tiago Peixoto committed
132
        shape = [sqrt(g.num_vertices())] * dim
133
134

    for i in xrange(dim):
Tiago Peixoto's avatar
Tiago Peixoto committed
135
136
137
138
139
140
141
        if hasattr(shape[i], "__len__"):
            if len(shape[i]) != 2:
                raise ValueError("The elements of 'shape' must have size 2.")
            r = [min(shape[i]), max(shape[i])]
        else:
            r = [min(shape[i], 0), max(shape[i], 0)]
        d = r[1] - r[0]
142
143
144
145

        # deal with filtering
        p = pos[i].ma
        p[:] = numpy.random.random(len(p)) * d + r[0]
146

Tiago Peixoto's avatar
Tiago Peixoto committed
147
    pos = group_vector_property(pos)
148
149
    return pos

Tiago Peixoto's avatar
Tiago Peixoto committed
150

151
152
153
154
155
156
157
def fruchterman_reingold_layout(g, weight=None, a=None, r=1., scale=None,
                                circular=False, grid=True, t_range=None,
                                n_iter=100, pos=None):
    r"""Calculate the Fruchterman-Reingold spring-block layout of the graph.

    Parameters
    ----------
158
    g : :class:`~graph_tool.Graph`
159
        Graph to be used.
160
    weight : :class:`PropertyMap` (optional, default: ``None``)
161
162
163
164
165
166
167
        An edge property map with the respective weights.
    a : float (optional, default: :math:`V`)
        Attracting force between adjacent vertices.
    r : float (optional, default: 1.0)
        Repulsive force between vertices.
    scale : float (optional, default: :math:`\sqrt{V}`)
        Total scale of the layout (either square side or radius).
168
169
    circular : bool (optional, default: ``False``)
        If ``True``, the layout will have a circular shape. Otherwise the shape
170
        will be a square.
171
172
    grid : bool (optional, default: ``True``)
        If ``True``, the repulsive forces will only act on vertices which are on
173
        the same site on a grid. Otherwise they will act on all vertex pairs.
174
    t_range : tuple of floats (optional, default: ``(scale / 10, scale / 1000)``)
175
176
        Temperature range used in annealing. The temperature limits the
        displacement at each iteration.
177
    n_iter : int (optional, default: ``100``)
178
        Total number of iterations.
179
    pos : :class:`PropertyMap` (optional, default: ``None``)
180
181
182
183
184
185
        Vector vertex property maps where the coordinates should be stored. If
        provided, this will also be used as the initial position of the
        vertices.

    Returns
    -------
186
187
188
    pos : :class:`~graph_tool.PropertyMap`
        A vector-valued vertex property map with the coordinates of the
        vertices.
189
190
191
192

    Notes
    -----
    This algorithm is defined in [fruchterman-reingold]_, and has
Tiago Peixoto's avatar
Tiago Peixoto committed
193
194
    complexity :math:`O(\text{n-iter}\times V^2)` if `grid=False` or
    :math:`O(\text{n-iter}\times (V + E))` otherwise.
195
196
197
198
199
200
201

    Examples
    --------
    >>> from numpy.random import seed, zipf
    >>> seed(42)
    >>> g = gt.price_network(300)
    >>> pos = gt.fruchterman_reingold_layout(g, n_iter=1000)
202
    >>> gt.graph_draw(g, pos=pos, output="graph-draw-fr.pdf")
203
204
    <...>

205
    .. figure:: graph-draw-fr.*
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
        :align: center

        Fruchterman-Reingold layout of a Price network.

    References
    ----------
    .. [fruchterman-reingold] Fruchterman, Thomas M. J.; Reingold, Edward M.
       "Graph Drawing by Force-Directed Placement". Software – Practice & Experience
       (Wiley) 21 (11): 1129–1164. (1991) :doi:`10.1002/spe.4380211102`
    """

    if pos == None:
        pos = random_layout(g, dim=2)
    _check_prop_vector(pos, name="pos", floating=True)

    if a is None:
        a = float(g.num_vertices())

    if scale is None:
        scale = sqrt(g.num_vertices())

    if t_range is None:
        t_range = (scale / 10, scale / 1000)

    ug = GraphView(g, directed=False)
    libgraph_tool_layout.fruchterman_reingold_layout(ug._Graph__graph,
                                                     _prop("v", g, pos),
                                                     _prop("e", g, weight),
                                                     a, r, not circular, scale,
                                                     grid, t_range[0],
                                                     t_range[1], n_iter)
    return pos


def arf_layout(g, weight=None, d=0.5, a=10, dt=0.001, epsilon=1e-6,
241
               max_iter=1000, pos=None, dim=2):
242
243
    r"""Calculate the ARF spring-block layout of the graph.

Tiago Peixoto's avatar
Tiago Peixoto committed
244
245
246
247
    Parameters
    ----------
    g : :class:`~graph_tool.Graph`
        Graph to be used.
Tiago Peixoto's avatar
Tiago Peixoto committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
    weight : :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
        An edge property map with the respective weights.
    d : float (optional, default: ``0.5``)
        Opposing force between vertices.
    a : float (optional, default: ``10``)
        Attracting force between adjacent vertices.
    dt : float (optional, default: ``0.001``)
        Iteration step size.
    epsilon : float (optional, default: ``1e-6``)
        Convergence criterion.
    max_iter : int (optional, default: ``1000``)
        Maximum number of iterations. If this value is ``0``, it runs until
        convergence.
    pos : :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
        Vector vertex property maps where the coordinates should be stored.
    dim : int (optional, default: ``2``)
        Number of coordinates per vertex.
Tiago Peixoto's avatar
Tiago Peixoto committed
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282

    Returns
    -------
    pos : :class:`~graph_tool.PropertyMap`
        A vector-valued vertex property map with the coordinates of the
        vertices.

    Notes
    -----
    This algorithm is defined in [geipel-self-organization-2007]_, and has
    complexity :math:`O(V^2)`.

    Examples
    --------
    >>> from numpy.random import seed, zipf
    >>> seed(42)
    >>> g = gt.price_network(300)
    >>> pos = gt.arf_layout(g, max_iter=0)
283
    >>> gt.graph_draw(g, pos=pos, output="graph-draw-arf.pdf")
Tiago Peixoto's avatar
Tiago Peixoto committed
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
    <...>

    .. figure:: graph-draw-arf.*
        :align: center

        ARF layout of a Price network.

    References
    ----------
    .. [geipel-self-organization-2007] Markus M. Geipel, "Self-Organization
       applied to Dynamic Network Layout", International Journal of Modern
       Physics C vol. 18, no. 10 (2007), pp. 1537-1549,
       :doi:`10.1142/S0129183107011558`, :arxiv:`0704.1748v5`
    .. _arf: http://www.sg.ethz.ch/research/graphlayout
    """

    if pos is None:
        if dim != 2:
            pos = random_layout(g, dim=dim)
        else:
            pos = graph_draw(g, output=None)
    _check_prop_vector(pos, name="pos", floating=True)

    ug = GraphView(g, directed=False)
    libgraph_tool_layout.arf_layout(ug._Graph__graph, _prop("v", g, pos),
                                    _prop("e", g, weight), d, a, dt, max_iter,
                                    epsilon, dim)
    return pos


Tiago Peixoto's avatar
Tiago Peixoto committed
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
def _coarse_graph(g, vweight, eweight, mivs=False):
    if mivs:
        mivs = max_independent_vertex_set(g, high_deg=True)
        u = GraphView(g, vfilt=mivs, directed=False)
        c = label_components(u)[0]
        c.fa += 1
        u = GraphView(g, directed=False)
        infect_vertex_property(u, c,
                               range(1, c.fa.max() + 1))
        c = g.own_property(c)
    else:
        mivs = None
        m = max_cardinality_matching(GraphView(g, directed=False),
                                     heuristic=True, weight=eweight,
                                     minimize=False)
        u = GraphView(g, efilt=m, directed=False)
        c = label_components(u)[0]
        c = g.own_property(c)
        u = GraphView(g, directed=False)
    cg, cc, vcount, ecount = condensation_graph(u, c, vweight, eweight)
    return cg, cc, vcount, ecount, c, mivs


def _propagate_pos(g, cg, c, cc, cpos, delta, mivs):
    seed = numpy.random.randint(sys.maxint)
    pos = g.new_vertex_property(cpos.value_type())

    if mivs is not None:
        g = GraphView(g, vfilt=mivs)
    libgraph_tool_layout.propagate_pos(g._Graph__graph,
                                       cg._Graph__graph,
                                       _prop("v", g, c),
                                       _prop("v", cg, cc),
                                       _prop("v", g, pos),
                                       _prop("v", cg, cpos),
                                       delta if mivs is None else 0,
                                       seed)
    if mivs is not None:
        g = g.base
        u = GraphView(g, directed=False)
        try:
            libgraph_tool_layout.propagate_pos_mivs(u._Graph__graph,
                                                    _prop("v", u, mivs),
                                                    _prop("v", u, pos),
                                                    delta, seed)
        except ValueError:
            graph_draw(u, mivs, vertex_fillcolor=mivs)
    return pos


def _avg_edge_distance(g, pos):
    return libgraph_tool_layout.avg_dist(g._Graph__graph, _prop("v", g, pos))


def coarse_graphs(g, method="hybrid", mivs_thres=0.9, ec_thres=0.75,
                  weighted_coarse=False, verbose=False):
    cg = [[g, None, None, None, None, None]]
    mivs = not (method in ["hybrid", "ec"])
    while True:
        u = _coarse_graph(cg[-1][0], cg[-1][2], cg[-1][3], mivs)
        if (mivs and
            u[0].num_vertices() > mivs_thres * cg[-1][0].num_vertices()):
            break
        if u[0].num_vertices() > ec_thres * cg[-1][0].num_vertices():
            if method == "hybrid":
                mivs = True
            else:
                break
        if u[0].num_vertices() <= 2:
            break
        cg.append(u)
        if verbose:
            print "Coarse level (%s):" % ("MIVS" if mivs else "EC"),
            print len(cg), " num vertices:",
            print u[0].num_vertices()
    cg.reverse()
    Ks = []
    pos = random_layout(cg[0][0], dim=2)
    for i in xrange(len(cg)):
        if i == 0:
            u = cg[i][0]
            K = _avg_edge_distance(u, pos)
            Ks.append(K)
            continue
        if weighted_coarse:
            gamma = 1.
        else:
            #u = cg[i - 1][0]
            #w = cg[i][0]
            #du = pseudo_diameter(u)[0]
            #dw = pseudo_diameter(w)[0]
            #gamma = du / float(max(dw, du))
            gamma = 0.75
        Ks.append(Ks[-1] * gamma)

    for i in xrange(len(cg)):
        u, cc, vcount, ecount, c, mivs = cg[i]
        yield u, pos, Ks[i], vcount, ecount

        if verbose:
            print "avg edge distance:", _avg_edge_distance(u, pos)

        if i < len(cg) - 1:
            if verbose:
                print "propagating...",
                print mivs.a.sum() if mivs is not None else ""
            pos = _propagate_pos(cg[i + 1][0], u, c, cc, pos,
                                 Ks[i] / 1000, mivs)


def sfdp_layout(g, vweight=None, eweight=None, pin=None, C=0.2, K=None, p=2.,
                theta=0.6, init_step=None, cooling_step=0.9,
                adaptive_cooling=True, max_level=11, epsilon=1e-1, max_iter=0,
                pos=None, multilevel=None, coarse_method="hybrid",
                mivs_thres=0.9, ec_thres=0.75,
                weighted_coarse=False, verbose=False):
Tiago Peixoto's avatar
Tiago Peixoto committed
430
431
    r"""Calculate the sfdp spring-block layout of the graph.

432
433
    Parameters
    ----------
434
    g : :class:`~graph_tool.Graph`
435
        Graph to be used.
436
    weight : :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
437
        An edge property map with the respective weights.
438
    epsilon : float (optional, default: ``1e-6``)
439
        Convergence criterion.
440
441
    max_iter : int (optional, default: ``1000``)
        Maximum number of iterations. If this value is ``0``, it runs until
442
        convergence.
443
    pos : :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
444
445
446
447
        Vector vertex property maps where the coordinates should be stored.

    Returns
    -------
448
449
450
    pos : :class:`~graph_tool.PropertyMap`
        A vector-valued vertex property map with the coordinates of the
        vertices.
451
452
453

    Notes
    -----
454
    This algorithm is defined in [geipel-self-organization-2007]_, and has
455
456
457
458
459
460
    complexity :math:`O(V^2)`.

    Examples
    --------
    >>> from numpy.random import seed, zipf
    >>> seed(42)
461
462
    >>> g = gt.price_network(300)
    >>> pos = gt.arf_layout(g, max_iter=0)
463
    >>> gt.graph_draw(g, pos=pos, pin=True, output="graph-draw-arf.pdf")
464
465
    <...>

466
    .. figure:: graph-draw-arf.*
467
468
        :align: center

469
        ARF layout of a Price network.
470
471
472

    References
    ----------
473
    .. [geipel-self-organization-2007] Markus M. Geipel, "Self-Organization
474
475
476
       applied to Dynamic Network Layout", International Journal of Modern
       Physics C vol. 18, no. 10 (2007), pp. 1537-1549,
       :doi:`10.1142/S0129183107011558`, :arxiv:`0704.1748v5`
477
478
479
    .. _arf: http://www.sg.ethz.ch/research/graphlayout
    """

480
    if pos is None:
Tiago Peixoto's avatar
Tiago Peixoto committed
481
        pos = random_layout(g, dim=2)
482
483
    _check_prop_vector(pos, name="pos", floating=True)

Tiago Peixoto's avatar
Tiago Peixoto committed
484
485
    g = GraphView(g, directed=False)

Tiago Peixoto's avatar
Tiago Peixoto committed
486
487
488
489
    if pin is not None and pin.value_type() != "bool":
        raise ValueError("'pin' property must be of type 'bool'.")

    if K is None:
Tiago Peixoto's avatar
Tiago Peixoto committed
490
        K = _avg_edge_distance(g, pos)
Tiago Peixoto's avatar
Tiago Peixoto committed
491
492

    if init_step is None:
Tiago Peixoto's avatar
Tiago Peixoto committed
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
        init_step = 10 * max(_avg_edge_distance(g, pos), K)

    if multilevel is None:
        multilevel = g.num_vertices() > 1000

    if multilevel:
        cgs = coarse_graphs(g, method=coarse_method,
                            mivs_thres=mivs_thres,
                            ec_thres=ec_thres,
                            weighted_coarse=weighted_coarse,
                            verbose=verbose)
        count = 0
        for u, pos, K, vcount, ecount in cgs:
            if verbose:
                print "Positioning level:", count, u.num_vertices(),
                print "with K =", K, "..."
                count += 1
            #graph_draw(u, pos)
            pos = sfdp_layout(u, pos=pos,
                              vweight=vcount if weighted_coarse else None,
                              eweight=ecount if weighted_coarse else None,
                              C=C, K=K, p=p,
                              theta=theta, epsilon=epsilon,
                              max_iter=max_iter,
                              cooling_step=cooling_step,
                              adaptive_cooling=False,
                              init_step=max(2 * K,
                                            _avg_edge_distance(u, pos) / 10),
                              multilevel=False,
                              verbose=False)
            #graph_draw(u, pos)
        return pos

    if g.num_vertices() <= 1:
        return pos
    if g.num_vertices() == 2:
        vs = [g.vertex(0, False), g.vertex(1, False)]
        pos[vs[0]] = [0, 0]
        pos[vs[1]] = [1, 1]
        return pos
    if g.num_vertices() <= 50:
        max_level = 0
    libgraph_tool_layout.sfdp_layout(g._Graph__graph, _prop("v", g, pos),
                                     _prop("v", g, vweight),
                                     _prop("e", g, eweight),
                                     _prop("v", g, pin), (C, K, p), theta,
                                     init_step, cooling_step, max_level,
                                     epsilon, max_iter, not adaptive_cooling,
                                     verbose)
542
    return pos
Tiago Peixoto's avatar
Tiago Peixoto committed
543

Tiago Peixoto's avatar
Tiago Peixoto committed
544
545
from cairo_draw import graph_draw, GraphWidget, GraphWindow, \
     interactive_window, cairo_draw
Tiago Peixoto's avatar
Tiago Peixoto committed
546

Tiago Peixoto's avatar
Tiago Peixoto committed
547
from graphviz_draw import graphviz_draw