Commit 73195ddd authored by Tiago Peixoto's avatar Tiago Peixoto

* fixed bug in scalarS where undirected edge properties were not properly handled

* removed exeption throwing in scalarS to improve performance


git-svn-id: https://svn.forked.de/graph-tool/trunk@113 d4600afd-f417-0410-95de-beed9576f240
parent cf706874
......@@ -260,7 +260,7 @@ struct choose_vertex_histogram
{
if (mpl::at<degree_selector_index,DegreeSelector>::type::value == _deg)
{
DegreeSelector selector(_deg_name, _g);
DegreeSelector selector(_deg_name, _g, true);
check_filter(_g, bind<void>(get_vertex_histogram<DegreeSelector>(selector), _1, var(_hist)),reverse_check(),directed_check());
}
}
......@@ -329,7 +329,7 @@ GraphInterface::hist_t GraphInterface::GetEdgeHistogram(string property) const
hist_t hist;
try
{
scalarS prop(property, *this);
scalarS prop(property, *this, false);
check_filter(*this, bind<void>(get_edge_histogram(prop), _1, var(hist)),reverse_check(),directed_check());
}
catch (dynamic_get_failure &e)
......
......@@ -128,7 +128,7 @@ struct choose_assortativity_coefficient
{
if (mpl::at<degree_selector_index, DegreeSelector>::type::value == _deg)
{
DegreeSelector deg(_deg_name, _g);
DegreeSelector deg(_deg_name, _g, true);
check_filter(_g, bind<void>(get_assortativity_coefficient<DegreeSelector>(deg), _1, var(_a), var(_a_err)),
reverse_check(),directed_check());
}
......@@ -264,7 +264,7 @@ struct choose_scalar_assortativity_coefficient
{
if (mpl::at<degree_selector_index, DegreeSelector>::type::value == _deg)
{
DegreeSelector deg(_deg_name, _g);
DegreeSelector deg(_deg_name, _g, true);
check_filter(_g, bind<void>(get_scalar_assortativity_coefficient<DegreeSelector>(deg), _1, var(_a), var(_a_err)),
reverse_check(),directed_check());
}
......
......@@ -97,8 +97,8 @@ struct choose_vertex_correlation_histogram
{
if (mpl::at<degree_selector_index,DegreeSelector2>::type::value == _parent._deg2)
{
DegreeSelector1 deg1(_parent._deg_name1, _parent._g);
DegreeSelector2 deg2(_parent._deg_name2, _parent._g);
DegreeSelector1 deg1(_parent._deg_name1, _parent._g, true);
DegreeSelector2 deg2(_parent._deg_name2, _parent._g, true);
check_filter(_parent._g, bind<void>(get_correlation_histogram<DegreeSelector1,DegreeSelector2>(deg1,deg2), _1, var(_parent._weight), var(_parent._hist)),
reverse_check(),directed_check());
}
......
......@@ -89,8 +89,8 @@ struct choose_combined_degree_histogram
{
if (mpl::at<degree_selector_index,DegreeSelector2>::type::value == _parent._deg2)
{
DegreeSelector1 deg1(_parent._deg_name1, _parent._g);
DegreeSelector2 deg2(_parent._deg_name2, _parent._g);
DegreeSelector1 deg1(_parent._deg_name1, _parent._g, true);
DegreeSelector2 deg2(_parent._deg_name2, _parent._g, true);
check_filter(_parent._g, bind<void>(get_combined_degree_histogram<DegreeSelector1,DegreeSelector2>(deg1,deg2), _1, var(_parent._hist)),
reverse_check(),directed_check());
}
......@@ -195,8 +195,8 @@ struct choose_average_combined_degree_correlation
{
if (mpl::at<degree_selector_index,DegreeSelector2>::type::value == _parent._deg2)
{
DegreeSelector1 deg1(_parent._deg_name1, _parent._g);
DegreeSelector2 deg2(_parent._deg_name2, _parent._g);
DegreeSelector1 deg1(_parent._deg_name1, _parent._g, true);
DegreeSelector2 deg2(_parent._deg_name2, _parent._g, true);
check_filter(_parent._g, bind<void>(get_average_combined_degree_correlation<DegreeSelector1,DegreeSelector2>(deg1,deg2),
_1, var(_parent._avg_corr)),
reverse_check(),directed_check());
......
......@@ -115,8 +115,8 @@ struct choose_average_nearest_neighbours_correlation
{
if ( mpl::at<degree_selector_index, DegreeSelector>::type::value == _parent._neighbour_deg)
{
OriginDegreeSelector origin_deg(_parent._origin_deg_name, _parent._g);
DegreeSelector deg(_parent._neighbour_deg_name, _parent._g);
OriginDegreeSelector origin_deg(_parent._origin_deg_name, _parent._g, true);
DegreeSelector deg(_parent._neighbour_deg_name, _parent._g, true);
check_filter(_parent._g, bind<void>(get_average_nearest_neighbours_correlation<OriginDegreeSelector,DegreeSelector>(origin_deg, deg),
_1, var(_parent._weight), var(_parent._avg_deg)),
reverse_check(),directed_check());
......
......@@ -102,8 +102,8 @@ struct choose_edge_vertex_correlation_histogram
{
if (mpl::at<degree_selector_index,DegreeSelector2>::type::value == _parent._deg2)
{
DegreeSelector1 deg1(_parent._deg_name1, _parent._g);
DegreeSelector2 deg2(_parent._deg_name2, _parent._g);
DegreeSelector1 deg1(_parent._deg_name1, _parent._g, true);
DegreeSelector2 deg2(_parent._deg_name2, _parent._g, true);
check_filter(_parent._g, bind<void>(get_edge_correlation_histogram<DegreeSelector1,DegreeSelector2>(deg1,deg2,_parent._edge_scalar),
_1, var(_parent._hist)),
reverse_check(),directed_check());
......@@ -132,7 +132,7 @@ GraphInterface::GetEdgeVertexCorrelationHistogram(GraphInterface::deg_t deg1, st
{
hist3d_t hist;
scalarS scalar(edge_scalar, *this);
scalarS scalar(edge_scalar, *this, false);
try
{
typedef mpl::vector<in_degreeS, out_degreeS, total_degreeS, scalarS> degree_selectors;
......
......@@ -36,7 +36,7 @@ namespace graph_tool
struct total_degreeS
{
total_degreeS() {}
total_degreeS(std::string scalar_property, const GraphInterface& g) {}
total_degreeS(std::string scalar_property, const GraphInterface& g, bool vertex = true) {}
template <class Graph, class Vertex>
size_t operator()(const Vertex& v, const Graph &g) const
{
......@@ -63,7 +63,7 @@ struct total_degreeS
struct in_degreeS
{
in_degreeS() {}
in_degreeS(std::string scalar_property, const GraphInterface& g) {}
in_degreeS(std::string scalar_property, const GraphInterface& g, bool vertex = true) {}
template <class Graph, class Vertex>
size_t operator()(const Vertex& v, const Graph &g) const
{
......@@ -90,7 +90,7 @@ struct in_degreeS
struct out_degreeS
{
out_degreeS() {}
out_degreeS(std::string scalar_property, const GraphInterface& g) {}
out_degreeS(std::string scalar_property, const GraphInterface& g, bool vertex = true) {}
template <class Graph, class Vertex>
size_t operator()(const Vertex& v, const Graph &g) const
{
......@@ -102,46 +102,64 @@ struct out_degreeS
struct scalarS
{
scalarS(){}
scalarS(std::string scalar_property, const GraphInterface& g):
_scalar_property(scalar_property), _g(&g) {}
typedef boost::mpl::vector<double,long double,float,long,unsigned long,int,unsigned int,short,unsigned short,char,unsigned char,bool,std::string> scalar_types;
template <class Graph, class VertexOrEdge>
double operator()(const VertexOrEdge& v, const Graph &g) const
scalarS(std::string scalar_property, const GraphInterface& g, bool is_vertex_prop):
_name(scalar_property), _g(&g)
{
if (is_vertex_prop)
_scalar_property = &find_property_map(g._properties, _name, typeid(boost::graph_traits<GraphInterface::multigraph_t>::vertex_descriptor));
else
_scalar_property = &find_property_map(g._properties, _name, typeid(boost::graph_traits<GraphInterface::multigraph_t>::edge_descriptor));
}
typedef boost::mpl::vector<double,int,long double,float,long,unsigned long,unsigned int,short,unsigned short,char,unsigned char,bool,std::string> scalar_types;
template <class Graph>
double operator()(const typename boost::graph_traits<Graph>::vertex_descriptor& v, const Graph &g) const
{
try
{
return boost::get(_scalar_property, _g->_properties, v, boost::type<double>());
}
catch (boost::bad_any_cast)
{
using namespace boost::mpl;
return get_value<next<begin<scalar_types>::type>::type>(v);
}
using namespace boost;
any value = _scalar_property->get(v);
double *val = any_cast<double>(&value);
if (val != 0)
return *val;
else
return get_value<mpl::next<mpl::begin<scalar_types>::type>::type>(value);
}
template <class ValueIter, class VertexOrEdge>
double get_value(const VertexOrEdge& v, ValueIter = ValueIter()) const
template <class Graph>
double operator()(const typename boost::graph_traits<Graph>::edge_descriptor& e, const Graph &g) const
{
using namespace boost;
using namespace boost::mpl;
try
{
return lexical_cast<double>(get(_scalar_property, _g->_properties, v, type<typename deref<ValueIter>::type>()));
}
catch (bad_any_cast)
{
return get_value(v, typename boost::mpl::next<ValueIter>::type());
}
graph_traits<GraphInterface::multigraph_t>::edge_descriptor edge = e;
any value = _scalar_property->get(edge);
double *val = any_cast<double>(&value);
if (val != 0)
return *val;
else
return get_value<mpl::next<mpl::begin<scalar_types>::type>::type>(value);
}
template <class ValueIter>
double get_value(const boost::any& value , ValueIter = ValueIter()) const
{
using namespace boost;
typedef typename mpl::deref<ValueIter>::type val_type;
const val_type *val = any_cast<val_type>(&value);
if (val != 0)
return lexical_cast<double>(*val);
else
return get_value(value, typename mpl::next<ValueIter>::type());
}
template <class VertexOrEdge>
double get_value(const VertexOrEdge& v, boost::mpl::end<scalar_types>::type) const
double get_value(const boost::any& value, boost::mpl::end<scalar_types>::type) const
{
throw boost::dynamic_get_failure(_scalar_property);
throw boost::dynamic_get_failure(_name);
}
std::string name() {return _scalar_property;}
std::string _scalar_property;
std::string name() {return _name;}
std::string _name;
boost::dynamic_property_map* _scalar_property;
GraphInterface const* _g;
};
......
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