__init__.py 141 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) 2006-2019 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
graph_tool - efficient graph analysis and manipulation
======================================================

Summary
-------

.. autosummary::
   :nosignatures:

   Graph
   GraphView
   Vertex
   Edge
35 36 37
   VertexPropertyMap
   EdgePropertyMap
   GraphPropertyMap
Tiago Peixoto's avatar
Tiago Peixoto committed
38 39 40
   PropertyMap
   PropertyArray
   load_graph
41
   load_graph_from_csv
Tiago Peixoto's avatar
Tiago Peixoto committed
42 43
   group_vector_property
   ungroup_vector_property
44
   map_property_values
45
   infect_vertex_property
46
   edge_endpoint_property
47
   incident_edges_op
48
   perfect_prop_hash
Tiago Peixoto's avatar
Tiago Peixoto committed
49
   value_types
50 51 52 53 54
   openmp_enabled
   openmp_get_num_threads
   openmp_set_num_threads
   openmp_get_schedule
   openmp_set_schedule
Tiago Peixoto's avatar
Tiago Peixoto committed
55 56
   show_config

Tiago Peixoto's avatar
Tiago Peixoto committed
57 58

This module provides:
59

60
   1. A :class:`~graph_tool.Graph` class for graph representation and manipulation
61 62
   2. Property maps for Vertex, Edge or Graph.
   3. Fast algorithms implemented in C++.
63

64 65
How to use the documentation
----------------------------
66

