Commit 7e06567e authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

changed sampled distance semantics


git-svn-id: https://svn.forked.de/graph-tool/trunk@60 d4600afd-f417-0410-95de-beed9576f240
parent 531da979
......@@ -22,7 +22,6 @@ sys.path.append(".")
from libgraph_tool import *
from optparse import *
import string
import os
import os.path
import re
......@@ -108,9 +107,9 @@ statistics.add_option("--combined-vertex-histogram", action="callback", callback
statistics.add_option("--distance-histogram", action="callback", callback=push_option, type="string", metavar="[WEIGHT|]FILE", help="get the distance histogram")
statistics.add_option("--average-distance", action="callback", callback=push_option, type="string", metavar="[WEIGHT|]FILE", help="get the averarge distance")
statistics.add_option("--average-harmonic-distance", action="callback", callback=push_option, type="string", metavar="[WEIGHT|]FILE", help="get the averarge harmonic distance")
statistics.add_option("--sampled-distance-histogram", action="callback", callback=push_option, type="string", metavar="[WEIGHT|]SAMPLES|SEED|FILE", help="get the sampled distance histogram")
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("--sampled-distance-histogram", action="callback", callback=push_option, type="string", metavar="OPTIONS|FILE", help="get the sampled distance histogram. Options are samples, weight (optional), seed (default: from clock)")
statistics.add_option("--average-sampled-distance", action="callback", callback=push_option, type="string", metavar="OPTIONS|FILE", help="get the average sampled distance. Options are samples, weight (optional), seed (default: from clock)")
statistics.add_option("--average-sampled-harmonic-distance", action="callback", callback=push_option, type="string", metavar="OPTIONS|FILE", help="get the average sampled harmonic distance. Options are samples, weight (optional), seed (default: from clock)")
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")
......@@ -393,39 +392,63 @@ def parse_option(opt, just_file=False):
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:
if len(values) != 2:
raise OptionError(opt.name, "invalid value '%s'" % opt.value)
if len(values) == 3:
weight, samples, seed, file_name = "", int(values[0]), int(values[1]), values[2]
if get_suboption("weight", values[0]) != False:
weight = get_suboption("weight", values[0])
else:
weight = ""
if get_suboption("samples", values[0]) != False:
samples = int(get_suboption("samples", values[0]))
else:
raise OptionError(opt.name, "you must provide the number of samples")
if get_suboption("seed", values[0]) != False:
seed = int(get_suboption("seed", values[0]))
else:
weight, samples, seed, file_name = values[0], int(values[1]), int(values[2]), values[3]
seed = int(time())
if just_file:
return file_name
return (graph.GetSampledDistanceHistogram(weight, samples, seed), file_name)
return values[1]
return (graph.GetSampledDistanceHistogram(weight, samples, seed), values[1])
elif opt.name == "average-sampled-distance":
values = parse_values(opt.value)
if len(values) > 4 or len(values) < 3:
if len(values) != 2:
raise OptionError(opt.name, "invalid value '%s'" % opt.value)
if len(values) == 3:
weight, samples, seed, file_name = "", int(values[0]), int(values[1]), values[2]
if get_suboption("weight", values[0]) != False:
weight = get_suboption("weight", values[0])
else:
weight = ""
if get_suboption("samples", values[0]) != False:
samples = int(get_suboption("samples", values[0]))
else:
weight, samples, seed, file_name = values[0], int(values[1]), int(values[2]), values[3]
raise OptionError(opt.name, "you must provide the number of samples")
if get_suboption("seed", values[0]) != False:
seed = int(get_suboption("seed", values[0]))
else:
seed = int(time())
if just_file:
return file_name
return ("%f\t%f" % get_mean(graph.GetSampledDistanceHistogram(weight, samples, seed)), file_name)
return values[1]
return ("%f\t%f" % get_mean(graph.GetSampledDistanceHistogram(weight, samples, seed)), values[1])
elif opt.name == "average-sampled-harmonic-distance":
values = parse_values(opt.value)
if len(values) > 4 or len(values) < 3:
if len(values) != 2:
raise OptionError(opt.name, "invalid value '%s'" % opt.value)
if len(values) == 3:
weight, samples, seed, file_name = "", int(values[0]), int(values[1]), values[2]
if get_suboption("weight", values[0]) != False:
weight = get_suboption("weight", values[0])
else:
weight, samples, seed, file_name = values[0], int(values[1]), int(values[2]), values[3]
weight = ""
if get_suboption("samples", values[0]) != False:
samples = int(get_suboption("samples", values[0]))
else:
raise OptionError(opt.name, "you must provide the number of samples")
if get_suboption("seed", values[0]) != False:
seed = int(get_suboption("seed", values[0]))
else:
seed = int(time())
if just_file:
return file_name
return values[1]
hist = graph.GetSampledDistanceHistogram(weight, samples, seed)
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)
return ("%f\t%f" % (1.0/avg,1.0/err) , values[1])
elif opt.name == "label-components":
if just_file:
return
......
......@@ -60,9 +60,12 @@ struct get_sampled_distances
tr1::unordered_map<size_t, typename graph_traits<Graph>::vertex_descriptor> descriptors;
typename graph_traits<Graph>::vertex_iterator v, v_end;
size_t i = 0;
size_t i = 0, N = 0;
for(tie(v, v_end) = vertices(g); v != v_end; ++v,++i)
{
descriptors[i] = *v;
N++;
}
rng_t rng(static_cast<rng_t::result_type>(seed));
uniform_int<size_t> sampler(0,descriptors.size()-1);
......@@ -71,12 +74,17 @@ struct get_sampled_distances
{
for(tie(v, v_end) = vertices(g); v != v_end; ++v)
dist_map[*v] = numeric_limits<double>::max();
typename graph_traits<Graph>::vertex_descriptor s = descriptors[sampler(rng)];
typename graph_traits<Graph>::vertex_descriptor s = descriptors[sampler(rng)], t;
do
{
t = descriptors[sampler(rng)];
}
while (t == s && N != 1);
dist_map[s] = 0.0;
get_vertex_dists(g, s, index_map, dist_map, weights);
for(tie(v, v_end) = vertices(g); v != v_end; ++v,++i)
if (*v != s && dist_map[*v] != numeric_limits<double>::max() )
hist[dist_map[*v]]++;
if (dist_map[t] != numeric_limits<double>::max() && dist_map[t] != 0.0)
hist[dist_map[t]]++;
}
}
......
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