__init__.py 67.9 KB
Newer Older
1
#! /usr/bin/env python
2
# -*- coding: utf-8 -*-
3
#
4
5
# graph_tool -- a general graph manipulation python module
#
Tiago Peixoto's avatar
Tiago Peixoto committed
6
# Copyright (C) 2007-2012 Tiago de Paula Peixoto <tiago@skewed.de>
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
"""
Tiago Peixoto's avatar
Tiago Peixoto committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
graph_tool - efficient graph analysis and manipulation
======================================================

Summary
-------

.. autosummary::
   :nosignatures:

   Graph
   GraphView
   Vertex
   Edge
   PropertyMap
   PropertyArray
   load_graph
   group_vector_property
   ungroup_vector_property
   value_types
   show_config

Tiago Peixoto's avatar
Tiago Peixoto committed
43
44

This module provides:
45

46
   1. A :class:`~graph_tool.Graph` class for graph representation and manipulation
47
48
   2. Property maps for Vertex, Edge or Graph.
   3. Fast algorithms implemented in C++.
49

50
51
How to use the documentation
----------------------------
52

53
54
Documentation is available in two forms: docstrings provided
with the code, and the full documentation available in
Tiago Peixoto's avatar
Tiago Peixoto committed
55
`the graph-tool homepage <http://graph-tool.skewed.de>`_.
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

We recommend exploring the docstrings using `IPython
<http://ipython.scipy.org>`_, an advanced Python shell with TAB-completion and
introspection capabilities.

The docstring examples assume that ``graph_tool.all`` has been imported as
``gt``::

   >>> import graph_tool.all as gt

Code snippets are indicated by three greater-than signs::

   >>> x = x + 1

Use the built-in ``help`` function to view a function's docstring::

   >>> help(gt.Graph)

Tiago Peixoto's avatar
Tiago Peixoto committed
74
75
Contents
--------
76
"""
77

Tiago Peixoto's avatar
Tiago Peixoto committed
78
__author__ = "Tiago de Paula Peixoto <tiago@skewed.de>"
Tiago Peixoto's avatar
Tiago Peixoto committed
79
__copyright__ = "Copyright 2007-2012 Tiago de Paula Peixoto"
Tiago Peixoto's avatar
Tiago Peixoto committed
80
__license__ = "GPL version 3 or above"
Tiago Peixoto's avatar
Tiago Peixoto committed
81
__URL__ = "http://graph-tool.skewed.de"
82

83
84
85
86
# import numpy and scipy before everything to avoid weird segmentation faults
# depending on the order things are imported.

import numpy
87
import numpy.ma
88
89
90
import scipy
import scipy.stats

Tiago Peixoto's avatar
Tiago Peixoto committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from dl_import import *
dl_import("import libgraph_tool_core as libcore")
import libgraph_tool_core as libcore   # for pylint
__version__ = libcore.mod_info().version

import io  # sets up libcore io routines

import sys
import os
import re
import gzip
import bz2
import weakref
import copy

from StringIO import StringIO
from decorators import _wraps, _require, _attrs, _limit_args
from inspect import ismethod
Tiago Peixoto's avatar
Tiago Peixoto committed
109
110
111
112
113

__all__ = ["Graph", "GraphView", "Vertex", "Edge", "Vector_bool",
           "Vector_int32_t", "Vector_int64_t", "Vector_double",
           "Vector_long_double", "Vector_string", "value_types", "load_graph",
           "PropertyMap", "group_vector_property", "ungroup_vector_property",
114
           "infect_vertex_property", "edge_difference", "show_config",
Tiago Peixoto's avatar
Tiago Peixoto committed
115
116
           "PropertyArray", "__author__", "__copyright__", "__URL__",
           "__version__"]
Tiago Peixoto's avatar
Tiago Peixoto committed
117

Tiago Peixoto's avatar
Tiago Peixoto committed
118
119
# this is rather pointless, but it works around a sphinx bug
graph_tool = sys.modules[__name__]
Tiago Peixoto's avatar
Tiago Peixoto committed
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

################################################################################
# Utility functions
################################################################################


def _prop(t, g, prop):
    """Return either a property map, or an internal property map with a given
    name."""
    if type(prop) == str:
        try:
            pmap = g.properties[(t, prop)]
        except KeyError:
            raise KeyError("no internal %s property named: %s" %\
                           ("vertex" if t == "v" else \
                            ("edge" if t == "e" else "graph"), prop))
    else:
        pmap = prop
    if pmap == None:
        return libcore.any()
    else:
        if t != prop.key_type():
            names = {'e': 'edge', 'v': 'vertex', 'g': 'graph'}
            raise ValueError("Expected '%s' property map, got '%s'" %
                             (names[t], names[prop.key_type()]))
        return pmap._PropertyMap__map.get_map()


def _degree(g, name):
    """Retrieve the degree type from string, or returns the corresponding
    property map."""
    deg = name
    if name == "in-degree" or name == "in":
        deg = libcore.Degree.In
    elif name == "out-degree" or name == "out":
        deg = libcore.Degree.Out
    elif name == "total-degree" or name == "total":
        deg = libcore.Degree.Total
    else:
        deg = _prop("v", g, deg)
    return deg


def _type_alias(type_name):
    alias = {"int8_t": "bool",
             "boolean": "bool",
             "int": "int32_t",
             "long": "int64_t",
             "long long": "int64_t",
             "object": "python::object",
             "float": "double"}
    if type_name in value_types():
        return type_name
    if type_name in alias:
        return alias[type_name]
    ma = re.compile(r"vector<(.*)>").match(type_name)
    if ma:
        t = ma.group(1)
        if t in alias:
            return "vector<%s>" % alias[t]
    raise ValueError("invalid property value type: " + type_name)


def _python_type(type_name):
    type_name = _type_alias(type_name)
    if "vector" in type_name:
        ma = re.compile(r"vector<(.*)>").match(type_name)
        t = ma.group(1)
        return list, _python_type(t)
    if "int" in type_name:
        return int
    if type_name == "bool":
        return bool
    if "double" in type_name:
        return float
    if "string" in type_name:
        return str
    return object


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
def _gt_type(obj):
    t = type(obj)
    if t is numpy.longlong or t is numpy.uint64:
        return "long long"
    if t is int or issubclass(t, numpy.int):
        return "int"
    if t is numpy.float128:
        return "long double"
    if t is float or issubclass(t, numpy.float):
        return "double"
    if t is str:
        return "string"
    if t is bool:
        return "bool"
    if issubclass(t, list) or issubclass(t, numpy.ndarray):
        return "vector<%s>" % _gt_type(obj[0])
    return "object"


219
220
221
222
223
224
225
226
227
def _convert(prop, val):
    # attempt to convert to a compatible python type. This is useful,
    # for instance, when dealing with numpy types.
    vtype = _python_type(prop.value_type())
    if type(vtype) is tuple:
        return [vtype[1](x) for x in val]
    return vtype(val)


Tiago Peixoto's avatar
Tiago Peixoto committed
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
def show_config():
    """Show ``graph_tool`` build configuration."""
    info = libcore.mod_info()
    print "version:", info.version
    print "gcc version:", info.gcc_version
    print "compilation flags:", info.cxxflags
    print "install prefix:", info.install_prefix
    print "python dir:", info.python_dir
    print "graph filtering:", libcore.graph_filtering_enabled()
    print "openmp:", libcore.openmp_enabled()
    print "uname:", " ".join(os.uname())

################################################################################
# Property Maps
################################################################################


class PropertyArray(numpy.ndarray):
    """This is a :class:`~numpy.ndarray` subclass which keeps a reference of its :class:`~graph_tool.PropertyMap` owner, and detects if the underlying data has been invalidated."""

    __array_priority__ = -10

    def _get_pmap(self):
        return self._prop_map

    def _set_pmap(self, value):
        self._prop_map = value

    prop_map = property(_get_pmap, _set_pmap,
                        doc=":class:`~graph_tool.PropertyMap` owner instance.")

    def __new__(cls, input_array, prop_map):
        obj = numpy.asarray(input_array).view(cls)
        obj.prop_map = prop_map

        # check if data really belongs to property map
        if (prop_map._get_data().__array_interface__['data'][0] !=
            obj._get_base_data()):
            obj.prop_map = None
267
            # do a copy
