spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mengxr <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-2479][MLlib] Comparing floating-point n...
Date Thu, 24 Jul 2014 17:55:14 GMT
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1425#discussion_r15361772
  
    --- Diff: mllib/src/test/scala/org/apache/spark/mllib/util/TestingUtilsSuite.scala ---
    @@ -0,0 +1,264 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.mllib.util
    +
    +import org.apache.spark.mllib.linalg.Vectors
    +import org.scalatest.FunSuite
    +import org.apache.spark.mllib.util.TestingUtils._
    +import org.scalatest.exceptions.TestFailedException
    +
    +class TestingUtilsSuite extends FunSuite {
    +
    +  test("Comparing doubles using relative error in percentage.") {
    +
    +    assert(23.1 ~== 23.52 %+- 2.0)
    +    assert(23.1 ~== 22.74 %+- 2.0)
    +    assert(23.1 ~= 23.52 %+- 2.0)
    +    assert(23.1 ~= 22.74 %+- 2.0)
    +    assert(!(23.1 !~= 23.52 %+- 2.0))
    +    assert(!(23.1 !~= 22.74 %+- 2.0))
    +
    +    withClue("Should throw exception with message when test fails.") {
    +      intercept[TestFailedException] {
    +        // This will throw exception with the following message.
    +        // "Did not expect 23.1 and 23.52 to be within 2.0% using relative error."
    +        assert(23.1 !~== 23.52 %+- 2.0)
    +      }
    +      intercept[TestFailedException] {
    +        // "Did not expect 23.1 and 22.74 to be within 2.0% using relative error."
    +        assert(23.1 !~== 22.74 %+- 2.0)
    +      }
    +    }
    +
    +    assert(23.1 !~== 23.63 %+- 2.0)
    +    assert(23.1 !~== 22.34 %+- 2.0)
    +    assert(23.1 !~= 23.63 %+- 2.0)
    +    assert(23.1 !~= 22.34 %+- 2.0)
    +    assert(!(23.1 ~= 23.63 %+- 2.0))
    +    assert(!(23.1 ~= 22.34 %+- 2.0))
    +
    +    withClue("Should throw exception with message when test fails.") {
    +      intercept[TestFailedException] {
    +        // "Expected 23.1 and 23.63 to be within 2.0% using relative error."
    +        assert(23.1 ~== 23.63 %+- 2.0)
    +      }
    +      intercept[TestFailedException] {
    +        // "Expected 23.1 and 22.34 to be within 2.0% using relative error."
    +        assert(23.1 ~== 22.34 %+- 2.0)
    +      }
    +    }
    +
    +    withClue("Comparing against zero should fail the test and throw exception with message.")
{
    +      intercept[TestFailedException] {
    +        // 0.1 or 0.0 is extremely close to zero, so the relative error is meaningless.
    +        assert(0.1 ~== 0.0 %+- 3.2)
    +      }
    +      intercept[TestFailedException] {
    +        // 0.1 or 0.0 is extremely close to zero, so the relative error is meaningless.
    +        assert(0.1 ~= 0.0 %+- 3.2)
    +      }
    +      intercept[TestFailedException] {
    +        // 0.1 or 0.0 is extremely close to zero, so the relative error is meaningless.
    +        assert(0.1 !~== 0.0 %+- 3.2)
    +      }
    +      intercept[TestFailedException] {
    +        // 0.1 or 0.0 is extremely close to zero, so the relative error is meaningless.
    +        assert(0.1 !~= 0.0 %+- 3.2)
    +      }
    +      intercept[TestFailedException] {
    +        // 0.0 or 0.1 is extremely close to zero, so the relative error is meaningless.
    +        assert(0.0 ~== 0.1 %+- 3.2)
    +      }
    +      intercept[TestFailedException] {
    +        // 0.0 or 0.1 is extremely close to zero, so the relative error is meaningless.
    +        assert(0.0 ~= 0.1 %+- 3.2)
    +      }
    +      intercept[TestFailedException] {
    +        // 0.0 or 0.1 is extremely close to zero, so the relative error is meaningless.
    +        assert(0.0 !~== 0.1 %+- 3.2)
    +      }
    +      intercept[TestFailedException] {
    +        // 0.0 or 0.1 is extremely close to zero, so the relative error is meaningless.
    +        assert(0.0 !~= 0.1 %+- 3.2)
    +      }
    +    }
    +
    +    // Comparisons of numbers very close to zero.
    +    assert(10 * Double.MinPositiveValue ~== 9.5 * Double.MinPositiveValue %+- 1.0)
    +    assert(10 * Double.MinPositiveValue !~== 11 * Double.MinPositiveValue %+- 1.0)
    +
    +    assert(-Double.MinPositiveValue ~== 1.18 * -Double.MinPositiveValue %+- 1.2)
    +    assert(-Double.MinPositiveValue ~== 1.38 * -Double.MinPositiveValue %+- 1.2)
    +  }
    +
    +  test("Comparing doubles using absolute error.") {
    +
    +    assert(17.8 ~== 17.99 +- 0.2)
    +    assert(17.8 ~== 17.61 +- 0.2)
    +    assert(17.8 ~= 17.99 +- 0.2)
    +    assert(17.8 ~= 17.61 +- 0.2)
    +    assert(!(17.8 !~= 17.99 +- 0.2))
    +    assert(!(17.8 !~= 17.61 +- 0.2))
    +
    +    withClue("Should throw exception with message when test fails.") {
    +      intercept[TestFailedException] {
    +        // This will throw exception with the following message.
    +        // "Did not expect 17.8 and 17.99 to be within 0.2 using absolute error."
    +        assert(17.8 !~== 17.99 +- 0.2)
    +      }
    +      intercept[TestFailedException] {
    +        // "Did not expect 17.8 and 17.61 to be within 0.2 using absolute error."
    +        assert(17.8 !~== 17.61 +- 0.2)
    +      }
    +    }
    +
    +    assert(17.8 !~== 18.01 +- 0.2)
    +    assert(17.8 !~== 17.59 +- 0.2)
    +    assert(17.8 !~= 18.01 +- 0.2)
    +    assert(17.8 !~= 17.59 +- 0.2)
    +    assert(!(17.8 ~= 18.01 +- 0.2))
    +    assert(!(17.8 ~= 17.59 +- 0.2))
    +
    +    withClue("Should throw exception with message when test fails.") {
    +      intercept[TestFailedException] {
    +        // "Expected 17.8 and 18.01 to be within 0.2 using absolute error."
    +        assert(17.8 ~== 18.01 +- 0.2)
    +      }
    +      intercept[TestFailedException] {
    +        // "Expected 17.8 and 17.59 to be within 0.2 using absolute error."
    +        assert(17.8 ~== 17.59 +- 0.2)
    +      }
    +    }
    +
    +    // Comparisons of numbers very close to zero, and both side of zeros
    +    assert(Double.MinPositiveValue ~== 4 * Double.MinPositiveValue +- 5 * Double.MinPositiveValue)
    +    assert(Double.MinPositiveValue !~== 6 * Double.MinPositiveValue +- 5 * Double.MinPositiveValue)
    +
    +    assert(-Double.MinPositiveValue ~== 3 * Double.MinPositiveValue +- 5 * Double.MinPositiveValue)
    +    assert(Double.MinPositiveValue !~== -4 * Double.MinPositiveValue +- 5 * Double.MinPositiveValue)
    +  }
    +
    +  test("Comparing vectors using relative error in percentage.") {
    +
    +    //Comparisons of two dense vectors
    +    assert(Vectors.dense(Array(3.1, 3.5)) ~== Vectors.dense(Array(3.130, 3.534)) %+-
1.0)
    +
    +    assert(Vectors.dense(Array(3.1, 3.5)) !~== Vectors.dense(Array(3.135, 3.534)) %+-
1.0)
    +
    +    assert(Vectors.dense(Array(3.1, 3.5)) ~= Vectors.dense(Array(3.130, 3.534)) %+- 1.0)
    +
    +    assert(Vectors.dense(Array(3.1, 3.5)) !~= Vectors.dense(Array(3.135, 3.534)) %+-
1.0)
    +
    +    assert(!(Vectors.dense(Array(3.1, 3.5)) !~= Vectors.dense(Array(3.130, 3.534)) %+-
1.0))
    +
    +    assert(!(Vectors.dense(Array(3.1, 3.5)) ~= Vectors.dense(Array(3.135, 3.534)) %+-
1.0))
    +
    +    withClue("Should throw exception with message when test fails.") {
    +      intercept[TestFailedException] {
    +        // This will throw exception with the following message.
    +        // "Did not expect [3.1,3.5] and [3.13,3.534] to be within 1.0%
    +        // using relative error for all elements."
    +        assert(Vectors.dense(Array(3.1, 3.5)) !~== Vectors.dense(Array(3.130, 3.534))
%+- 1.0)
    +      }
    +      intercept[TestFailedException] {
    +        // "Expected [3.1,3.5] and [3.135,3.534] to be within 1.0%
    +        // using relative error for all elements."
    +        assert(Vectors.dense(Array(3.1, 3.5)) ~== Vectors.dense(Array(3.135, 3.534))
%+- 1.0)
    +      }
    +    }
    +
    +    withClue("Comparing against zero should fail the test and throw exception with message.")
{
    +      intercept[TestFailedException] {
    +        // "0.01 or 0.0 is extremely close to zero, so the relative error is meaningless."
    +        assert(Vectors.dense(Array(3.1, 0.01)) ~== Vectors.dense(Array(3.13, 0.0)) %+-
1.0)
    +      }
    +      intercept[TestFailedException] {
    +        // "0.01 or 0.0 is extremely close to zero, so the relative error is meaningless."
    +        assert(Vectors.dense(Array(3.1, 0.01)) ~== Vectors.sparse(2, Array(0), Array(3.13))
%+- 1.0)
    +      }
    +    }
    +
    +    // Comparisons of two sparse vectors
    +    assert(Vectors.dense(Array(3.1, 3.5)) ~==
    +      Vectors.sparse(2, Array(0, 1), Array(3.130, 3.534)) %+- 1.0)
    +
    +    assert(Vectors.dense(Array(3.1, 3.5)) !~==
    +      Vectors.sparse(2, Array(0, 1), Array(3.135, 3.534)) %+- 1.0)
    +  }
    +
    +  test("Comparing vectors using absolute error.") {
    +
    +    //Comparisons of two dense vectors
    +    assert(Vectors.dense(Array(3.1, 3.5, 0.0)) ~==
    +      Vectors.dense(Array(3.1 + 1E-8, 3.5 + 2E-7, 1E-8)) +- 1E-6)
    +
    +    assert(Vectors.dense(Array(3.1, 3.5, 0.0)) !~==
    +      Vectors.dense(Array(3.1 + 1E-5, 3.5 + 2E-7, 1 + 1E-3)) +- 1E-6)
    +
    +    assert(Vectors.dense(Array(3.1, 3.5, 0.0)) ~=
    +      Vectors.dense(Array(3.1 + 1E-8, 3.5 + 2E-7, 1E-8)) +- 1E-6)
    +
    +    assert(Vectors.dense(Array(3.1, 3.5, 0.0)) !~=
    +      Vectors.dense(Array(3.1 + 1E-5, 3.5 + 2E-7, 1 + 1E-3)) +- 1E-6)
    +
    +    assert(!(Vectors.dense(Array(3.1, 3.5, 0.0)) !~=
    +      Vectors.dense(Array(3.1 + 1E-8, 3.5 + 2E-7, 1E-8)) +- 1E-6))
    +
    +    assert(!(Vectors.dense(Array(3.1, 3.5, 0.0)) ~=
    +      Vectors.dense(Array(3.1 + 1E-5, 3.5 + 2E-7, 1 + 1E-3)) +- 1E-6))
    +
    +    withClue("Should throw exception with message when test fails.") {
    --- End diff --
    
    Is it necessary? The clue doesn't contain useful information. Maybe we can use `intercept`
directly.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message