| 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 random | 5 import random |
| 6 import unittest | 6 import unittest |
| 7 | 7 |
| 8 from crash.loglinear.feature import Feature |
| 9 from crash.loglinear.feature import FeatureFunction |
| 8 from crash.loglinear.feature import FeatureValue | 10 from crash.loglinear.feature import FeatureValue |
| 9 from crash.loglinear.model import ToFeatureFunction | |
| 10 | 11 |
| 11 | 12 |
| 12 class LoglinearTestCase(unittest.TestCase): # pragma: no cover | 13 # Some arbitrary features. |
| 14 class Feature0(Feature): # pragma: no cover |
| 15 @property |
| 16 def name(self): |
| 17 return 'feature0' |
| 18 |
| 19 def __call__(self, x): |
| 20 return lambda y: FeatureValue('feature0', y == (x > 5), 'reason0', None) |
| 21 |
| 22 class Feature1(Feature): # pragma: no cover |
| 23 @property |
| 24 def name(self): |
| 25 return 'feature1' |
| 26 |
| 27 def __call__(self, x): |
| 28 return lambda y: FeatureValue('feature1', y == ((x % 2) == 1), |
| 29 'reason1', None) |
| 30 |
| 31 class Feature2(Feature): # pragma: no cover |
| 32 @property |
| 33 def name(self): |
| 34 return 'feature2' |
| 35 |
| 36 def __call__(self, x): |
| 37 return lambda y: FeatureValue('feature2', y == (x <= 7), 'reason2', None) |
| 38 |
| 39 |
| 40 class LoglinearTestCase(unittest.TestCase): # pragma: no cover |
| 13 """Common code for testing ``model.py`` and ``training.py``.""" | 41 """Common code for testing ``model.py`` and ``training.py``.""" |
| 14 | 42 |
| 15 def setUp(self): | 43 def setUp(self): |
| 16 """Set up some basic parts of our loglinear model. | 44 """Set up some basic parts of our loglinear model. |
| 17 | 45 |
| 18 These parts describe a silly model for detecting whether an integer | 46 These parts describe a silly model for detecting whether an integer |
| 19 in [0..9] is the number 7. So ``X`` is the set of integers [0..9], | 47 in [0..9] is the number 7. So ``X`` is the set of integers [0..9], |
| 20 and ``Y`` is the set of ``bool`` values. The independent variable | 48 and ``Y`` is the set of ``bool`` values. The independent variable |
| 21 is boolean-valued because we only have two categories: "yes, x == | 49 is boolean-valued because we only have two categories: "yes, x == |
| 22 7" and "no, x != 7". This doesn't take advantage of the fact that | 50 7" and "no, x != 7". This doesn't take advantage of the fact that |
| 23 loglinear models can categorize larger sets of labels, but it's good | 51 loglinear models can categorize larger sets of labels, but it's good |
| 24 enough for testing purposes. | 52 enough for testing purposes. |
| 25 | 53 |
| 26 In addition to specifying ``X`` and ``Y``, we also specify a set of | 54 In addition to specifying ``X`` and ``Y``, we also specify a set of |
| 27 features and choose some random weights for them. | 55 features and choose some random weights for them. |
| 28 """ | 56 """ |
| 29 super(LoglinearTestCase, self).setUp() | 57 super(LoglinearTestCase, self).setUp() |
| 30 | 58 |
| 31 # Some arbitrary features. | 59 self._feature_list = [Feature0(), Feature1(), Feature2()] |
| 32 # We don't use double lambdas because gpylint complains about that. | 60 self._feature_function = FeatureFunction(self._feature_list) |
| 33 def feature0(x): | |
| 34 return lambda y: FeatureValue('feature0', y == (x > 5), None, None) | |
| 35 | |
| 36 def feature1(x): | |
| 37 return lambda y: FeatureValue('feature1', y == ((x % 2) == 1), None, None) | |
| 38 | |
| 39 def feature2(x): | |
| 40 return lambda y: FeatureValue('feature2', y == (x <= 7), None, None) | |
| 41 | |
| 42 self._feature_list = [feature0, feature1, feature2] | |
| 43 self._feature_function = ToFeatureFunction(self._feature_list) | |
| 44 self._qty_features = len(self._feature_list) | 61 self._qty_features = len(self._feature_list) |
| 45 self._X = range(10) | 62 self._X = range(10) |
| 46 self._Y = lambda _x: [False, True] | 63 self._Y = lambda _x: [False, True] |
| 47 self._weights = [random.random() for _ in xrange(self._qty_features)] | 64 self._weights = {feature.name: random.random() |
| 65 for feature in self._feature_list} |
| OLD | NEW |