source: orange/Orange/testing/unit/tests/test_association.py @ 9979:956ce7805994

Revision 9979:956ce7805994, 4.1 KB checked in by ales_erjavec, 2 years ago (diff)

Removed prints.

Line 
1from __future__ import division
2from Orange import associate
3from Orange.misc import testing
4from Orange.misc.testing import test_on_data, datasets_driven
5import unittest
6import pickle
7
8# helper functions
9
10def test_inducer_on(self, table):
11    rules = self.inducer(table)
12    if self.inducer.store_examples and rules:
13        self.assertIsNotNone(rules[0].examples)
14       
15    self.assertLessEqual(len(rules), self.inducer.max_item_sets)
16    for r in rules:
17        self.assertGreaterEqual(r.support, self.inducer.support)
18        self.assertIsNotNone(r.left)
19        self.assertIsNotNone(r.right)
20        self.assertAlmostEqual(r.support, r.n_applies_both / r.n_examples, places=3)
21        self.assertAlmostEqual(r.confidence, r.n_applies_both / r.n_applies_left, places=3)
22        self.assertAlmostEqual(r.coverage, r.n_applies_left / r.n_examples, places=3)
23        self.assertAlmostEqual(r.strength, r.n_applies_right / r.n_applies_left, places=3)
24        self.assertAlmostEqual(r.lift, r.n_examples * r.n_applies_both / (r.n_applies_left * r.n_applies_right), places=3)
25#        self.assertAlmostEqual(r.leverage, (r.n_examples * r.n_applies_both - r.n_applies_left * r.n_applies_right) / 100.0)
26   
27    itemsets = self.inducer.get_itemsets(table)
28    self.rules = rules
29
30
31def test_pickling_on(self, table):
32    rules = self.inducer(table)
33    rules_clone = pickle.loads(pickle.dumps(rules))
34    inducer_clone = pickle.loads(pickle.dumps(self.inducer))
35    rules_clone1 = inducer_clone(table)
36    for r1, r2, r3 in zip(rules, rules_clone, rules_clone1):
37        self.assertEqual(r1.support, r2.support)
38        self.assertEqual(r2.support, r3.support)
39       
40        self.assertEqual(r1.confidence, r2.confidence)
41        self.assertEqual(r2.confidence, r3.confidence)
42       
43        self.assertEqual(r1.coverage, r2.coverage)
44        self.assertEqual(r2.coverage, r3.coverage)
45       
46        self.assertEqual(r1.strength, r2.strength)
47        self.assertEqual(r2.strength, r3.strength)
48       
49        for inst in table:
50            self.assertEqual(r1.applies_left(inst), r2.applies_left(inst))
51            self.assertEqual(r2.applies_left(inst), r3.applies_left(inst))
52           
53            self.assertEqual(r1.applies_right(inst), r2.applies_right(inst))
54            self.assertEqual(r2.applies_right(inst), r3.applies_right(inst))
55           
56            self.assertEqual(r1.applies_both(inst), r2.applies_both(inst))
57            self.assertEqual(r2.applies_both(inst), r3.applies_both(inst))
58           
59           
60@datasets_driven(datasets=["inquisition.basket"])
61class TestSparseInducer(unittest.TestCase):
62    def setUp(self):
63        self.inducer = associate.AssociationRulesSparseInducer(support=0.5,
64                                store_examples=True, max_item_sets=2000)
65       
66    @test_on_data
67    def test_inducer_on(self, table):
68        test_inducer_on(self, table)
69
70
71    @test_on_data
72    def test_pickling_on(self, table):
73        test_pickling_on(self, table)
74               
75
76@datasets_driven(datasets=["lenses", "monks-1"])
77class TestInducer(unittest.TestCase):
78    def setUp(self):
79        self.inducer = associate.AssociationRulesInducer(support=0.2,
80                            confidence=0.5, store_examples=True,
81                            max_item_sets=2000)
82       
83    @test_on_data
84    def test_inducer_on(self, table):
85        test_inducer_on(self, table)
86       
87    @test_on_data
88    def test_pickling_on(self, table):
89        test_pickling_on(self, table)
90       
91       
92@datasets_driven(datasets=["lenses", "monks-1"])
93class TestInducerClassification(unittest.TestCase):
94    def setUp(self):
95        self.inducer = associate.AssociationRulesInducer(support=0.2,
96                            confidence=0.5, store_examples=True,
97                            max_item_sets=2000,
98                            classification_rules=True)
99       
100    @test_on_data
101    def test_inducer_on(self, table):
102        test_inducer_on(self, table)
103       
104    @test_on_data
105    def test_pickling_on(self, table):
106        test_pickling_on(self, table)
107       
108
109if __name__ is "__main__":
110    unittest.main()
111
112   
Note: See TracBrowser for help on using the repository browser.