Tiago Peixoto's avatar
Tiago Peixoto committed
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
            obj = numpy.asarray(obj)

        return obj

    def _get_base(self):
        base = self
        while base.base is not None:
            base = base.base
        return base

    def _get_base_data(self):
        return self._get_base().__array_interface__['data'][0]

    def _check_data(self):
        if self.prop_map is None:
            return

        data = self.prop_map._get_data()

        if (data is None or
            data.__array_interface__['data'][0] != self._get_base_data()):
            raise ValueError(("The graph correspondig to the underlying" +
                              " property map %s has changed. The" +
                              " PropertyArray at 0x%x is no longer valid!") %
                             (repr(self.prop_map), id(self)))

    def __array_finalize__(self, obj):
        if type(obj) is PropertyArray:
            obj._check_data()

        if obj is not None:
            # inherit prop_map only if the data is the same
            if (type(obj) is PropertyArray and
                self._get_base_data() == obj._get_base_data()):
                self.prop_map = getattr(obj, 'prop_map', None)
            else:
                self.prop_map = None
        self._check_data()

    def __array_prepare__(self, out_arr, context=None):
        self._check_data()
        return numpy.ndarray.__array_prepare__(self, out_arr, context)

    def __array_wrap__(self, out_arr, context=None):
        #demote to ndarray
        obj = numpy.ndarray.__array_wrap__(self, out_arr, context)
        return numpy.asarray(obj)

    # Overload members and operators to add data checking

    def _wrap_method(method):
        method = getattr(numpy.ndarray, method)

        def checked_method(self, *args, **kwargs):
            self._check_data()
            return method(self, *args, **kwargs)

        if ismethod(method):
            checked_method = _wraps(method)(checked_method)
        checked_method.__doc__ = getattr(method, "__doc__", None)
        return checked_method

    for method in ['all', 'any', 'argmax', 'argmin', 'argsort', 'astype',
                   'byteswap', 'choose', 'clip', 'compress', 'conj',
                   'conjugate', 'copy', 'cumprod', 'cumsum', 'diagonal', 'dot',
                   'dump', 'dumps', 'fill', 'flat', 'flatten', 'getfield',
                   'imag', 'item', 'itemset', 'itemsize', 'max', 'mean', 'min',
                   'newbyteorder', 'nonzero', 'prod', 'ptp', 'put', 'ravel',
                   'real', 'repeat', 'reshape', 'resize', 'round',
                   'searchsorted', 'setfield', 'setflags', 'sort', 'squeeze',
                   'std', 'sum', 'swapaxes', 'take', 'tofile', 'tolist',
                   'tostring', 'trace', 'transpose', 'var', 'view',
                   '__getitem__']:
        if hasattr(numpy.ndarray, method):
            locals()[method] = _wrap_method(method)


class PropertyMap(object):
    """This class provides a mapping from vertices, edges or whole graphs to arbitrary properties.

348
349
350
    See :ref:`sec_property_maps` for more details.

    The possible property value types are listed below.
Tiago Peixoto's avatar
Tiago Peixoto committed
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374

    .. table::

        =======================     ======================
         Type name                  Alias
        =======================     ======================
        ``bool``                    ``uint8_t``
        ``int32_t``                 ``int``
        ``int64_t``                 ``long``, ``long long``
        ``double``                  ``float``
        ``long double``
        ``string``
        ``vector<bool>``            ``vector<uint8_t>``
        ``vector<int32_t>``         ``vector<int>``
        ``vector<int64_t>``         ``vector<long>``, ``vector<long long>``
        ``vector<double>``          ``vector<float>``
        ``vector<long double>``
        ``vector<string>``
        ``python::object``          ``object``
        =======================     ======================
    """
    def __init__(self, pmap, g, key_type, key_trans=None):
        self.__map = pmap
        self.__g = weakref.ref(g)
375
376
377
378
379
380
381
382
        self.__base_g = lambda: None
        try:
            if isinstance(g, GraphView):
                self.__base_g = weakref.ref(g.base)  # keep reference to the
                                                     # base graph, in case the
                                                     # graph view is deleted.
        except NameError:
            pass  # ignore if GraphView is yet undefined
Tiago Peixoto's avatar
Tiago Peixoto committed
383
384
385
386
387
        self.__key_type = key_type
        self.__key_trans = key_trans if key_trans is not None else lambda k: k
        self.__register_map()

    def __register_map(self):
388
389
390
391
        for g in [self.__g(), self.__base_g()]:
            if g is not None:
                g._Graph__known_properties.append((self.key_type(),
                                                   weakref.ref(self.__map)))
Tiago Peixoto's avatar
Tiago Peixoto committed
392
393

    def __unregister_map(self):
394
395
396
397
398
        for g in [self.__g(), self.__base_g()]:
            if g is not None:
                i = g._Graph__known_properties.index((self.key_type(),
                                                      weakref.ref(self.__map)))
                del g._Graph__known_properties[i]
Tiago Peixoto's avatar
Tiago Peixoto committed
399
400
401
402
403
404
405
406
407
408
409
410

    def __del__(self):
        self.__unregister_map()

    def __getitem__(self, k):
        return self.__map[self.__key_trans(k)]

    def __setitem__(self, k, v):
        key = self.__key_trans(k)
        try:
            self.__map[key] = v
        except TypeError:
411
            self.__map[key] = _convert(self, v)
Tiago Peixoto's avatar
Tiago Peixoto committed
412
413
414
415
416
417
418
419
420

    def __repr__(self):
        # provide some more useful information
        if self.key_type() == "e":
            k = "Edge"
        elif self.key_type() == "v":
            k = "Vertex"
        else:
            k = "Graph"
421
        g = self.get_graph()
Tiago Peixoto's avatar
Tiago Peixoto committed
422
423
424
425
426
427
428
        if g == None:
            g = "a non-existent graph"
        else:
            g = "Graph 0x%x" % id(g)
        return ("<PropertyMap object with key type '%s' and value type '%s',"
                + " for %s, at 0x%x>") % (k, self.value_type(), g, id(self))

429
430
431
432
    def copy(self, value_type=None):
        """Return a copy of the property map. If ``value_type`` is specified,
        the value type is converted to the chosen type."""
        return self.get_graph().copy_property(self, value_type=value_type)
433

Tiago Peixoto's avatar
Tiago Peixoto committed
434
435
    def get_graph(self):
        """Get the graph class to which the map refers."""
436
437
438
439
        g = self.__g()
        if g is None:
            g = self.__base_g()
        return g
Tiago Peixoto's avatar
Tiago Peixoto committed
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455

    def key_type(self):
        """Return the key type of the map. Either 'g', 'v' or 'e'."""
        return self.__key_type

    def value_type(self):
        """Return the value type of the map."""
        return self.__map.value_type()

    def python_value_type(self):
        """Return the python-compatible value type of the map."""
        return _python_type(self.__map.value_type())

    def get_array(self):
        """Get a :class:`~graph_tool.PropertyArray` with the property values.

456
457
458
459
460
461
462
        .. note::

           An array is returned *only if* the value type of the property map is
           a scalar. For vector, string or object types, ``None`` is returned
           instead.

        .. warning::
Tiago Peixoto's avatar
Tiago Peixoto committed
463
464
465
466
467
468
469
470
471
472
473
474
475

           The returned array does not own the data, which belongs to the
           property map. Therefore, if the graph changes, the array may become
           *invalid* and any operation on it will fail with a
           :class:`ValueError` exception. Do **not** store the array if
           the graph is to be modified; store a **copy** instead.
        """
        a = self._get_data()
        if a is None:
            return None
        return PropertyArray(a, prop_map=self)

    def _get_data(self):
476
477
        g = self.get_graph()
        if g is None:
Tiago Peixoto's avatar
Tiago Peixoto committed
478
            return None
479
        g.stash_filter(edge=True, vertex=True)
Tiago Peixoto's avatar
Tiago Peixoto committed
480
        if self.__key_type == 'v':
481
            n = g.num_vertices()
Tiago Peixoto's avatar
Tiago Peixoto committed
482
        elif self.__key_type == 'e':
483
            n = g._Graph__graph.GetMaxEdgeIndex() + 1
Tiago Peixoto's avatar
Tiago Peixoto committed
484
485
        else:
            n = 1
486
        g.pop_filter(edge=True, vertex=True)
Tiago Peixoto's avatar
Tiago Peixoto committed
487
488
489
490
        a = self.__map.get_array(n)
        return a

    def __set_array(self, v):
491
492
493
494
        a = self.get_array()
        if a is None:
            return
        a[:] = v
Tiago Peixoto's avatar
Tiago Peixoto committed
495

496
    a = property(get_array, __set_array,
Tiago Peixoto's avatar
Tiago Peixoto committed
497
                 doc=r"""Shortcut to the :meth:`~PropertyMap.get_array` method
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
                 as an attribute. This makes assignments more convenient, e.g.:

                 >>> g = gt.Graph()
                 >>> g.add_vertex(10)
                 [...]
                 >>> prop = g.new_vertex_property("double")
                 >>> prop.a = np.random.random(10)           # Assignment from array
                 """)

    def __get_set_f_array(self, v=None, get=True):
        g = self.get_graph()
        if g is None:
            return None
        a = self.get_array()
        filt = [None]
        if self.__key_type == 'v':
            filt = g.get_vertex_filter()
        elif self.__key_type == 'e':
            filt = g.get_edge_filter()
        if get:
518
519
520
521
            if a is None:
                return a
            if filt[0] is None:
                return a
