Author: jeastman
Date: Mon May 26 17:18:37 2008
New Revision: 660343

URL: http://svn.apache.org/viewvc?rev=660343&view=rev
Log:
MAHOUT-59: Refactored arguments of KMeansDriver.runJob() for consistency with 
the other clustering driver methods. Added a new KMeansJob class to hold code 
that deletes the output directory so that examples can be chained. Adjusted 
TestKmeansClustering to new argument types.

KMeans tests still run. 

Added:
    
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/KMeansJob.java
Modified:
    
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/KMeansDriver.java
    
lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/TestKmeansClustering.java

Modified: 
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/KMeansDriver.java
URL: 
http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/KMeansDriver.java?rev=660343&r1=660342&r2=660343&view=diff
==============================================================================
--- 
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/KMeansDriver.java
 (original)
+++ 
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/KMeansDriver.java
 Mon May 26 17:18:37 2008
@@ -42,8 +42,8 @@
     String clusters = args[1];
     String output = args[2];
     String measureClass = args[3];
-    String convergenceDelta = args[4];
-    String maxIterations = args[5];
+    double convergenceDelta = new Double(args[4]);
+    int maxIterations = new Integer(args[5]);
     runJob(input, clusters, output, measureClass, convergenceDelta, 
maxIterations);
   }
 
@@ -58,8 +58,7 @@
    * @param maxIterations    the maximum number of iterations
    */
   public static void runJob(String input, String clustersIn, String output,
-                            String measureClass, String convergenceDelta, 
String maxIterations) {
-    int maxIter = new Integer(maxIterations);
+                            String measureClass, double convergenceDelta, int 
maxIterations) {
     try {
       // delete the output directory
       JobConf conf = new JobConf(KMeansDriver.class);
@@ -72,13 +71,14 @@
       // iterate until the clusters converge
       boolean converged = false;
       int iteration = 0;
+      String delta = Double.toString(convergenceDelta);
 
-      while (!converged && iteration < maxIter) {
+      while (!converged && iteration < maxIterations) {
         log.info("Iteration {}", iteration);
         // point the output to a new directory per iteration
         String clustersOut = output + "/clusters-" + iteration;
         converged = runIteration(input, clustersIn, clustersOut, measureClass,
-                convergenceDelta);
+                delta);
         // now point the input to the old output directory
         clustersIn = output + "/clusters-" + iteration;
         iteration++;
@@ -86,7 +86,7 @@
       // now actually cluster the points
       log.info("Clustering ");
       runClustering(input, clustersIn, output + "/points", measureClass,
-              convergenceDelta);
+              delta);
     } catch (Exception e) {
       throw new RuntimeException(e);
     }

Added: 
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/KMeansJob.java
URL: 
http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/KMeansJob.java?rev=660343&view=auto
==============================================================================
--- 
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/KMeansJob.java
 (added)
+++ 
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/KMeansJob.java
 Mon May 26 17:18:37 2008
@@ -0,0 +1,67 @@
+/* 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.mahout.clustering.kmeans;
+
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.mapred.JobConf;
+
+public class KMeansJob {
+
+  private KMeansJob() {
+  }
+
+  public static void main(String[] args) {
+    String input = args[0];
+    String clusters = args[1];
+    String output = args[2];
+    String measureClass = args[3];
+    double convergenceDelta = new Double(args[4]);
+    int maxIterations = new Integer(args[5]);
+    runJob(input, clusters, output, measureClass, convergenceDelta,
+        maxIterations);
+  }
+
+  /**
+   * Run the job using supplied arguments, deleting the output directory if it
+   * exists beforehand
+   * 
+   * @param input the directory pathname for input points
+   * @param clustersIn the directory pathname for initial & computed clusters
+   * @param output the directory pathname for output points
+   * @param measureClass the classname of the DistanceMeasure
+   * @param convergenceDelta the convergence delta value
+   * @param maxIterations the maximum number of iterations
+   */
+  public static void runJob(String input, String clustersIn, String output,
+      String measureClass, double convergenceDelta, int maxIterations) {
+    try {
+      // delete the output directory
+      JobConf conf = new JobConf(KMeansJob.class);
+      Path outPath = new Path(output);
+      FileSystem fs = FileSystem.get(conf);
+      if (fs.exists(outPath)) {
+        fs.delete(outPath);
+      }
+      fs.mkdirs(outPath);
+      KMeansDriver.runJob(input, clustersIn, output, measureClass,
+          convergenceDelta, maxIterations);
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+  }
+}

Modified: 
lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/TestKmeansClustering.java
URL: 
http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/TestKmeansClustering.java?rev=660343&r1=660342&r2=660343&view=diff
==============================================================================
--- 
lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/TestKmeansClustering.java
 (original)
+++ 
lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/TestKmeansClustering.java
 Mon May 26 17:18:37 2008
@@ -397,8 +397,8 @@
       writer.close();
 
       // now run the Job
-      KMeansDriver.runJob("testdata/points", "testdata/clusters", "output",
-          EuclideanDistanceMeasure.class.getName(), "0.001", "10");
+      KMeansJob.runJob("testdata/points", "testdata/clusters", "output",
+          EuclideanDistanceMeasure.class.getName(), 0.001, 10);
 
       // now compare the expected clusters with actual
       File outDir = new File("output/points");
@@ -449,8 +449,8 @@
         ManhattanDistanceMeasure.class.getName(), 3.1, 2.1);
 
     // now run the KMeans job
-    KMeansDriver.runJob("testdata/points", "testdata/canopies", "output",
-        EuclideanDistanceMeasure.class.getName(), "0.001", "10");
+    KMeansJob.runJob("testdata/points", "testdata/canopies", "output",
+        EuclideanDistanceMeasure.class.getName(), 0.001, 10);
 
     // now compare the expected clusters with actual
     File outDir = new File("output/points");


Reply via email to