Class: Experiment::Config

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

Overview

You have a config directory containing a config.yaml file. This file contains several environments. The idea is that you might want to tweak your options differently when running on your laptop then when running on a university supercomputer.

development is the default environment, you can set any other with the –env option.

Experimental conditions also get their own config.yaml file. This file overrides the main config file so you can introduce in condition specific options.

And finally when running an experiment you can use the -o or –options option to override any config you want.

Examples:

With the yamls like this:

# config/config.yaml
environments:
  development:
    ref_dir: /Users/kubowo/Desktop/points-vals
    master_dir: /Users/kubowo/Desktop/points-vals/s:writer
    alpha: 0.4
  compute:
    ref_dir: /afs/group/DB/points
    master_dir: /afs/group/DB/points/s:writer
    alpha: 0.4

# experiments/my_condition/config.yaml
experiment:
  development:
    alpha: 0.5
  compute:
    alpha: 0.6

# And you run the experiment with
$ experiment console my_condition --env compute -o "master_dir: /Users/kubowo/Desktop/points-vals/aaa/s:writer"

# Then your final config will look like this:
> Experiment::Config.to_hash
=> { :ref_dir => "/afs/group/DB/points", 
       :master_dir => "/Users/kubowo/Desktop/points-vals/s:writer", 
       :alpha => 0.6 }
> Experiment::Config[:master_dir]
=> "/Users/kubowo/Desktop/points-vals/s:writer"
> Experiment::Config::get :master_dir, :writer => 145
=> "/Users/kubowo/Desktop/points-vals/s145"

See Also:

Accessing configuration collapse

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object

Allows access to any config option by key

Examples:

Config[:decay] # looks up decay in hierarchy of config files

Parameters:

  • key (#to_s)

    to llok up in config



81
82
83
84
85
# File 'lib/experiment/config.rb', line 81

def [](key)
    @used ||= []
    @used << key.to_s
	@config[key.to_s]
end

.get(key) ⇒ Object .get(key, default) ⇒ Object .get(key, default = nil, interpolations) ⇒ Object

Allows access to any config option by key. Supports Interploations. Interpolations are supported as opts argument

Words preceded with a colon (:) are interpolated

Examples:

Config.get :existing                   #=> "hello :what"
Config.get :non_existent, "hello"      #=> "hello"
Config.get :exisitng, :what => "world" #=> "hello world"

Overloads:

  • .get(key) ⇒ Object

    Same as [].

  • .get(key, default) ⇒ Object

    Returns default if key not found in configuration.

  • .get(key, default = nil, interpolations) ⇒ Object

    Interpolates values preceded by semicolon. Otionaly second argument may be a default value to use if option not present.

    Parameters:

    • interpolations (Hash)

      key will be replaced by value.



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/experiment/config.rb', line 104

def get(v, *opts)
  @used ||= []
    @used << v.to_s
  default = opts.shift if opts.length == 2 || !opts.first.is_a?(Hash)
  out = @config[v.to_s] || default
  if opts = opts.first
      opts.keys.reduce(out.dup) do |result, inter|
        result.gsub /:#{inter}/, opts[inter]
      end
    else
	  out
 end
end

.init(env = :development) ⇒ Object

loads the main config file based on the environment



70
71
72
73
# File 'lib/experiment/config.rb', line 70

def init(env = :development)
  conf = YAML::load_file("./config/config.yaml")
  @config = conf["environments"][env.to_s]
end

.inspectString

Returns:

  • (String)


189
190
191
# File 'lib/experiment/config.rb', line 189

def inspect
  "Experiment::Config \"" + @config.to_a.map {|k,v| "#{k}: #{v}"}.join(", ") + '"'
end

.load(experiment, options, env = :development) ⇒ Object

the load method takes the basic config file, which is then overriden by the experimental config file and finally by the options string (which should be in this format: “key: value, key2:value2,key3: value3”)



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/experiment/config.rb', line 57

def load(experiment, options, env = :development)
   #init env
   @config ||= {}
	expath = File.expand_path("./experiments/#{experiment}/config.yaml")
	if File.exists? expath
		exp = YAML::load_file(expath)
	  @config.merge! exp["experiment"][env.to_s] if exp["experiment"][env.to_s].is_a? Hash
	end
	@config.merge! @override_options if @override_options
	@config.merge! parse(options)
end

.parse(options) ⇒ Object

parses a string as passed into the CLI -o option

Parameters:

  • options (String)

    should be in the form of key:value separated by commas



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/experiment/config.rb', line 134

def parse(options)
  return {} if options == ""
  Hash[options.split(/\, ?/).map do |a| 
    a = a.split /\: ?/
    case a.last
    when /^\d+$/
      a[1] = a[1].to_i
    when /^\d+\.\d+$/
      a[1] = a[1].to_f
    end
    a
  end]
end

.parsing_for_options(o, options) ⇒ Boolean

Reads all the keys in config/config.yaml and provides optparse blocks for them.

Parameters:

  • o (OptParse)

    Optparse instance to define options on.

  • options (OStruct)

    The Options instance where to save parsed config and get reserved names from.

Returns:

  • (Boolean)

    Returns true if some parses were set.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/experiment/config.rb', line 169

def parsing_for_options(o, options)
  return unless File.exists? "./config/config.yaml"
  conf = YAML::load_file("./config/config.yaml")
   num = 0
  conf["environments"].each do |env, keys|
    (keys || []).each do |key, value|
      next if options.marshal_dump.keys.include? key.to_sym
      #puts env.inspect, key.inspect, value.inspect
      num += 1
      cl = value.class == Fixnum ? Integer : value.class;
      o.on("--#{key} VALUE", cl, "Default value #{value.inspect}") do |v| 
        @override_options ||= {}
        @override_options[key] = v
       end
     end
   end
   num > 0
end

.set(opts) ⇒ Object

Mainly for use on the console for development.

Usage in experiments may result in a warning, since it may invalidate results.



124
125
126
127
128
129
# File 'lib/experiment/config.rb', line 124

def set(opts)
 @used ||= []
 opts.keys.each {|key| puts "Warning: Overwriting '#{key}' that was already used in an experiment" if @used.include? key }
 @config ||= opts
 @config.merge! opts
end

.to_hHash

returns current options that were already accessed

Returns:

  • (Hash)


150
151
152
153
# File 'lib/experiment/config.rb', line 150

def to_h
 @used ||= []
 Hash[*@config.select{|k,v| @used.include? k }.flatten]
end

.to_hashHash

returns all Config values currently loaded

Returns:

  • (Hash)


157
158
159
160
# File 'lib/experiment/config.rb', line 157

def to_hash
 @used = @config.keys
 @config
end