class Evoc::RecommendationCache

Attributes

base_recommendation[RW]
evaluation[RW]
filtered_model_size[RW]
last_recommendation[RW]
tag[RW]
time_aggregation[RW]
time_measurecalculation[RW]
time_rulegeneration[RW]

Public Class Methods

evaluate_last(evaluators: ,expected_outcome:,measure_combination:,topk: nil, unique_consequents: nil) click to toggle source

Evaluate the currently cached recommendation

@param [Array<String>] evaluators the evaluators to apply @param [Array<String>] expected_outcome the expected outcome to use in evaluations @param [Array<String>] measure_combinations the list of measures to use when sorting a recommendation before evaluating

@return [Hash[evaluator]] the hash of results

# File lib/evoc/recommendation_cache.rb, line 79
def self.evaluate_last(evaluators: ,expected_outcome:,measure_combination:,topk: nil, unique_consequents: nil)
  if !self.last_recommendation.nil?
      self.evaluation = self.last_recommendation.evaluate_with(evaluators: evaluators,
                                                    topk: topk,
                                                    unique_consequents: unique_consequents,
                                                    expected_outcome: expected_outcome,
                                                    measure_combination: measure_combination)
  else
    STDERR.puts "TAG = #{self.tag}No recommendation to evaluate"
  end
end
get_recommendation(algorithm:, query:, model_start:, model_end:, max_size: nil, aggregator: nil, measures: []) click to toggle source
# File lib/evoc/recommendation_cache.rb, line 24
def self.get_recommendation(algorithm:,
                            query:,
                            model_start:,
                            model_end:,
                            max_size: nil,
                            aggregator: nil,
                            measures: [])
  # check if a new base recommendation needs to be generated
    tag = [algorithm,query,model_start,model_end,max_size].hash
    if self.tag != tag
        # clear out any evaluation done
        self.evaluation = Hash.new
        # new recommendation
        logger.debug "Caching new recommendation: algorithm: #{algorithm}, query: #{query}, model_start/end: #{model_start} - #{model_end}, maxsize: #{max_size}"
        self.tag = tag
        tx_store = Evoc::HistoryStore.get_history(model_start,
                                                     model_end,
                                                     max_size)
        self.filtered_model_size = tx_store.size

        t1 = Time.new
        self.base_recommendation = Evoc::Algorithm.execute(tx_store: tx_store,
                                                     query: query,
                                                     algorithm: algorithm)
        self.last_recommendation = self.base_recommendation
        t2 = Time.new
        self.time_rulegeneration = TimeDifference.between(t1,t2).in_seconds.round(8)
    end

    # calculate measures on rules
    t1 = Time.new
    self.base_recommendation.calculate_measures(measures)
    t2 = Time.new
    self.time_measurecalculation = TimeDifference.between(t1,t2).in_seconds.round(8)

    # perform aggregation
    if !aggregator.nil?
      t1 = Time.new
      self.last_recommendation = self.base_recommendation.aggregate_by(aggregator: aggregator.to_sym,measures: measures) {|r| r.rhs}
      t2 = Time.new
      self.time_aggregation = TimeDifference.between(t1,t2).in_seconds.round(8)
    else
      self.last_recommendation = self.base_recommendation
    end
    return self.last_recommendation
end
recommendation_cached?(algorithm:, query:, model_start:, model_end:, max_size: nil) click to toggle source
# File lib/evoc/recommendation_cache.rb, line 15
def self.recommendation_cached?(algorithm:,
                            query:,
                            model_start:,
                            model_end:,
                            max_size: nil)
    return self.tag == [algorithm,query,model_start,model_end,max_size].hash
end
to_h(measures: Evoc::Rule.measures) click to toggle source

format:

{
    time: 'execution time',
    filtered_model_size: 
    number_of_rules :
    average_precision: 
    rules: [
      {
        lhs: [lhs]
        rhs: [rhs],
        measures: {
          measure_1: value,
          measure_n: value
        }
      },
      ..next rule..
]
}

measures: the interestingness measures that you want to output in the hash

# File lib/evoc/recommendation_cache.rb, line 112
def self.to_h(measures: Evoc::Rule.measures)
    recommendation_hash = Hash.new
    recommendation_hash[:recommendation_tag] = self.tag
    recommendation_hash[:time_rulegeneration] = self.time_rulegeneration
    recommendation_hash[:time_measurecalculation] = self.time_measurecalculation
    recommendation_hash[:time_aggregation] = self.time_aggregation
    recommendation_hash[:filtered_model_size] = self.filtered_model_size
    recommendation_hash[:number_of_baserules] = self.base_recommendation.size
    recommendation_hash[:number_of_rules] = self.last_recommendation.size
    recommendation_hash[:aggregator] = self.last_recommendation.aggregator
    recommendation_hash[:number_of_hyper_rules] = self.last_recommendation.number_of_hyper_rules
    recommendation_hash[:mean_hyper_coefficient] = self.last_recommendation.inject(0.0){ |sum, r| 
      sum + r.get_measure('m_hyper_coefficient').value } / self.last_recommendation.size
    recommendation_hash[:largest_antecedent] = self.last_recommendation.largest_antecedent
    if !self.evaluation.nil?
      self.evaluation.each do |evaluator,results|
        recommendation_hash[evaluator] = results['value']
        # time can also be added like this:
        # recommendation_hash[evaluator+'_time'] = results['time']
      end
    end
    recommendation_hash[:rules] = []
    self.last_recommendation.each do |rule|
        rule_hash = Hash.new
        rule_hash[:lhs] = rule.lhs.is_a?(String) ? rule.lhs : rule.lhs.join(',')
        rule_hash[:rhs] = rule.rhs.is_a?(String) ? rule.rhs : rule.rhs.join(',')
        rule_hash[:measures] = Hash.new
        measures.each do |m| 
            if rule.measure_instantiated?(m)
                rule_hash[:measures][m] = rule.get_measure(m).value
            end
        end
        recommendation_hash[:rules] << rule_hash    
    end
    return recommendation_hash
end