Skip to content

Commit 64e6d2d

Browse files
committed
TST: Add better test errors and messages
1 parent 12dae73 commit 64e6d2d

File tree

4 files changed

+8
-14
lines changed

4 files changed

+8
-14
lines changed

mllib/src/main/scala/org/apache/spark/mllib/linalg/distributed/IndexedRowMatrix.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ class IndexedRowMatrix(
103103
computeU: Boolean = false,
104104
rCond: Double = 1e-9): SingularValueDecomposition[IndexedRowMatrix, Matrix] = {
105105

106-
require(k >= 1, "k should be at least one.")
106+
val n = numCols().toInt
107+
require(k > 0 && k <= n, s"Requested k singular values but got k=$k and numCols=$n.")
107108
val indices = rows.map(_.index)
108109
val svd = toRowMatrix().computeSVD(k, computeU, rCond)
109110
val U = if (computeU) {

mllib/src/main/scala/org/apache/spark/mllib/linalg/distributed/RowMatrix.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class RowMatrix(
212212
tol: Double,
213213
mode: String): SingularValueDecomposition[RowMatrix, Matrix] = {
214214
val n = numCols().toInt
215-
require(k > 0 && k <= n, s"Request up to n singular values but got k=$k and n=$n.")
215+
require(k > 0 && k <= n, s"Requested k singular values but got k=$k and numCols=$n.")
216216

217217
object SVDMode extends Enumeration {
218218
val LocalARPACK, LocalLAPACK, DistARPACK = Value

mllib/src/test/scala/org/apache/spark/mllib/linalg/distributed/IndexedRowMatrixSuite.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,11 @@ class IndexedRowMatrixSuite extends FunSuite with MLlibTestSparkContext {
115115

116116
test("validate k in svd") {
117117
val A = new IndexedRowMatrix(indexedRows)
118-
try {
119-
A.computeSVD(-1)
120-
} catch {
121-
case ie: IllegalArgumentException =>
122-
}
118+
intercept[IllegalArgumentException] {
119+
A.computeSVD(-1)
120+
}
123121
}
124122

125-
126123
def closeToZero(G: BDM[Double]): Boolean = {
127124
G.valuesIterator.map(math.abs).sum < 1e-6
128125
}

mllib/src/test/scala/org/apache/spark/mllib/linalg/distributed/RowMatrixSuite.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,8 @@ class RowMatrixSuite extends FunSuite with MLlibTestSparkContext {
173173

174174
test("validate k in svd") {
175175
for (mat <- Seq(denseMat, sparseMat)) {
176-
for (mode <- Seq("auto", "local-svd", "local-eigs", "dist-eigs")) {
177-
try {
178-
mat.computeSVD(-1, computeU = true, 1e-6, 300, 1e-10, mode)
179-
} catch {
180-
case ie: IllegalArgumentException =>
181-
}
176+
intercept[IllegalArgumentException] {
177+
mat.computeSVD(-1)
182178
}
183179
}
184180
}

0 commit comments

Comments
 (0)