source: orange/Orange/testing/unit/tests/test_association.py @ 10655:a228b55811a4

Revision 10655:a228b55811a4, 4.0 KB checked in by markotoplak, 2 years ago (diff)

Moved testing from utils into testing.

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