Class: Experiment::Config
- Inherits:
-
Object
- Object
- Experiment::Config
- 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.
Accessing configuration collapse
-
.[](key) ⇒ Object
Allows access to any config option by key.
-
.get(v, *opts) ⇒ Object
Allows access to any config option by key.
Class Method Summary collapse
-
.init(env = :development) ⇒ Object
loads the main config file based on the environment.
- .inspect ⇒ String
-
.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”).
-
.parse(options) ⇒ Object
parses a string as passed into the CLI -o option.
-
.parsing_for_options(o, options) ⇒ Boolean
Reads all the keys in config/config.yaml and provides optparse blocks for them.
-
.set(opts) ⇒ Object
Mainly for use on the console for development.
-
.to_h ⇒ Hash
returns current options that were already accessed.
-
.to_hash ⇒ Hash
returns all Config values currently loaded.
Class Method Details
.[](key) ⇒ Object
Allows access to any config option by key
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
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 |
.inspect ⇒ 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, , env = :development) #init env @config ||= {} expath = File.("./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() end |
.parse(options) ⇒ Object
parses a string as passed into the CLI -o option
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/experiment/config.rb', line 134 def parse() return {} if == "" Hash[.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.
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 (o, ) 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 .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_h ⇒ Hash
returns current options that were already accessed
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_hash ⇒ Hash
returns all Config values currently loaded
157 158 159 160 |
# File 'lib/experiment/config.rb', line 157 def to_hash @used = @config.keys @config end |