| 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 ChangedFile |
| 8 from crash.loglinear.feature import Feature | 9 from crash.loglinear.feature import Feature |
| 9 from crash.loglinear.feature import FeatureFunction | |
| 10 from crash.loglinear.feature import FeatureValue | 10 from crash.loglinear.feature import FeatureValue |
| 11 | 11 from crash.loglinear.feature import MetaFeature |
| 12 from crash.loglinear.feature import MetaFeatureValue |
| 13 from crash.loglinear.feature import WrapperMetaFeature |
| 14 from crash.loglinear.weight import MetaWeight |
| 15 from crash.loglinear.weight import Weight |
| 12 | 16 |
| 13 # Some arbitrary features. | 17 # Some arbitrary features. |
| 14 class Feature0(Feature): # pragma: no cover | 18 class Feature0(Feature): # pragma: no cover |
| 15 @property | 19 @property |
| 16 def name(self): | 20 def name(self): |
| 17 return 'feature0' | 21 return 'Feature0' |
| 18 | 22 |
| 19 def __call__(self, x): | 23 def __call__(self, x): |
| 20 return lambda y: FeatureValue('feature0', y == (x > 5), 'reason0', None) | 24 return lambda y: FeatureValue(self.name, y == (x > 5), 'reason0', |
| 25 [ChangedFile(name='a.cc', |
| 26 blame_url=None, |
| 27 reasons=['file_reason0']), |
| 28 ChangedFile(name='b.cc', |
| 29 blame_url=None, |
| 30 reasons=['file_reason0'])]) |
| 31 |
| 21 | 32 |
| 22 class Feature1(Feature): # pragma: no cover | 33 class Feature1(Feature): # pragma: no cover |
| 23 @property | 34 @property |
| 24 def name(self): | 35 def name(self): |
| 25 return 'feature1' | 36 return 'Feature1' |
| 26 | 37 |
| 27 def __call__(self, x): | 38 def __call__(self, x): |
| 28 return lambda y: FeatureValue('feature1', y == ((x % 2) == 1), | 39 return lambda y: FeatureValue(self.name, y == ((x % 2) == 1), 'reason1', |
| 29 'reason1', None) | 40 [ChangedFile(name='b.cc', |
| 41 blame_url=None, |
| 42 reasons=['file_reason1'])]) |
| 43 |
| 30 | 44 |
| 31 class Feature2(Feature): # pragma: no cover | 45 class Feature2(Feature): # pragma: no cover |
| 32 @property | 46 @property |
| 33 def name(self): | 47 def name(self): |
| 34 return 'feature2' | 48 return 'Feature2' |
| 35 | 49 |
| 36 def __call__(self, x): | 50 def __call__(self, x): |
| 37 return lambda y: FeatureValue('feature2', y == (x <= 7), 'reason2', None) | 51 return lambda y: FeatureValue(self.name, y == (x <= 7), 'reason2', None) |
| 52 |
| 53 |
| 54 class Feature3(Feature): # pragma: no cover |
| 55 @property |
| 56 def name(self): |
| 57 return 'Feature3' |
| 58 |
| 59 def __call__(self, x): |
| 60 return lambda y: FeatureValue(self.name, y == ((x % 3) == 0), None, None) |
| 61 |
| 62 |
| 63 class Feature4(Feature): # pragma: no cover |
| 64 @property |
| 65 def name(self): |
| 66 return 'Feature4' |
| 67 |
| 68 def __call__(self, x): |
| 69 return lambda y: FeatureValue(self.name, y == (x < 9), None, None) |
| 38 | 70 |
| 39 | 71 |
| 40 class LoglinearTestCase(unittest.TestCase): # pragma: no cover | 72 class LoglinearTestCase(unittest.TestCase): # pragma: no cover |
| 41 """Common code for testing ``model.py`` and ``training.py``.""" | 73 """Common code for testing ``model.py`` and ``training.py``.""" |
| 42 | 74 |
| 43 def setUp(self): | 75 def setUp(self): |
| 44 """Set up some basic parts of our loglinear model. | 76 """Set up some basic parts of our loglinear model. |
| 45 | 77 |
| 46 These parts describe a silly model for detecting whether an integer | 78 These parts describe a silly model for detecting whether an integer |
| 47 in [0..9] is the number 7. So ``X`` is the set of integers [0..9], | 79 in [0..9] is the number 7. So ``X`` is the set of integers [0..9], |
| 48 and ``Y`` is the set of ``bool`` values. The independent variable | 80 and ``Y`` is the set of ``bool`` values. The independent variable |
| 49 is boolean-valued because we only have two categories: "yes, x == | 81 is boolean-valued because we only have two categories: "yes, x == |
| 50 7" and "no, x != 7". This doesn't take advantage of the fact that | 82 7" and "no, x != 7". This doesn't take advantage of the fact that |
| 51 loglinear models can categorize larger sets of labels, but it's good | 83 loglinear models can categorize larger sets of labels, but it's good |
| 52 enough for testing purposes. | 84 enough for testing purposes. |
| 53 | 85 |
| 54 In addition to specifying ``X`` and ``Y``, we also specify a set of | 86 In addition to specifying ``X`` and ``Y``, we also specify a set of |
| 55 features and choose some random weights for them. | 87 features and choose some random weights for them. |
| 56 """ | 88 """ |
| 57 super(LoglinearTestCase, self).setUp() | 89 super(LoglinearTestCase, self).setUp() |
| 58 | 90 |
| 59 self._feature_list = [Feature0(), Feature1(), Feature2()] | 91 self._meta_feature = WrapperMetaFeature([Feature0(), |
| 60 self._feature_function = FeatureFunction(self._feature_list) | 92 Feature1(), |
| 61 self._qty_features = len(self._feature_list) | 93 Feature2(), |
| 94 WrapperMetaFeature([Feature3(), |
| 95 Feature4()])]) |
| 96 self._meta_weight = MetaWeight( |
| 97 { |
| 98 'Feature0': Weight(random.random()), |
| 99 'Feature1': Weight(random.random()), |
| 100 'Feature2': Weight(random.random()), |
| 101 'WrapperFeature': MetaWeight( |
| 102 { |
| 103 'Feature3': Weight(random.random()), |
| 104 'Feature4': Weight(random.random()) |
| 105 }) |
| 106 }) |
| 107 |
| 62 self._X = range(10) | 108 self._X = range(10) |
| 63 self._Y = lambda _x: [False, True] | 109 self._Y = lambda _x: [False, True] |
| 64 self._weights = {feature.name: random.random() | |
| 65 for feature in self._feature_list} | |
| OLD | NEW |