From c4623d9ce6d8549406effb86c44719841a08d992 Mon Sep 17 00:00:00 2001 From: Carlos Agarie Date: Tue, 17 Dec 2013 22:54:28 -0200 Subject: [PATCH 1/4] Removes continuous.png file after specs The "continuous.png" file is being created during some specs and ends up in the repository. This shouldn't happen. --- spec/id3_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/id3_spec.rb b/spec/id3_spec.rb index b86b802..828f260 100644 --- a/spec/id3_spec.rb +++ b/spec/id3_spec.rb @@ -1,6 +1,9 @@ require 'spec_helper' describe describe DecisionTree::ID3Tree do + after :each do + File.delete("continuous.png") if File.file?("continuous.png") + end describe "simple discrete case" do Given(:labels) { ["sun", "rain"]} From a8d66d0f4ad6cabe308d7cd891c990014cc41b81 Mon Sep 17 00:00:00 2001 From: Carlos Agarie Date: Thu, 19 Dec 2013 01:12:36 -0200 Subject: [PATCH 2/4] Removed graph calls in specs They're unnecessary and will be substituted by a further spec. --- spec/id3_spec.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/spec/id3_spec.rb b/spec/id3_spec.rb index 828f260..8c1eed6 100644 --- a/spec/id3_spec.rb +++ b/spec/id3_spec.rb @@ -1,9 +1,6 @@ require 'spec_helper' describe describe DecisionTree::ID3Tree do - after :each do - File.delete("continuous.png") if File.file?("continuous.png") - end describe "simple discrete case" do Given(:labels) { ["sun", "rain"]} @@ -51,7 +48,6 @@ describe describe DecisionTree::ID3Tree do end Given(:tree) { DecisionTree::ID3Tree.new(labels, data, "not angry", :continuous) } When { tree.train } - Then { tree.graph("continuous") } Then { tree.predict([7, 7]).should == "angry" } Then { tree.predict([2, 3]).should == "not angry" } end @@ -72,7 +68,6 @@ describe describe DecisionTree::ID3Tree do end Given(:tree) { DecisionTree::ID3Tree.new(labels, data, "not angry", color: :discrete, hunger: :continuous) } When { tree.train } - Then { tree.graph("continuous") } Then { tree.predict([7, "red"]).should == "angry" } Then { tree.predict([2, "blue"]).should == "not angry" } end From 3e4619604ad6513805852645d4b514021c07ff6f Mon Sep 17 00:00:00 2001 From: Carlos Agarie Date: Thu, 19 Dec 2013 02:07:29 -0200 Subject: [PATCH 3/4] Test figure creation Simply creates a tree, trains it and produces a PNG figure, testing to confirm that no exception was raised and that the image is really there. --- spec/id3_spec.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/spec/id3_spec.rb b/spec/id3_spec.rb index 8c1eed6..a875904 100644 --- a/spec/id3_spec.rb +++ b/spec/id3_spec.rb @@ -103,4 +103,23 @@ describe describe DecisionTree::ID3Tree do lambda { tree.predict([1, 1]) }.should_not raise_error } end + + describe "create a figure" do + after(:all) do + File.delete("just_a_spec.png") if File.file?("just_a_spec.png") + end + + Given(:labels) { ["sun", "rain"]} + Given(:data) do + [ + [1,0,1], + [0,1,0] + ] + end + Given(:tree) { DecisionTree::ID3Tree.new(labels, data, 1, :discrete) } + When { tree.train } + When(:result) { tree.graph("just_a_spec") } + Then { expect(result).to_not have_failed } + And { File.file?("just_a_spec.png") } + end end From 32b384420f589c42f4d4de997a259e38f28ba542 Mon Sep 17 00:00:00 2001 From: Carlos Agarie Date: Thu, 19 Dec 2013 22:25:46 -0200 Subject: [PATCH 4/4] Using a constant for the filename --- spec/id3_spec.rb | 6 +++--- spec/spec_helper.rb | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/spec/id3_spec.rb b/spec/id3_spec.rb index a875904..a27a96a 100644 --- a/spec/id3_spec.rb +++ b/spec/id3_spec.rb @@ -106,7 +106,7 @@ describe describe DecisionTree::ID3Tree do describe "create a figure" do after(:all) do - File.delete("just_a_spec.png") if File.file?("just_a_spec.png") + File.delete("#{FIGURE_FILENAME}.png") if File.file?("#{FIGURE_FILENAME}.png") end Given(:labels) { ["sun", "rain"]} @@ -118,8 +118,8 @@ describe describe DecisionTree::ID3Tree do end Given(:tree) { DecisionTree::ID3Tree.new(labels, data, 1, :discrete) } When { tree.train } - When(:result) { tree.graph("just_a_spec") } + When(:result) { tree.graph(FIGURE_FILENAME) } Then { expect(result).to_not have_failed } - And { File.file?("just_a_spec.png") } + And { File.file?("#{FIGURE_FILENAME}.png") } end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7d706da..04de356 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ require 'rspec/given' require 'decisiontree' require 'pry' + +FIGURE_FILENAME = "just_a_spec"