522
523
            return a[filt[0].a == (not filt[1])]
        else:
524
525
526
527
528
529
            if a is None:
                return
            if filt[0] is None:
                a[:] = v
            else:
                a[filt[0].a == (not filt[1])] = v
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572

    fa = property(__get_set_f_array,
                  lambda self, v: self.__get_set_f_array(v, False),
                  doc=r"""The same as the :attr:`~PropertyMap.a` attribute, but
                  instead an *indexed* array is returned, which contains only
                  entries for vertices/edges which are not filtered out. If
                  there are no filters in place, the array is not indexed, and
                  is identical to the :attr:`~PropertyMap.a` attribute.

                  Note that because advanced indexing is triggered, a **copy**
                  of the array is returned, not a view, as for the
                  :attr:`~PropertyMap.a` attribute. Nevertheless, the assignment
                  of values to the *whole* array at once works as expected.""")

    def __get_set_m_array(self, v=None, get=True):
        g = self.get_graph()
        if g is None:
            return None
        a = self.get_array()
        filt = [None]
        if self.__key_type == 'v':
            filt = g.get_vertex_filter()
        elif self.__key_type == 'e':
            filt = g.get_edge_filter()
        if filt[0] is None or a is None:
            if get:
                return a
            else:
                return
        ma = numpy.ma.array(a, mask=(filt[0].a == False) if not filt[1] else (filt[0].a == True))
        if get:
            return ma
        else:
            ma[:] = v

    ma = property(__get_set_m_array,
                  lambda self, v: self.__get_set_m_array(v, False),
                  doc=r"""The same as the :attr:`~PropertyMap.a` attribute, but
                  instead a :class:`~numpy.ma.MaskedArray` object is returned,
                  which contains only entries for vertices/edges which are not
                  filtered out. If there are no filters in place, a regular
                  :class:`~graph_tool.PropertyArray` is returned, which is
                  identical to the :attr:`~PropertyMap.a` attribute.""")
Tiago Peixoto's avatar
Tiago Peixoto committed
573
574
575
576
577

    def is_writable(self):
        """Return True if the property is writable."""
        return self.__map.is_writable()

578
579
580
581
582
    def __call__(self, a):
        p = self.copy()
        p.fa = a
        return p

Tiago Peixoto's avatar
Tiago Peixoto committed
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635

def _check_prop_writable(prop, name=None):
    if not prop.is_writable():
        raise ValueError("property map%s is not writable." %\
                         ((" '%s'" % name) if name != None else ""))


def _check_prop_scalar(prop, name=None, floating=False):
    scalars = ["bool", "int32_t", "int64_t", "unsigned long",
               "double", "long double"]
    if floating:
        scalars = ["double", "long double"]

    if prop.value_type() not in scalars:
        raise ValueError("property map%s is not of scalar%s type." %\
                         (((" '%s'" % name) if name != None else ""),
                          (" floating" if floating else "")))


def _check_prop_vector(prop, name=None, scalar=True, floating=False):
    scalars = ["bool", "int32_t", "int64_t", "unsigned long",
               "double", "long double"]
    if not scalar:
        scalars += ["string"]
    if floating:
        scalars = ["double", "long double"]
    vals = ["vector<%s>" % v for v in scalars]
    if prop.value_type() not in vals:
        raise ValueError("property map%s is not of vector%s type." %\
                         (((" '%s'" % name) if name != None else ""),
                          (" floating" if floating else "")))


def group_vector_property(props, value_type=None, vprop=None, pos=None):
    """Group list of properties ``props`` into a vector property map of the same type.

    Parameters
    ----------
    props : list of :class:`~graph_tool.PropertyMap`
        Properties to be grouped.
    value_type : string (optional, default: None)
        If supplied, defines the value type of the grouped property.
    vprop : :class:`~graph_tool.PropertyMap` (optional, default: None)
        If supplied, the properties are grouped into this property map.
    pos : list of ints (optional, default: None)
        If supplied, should contain a list of indexes where each corresponding
        element of ``props`` should be inserted.

    Returns
    -------
    vprop : :class:`~graph_tool.PropertyMap`
       A vector property map with the grouped values of each property map in
       ``props``.
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650

    Examples
    --------
    >>> from numpy.random import seed, randint
    >>> from numpy import array
    >>> seed(42)
    >>> g = gt.random_graph(100, lambda: (3, 3))
    >>> props = [g.new_vertex_property("int") for i in xrange(3)]
    >>> for i in xrange(3):
    ...    props[i].a = randint(0, 100, g.num_vertices())
    >>> gprop = gt.group_vector_property(props)
    >>> print gprop[g.vertex(0)].a
    [71 40 96]
    >>> print array([p[g.vertex(0)] for p in props])
    [71 40 96]
Tiago Peixoto's avatar
Tiago Peixoto committed
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
    """
    g = props[0].get_graph()
    vtypes = set()
    keys = set()
    for i, p in enumerate(props):
        if "vector" in p.value_type():
            raise ValueError("property map 'props[%d]' is a vector property." %
                             i)
        vtypes.add(p.value_type())
        keys.add(p.key_type())
    if len(keys) > 1:
        raise ValueError("'props' must be of the same key type.")
    k = keys.pop()

    if vprop == None:
        if value_type == None and len(vtypes) == 1:
            value_type = vtypes.pop()

        if value_type != None:
            value_type = "vector<%s>" % value_type
            if k == 'v':
                vprop = g.new_vertex_property(value_type)
            elif k == 'e':
                vprop = g.new_edge_property(value_type)
            else:
                vprop = g.new_graph_property(value_type)
        else:
            ValueError("Can't automatically determine property map value" +
                       " type. Please provide the 'value_type' parameter.")
    _check_prop_vector(vprop, name="vprop", scalar=False)

    for i, p in enumerate(props):
        if k != "g":
            g.stash_filter(directed=True, reversed=True)
            g.set_directed(True)
            g.set_reversed(False)
687
            libcore.group_vector_property(g._Graph__graph, _prop(k, g, vprop),
Tiago Peixoto's avatar
Tiago Peixoto committed
688
689
690
691
692
                                          _prop(k, g, p),
                                          i if pos == None else pos[i],
                                          k == 'e')
            g.pop_filter(directed=True, reversed=True)
        else:
693
            vprop[g][i if pos is None else pos[i]] = p[g]
Tiago Peixoto's avatar
Tiago Peixoto committed
694
695
696
697
698
699
    return vprop


def ungroup_vector_property(vprop, pos, props=None):
    """Ungroup vector property map ``vprop`` into a list of individual property maps.

700
    Parameters 
Tiago Peixoto's avatar
Tiago Peixoto committed
701
702
703
    ----------
    vprop : :class:`~graph_tool.PropertyMap`
        Vector property map to be ungrouped.
704
705
706
    pos : list of ints
        A list of indexes corresponding to where each element of ``vprop``
        should be inserted into the ungrouped list.
Tiago Peixoto's avatar
Tiago Peixoto committed
707
708
709
710
711
712
713
714
    props : list of :class:`~graph_tool.PropertyMap`  (optional, default: None)
        If supplied, should contain a list of property maps to which ``vprop``
        should be ungroupped.

    Returns
    -------
    props : list of :class:`~graph_tool.PropertyMap`
       A list of property maps with the ungrouped values of ``vprop``.
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729

    Examples
    --------
    >>> from numpy.random import seed, randint
    >>> from numpy import array
    >>> seed(42)
    >>> g = gt.random_graph(100, lambda: (3, 3))
    >>> prop = g.new_vertex_property("vector<int>")
    >>> for v in g.vertices():
    ...    prop[v] = randint(0, 100, 3)
    >>> uprops = gt.ungroup_vector_property(prop, [0, 1, 2])
    >>> print prop[g.vertex(0)].a
    [71 60 20]
    >>> print array([p[g.vertex(0)] for p in uprops])
    [71 60 20]
Tiago Peixoto's avatar
Tiago Peixoto committed
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
    """

    g = vprop.get_graph()
    _check_prop_vector(vprop, name="vprop", scalar=False)
    k = vprop.key_type()
    value_type = vprop.value_type().split("<")[1].split(">")[0]
    if props == None:
        if k == 'v':
            props = [g.new_vertex_property(value_type) for i in pos]
        elif k == 'e':
            props = [g.new_edge_property(value_type) for i in pos]
        else:
            props = [g.new_graph_property(value_type) for i in pos]

    for i, p in enumerate(pos):
        if props[i].key_type() != k:
            raise ValueError("'props' must be of the same key type as 'vprop'.")

        if k != 'g':
            g.stash_filter(directed=True, reversed=True)
            g.set_directed(True)
            g.set_reversed(False)
            libcore.ungroup_vector_property(g._Graph__graph,
                                            _prop(k, g, vprop),
                                            _prop(k, g, props[i]),
                                            p, k == 'e')
            g.pop_filter(directed=True, reversed=True)
        else:
            if len(vprop[g]) <= pos[i]:
                vprop[g].resize(pos[i] + 1)
            props[i][g] = vprop[g][pos[i]]
    return props


764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
def infect_vertex_property(g, prop, vals=None):
    """Propagate the `prop` values of vertices with value `val` to all their
    out-neighbours.

    Parameters
    ----------
    prop : :class:`~graph_tool.PropertyMap`
        Property map to be modified.
    vals : list (optional, default: `None`)
        List of values to be propagated. If not provided, all values
        will be propagated.

    Returns
    -------
    None

    Examples
    --------
    >>> from numpy.random import seed
    >>> seed(42)
    >>> g = gt.random_graph(100, lambda: (3, 3))
    >>> prop = g.copy_property(g.vertex_index)
    >>> gt.infect_vertex_property(g, prop, [10])
    >>> print sum(prop.a == 10)
    3
    """
    libcore.infect_vertex_property(g._Graph__graph, _prop("v", g, prop),
                                   vals)

Tiago Peixoto's avatar
Tiago Peixoto committed
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832

def edge_difference(g, prop, ediff=None):
    """Return an edge property map corresponding to the difference between the
    values of `prop` of target and source vertices of each edge.

    Parameters
    ----------
    prop : :class:`~graph_tool.PropertyMap`
        Vertex property map to be used to compute the difference..
    ediff : :class:`~graph_tool.PropertyMap` (optional, default: `None`)
        If not provided, the difference values will be stored in this property
        map.

    Returns
    -------
    ediff : :class:`~graph_tool.PropertyMap`
        Edge differences.

    Examples
    --------
    >>> from numpy.random import seed
    >>> seed(42)
    >>> g = gt.random_graph(100, lambda: (3, 3))
    >>> ediff = gt.edge_difference(g, g.vertex_index)
    >>> print ediff.a
    3
    """
    val_t = prop.value_type()
    if val_t == "unsigned long":
        val_t = "int32_t"
    if ediff is None:
        ediff = g.new_edge_property(val_t)
    if ediff.value_type() != val_t:
        raise ValueError("'ediff' must be of the same value type as 'prop': " +
                         val_t)
    libcore.edge_difference(g._Graph__graph, _prop("v", g, prop),
                            _prop("e", g, ediff))
    return ediff


Tiago Peixoto's avatar
Tiago Peixoto committed
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
class PropertyDict(dict):
    """Wrapper for the dict of vertex, graph or edge properties, which sets the
    value on the property map when changed in the dict."""
    def __init__(self, g, old, get_func, set_func, del_func):
        dict.__init__(self)
        dict.update(self, old)
        self.g = g
        self.get_func = get_func
        self.set_func = set_func
        self.del_func = del_func

    def __setitem__(self, key, val):
        if self.set_func != None:
            self.set_func(self.g, key, val)
        else:
            raise KeyError("Property dict cannot be set")
        dict.__setitem__(self, key, val)

    def __delitem__(self, key):
        self.del_func(self.g, key)
        dict.__delitem__(self, key)

################################################################################
# Graph class
# The main graph interface
################################################################################

860
from libgraph_tool_core import Vertex, EdgeBase, Vector_bool, Vector_int32_t, \
Tiago Peixoto's avatar
Tiago Peixoto committed
861
862
863
864
865
866
867
     Vector_int64_t, Vector_double, Vector_long_double, Vector_string, \
     new_vertex_property, new_edge_property, new_graph_property


class Graph(object):
    """Generic multigraph class.

868
869
    This class encapsulates either a directed multigraph (default or if
    ``directed=True``) or an undirected multigraph (if ``directed=False``),
870
    with optional internal edge, vertex or graph properties.
Tiago Peixoto's avatar
Tiago Peixoto committed
871
872
873
874

    If ``g`` is specified, the graph (and its internal properties) will be
    copied.

875
876
877
878
879
    If ``prune`` is set to True, and ``g`` is specified, only the filtered graph
    will be copied, and the new graph object will not be filtered. Optionally, a
    tuple of three booleans can be passed as value to ``prune``, to specify a
    different behavior to vertex, edge, and reversal filters, respectively.

Tiago Peixoto's avatar
Tiago Peixoto committed
880
881
882
883
884
885
886
    The graph is implemented as an `adjacency list`_, where both vertex and edge
    lists are C++ STL vectors.

    .. _adjacency list: http://en.wikipedia.org/wiki/Adjacency_list

    """

887
    def __init__(self, g=None, directed=True, prune=False):
Tiago Peixoto's avatar
Tiago Peixoto committed
888
889
890
891
892
893
894
895
        self.__properties = {}
        self.__known_properties = []
        self.__filter_state = {"reversed": False,
                               "edge_filter": (None, False),
                               "vertex_filter": (None, False),
                               "directed": True}
        self.__stashed_filter_state = []

896
        if g is None:
Tiago Peixoto's avatar
Tiago Peixoto committed
897
898
899
            self.__graph = libcore.GraphInterface()
            self.set_directed(directed)
        else:
900
901
902
903
904
905
            if isinstance(prune, bool):
                vprune = eprune = rprune = prune
            else:
                vprune, eprune, rprune = prune
            if not (vprune or eprune or rprune):
                g.stash_filter(vertex=vprune, edge=vprune, reversed=rprune)
Tiago Peixoto's avatar
Tiago Peixoto committed
906
            self.__graph = libcore.GraphInterface(g.__graph, False)
907
908
909
            if not (vprune or eprune or rprune):
                g.pop_filter(vertex=vprune, edge=vprune, reversed=rprune)

Tiago Peixoto's avatar
Tiago Peixoto committed
910
911
            for k, v in g.__properties.iteritems():
                new_p = self.new_property(v.key_type(), v.value_type())
912
                self.copy_property(v, new_p, g=g)
Tiago Peixoto's avatar
Tiago Peixoto committed
913
914
915
916
                self.properties[k] = new_p

            self.__stashed_filter_state = [self.get_filter_state()]

917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
            if not vprune:
                v_filt, v_rev = g.__filter_state["vertex_filter"]
                if v_filt != None:
                    if v_filt not in g.vertex_properties.values():
                        new_filt = self.new_vertex_property("bool")
                        self.copy_property(v_filt, new_filt)

                    else:
                        for k, v in g.vertex_properties.iteritems():
                            if v == v_filt:
                                new_filt = self.vertex_properties[k]
                    self.__stashed_filter_state[0]["vertex_filter"] = (new_filt,
                                                                       v_rev)
            if not eprune:
                e_filt, e_rev = g.__filter_state["edge_filter"]
                if e_filt != None:
                    if e_filt not in g.edge_properties.values():
                        new_filt = self.new_edge_property("bool")
                        self.copy_property(e_filt, new_filt)

                    else:
                        for k, v in g.edge_properties.iteritems():
                            if v == e_filt:
                                new_filt = self.edge_properties[k]
                    self.__stashed_filter_state[0]["edge_filter"] = (new_filt,
                                                                     e_rev)
            if not rprune:
                self.__stashed_filter_state[0]["reversed"] = g.is_reversed()

            # directedness is always a filter
            self.__stashed_filter_state[0]["directed"] = g.is_directed()

Tiago Peixoto's avatar
Tiago Peixoto committed
949
            self.pop_filter()
950
951
952
953

            if vprune or eprune:
                self.reindex_edges()

Tiago Peixoto's avatar
Tiago Peixoto committed
954
955
956
957
958
959
960
961
962
963
964
        # internal index maps
        self.__vertex_index = \
                 PropertyMap(libcore.get_vertex_index(self.__graph), self, "v")
        self.__edge_index = \
                 PropertyMap(libcore.get_edge_index(self.__graph), self, "e")

        # modification permissions
        self.__perms = {"add_edge": True, "del_edge": True,
                        "add_vertex": True, "del_vertex": True}

    def copy(self):
965
966
        """Return a deep copy of self. All :ref:`internal property maps <sec_internal_props>`
        are also copied."""
Tiago Peixoto's avatar
Tiago Peixoto committed
967
968
969
        return Graph(self)

    def __repr__(self):
970
        # provide more useful information
Tiago Peixoto's avatar
Tiago Peixoto committed
971
972
973
        d = "directed" if self.is_directed() else "undirected"
        fr = ", reversed" if self.is_reversed() and self.is_directed() else ""
        f = ""
974
        if self.get_edge_filter()[0] is not None:
Tiago Peixoto's avatar
Tiago Peixoto committed
975
            f += ", edges filtered by %s" % (str(self.get_edge_filter()))
976
        if self.get_vertex_filter()[0] is not None:
