Created
February 19, 2010 14:34
-
-
Save cth/308743 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
if ARGV.length != 1 | |
puts "Usage: ./average_experiments experiment_directory\n\n" | |
puts "Computes the average over a number of experiment data files" | |
puts "located in the directory given as first argument. " | |
puts "The experiment files are expected to contain an equal number lines " | |
puts "with an equal number of entries. Each entry is expected to be a number" | |
puts "and entries are expected to be separated by space." | |
exit | |
end | |
d = Dir.open ARGV[0] | |
experiments = [] | |
numfiles = 0 | |
d.each do |file| | |
next if file == "." or file == ".." | |
numfiles = numfiles + 1 | |
file_lines = [] | |
fd = File.open("runs/" + file) | |
fd.each do |line| | |
line_entries = line.split | |
entries_per_line = line_entries.length | |
line_entries.map! { |entry| entry.to_i } | |
file_lines << line_entries | |
end | |
fd.close | |
experiments << file_lines | |
end | |
number_of_experiments = experiments.length | |
avg_experiment = experiments.shift | |
experiments.each do |exp| | |
0.upto(exp.length-1) do |run_i| | |
run = exp[run_i] | |
0.upto(run.length-1) do |datapoint_i| | |
avg_experiment[run_i][datapoint_i] = avg_experiment[run_i][datapoint_i] + run[datapoint_i] | |
end | |
end | |
end | |
0.upto(avg_experiment.length-1) do |run_i| | |
run = avg_experiment[run_i] | |
0.upto(run.length-1) do |datapoint_i| | |
avg_experiment[run_i][datapoint_i] = avg_experiment[run_i][datapoint_i].to_f / number_of_experiments | |
end | |
end | |
avg_experiment.each do |run| | |
puts run.join(" ") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment