graph_vertex_similarity.cc 5.83 KB
Newer Older
1 2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2006-2019 Tiago de Paula Peixoto <tiago@skewed.de>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
//
// 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/>.

#include <boost/python.hpp>

#include "graph_tool.hh"
#include "graph_vertex_similarity.hh"
#include "numpy_bind.hh"

using namespace std;
using namespace boost;
using namespace graph_tool;

28 29 30 31 32 33
typedef UnityPropertyMap<uint8_t, GraphInterface::edge_t> ecmap_t;
typedef boost::mpl::push_back<edge_scalar_properties, ecmap_t>::type
        weight_props_t;


void get_dice_similarity(GraphInterface& gi, boost::any as, boost::any weight)
34
{
35 36 37
    if (weight.empty())
        weight = ecmap_t();

38
    gt_dispatch<>()
39
        ([&](auto& g, auto& s, auto& w)
40 41
         {
             all_pairs_similarity(g, s,
42
                                  [&](auto u, auto v, auto& mask, auto& w)
43
                                  {
44 45
                                      return dice(u, v, mask, w, g);
                                  }, w);
46
         },
47 48 49
         all_graph_views(), vertex_floating_vector_properties(),
         weight_props_t())
        (gi.get_graph_view(), as, weight);
50 51 52
}

void get_dice_similarity_pairs(GraphInterface& gi, python::object opairs,
53
                               python::object osim, boost::any weight)
54 55 56 57
{
    multi_array_ref<int64_t,2> pairs = get_array<int64_t,2>(opairs);
    multi_array_ref<double,1> sim = get_array<double,1>(osim);

58 59 60
    if (weight.empty())
        weight = ecmap_t();

61
    gt_dispatch<>()
62
        ([&](auto& g, auto w)
63 64
         {
             some_pairs_similarity(g, pairs, sim,
65
                                   [&](auto u, auto v, auto& mask, auto& w)
66
                                   {
67 68
                                       return dice(u, v, mask, w, g);
                                   }, w);
69
         },
70 71
         all_graph_views(), weight_props_t())
        (gi.get_graph_view(), weight);
72 73
}

74
void get_jaccard_similarity(GraphInterface& gi, boost::any as, boost::any weight)
75
{
76 77 78
    if (weight.empty())
        weight = ecmap_t();

79
    gt_dispatch<>()
80
        ([&](auto& g, auto& s, auto w)
81 82
         {
             all_pairs_similarity(g, s,
83
                                  [&](auto u, auto v, auto& mask, auto w)
84
                                  {
85 86
                                      return jaccard(u, v, mask, w, g);
                                  }, w);
87
         },
88 89 90
         all_graph_views(), vertex_floating_vector_properties(),
         weight_props_t())
        (gi.get_graph_view(), as, weight);
91 92 93
}

void get_jaccard_similarity_pairs(GraphInterface& gi, python::object opairs,
94
                                  python::object osim, boost::any weight)
95 96 97 98
{
    multi_array_ref<int64_t,2> pairs = get_array<int64_t,2>(opairs);
    multi_array_ref<double,1> sim = get_array<double,1>(osim);

99 100 101
    if (weight.empty())
        weight = ecmap_t();

102
    gt_dispatch<>()
103
        ([&](auto& g, auto w)
104 105
         {
             some_pairs_similarity(g, pairs, sim,
106
                                   [&](auto u, auto v, auto& mask, auto w)
107
                                   {
108 109
                                       return jaccard(u, v, mask, w, g);
                                   }, w);
110
         },
111 112
         all_graph_views(), weight_props_t())
        (gi.get_graph_view(), weight);
113 114
}

115 116
void get_inv_log_weight_similarity(GraphInterface& gi, boost::any as,
                                   boost::any weight)
117
{
118 119 120
    if (weight.empty())
        weight = ecmap_t();

121
    gt_dispatch<>()
122
        ([&](auto& g, auto& s, auto w)
123 124
         {
             all_pairs_similarity(g, s,
125
                                  [&](auto u, auto v, auto& mask, auto w)
126
                                  {
127 128
                                      return inv_log_weighted(u, v, mask, w, g);
                                  }, w);
129
         },
130 131 132
         all_graph_views(), vertex_floating_vector_properties(),
         weight_props_t())
        (gi.get_graph_view(), as, weight);
133 134 135 136
}

void get_inv_log_weight_similarity_pairs(GraphInterface& gi,
                                         python::object opairs,
137 138
                                         python::object osim,
                                         boost::any weight)
139 140 141 142
{
    multi_array_ref<int64_t,2> pairs = get_array<int64_t,2>(opairs);
    multi_array_ref<double,1> sim = get_array<double,1>(osim);

143 144 145
    if (weight.empty())
        weight = ecmap_t();

146
    gt_dispatch<>()
147
        ([&](auto& g, auto w)
148 149
         {
             some_pairs_similarity(g, pairs, sim,
150
                                   [&](auto u, auto v, auto& mask, auto w)
151
                                   {
152 153
                                       return inv_log_weighted(u, v, mask, w, g);
                                   }, w);
154
         },
155 156
         all_graph_views(), weight_props_t())
        (gi.get_graph_view(), weight);
157 158 159 160 161 162 163 164 165 166 167 168 169
}


void export_vertex_similarity()
{
    python::def("dice_similarity", &get_dice_similarity);
    python::def("dice_similarity_pairs", &get_dice_similarity_pairs);
    python::def("jaccard_similarity", &get_jaccard_similarity);
    python::def("jaccard_similarity_pairs", &get_jaccard_similarity_pairs);
    python::def("inv_log_weight_similarity", &get_inv_log_weight_similarity);
    python::def("inv_log_weight_similarity_pairs",
                &get_inv_log_weight_similarity_pairs);
};