67 68
Documentation is available in two forms: docstrings provided
with the code, and the full documentation available in
Tiago Peixoto's avatar
Tiago Peixoto committed
69
`the graph-tool homepage <http://graph-tool.skewed.de>`_.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

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
88 89
Contents
--------
90
"""
91

92
from __future__ import division, absolute_import, print_function
93 94 95
import sys
if sys.version_info < (3,):
    range = xrange
96 97
else:
    unicode = str
98

Tiago Peixoto's avatar
Tiago Peixoto committed
99
__author__ = "Tiago de Paula Peixoto <tiago@skewed.de>"
Tiago Peixoto's avatar
Tiago Peixoto committed
100
__copyright__ = "Copyright 2006-2019 Tiago de Paula Peixoto"
Tiago Peixoto's avatar
Tiago Peixoto committed
101
__license__ = "GPL version 3 or above"
Tiago Peixoto's avatar
Tiago Peixoto committed
102
__URL__ = "http://graph-tool.skewed.de"
103

104 105 106 107
# import numpy and scipy before everything to avoid weird segmentation faults
# depending on the order things are imported.

import numpy
108
import numpy.ma
109 110 111
import scipy
import scipy.stats

112 113 114

from .dl_import import *
dl_import("from . import libgraph_tool_core as libcore")
115 116
__version__ = libcore.mod_info().version

Tiago Peixoto's avatar
Tiago Peixoto committed
117
from . import gt_io  # sets up libcore io routines
118 119 120 121 122

import sys
import os
import re
import gzip
123 124 125 126 127
import bz2
try:
    import lzma
except ImportError:
    pass
128 129
import weakref
import copy
130
import textwrap
Tiago Peixoto's avatar
Tiago Peixoto committed
131
import io
132
import collections
133 134
import itertools
import csv
Tiago Peixoto's avatar
Tiago Peixoto committed
135 136 137

if sys.version_info < (3,):
    import StringIO
138 139 140 141 142 143 144
    def _to_str(x):
        if isinstance(x, unicode):
            return x.encode("utf8")
        return x
else:
    def _to_str(x):
        return x
145

146
from .decorators import _require, _attrs, _limit_args, _copy_func
147
from inspect import ismethod
Tiago Peixoto's avatar
Tiago Peixoto committed
148

149 150
__all__ = ["Graph", "GraphView", "Vertex", "Edge", "VertexBase", "EdgeBase",
           "Vector_bool", "Vector_int16_t", "Vector_int32_t", "Vector_int64_t",
151
           "Vector_double", "Vector_long_double", "Vector_string",
Tiago Peixoto's avatar
Tiago Peixoto committed
152 153 154 155 156 157 158 159
           "Vector_size_t", "Vector_cdouble", "value_types", "load_graph",
           "load_graph_from_csv", "VertexPropertyMap", "EdgePropertyMap",
           "GraphPropertyMap", "PropertyMap", "PropertyArray",
           "group_vector_property", "ungroup_vector_property",
           "map_property_values", "infect_vertex_property",
           "edge_endpoint_property", "incident_edges_op", "perfect_prop_hash",
           "seed_rng", "show_config", "openmp_enabled",
           "openmp_get_num_threads", "openmp_set_num_threads",
160 161
           "openmp_get_schedule", "openmp_set_schedule", "__author__",
           "__copyright__", "__URL__", "__version__"]
162

Tiago Peixoto's avatar
Tiago Peixoto committed
163 164
# this is rather pointless, but it works around a sphinx bug
graph_tool = sys.modules[__name__]
165 166 167 168 169 170 171 172 173

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


def _prop(t, g, prop):
    """Return either a property map, or an internal property map with a given
    name."""
174
    if isinstance(prop, (str, unicode)):
175 176 177 178 179 180 181 182
        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
183
    if pmap is None:
184
        return libcore.any()
185 186 187 188
    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()]))
189 190 191 192 193 194
    u = pmap.get_graph()
    if u is None:
        raise ValueError("Received orphaned property map")
    if g.base is not u.base:
        raise ValueError("Received property map for graph %s (base: %s), expected: %s (base: %s)" %
                         (str(g), str(g.base), str(u), str(u.base)))
195
    return pmap._get_any()
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215


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",
216
             "short": "int16_t",
217
             "int": "int32_t",
Tiago Peixoto's avatar
Tiago Peixoto committed
218
             "unsigned int": "int32_t",
219 220
             "long": "int64_t",
             "long long": "int64_t",
221
             "unsigned long": "int64_t",
222 223 224 225
             "object": "python::object",
             "float": "double"}
    if type_name in alias:
        return alias[type_name]
226
    if type_name in value_types():
227
        return _to_str(type_name)
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    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
248
    if type_name == "string":
249 250 251
        return str
    return object

252
def _gt_type(obj):
253 254 255 256 257
    if isinstance(obj, numpy.dtype):
        t = obj.type
    else:
        t = type(obj)
    if issubclass(t, (numpy.int16, numpy.uint16, numpy.int8, numpy.uint8)):
258
        return "int16_t"
259
    if issubclass(t, (int, numpy.int32, numpy.uint32)):
260
        return "int32_t"
261
    if issubclass(t, (numpy.longlong, numpy.uint64, numpy.int64)):
262
        return "int64_t"
263
    if issubclass(t, (float, numpy.float, numpy.float16, numpy.float32, numpy.float64)):
264
        return "double"
265
    if issubclass(t, numpy.float128):
266
        return "long double"
267
    if issubclass(t, (str, unicode)):
268
        return "string"
269
    if issubclass(t, bool):
270
        return "bool"
271
    if issubclass(t, (list, numpy.ndarray)):
272 273 274
        return "vector<%s>" % _gt_type(obj[0])
    return "object"

275
def _converter(val_type):
276 277
    # attempt to convert to a compatible python type. This is useful,
    # for instance, when dealing with numpy types.
278
    vtype = _python_type(val_type)
279
    if type(vtype) is tuple:
280 281 282 283 284
        def convert(val):
            return [vtype[1](x) for x in val]
    elif vtype is object:
        def convert(val):
            return val
285 286
    elif vtype is str:
        return _c_str
287 288 289 290
    else:
        def convert(val):
            return vtype(val)
    return convert
291

292 293 294
def show_config():
    """Show ``graph_tool`` build configuration."""
    info = libcore.mod_info()
295 296 297 298 299 300 301 302
    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()))
303

304
def terminal_size():
305 306 307 308 309 310 311
    try:
        import fcntl, termios, struct
        h, w, hp, wp = struct.unpack('HHHH',
            fcntl.ioctl(0, termios.TIOCGWINSZ,
            struct.pack('HHHH', 0, 0, 0, 0)))
    except IOError:
        w, h = 80, 100
312 313
    return w, h

314 315 316 317 318
try:
    libcore.mod_info("wrong")
except BaseException as e:
    ArgumentError = type(e)

319 320
# Python 2 vs 3 compatibility

321 322 323 324
if sys.version_info < (3,):
    def _c_str(s):
        if isinstance(s, unicode):
            return s.encode("utf-8")
325
        return str(s)
326 327
    def _str_decode(s):
        return s
328 329
else:
    def _c_str(s):
330
        return str(s)
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    def _str_decode(s):
        if isinstance(s, bytes):
            return s.decode("utf-8")
        return s

def get_bytes_io(buf=None):
    """We want BytesIO for python 3, but StringIO for python 2."""
    if sys.version_info < (3,):
        return StringIO.StringIO(buf)
    else:
        return io.BytesIO(buf)

def conv_pickle_state(state):
    """State keys may be of type `bytes` if python 3 is being used, but state was
    pickled with python 2."""

    if sys.version_info >= (3,):
        keys = [k for k in state.keys() if type(k) is bytes]
        for k in keys:
            state[k.decode("utf-8")] = state[k]
            del state[k]

353

354 355 356 357 358
################################################################################
# Property Maps
################################################################################

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

362 363 364
    See :ref:`sec_property_maps` for more details.

    The possible property value types are listed below.
365 366 367 368 369 370 371

    .. table::

        =======================     ======================
         Type name                  Alias
        =======================     ======================
        ``bool``                    ``uint8_t``
372
        ``int16_t``                 ``short``
373 374 375 376 377 378
        ``int32_t``                 ``int``
        ``int64_t``                 ``long``, ``long long``
        ``double``                  ``float``
        ``long double``
        ``string``
        ``vector<bool>``            ``vector<uint8_t>``
379
        ``vector<int16_t>``         ``short``
380 381 382 383 384 385 386 387
        ``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``
        =======================     ======================
    """
388
    def __init__(self, pmap, g, key_type):
389 390
        if type(self) == PropertyMap:
            raise Exception("PropertyMap cannot be instantiated directly")
391 392
        self.__map = pmap
        self.__g = weakref.ref(g)
393 394 395
        self.__base_g = weakref.ref(g.base)  # keep reference to the
                                             # base graph, in case the
                                             # graph view is deleted.
396
        self.__key_type = key_type
