Class: Experiment::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/experiment/runner.rb

Overview

This is the class behind the command line magic. It is possible to use it programatically, though.

Examples:

For documentation on the CLI run

experiment -h

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg, opt) ⇒ Runner

If you are using this programmatically you need to set these params correctly:

Parameters:

  • arg (Array<String>)

    Typically the name of the experiment the operation needs to operate on.

  • opt (Struct, OpenStruct)

    an options object that should respond according to the CLI.



18
19
20
# File 'lib/experiment/runner.rb', line 18

def initialize(arg, opt)
  @arguments, @options = arg, opt
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/experiment/runner.rb', line 11

def options
  @options
end

Instance Method Details

#consoleObject

Creates an IRB console useful for debugging experiments Loads up the environment for the condition passed



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/experiment/runner.rb', line 154

def console
cla = as_class_name(@arguments.first)	if @arguments.length == 1
 File.open("./tmp/irb-setup.rb", 'w') do |f|
   f.puts "# Initializes the environment for IRb."
   f.puts "Experiment::Config::init #{@options.env.inspect}"
   f.puts "$: << '#{File.expand_path(".")}/'"
   if @arguments.length == 1
     f.puts "require 'experiments/#{@arguments.first}/#{@arguments.first}'"
     f.puts "def experiment"
	    f.puts "  @experiment ||= #{cla}.new :normal, #{@arguments.first.inspect}, OpenStruct.new(#{@options.marshal_dump})"
	    f.puts "end"
	    f.puts "experiment #load up the configs"
	  else
	    f.puts 'Dir["./app/**/*.{rb,o,so,dll,bundle}"].each{|e| require e.gsub(/\.(rb|so|o|dll|bundle)$/, "") }'
	    f.puts "Experiment::Config::load '', #{options.opts.inspect}"
   end
   
 end
   irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
   libs =  " -r irb/completion"
   libs <<  " -r #{File.dirname(__FILE__) + "/base"}"
   libs << " -r./experiments/experiment"
   libs << " -r ./tmp/irb-setup.rb"
   puts "Loading #{@options.env} environment..."
   exec "#{irb} #{libs} --simple-prompt"
end

#generateObject

Generates a new experiment condition Usage of the -m flag for writing a hypothesis is recommended



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/experiment/runner.rb', line 25

def generate
dir = "./experiments/" + @arguments.first
			Dir.mkdir(dir)
			File.open(dir + "/" + @arguments.first + ".rb", "w") do |req_file|
 req_file.puts "# ## #{as_human_name @arguments.first} ##"
 req_file.puts "# "+@options.description.split("\n").join("\n# ")
 req_file.puts
 req_file.puts
 req_file.puts "# The first contious block of comment will be included in your report."
 req_file.puts "# This includes the reference implementation."
 req_file.puts "# Override any desired files in this directory."
 Dir["./app/**/*.{rb,o,dll,so,bundle}"].each do |f|
   next if File.basename(f) == 'extconfig.rb'
   p = File.expand_path(f).split("/") - File.expand_path(".").split("/")
   req_file.puts "require \"#{p.join("/").gsub(/\.(rb|o|dll|so|bundle)$/, "")}\""
 end
 req_file.puts "\nclass #{as_class_name @arguments.first} < MyExperiment\n\t\nend"
			end
			File.open(dir + "/config.yaml", "w") do |f|
 f << "---\nexperiment:\n  development:\n  compute:\n"
  end
end

#listObject

Lists available experiments



69
70
71
72
# File 'lib/experiment/runner.rb', line 69

def list
  puts "Available experiments:"
  puts "  " + Dir["./experiments/*"].map{|a| File.dirname(a) }.join(", ")
end

#new_projectObject

generate a new project in the current directory



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/experiment/runner.rb', line 49