Tiago Peixoto's avatar
Tiago Peixoto committed
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
            f += ", vertices filtered by %s" % (str(self.get_vertex_filter()))
        n = self.num_vertices()
        e = self.num_edges()
        return "<%s object, %s%s, with %d %s and %d edge%s%s at 0x%x>"\
               % (type(self).__name__, d, fr, n,
                  "vertex" if n == 1 else "vertices", e, "" if e == 1 else "s",
                  f, id(self))

    # Graph access
    # ============

    def __check_perms(self, ptype):
        if not self.__perms[ptype]:
            raise RuntimeError("the graph cannot be modified at this point!")

    def vertices(self):
993
994
995
996
997
998
999
        """Return an :meth:`iterator <iterator.__iter__>` over the vertices.

        .. note::

           The order of the vertices traversed by the iterator **always**
           corresponds to the vertex index ordering, as given by the
           :attr:`~graph_tool.Graph.vertex_index` property map.
Tiago Peixoto's avatar
Tiago Peixoto committed
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011

        Examples
        --------
        >>> g = gt.Graph()
        >>> vlist = g.add_vertex(5)
        >>> vlist2 = []
        >>> for v in g.vertices():
        ...     vlist2.append(v)
        ...
        >>> assert(vlist == vlist2)

        """
1012
        return libcore.get_vertices(weakref.ref(self))
Tiago Peixoto's avatar
Tiago Peixoto committed
1013
1014
1015
1016
1017
1018
1019
1020

    def vertex(self, i, use_index=True):
        """Return the vertex with index ``i``. If ``use_index=False``, the
        ``i``-th vertex is returned (which can differ from the vertex with index
        ``i`` in case of filtered graphs). """
        if use_index:
            self.stash_filter(vertex=True)
        try:
1021
            v = libcore.get_vertex(weakref.ref(self), int(i))
Tiago Peixoto's avatar
Tiago Peixoto committed
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
        finally:
            if use_index:
                self.pop_filter(vertex=True)
        return v

    def edge(self, s, t, all_edges=False):
        """Return the edge from vertex ``s`` to ``t``, if it exists. If
        ``all_edges=True`` then a list is returned with all the parallel edges
        from ``s`` to ``t``, otherwise only one edge is returned.

        This operation will take :math:`O(k(s))` time, where :math:`k(s)` is the
        out-degree of vertex :math:`s`.
        """
        s = self.vertex(int(s))
        t = self.vertex(int(t))
        edges = []
        for e in s.out_edges():
            if e.target() == t:
                if not all_edges:
                    return e
                edges.append(e)
        if all_edges:
            return edges
        return None

    def edges(self):
1048
1049
1050
1051
1052
        """Return an :meth:`iterator <iterator.__iter__>` over the edges.

        .. note::

           The order of the edges traversed by the iterator **does not**
Tiago Peixoto's avatar
Tiago Peixoto committed
1053
           necessarily correspond to the edge index ordering, as given by the
1054
1055
1056
1057
1058
1059
1060
           :attr:`~graph_tool.Graph.edge_index` property map. This will only
           happen after :meth:`~graph_tool.Graph.reindex_edges` is called, or in
           certain situations such as just after a graph is loaded from a
           file. However, further manipulation of the graph may destroy the
           ordering.

        """
1061
        return libcore.get_edges(weakref.ref(self))
Tiago Peixoto's avatar
Tiago Peixoto committed
1062
1063
1064
1065
1066

    def add_vertex(self, n=1):
        """Add a vertex to the graph, and return it. If ``n > 1``, ``n``
        vertices are inserted and a list is returned."""
        self.__check_perms("add_vertex")
1067
1068
1069
        vlist = []
        vfilt = self.get_vertex_filter()
        for i in xrange(n):
1070
            v = libcore.add_vertex(weakref.ref(self))
1071
1072
1073
            if vfilt[0] is not None:
                vfilt[0][v] = not vfilt[1]
            vlist.append(v)
Tiago Peixoto's avatar
Tiago Peixoto committed
1074
1075
1076
1077
1078
1079
1080
1081
1082
        if n == 1:
            return vlist[0]
        return vlist

    def remove_vertex(self, vertex):
        """Remove a vertex from the graph."""
        self.__check_perms("del_vertex")
        index = self.vertex_index[vertex]
        for pmap in self.__known_properties:
1083
            if pmap[0] == "v" and pmap[1]() != None and pmap[1]().is_writable():
Tiago Peixoto's avatar
Tiago Peixoto committed
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
                self.__graph.ShiftVertexProperty(pmap[1]().get_map(), index)
        self.clear_vertex(vertex)
        libcore.remove_vertex(self.__graph, vertex)

    def remove_vertex_if(self, predicate):
        """Remove all the vertices from the graph for which ``predicate(v)``
        evaluates to ``True``. """
        N = self.num_vertices()
        for i in xrange(0, N):
            v = self.vertex(N - i - 1)
            if predicate(v):
                self.remove_vertex(v)

    def clear_vertex(self, vertex):
        """Remove all in and out-edges from the given vertex."""
        del_es = set()
        for e in vertex.all_edges():
            del_es.add(e)
        for e in del_es:
            self.remove_edge(e)

    def add_edge(self, source, target):
        """Add a new edge from ``source`` to ``target`` to the graph, and return
        it."""
        self.__check_perms("add_edge")
1109
        e = libcore.add_edge(weakref.ref(self), source, target)
1110
1111
1112
1113
        efilt = self.get_edge_filter()
        if efilt[0] is not None:
            efilt[0][e] = not efilt[1]
        return e
Tiago Peixoto's avatar
Tiago Peixoto committed
1114
1115
1116
1117

    def remove_edge(self, edge):
        """Remove an edge from the graph."""
        self.__check_perms("del_edge")
Tiago Peixoto's avatar
Tiago Peixoto committed
1118
        return libcore.remove_edge(self.__graph, edge)
Tiago Peixoto's avatar
Tiago Peixoto committed
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168

    def remove_edge_if(self, predicate):
        """Remove all edges from the graph, for which ``predicate(e)`` evaluates
        to ``True``."""
        for v in self.vertices():
            del_es = []
            for e in v.out_edges():
                if predicate(e):
                    del_es.append(e)
            for e in del_es:
                self.remove_edge(e)

    def clear(self):
        """Remove all vertices and edges from the graph."""
        self.__check_perms("del_vertex")
        self.__check_perms("del_edge")
        self.__graph.Clear()

    def clear_edges(self):
        """Remove all edges from the graph."""
        self.__check_perms("del_edge")
        self.__graph.ClearEdges()

    # Internal property maps
    # ======================

    # all properties
    def __get_properties(self):
        return PropertyDict(self, self.__properties,
                            lambda g, k: g.__properties[k],
                            lambda g, k, v: g.__set_property(k[0], k[1], v),
                            lambda g, k: g.__del_property(k[0], k[1]))

    @_limit_args({"t": ["v", "e", "g"]})
    @_require("k", str)
    @_require("v", PropertyMap)
    def __set_property(self, t, k, v):
        if t != v.key_type():
            raise ValueError("wrong key type for property map")
        self.__properties[(t, k)] = v

    @_limit_args({"t": ["v", "e", "g"]})
    @_require("k", str)
    def __del_property(self, t, k):
        del self.__properties[(t, k)]

    properties = property(__get_properties,
                          doc=
    """Dictionary of internal properties. Keys must always be a tuple, where the
    first element if a string from the set {'v', 'e', 'g'}, representing a
1169
1170
    vertex, edge or graph property, respectively, and the second element is the
    name of the property map.
Tiago Peixoto's avatar
Tiago Peixoto committed
1171
1172
1173
1174
1175
1176
1177
1178
1179

    Examples
    --------
    >>> g = gt.Graph()
    >>> g.properties[("e", "foo")] = g.new_edge_property("vector<double>")
    >>> del g.properties[("e", "foo")]
    """)

    def __get_specific_properties(self, t):
1180
1181
        props = dict([(k[1], v) for k, v in self.__properties.iteritems() \
                      if k[0] == t])
Tiago Peixoto's avatar
Tiago Peixoto committed
1182
1183
1184
1185
1186
1187
1188
1189
1190
        return props

    # vertex properties
    def __get_vertex_properties(self):
        return PropertyDict(self, self.__get_specific_properties("v"),
                            lambda g, k: g.__properties[("v", k)],
                            lambda g, k, v: g.__set_property("v", k, v),
                            lambda g, k: g.__del_property("v", k))
    vertex_properties = property(__get_vertex_properties,
1191
                                 doc="Dictionary of internal vertex properties. The keys are the property names.")
Tiago Peixoto's avatar
Tiago Peixoto committed
1192
1193
1194
1195
1196
1197
1198
1199

    # edge properties
    def __get_edge_properties(self):
        return PropertyDict(self, self.__get_specific_properties("e"),
                            lambda g, k: g.__properties[("e", k)],
                            lambda g, k, v: g.__set_property("e", k, v),
                            lambda g, k: g.__del_property("e", k))
    edge_properties = property(__get_edge_properties,
1200
                                 doc="Dictionary of internal edge properties. The keys are the property names.")
