Github user javadba commented on a diff in the pull request:
https://github.com/apache/spark/pull/4254#discussion_r23808251
--- Diff:
mllib/src/test/scala/org/apache/spark/mllib/clustering/PowerIterationClusteringSuite.scala
---
@@ -0,0 +1,317 @@
+/*
+ * 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.clustering
+
+import breeze.linalg.{DenseMatrix => BDM, DenseVector => BDV}
+import org.apache.log4j.Logger
+import org.apache.spark.SparkContext
+import org.apache.spark.graphx.{EdgeRDD, Edge, Graph}
+import
org.apache.spark.mllib.clustering.PowerIterationClustering.{LabeledPoint,
Points, IndexedVector}
+import org.apache.spark.mllib.util.MLlibTestSparkContext
+import org.apache.spark.rdd.RDD
+import org.scalatest.FunSuite
+
+import scala.util.Random
+
+class PowerIterationClusteringSuite extends FunSuite with
MLlibTestSparkContext {
+
+ val logger = Logger.getLogger(getClass.getName)
+
+ import org.apache.spark.mllib.clustering.PowerIterationClusteringSuite._
+
+ test("concentricCirclesTest") {
+ concentricCirclesTest()
+ }
+
+ def concentricCirclesTest() = {
+ val sigma = 1.0
+ val nIterations = 10
+
+ val circleSpecs = Seq(
+ // Best results for 30 points
+ CircleSpec(Point(0.0, 0.0), 0.03, 0.1, 3),
+ CircleSpec(Point(0.0, 0.0), 0.3, 0.03, 12),
+ CircleSpec(Point(0.0, 0.0), 1.0, 0.01, 15)
+ // Add following to get 100 points
+ , CircleSpec(Point(0.0, 0.0), 1.5, 0.005, 30),
+ CircleSpec(Point(0.0, 0.0), 2.0, 0.002, 40)
+ )
+
+ val nClusters = circleSpecs.size
+ val cdata = createConcentricCirclesData(circleSpecs)
+ val vertices = new Random().shuffle(cdata.map { p =>
+ (p.label, new BDV(Array(p.x, p.y)))
+ })
+
+ val nVertices = vertices.length
+ val G = createGaussianAffinityMatrix(sc, vertices)
+ val (ccenters, estCollected) = PIC.run(sc, G, nClusters, nIterations)
+ logger.info(s"Cluster centers: ${ccenters.mkString(",")} " +
+ s"\nEstimates: ${estCollected.mkString("[", ",", "]")}")
+ assert(ccenters.size == circleSpecs.length, "Did not get correct
number of centers")
+
+ }
+
+}
+
+object PowerIterationClusteringSuite {
+ val logger = Logger.getLogger(getClass.getName)
+ val A = Array
+ val PIC = PowerIterationClustering
+
+ // Default sigma for Gaussian Distance calculations
+ val defaultSigma = 1.0
+
+ // Default minimum affinity between points - lower than this it is
considered
+ // zero and no edge will be created
+ val defaultMinAffinity = 1e-11
+
+ def pdoub(d: Double) = f"$d%1.6f"
+
+ case class Point(label: Long, x: Double, y: Double) {
+ def this(x: Double, y: Double) = this(-1L, x, y)
+
+ override def toString() = s"($label, (${pdoub(x)},${pdoub(y)}))"
+ }
+
+ object Point {
+ def apply(x: Double, y: Double) = new Point(-1L, x, y)
+ }
+
+ case class CircleSpec(center: Point, radius: Double, noiseToRadiusRatio:
Double,
+ nPoints: Int, uniformDistOnCircle: Boolean = true)
+
+ def createConcentricCirclesData(circleSpecs: Seq[CircleSpec]) = {
+ import org.apache.spark.mllib.random.StandardNormalGenerator
+ val normalGen = new StandardNormalGenerator
+ var idStart = 0
+ val circles = for (csp <- circleSpecs) yield {
+ idStart += 1000
+ val circlePoints = for (thetax <- 0 until csp.nPoints) yield {
+ val theta = thetax * 2 * Math.PI / csp.nPoints
+ val (x, y) = (csp.radius * Math.cos(theta)
+ * (1 + normalGen.nextValue * csp.noiseToRadiusRatio),
+ csp.radius * Math.sin(theta) * (1 + normalGen.nextValue *
csp.noiseToRadiusRatio))
+ (Point(idStart + thetax, x, y))
+ }
+ circlePoints
+ }
+ val points = circles.flatten.sortBy(_.label)
+ logger.info(printPoints(points))
+ points
+ }
+
+ def printPoints(points: Seq[Point]) = {
+ points.mkString("[", " , ", "]")
+ }
+
+ private[mllib] def printVector(dvect: BDV[Double]) = {
+ dvect.toArray.mkString(",")
+ }
+
+
+ /**
+ *
+ * Create an affinity matrix
+ *
+ * @param sc Spark Context
+ * @param points Input Points in format of [(VertexId,(x,y)]
+ * where VertexId is a Long
+ * @param sigma Sigma for Gaussian distribution calculation according
to
+ * [1/2 *sqrt(pi*sigma)] exp (- (x-y)**2 / 2sigma**2
+ * @param minAffinity Minimum Affinity between two Points in the input
dataset: below
+ * this threshold the affinity will be considered
"close to" zero and
+ * no Edge will be created between those Points in
the sparse matrix
+ * @return Tuple of (Seq[(Cluster Id,Cluster Center)],
+ * Seq[(VertexId, ClusterID Membership)]
+ */
+ def createGaussianAffinityMatrix(sc: SparkContext,
--- End diff --
Actually let's do the replacement of this in a follow-up PR, as mentioned
in one of your later posts. Making this change kept me up late (and still in
progress). Your later comment said to focus on the main src items - and let us
proceed that way.
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]