__init__.py 2.94 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
__all__ = ["vertex_hist", "edge_hist", "vertex_average", "edge_average",
26
27
           "label_components", "label_parallel_edges", "remove_parallel_edges",
           "label_self_loops", "remove_self_loops", "remove_labeled_edges"]
Tiago Peixoto's avatar
Tiago Peixoto committed
28

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

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

39
40
41
42
43
44
45
46
47
48
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

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

56
57
58
59
60
61
62
def remove_labeled_edges(g, label):
    g.stash_filter(all=False, directed=True, reversed=True)
    libgraph_tool_stats.\
          remove_labeled_edges(g._Graph__graph, _prop("e", g, label))
    g.pop_filter(all=False, directed=True, reversed=True)

def label_parallel_edges(g, eprop=None):
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_parallel_edges(g._Graph__graph, _prop("e", g, eprop))
67
    return eprop
Tiago Peixoto's avatar
Tiago Peixoto committed
68

69
70
71
72
73
def remove_parallel_edges(g):
    eprop = label_parallel_edges(g)
    remove_labeled_edges(g, eprop)

def label_self_loops(g, eprop=None):
74
75
    if eprop == None:
        eprop = g.new_edge_property("int32_t")
Tiago Peixoto's avatar
Tiago Peixoto committed
76
    libgraph_tool_stats.\
77
          label_self_loops(g._Graph__graph, _prop("e", g, eprop))
78
    return eprop
79
80
81
82
83

def remove_self_loops(g):
    eprop = label_self_loops(g)
    remove_labeled_edges(g, eprop)