Class: Evoc::Experiment
- Inherits:
-
Object
- Object
- Evoc::Experiment
- Includes:
- Logging
- Defined in:
- lib/evoc/experiment.rb
Constant Summary collapse
- @@logger_level =
:error
Instance Attribute Summary collapse
-
#opts ⇒ Object
Returns the value of attribute opts.
Instance Method Summary collapse
-
#execute_scenarios ⇒ Object
Execute a set of scenarios.
-
#generate_queries ⇒ Object
Generates a CSV of queries according to the given options.
-
#initialize(opts = Hash.new) ⇒ Experiment
constructor
A new instance of Experiment.
- #sample_transactions ⇒ Object
Methods included from Logging
configure_logger_for, #logger, logger_for, set_level
Constructor Details
#initialize(opts = Hash.new) ⇒ Experiment
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/evoc/experiment.rb', line 8 def initialize(opts = Hash.new) self.opts = opts # Set logger level Logging.set_level(self.opts[:logger_level]) logger.debug "Initialized experiment with options: #{opts}" # setup history if !opts[:transactions].nil? Evoc::HistoryStore.initialize(path: self.opts[:transactions],case_id: self.opts[:case_id], granularity: self.opts[:granularity]) end end |
Instance Attribute Details
#opts ⇒ Object
Returns the value of attribute opts.
4 5 6 |
# File 'lib/evoc/experiment.rb', line 4 def opts @opts end |
Instance Method Details
#execute_scenarios ⇒ Object
Execute a set of scenarios
@return: json lines stream to stdout (jsonlines.org/)
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/evoc/experiment.rb', line 229 def execute_scenarios ###### # Setup factors ##### # Factor: model size aka model/learning/history size factor_model_size = self.opts[:model_size].nil? ? nil : self.opts[:model_size].map {|s| [ 'model_size',s ]} # Factor: Max size aka filtering constraint on history factor_max_size = self.opts[:max_size].nil? ? nil : self.opts[:max_size].map {|s| [ 'max_size',s ]} # Factor: Model age aka number of commits between query and last tx in history factor_model_age = self.opts[:model_age].nil? ? nil : self.opts[:model_age].map {|s| [ 'model_age',s ]} # Factor: Measures factor_measures = self.opts[:measures].map {|c| ['measures',c]} # Factor: Aggregator # if aggregation is requested, we also assume that no aggregation wants to be tested # the non-aggregation rows are specified with 'aggregator' == nil factor_aggregators = self.opts[:aggregators].nil? ? nil : (self.opts[:aggregators]+[nil]).map {|a| ['aggregator',a]} # deprecated factor, allways set to 1 for backwards compatibility # factor_permutation = self.opts[:permutation].nil? ? nil : (1..self.opts[:permutation]).to_a.map {|p| [ 'permutation',p ]} factor_permutation = [[ 'permutation',1 ]] #### # Iterate over the queries provided and execute each query in each scenario # # queries CSV header: # tx_id,tx_index,tx,query,query_size,expected_outcome,expected_outcome_size # Count the number of lines so we can properly format the output json num_lines = File.read(self.opts[:queries]).each_line.count-1 current_line = 1 # compact removes nil values (not used factors) factors = [factor_model_size,factor_max_size,factor_model_age,factor_measures,factor_permutation,factor_aggregators].compact num_of_scenarios = factors.inject(1) {|product,f| product * f.size} invalid_configuration = 0 last_error = 'no errors' CSV.foreach(self.opts[:queries], headers: true) do |query| # abort if the failsafe file is present if !self.opts[:fail_safe].nil? if File.exists?(self.opts[:fail_safe]) $stderr.puts "\nFail safe detected, exiting.." break end end # get query query_hash = query.to_h # convert query string to array of items query_hash['query'] = query_hash['query'].split(',').map(&:to_i) # verify query before executing tx = nil if tx = Evoc::HistoryStore.base_history.get_tx(id: query_hash['tx_id'],id_type: :id) if !(query_hash['query'] - tx.items).empty? raise Evoc::Exceptions::ConfigurationError.new "The query generated from #{query_hash['tx_id']} was not a subset of the same tx in the loaded history. The query was: '#{query_hash['query']}', the tx was '#{tx.items}'" end else raise Evoc::Exceptions::ConfigurationError.new "Could not find the tx: '#{query_hash['tx_id']}' from #{self.opts[:queries]} in the history #{self.opts[:transactions]}" end current_scenario = 1 # - the splat operator '*' turns the array into parameters for #product # - the block form of #product makes it lazy (i.e., the whole cartesian product isn't generated at once) factors.first.product(*factors[1..-1]).each do |scenario| params = query_hash.merge(scenario.to_h) params[:case_id] = self.opts[:case_id] params[:granularity] = self.opts[:granularity] # initialize scenario s = Evoc::Scenario.new(params) scenario_stats = {} if self.opts[:stats] scenario_stats = s.stats end # Factor: Algorithm self.opts[:algorithms].each do |algorithm| s.algorithm = algorithm # Print progress to stderr STDERR.print "(#{self.opts[:case_id]}) Executing #{algorithm} on scenario #{current_scenario} of #{num_of_scenarios} on query #{current_line} of #{num_lines}" if invalid_configuration > 0 STDERR.print " (scenarios skipped: #{invalid_configuration},last reason: #{last_error[0..20]}...) \r" else STDERR.print " \r" end begin Evoc::RecommendationCache.get_recommendation(algorithm: algorithm, query: s.query, model_start: s.model_start, model_end: s.model_end, max_size: s.max_size, aggregator: s.aggregator, measures: s.measures) Evoc::RecommendationCache.evaluate_last(evaluators: self.opts[:evaluators], topk: self.opts[:topk], unique_consequents: self.opts[:unique_consequents], expected_outcome: s.expected_outcome, measure_combination: s.measures) # build json line by merging hashes $stdout.puts s.to_h .merge(scenario_stats) .merge({topk: self.opts[:topk], date: tx.date}) .merge(Evoc::RecommendationCache.to_h(measures: s.measures)) .to_json rescue ArgumentError => e invalid_configuration += 1 last_error = e. end end current_scenario += 1 end current_line += 1 end STDERR.puts "\n(#{self.opts[:case_id]}) DONE" end |
#generate_queries ⇒ Object
Generates a CSV of queries according to the given options
CSV HEADER:
tx_id, query
113 114 115 116 117 118 119 120 121 122 123 124 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 151 152 153 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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/evoc/experiment.rb', line 113 def generate_queries # initialze a random number generator with fixed seed rand = Random.new(self.opts[:seed]) ## # write dict ## if path = self.opts[:write_dict] tmp = Tempfile.new('dict') begin tmp.puts("id,name") Evoc::HistoryStore.base_history.int_2_name.each do |id,name| tmp.puts("#{id},#{name}") end tmp.close FileUtils.mv(tmp.path,path) ensure tmp.close tmp.unlink end end ## # WRITE CSV HEADER CSV {|row| row << %W(tx_id query)} ### # Iterate over sampled tx ids CSV.foreach(self.opts[:transaction_ids_path], headers: true) do |row| tx_id = row['tx_id'] ## # GET THE TRANSACTION if tx = Evoc::HistoryStore.base_history.get_tx(id: tx_id, id_type: :id) items = tx.items tx_size = items.size ## # SAMPLE QUERIES # # We have 3 different strategies, which may produce the same sizes, # but the same size does not need to be executed several times, # so duplicates are removed specified_sizes = [] if !self.opts[:select].nil? then specified_sizes << self.opts[:select].map(&:to_i) end if !self.opts[:reverse_select].nil? then specified_sizes << self.opts[:reverse_select].map {|i| tx_size-i.to_i} end if !self.opts[:percentage].nil? then specified_sizes << self.opts[:percentage].map {|p| (p.to_f/100*tx_size).ceil} end # filter out sizes <= 1 specified_sizes.flatten!.select! {|s| s > 0} specified_sizes.uniq! random_sizes = [] if self.opts[:random_select] then random_sizes << Random.new.rand(self.opts[:minimum_query_size]..(tx_size-1)) end sampled_queries = [] # only specified sizes if random_sizes.empty? & !specified_sizes.empty? sampled_queries = specified_sizes.map {|s| items.sample(s, random: rand)} # only random sizes elsif !random_sizes.empty? & specified_sizes.empty? sampled_queries = random_sizes.map {|s| items.sample(s, random: rand)} # random + specified = randomly sample in range defined by specified # ex: # specified = [1,3,10,20] # tx size = 4 # # 1. remove X in specified that are larger than or equal to 4 # 2. randomly select X in specified = Y # 3. randomly select Y in tx elsif !random_sizes.empty? & !specified_sizes.empty? specified_sizes.select! {|s| (s < tx_size) & (s >= self.opts[:minimum_query_size])} #1. if randomly_sampled_size = specified_sizes.sample(random: rand) #2. sampled_queries = [items.sample(randomly_sampled_size, random: rand)] #3. end end if sampled_queries.empty? logger.warn "Unable to generate query from tx: #{items}, with params #{self.opts}" end ## # WRITE CSV sampled_queries.each do |query| if query.size == tx_size logger.debug "The size of the sampled query was equal to the size of the transaction, skipping.. Tx ID: #{tx_id}. Query size: #{query.size}" next end if query.size < self.opts[:minimum_query_size] next end CSV {|row| row << [tx_id,query.join(',')]} end else raise ArgumentError, "The tx with id '#{tx_id}' was not found in the history: #{self.opts[:transactions]}, wrong file?" end end #TODO possibly move this to execution ############################################# # Filter the generated queries if requested # ############################################# # # # logger.debug "Number of queries before filtering: #{query_store.size}" # # if self.opts[:filter_expected_outcome] # STDERR.puts "Filtering expected outcome.." # init_size = query_store.size # query_store.filter_expected_outcome! # STDERR.puts "Had to remove #{init_size - query_store.size} queries as a result of the expected outcome now being empty" # end # if query_store.empty? # STDERR.puts "WARNING: Returning 0 queries (maybe increase sample size?)" # end end |
#sample_transactions ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 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 |
# File 'lib/evoc/experiment.rb', line 19 def sample_transactions # initialze a random number generator with fixed seed rand = Random.new(self.opts[:seed]) # by default we can sample from the whole history sampling_history = Evoc::HistoryStore.base_history STDERR.puts "Sampling transactions from a pool of #{sampling_history.size}.." sample = [] ################################################################################# # performing filtering steps on min/max commits size and minimum previous history ################################################################################# if !self.opts[:recent].nil? size = sampling_history.size sampling_history = sampling_history[[0,size-self.opts[:recent]].max..-1] STDERR.puts " Filtering to the #{self.opts[:recent]} most recent transactions (new pool size: #{sampling_history.size})" end # filter out transactions larger than X if !self.opts[:minimum_commit_size].nil? sampling_history = sampling_history.select {|tx| tx.size >= self.opts[:minimum_commit_size]} STDERR.puts " Filtering to txes larger than or equal to #{self.opts[:minimum_commit_size]} (new pool size: #{sampling_history.size})" end if !self.opts[:maximum_commit_size].nil? sampling_history = sampling_history.select {|tx| tx.size <= self.opts[:maximum_commit_size]} STDERR.puts " Filtering to txes smaller than or equal to #{self.opts[:maximum_commit_size]} (new pool size: #{sampling_history.size})" end # only sample transactions that have at least 'minimum_history' previous history if !self.opts[:minimum_history].nil? sampling_history = sampling_history.select {|tx| tx.index >= self.opts[:minimum_history]} STDERR.puts " Filtering to txes with at least #{self.opts[:minimum_history]} previous txes (new pool size: #{sampling_history.size})" end if !self.opts[:recent_viable].nil? size = sampling_history.size sampling_history = sampling_history[[0,size-self.opts[:recent_viable]].max..-1] STDERR.puts " Filtering to the #{self.opts[:recent_viable]} most recent viable transactions (new pool size: #{sampling_history.size})" end filtering_switches = [:recent,:recent_viable,:minimum_commit_size,:maximum_commit_size,:minimum_history] if filtering_switches.any? {|s| !self.opts[s].nil?} if sampling_history.size == 0 STDERR.puts "WARNING: All transactions were filtered out, unable to sample" return [] end end if self.opts[:sample_size] > sampling_history.size STDERR.puts "WARNING: The sample size is larger than the available transactions" end ###################### # performing sampling ###################### # group the txes by size groups = sampling_history.group_by {|tx| tx.size} # sort the sample_groups option to reduce the need for maintaining control over which txes that have been sampled # i.e., random sampling is done first, then the sampled txes are removed from the sampling tx_sizes_to_sample_from = self.opts[:sample_groups].sort_by(&:to_s) tx_sizes_to_sample_from.each do |group_size| if group_size == '*' sampled_ids = sampling_history.map(&:id).sample(self.opts[:sample_size], random: rand) sample << sampled_ids STDERR.puts "Sampled #{sampled_ids.size} txes" # remove sampled txes from sampling_history filtered_hist = sampling_history.reject {|tx| sampled_ids.include? tx.id} sampling_history.clear filtered_hist.each {|tx| sampling_history << tx} elsif group_size.to_i # check if there were any txes of this size if group = groups[group_size.to_i] if group.size < self.opts[:sample_size] logger.warn "Only #{group.size} transactions found of size #{group_size}, asked for #{self.opts[:sample_size]}" end sampled_ids = group.sample(self.opts[:sample_size], random: rand).map(&:id) sample << sampled_ids STDERR.puts "Sampled #{sampled_ids.size} txes of size #{group_size}" else logger.warn "No transactions found of size #{group_size}, asked for #{self.opts[:sample_size]} (minimum history: #{self.opts[:minimum_history]})" end else raise ArgumentError.new, "Tx size for sampling must either be specified by an Integer or '*' (was #{group_size}:#{group_size.class})" end end sample.flatten.uniq end |