Commit dbb7bb2c authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Added LabelParallelEdges()


git-svn-id: https://svn.forked.de/graph-tool/trunk@57 d4600afd-f417-0410-95de-beed9576f240
parent 05414a53
......@@ -112,11 +112,12 @@ statistics.add_option("--sampled-distance-histogram", action="callback", callbac
statistics.add_option("--average-sampled-distance", action="callback", callback=push_option, type="string", metavar="[WEIGHT|]SAMPLES|SEED|FILE", help="get the average sampled distance")
statistics.add_option("--average-sampled-harmonic-distance", action="callback", callback=push_option, type="string", metavar="[WEIGHT|]SAMPLES|SEED|FILE", help="get the average sampled harmonic distance")
statistics.add_option("--label-components", action="callback", callback=push_option, type="string", metavar="PROPERTY", help="label components to PROPERTY")
statistics.add_option("--label-parallel-edges", action="callback", callback=push_option, type="string", metavar="PROPERTY", help="label parallel edges to 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("--reciprocity", action="callback", callback=push_option, type="string", metavar="FILE", help="get the edge reciprocity")
statistics.add_option("--minimum-spanning-tree", action="callback", callback=push_option, type="string", metavar="[WEIGHT|]PROPERTY", help="mark the minimum spanning tree edges in PROPERTY")
statistics.add_option("--line-graph", action="callback", callback=push_option, type="string", metavar="FILE[|FORMAT]", help="mark the minimum spanning tree edges in PROPERTY")
statistics.add_option("--line-graph", action="callback", callback=push_option, type="string", metavar="FILE[|FORMAT]", help="save the corresponding line graph to FILE")
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")
......@@ -180,13 +181,14 @@ def degree(name):
def get_mean(hist):
avg,dev,count = 0.0,0.0,0.0
for k,v in hist.iteritems():
avg += k*v
dev += k*k*v
count += v
try:
for k,v in hist.iteritems():
avg += k*v
count += v
avg /= count
dev = math.sqrt(dev/count - avg*avg)/math.sqrt(count)
for k,v in hist.iteritems():
dev += (k - avg)**2
dev = sqrt(dev/(count**2))
except ZeroDivisionError:
avg = dev = float("nan") # nans are ok, since graph can be empty
return (avg,dev)
......@@ -387,8 +389,8 @@ def parse_option(opt, just_file=False):
if just_file:
return file_name
hist = graph.GetDistanceHistogram(weight)
avg, err = hist_mean(dict((1.0/k,v) for k,v in hist.iteritems()))
return ("%f\t%f" % (1.0/avg,1.0/err) , file_name)
avg, err = get_mean(dict((1.0/k,v) for k,v in hist.iteritems()))
return ("%f\t%f" % (1.0/avg,err/(avg**2)) , file_name)
elif opt.name == "sampled-distance-histogram":
values = parse_values(opt.value)
if len(values) > 4 or len(values) < 3:
......@@ -422,12 +424,16 @@ def parse_option(opt, just_file=False):
if just_file:
return file_name
hist = graph.GetSampledDistanceHistogram(weight, samples, seed)
avg, err = hist_mean(dict((1.0/k,v) for k,v in hist.iteritems()))
avg, err = get_mean(dict((1.0/k,v) for k,v in hist.iteritems()))
return ("%f\t%f" % (1.0/avg,1.0/err) , file_name)
elif opt.name == "label-components":
if just_file:
return
graph.LabelComponents(opt.value)
elif opt.name == "label-parallel-edges":
if just_file:
return
graph.LabelParallelEdges(opt.value)
elif opt.name == "vertex-histogram":
values = parse_values(opt.value)
if len(values) != 2:
......
......@@ -320,6 +320,57 @@ void GraphInterface::LabelComponents(string prop)
_properties.property(prop, comp_map);
}
//==============================================================================
// label_parallel_edges
// label parallel edges in order
//==============================================================================
struct label_parallel_edges
{
template <class Graph, class EdgeIndexMap, class ParallelMap>
void operator()(const Graph& g, EdgeIndexMap edge_index, ParallelMap parallel) const
{
typename graph_traits<Graph>::vertex_iterator v, v_end;
for (tie(v, v_end) = vertices(g); v != v_end; ++v)
{
tr1::unordered_set<typename graph_traits<Graph>::edge_descriptor,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);
}
}
}
}
};
//==============================================================================
// LabelParallelEdges(property)
//==============================================================================
void GraphInterface::LabelParallelEdges(string property)
{
try
{
DynamicPropertyMapWrap<size_t,graph_traits<multigraph_t>::edge_descriptor> parallel_map(find_property_map(_properties, property, typeid(graph_traits<multigraph_t>::edge_descriptor)));
check_filter(*this, bind<void>(label_parallel_edges(), _1, _edge_index, parallel_map), reverse_check(), directed_check());
}
catch (property_not_found)
{
typedef HashedDescriptorMap<edge_index_map_t,size_t> parallel_map_t;
parallel_map_t parallel_map(_edge_index);
check_filter(*this, bind<void>(label_parallel_edges(), _1, _edge_index, parallel_map), reverse_check(), directed_check());
_properties.property(property, parallel_map);
}
}
//==============================================================================
// InsertEdgeIndexProperty(property)
//==============================================================================
......
......@@ -91,6 +91,7 @@ public:
// other
void LabelComponents(std::string property);
void LabelParallelEdges(std::string property);
hist_t GetDistanceHistogram(std::string weight) const;
hist_t GetSampledDistanceHistogram(std::string weight, size_t samples, size_t seed) const;
double GetReciprocity() const;
......
......@@ -223,6 +223,7 @@ BOOST_PYTHON_MODULE(libgraph_tool)
.def("GetVertexHistogram", &GraphInterfaceWrap::GetVertexHistogram)
.def("GetEdgeHistogram", &GraphInterfaceWrap::GetEdgeHistogram)
.def("LabelComponents", &GraphInterfaceWrap::LabelComponents)
.def("LabelParallelEdges", &GraphInterfaceWrap::LabelParallelEdges)
.def("GetCombinedVertexHistogram", &GraphInterfaceWrap::GetCombinedVertexHistogram)
.def("GetAverageCombinedVertexCorrelation", &GraphInterfaceWrap::GetAverageCombinedVertexCorrelation)
.def("GetVertexCorrelationHistogram", &GraphInterfaceWrap::GetVertexCorrelationHistogram)
......
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