397
        self.__convert = _converter(self.value_type())
398 399
        self.__register_map()

400 401 402 403 404 405 406 407 408 409 410 411
    def _get_any(self):
        t = self.key_type()
        g = self.get_graph()
        if t == "v":
            N = g.num_vertices(True)
        elif t == "e":
            N = g.edge_index_range
        else:
            N = 1
        self.reserve(N)
        return self.__map.get_map()

412
    def __register_map(self):
413 414
        for g in [self.__g(), self.__base_g()]:
            if g is not None:
415
                g._Graph__known_properties[id(self)] = weakref.ref(self)
416 417

    def __unregister_map(self):
418
        for g in [self.__g(), self.__base_g()]:
419
            if g is not None and id(self) in g._Graph__known_properties:
420
                del g._Graph__known_properties[id(self)]
421 422

    def __del__(self):
423 424
        if type(self) != PropertyMap:
            self.__unregister_map()
425 426 427 428 429 430 431 432 433

    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"
434
        g = self.get_graph()
435
        if g is None:
436 437 438
            g = "a non-existent graph"
        else:
            g = "Graph 0x%x" % id(g)
439
        return ("<%sPropertyMap object with value type '%s',"
440
                + " for %s, at 0x%x>") % (k, self.value_type(), g, id(self))
441

442 443 444 445 446 447 448 449 450
    def copy(self, value_type=None, full=True):
        """Return a copy of the property map. If ``value_type`` is specified, the value
        type is converted to the chosen type. If ``full == False``, in the case
        of filtered graphs only the unmasked values are copied (with the
        remaining ones taking the type-dependent default value).

        """
        return self.get_graph().copy_property(self, value_type=value_type,
                                              full=full)
451

452 453 454
    def __copy__(self):
        return self.copy()

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
    def __deepcopy__(self, memo):
        if self.value_type() != "python::object":
            return self.copy()
        else:
            pmap = self.copy()
            g = self.get_graph()
            if self.key_type() == "g":
                iters = [g]
            elif self.key_type() == "v":
                iters = g.vertices()
            else:
                iters = g.edges()
            for v in iters:
                pmap[v] = copy.deepcopy(self[v], memo)
            return pmap

471 472
    def get_graph(self):
        """Get the graph class to which the map refers."""
473 474 475 476
        g = self.__g()
        if g is None:
            g = self.__base_g()
        return g
477 478 479 480 481 482 483 484 485 486 487 488 489 490

    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):
491 492
        """Get a :class:`numpy.ndarray` subclass (:class:`~graph_tool.PropertyArray`)
        with the property values.
493

494 495 496 497
        .. 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
498 499 500
           instead. For vector and string objects, indirect array access is
           provided via the :func:`~graph_tool.PropertyMap.get_2d_array()` and
           :func:`~graph_tool.PropertyMap.set_2d_array()` member functions.
501 502

        .. warning::
503 504 505

           The returned array does not own the data, which belongs to the
           property map. Therefore, if the graph changes, the array may become
506 507 508
           *invalid*. Do **not** store the array if the graph is to be modified;
           store a **copy** instead.

509
        """
510
        return self._get_data()
511 512

    def __set_array(self, v):
513
        a = self.get_array()
514 515 516
        if a is None:
            raise TypeError("cannot set property map values from array for" +
                            " property map of type: " + self.value_type())
517
        a[:] = v
518

519
    a = property(get_array, __set_array,
520
                 doc=r"""Shortcut to the :meth:`~PropertyMap.get_array` method
521 522 523 524
                 as an attribute. This makes assignments more convenient, e.g.:

                 >>> g = gt.Graph()
                 >>> g.add_vertex(10)
Tiago Peixoto's avatar
Tiago Peixoto committed
525
                 <...>
526 527 528 529 530 531 532 533 534 535 536 537
                 >>> 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()
538
            N = g.num_vertices()
539 540
        elif self.__key_type == 'e':
            filt = g.get_edge_filter()
541 542
            if g.get_vertex_filter()[0] is not None:
                filt = (g.new_edge_property("bool"), filt[1])
543
                libcore.mark_edges(g._Graph__graph, _prop("e", g, filt[0]))
544
                if filt[1]:
545
                    filt[0].a = numpy.logical_not(filt[0].a)
546
            elif g.edge_index_range != g.num_edges():
547
                filt = (g.new_edge_property("bool"), False)
548
                libcore.mark_edges(g._Graph__graph, _prop("e", g, filt[0]))
549
            if filt[0] is None:
550
                N = g.edge_index_range
551 552
            else:
                N = (filt[0].a == (not filt[1])).sum()
553
        if get:
554 555 556 557
            if a is None:
                return a
            if filt[0] is None:
                return a
558
            return a[filt[0].a == (not filt[1])][:N]
559
        else:
560
            if a is None:
561 562
                raise TypeError("cannot set property map values from array for" +
                                " property map of type: " + self.value_type())
563
            if filt[0] is None:
564 565 566 567
                try:
                    a[:] = v
                except ValueError:
                    a[:] = v[:len(a)]
568
            else:
569
                m = filt[0].a == (not filt[1])
570
                m *= m.cumsum() <= N
571 572 573 574
                try:
                    a[m] = v
                except ValueError:
                    a[m] = v[:len(m)][m]
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598

    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()
599 600
            if g.get_vertex_filter()[0] is not None:
                filt = (g.new_edge_property("bool"), filt[1])
601
                libcore.mark_edges(g._Graph__graph, _prop("e", g, filt[0]))
602 603
                if filt[1]:
                    filt[0].a = 1 - filt[0].a
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
        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
621
                  :class:`~graph_tool.PropertyArray` is returned, which
622
                  is identical to the :attr:`~PropertyMap.a` attribute.""")
623

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
    def get_2d_array(self, pos):
        r"""Return a two-dimensional array with a copy of the entries of the
        vector-valued property map. The parameter ``pos`` must be a sequence of
        integers which specifies the indexes of the property values which will
        be used. """

        if self.key_type() == "g":
            raise ValueError("Cannot create multidimensional array for graph property maps.")
        if "vector" not in self.value_type() and (len(pos) > 1 or pos[0] != 0):
            raise ValueError("Cannot create array of dimension %d (indexes %s) from non-vector property map of type '%s'." \
                             % (len(pos), str(pos), self.value_type()))
        if "string" in self.value_type():
            if "vector" in self.value_type():
                p = ungroup_vector_property(self, pos)
            else:
                p = [self]
            g = self.get_graph()
641 642
            vfilt = g.get_vertex_filter()
            efilt = g.get_edge_filter()
643 644 645
            if self.key_type() == "v":
                iters = g.vertices()
            else:
646 647 648 649 650 651
                iters = [None for i in range(g.edge_index_range)]
                idx = g.edge_index
                for e in g.edges():
                    iters[idx[e]] = e
                iters = [e for e in iters if e is not None]
            a = [[] for i in range(len(p))]
652 653
            for v in iters:
                for i in range(len(p)):
654
                    a[i].append(p[i][v])
655 656
            a = numpy.array(a)
            return a
657

658 659
        p = ungroup_vector_property(self, pos)
        a = numpy.array([x.fa for x in p])
660
        return a
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680

    def set_2d_array(self, a, pos=None):
        r"""Set the entries of the vector-valued property map from a
        two-dimensional array ``a``. If given, the parameter ``pos`` must be a
        sequence of integers which specifies the indexes of the property values
        which will be set."""

        if self.key_type() == "g":
            raise ValueError("Cannot set multidimensional array for graph property maps.")
        if "vector" not in self.value_type():
            if len(a.shape) != 1:
                raise ValueError("Cannot set array of shape %s to non-vector property map of type %s" % \
                                 (str(a.shape), self.value_type()))
            if self.value_type() != "string":
                self.fa = a
            else:
                g = self.get_graph()
                if self.key_type() == "v":
                    iters = g.vertices()
                else:
681 682 683 684 685
                    iters = [None for i in range(g.edge_index_range)]
                    idx = g.edge_index
                    for e in g.edges():
                        iters[idx[e]] = e
                    iters = [e for e in iters if e is not None]
686 687
                for j, v in enumerate(iters):
                    self[v] = a[j]
688 689 690 691 692 693 694 695 696 697 698 699 700
            return

        val = self.value_type()[7:-1]
        ps = []
        for i in range(a.shape[0]):
            ps.append(self.get_graph().new_property(self.key_type(), val))
            if self.value_type() != "string":
                ps[-1].fa = a[i]
            else:
                g = self.get_graph()
                if self.key_type() == "v":
                    iters = g.vertices()
                else:
701 702 703 704 705
                    iters = [None for i in range(g.edge_index_range)]
                    idx = g.edge_index
                    for e in g.edges():
                        iters[idx[e]] = e
                    iters = [e for e in iters if e is not None]
706 707 708 709
                for j, v in enumerate(iters):
                    ps[-1][v] = a[i, j]
        group_vector_property(ps, val, self, pos)

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

714 715 716 717

    def set_value(self, val):
        """Sets all values in the property map to ``val``."""
        g = self.get_graph()
718
        val = self.__convert(val)
719
        if self.key_type() == "v":
720
            libcore.set_vertex_property(g._Graph__graph, _prop("v", g, self), val)
721
        elif self.key_type() == "e":
722
            libcore.set_edge_property(g._Graph__graph, _prop("e", g, self), val)
723 724 725
        else:
            self[g] = val

726 727
    def reserve(self, size):
        """Reserve enough space for ``size`` elements in underlying container. If the
728
           original size is already equal or larger, nothing will happen."""
729 730
        self.__map.reserve(size)

731 732 733 734
    def resize(self, size):
        """Resize the underlying container to contain exactly ``size`` elements."""
        self.__map.resize(size)

735 736 737 738 739 740 741
    def shrink_to_fit(self):
        """Shrink size of underlying container to accommodate only the necessary amount,
        and thus potentially freeing memory."""
        g = self.get_graph()
        if self.key_type() == "v":
            size = g.num_vertices(True)
        elif self.key_type() == "e":
742
            size = g.edge_index_range
743 744 745 746 747
        else:
            size = 1
        self.__map.resize(size)
        self.__map.shrink_to_fit()

Tiago Peixoto's avatar
Tiago Peixoto committed
748 749 750 751 752 753 754 755
    def swap(self, other):
        """Swap internal storage with ``other``."""
        if self.key_type() != other.key_type():
            raise ValueError("property maps must have the same key type")
        if self.value_type() != other.value_type():
            raise ValueError("property maps must have the same value type")
        self.__map.swap(other.__map)

