Skip to content

Commit 9c67049

Browse files
hhbyyhmengxr
authored andcommitted
[Spark-6693][MLlib]add tostring with max lines and width for matrix
jira: https://issues.apache.org/jira/browse/SPARK-6693 It's kind of annoying when debugging and found you cannot print out the matrix as you want. original toString of Matrix only print like following, 0.17810102596909183 0.5616906241468385 ... (10 total) 0.9692861997823815 0.015558159784155756 ... 0.8513015122819192 0.031523763918528847 ... 0.5396875653953941 0.3267864552779176 ... The def toString(maxLines : Int, maxWidth : Int) is useful when debuging, logging and saving matrix to files. Author: Yuhao Yang <[email protected]> Closes #5344 from hhbyyh/addToString and squashes the following commits: 19a6836 [Yuhao Yang] remove extra line 6314b21 [Yuhao Yang] add exclude 736c324 [Yuhao Yang] add ut and exclude 420da39 [Yuhao Yang] Merge remote-tracking branch 'upstream/master' into addToString c22f352 [Yuhao Yang] style change 64a9e0f [Yuhao Yang] add specific to string to matrix
1 parent a0411ae commit 9c67049

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

mllib/src/main/scala/org/apache/spark/mllib/linalg/Matrices.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ sealed trait Matrix extends Serializable {
8787
/** A human readable representation of the matrix */
8888
override def toString: String = toBreeze.toString()
8989

90+
/** A human readable representation of the matrix with maximum lines and width */
91+
def toString(maxLines: Int, maxLineWidth: Int): String = toBreeze.toString(maxLines, maxLineWidth)
92+
9093
/** Map the values of this matrix using a function. Generates a new matrix. Performs the
9194
* function on only the backing array. For example, an operation such as addition or
9295
* subtraction will only be performed on the non-zero values in a `SparseMatrix`. */

mllib/src/test/scala/org/apache/spark/mllib/linalg/MatricesSuite.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,20 @@ class MatricesSuite extends FunSuite {
439439
assert(mUDT.typeName == "matrix")
440440
assert(mUDT.simpleString == "matrix")
441441
}
442+
443+
test("toString") {
444+
val empty = Matrices.ones(0, 0)
445+
empty.toString(0, 0)
446+
447+
val mat = Matrices.rand(5, 10, new Random())
448+
mat.toString(-1, -5)
449+
mat.toString(0, 0)
450+
mat.toString(Int.MinValue, Int.MinValue)
451+
mat.toString(Int.MaxValue, Int.MaxValue)
452+
var lines = mat.toString(6, 50).lines.toArray
453+
assert(lines.size == 5 && lines.forall(_.size <= 50))
454+
455+
lines = mat.toString(5, 100).lines.toArray
456+
assert(lines.size == 5 && lines.forall(_.size <= 100))
457+
}
442458
}

project/MimaExcludes.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ object MimaExcludes {
6464
// SPARK-6492 Fix deadlock in SparkContext.stop()
6565
ProblemFilters.exclude[MissingMethodProblem]("org.apache.spark.SparkContext.org$" +
6666
"apache$spark$SparkContext$$SPARK_CONTEXT_CONSTRUCTOR_LOCK")
67+
)++ Seq(
68+
// SPARK-6693 add tostring with max lines and width for matrix
69+
ProblemFilters.exclude[MissingMethodProblem](
70+
"org.apache.spark.mllib.linalg.Matrix.toString")
6771
)
6872

6973
case v if v.startsWith("1.3") =>

0 commit comments

Comments
 (0)