| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import math | 5 import math |
| 6 import numpy as np | 6 import numpy as np |
| 7 | 7 |
| 8 from crash.loglinear.model import ToFeatureFunction | 8 from crash.loglinear.model import ToFeatureFunction |
| 9 from crash.loglinear.model import LogLinearModel | 9 from crash.loglinear.model import LogLinearModel |
| 10 from crash.loglinear.model import UnnormalizedLogLinearModel |
| 10 from crash.loglinear.test.loglinear_testcase import LoglinearTestCase | 11 from crash.loglinear.test.loglinear_testcase import LoglinearTestCase |
| 11 | 12 |
| 12 | 13 |
| 14 class UnnormalizedLogLinearModelTest(LoglinearTestCase): |
| 15 |
| 16 def testSingleFeatureScore(self): |
| 17 """Test that ``SingleFeatureScore`` returns weighted feature score.""" |
| 18 model = UnnormalizedLogLinearModel(self._feature_function, self._weights, |
| 19 self._feature_to_weight, 0.1) |
| 20 for feature in self._feature_list: |
| 21 feature_value = feature(5)(True) |
| 22 self.assertEqual( |
| 23 model.SingleFeatureScore(feature_value), |
| 24 feature_value.value * self._feature_to_weight.get(feature_value.name, |
| 25 0.)) |
| 26 |
| 27 |
| 13 class LoglinearTest(LoglinearTestCase): | 28 class LoglinearTest(LoglinearTestCase): |
| 14 | 29 |
| 15 def testToFeatureFunction(self): | 30 def testToFeatureFunction(self): |
| 16 """Test that ``ToFeatureFunction`` obeys the equality its docstring says.""" | 31 """Test that ``ToFeatureFunction`` obeys the equality its docstring says.""" |
| 17 for x in self._X: | 32 for x in self._X: |
| 18 for y in self._Y(x): | 33 for y in self._Y(x): |
| 19 for i in xrange(self._qty_features): | 34 for i in xrange(self._qty_features): |
| 20 self.assertEqual(self._feature_list[i](x)(y), | 35 self.assertEqual(self._feature_list[i](x)(y), |
| 21 self._feature_function(x)(y)[i]) | 36 self._feature_function(x)(y)[i]) |
| 22 | 37 |
| 23 def testLogLinearModel(self): | 38 def testLogLinearModel(self): |
| 24 """An arbitrary test to get 100% code coverage. | 39 """An arbitrary test to get 100% code coverage. |
| 25 | 40 |
| 26 Right now this test simply calls every method. The only assertions are | 41 Right now this test simply calls every method. The only assertions are |
| 27 that log-domain and normal-domain things are related appropriately; | 42 that log-domain and normal-domain things are related appropriately; |
| 28 and similarly for the quadrance and l2-norm. Since the one is defined | 43 and similarly for the quadrance and l2-norm. Since the one is defined |
| 29 in terms of the other in exactly the way written here, those should | 44 in terms of the other in exactly the way written here, those should |
| 30 be trivially true. However, if the implementation changes, then they | 45 be trivially true. However, if the implementation changes, then they |
| 31 may become flaky due to floating point fuzz. Really this should be | 46 may become flaky due to floating point fuzz. Really this should be |
| 32 replaced by a collection of semantically meaningful tests, i.e., | 47 replaced by a collection of semantically meaningful tests, i.e., |
| 33 ones that actually look for bugs we might realistically need to | 48 ones that actually look for bugs we might realistically need to |
| 34 guard against. At least this test is good for detecting typo-style | 49 guard against. At least this test is good for detecting typo-style |
| 35 errors where we try accessing fields/methods that don't exist. | 50 errors where we try accessing fields/methods that don't exist. |
| 36 """ | 51 """ |
| 37 model = LogLinearModel(self._Y, self._feature_function, self._weights, 0.1) | 52 model = LogLinearModel(self._Y, self._feature_function, self._weights, |
| 53 self._feature_to_weight, 0.1) |
| 38 model.ClearAllMemos() | 54 model.ClearAllMemos() |
| 39 model = LogLinearModel(self._Y, self._feature_function, self._weights) | 55 model = LogLinearModel(self._Y, self._feature_function, self._weights, |
| 56 self._feature_to_weight) |
| 40 self.assertListEqual(self._weights, model.weights.tolist()) | 57 self.assertListEqual(self._weights, model.weights.tolist()) |
| 41 self.assertEqual(math.sqrt(model.quadrance), model.l2) | 58 self.assertEqual(math.sqrt(model.quadrance), model.l2) |
| 42 | 59 |
| 43 for x in self._X: | 60 for x in self._X: |
| 44 self.assertEqual(math.exp(model.LogZ(x)), model.Z(x)) | 61 self.assertEqual(math.exp(model.LogZ(x)), model.Z(x)) |
| 45 model.Expectation(x, lambda y: np.array([1.0])) | 62 model.Expectation(x, lambda y: np.array([1.0])) |
| 46 for y in self._Y(x): | 63 for y in self._Y(x): |
| 47 model.Features(x)(y) | 64 model.Features(x)(y) |
| 48 model.Score(x)(y) | 65 model.Score(x)(y) |
| 49 self.assertEqual( | 66 self.assertEqual( |
| 50 math.exp(model.LogProbability(x)(y)), | 67 math.exp(model.LogProbability(x)(y)), |
| 51 model.Probability(x)(y)) | 68 model.Probability(x)(y)) |
| OLD | NEW |