| 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 import random | |
| 8 import unittest | |
| 9 | 7 |
| 10 from crash.loglinear.feature import FeatureValue | |
| 11 from crash.loglinear.model import ToFeatureFunction | 8 from crash.loglinear.model import ToFeatureFunction |
| 12 from crash.loglinear.model import LogLinearModel | 9 from crash.loglinear.model import LogLinearModel |
| 10 from crash.loglinear.test.loglinear_testcase import LoglinearTestCase |
| 13 | 11 |
| 14 | 12 |
| 15 # Some arbitrary features. | 13 class LoglinearTest(LoglinearTestCase): |
| 16 # We don't use double lambdas because gpylint complains about that. | |
| 17 def feature0(x): | |
| 18 return lambda y: FeatureValue('feature0', y == (x > 5), None, None) | |
| 19 | |
| 20 | |
| 21 def feature1(x): | |
| 22 return lambda y: FeatureValue('feature1', y == ((x % 2) == 1), None, None) | |
| 23 | |
| 24 | |
| 25 def feature2(x): | |
| 26 return lambda y: FeatureValue('feature2', y == (x <= 7), None, None) | |
| 27 | |
| 28 | |
| 29 features = [feature0, feature1, feature2] | |
| 30 X = range(10) | |
| 31 Y = [False, True] | |
| 32 | |
| 33 | |
| 34 class LoglinearTest(unittest.TestCase): | |
| 35 | 14 |
| 36 def testToFeatureFunction(self): | 15 def testToFeatureFunction(self): |
| 37 """Test that ``ToFeatureFunction`` obeys the equality its docstring says.""" | 16 """Test that ``ToFeatureFunction`` obeys the equality its docstring says.""" |
| 38 f = ToFeatureFunction(features) | 17 for x in self._X: |
| 39 for x in X: | 18 for y in self._Y: |
| 40 for y in Y: | 19 for i in xrange(self._qty_features): |
| 41 for i in xrange(len(features)): | 20 self.assertEqual(self._feature_list[i](x)(y), |
| 42 self.assertEqual(features[i](x)(y), f(x)(y)[i]) | 21 self._feature_function(x)(y)[i]) |
| 43 | 22 |
| 44 def testLogLinearModel(self): | 23 def testLogLinearModel(self): |
| 45 """An arbitrary test to get 100% code coverage. | 24 """An arbitrary test to get 100% code coverage. |
| 46 | 25 |
| 47 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 |
| 48 that log-domain and normal-domain things are related appropriately; | 27 that log-domain and normal-domain things are related appropriately; |
| 49 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 |
| 50 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 |
| 51 be trivially true. However, if the implementation changes, then they | 30 be trivially true. However, if the implementation changes, then they |
| 52 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 |
| 53 replaced by a collection of semantically meaningful tests, i.e., | 32 replaced by a collection of semantically meaningful tests, i.e., |
| 54 ones that actually look for bugs we might realistically need to | 33 ones that actually look for bugs we might realistically need to |
| 55 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 |
| 56 errors where we try accessing fields/methods that don't exist. | 35 errors where we try accessing fields/methods that don't exist. |
| 57 """ | 36 """ |
| 58 weights = [random.random() for _ in features] | 37 model = LogLinearModel(self._Y, self._feature_function, self._weights, 0.1) |
| 59 | |
| 60 model = LogLinearModel(Y, ToFeatureFunction(features), weights, 0.1) | |
| 61 model.ClearAllMemos() | 38 model.ClearAllMemos() |
| 62 model = LogLinearModel(Y, ToFeatureFunction(features), weights) | 39 model = LogLinearModel(self._Y, self._feature_function, self._weights) |
| 63 self.assertListEqual(weights, model.weights.tolist()) | 40 self.assertListEqual(self._weights, model.weights.tolist()) |
| 64 self.assertEqual(math.sqrt(model.quadrance), model.l2) | 41 self.assertEqual(math.sqrt(model.quadrance), model.l2) |
| 65 | 42 |
| 66 for x in X: | 43 for x in self._X: |
| 67 self.assertEqual(math.exp(model.LogZ(x)), model.Z(x)) | 44 self.assertEqual(math.exp(model.LogZ(x)), model.Z(x)) |
| 68 model.Expectation(x, lambda y: np.array([1.0])) | 45 model.Expectation(x, lambda y: np.array([1.0])) |
| 69 for y in Y: | 46 for y in self._Y: |
| 70 model.Features(x)(y) | 47 model.Features(x)(y) |
| 71 model.Score(x)(y) | 48 model.Score(x)(y) |
| 72 self.assertEqual( | 49 self.assertEqual( |
| 73 math.exp(model.LogProbability(x)(y)), | 50 math.exp(model.LogProbability(x)(y)), |
| 74 model.Probability(x)(y)) | 51 model.Probability(x)(y)) |
| OLD | NEW |