| OLD | NEW |
| (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 self.assertSetEqual(set(meta_weight.iteritems()), set(weights.iteritems())) |
| 76 self.assertSetEqual(set(meta_weight.itervalues()), |
| 77 set(weights.itervalues())) |
| 78 |
| 79 def testLen(self): |
| 80 """Tests overloading operator ``__len__``.""" |
| 81 self.assertEqual(len(MetaWeight({'a': Weight(0.5), 'b': Weight(0.23)})), 2) |
| 82 |
| 83 def testIsZero(self): |
| 84 """Tests ``IsZero`` method.""" |
| 85 self.assertTrue(MetaWeight({'f1': Weight(0.008), |
| 86 'f2': Weight(0.00004)}).IsZero(0.01)) |
| 87 self.assertFalse(MetaWeight({'f1': Weight(0.08), |
| 88 'f2': Weight(0.00004)}).IsZero(0.001)) |
| 89 |
| 90 def testEqual(self): |
| 91 """Tests ``__eq__`` and ``__ne__``.""" |
| 92 self.assertTrue(MetaWeight({'f1': Weight(0.008)}) == |
| 93 MetaWeight({'f1': Weight(0.008)})) |
| 94 self.assertFalse(MetaWeight({'f1': MetaWeight({'f2': Weight(0.008)})}) == |
| 95 MetaWeight({'f1': MetaWeight({'f2': Weight(0.1)})})) |
| 96 self.assertFalse(MetaWeight({'f1': Weight(0.008)}) == |
| 97 MetaWeight({})) |
| 98 |
| 99 def testDropZeroWeights(self): |
| 100 meta_weight = MetaWeight({'f1': Weight(0.02), |
| 101 'f2': MetaWeight({'f3': Weight(0.00001), |
| 102 'f4': Weight(0.0000003)})}) |
| 103 meta_weight.DropZeroWeights(epsilon=0.0001) |
| 104 expected_meta_weight = MetaWeight({'f1': Weight(0.02)}) |
| 105 self.assertTrue(meta_weight == expected_meta_weight) |
| 106 |
| 107 def testl0(self): |
| 108 """Tests ``l0`` property.""" |
| 109 self.assertEqual(MetaWeight({'a': Weight(0.2), 'b': Weight(0.), |
| 110 'd': Weight(0.), 'e': Weight(2.)}).l0, 2) |
| 111 self.assertEqual(MetaWeight({'a': Weight(0.), 'b': Weight(0.)}).l0, 0) |
| 112 |
| 113 def testl1(self): |
| 114 """Tests ``l1`` property.""" |
| 115 self.assertEqual(MetaWeight({'a': Weight(0.3), 'b': Weight(0.2)}).l1, 0.5) |
| 116 self.assertEqual(MetaWeight({'a': Weight(0.3), 'b': Weight(-0.3)}).l1, 0.6) |
| 117 |
| 118 def testquadrance(self): |
| 119 """Tests ``quadrance`` property.""" |
| 120 self.assertEqual(MetaWeight({'a': Weight(0.3), |
| 121 'b': Weight(0.2)}).quadrance, 0.13) |
| 122 self.assertEqual(MetaWeight({'a': Weight(0.3), |
| 123 'b': Weight(-0.3)}).quadrance, 0.18) |
| OLD | NEW |