__init__.py 2.41 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
#! /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/>.

Tiago Peixoto's avatar
Tiago Peixoto committed
19
20
from .. dl_import import dl_import
dl_import("import libgraph_tool_stats")
Tiago Peixoto's avatar
Tiago Peixoto committed
21

22
from .. core import _degree, _prop
Tiago Peixoto's avatar
Tiago Peixoto committed
23
24
from numpy import *

25
26
__all__ = ["vertex_hist", "edge_hist", "vertex_average", "edge_average",
           "label_components", "label_parallel_edges",  "label_self_loops"]
Tiago Peixoto's avatar
Tiago Peixoto committed
27

28
def vertex_hist(g, deg, bins=[1], float_count=True):
Tiago Peixoto's avatar
Tiago Peixoto committed
29
    ret = libgraph_tool_stats.\
30
          get_vertex_histogram(g._Graph__graph, _degree(g, deg), bins)
31
    return [array(ret[0], dtype="float64") if float_count else ret[0], ret[1]]
Tiago Peixoto's avatar
Tiago Peixoto committed
32

33
def edge_hist(g, eprop, bins=[1], float_count=True):
Tiago Peixoto's avatar
Tiago Peixoto committed
34
    ret = libgraph_tool_stats.\
35
          get_edge_histogram(g._Graph__graph, _prop("e", g, eprop), bins)
36
    return [array(ret[0], dtype="float64") if float_count else ret[0], ret[1]]
Tiago Peixoto's avatar
Tiago Peixoto committed
37

38
39
40
41
42
43
44
45
46
47
def vertex_average(g, deg):
    ret = libgraph_tool_stats.\
          get_vertex_average(g._Graph__graph, _degree(g, deg))
    return ret

def edge_average(g, eprop):
    ret = libgraph_tool_stats.\
          get_edge_average(g._Graph__graph, _prop("e", g, eprop))
    return ret

48
49
50
def label_components(g, vprop=None):
    if vprop == None:
        vprop = g.new_vertex_property("int32_t")
Tiago Peixoto's avatar
Tiago Peixoto committed
51
    libgraph_tool_stats.\
52
          label_components(g._Graph__graph, _prop("v", g, vprop))
53
    return vprop
Tiago Peixoto's avatar
Tiago Peixoto committed
54
55

def label_parallel_edges(g, eprop):
56
57
    if eprop == None:
        eprop = g.new_edge_property("int32_t")
Tiago Peixoto's avatar
Tiago Peixoto committed
58
    libgraph_tool_stats.\
59
          label_parallel_edges(g._Graph__graph, _prop("e", g, eprop))
60
    return eprop
Tiago Peixoto's avatar
Tiago Peixoto committed
61
62

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