Commit 3266b079 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

* added support for reciprocity

* python_filtering: added n_parallel() function


git-svn-id: https://svn.forked.de/graph-tool/trunk@45 d4600afd-f417-0410-95de-beed9576f240
parent feb7071b
...@@ -89,7 +89,6 @@ modification.add_option("--remove-vertex-property", action="callback", callback= ...@@ -89,7 +89,6 @@ modification.add_option("--remove-vertex-property", action="callback", callback=
modification.add_option("--remove-edge-property", action="callback", callback=push_option, type="string", metavar="PROPERTY|RANGE", help="remove edge property from graph") modification.add_option("--remove-edge-property", action="callback", callback=push_option, type="string", metavar="PROPERTY|RANGE", help="remove edge property from graph")
modification.add_option("--insert-vertex-index-property", action="callback", callback=push_option, type="string", metavar="PROPERTY", help="insert vertex index as property") modification.add_option("--insert-vertex-index-property", action="callback", callback=push_option, type="string", metavar="PROPERTY", help="insert vertex index as property")
modification.add_option("--insert-edge-index-property", action="callback", callback=push_option, type="string", metavar="PROPERTY", help="insert edge index as property") modification.add_option("--insert-edge-index-property", action="callback", callback=push_option, type="string", metavar="PROPERTY", help="insert edge index as property")
modification.add_option("--remove-parallel-edges", action="callback", callback=push_option, help="remove parallel edges from graph")
statistics = parser.add_option_group("Basic Statistics") statistics = parser.add_option_group("Basic Statistics")
statistics.add_option("--number-of-vertices", action="callback", callback=push_option, type="string", metavar="FILE", help="get the number of vertices") statistics.add_option("--number-of-vertices", action="callback", callback=push_option, type="string", metavar="FILE", help="get the number of vertices")
...@@ -106,7 +105,7 @@ statistics.add_option("--average-sampled-harmonic-distance", action="callback", ...@@ -106,7 +105,7 @@ statistics.add_option("--average-sampled-harmonic-distance", action="callback",
statistics.add_option("--component-size-histogram", action="callback", callback=push_option, type="string", metavar="FILE", help="get the component size histogram") statistics.add_option("--component-size-histogram", action="callback", callback=push_option, type="string", metavar="FILE", help="get the component size histogram")
statistics.add_option("--average-vertex-property", action="callback", callback=push_option, type="string", metavar="PROPERTY|FILE", help="get the average of the vertex property") statistics.add_option("--average-vertex-property", action="callback", callback=push_option, type="string", metavar="PROPERTY|FILE", help="get the average of the vertex property")
statistics.add_option("--average-edge-property", action="callback", callback=push_option, type="string", metavar="PROPERTY|FILE", help="get the average of the edge property") statistics.add_option("--average-edge-property", action="callback", callback=push_option, type="string", metavar="PROPERTY|FILE", help="get the average of the edge property")
statistics.add_option("--reciprocity", action="callback", callback=push_option, type="string", metavar="FILE", help="get the edge reciprocity")
correlations = parser.add_option_group("Correlations") correlations = parser.add_option_group("Correlations")
correlations.add_option("--average-combined-vertex-correlation", action="callback", callback=push_option, type="string", metavar="DEGREE1|DEGREE2|FILE", help="get the average of DEGREE2 in function of DEGREE1. Scalar properties are also accepted as DEGREE1 or DEGREE2") correlations.add_option("--average-combined-vertex-correlation", action="callback", callback=push_option, type="string", metavar="DEGREE1|DEGREE2|FILE", help="get the average of DEGREE2 in function of DEGREE1. Scalar properties are also accepted as DEGREE1 or DEGREE2")
...@@ -409,6 +408,10 @@ def parse_option(opt, just_file=False): ...@@ -409,6 +408,10 @@ def parse_option(opt, just_file=False):
if just_file: if just_file:
return values[1] return values[1]
return (graph.GetEdgeHistogram(values[0]), values[1]) return (graph.GetEdgeHistogram(values[0]), values[1])
elif opt.name == "reciprocity":
if just_file:
return opt.value
return (graph.GetReciprocity(), opt.value)
elif opt.name == "vertex-correlation-histogram": elif opt.name == "vertex-correlation-histogram":
values = parse_values(opt.value) values = parse_values(opt.value)
if len(values) != 3: if len(values) != 3:
...@@ -570,10 +573,6 @@ def parse_option(opt, just_file=False): ...@@ -570,10 +573,6 @@ def parse_option(opt, just_file=False):
if just_file: if just_file:
return None return None
graph.SetReversed(not graph.GetReversed()) graph.SetReversed(not graph.GetReversed())
elif opt.name == "remove-parallel-edges":
if just_file:
return None
graph.RemoveParallelEdges()
elif opt.name == "edit-vertex-property" or opt.name == "edit-edge-property": elif opt.name == "edit-vertex-property" or opt.name == "edit-edge-property":
values = parse_values(opt.value) values = parse_values(opt.value)
if len(values) != 2: if len(values) != 2:
...@@ -797,7 +796,7 @@ try: ...@@ -797,7 +796,7 @@ try:
count += 1 count += 1
del history_files del history_files
except (OptionError, HistoryException, IOError), e: except (OptionError, HistoryException, IOError, RuntimeError), e:
print "graph-tool:", e print "graph-tool error:", e
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
...@@ -34,6 +34,7 @@ libgraph_tool_la_SOURCES = \ ...@@ -34,6 +34,7 @@ libgraph_tool_la_SOURCES = \
graph_generation.cc\ graph_generation.cc\
graph_distance.cc\ graph_distance.cc\
graph_distance_sampled.cc\ graph_distance_sampled.cc\
graph_reciprocity.cc\
graph_io.cc\ graph_io.cc\
graph_bind.cc\ graph_bind.cc\
graphml.hpp\ graphml.hpp\
......
...@@ -346,50 +346,6 @@ void GraphInterface::InsertVertexIndexProperty(string property) ...@@ -346,50 +346,6 @@ void GraphInterface::InsertVertexIndexProperty(string property)
_properties.property(property, _vertex_index); _properties.property(property, _vertex_index);
} }
//==============================================================================
// RemoveParallelEdges(property)
//==============================================================================
struct remove_parallel_edges
{
template <class Graph>
void operator()(Graph &g) const
{
typename graph_traits<Graph>::edge_iterator e, e_end;
for (tie(e, e_end) = edges(g); e != e_end; ++e)
{
bool finished = false;
while (!finished)
{
finished = true;
typename graph_traits<Graph>::out_edge_iterator oe, oe_end;
typename graph_traits<Graph>::vertex_descriptor s,t;
s = source(*e,g);
t = target(*e,g);
for (tie(oe,oe_end) = out_edges(s,g); oe != oe_end; ++oe)
if (*oe != *e && target(*oe,g) == t) // is a parallel edge
{
remove_edge(*oe, g);
finished = false;
break;
}
}
}
}
};
void GraphInterface::RemoveParallelEdges()
{
if (_directed)
{
remove_parallel_edges()(_mg);
}
else
{
UndirectedAdaptor<multigraph_t> ug(_mg);
remove_parallel_edges()(ug);
}
}
//============================================================================== //==============================================================================
// ComputeGraphLayoutGursoy(iter, seed) // ComputeGraphLayoutGursoy(iter, seed)
//============================================================================== //==============================================================================
......
...@@ -92,6 +92,7 @@ public: ...@@ -92,6 +92,7 @@ public:
hist_t GetComponentSizeHistogram() const; hist_t GetComponentSizeHistogram() const;
hist_t GetDistanceHistogram(std::string weight) const; hist_t GetDistanceHistogram(std::string weight) const;
hist_t GetSampledDistanceHistogram(std::string weight, size_t samples, size_t seed) const; hist_t GetSampledDistanceHistogram(std::string weight, size_t samples, size_t seed) const;
double GetReciprocity() const;
// filtering // filtering
void SetDirected(bool directed) {_directed = directed;} void SetDirected(bool directed) {_directed = directed;}
...@@ -122,7 +123,6 @@ public: ...@@ -122,7 +123,6 @@ public:
void InsertVertexIndexProperty(std::string property); void InsertVertexIndexProperty(std::string property);
void EditVertexProperty(std::string property, boost::python::object op); void EditVertexProperty(std::string property, boost::python::object op);
void EditEdgeProperty(std::string property, boost::python::object op); void EditEdgeProperty(std::string property, boost::python::object op);
void RemoveParallelEdges();
// layout // layout
void ComputeGraphLayoutGursoy(size_t iter = 0, size_t seed = 4357); void ComputeGraphLayoutGursoy(size_t iter = 0, size_t seed = 4357);
......
...@@ -228,6 +228,7 @@ BOOST_PYTHON_MODULE(libgraph_tool) ...@@ -228,6 +228,7 @@ BOOST_PYTHON_MODULE(libgraph_tool)
.def("SetExtendedClusteringToProperty", &GraphInterfaceWrap::SetExtendedClusteringToProperty) .def("SetExtendedClusteringToProperty", &GraphInterfaceWrap::SetExtendedClusteringToProperty)
.def("GetDistanceHistogram", &GraphInterfaceWrap::GetDistanceHistogram) .def("GetDistanceHistogram", &GraphInterfaceWrap::GetDistanceHistogram)
.def("GetSampledDistanceHistogram", &GraphInterfaceWrap::GetSampledDistanceHistogram) .def("GetSampledDistanceHistogram", &GraphInterfaceWrap::GetSampledDistanceHistogram)
.def("GetReciprocity", &GraphInterfaceWrap::GetReciprocity)
.def("SetDirected", &GraphInterfaceWrap::SetDirected) .def("SetDirected", &GraphInterfaceWrap::SetDirected)
.def("GetDirected", &GraphInterfaceWrap::GetDirected) .def("GetDirected", &GraphInterfaceWrap::GetDirected)
.def("SetReversed", &GraphInterfaceWrap::SetReversed) .def("SetReversed", &GraphInterfaceWrap::SetReversed)
...@@ -250,7 +251,6 @@ BOOST_PYTHON_MODULE(libgraph_tool) ...@@ -250,7 +251,6 @@ BOOST_PYTHON_MODULE(libgraph_tool)
.def("RemoveVertexProperty", &GraphInterfaceWrap::RemoveVertexProperty) .def("RemoveVertexProperty", &GraphInterfaceWrap::RemoveVertexProperty)
.def("InsertEdgeIndexProperty", &GraphInterfaceWrap::InsertEdgeIndexProperty) .def("InsertEdgeIndexProperty", &GraphInterfaceWrap::InsertEdgeIndexProperty)
.def("InsertVertexIndexProperty", &GraphInterfaceWrap::InsertVertexIndexProperty) .def("InsertVertexIndexProperty", &GraphInterfaceWrap::InsertVertexIndexProperty)
.def("RemoveParallelEdges", &GraphInterfaceWrap::RemoveParallelEdges)
.def("ComputeGraphLayoutGursoy", &GraphInterfaceWrap::ComputeGraphLayoutGursoy) .def("ComputeGraphLayoutGursoy", &GraphInterfaceWrap::ComputeGraphLayoutGursoy)
.def("ComputeGraphLayoutSpringBlock", &GraphInterfaceWrap::ComputeGraphLayoutSpringBlock) .def("ComputeGraphLayoutSpringBlock", &GraphInterfaceWrap::ComputeGraphLayoutSpringBlock)
.def("WriteToFile", &GraphInterfaceWrap::WriteToFile) .def("WriteToFile", &GraphInterfaceWrap::WriteToFile)
......
...@@ -74,6 +74,7 @@ struct populate_python_funcs ...@@ -74,6 +74,7 @@ struct populate_python_funcs
typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor; typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
variables["is_self"] = python::make_function(is_self<Graph>(g, e), python::default_call_policies(), mpl::vector<python::object>::type()); variables["is_self"] = python::make_function(is_self<Graph>(g, e), python::default_call_policies(), mpl::vector<python::object>::type());
variables["n_parallel"] = python::make_function(n_parallel<Graph>(g, e), python::default_call_policies(), mpl::vector<python::object>::type());
for(typeof(dp.begin()) iter = dp.begin(); iter != dp.end(); ++iter) for(typeof(dp.begin()) iter = dp.begin(); iter != dp.end(); ++iter)
{ {
...@@ -243,6 +244,28 @@ struct populate_python_funcs ...@@ -243,6 +244,28 @@ struct populate_python_funcs
const edge_descriptor& _e; const edge_descriptor& _e;
}; };
template<class Graph>
struct n_parallel
{
typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
n_parallel(const Graph& g, const edge_descriptor& e): _g(g), _e(e) {}
python::object operator()()
{
size_t n = 0;
typename graph_traits<Graph>::vertex_descriptor s,t;
s = source(_e, _g);
t = target(_e, _g);
typename graph_traits<Graph>::adjacency_iterator a, a_end;
for(tie(a, a_end) = adjacent_vertices(s, _g); a != a_end; ++a)
if (*a == t)
n++;
return python::object(n-1);
}
const Graph& _g;
const edge_descriptor& _e;
};
}; };
......
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2006 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 2
// 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, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include "graph.hh"
#include "histogram.hh"
#include "graph_filtering.hh"
#include "graph_selectors.hh"
#include "graph_properties.hh"
using namespace std;
using namespace boost;
using namespace boost::lambda;
using namespace graph_tool;
struct get_reciprocity
{
template <class Graph>
void operator()(Graph& g, double& reciprocity) const
{
size_t L = 0;
double Lbd = 0.0;
typename graph_traits<Graph>::edge_iterator e,e_end;
for (tie(e,e_end) = edges(g); e != e_end; ++e)
{
typename graph_traits<Graph>::vertex_descriptor s,t;
s = source(*e,g);
t = target(*e,g);
size_t o = 0;
typename graph_traits<Graph>::adjacency_iterator a, a_end;
for (tie(a,a_end) = adjacent_vertices(s, g); a != a_end; ++a)
if (*a == t)
o++;
size_t i = 0;
for (tie(a, a_end) = adjacent_vertices(t, g); a != a_end; ++a)
if (*a == s)
i++;
Lbd += min(i/double(o),1.0);
L++;
}
size_t N = HardNumVertices()(g);
double a = L/double(N*(N-1));
reciprocity = (Lbd/L - a)/(1-a);
}
};
double GraphInterface::GetReciprocity() const
{
double reciprocity;
check_filter(*this, bind<void>(get_reciprocity(), _1, var(reciprocity)), reverse_check(), directed_check());
return reciprocity;
}
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