Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: appengine/findit/crash/loglinear/test/weight_test.py

Issue 2625073003: [Predator] Add MetaWeight and MetaFeatureValue to group multiple weights and features together. (Closed)
Patch Set: Rebase. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2016 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import unittest
6
7 from crash.loglinear.feature import MetaFeatureValue
8 from crash.loglinear.weight import MetaWeight
9 from crash.loglinear.weight import Weight
10
11
12 class WeightTest(unittest.TestCase):
13 """Tests class ``Weight``."""
14
15 def testFloat(self):
16 """Tests convert ``Weight`` to float."""
17 self.assertEqual(float(Weight(0.8)), 0.8)
18
19 def testMultiply(self):
20 """Tests overloading operators ``__mul__`` and ``__rmul__``"""
21 self.assertEqual((Weight(0.8) * 2.0).value, 0.8 * 2.0)
22 self.assertEqual((2.0 * Weight(0.8)).value, 2.0 * 0.8)
23
24 def testEqual(self):
25 """Tests ``__eq__`` and ``__ne__``."""
26 self.assertTrue(Weight(0.2) == Weight(0.2))
27 self.assertTrue(Weight(0.2) != Weight(0.3))
28
29 def testIsZero(self):
30 """Tests ``IsZero`` method."""
31 self.assertTrue(Weight(0.00001).IsZero(0.001))
32 self.assertFalse(Weight(0.00001).IsZero(0.000001))
33
34 def testLen(self):
35 """Tests overloading operator ``__len__``."""
36 self.assertEqual(len(Weight(0.5)), 1)
37
38 def testl0(self):
39 """Tests ``l0`` property."""
40 self.assertEqual(Weight(0.2).l0, 1)
41 self.assertEqual(Weight(0.).l0, 0)
42
43 def testl1(self):
44 """Tests ``l1`` property."""
45 self.assertEqual(Weight(0.3).l1, 0.3)
46 self.assertEqual(Weight(-0.3).l1, 0.3)
47
48 def testquadrance(self):
49 """Tests ``quadrance`` property."""
50 self.assertEqual(Weight(0.3).quadrance, 0.09)
51 self.assertEqual(Weight(-0.3).quadrance, 0.09)
52
53
54 class MetaWeightTest(unittest.TestCase):
55 """Tests class ``MetaWeight``."""
56
57 def testMultiply(self):
58 """Tests overloading operators ``__mul__`` and ``__rmul__``"""
59 self.assertEqual(MetaWeight({'f1': Weight(0.8), 'f2': Weight(0.4)}) *
60 MetaFeatureValue('f', {'f1': 2., 'f2': 1.}), 2.)
61 self.assertEqual(MetaFeatureValue('f', {'f1': 0.8, 'f2': 0.4}) *
62 MetaWeight({'f1': Weight(2.), 'f2': Weight(1.)}), 2.)
63 with self.assertRaisesRegexp(Exception,
64 ('MetaWeight can only multiply with '
65 '``MetaFeatureValue`` '
66 'with the same length')):
67 dummy_result = MetaWeight({'f1': 0.8, 'f2': 0.4}) * MetaFeatureValue(
68 'f', {'f1': 2.})
69
70 def testIter(self):
71 """Tests overloading operator ``__iter__``."""
72 weights = {'a': Weight(0.2), 'b': Weight(0.4), 'c': Weight(3.2)}
73 meta_weight = MetaWeight(weights)
74 self.assertSetEqual(set(iter(meta_weight)), set(iter(weights)))
75 for key, value in meta_weight.iteritems():
76 self.assertEqual(value, weights[key])
77
78 def testLen(self):
79 """Tests overloading operator ``__len__``."""
80 self.assertEqual(len(MetaWeight({'a': Weight(0.5), 'b': Weight(0.23)})), 2)
81
82 def testIsZero(self):
83 """Tests ``IsZero`` method."""
84 self.assertTrue(MetaWeight({'f1': Weight(0.008),
85 'f2': Weight(0.00004)}).IsZero(0.01))
86 self.assertFalse(MetaWeight({'f1': Weight(0.08),
87 'f2': Weight(0.00004)}).IsZero(0.001))
88
89 def testEqual(self):
90 """Tests ``__eq__`` and ``__ne__``."""
91 self.assertTrue(MetaWeight({'f1': Weight(0.008)}) ==
92 MetaWeight({'f1': Weight(0.008)}))
93 self.assertFalse(MetaWeight({'f1': MetaWeight({'f2': Weight(0.008)})}) ==
94 MetaWeight({'f1': MetaWeight({'f2': Weight(0.1)})}))
95 self.assertFalse(MetaWeight({'f1': Weight(0.008)}) ==
96 MetaWeight({}))
97
98 def testDropZeroWeights(self):
99 meta_weight = MetaWeight({'f1': Weight(0.02),
100 'f2': MetaWeight({'f3': Weight(0.00001),
101 'f4': Weight(0.0000003)})})
102 meta_weight.DropZeroWeights(epsilon=0.0001)
103 expected_meta_weight = MetaWeight({'f1': Weight(0.02)})
104 self.assertTrue(meta_weight == expected_meta_weight)
105
106 def testl0(self):
107 """Tests ``l0`` property."""
108 self.assertEqual(MetaWeight({'a': Weight(0.2), 'b': Weight(0.),
109 'd': Weight(0.), 'e': Weight(2.)}).l0, 2)
110 self.assertEqual(MetaWeight({'a': Weight(0.), 'b': Weight(0.)}).l0, 0)
111
112 def testl1(self):
113 """Tests ``l1`` property."""
114 self.assertEqual(MetaWeight({'a': Weight(0.3), 'b': Weight(0.2)}).l1, 0.5)
115 self.assertEqual(MetaWeight({'a': Weight(0.3), 'b': Weight(-0.3)}).l1, 0.6)
116
117 def testquadrance(self):
118 """Tests ``quadrance`` property."""
119 self.assertEqual(MetaWeight({'a': Weight(0.3),
120 'b': Weight(0.2)}).quadrance, 0.13)
121 self.assertEqual(MetaWeight({'a': Weight(0.3),
122 'b': Weight(-0.3)}).quadrance, 0.18)
OLDNEW
« no previous file with comments | « appengine/findit/crash/loglinear/test/training_test.py ('k') | appengine/findit/crash/loglinear/training.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698