Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Unified Diff: appengine/findit/crash/loglinear/test/model_test.py

Issue 2617273002: [Predator] Move ``SingleFeatureScore`` to LLM. (Closed)
Patch Set: Address comment. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 17d8e6e4b85a3b0a2cf9bb9e235042f119b6f750..c4fd5c5e8801d850b51ec8483ee1033f3be402e6 100644
--- a/appengine/findit/crash/loglinear/test/model_test.py
+++ b/appengine/findit/crash/loglinear/test/model_test.py
@@ -5,20 +5,71 @@
import math
import numpy as np
-from crash.loglinear.model import ToFeatureFunction
+from crash.loglinear.feature import ChangedFile
+from crash.loglinear.feature import FeatureValue
+from crash.loglinear.feature import FeatureFunction
from crash.loglinear.model import LogLinearModel
+from crash.loglinear.model import UnnormalizedLogLinearModel
from crash.loglinear.test.loglinear_testcase import LoglinearTestCase
-class LoglinearTest(LoglinearTestCase):
+class UnnormalizedLogLinearModelTest(LoglinearTestCase):
- def testToFeatureFunction(self):
- """Test that ``ToFeatureFunction`` obeys the equality its docstring says."""
- for x in self._X:
- for y in self._Y(x):
- for i in xrange(self._qty_features):
- self.assertEqual(self._feature_list[i](x)(y),
- self._feature_function(x)(y)[i])
+ def setUp(self):
+ super(UnnormalizedLogLinearModelTest, self).setUp()
+ self.model = UnnormalizedLogLinearModel(self._feature_function,
+ self._weights)
+
+ 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))
+
+
+class LoglinearTest(LoglinearTestCase):
def testLogLinearModel(self):
"""An arbitrary test to get 100% code coverage.
@@ -37,7 +88,7 @@ class LoglinearTest(LoglinearTestCase):
model = LogLinearModel(self._Y, self._feature_function, self._weights, 0.1)
model.ClearAllMemos()
model = LogLinearModel(self._Y, self._feature_function, self._weights)
- self.assertListEqual(self._weights, model.weights.tolist())
+ self.assertDictEqual(self._weights, model.weights)
self.assertEqual(math.sqrt(model.quadrance), model.l2)
for x in self._X:

Powered by Google App Engine
This is Rietveld 408576698