You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by tr...@apache.org on 2015/04/01 11:21:18 UTC
[3/3] flink git commit: [FLINK-1718] [ml] Unifies existing test cases
[FLINK-1718] [ml] Unifies existing test cases
This closes #539.
Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/d2e2d79f
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/d2e2d79f
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/d2e2d79f
Branch: refs/heads/master
Commit: d2e2d79fc0052c064188940520c93bbd0c1b1d4b
Parents: 5ddb2dd
Author: Till Rohrmann <tr...@apache.org>
Authored: Tue Mar 31 17:01:12 2015 +0200
Committer: Till Rohrmann <tr...@apache.org>
Committed: Wed Apr 1 10:56:57 2015 +0200
----------------------------------------------------------------------
.../apache/flink/ml/math/BreezeMathSuite.scala | 68 ++++++++++
.../apache/flink/ml/math/BreezeMathTest.scala | 69 -----------
.../apache/flink/ml/math/DenseMatrixSuite.scala | 86 +++++++++++++
.../apache/flink/ml/math/DenseMatrixTest.scala | 89 -------------
.../apache/flink/ml/math/DenseVectorSuite.scala | 50 ++++++++
.../apache/flink/ml/math/DenseVectorTest.scala | 52 --------
.../flink/ml/math/SparseMatrixSuite.scala | 121 ++++++++++++++++++
.../apache/flink/ml/math/SparseMatrixTest.scala | 124 -------------------
.../flink/ml/math/SparseVectorSuite.scala | 90 ++++++++++++++
.../apache/flink/ml/math/SparseVectorTest.scala | 93 --------------
10 files changed, 415 insertions(+), 427 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/flink/blob/d2e2d79f/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/BreezeMathSuite.scala
----------------------------------------------------------------------
diff --git a/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/BreezeMathSuite.scala b/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/BreezeMathSuite.scala
new file mode 100644
index 0000000..b03f08f
--- /dev/null
+++ b/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/BreezeMathSuite.scala
@@ -0,0 +1,68 @@
+/*
+ * 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.flink.ml.math
+
+import Breeze._
+
+import org.scalatest.{Matchers, FlatSpec}
+
+class BreezeMathSuite extends FlatSpec with Matchers {
+
+ behavior of "Breeze vector conversion"
+
+ it should "convert a DenseMatrix into breeze.linalg.DenseMatrix and vice versa" in {
+ val numRows = 5
+ val numCols = 4
+
+ val data = Array.range(0, numRows * numCols)
+ val expectedData = Array.range(0, numRows * numCols).map(_ * 2)
+
+ val denseMatrix = DenseMatrix(numRows, numCols, data)
+ val expectedMatrix = DenseMatrix(numRows, numCols, expectedData)
+
+ val m = denseMatrix.asBreeze
+
+ val result = (m * 2.0).fromBreeze
+
+ result should equal(expectedMatrix)
+ }
+
+ it should "convert a SparseMatrix into breeze.linalg.CSCMatrix" in {
+ val numRows = 5
+ val numCols = 4
+
+ val sparseMatrix = SparseMatrix.fromCOO(numRows, numCols,
+ (0, 1, 1),
+ (4, 3, 13),
+ (3, 2, 45),
+ (4, 0, 12))
+
+ val expectedMatrix = SparseMatrix.fromCOO(numRows, numCols,
+ (0, 1, 2),
+ (4, 3, 26),
+ (3, 2, 90),
+ (4, 0, 24))
+
+ val sm = sparseMatrix.asBreeze
+
+ val result = (sm * 2.0).fromBreeze
+
+ result should equal(expectedMatrix)
+ }
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/d2e2d79f/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/BreezeMathTest.scala
----------------------------------------------------------------------
diff --git a/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/BreezeMathTest.scala b/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/BreezeMathTest.scala
deleted file mode 100644
index 7084f2a..0000000
--- a/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/BreezeMathTest.scala
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- * 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.flink.ml.math
-
-import Breeze._
-
-import org.junit.Test
-import org.scalatest.ShouldMatchers
-
-class BreezeMathTest extends ShouldMatchers {
-
- @Test
- def testBreezeDenseMatrixWrapping: Unit = {
- val numRows = 5
- val numCols = 4
-
- val data = Array.range(0, numRows * numCols)
- val expectedData = Array.range(0, numRows * numCols).map(_ * 2)
-
- val denseMatrix = DenseMatrix(numRows, numCols, data)
- val expectedMatrix = DenseMatrix(numRows, numCols, expectedData)
-
- val m = denseMatrix.asBreeze
-
- val result = (m * 2.0).fromBreeze
-
- result should equal(expectedMatrix)
- }
-
- @Test
- def testBreezeSparseMatrixWrapping: Unit = {
- val numRows = 5
- val numCols = 4
-
- val sparseMatrix = SparseMatrix.fromCOO(numRows, numCols,
- (0, 1, 1),
- (4, 3, 13),
- (3, 2, 45),
- (4, 0, 12))
-
- val expectedMatrix = SparseMatrix.fromCOO(numRows, numCols,
- (0, 1, 2),
- (4, 3, 26),
- (3, 2, 90),
- (4, 0, 24))
-
- val sm = sparseMatrix.asBreeze
-
- val result = (sm * 2.0).fromBreeze
-
- result should equal(expectedMatrix)
- }
-}
http://git-wip-us.apache.org/repos/asf/flink/blob/d2e2d79f/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/DenseMatrixSuite.scala
----------------------------------------------------------------------
diff --git a/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/DenseMatrixSuite.scala b/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/DenseMatrixSuite.scala
new file mode 100644
index 0000000..ca3d601
--- /dev/null
+++ b/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/DenseMatrixSuite.scala
@@ -0,0 +1,86 @@
+/*
+ * 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.flink.ml.math
+
+import org.scalatest.{Matchers, FlatSpec}
+
+class DenseMatrixSuite extends FlatSpec with Matchers {
+
+ behavior of "Flink's DenseMatrix"
+
+ it should "contain the initialization data" in {
+ val numRows = 10
+ val numCols = 13
+
+ val data = Array.range(0, numRows*numCols)
+
+ val matrix = DenseMatrix(numRows, numCols, data)
+
+ assertResult(numRows)(matrix.numRows)
+ assertResult(numCols)(matrix.numCols)
+
+ for(row <- 0 until numRows; col <- 0 until numCols) {
+ assertResult(data(col*numRows + row))(matrix(row, col))
+ }
+ }
+
+ it should "fail in case of invalid element access" in {
+ val numRows = 10
+ val numCols = 13
+
+ val matrix = DenseMatrix.zeros(numRows, numCols)
+
+ intercept[IllegalArgumentException] {
+ matrix(-1, 2)
+ }
+
+ intercept[IllegalArgumentException] {
+ matrix(0, -1)
+ }
+
+ intercept[IllegalArgumentException] {
+ matrix(numRows, 0)
+ }
+
+ intercept[IllegalArgumentException] {
+ matrix(0, numCols)
+ }
+
+ intercept[IllegalArgumentException] {
+ matrix(numRows, numCols)
+ }
+ }
+
+ it should "be copyable" in {
+ val numRows = 4
+ val numCols = 5
+
+ val data = Array.range(0, numRows*numCols)
+
+ val denseMatrix = DenseMatrix.apply(numRows, numCols, data)
+
+ val copy = denseMatrix.copy
+
+ denseMatrix should equal(copy)
+
+ copy(0, 0) = 1
+
+ denseMatrix should not equal(copy)
+ }
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/d2e2d79f/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/DenseMatrixTest.scala
----------------------------------------------------------------------
diff --git a/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/DenseMatrixTest.scala b/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/DenseMatrixTest.scala
deleted file mode 100644
index 12001fc..0000000
--- a/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/DenseMatrixTest.scala
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * 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.flink.ml.math
-
-import org.junit.Test
-import org.scalatest.ShouldMatchers
-
-class DenseMatrixTest extends ShouldMatchers {
-
- @Test
- def testDataAfterInitialization: Unit = {
- val numRows = 10
- val numCols = 13
-
- val data = Array.range(0, numRows*numCols)
-
- val matrix = DenseMatrix(numRows, numCols, data)
-
- assertResult(numRows)(matrix.numRows)
- assertResult(numCols)(matrix.numCols)
-
- for(row <- 0 until numRows; col <- 0 until numCols) {
- assertResult(data(col*numRows + row))(matrix(row, col))
- }
- }
-
- @Test
- def testIllegalArgumentExceptionInCaseOfInvalidIndexAccess: Unit = {
- val numRows = 10
- val numCols = 13
-
- val matrix = DenseMatrix.zeros(numRows, numCols)
-
- intercept[IllegalArgumentException] {
- matrix(-1, 2)
- }
-
- intercept[IllegalArgumentException] {
- matrix(0, -1)
- }
-
- intercept[IllegalArgumentException] {
- matrix(numRows, 0)
- }
-
- intercept[IllegalArgumentException] {
- matrix(0, numCols)
- }
-
- intercept[IllegalArgumentException] {
- matrix(numRows, numCols)
- }
- }
-
- @Test
- def testCopy: Unit = {
- val numRows = 4
- val numCols = 5
-
- val data = Array.range(0, numRows*numCols)
-
- val denseMatrix = DenseMatrix.apply(numRows, numCols, data)
-
- val copy = denseMatrix.copy
-
-
- denseMatrix should equal(copy)
-
- copy(0, 0) = 1
-
- denseMatrix should not equal(copy)
- }
-}
http://git-wip-us.apache.org/repos/asf/flink/blob/d2e2d79f/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/DenseVectorSuite.scala
----------------------------------------------------------------------
diff --git a/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/DenseVectorSuite.scala b/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/DenseVectorSuite.scala
new file mode 100644
index 0000000..553f672
--- /dev/null
+++ b/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/DenseVectorSuite.scala
@@ -0,0 +1,50 @@
+/*
+ * 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.flink.ml.math
+
+import org.scalatest.{Matchers, FlatSpec}
+
+class DenseVectorSuite extends FlatSpec with Matchers {
+
+ behavior of "Flink's DenseVector"
+
+ it should "contain the initialization data" in {
+ val data = Array.range(1,10)
+
+ val vector = DenseVector(data)
+
+ assertResult(data.length)(vector.size)
+
+ data.zip(vector.map(_._2)).foreach{case (expected, actual) => assertResult(expected)(actual)}
+ }
+
+ it should "fail in case of an illegal element access" in {
+ val size = 10
+
+ val vector = DenseVector.zeros(size)
+
+ intercept[IllegalArgumentException] {
+ vector(-1)
+ }
+
+ intercept[IllegalArgumentException] {
+ vector(size)
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/d2e2d79f/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/DenseVectorTest.scala
----------------------------------------------------------------------
diff --git a/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/DenseVectorTest.scala b/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/DenseVectorTest.scala
deleted file mode 100644
index 66a51fe..0000000
--- a/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/DenseVectorTest.scala
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * 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.flink.ml.math
-
-import org.junit.Test
-import org.scalatest.ShouldMatchers
-
-
-class DenseVectorTest extends ShouldMatchers {
-
- @Test
- def testDataAfterInitialization {
- val data = Array.range(1,10)
-
- val vector = DenseVector(data)
-
- assertResult(data.length)(vector.size)
-
- data.zip(vector.map(_._2)).foreach{case (expected, actual) => assertResult(expected)(actual)}
- }
-
- @Test
- def testIllegalArgumentExceptionInCaseOfIllegalIndexAccess {
- val size = 10
-
- val vector = DenseVector.zeros(size)
-
- intercept[IllegalArgumentException] {
- vector(-1)
- }
-
- intercept[IllegalArgumentException] {
- vector(size)
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/flink/blob/d2e2d79f/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/SparseMatrixSuite.scala
----------------------------------------------------------------------
diff --git a/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/SparseMatrixSuite.scala b/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/SparseMatrixSuite.scala
new file mode 100644
index 0000000..5710931
--- /dev/null
+++ b/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/SparseMatrixSuite.scala
@@ -0,0 +1,121 @@
+/*
+ * 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.flink.ml.math
+
+import org.scalatest.{Matchers, FlatSpec}
+
+class SparseMatrixSuite extends FlatSpec with Matchers {
+
+ behavior of "Flink's SparseMatrix"
+
+ it should "be initialized from a coordinate list representation (COO)" in {
+ val data = List[(Int, Int, Double)]((0, 0, 0), (0, 1, 0), (3, 4, 43), (2, 1, 17),
+ (3, 3, 88), (4 , 2, 99), (1, 4, 91), (3, 4, -1))
+
+ val numRows = 5
+ val numCols = 5
+
+ val sparseMatrix = SparseMatrix.fromCOO(numRows, numCols, data)
+
+ val expectedSparseMatrix = SparseMatrix.fromCOO(5, 5, (3, 4, 42), (2, 1, 17), (3, 3, 88),
+ (4, 2, 99), (1, 4, 91))
+
+ val expectedDenseMatrix = DenseMatrix.zeros(5, 5)
+ expectedDenseMatrix(3, 4) = 42
+ expectedDenseMatrix(2, 1) = 17
+ expectedDenseMatrix(3, 3) = 88
+ expectedDenseMatrix(4, 2) = 99
+ expectedDenseMatrix(1, 4) = 91
+
+ sparseMatrix should equal(expectedSparseMatrix)
+ sparseMatrix should equal(expectedDenseMatrix)
+
+ sparseMatrix.toDenseMatrix.data.sameElements(expectedDenseMatrix.data) should be(true)
+
+ val dataMap = data.
+ map{ case (row, col, value) => (row, col) -> value }.
+ groupBy{_._1}.
+ mapValues{
+ entries =>
+ entries.map(_._2).reduce(_ + _)
+ }
+
+ for(row <- 0 until numRows; col <- 0 until numCols) {
+ sparseMatrix(row, col) should be(dataMap.getOrElse((row, col), 0))
+ }
+
+ // test access to defined field even though it was set to 0
+ sparseMatrix(0, 1) = 10
+
+ // test that a non-defined field is not accessible
+ intercept[IllegalArgumentException]{
+ sparseMatrix(1, 1) = 1
+ }
+ }
+
+ it should "fail when accessing zero elements or using invalid indices" in {
+ val data = List[(Int, Int, Double)]((0, 0, 0), (0, 1, 0), (3, 4, 43), (2, 1, 17),
+ (3, 3, 88), (4 , 2, 99), (1, 4, 91), (3, 4, -1))
+
+ val numRows = 5
+ val numCols = 5
+
+ val sparseMatrix = SparseMatrix.fromCOO(numRows, numCols, data)
+
+ intercept[IllegalArgumentException] {
+ sparseMatrix(-1, 4)
+ }
+
+ intercept[IllegalArgumentException] {
+ sparseMatrix(numRows, 0)
+ }
+
+ intercept[IllegalArgumentException] {
+ sparseMatrix(0, numCols)
+ }
+
+ intercept[IllegalArgumentException] {
+ sparseMatrix(3, -1)
+ }
+ }
+
+ it should "fail when elements of the COO list have invalid indices" in {
+ intercept[IllegalArgumentException]{
+ val sparseMatrix = SparseMatrix.fromCOO(5 ,5, (5, 0, 10), (0, 0, 0), (0, 1, 0), (3, 4, 43),
+ (2, 1, 17))
+ }
+
+ intercept[IllegalArgumentException]{
+ val sparseMatrix = SparseMatrix.fromCOO(5, 5, (0, 0, 0), (0, 1, 0), (3, 4, 43), (2, 1, 17),
+ (-1, 4, 20))
+ }
+ }
+
+ it should "be copyable" in {
+ val sparseMatrix = SparseMatrix.fromCOO(4, 4, (0, 1, 2), (2, 3, 1), (2, 0, 42), (1, 3, 3))
+
+ val copy = sparseMatrix.copy
+
+ sparseMatrix should equal(copy)
+
+ copy(2, 3) = 2
+
+ sparseMatrix should not equal(copy)
+ }
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/d2e2d79f/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/SparseMatrixTest.scala
----------------------------------------------------------------------
diff --git a/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/SparseMatrixTest.scala b/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/SparseMatrixTest.scala
deleted file mode 100644
index 7fcdf54..0000000
--- a/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/SparseMatrixTest.scala
+++ /dev/null
@@ -1,124 +0,0 @@
-/*
- * 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.flink.ml.math
-
-import org.junit.Test
-import org.scalatest.ShouldMatchers
-
-class SparseMatrixTest extends ShouldMatchers {
-
- @Test
- def testSparseMatrixFromCOO: Unit = {
- val data = List[(Int, Int, Double)]((0, 0, 0), (0, 1, 0), (3, 4, 43), (2, 1, 17),
- (3, 3, 88), (4 , 2, 99), (1, 4, 91), (3, 4, -1))
-
- val numRows = 5
- val numCols = 5
-
- val sparseMatrix = SparseMatrix.fromCOO(numRows, numCols, data)
-
- val expectedSparseMatrix = SparseMatrix.fromCOO(5, 5, (3, 4, 42), (2, 1, 17), (3, 3, 88),
- (4, 2, 99), (1, 4, 91))
-
- val expectedDenseMatrix = DenseMatrix.zeros(5, 5)
- expectedDenseMatrix(3, 4) = 42
- expectedDenseMatrix(2, 1) = 17
- expectedDenseMatrix(3, 3) = 88
- expectedDenseMatrix(4, 2) = 99
- expectedDenseMatrix(1, 4) = 91
-
- sparseMatrix should equal(expectedSparseMatrix)
- sparseMatrix should equal(expectedDenseMatrix)
-
- sparseMatrix.toDenseMatrix.data.sameElements(expectedDenseMatrix.data) should be(true)
-
- val dataMap = data.
- map{ case (row, col, value) => (row, col) -> value }.
- groupBy{_._1}.
- mapValues{
- entries =>
- entries.map(_._2).reduce(_ + _)
- }
-
- for(row <- 0 until numRows; col <- 0 until numCols) {
- sparseMatrix(row, col) should be(dataMap.getOrElse((row, col), 0))
- }
-
- // test access to defined field even though it was set to 0
- sparseMatrix(0, 1) = 10
-
- // test that a non-defined field is not accessible
- intercept[IllegalArgumentException]{
- sparseMatrix(1, 1) = 1
- }
- }
-
- @Test
- def testInvalidIndexAccess: Unit = {
- val data = List[(Int, Int, Double)]((0, 0, 0), (0, 1, 0), (3, 4, 43), (2, 1, 17),
- (3, 3, 88), (4 , 2, 99), (1, 4, 91), (3, 4, -1))
-
- val numRows = 5
- val numCols = 5
-
- val sparseMatrix = SparseMatrix.fromCOO(numRows, numCols, data)
-
- intercept[IllegalArgumentException] {
- sparseMatrix(-1, 4)
- }
-
- intercept[IllegalArgumentException] {
- sparseMatrix(numRows, 0)
- }
-
- intercept[IllegalArgumentException] {
- sparseMatrix(0, numCols)
- }
-
- intercept[IllegalArgumentException] {
- sparseMatrix(3, -1)
- }
- }
-
- @Test
- def testSparseMatrixFromCOOWithInvalidIndices: Unit = {
- intercept[IllegalArgumentException]{
- val sparseMatrix = SparseMatrix.fromCOO(5 ,5, (5, 0, 10), (0, 0, 0), (0, 1, 0), (3, 4, 43),
- (2, 1, 17))
- }
-
- intercept[IllegalArgumentException]{
- val sparseMatrix = SparseMatrix.fromCOO(5, 5, (0, 0, 0), (0, 1, 0), (3, 4, 43), (2, 1, 17),
- (-1, 4, 20))
- }
- }
-
- @Test
- def testSparseMatrixCopy: Unit = {
- val sparseMatrix = SparseMatrix.fromCOO(4, 4, (0, 1, 2), (2, 3, 1), (2, 0, 42), (1, 3, 3))
-
- val copy = sparseMatrix.copy
-
- sparseMatrix should equal(copy)
-
- copy(2, 3) = 2
-
- sparseMatrix should not equal(copy)
- }
-}
http://git-wip-us.apache.org/repos/asf/flink/blob/d2e2d79f/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/SparseVectorSuite.scala
----------------------------------------------------------------------
diff --git a/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/SparseVectorSuite.scala b/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/SparseVectorSuite.scala
new file mode 100644
index 0000000..28415e8
--- /dev/null
+++ b/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/SparseVectorSuite.scala
@@ -0,0 +1,90 @@
+/*
+ * 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.flink.ml.math
+
+import org.scalatest.{Matchers, FlatSpec}
+
+class SparseVectorSuite extends FlatSpec with Matchers {
+
+ behavior of "Flink's SparseVector"
+
+ it should "contain the initialization data provided as coordinate list (COO)" in {
+ val data = List[(Int, Double)]((0, 1), (2, 0), (4, 42), (0, 3))
+ val size = 5
+ val sparseVector = SparseVector.fromCOO(size, data)
+
+ val expectedSparseVector = SparseVector.fromCOO(5, (0, 4), (4, 42))
+ val expectedDenseVector = DenseVector.zeros(5)
+
+ expectedDenseVector(0) = 4
+ expectedDenseVector(4) = 42
+
+ sparseVector should equal(expectedSparseVector)
+ sparseVector should equal(expectedDenseVector)
+
+ val denseVector = sparseVector.toDenseVector
+
+ denseVector should equal(expectedDenseVector)
+
+ val dataMap = data.
+ groupBy{_._1}.
+ mapValues{
+ entries =>
+ entries.map(_._2).reduce(_ + _)
+ }
+
+ for(index <- 0 until size) {
+ sparseVector(index) should be(dataMap.getOrElse(index, 0))
+ }
+ }
+
+ it should "fail when accessing elements using an invalid index" in {
+ val sparseVector = SparseVector.fromCOO(5, (1, 1), (3, 3), (4, 4))
+
+ intercept[IllegalArgumentException] {
+ sparseVector(-1)
+ }
+
+ intercept[IllegalArgumentException] {
+ sparseVector(5)
+ }
+ }
+
+ it should "fail when the COO list contains elements with invalid indices" in {
+ intercept[IllegalArgumentException] {
+ val sparseVector = SparseVector.fromCOO(5, (0, 1), (-1, 34), (3, 2))
+ }
+
+ intercept[IllegalArgumentException] {
+ val sparseVector = SparseVector.fromCOO(5, (0, 1), (4,3), (5, 1))
+ }
+ }
+
+ it should "be copyable" in {
+ val sparseVector = SparseVector.fromCOO(5, (0, 1), (4, 3), (3, 2))
+
+ val copy = sparseVector.copy
+
+ sparseVector should equal(copy)
+
+ copy(3) = 3
+
+ sparseVector should not equal(copy)
+ }
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/d2e2d79f/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/SparseVectorTest.scala
----------------------------------------------------------------------
diff --git a/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/SparseVectorTest.scala b/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/SparseVectorTest.scala
deleted file mode 100644
index 88d4878..0000000
--- a/flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/math/SparseVectorTest.scala
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * 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.flink.ml.math
-
-import org.junit.Test
-import org.scalatest.ShouldMatchers
-
-class SparseVectorTest extends ShouldMatchers{
-
- @Test
- def testDataAfterInitialization: Unit = {
- val data = List[(Int, Double)]((0, 1), (2, 0), (4, 42), (0, 3))
- val size = 5
- val sparseVector = SparseVector.fromCOO(size, data)
-
- val expectedSparseVector = SparseVector.fromCOO(5, (0, 4), (4, 42))
- val expectedDenseVector = DenseVector.zeros(5)
-
- expectedDenseVector(0) = 4
- expectedDenseVector(4) = 42
-
- sparseVector should equal(expectedSparseVector)
- sparseVector should equal(expectedDenseVector)
-
- val denseVector = sparseVector.toDenseVector
-
- denseVector should equal(expectedDenseVector)
-
- val dataMap = data.
- groupBy{_._1}.
- mapValues{
- entries =>
- entries.map(_._2).reduce(_ + _)
- }
-
- for(index <- 0 until size) {
- sparseVector(index) should be(dataMap.getOrElse(index, 0))
- }
- }
-
- @Test
- def testInvalidIndexAccess: Unit = {
- val sparseVector = SparseVector.fromCOO(5, (1, 1), (3, 3), (4, 4))
-
- intercept[IllegalArgumentException] {
- sparseVector(-1)
- }
-
- intercept[IllegalArgumentException] {
- sparseVector(5)
- }
- }
-
- @Test
- def testSparseVectorFromCOOWithInvalidIndices: Unit = {
- intercept[IllegalArgumentException] {
- val sparseVector = SparseVector.fromCOO(5, (0, 1), (-1, 34), (3, 2))
- }
-
- intercept[IllegalArgumentException] {
- val sparseVector = SparseVector.fromCOO(5, (0, 1), (4,3), (5, 1))
- }
- }
-
- @Test
- def testSparseVectorCopy: Unit = {
- val sparseVector = SparseVector.fromCOO(5, (0, 1), (4, 3), (3, 2))
-
- val copy = sparseVector.copy
-
- sparseVector should equal(copy)
-
- copy(3) = 3
-
- sparseVector should not equal(copy)
- }
-}