graph_vertex_similarity.cc 4.97 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
//
// 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;

void get_dice_similarity(GraphInterface& gi, boost::any as, bool self_loop)
{
    gt_dispatch<>()
        ([&](auto& g, auto& s)
         {
             all_pairs_similarity(g, s,
                                  [&](auto u, auto v, auto& mask)
                                  {
                                      return dice(u, v, self_loop, mask, g);
                                  });
         },
         all_graph_views(), vertex_floating_vector_properties())
        (gi.get_graph_view(), as);
}

void get_dice_similarity_pairs(GraphInterface& gi, python::object opairs,
                               python::object osim, bool self_loop)
{
    multi_array_ref<int64_t,2> pairs = get_array<int64_t,2>(opairs);
    multi_array_ref<double,1> sim = get_array<double,1>(osim);

    gt_dispatch<>()
        ([&](auto& g)
         {
             some_pairs_similarity(g, pairs, sim,
                                   [&](auto u, auto v, auto& mask)
                                   {
                                       return dice(u, v, self_loop, mask, g);
                                   });
         },
         all_graph_views())
        (gi.get_graph_view());
}

void get_jaccard_similarity(GraphInterface& gi, boost::any as, bool self_loop)
{
    gt_dispatch<>()
        ([&](auto& g, auto& s)
         {
             all_pairs_similarity(g, s,
                                  [&](auto u, auto v, auto& mask)
                                  {
                                      return jaccard(u, v, self_loop, mask, g);
                                  });
         },
         all_graph_views(), vertex_floating_vector_properties())
        (gi.get_graph_view(), as);
}

void get_jaccard_similarity_pairs(GraphInterface& gi, python::object opairs,
                                  python::object osim, bool self_loop)
{
    multi_array_ref<int64_t,2> pairs = get_array<int64_t,2>(opairs);
    multi_array_ref<double,1> sim = get_array<double,1>(osim);

    gt_dispatch<>()
        ([&](auto& g)
         {
             some_pairs_similarity(g, pairs, sim,
                                   [&](auto u, auto v, auto& mask)
                                   {
                                       return jaccard(u, v, self_loop, mask, g);
                                   });
         },
         all_graph_views())
        (gi.get_graph_view());
}

void get_inv_log_weight_similarity(GraphInterface& gi, boost::any as)
{
    gt_dispatch<>()
        ([&](auto& g, auto& s)
         {
             all_pairs_similarity(g, s,
                                  [&](auto u, auto v, auto& mask)
                                  {
                                      return inv_log_weighted(u, v, mask, g);
                                  });
         },
         all_graph_views(), vertex_floating_vector_properties())
        (gi.get_graph_view(), as);
}

void get_inv_log_weight_similarity_pairs(GraphInterface& gi,
                                         python::object opairs,
                                         python::object osim)
{
    multi_array_ref<int64_t,2> pairs = get_array<int64_t,2>(opairs);
    multi_array_ref<double,1> sim = get_array<double,1>(osim);

    gt_dispatch<>()
        ([&](auto& g)
         {
             some_pairs_similarity(g, pairs, sim,
                                   [&](auto u, auto v, auto& mask)
                                   {
                                       return inv_log_weighted(u, v,  mask, g);
                                   });
         },
         all_graph_views())
        (gi.get_graph_view());
}


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);
};