Chromium Code Reviews| Index: appengine/findit/crash/test/loglinear_test.py |
| diff --git a/appengine/findit/crash/test/loglinear_test.py b/appengine/findit/crash/test/loglinear_test.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7b90d8f02e4a82ec4b437fdb182e8ebb99e9a299 |
| --- /dev/null |
| +++ b/appengine/findit/crash/test/loglinear_test.py |
| @@ -0,0 +1,65 @@ |
| +# Copyright 2016 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import math |
| +import numpy as np |
| +import random |
| +import unittest |
| + |
| +from crash.loglinear import ToFeatureFunction |
| +from crash.loglinear import LogLinearModel |
| + |
| + |
| +# Some arbitrary features. |
| +# We don't use double lambdas because gpylint complains about that. |
| +def feature0(x): |
| + return lambda y: 1.0 if y == (x > 5) else 0.0 |
| + |
| + |
| +def feature1(x): |
| + return lambda y: 1.0 if y == ((x % 2) == 1) else 0.0 |
| + |
| + |
| +def feature2(x): |
| + return lambda y: 1.0 if y == (x <= 7) else 0.0 |
| + |
| + |
| +features = [feature0, feature1, feature2] |
| +X = range(10) |
| +Y = [False, True] |
| + |
| + |
| +class LoglinearTest(unittest.TestCase): |
| + |
| + def testToFeatureFunction(self): |
| + """Test that ``ToFeatureFunction`` obeys the equality its docstring says.""" |
| + f = ToFeatureFunction(features) |
| + for x in X: |
| + for y in Y: |
| + for i in xrange(len(features)): |
| + self.assertEqual(features[i](x)(y), f(x)(y)[i]) |
| + |
| + # TODO(wrengr): how to make this test something reasonable? |
|
inferno
2016/12/06 18:07:06
rephrase this: Make this test reasonable in terms
|
| + def testLogLinearModel(self): |
| + """An arbitrary test to get 100% code coverage.""" |
| + weights = [random.random() for _ in features] |
| + |
| + model = LogLinearModel(Y, ToFeatureFunction(features), weights, 0.1) |
| + model.ClearAllMemos() |
| + model = LogLinearModel(Y, ToFeatureFunction(features), weights) |
| + self.assertListEqual(weights, model.weights.tolist()) |
| + # TODO(wrengr): this may be flaky due to floating point fuzz. |
|
inferno
2016/12/06 18:07:06
Just one todo for the next three is better
# TODO
|
| + self.assertEqual(math.sqrt(model.quadrance), model.l2) |
| + |
| + for x in X: |
| + # TODO(wrengr): this may be flaky due to floating point fuzz. |
| + self.assertEqual(math.exp(model.logZ(x)), model.Z(x)) |
| + model.Expectation(x, lambda y: np.array([1.0])) |
| + for y in Y: |
| + model.Features(x)(y) |
| + model.Score(x)(y) |
| + # TODO(wrengr): this may be flaky due to floating point fuzz. |
| + self.assertEqual( |
| + math.exp(model.LogProbability(x)(y)), |
| + model.Probability(x)(y)) |