__init__.py 3.86 KB
Newer Older
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_correlations
sys.setdlopenflags(_orig_dlopen_flags) # reset it to normal case to avoid
                                       # unnecessary symbol collision

38
from .. core import _degree, _prop
39
40
41
42
43
44
45
from numpy import *

__all__ = ["assortativity", "scalar_assortativity",
           "corr_hist", "combined_corr_hist", "avg_neighbour_corr"]

def assortativity(g, deg):
    return libgraph_tool_correlations.\
46
           assortativity_coefficient(g.underlying_graph(), _degree(g, deg))
47
48
49
50

def scalar_assortativity(g, deg):
    return libgraph_tool_correlations.\
           scalar_assortativity_coefficient(g.underlying_graph(),
51
                                            _degree(g, deg))
52

53
def corr_hist(g, deg1, deg2, bins=[[1],[1]], weight=None):
54
    ret = libgraph_tool_correlations.\
55
56
57
          vertex_correlation_histogram(g.underlying_graph(), _degree(g, deg1),
                                       _degree(g, deg2), _prop("e", g, weight),
                                       bins[0], bins[1])
58
59
60
61
62
    return [ret[0], [ret[1][0], ret[1][1]]]

def combined_corr_hist(g, deg1, deg2, bins=[[1],[1]]):
    ret = libgraph_tool_correlations.\
          vertex_combined_correlation_histogram(g.underlying_graph(),
63
64
                                                _degree(g, deg1),
                                                _degree(g, deg2),
65
66
67
                                                bins[0], bins[1])
    return [ret[0], [ret[1][0], ret[1][1]]]

68
69
def avg_neighbour_corr(g, deg1, deg2, bins=[[1],[1]], weight=None):
    ret = corr_hist(g, deg1, deg2, bins, weight)
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    xbins = ret[1][0]
    ybins = ret[1][1]
    counts = ret[0]
    avg = empty((counts.shape[0]))
    dev = empty((counts.shape[0]))
    mask = empty((counts.shape[0]), dtype=dtype('bool'))
    n_masked = 0
    for i in xrange(0, len(ret[0])):
        N = counts[i,:].sum()
        if N > 0:
            avg[i] = average(ybins, weights=counts[i,:])
            dev[i] = sqrt(average((ybins-avg[i])**2,
                                  weights=counts[i,:]))/sqrt(N)
            mask[i] = False
        else:
            mask[i] = True
            n_masked += 1
    if n_masked > 0: # remove empty bins
        navg = empty(len(avg) - n_masked)
        ndev = empty(len(dev) - n_masked)
        nxbins = empty(len(xbins) - n_masked)
        cum = 0
        for i in xrange(0, len(avg)):
            if not mask[i]:
                navg[i-cum] = avg[i]
                ndev[i-cum] = dev[i]
                nxbins[i-cum] = xbins[i]
            else:
                cum += 1
        avg = navg
        dev = ndev
        xbins = nxbins
    return [avg, dev, xbins]