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