Commit 780a8055 authored by Tiago Peixoto's avatar Tiago Peixoto

Port graph histograms and misc statistics to new filtering engine

Python binding will follow
parent 0097e2ba
## Process this file with automake to produce Makefile.in
AM_CPPFLAGS =\
-I. -I.. \
-I../boost-workaround \
-DHAVE_CONFIG_H
AM_CFLAGS=$(AM_CXXFLAGS)
libgraph_tool_statsdir = $(pythondir)/graph_tool/stats
libgraph_tool_stats_LTLIBRARIES = libgraph_tool_stats.la
libgraph_tool_stats_la_includedir = $(pythondir)/graph_tool/include
libgraph_tool_stats_la_SOURCES = \
graph_components.cc \
graph_histograms.cc \
graph_parallel.cc \
graph_stats_bind.cc
libgraph_tool_stats_la_include_HEADERS = \
graph_components.hh \
graph_parallel.hh \
graph_histograms.hh
libgraph_tool_stats_la_LIBADD = \
$(PYTHON_LDFLAGS) \
$(BOOST_LDFLAGS) \
$(OPENMP_LDFLAGS) \
-lboost_python \
-lboost_iostreams \
-lexpat
# needed for typeinfo objects to work across DSO boundaries.
# see http://gcc.gnu.org/faq.html#dso
libgraph_tool_stats_la_LDFLAGS = \
-module \
-avoid-version \
-export-dynamic \
-no-undefined \
-Wl,-E
// Copyright (C) 2008 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/>.
#include "graph.hh"
#include "graph_filtering.hh"
#include "graph_properties.hh"
#include "graph_components.hh"
#include <boost/python.hpp>
using namespace std;
using namespace boost;
using namespace graph_tool;
void do_label_components(GraphInterface& gi, const string& prop)
{
typedef property_map_types::apply<scalar_types,
GraphInterface::vertex_index_map_t>::type
vertex_props;
run_action<>()(gi, label_components(), vertex_props())
(vertex_prop(prop, gi));
}
void export_components()
{
python::def("label_components", &do_label_components);
};
// Copyright (C) 2008 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/>.
#ifndef GRAPH_COMPONENTS_HH
#define GRAPH_COMPONENTS_HH
#include <boost/graph/connected_components.hpp>
#include <boost/graph/strong_components.hpp>
namespace graph_tool
{
using namespace std;
using namespace boost;
// this will label the components of a graph to a given vertex property, from
// [0, number of components - 1]. If the graph is directed the "strong
// components" are used.
struct label_components
{
template <class Graph, class CompMap>
void operator()(const Graph* gp, CompMap comp_map) const
{
const Graph& g = *gp;
typedef typename graph_traits<Graph>::directed_category
directed_category;
get_components(g, comp_map,
typename is_convertible<directed_category,
directed_tag>::type());
}
template <class Graph, class CompMap>
void get_components(const Graph& g, CompMap comp_map,
boost::true_type is_directed) const
{
strong_components(g, comp_map);
}
template <class Graph, class CompMap>
void get_components(const Graph& g, CompMap comp_map,
boost::false_type is_directed) const
{
connected_components(g, comp_map);
}
};
} // graph_tool namespace
#endif // GRAPH_COMPONENTS_HH
......@@ -13,3 +13,60 @@
// 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 "graph.hh"
#include "graph_filtering.hh"
#include "graph_properties.hh"
#include "graph_histograms.hh"
#include <boost/python.hpp>
using namespace std;
using namespace boost;
using namespace graph_tool;
// this will return the vertex histogram of degrees or scalar properties
python::object
get_vertex_histogram(const GraphInterface& gi, GraphInterface::deg_t deg,
const vector<long double>& bins)
{
python::object hist;
python::object ret_bins;
run_action<>()(gi, get_histogram<VertexHistogramFiller>(hist, bins,
ret_bins),
all_selectors())(degree_selector(deg, gi));
return python::make_tuple(hist, ret_bins);
}
// this will return the vertex histogram of degrees or scalar properties
python::object
get_edge_histogram(GraphInterface& gi, const string& prop,
const vector<long double>& bins)
{
python::object hist;
python::object ret_bins;
bool directed = gi.GetDirected();
gi.SetDirected(false);
typedef property_map_types::apply<scalar_types,
GraphInterface::edge_index_map_t,
mpl::bool_<true> >::type
edge_props;
run_action<graph_tool::detail::always_directed>()
(gi, get_histogram<EdgeHistogramFiller>(hist, bins, ret_bins),
edge_props())(edge_prop(prop,gi));
gi.SetDirected(directed);
return python::make_tuple(hist, ret_bins);
}
using namespace boost::python;
void export_histograms()
{
def("get_vertex_histogram", &get_vertex_histogram);
def("get_edge_histogram", &get_edge_histogram);
}
This diff is collapsed.
// Copyright (C) 2008 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/>.
#include "graph.hh"
#include "graph_filtering.hh"
#include "graph_properties.hh"
#include "graph_parallel.hh"
#include <boost/python.hpp>
using namespace std;
using namespace boost;
using namespace graph_tool;
void do_label_parallel_edges(GraphInterface& gi, const string& property)
{
run_action<>()(gi, bind<void>(label_parallel_edges(), _1,
gi.GetEdgeIndex(), _2),
edge_scalar_properties())
(edge_prop(property, gi));
}
void do_label_self_loops(GraphInterface& gi, const string& property)
{
run_action<>()(gi, bind<void>(label_self_loops(), _1,
gi.GetEdgeIndex(), _2),
edge_scalar_properties())
(edge_prop(property, gi));
}
using namespace boost::python;
void export_parallel()
{
def("label_parallel_edges", &do_label_parallel_edges);
def("label_self_loops", &do_label_self_loops);
}
// Copyright (C) 2008 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/>.
#ifndef GRAPH_PARALLEL_HH
#define GRAPH_PARALLEL_HH
#include <tr1/unordered_set>
namespace graph_tool
{
using namespace std;
using namespace boost;
// label parallel edges in the order they are found, starting from 1, all others
struct label_parallel_edges
{
template <class Graph, class EdgeIndexMap, class ParallelMap>
void operator()(const Graph* gp, EdgeIndexMap edge_index,
ParallelMap parallel) const
{
const Graph& g = *gp;
typedef typename graph_traits<Graph>::edge_descriptor edge_t;
int i, N = num_vertices(g);
#pragma omp parallel for default(shared) private(i) schedule(dynamic)
for (i = 0; i < N; ++i)
{
typename graph_traits<Graph>::vertex_descriptor v = vertex(i, g);
if (v == graph_traits<Graph>::null_vertex())
continue;
tr1::unordered_set<edge_t,DescriptorHash<EdgeIndexMap> >
p_edges(0, DescriptorHash<EdgeIndexMap>(edge_index));
typename graph_traits<Graph>::out_edge_iterator e1, e2, e_end;
for (tie(e1, e_end) = out_edges(v, g); e1 != e_end; ++e1)
{
if (p_edges.find(*e1) != p_edges.end())
continue;
size_t n = 0;
put(parallel, *e1, n);
for (tie(e2, e_end) = out_edges(v, g); e2 != e_end; ++e2)
if (*e2 != *e1 && target(*e1, g) == target(*e2, g))
{
put(parallel, *e2, ++n);
p_edges.insert(*e2);
}
}
}
}
};
// label self loops edges in the order they are found, starting from 1
struct label_self_loops
{
template <class Graph, class EdgeIndexMap, class SelfMap>
void operator()(const Graph* gp, EdgeIndexMap edge_index,
SelfMap self) const
{
const Graph& g = *gp;
typedef typename graph_traits<Graph>::edge_descriptor edge_t;
int i, N = num_vertices(g);
#pragma omp parallel for default(shared) private(i) schedule(dynamic)
for (i = 0; i < N; ++i)
{
typename graph_traits<Graph>::vertex_descriptor v = vertex(i, g);
if (v == graph_traits<Graph>::null_vertex())
continue;
size_t n = 1;
typename graph_traits<Graph>::out_edge_iterator e, e_end;
for (tie(e, e_end) = out_edges(v, g); e != e_end; ++e)
{
if (target(*e, g) == v)
put(self, *e, n++);
else
put(self, *e, 0);
}
}
}
};
} // graph_tool namespace
#endif //GRAPH_PARALLEL_HH
// Copyright (C) 2008 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/>.
#include <boost/python.hpp>
using namespace boost;
void export_components();
void export_parallel();
void export_histograms();
BOOST_PYTHON_MODULE(libgraph_tool_stats)
{
export_components();
export_parallel();
export_histograms();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment