SemyonSinchenko commented on code in PR #58302:
URL: https://github.com/apache/spark/pull/58302#discussion_r4288878063


##########
graphframes/src/main/scala/org/apache/spark/graphframes/lib/LabelPropagation.scala:
##########
@@ -0,0 +1,147 @@
+/*
+ * 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.graphframes.lib
+
+import org.apache.spark.graphframes.GraphFrame
+import org.apache.spark.graphframes.Logging
+import org.apache.spark.graphframes.WithAlgorithmChoice
+import org.apache.spark.graphframes.WithCheckpointInterval
+import org.apache.spark.graphframes.WithIntermediateStorageLevel
+import org.apache.spark.graphframes.WithLocalCheckpoints
+import org.apache.spark.graphframes.WithMaxIter
+import org.apache.spark.graphx
+import org.apache.spark.sql.Column
+import org.apache.spark.sql.DataFrame
+import org.apache.spark.sql.functions._
+import org.apache.spark.sql.types.IntegerType
+import org.apache.spark.sql.types.MapType
+import org.apache.spark.storage.StorageLevel
+
+/**
+ * Run static Label Propagation for detecting communities in networks.
+ *
+ * Each node in the network is initially assigned to its own community. At 
every iteration, nodes
+ * send their community affiliation to all neighbors and update their state to 
the mode community
+ * affiliation of incoming messages.
+ *
+ * LPA is a standard community detection algorithm for graphs. It is very 
inexpensive
+ * computationally, although (1) convergence is not guaranteed and (2) one can 
end up with trivial
+ * solutions (all nodes are identified into a single community).
+ *
+ * The resulting DataFrame contains all the original vertex information and 
one additional column:
+ *   - label (`LongType`): label of community affiliation
+ */
+class LabelPropagation private[graphframes] (private val graph: GraphFrame)
+    extends Arguments
+    with WithAlgorithmChoice
+    with WithCheckpointInterval
+    with WithMaxIter
+    with WithLocalCheckpoints
+    with WithIntermediateStorageLevel
+    with Logging {
+
+  def run(): DataFrame = {
+    val maxIterChecked = check(maxIter, "maxIter")
+    val res = algorithm match {
+      case "graphx" => LabelPropagation.runInGraphX(graph, maxIterChecked)
+      case "graphframes" =>
+        LabelPropagation.runInGraphFrames(
+          graph,
+          maxIterChecked,
+          checkpointInterval,
+          useLocalCheckpoints = useLocalCheckpoints,
+          intermediateStorageLevel = intermediateStorageLevel)
+    }
+    resultIsPersistent()
+    res
+  }
+}
+
+private object LabelPropagation {
+  private def runInGraphX(graph: GraphFrame, maxIter: Int): DataFrame = {
+    val gx = graphx.lib.LabelPropagation.run(graph.cachedTopologyGraphX, 
maxIter)
+    val res = GraphXConversions.fromGraphX(graph, gx, vertexNames = 
Seq(LABEL_ID)).vertices
+    res.persist(StorageLevel.MEMORY_AND_DISK_SER)
+    res.count()
+    gx.unpersist()
+    res
+  }
+
+  private def keyWithMaxValue(column: Column): Column = {

Review Comment:
   The only reason we did not use the built-in `mode` was a requirement to 
support the 3.5.x where there was no `deterministic` arg (it was added in 
4.0.x); my suggestion would be to replace this (and code at lines 134-141) by 
just `mode` + `deterministic`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to