| Index: appengine/findit/crash/loglinear/test/model_test.py
|
| diff --git a/appengine/findit/crash/loglinear/test/model_test.py b/appengine/findit/crash/loglinear/test/model_test.py
|
| index c4fd5c5e8801d850b51ec8483ee1033f3be402e6..8bba65cb86c6c029c17fbed79861fc61890c528c 100644
|
| --- a/appengine/findit/crash/loglinear/test/model_test.py
|
| +++ b/appengine/findit/crash/loglinear/test/model_test.py
|
| @@ -2,14 +2,16 @@
|
| # Use of this source code is governed by a BSD-style license that can be
|
| # found in the LICENSE file.
|
|
|
| +import copy
|
| import math
|
| import numpy as np
|
|
|
| from crash.loglinear.feature import ChangedFile
|
| from crash.loglinear.feature import FeatureValue
|
| -from crash.loglinear.feature import FeatureFunction
|
| +from crash.loglinear.feature import WrapperMetaFeature
|
| from crash.loglinear.model import LogLinearModel
|
| from crash.loglinear.model import UnnormalizedLogLinearModel
|
| +from crash.loglinear.weight import Weight
|
| from crash.loglinear.test.loglinear_testcase import LoglinearTestCase
|
|
|
|
|
| @@ -17,56 +19,13 @@ class UnnormalizedLogLinearModelTest(LoglinearTestCase):
|
|
|
| def setUp(self):
|
| super(UnnormalizedLogLinearModelTest, self).setUp()
|
| - self.model = UnnormalizedLogLinearModel(self._feature_function,
|
| - self._weights)
|
| + self.model = UnnormalizedLogLinearModel(self._meta_feature,
|
| + self._meta_weight,
|
| + 0.00001)
|
|
|
| - def testSingleFeatureScore(self):
|
| - """Test that ``SingleFeatureScore`` returns weighted feature score."""
|
| - for feature in self._feature_list:
|
| - feature_value = feature(5)(True)
|
| - self.assertEqual(
|
| - self.model.SingleFeatureScore(feature_value),
|
| - feature_value.value * self.model._weights.get(feature_value.name, 0.))
|
| -
|
| - def testFormatReasons(self):
|
| - """Tests ``FormatReasons`` returnes a list of formated reasons."""
|
| - features = [feature(3)(False) for feature in self._feature_list]
|
| - self.assertListEqual([(feature.name, self.model.SingleFeatureScore(feature),
|
| - feature.reason) for feature in features],
|
| - self.model.FormatReasons(features))
|
| -
|
| - def testAggregateChangedFilesAggregates(self):
|
| - """Test that ``AggregateChangedFiles`` does aggregate reasons per file.
|
| -
|
| - In the main/inner loop of ``AggregateChangedFiles``: if multiple
|
| - features all blame the same file change, we try to aggregate those
|
| - reasons so that we only report the file once (with all reasons). None
|
| - of the other tests here actually check the case where the same file
|
| - is blamed multiple times, so we check that here.
|
| -
|
| - In particular, we provide the same ``FeatureValue`` twice, and
|
| - hence the same ``ChangedFile`` twice; so we should get back a single
|
| - ``ChangedFile`` but with the ``reasons`` fields concatenated.
|
| - """
|
| - file_reason = 'I blame you!'
|
| - file_blame = ChangedFile(
|
| - name = 'a.cc',
|
| - blame_url = None,
|
| - reasons = [file_reason]
|
| - )
|
| -
|
| - feature_value = FeatureValue(
|
| - name = 'dummy feature',
|
| - value = 42,
|
| - reason = 'dummy reason',
|
| - changed_files = [file_blame]
|
| - )
|
| -
|
| - expected_file_blame = file_blame._replace(reasons = [file_reason] * 2)
|
| -
|
| - self.assertListEqual(
|
| - [expected_file_blame],
|
| - self.model.AggregateChangedFiles([feature_value] * 2))
|
| + def testLogZeroish(self):
|
| + self.assertTrue(self.model.LogZeroish(-float('inf')))
|
| + self.assertFalse(self.model.LogZeroish(2.))
|
|
|
|
|
| class LoglinearTest(LoglinearTestCase):
|
| @@ -85,10 +44,9 @@ class LoglinearTest(LoglinearTestCase):
|
| guard against. At least this test is good for detecting typo-style
|
| errors where we try accessing fields/methods that don't exist.
|
| """
|
| - model = LogLinearModel(self._Y, self._feature_function, self._weights, 0.1)
|
| + model = LogLinearModel(self._Y, self._meta_feature, self._meta_weight)
|
| model.ClearAllMemos()
|
| - model = LogLinearModel(self._Y, self._feature_function, self._weights)
|
| - self.assertDictEqual(self._weights, model.weights)
|
| + self.assertEqual(self._meta_weight, model.meta_weight)
|
| self.assertEqual(math.sqrt(model.quadrance), model.l2)
|
|
|
| for x in self._X:
|
| @@ -100,3 +58,10 @@ class LoglinearTest(LoglinearTestCase):
|
| self.assertEqual(
|
| math.exp(model.LogProbability(x)(y)),
|
| model.Probability(x)(y))
|
| +
|
| + def testMetaWeightSetter(self):
|
| + model = LogLinearModel(self._Y, self._meta_feature, self._meta_weight)
|
| + new_meta_weight = copy.deepcopy(self._meta_weight)
|
| + new_meta_weight['Feature0'] = Weight(2.1)
|
| + model.meta_weight = new_meta_weight
|
| + self.assertTrue(model.meta_weight == new_meta_weight)
|
|
|