| 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.feature import FeatureFunction |
| 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 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 * model._weights.get(feature_value.name, 0.)) |
| 25 |
| 26 |
| 13 class LoglinearTest(LoglinearTestCase): | 27 class LoglinearTest(LoglinearTestCase): |
| 14 | 28 |
| 15 def testToFeatureFunction(self): | |
| 16 """Test that ``ToFeatureFunction`` obeys the equality its docstring says.""" | |
| 17 for x in self._X: | |
| 18 for y in self._Y(x): | |
| 19 for i in xrange(self._qty_features): | |
| 20 self.assertEqual(self._feature_list[i](x)(y), | |
| 21 self._feature_function(x)(y)[i]) | |
| 22 | |
| 23 def testLogLinearModel(self): | 29 def testLogLinearModel(self): |
| 24 """An arbitrary test to get 100% code coverage. | 30 """An arbitrary test to get 100% code coverage. |
| 25 | 31 |
| 26 Right now this test simply calls every method. The only assertions are | 32 Right now this test simply calls every method. The only assertions are |
| 27 that log-domain and normal-domain things are related appropriately; | 33 that log-domain and normal-domain things are related appropriately; |
| 28 and similarly for the quadrance and l2-norm. Since the one is defined | 34 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 | 35 in terms of the other in exactly the way written here, those should |
| 30 be trivially true. However, if the implementation changes, then they | 36 be trivially true. However, if the implementation changes, then they |
| 31 may become flaky due to floating point fuzz. Really this should be | 37 may become flaky due to floating point fuzz. Really this should be |
| 32 replaced by a collection of semantically meaningful tests, i.e., | 38 replaced by a collection of semantically meaningful tests, i.e., |
| 33 ones that actually look for bugs we might realistically need to | 39 ones that actually look for bugs we might realistically need to |
| 34 guard against. At least this test is good for detecting typo-style | 40 guard against. At least this test is good for detecting typo-style |
| 35 errors where we try accessing fields/methods that don't exist. | 41 errors where we try accessing fields/methods that don't exist. |
| 36 """ | 42 """ |
| 37 model = LogLinearModel(self._Y, self._feature_function, self._weights, 0.1) | 43 model = LogLinearModel(self._Y, self._feature_function, self._weights, 0.1) |
| 38 model.ClearAllMemos() | 44 model.ClearAllMemos() |
| 39 model = LogLinearModel(self._Y, self._feature_function, self._weights) | 45 model = LogLinearModel(self._Y, self._feature_function, self._weights) |
| 40 self.assertListEqual(self._weights, model.weights.tolist()) | 46 self.assertDictEqual(self._weights, model.weights) |
| 41 self.assertEqual(math.sqrt(model.quadrance), model.l2) | 47 self.assertEqual(math.sqrt(model.quadrance), model.l2) |
| 42 | 48 |
| 43 for x in self._X: | 49 for x in self._X: |
| 44 self.assertEqual(math.exp(model.LogZ(x)), model.Z(x)) | 50 self.assertEqual(math.exp(model.LogZ(x)), model.Z(x)) |
| 45 model.Expectation(x, lambda y: np.array([1.0])) | 51 model.Expectation(x, lambda y: np.array([1.0])) |
| 46 for y in self._Y(x): | 52 for y in self._Y(x): |
| 47 model.Features(x)(y) | 53 model.Features(x)(y) |
| 48 model.Score(x)(y) | 54 model.Score(x)(y) |
| 49 self.assertEqual( | 55 self.assertEqual( |
| 50 math.exp(model.LogProbability(x)(y)), | 56 math.exp(model.LogProbability(x)(y)), |
| 51 model.Probability(x)(y)) | 57 model.Probability(x)(y)) |
| OLD | NEW |