756 757 758 759
    def data_ptr(self):
        """Return the pointer to memory where the data resides."""
        return self.__map.data_ptr()

760 761
    def __getstate__(self):
        g = self.get_graph()
762 763
        if g is None:
            raise ValueError("cannot pickle orphaned property map")
764 765 766 767 768 769 770
        value_type = self.value_type()
        key_type = self.key_type()
        if not self.is_writable():
            vals = None
        else:
            u = GraphView(g, skip_vfilt=True, skip_efilt=True)
            if key_type == "v":
771
                vals = [self.__convert(self[v]) for v in u.vertices()]
772
            elif key_type == "e":
773
                vals = [self.__convert(self[e]) for e in u.edges()]
774
            else:
775
                vals = self.__convert(self[g])
776 777 778 779 780 781 782 783 784

        state = dict(g=g, value_type=value_type,
                     key_type=key_type, vals=vals,
                     is_vindex=self is g.vertex_index,
                     is_eindex=self is g.edge_index)

        return state

    def __setstate__(self, state):
785
        conv_pickle_state(state)
786
        g = state["g"]
787 788
        key_type = _str_decode(state["key_type"])
        value_type = _str_decode(state["value_type"])
789 790 791 792 793 794 795 796 797
        vals = state["vals"]

        if state["is_vindex"]:
            pmap = g.vertex_index
        elif state["is_eindex"]:
            pmap = g.edge_index
        else:
            u = GraphView(g, skip_vfilt=True, skip_efilt=True)
            if key_type == "v":
798
                pmap = u.new_vertex_property(value_type, vals=vals)
799
            elif key_type == "e":
800
                pmap = u.new_edge_property(value_type, vals=vals)
801
            else:
802 803 804
                pmap = u.new_graph_property(value_type)
                pmap[u] = vals
            pmap = g.own_property(pmap)
805 806 807 808 809

        self.__map = pmap.__map
        self.__g = pmap.__g
        self.__base_g = pmap.__base_g
        self.__key_type = key_type
Tiago Peixoto's avatar
Tiago Peixoto committed
810
        self.__convert = _converter(self.value_type())
811 812
        self.__register_map()

813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 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 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 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 949
    # Guarantee pickle backward compatibility

    def __getitem__(self, k):
        if self.key_type() == "v":
            return VertexPropertyMap.__getitem__(self, k)
        if self.key_type() == "e":
            return EdgePropertyMap.__getitem__(self, k)
        if self.key_type() == "g":
            return GraphPropertyMap.__getitem__(self, k)

    def __setitem__(self, k, v):
        if self.key_type() == "v":
            VertexPropertyMap.__setitem__(self, k, v)
        if self.key_type() == "e":
            EdgePropertyMap.__setitem__(self, k, v)
        if self.key_type() == "g":
            GraphPropertyMap.__setitem__(self, k, v)

    def __iter__(self):
        if self.key_type() == "v":
            return VertexPropertyMap.__iter__(self)
        if self.key_type() == "e":
            return EdgePropertyMap.__iter__(self)
        if self.key_type() == "g":
            return GraphPropertyMap.__iter__(self)

    def _get_data(self):
        if self.key_type() == "v":
            return VertexPropertyMap._get_data(self, k)
        if self.key_type() == "e":
            return EdgePropertyMap._get_data(self, k)
        if self.key_type() == "g":
            return GraphPropertyMap._get_data(self, k)


class VertexPropertyMap(PropertyMap):
    """This class provides a mapping from vertices to arbitrary properties.

    See :ref:`sec_property_maps` and :class:`PropertyMap` for more details.

    """

    def __init__(self, pmap, g):
        PropertyMap.__init__(self, pmap, g, "v")

    def __getitem__(self, k):
        return self._PropertyMap__map[int(k)]

    def __setitem__(self, k, v):
        k = int(k)
        try:
            self._PropertyMap__map[k] = v
        except TypeError:
            self._PropertyMap__map[k] = self._PropertyMap__convert(v)

    def __iter__(self):
        g = self.get_graph()
        for v in g.vertices():
            yield self[v]

    def _get_data(self):
        g = self.get_graph()
        if g is None:
            raise ValueError("Cannot get array for an orphaned property map")
        n = g._Graph__graph.get_num_vertices(False)
        a = self._PropertyMap__map.get_array(n)
        if a is None:
            return None
        return PropertyArray(a, self)

class EdgePropertyMap(PropertyMap):
    """This class provides a mapping from edges to arbitrary properties.

    See :ref:`sec_property_maps` and :class:`PropertyMap` for more details.

    """

    def __init__(self, pmap, g):
        PropertyMap.__init__(self, pmap, g, "e")

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

    def __setitem__(self, k, v):
        try:
            self._PropertyMap__map[k] = v
        except TypeError:
            self._PropertyMap__map[k] = self.__convert(v)

    def __iter__(self):
        g = self.get_graph()
        for e in g.edges():
            yield self[e]

    def _get_data(self):
        g = self.get_graph()
        if g is None:
            raise ValueError("Cannot get array for an orphaned property map")
        n = g.edge_index_range
        a = self._PropertyMap__map.get_array(n)
        if a is None:
            return None
        return PropertyArray(a, self)