Tiago Peixoto's avatar
Tiago Peixoto committed
1201
1202
1203
1204
1205
1206
1207
1208

    # graph properties
    def __get_graph_properties(self):
        return PropertyDict(self, self.__get_specific_properties("g"),
                            lambda g, k: g.__properties[("g", k)],
                            lambda g, k, v: g.__set_property("g", k, v),
                            lambda g, k: g.__del_property("g", k))
    graph_properties = property(__get_graph_properties,
1209
                                 doc="Dictionary of internal graph properties. The keys are the property names.")
Tiago Peixoto's avatar
Tiago Peixoto committed
1210

1211
1212
1213
1214
    def own_property(self, prop):
        """'Own' property map 'prop', which may belong to another graph."""
        return PropertyMap(prop._PropertyMap__map, self, prop.key_type())

Tiago Peixoto's avatar
Tiago Peixoto committed
1215
    def list_properties(self):
1216
        """Print a list of all internal properties.
Tiago Peixoto's avatar
Tiago Peixoto committed
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254

        Examples
        --------
        >>> g = gt.Graph()
        >>> g.properties[("e", "foo")] = g.new_edge_property("vector<double>")
        >>> g.vertex_properties["foo"] = g.new_vertex_property("double")
        >>> g.vertex_properties["bar"] = g.new_vertex_property("python::object")
        >>> g.graph_properties["gnat"] = g.new_graph_property("string", "hi there!")
        >>> g.list_properties()
        gnat           (graph)   (type: string, val: hi there!)
        bar            (vertex)  (type: python::object)
        foo            (vertex)  (type: double)
        foo            (edge)    (type: vector<double>)
        """

        if len(self.__properties) == 0:
            return
        w = max([len(x[0]) for x in self.__properties.keys()]) + 4
        w = w if w > 14 else 14

        for k, v in self.__properties.iteritems():
            if k[0] == "g":
                print "%%-%ds (graph)   (type: %%s, val: %%s)" % w % \
                      (k[1], v.value_type(), str(v[self]))
        for k, v in self.__properties.iteritems():
            if k[0] == "v":
                print "%%-%ds (vertex)  (type: %%s)" % w % (k[1],
                                                            v.value_type())
        for k, v in self.__properties.iteritems():
            if k[0] == "e":
                print "%%-%ds (edge)    (type: %%s)" % w % (k[1],
                                                            v.value_type())

    # index properties

    def _get_vertex_index(self):
        return self.__vertex_index
    vertex_index = property(_get_vertex_index,
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
                            doc="""Vertex index map.

                            It maps for each vertex in the graph an unique
                            integer in the range [0, :meth:`~graph_tool.Graph.num_vertices` - 1].

                            .. note::

                                This is a special instance of a :class:`~graph_tool.PropertyMap`
                                class, which is **immutable**, and cannot be
                                accessed as an array.""")
Tiago Peixoto's avatar
Tiago Peixoto committed
1265
1266
1267

    def _get_edge_index(self):
        return self.__edge_index
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
    edge_index = property(_get_edge_index, doc="""Edge index map.

                            It maps for each edge in the graph an unique
                            integer.

                            .. warning::

                                Differently from :attr:`~graph_tool.Graph.vertex_index`,
                                this is a **regular** instance of a :class:`~graph_tool.PropertyMap`
                                class, and is therefore **mutable**!

                                Additionally, the indexes may not necessarily
                                lie in the range [0, :meth:`~graph_tool.Graph.num_edges` - 1].
                                However this will always happen whenever no
                                edges are deleted from the graph.

                                The internal consistency expected by most
                                algorithms and the proper functioning of
                                property maps assume that the indexes are unique
                                and constant, which is guaranteed by the
                                library.  Therefore it is recommended **never**
                                to modify these values, unless you know what you
                                are doing.""")
Tiago Peixoto's avatar
Tiago Peixoto committed
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306

    def _get_max_edge_index(self):
        return self.__graph.GetMaxEdgeIndex()
    max_edge_index = property(_get_max_edge_index,
                              doc="The maximum value of the edge index map.")

    def reindex_edges(self):
        """
        Reset the edge indexes so that they lie in the [0, :meth:`~graph_tool.Graph.num_edges` - 1]
        range. The index ordering will be compatible with the sequence returned
        by the :meth:`~graph_tool.Graph.edges` function.

        .. WARNING::

           Calling this function will invalidate all existing edge property
           maps, if the index ordering is modified! The property maps will still
1307
1308
           be usable, but their contents will still be tied to the old indexes,
           and thus may become scrambled.
Tiago Peixoto's avatar
Tiago Peixoto committed
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
        """
        self.__graph.ReIndexEdges()

    # Property map creation

    def new_property(self, key_type, value_type):
        """Create a new (uninitialized) vertex property map of key type
        ``key_type`` (``v``, ``e`` or ``g``), value type ``value_type``, and
        return it.
        """
        if key_type == "v" or key_type == "vertex":
            return self.new_vertex_property(value_type)
        if key_type == "e" or key_type == "edge":
            return self.new_edge_property(value_type)
        if key_type == "g" or key_type == "graph":
            return self.new_graph_property(value_type)
        raise ValueError("unknown key type: " + key_type)

    def new_vertex_property(self, value_type):
        """Create a new (uninitialized) vertex property map of type
        ``value_type``, and return it."""
        return PropertyMap(new_vertex_property(_type_alias(value_type),
                                               self.__graph.GetVertexIndex()),
                           self, "v")

    def new_edge_property(self, value_type):
        """Create a new (uninitialized) edge property map of type
        ``value_type``, and return it."""
        return PropertyMap(new_edge_property(_type_alias(value_type),
                                             self.__graph.GetEdgeIndex()),
                           self, "e")

    def new_graph_property(self, value_type, val=None):
        """Create a new graph property map of type ``value_type``, and return
        it. If ``val`` is not None, the property is initialized to its value."""
        prop = PropertyMap(new_graph_property(_type_alias(value_type),
                                              self.__graph.GetGraphIndex()),
                           self, "g", lambda k: k.__graph)
        if val != None:
            prop[self] = val
        return prop

    # property map copying
    @_require("src", PropertyMap)
    @_require("tgt", (PropertyMap, type(None)))
    def copy_property(self, src, tgt=None, value_type=None, g=None):
        """Copy contents of ``src`` property to ``tgt`` property. If ``tgt`` is
        None, then a new property map of the same type (or with the type given
        by the optional ``value_type`` parameter) is created, and returned. The
        optional parameter g specifies the (identical) source graph to copy
        properties from (defaults to self).
        """
1361
        if tgt is None:
Tiago Peixoto's avatar
Tiago Peixoto committed
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
            tgt = self.new_property(src.key_type(),
                                    (src.value_type()
                                     if value_type == None else value_type))
            ret = tgt
        else:
            ret = None

        if src.key_type() != tgt.key_type():
            raise ValueError("source and target properties must have the same" +
                             " key type")
1372
        if g is None:
Tiago Peixoto's avatar
Tiago Peixoto committed
1373
            g = self
1374
1375
1376
        if g is not self:
            g.stash_filter(directed=True, reversed=True)
        self.stash_filter(directed=True, reversed=True)
Tiago Peixoto's avatar
Tiago Peixoto committed
1377
1378
        if src.key_type() == "v":
            self.__graph.CopyVertexProperty(g.__graph, _prop("v", g, src),
1379
                                            _prop("v", self, tgt))
Tiago Peixoto's avatar
Tiago Peixoto committed
1380
1381
        elif src.key_type() == "e":
            self.__graph.CopyEdgeProperty(g.__graph, _prop("e", g, src),
1382
                                            _prop("e", self, tgt))
Tiago Peixoto's avatar
Tiago Peixoto committed
1383
1384
        else:
            tgt[self] = src[g]
1385
1386
1387
        self.pop_filter(directed=True, reversed=True)
        if g is not self:
            g.pop_filter(directed=True, reversed=True)
Tiago Peixoto's avatar
Tiago Peixoto committed
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
        return ret

    # degree property map
    @_limit_args({"deg": ["in", "out", "total"]})
    def degree_property_map(self, deg):
        """Create and return a vertex property map containing the degree type
        given by ``deg``."""
        return PropertyMap(self.__graph.DegreeMap(deg), self, "v")

    # I/O operations
    # ==============
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
    def __get_file_format(self, file_name):
        fmt = None
        for f in ["xml", "dot", "gml"]:
            names = ["." + f, ".%s.gz" % f, ".%s.bz2" % f]
            for name in names:
                if file_name.endswith(name):
                    fmt = f
                    break
        if fmt is None:
            raise ValueError("cannot determine file format of: " + file_name)
        return fmt

    def load(self, file_name, fmt="auto"):
Tiago Peixoto's avatar
Tiago Peixoto committed
1412
1413
        """Load graph from ``file_name`` (which can be either a string or a
        file-like object). The format is guessed from ``file_name``, or can be
1414
        specified by ``fmt``, which can be either "xml", "dot" or "gml". """
