__init__.py 2.63 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#! /usr/bin/env python
# graph_tool.py -- a general graph manipulation python module
#
# Copyright (C) 2007 Tiago de Paula Peixoto <tiago@forked.de>
#
# 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/>.

import sys
# RTLD_GLOBAL needs to be set in dlopen() if we want typeinfo and friends to
# work properly across DSO boundaries. See http://gcc.gnu.org/faq.html#dso

# The "except" is because the dl module raises a system error on ia64 and x86_64
# systems because "int" and addresses are different sizes.
try:
    from dl import RTLD_LAZY, RTLD_NOW, RTLD_GLOBAL
except ImportError:
    RTLD_LAZY = 1
    RTLD_NOW = 2
    RTLD_GLOBAL = 256
_orig_dlopen_flags = sys.getdlopenflags()

sys.setdlopenflags(RTLD_LAZY|RTLD_GLOBAL)
import libgraph_tool_stats
sys.setdlopenflags(_orig_dlopen_flags) # reset it to normal case to avoid
                                       # unnecessary symbol collision

38
from .. core import _degree, _prop
Tiago Peixoto's avatar
Tiago Peixoto committed
39
40
41
42
43
44
45
from numpy import *

__all__ = ["vertex_hist", "edge_hist", "label_components",
           "label_parallel_edges",  "label_self_loops"]

def vertex_hist(g, deg, bins=[1]):
    ret = libgraph_tool_stats.\
46
          get_vertex_histogram(g._Graph__graph, _degree(g, deg), bins)
47
    return [array(ret[0]), ret[1]]
Tiago Peixoto's avatar
Tiago Peixoto committed
48
49
50

def edge_hist(g, eprop, bins=[1]):
    ret = libgraph_tool_stats.\
51
          get_edge_histogram(g._Graph__graph, _prop("e", g, eprop), bins)
52
    return [array(ret[0]), ret[1]]
Tiago Peixoto's avatar
Tiago Peixoto committed
53

54
55
56
def label_components(g, vprop=None):
    if vprop == None:
        vprop = g.new_vertex_property("int32_t")
Tiago Peixoto's avatar
Tiago Peixoto committed
57
    libgraph_tool_stats.\
58
          label_components(g._Graph__graph, _prop("v", g, vprop))
59
    return _prop("v", g, vprop)
Tiago Peixoto's avatar
Tiago Peixoto committed
60
61

def label_parallel_edges(g, eprop):
62
63
    if eprop == None:
        eprop = g.new_edge_property("int32_t")
Tiago Peixoto's avatar
Tiago Peixoto committed
64
    libgraph_tool_stats.\
65
          label_parallel_edges(g._Graph__graph, _prop("e", g, eprop))
66
    return _prop("e", g, eprop)
Tiago Peixoto's avatar
Tiago Peixoto committed
67
68

def label_self_loops(g, eprop):
69
70
    if eprop == None:
        eprop = g.new_edge_property("int32_t")
Tiago Peixoto's avatar
Tiago Peixoto committed
71
    libgraph_tool_stats.\
72
          label_self_loops(g._Graph__graph, _prop("e", g, eprop))
73
    return _prop("e", g, eprop)