From 119222e1867ab97fe9004b23d6781eaba701582c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Bauduin?= Date: Tue, 6 Nov 2012 12:51:03 +0100 Subject: [PATCH 1/2] redis_prefix now only impacts one recommender When using multiple recommenders in a script, eg for cross validation, the redis_prefix was the same for input matrices of all recommenders. What's more, all recommenders had access to all input matrices of all other recommenders. This patch fixes these problems and enables testing 2 different recommenders in a script to see if their difference of performance is statistically significant. --- lib/recommendify/base.rb | 7 ++++--- spec/base_spec.rb | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/recommendify/base.rb b/lib/recommendify/base.rb index 620a3fd..a50bf94 100644 --- a/lib/recommendify/base.rb +++ b/lib/recommendify/base.rb @@ -11,7 +11,8 @@ def self.max_neighbors(n=nil) end def self.input_matrix(key, opts) - @@input_matrices[key] = opts + @@input_matrices[self.to_s] = {} if @@input_matrices[self.to_s].nil? + @@input_matrices[self.to_s][key] = opts end def self.input_matrices @@ -19,7 +20,7 @@ def self.input_matrices end def initialize - @input_matrices = Hash[self.class.input_matrices.map{ |key, opts| + @input_matrices = Hash[self.class.input_matrices[self.class.to_s].map{ |key, opts| opts.merge!(:key => key, :redis_prefix => redis_prefix) [ key, Recommendify::InputMatrix.create(opts) ] }] @@ -83,4 +84,4 @@ def delete_item!(item_id) end end -end \ No newline at end of file +end diff --git a/spec/base_spec.rb b/spec/base_spec.rb index 8d071ee..a3b7ced 100644 --- a/spec/base_spec.rb +++ b/spec/base_spec.rb @@ -24,7 +24,8 @@ it "should add an input_matrix by 'key'" do Recommendify::Base.input_matrix(:myinput, :similarity_func => :jaccard) - Recommendify::Base.send(:class_variable_get, :@@input_matrices).keys.should == [:myinput] + Recommendify::Base.send(:class_variable_get, :@@input_matrices).keys.should == [ "Recommendify::Base" ] + Recommendify::Base.send(:class_variable_get, :@@input_matrices)["Recommendify::Base"].keys.should == [:myinput] end it "should retrieve an input_matrix on a new instance" do From 36c6e024c30c7ec7f37f0eeafb066a783cd02b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Bauduin?= Date: Tue, 6 Nov 2012 13:24:48 +0100 Subject: [PATCH 2/2] make tests pass for redis_prefix correction --- lib/recommendify/base.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/recommendify/base.rb b/lib/recommendify/base.rb index a50bf94..b83cc79 100644 --- a/lib/recommendify/base.rb +++ b/lib/recommendify/base.rb @@ -20,6 +20,7 @@ def self.input_matrices end def initialize + @@input_matrices[self.class.to_s] = {} if @@input_matrices[self.class.to_s].nil? @input_matrices = Hash[self.class.input_matrices[self.class.to_s].map{ |key, opts| opts.merge!(:key => key, :redis_prefix => redis_prefix) [ key, Recommendify::InputMatrix.create(opts) ]