Tiago Peixoto's avatar
Tiago Peixoto committed
1415
1416
1417

        if type(file_name) == str:
            file_name = os.path.expanduser(file_name)
1418
1419
1420
1421
        if fmt == 'auto' and isinstance(file_name, str):
            fmt = self.__get_file_format(file_name)
        elif fmt == "auto":
            fmt = "xml"
Tiago Peixoto's avatar
Tiago Peixoto committed
1422
        if isinstance(file_name, str):
1423
            props = self.__graph.ReadFromFile(file_name, None, fmt)
Tiago Peixoto's avatar
Tiago Peixoto committed
1424
        else:
1425
            props = self.__graph.ReadFromFile("", file_name, fmt)
Tiago Peixoto's avatar
Tiago Peixoto committed
1426
1427
1428
1429
1430
1431
1432
1433
        for name, prop in props[0].iteritems():
            self.vertex_properties[name] = PropertyMap(prop, self, "v")
        for name, prop in props[1].iteritems():
            self.edge_properties[name] = PropertyMap(prop, self, "e")
        for name, prop in props[2].iteritems():
            self.graph_properties[name] = PropertyMap(prop, self, "g",
                                                      lambda k: k.__graph)

1434
    def save(self, file_name, fmt="auto"):
Tiago Peixoto's avatar
Tiago Peixoto committed
1435
1436
        """Save graph to ``file_name`` (which can be either a string or a
        file-like object). The format is guessed from the ``file_name``, or can
1437
        be specified by ``fmt``, which can be either "xml", "dot" or "gml". """
Tiago Peixoto's avatar
Tiago Peixoto committed
1438
1439
1440

        if type(file_name) == str:
            file_name = os.path.expanduser(file_name)
1441
1442
1443
1444
        if fmt == 'auto' and isinstance(file_name, str):
            fmt = self.__get_file_format(file_name)
        elif fmt == "auto":
            fmt = "xml"
Tiago Peixoto's avatar
Tiago Peixoto committed
1445
1446
1447
        props = [(name[1], prop._PropertyMap__map) for name, prop in \
                 self.__properties.iteritems()]
        if isinstance(file_name, str):
1448
            self.__graph.WriteToFile(file_name, None, fmt, props)
Tiago Peixoto's avatar
Tiago Peixoto committed
1449
        else:
1450
            self.__graph.WriteToFile("", file_name, fmt, props)
Tiago Peixoto's avatar
Tiago Peixoto committed
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510

    # Directedness
    # ============

    def set_directed(self, is_directed):
        """Set the directedness of the graph."""
        self.__graph.SetDirected(is_directed)

    def is_directed(self):
        """Get the directedness of the graph."""
        return self.__graph.GetDirected()

    # Reversedness
    # ============

    def set_reversed(self, is_reversed):
        """Reverse the direction of the edges, if ``is_reversed`` is ``True``,
        or maintain the original direction otherwise."""
        self.__graph.SetReversed(is_reversed)

    def is_reversed(self):
        """Return ``True`` if the edges are reversed, and ``False`` otherwise.
        """
        return self.__graph.GetReversed()

    # Filtering
    # =========

    def set_vertex_filter(self, prop, inverted=False):
        """Choose vertex boolean filter property. Only the vertices with value
        different than zero are kept in the filtered graph. If the ``inverted``
        option is supplied with value ``True``, only the vertices with value
        zero are kept. If the supplied property is ``None``, any previous
        filtering is removed."""

        self.__graph.SetVertexFilterProperty(_prop("v", self, prop),
                                             inverted)
        self.__filter_state["vertex_filter"] = (prop, inverted)

    def get_vertex_filter(self):
        """Return a tuple with the vertex filter property and bool value
        indicating whether or not it is inverted."""
        return self.__filter_state["vertex_filter"]

    def set_edge_filter(self, prop, inverted=False):
        """Choose edge boolean filter property. Only the edges with value
        different than zero are kept in the filtered graph. If the ``inverted``
        option is supplied with value ``True``, only the edges with value zero
        are kept. If the supplied property is ``None``, any previous filtering
        is removed."""
        self.__graph.SetEdgeFilterProperty(_prop("e", self, prop), inverted)
        self.__filter_state["edge_filter"] = (prop, inverted)

    def get_edge_filter(self):
        """Return a tuple with the edge filter property and bool value
        indicating whether or not it is inverted."""
        return self.__filter_state["edge_filter"]

    def purge_vertices(self):
        """Remove all vertices of the graph which are currently being filtered
1511
1512
1513
1514
        out, and return it to the unfiltered state. This operation is not
        reversible."""
        old_indexes = self.vertex_index.copy("int32_t")
        self.__graph.PurgeVertices(_prop("v", self, old_indexes))
Tiago Peixoto's avatar
Tiago Peixoto committed
1515
        self.set_vertex_filter(None)
1516
1517
1518
1519
        for pmap in self.__known_properties:
            if pmap[0] == "v" and pmap[1]() != None and pmap[1]().is_writable():
                self.__graph.ReIndexVertexProperty(pmap[1]().get_map(),
                                                   _prop("v", self, old_indexes))
Tiago Peixoto's avatar
Tiago Peixoto committed
1520
1521
1522

    def purge_edges(self):
        """Remove all edges of the graph which are currently being filtered out,
1523
        and return it to the unfiltered state. This operation is not reversible."""
Tiago Peixoto's avatar
Tiago Peixoto committed
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
        self.__graph.PurgeEdges()
        self.set_edge_filter(None)

    def stash_filter(self, edge=False, vertex=False, directed=False,
                     reversed=False, all=True):
        """Stash current filter state and set the graph to its unfiltered
        state. The optional keyword arguments specify which type of filter
        should be stashed."""
        if edge or vertex or directed or reversed:
            all = False
        self.__stashed_filter_state.append(self.get_filter_state())
        if libcore.graph_filtering_enabled():
            if vertex or all:
                self.set_vertex_filter(None)
            if edge or all:
                self.set_edge_filter(None)
        if directed or all:
            self.set_directed(True)
        if reversed or all:
            self.set_reversed(False)

    def pop_filter(self, edge=False, vertex=False, directed=False,
                   reversed=False, all=True):
        """Pop last stashed filter state. The optional keyword arguments specify
        which type of filter should be recovered."""
        if edge or vertex or directed or reversed:
            all = False
        state = self.__stashed_filter_state.pop()
        if libcore.graph_filtering_enabled():
            if vertex or all:
                self.set_vertex_filter(state["vertex_filter"][0],
                                       state["vertex_filter"][1])
            if edge or all:
                self.set_edge_filter(state["edge_filter"][0],
                                     state["edge_filter"][1])
        if directed or all:
            self.set_directed(state["directed"])
        if reversed or all:
            self.set_reversed(state["reversed"])

    def get_filter_state(self):
        """Return a copy of the filter state of the graph."""
        self.__filter_state["directed"] = self.is_directed()
        self.__filter_state["reversed"] = self.is_reversed()
        return copy.copy(self.__filter_state)

    def set_filter_state(self, state):
        """Set the filter state of the graph."""
        if libcore.graph_filtering_enabled():
            self.set_vertex_filter(state["vertex_filter"][0],
                                   state["vertex_filter"][1])
            self.set_edge_filter(state["edge_filter"][0],
                                 state["edge_filter"][1])
        self.set_directed(state["directed"])
        self.set_reversed(state["reversed"])

    # Basic graph statistics
    # ======================

    def num_vertices(self):
1584
1585
1586
1587
1588
1589
1590
        """Get the number of vertices.

        .. note::

            If the vertices are being filtered, this operation is
            :math:`O(N)`. Otherwise it is :math:`O(1)`.
        """
Tiago Peixoto's avatar
Tiago Peixoto committed
1591
1592
1593
        return self.__graph.GetNumberOfVertices()

    def num_edges(self):
1594
1595
1596
1597
1598
1599
1600
        """Get the number of edges.

        .. note::

            If the edges are being filtered, this operation is
            :math:`O(E)`. Otherwise it is :math:`O(1)`.
        """
Tiago Peixoto's avatar
Tiago Peixoto committed
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
        return self.__graph.GetNumberOfEdges()

    # Pickling support
    # ================

    def __getstate__(self):
        state = dict()
        sio = StringIO()
        if libcore.graph_filtering_enabled():
            if self.get_vertex_filter()[0] != None:
                self.vertex_properties["_Graph__pickle__vfilter"] = \
                    self.get_vertex_filter()[0]
                state["vfilter"] = self.get_vertex_filter()[1]
            if self.get_edge_filter()[0] != None:
                self.edge_properties["_Graph__pickle__efilter"] = \
                    self.get_edge_filter()[0]
                state["efilter"] = self.get_edge_filter()[1]
        self.save(sio, "xml")
        state["blob"] = sio.getvalue()
        return state

    def __setstate__(self, state):
        self.__init__()
        blob = state["blob"]
        if blob != "":
            sio = StringIO(blob)
            self.load(sio, "xml")
        if "vfilt" in state:
            vprop = self.vertex_properties["_Graph__pickle__vfilter"]
            self.set_vertex_filter(vprop, state["vfilt"])
        if "efilt" in state:
            eprop = self.edge_properties["_Graph__pickle__efilter"]
            self.set_edge_filter(eprop, state["efilt"])