class GraphPropertyMap(PropertyMap):
    """This class provides a mapping from graphs to arbitrary properties.

    See :ref:`sec_property_maps` and :class:`PropertyMap` for more details.

    """

    def __init__(self, pmap, g):
        PropertyMap.__init__(self, pmap, g, "g")

    def __getitem__(self, k):
        return self._PropertyMap__map[self.get_graph()._Graph__graph]

    def __setitem__(self, k, v):
        g = self.get_graph()._Graph__graph
        try:
            self._PropertyMap__map[g] = v
        except TypeError:
            self._PropertyMap__map[g] = self._PropertyMap__convert(v)

    def __iter__(self):
        return [self.get_graph()]

    def _get_data(self):
        g = self.get_graph()
        if g is None:
            raise ValueError("Cannot get array for an orphaned property map")
        a = self.__map.get_array(1)
        if a is None:
            return None
        return PropertyArray(a, self)


950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
class PropertyArray(numpy.ndarray):
    """This is a :class:`~numpy.ndarray` subclass which keeps a reference of its
    :class:`~graph_tool.PropertyMap` owner.
    """

    __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
        return obj
970 971 972 973

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


def _check_prop_scalar(prop, name=None, floating=False):
978
    scalars = ["bool", "int16_t", "int32_t", "int64_t", "unsigned long",
979 980 981 982 983 984
               "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." %\
985
                         (((" '%s'" % name) if name is not None else ""),
986 987 988 989
                          (" floating" if floating else "")))


def _check_prop_vector(prop, name=None, scalar=True, floating=False):
990
    scalars = ["bool", "int16_t", "int32_t", "int64_t", "unsigned long",
991 992 993 994 995 996 997 998
               "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." %\
999
                         (((" '%s'" % name) if name is not None else ""),
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
                          (" 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``.
1023 1024 1025 1026 1027 1028

    Examples
    --------
    >>> from numpy.random import seed, randint
    >>> from numpy import array
    >>> seed(42)
Tiago Peixoto's avatar
Tiago Peixoto committed
1029
    >>> gt.seed_rng(42)
1030
    >>> g = gt.random_graph(100, lambda: (3, 3))
1031 1032
    >>> props = [g.new_vertex_property("int") for i in range(3)]
    >>> for i in range(3):
1033 1034
    ...    props[i].a = randint(0, 100, g.num_vertices())
    >>> gprop = gt.group_vector_property(props)
1035
    >>> print(gprop[g.vertex(0)].a)
Tiago Peixoto's avatar
Tiago Peixoto committed
1036
    [51 25  8]
1037
    >>> print(array([p[g.vertex(0)] for p in props]))
Tiago Peixoto's avatar
Tiago Peixoto committed
1038
    [51 25  8]
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
    """
    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()

1053 1054
    if vprop is None:
        if value_type is None and len(vtypes) == 1:
1055 1056
            value_type = vtypes.pop()

1057
        if value_type is not None:
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
            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":
1072 1073
            u = GraphView(g, directed=True, reversed=g.is_reversed(),
                          skip_properties=True)
1074
            libcore.group_vector_property(u._Graph__graph, _prop(k, g, vprop),
1075
                                          _prop(k, g, p),
1076
                                          i if pos is None else pos[i],
1077 1078
                                          k == 'e')
        else:
1079
            vprop[g][i if pos is None else pos[i]] = p[g]
1080 1081 1082 1083 1084 1085
    return vprop


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

1086
    Parameters
1087 1088 1089
    ----------
    vprop : :class:`~graph_tool.PropertyMap`
        Vector property map to be ungrouped.
1090 1091 1092
    pos : list of ints
        A list of indexes corresponding to where each element of ``vprop``
        should be inserted into the ungrouped list.
1093 1094 1095 1096 1097 1098 1099 1100
    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``.
1101 1102 1103 1104 1105 1106

    Examples
    --------
    >>> from numpy.random import seed, randint
    >>> from numpy import array
    >>> seed(42)
Tiago Peixoto's avatar
Tiago Peixoto committed
1107
    >>> gt.seed_rng(42)
1108 1109 1110 1111 1112
    >>> 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])
1113
    >>> print(prop[g.vertex(0)].a)
Tiago Peixoto's avatar
Tiago Peixoto committed
1114
    [51 92 14]
1115
    >>> print(array([p[g.vertex(0)] for p in uprops]))
Tiago Peixoto's avatar
Tiago Peixoto committed
1116
    [51 92 14]
1117 1118 1119 1120 1121 1122
    """

    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]
1123
    if props is None:
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
        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':
1136 1137 1138
            u = GraphView(g, directed=True, reversed=g.is_reversed(),
                          skip_properties=True)
            libcore.ungroup_vector_property(u._Graph__graph,
1139 1140 1141 1142 1143 1144 1145 1146 1147
                                            _prop(k, g, vprop),
                                            _prop(k, g, props[i]),
                                            p, k == 'e')
        else:
            if len(vprop[g]) <= pos[i]:
                vprop[g].resize(pos[i] + 1)
            props[i][g] = vprop[g][pos[i]]
    return props

1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
def map_property_values(src_prop, tgt_prop, map_func):
    """Map the values of ``src_prop`` to ``tgt_prop`` according to the mapping
    function ``map_func``.

    Parameters
    ----------
    src_prop : :class:`~graph_tool.PropertyMap`
        Source property map.
    tgt_prop : :class:`~graph_tool.PropertyMap`
        Target property map.
    map_func : function or callable object
        Function mapping values of ``src_prop`` to values of ``tgt_prop``.

    Returns
    -------
    None

    Examples
    --------
    >>> g = gt.collection.data["lesmis"]
    >>> label_len = g.new_vertex_property("int64_t")
    >>> gt.map_property_values(g.vp.label, label_len,
Tiago Peixoto's avatar
Tiago Peixoto committed
1170
    ...                        lambda x: len(x))
1171
    >>> print(label_len.a)
Tiago Peixoto's avatar
Tiago Peixoto committed
1172 1173 1174 1175
    [ 6  8 14 11 12  8 12  8  5  6  7  7 10  6  7  7  9  9  7 11  9  6  7  7
     13 10  7  6 12 10  8  8 11  6  5 12  6 10 11  9 12  7  7  6 14  7  9  9
      8 12  6 16 12 11 14  6  9  6  8 10  9  7 10  7  7  4  9 14  9  5 10 12
      9  6  6  6 12]
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
    """

    if src_prop.key_type() != tgt_prop.key_type():
        raise ValueError("src_prop and tgt_prop must be of the same key type")
    g = src_prop.get_graph()
    k = src_prop.key_type()
    if k == "g":
        tgt_prop[g] = map_func(src_prop[g])
        return
    u = GraphView(g, directed=True, reversed=g.is_reversed(),
                  skip_properties=True)
    libcore.property_map_values(u._Graph__graph,
                                _prop(k, g, src_prop),
                                _prop(k, g, tgt_prop),
                                map_func, k == 'e')
1191

1192 1193
def infect_vertex_property(g, prop, vals=None):
    """Propagate the `prop` values of vertices with value `val` to all their
1194
    out-neighbors.
1195 1196 1197

    Parameters
    ----------
1198
    prop : :class:`~graph_tool.VertexPropertyMap`
1199 1200 1201 1202 1203 1204 1205
        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
    -------
1206
    None : ``None``
1207 1208 1209 1210 1211

    Examples
    --------
    >>> from numpy.random import seed
    >>> seed(42)
Tiago Peixoto's avatar
Tiago Peixoto committed
1212
    >>> gt.seed_rng(42)
1213
    >>> g = gt.random_graph(100, lambda: (3, 3))
Tiago Peixoto's avatar
Tiago Peixoto committed
1214
    >>> prop = g.vertex_index.copy("int32_t")
1215
    >>> gt.infect_vertex_property(g, prop, [10])
1216
    >>> print(sum(prop.a == 10))
Tiago Peixoto's avatar
Tiago Peixoto committed
1217
    4
1218 1219 1220 1221
    """
    libcore.infect_vertex_property(g._Graph__graph, _prop("v", g, prop),
                                   vals)

Tiago Peixoto's avatar
Tiago Peixoto committed
1222

1223 1224 1225 1226
@_limit_args({"endpoint": ["source", "target"]})
def edge_endpoint_property(g, prop, endpoint, eprop=None):
    """Return an edge property map corresponding to the vertex property `prop` of
    either the target and source of the edge, according to `endpoint`.
Tiago Peixoto's avatar
Tiago Peixoto committed
1227 1228 1229

    Parameters
    ----------
1230
    prop : :class:`~graph_tool.VertexPropertyMap`
1231 1232 1233 1234
        Vertex property map to be used to propagated to the edge.
    endpoint : `"source"` or `"target"`
        Edge endpoint considered. If the graph is undirected, the source is
        always the vertex with the lowest index.
1235
    eprop : :class:`~graph_tool.EdgePropertyMap` (optional, default: `None`)
1236
        If provided, the resulting edge properties will be stored here.
Tiago Peixoto's avatar
Tiago Peixoto committed
1237 1238 1239

    Returns
    -------
1240
    eprop : :class:`~graph_tool.EdgePropertyMap`
1241
        Propagated edge property.
Tiago Peixoto's avatar
Tiago Peixoto committed
1242 1243 1244

    Examples
    --------
Tiago Peixoto's avatar
Tiago Peixoto committed
1245
    >>> gt.seed_rng(42)
Tiago Peixoto's avatar
Tiago Peixoto committed
1246
    >>> g = gt.random_graph(100, lambda: (3, 3))
1247 1248
    >>> esource = gt.edge_endpoint_property(g, g.vertex_index, "source")
    >>> print(esource.a)
Tiago Peixoto's avatar
Tiago Peixoto committed
1249
    [ 0  0  0 96 96 96 92 92 92 88 88 88 84 84 84 80 80 80 76 76 76 72 72 72
Tiago Peixoto's avatar
Tiago Peixoto committed
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
     68 68 68 64 64 64 60 60 60 56 56 56 52 52 52 48 48 48 44 44 44 40 40 40
     36 36 36 32 32 32 28 28 28 24 24 24 20 20 20 16 16 16 12 12 12  8  8  8
      4  4  4 99 99 99  1  1  1  2  2  2  3  3  3  5  5  5  6  6  6  7  7  7
      9  9  9 10 10 10 14 14 14 19 19 19 25 25 25 30 30 30 35 35 35 41 41 41
     46 46 46 51 51 51 57 57 57 62 62 62 67 67 67 73 73 73 78 78 78 83 83 83
     89 89 89 94 94 94 11 11 11 98 98 98 97 97 97 95 95 95 93 93 93 91 91 91
     90 90 90 87 87 87 86 86 86 85 85 85 82 82 82 81 81 81 79 79 79 77 77 77
     75 75 75 74 74 74 71 71 71 69 69 69 61 61 61 54 54 54 47 47 47 39 39 39
     33 33 33 26 26 26 18 18 18 70 70 70 13 13 13 15 15 15 17 17 17 21 21 21
     22 22 22 23 23 23 27 27 27 29 29 29 31 31 31 34 34 34 37 37 37 38 38 38
     42 42 42 43 43 43 45 45 45 49 49 49 50 50 50 53 53 53 55 55 55 58 58 58
     59 59 59 63 63 63 65 65 65 66 66 66]
Tiago Peixoto's avatar
Tiago Peixoto committed
1262
    """
1263

Tiago Peixoto's avatar
Tiago Peixoto committed
1264
    val_t = prop.value_type()
1265
    if val_t == "unsigned long" or val_t == "unsigned int":
1266 1267 1268 1269 1270
        val_t = "int64_t"
    if eprop is None:
        eprop = g.new_edge_property(val_t)
    if eprop.value_type() != val_t:
        raise ValueError("'eprop' must be of the same value type as 'prop': " +
Tiago Peixoto's avatar
Tiago Peixoto committed
1271
                         val_t)
1272
    libcore.edge_endpoint(g._Graph__graph, _prop("v", g, prop),
1273
                          _prop("e", g, eprop), _to_str(endpoint))
1274
    return eprop
Tiago Peixoto's avatar
Tiago Peixoto committed
1275

1276 1277 1278 1279 1280
@_limit_args({"direction": ["in", "out"], "op": ["sum", "prod", "min", "max"]})
def incident_edges_op(g, direction, op, eprop, vprop=None):
    """Return a vertex property map corresponding to a specific operation (sum,
    product, min or max) on the edge property `eprop` of incident edges on each
    vertex, following the direction given by `direction`.
1281 1282 1283 1284 1285

    Parameters
    ----------
    direction : `"in"` or `"out"`
        Direction of the incident edges.
1286 1287
    op : `"sum"`, `"prod"`, `"min"` or `"max"`
        Operation performed on incident edges.
1288
    eprop : :class:`~graph_tool.EdgePropertyMap`
1289
        Edge property map to be summed.
1290
    vprop : :class:`~graph_tool.VertexPropertyMap` (optional, default: `None`)
1291 1292 1293 1294
        If provided, the resulting vertex properties will be stored here.

    Returns
    -------
1295
    vprop : :class:`~graph_tool.VertexPropertyMap`
1296
        Resulting vertex property.
1297 1298 1299 1300 1301

    Examples
    --------
    >>> gt.seed_rng(42)
    >>> g = gt.random_graph(100, lambda: (3, 3))
1302
    >>> vsum = gt.incident_edges_op(g, "out", "sum", g.edge_index)
1303 1304 1305 1306 1307 1308 1309
    >>> print(vsum.a)
    [  3 237 246 255 219 264 273 282 210 291 300 453 201 687 309 696 192 705
     669 318 183 714 723 732 174 327 660 741 165 750 336 759 156 651 768 345
     147 777 786 642 138 354 795 804 129 813 363 633 120 822 831 372 111 840
     624 849 102 381 858 867  93 615 390 876  84 885 894 399  75 606 678 597
      66 408 588 579  57 570 417 561  48 552 543 426  39 534 525 516  30 435
     507 498  21 489 444 480  12 471 462 228]
1310

1311 1312 1313
    """

    val_t = eprop.value_type()
1314
    if val_t == "unsigned long" or val_t == "unsigned int":
1315 1316 1317
        val_t = "int64_t"
    if vprop is None:
        vprop = g.new_vertex_property(val_t)
1318 1319 1320
    orig_vprop = vprop
    if vprop.value_type != val_t:
        vprop = g.new_vertex_property(val_t)
1321
    if direction == "in" and not g.is_directed():
1322
        return orig_vprop
1323 1324
    if direction == "in":
        g = GraphView(g, reversed=True, skip_properties=True)
1325
    libcore.out_edges_op(g._Graph__graph, _prop("e", g, eprop),
1326
                          _prop("v", g, vprop), _to_str(op))
1327 1328 1329
    if vprop is not orig_vprop:
        g.copy_property(vprop, orig_vprop)
    return orig_vprop
1330

1331 1332 1333
@_limit_args({"htype": ["int8_t", "int32_t", "int64_t"]})
def perfect_prop_hash(props, htype="int32_t"):
    """Given a list of property maps `props` of the same type, a derived list of
1334
    property maps with integral type `htype` is returned, where each value is
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
    replaced by a perfect (i.e. unique) hash value.

    .. note::
       The hash value is deterministic, but it will not be necessarily the same
       for different values of `props`.
    """

    val_types = set([p.value_type() for p in<