def new_project 
  require 'fileutils'
  dir = "./" + @arguments.first
  Dir.mkdir(dir)
  %w[app config experiments report results test tmp vendor].each do |d|
    Dir.mkdir(dir + "/" + d)
  end
  basedir = File.dirname(__FILE__)
  File.open(File.join(dir, "config", "config.yaml"), "w") do |f|
    f << "---\nenvironments:\n  development:\n  compute:\n"
  end
  File.open(File.join(dir, ".gitignore"), "w") do |f|
    f << "tmp/*"
  end
  FileUtils::cp File.join(basedir, "generator/readme_template.txt"), File.join(dir, "README")
  FileUtils::cp File.join(basedir, "generator/Rakefile"), File.join(dir, "Rakefile")
  FileUtils::cp File.join(basedir, "generator/experiment_template.rb.txt"), File.join(dir, "experiments", "experiment.rb")
end

#reportObject

Generates 2 files in the report directory method.mmd which sums up comments from experimental conditions data.csv which sums all results in a table



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/experiment/runner.rb', line 77

def report
  dir = "./report/"
    File.open(dir + "method.mmd", "w") do |f|
      f.puts "# Methods #"
      Dir["./experiments/*/*.rb"].each do |desc|
        if File.basename(desc) == File.basename(File.dirname(desc)) + ".rb"
          File.read(desc).split("\n").each do |line|
            if m = line.match(/^\# (.+)/)
              f.puts m[1]
            else
              break
            end
          end
        f.puts
        f.puts
        end
      end
    end
    require 'csv'
    require "yaml"
    require File.dirname(__FILE__) + "/stats"
    CSV.open(dir + "/data.csv", "w") do |csv|
      data = {}
      Dir["./results/*/results.yaml"].each do |res|
        d = YAML::load_file(res)
        da = {}
        d.each do |k, vals|
          da[k.to_s + " mean"], da[k.to_s + " sd"] = Stats::mean(vals), Stats::standard_deviation(vals)
          vals.each_with_index do |v, i|
            da[k.to_s + " cv:" + i.to_s] = v
          end
        end
        array_merge(data, da)
      end
      data.keys.map do |key| 
  		  # calculate stats
  		  a = data[key]
  		  [key] + a
		  end.transpose.each do |row|
		    csv << row
		  end
    end

end

#runObject

runs experiments passed as arguments use the -o option to override configuration



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/experiment/runner.rb', line 125

def run
  require File.dirname(__FILE__) + "/base"

  require "./experiments/experiment"
  $: << "./"
    Experiment::Config::init @options.env
  @options.cv = Experiment::Config.get :cross_validations, 5 if @options.cv.nil?
  if @options.distributed
    require "drb/drb"
    require File.dirname(__FILE__) + "/work_server"
    puts "Running in distributed mode. Run other machines with:\nexperiment worker --address #{local_ip}\n"
    Notify::init @arguments.length * @options.cv, STDOUT, Experiment::Config::get(:growl_notifications, true)
    ws = WorkServer.new @arguments, @options, local_ip
    Notify::done
    return true
	  else
	    Notify::init @arguments.length * @options.cv, @options.quiet ? false : STDERR, Experiment::Config::get(:growl_notifications, !@options.quiet)
	  @arguments.each do |exp|
			  require "./experiments/#{exp}/#{exp}"
			  cla = eval(as_class_name(exp))
				experiment = cla.new :normal, exp, @options
				experiment.run! @options.cv
			end
	  Notify::done
  end
end

#workerObject

Starts a Worker implementation. It requires an –address option of it’s master server and will recieve tasks (experiments and cross-validations) and compute them.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/experiment/runner.rb', line 185

def worker
 require "drb/drb"
 require File.dirname(__FILE__) + "/base"
 $: << "./"
 Experiment::Config::init @options.env
 loop do
   @server_uri="druby://#{@options.master}:8787"
	  connect
	  Notify::init 0, STDOUT, false, @master
     while item = @master.new_item
       #puts item
       exp = @master.experiment item
       require "./experiments/experiment"
       require "./experiments/#{exp}/#{exp}"
		  cla = eval(as_class_name(exp))
			experiment = cla.new :slave, exp, @options
		  experiment.master = @master.instance item
		  experiment.run! 0
     end
   end
end