1636
def load_graph(file_name, fmt="auto"):
Tiago Peixoto's avatar
Tiago Peixoto committed
1637
1638
1639
1640
    """
    Load a graph from ``file_name`` (which can be either a string or a file-like object).

    The format is guessed from ``file_name``, or can be specified by
1641
    ``fmt``, which can be either "xml", "dot" or "gml".
Tiago Peixoto's avatar
Tiago Peixoto committed
1642
1643
    """
    g = Graph()
1644
    g.load(file_name, fmt)
Tiago Peixoto's avatar
Tiago Peixoto committed
1645
1646
1647
1648
1649
1650
1651
1652
1653
    return g


def value_types():
    """Return a list of possible properties value types."""
    return libcore.get_property_types()

# Vertex and Edge Types
# =====================
1654
from libgraph_tool_core import Vertex, Edge, EdgeBase
Tiago Peixoto's avatar
Tiago Peixoto committed
1655
1656
1657

Vertex.__doc__ = """Vertex descriptor.

1658
1659
1660
1661
1662
This class represents a vertex in a :class:`~graph_tool.Graph` instance.

:class:`~graph_tool.Vertex` instances are hashable, and are convertible to
integers, corresponding to its index (see :attr:`~graph_tool.Graph.vertex_index`).
"""
Tiago Peixoto's avatar
Tiago Peixoto committed
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704


def _out_neighbours(self):
    """Return an iterator over the out-neighbours."""
    for e in self.out_edges():
        yield e.target()
Vertex.out_neighbours = _out_neighbours


def _in_neighbours(self):
    """Return an iterator over the in-neighbours."""
    for e in self.in_edges():
        yield e.source()
Vertex.in_neighbours = _in_neighbours


def _all_edges(self):
    """Return an iterator over all edges (both in or out)."""
    for e in self.out_edges():
        yield e
    for e in self.in_edges():
        yield e
Vertex.all_edges = _all_edges


def _all_neighbours(self):
    """Return an iterator over all neighbours (both in or out)."""
    for v in self.out_neighbours():
        yield v
    for v in self.in_neighbours():
        yield v
Vertex.all_neighbours = _all_neighbours


def _vertex_repr(self):
    if not self.is_valid():
        return "<invalid Vertex object at 0x%x>" % (id(self))
    return "<Vertex object with index '%d' at 0x%x>" % (int(self), id(self))
Vertex.__repr__ = _vertex_repr

_edge_doc = """Edge descriptor.

1705
1706
1707
1708
1709
This class represents an edge in a :class:`~graph_tool.Graph`.

:class:`~graph_tool.Edge` instances are hashable, and are convertible to a
tuple, which contains the source and target vertices.
"""
Tiago Peixoto's avatar
Tiago Peixoto committed
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727


def _edge_iter(self):
    """Iterate over the source and target"""
    for v in [self.source(), self.target()]:
        yield v


def _edge_repr(self):
    if not self.is_valid():
        return "<invalid Edge object at 0x%x>" % (id(self))

    return ("<Edge object with source '%d' and target '%d'" +
            " at 0x%x>") % (int(self.source()), int(self.target()), id(self))


# There are several edge classes... me must cycle through them all to modify
# them.
1728

Tiago Peixoto's avatar
Tiago Peixoto committed
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
def init_edge_classes():
    for directed in [True, False]:
        for e_reversed in [True, False]:
            for e_filtered in [True, False]:
                for v_filtered in [True, False]:
                    g = Graph(directed=directed)
                    g.set_reversed(e_reversed)
                    v = g.add_vertex()
                    g.add_edge(v, v)
                    if e_filtered:
                        e_filter = g.new_edge_property("bool")
                        e_filter.a = [1]
                        g.set_edge_filter(e_filter)
                    if v_filtered:
                        v_filter = g.new_vertex_property("bool")
                        v_filter.a = [1]
                        g.set_vertex_filter(v_filter)
                    e = g.edges().next()
                    e.__class__.__repr__ = _edge_repr
                    e.__class__.__iter__ = _edge_iter
                    e.__class__.__doc__ = _edge_doc

init_edge_classes()

1753
1754
1755
1756
1757
1758
1759
1760
# some shenanigans to make it seem there is only a single edge class
EdgeBase.__doc__ = Edge.__doc__
EdgeBase.source = Edge.source
EdgeBase.target = Edge.target
EdgeBase.is_valid = Edge.is_valid
EdgeBase.get_graph = Edge.get_graph
Edge = EdgeBase
Edge.__name__ = "Edge"
Tiago Peixoto's avatar
Tiago Peixoto committed
1761
1762


1763
# Add convenience function to vector classes
Tiago Peixoto's avatar
Tiago Peixoto committed
1764
1765
1766
def _get_array_view(self):
    return self.get_array()[:]

1767
1768
1769
1770

def _set_array_view(self, v):
    self.get_array()[:] = v

Tiago Peixoto's avatar
Tiago Peixoto committed
1771
1772
1773
vector_types = [Vector_bool, Vector_int32_t, Vector_int64_t, Vector_double,
                Vector_long_double]
for vt in vector_types:
1774
    vt.a = property(_get_array_view, _set_array_view,
1775
                    doc=r"""Shortcut to the `get_array` method as an attribute.""")
1776
    vt.__repr__ = lambda self: self.a.__repr__()
Tiago Peixoto's avatar
Tiago Peixoto committed
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
Vector_string.a = None
Vector_string.get_array = lambda self: None


class GraphView(Graph):
    """
    A view of selected vertices or edges of another graph.

    This class uses shared data from another :class:`~graph_tool.Graph`
    instance, but allows for local filtering of vertices and/or edges, edge
1787
1788
    directionality or reversal. See :ref:`sec_graph_views` for more details and
    examples.
Tiago Peixoto's avatar
Tiago Peixoto committed
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819

    The existence of a :class:`~graph_tool.GraphView` object does not affect the
    original graph, except if the graph view is modified (addition or removal of
    vertices or edges), in which case the modification is directly reflected in
    the original graph (and vice-versa), since they both point to the same
    underlying data. Because of this, instances of
    :class:`~graph_tool.PropertyMap` can be used interchangeably with a graph
    and its views.

    The argument ``g`` must be an instance of a :class:`~graph_tool.Graph`
    class. If specified, ``vfilt`` and ``efilt`` select which vertices and edges
    are filtered, respectively. These parameters can either be a
    boolean-valued :class:`~graph_tool.PropertyMap` or a
    :class:`~numpy.ndarray`, which specify which vertices/edges are selected, or
    an unary function which returns ``True`` if a given vertex/edge is to be
    selected, or ``False`` otherwise.

    The boolean parameter ``directed`` can be used to set the directionality of
    the graph view. If ``directed = None``, the directionality is inherited from
    ``g``.

    If ``reversed = True``, the direction of the edges is reversed.

    If ``vfilt`` or ``efilt`` is anything other than a
    :class:`~graph_tool.PropertyMap` instance, the instantiation running time is
    :math:`O(V)` and :math:`O(E)`, respectively. Otherwise, the running time is
    :math:`O(1)`.
    """

    def __init__(self, g, vfilt=None, efilt=None, directed=None,
                 reversed=False):
1820
        self.__base = g if not isinstance(g, GraphView) else g.base
Tiago Peixoto's avatar
Tiago Peixoto committed
1821
        Graph.__init__(self)
1822
        # copy graph reference
Tiago Peixoto's avatar
Tiago Peixoto committed
1823
1824
1825
1826
        self._Graph__graph = libcore.GraphInterface(g._Graph__graph, True)
        self._Graph__properties = g._Graph__properties
        self._Graph__known_properties = g._Graph__known_properties

1827
1828
1829
1830
1831
1832
1833
1834
        # set already existent filters
        vf = g.get_vertex_filter()
        if vf[0] is not None:
            self.set_vertex_filter(vf[0], vf[1])
        ef = g.get_edge_filter()
        if ef[0] is not None:
            self.set_edge_filter(ef[0], ef[1])

Tiago Peixoto's avatar
Tiago Peixoto committed
1835
1836
1837
1838
1839
1840
        if vfilt is not None:
            if type(vfilt) is PropertyMap:
                self.set_vertex_filter(vfilt)
            else:
                vmap = self.new_vertex_property("bool")
                if issubclass(type(vfilt), numpy.ndarray):