You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by je...@apache.org on 2012/06/02 00:19:54 UTC
svn commit: r1345375 - in /mahout/trunk/integration/src:
main/java/org/apache/mahout/clustering/evaluation/
test/java/org/apache/mahout/clustering/
test/java/org/apache/mahout/clustering/cdbw/
Author: jeastman
Date: Fri Jun 1 22:19:54 2012
New Revision: 1345375
URL: http://svn.apache.org/viewvc?rev=1345375&view=rev
Log:
MAHOUT-1020: Refactored printRepresentativePoints utility to RepresentativePointsDriver as a static so it can be used more easily outside of the tests. All tests run.
Modified:
mahout/trunk/integration/src/main/java/org/apache/mahout/clustering/evaluation/RepresentativePointsDriver.java
mahout/trunk/integration/src/test/java/org/apache/mahout/clustering/TestClusterEvaluator.java
mahout/trunk/integration/src/test/java/org/apache/mahout/clustering/cdbw/TestCDbwEvaluator.java
Modified: mahout/trunk/integration/src/main/java/org/apache/mahout/clustering/evaluation/RepresentativePointsDriver.java
URL: http://svn.apache.org/viewvc/mahout/trunk/integration/src/main/java/org/apache/mahout/clustering/evaluation/RepresentativePointsDriver.java?rev=1345375&r1=1345374&r2=1345375&view=diff
==============================================================================
--- mahout/trunk/integration/src/main/java/org/apache/mahout/clustering/evaluation/RepresentativePointsDriver.java (original)
+++ mahout/trunk/integration/src/main/java/org/apache/mahout/clustering/evaluation/RepresentativePointsDriver.java Fri Jun 1 22:19:54 2012
@@ -54,20 +54,19 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class RepresentativePointsDriver extends AbstractJob {
-
+
public static final String STATE_IN_KEY = "org.apache.mahout.clustering.stateIn";
-
+
public static final String DISTANCE_MEASURE_KEY = "org.apache.mahout.clustering.measure";
-
+
private static final Logger log = LoggerFactory.getLogger(RepresentativePointsDriver.class);
-
- private RepresentativePointsDriver() {
- }
-
+
+ private RepresentativePointsDriver() {}
+
public static void main(String[] args) throws Exception {
ToolRunner.run(new Configuration(), new RepresentativePointsDriver(), args);
}
-
+
@Override
public int run(String[] args) throws ClassNotFoundException, IOException, InterruptedException {
addInputOption();
@@ -79,7 +78,7 @@ public final class RepresentativePointsD
if (parseArguments(args) == null) {
return -1;
}
-
+
Path input = getInputPath();
Path output = getOutputPath();
String distanceMeasureClass = getOption(DefaultOptionCreator.DISTANCE_MEASURE_OPTION);
@@ -91,18 +90,36 @@ public final class RepresentativePointsD
run(getConf(), input, clusteredPoints, output, measure, maxIterations, runSequential);
return 0;
}
-
- public static void run(Configuration conf,
- Path clustersIn,
- Path clusteredPointsIn,
- Path output,
- DistanceMeasure measure,
- int numIterations,
- boolean runSequential)
- throws IOException, InterruptedException, ClassNotFoundException {
+
+ /**
+ * Utility to print out representative points
+ *
+ * @param output
+ * the Path to the directory containing representativePoints-i folders
+ * @param numIterations
+ * the int number of iterations to print
+ * @throws IOException
+ * if errors occur
+ */
+ public static void printRepresentativePoints(Path output, int numIterations) throws IOException {
+ for (int i = 0; i <= numIterations; i++) {
+ Path out = new Path(output, "representativePoints-" + i);
+ System.out.println("Representative Points for iteration " + i);
+ Configuration conf = new Configuration();
+ for (Pair<IntWritable,VectorWritable> record : new SequenceFileDirIterable<IntWritable,VectorWritable>(out,
+ PathType.LIST, PathFilters.logsCRCFilter(), null, true, conf)) {
+ System.out.println("\tC-" + record.getFirst().get() + ": "
+ + AbstractCluster.formatVector(record.getSecond().get(), null));
+ }
+ }
+ }
+
+ public static void run(Configuration conf, Path clustersIn, Path clusteredPointsIn, Path output,
+ DistanceMeasure measure, int numIterations, boolean runSequential) throws IOException, InterruptedException,
+ ClassNotFoundException {
Path stateIn = new Path(output, "representativePoints-0");
writeInitialState(stateIn, clustersIn);
-
+
for (int iteration = 0; iteration < numIterations; iteration++) {
log.info("Representative Points Iteration {}", iteration);
// point the output to a new directory per iteration
@@ -111,11 +128,11 @@ public final class RepresentativePointsD
// now point the input to the old output directory
stateIn = stateOut;
}
-
+
conf.set(STATE_IN_KEY, stateIn.toString());
conf.set(DISTANCE_MEASURE_KEY, measure.getClass().getName());
}
-
+
private static void writeInitialState(Path output, Path clustersIn) throws IOException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(output.toUri(), conf);
@@ -127,7 +144,7 @@ public final class RepresentativePointsD
for (ClusterWritable clusterWritable : new SequenceFileValueIterable<ClusterWritable>(inPart, true, conf)) {
Cluster cluster = clusterWritable.getValue();
if (log.isDebugEnabled()) {
- log.debug("C-{}: {}", cluster.getId(), AbstractCluster.formatVector(cluster.getCenter(), null));
+ log.debug("C-{}: {}", cluster.getId(), AbstractCluster.formatVector(cluster.getCenter(), null));
}
writer.append(new IntWritable(cluster.getId()), new VectorWritable(cluster.getCenter()));
}
@@ -136,24 +153,20 @@ public final class RepresentativePointsD
}
}
}
-
- private static void runIteration(Configuration conf,
- Path clusteredPointsIn,
- Path stateIn,
- Path stateOut,
- DistanceMeasure measure,
- boolean runSequential)
- throws IOException, InterruptedException, ClassNotFoundException {
+
+ private static void runIteration(Configuration conf, Path clusteredPointsIn, Path stateIn, Path stateOut,
+ DistanceMeasure measure, boolean runSequential) throws IOException, InterruptedException, ClassNotFoundException {
if (runSequential) {
runIterationSeq(conf, clusteredPointsIn, stateIn, stateOut, measure);
} else {
runIterationMR(conf, clusteredPointsIn, stateIn, stateOut, measure);
}
}
-
+
/**
* Run the job using supplied arguments as a sequential process
- * @param conf
+ *
+ * @param conf
* the Configuration to use
* @param clusteredPointsIn
* the directory pathname for input points
@@ -164,29 +177,21 @@ public final class RepresentativePointsD
* @param measure
* the DistanceMeasure to use
*/
- private static void runIterationSeq(Configuration conf,
- Path clusteredPointsIn,
- Path stateIn,
- Path stateOut,
- DistanceMeasure measure) throws IOException {
-
- Map<Integer, List<VectorWritable>> repPoints = RepresentativePointsMapper.getRepresentativePoints(conf, stateIn);
- Map<Integer, WeightedVectorWritable> mostDistantPoints = Maps.newHashMap();
+ private static void runIterationSeq(Configuration conf, Path clusteredPointsIn, Path stateIn, Path stateOut,
+ DistanceMeasure measure) throws IOException {
+
+ Map<Integer,List<VectorWritable>> repPoints = RepresentativePointsMapper.getRepresentativePoints(conf, stateIn);
+ Map<Integer,WeightedVectorWritable> mostDistantPoints = Maps.newHashMap();
FileSystem fs = FileSystem.get(clusteredPointsIn.toUri(), conf);
- for (Pair<IntWritable,WeightedVectorWritable> record :
- new SequenceFileDirIterable<IntWritable,WeightedVectorWritable>(
- clusteredPointsIn, PathType.LIST, PathFilters.logsCRCFilter(), null, true, conf)) {
- RepresentativePointsMapper.mapPoint(
- record.getFirst(), record.getSecond(), measure, repPoints, mostDistantPoints);
+ for (Pair<IntWritable,WeightedVectorWritable> record : new SequenceFileDirIterable<IntWritable,WeightedVectorWritable>(
+ clusteredPointsIn, PathType.LIST, PathFilters.logsCRCFilter(), null, true, conf)) {
+ RepresentativePointsMapper.mapPoint(record.getFirst(), record.getSecond(), measure, repPoints, mostDistantPoints);
}
int part = 0;
- SequenceFile.Writer writer = new SequenceFile.Writer(fs,
- conf,
- new Path(stateOut, "part-m-" + part++),
- IntWritable.class,
- VectorWritable.class);
+ SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, new Path(stateOut, "part-m-" + part++),
+ IntWritable.class, VectorWritable.class);
try {
- for (Entry<Integer, List<VectorWritable>> entry : repPoints.entrySet()) {
+ for (Entry<Integer,List<VectorWritable>> entry : repPoints.entrySet()) {
for (VectorWritable vw : entry.getValue()) {
writer.append(new IntWritable(entry.getKey()), vw);
}
@@ -194,20 +199,21 @@ public final class RepresentativePointsD
} finally {
Closeables.closeQuietly(writer);
}
- writer = new SequenceFile.Writer(fs, conf,
- new Path(stateOut, "part-m-" + part++), IntWritable.class, VectorWritable.class);
+ writer = new SequenceFile.Writer(fs, conf, new Path(stateOut, "part-m-" + part++), IntWritable.class,
+ VectorWritable.class);
try {
- for (Map.Entry<Integer, WeightedVectorWritable> entry : mostDistantPoints.entrySet()) {
+ for (Map.Entry<Integer,WeightedVectorWritable> entry : mostDistantPoints.entrySet()) {
writer.append(new IntWritable(entry.getKey()), new VectorWritable(entry.getValue().getVector()));
}
} finally {
Closeables.closeQuietly(writer);
}
}
-
+
/**
* Run the job using supplied arguments as a Map/Reduce process
- * @param conf
+ *
+ * @param conf
* the Configuration to use
* @param input
* the directory pathname for input points
@@ -218,12 +224,8 @@ public final class RepresentativePointsD
* @param measure
* the DistanceMeasure to use
*/
- private static void runIterationMR(Configuration conf,
- Path input,
- Path stateIn,
- Path stateOut,
- DistanceMeasure measure)
- throws IOException, InterruptedException, ClassNotFoundException {
+ private static void runIterationMR(Configuration conf, Path input, Path stateIn, Path stateOut,
+ DistanceMeasure measure) throws IOException, InterruptedException, ClassNotFoundException {
conf.set(STATE_IN_KEY, stateIn.toString());
conf.set(DISTANCE_MEASURE_KEY, measure.getClass().getName());
Job job = new Job(conf, "Representative Points Driver running over input: " + input);
@@ -232,15 +234,15 @@ public final class RepresentativePointsD
job.setOutputValueClass(VectorWritable.class);
job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(WeightedVectorWritable.class);
-
+
FileInputFormat.setInputPaths(job, input);
FileOutputFormat.setOutputPath(job, stateOut);
-
+
job.setMapperClass(RepresentativePointsMapper.class);
job.setReducerClass(RepresentativePointsReducer.class);
job.setInputFormatClass(SequenceFileInputFormat.class);
job.setOutputFormatClass(SequenceFileOutputFormat.class);
-
+
boolean succeeded = job.waitForCompletion(true);
if (!succeeded) {
throw new IllegalStateException("Job failed!");
Modified: mahout/trunk/integration/src/test/java/org/apache/mahout/clustering/TestClusterEvaluator.java
URL: http://svn.apache.org/viewvc/mahout/trunk/integration/src/test/java/org/apache/mahout/clustering/TestClusterEvaluator.java?rev=1345375&r1=1345374&r2=1345375&view=diff
==============================================================================
--- mahout/trunk/integration/src/test/java/org/apache/mahout/clustering/TestClusterEvaluator.java (original)
+++ mahout/trunk/integration/src/test/java/org/apache/mahout/clustering/TestClusterEvaluator.java Fri Jun 1 22:19:54 2012
@@ -21,12 +21,9 @@ import java.io.IOException;
import java.util.List;
import java.util.Map;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
-import org.apache.hadoop.io.IntWritable;
import org.apache.mahout.clustering.canopy.Canopy;
import org.apache.mahout.clustering.canopy.CanopyDriver;
import org.apache.mahout.clustering.dirichlet.DirichletDriver;
@@ -41,12 +38,8 @@ import org.apache.mahout.clustering.kmea
import org.apache.mahout.clustering.meanshift.MeanShiftCanopyDriver;
import org.apache.mahout.common.HadoopUtil;
import org.apache.mahout.common.MahoutTestCase;
-import org.apache.mahout.common.Pair;
import org.apache.mahout.common.distance.DistanceMeasure;
import org.apache.mahout.common.distance.EuclideanDistanceMeasure;
-import org.apache.mahout.common.iterator.sequencefile.PathFilters;
-import org.apache.mahout.common.iterator.sequencefile.PathType;
-import org.apache.mahout.common.iterator.sequencefile.SequenceFileDirIterable;
import org.apache.mahout.common.kernel.IKernelProfile;
import org.apache.mahout.common.kernel.TriangularKernelProfile;
import org.apache.mahout.math.DenseVector;
@@ -56,6 +49,9 @@ import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
public final class TestClusterEvaluator extends MahoutTestCase {
private static final double[][] REFERENCE = { {1, 1}, {2, 1}, {1, 2}, {2, 2}, {3, 3}, {4, 4}, {5, 4}, {4, 5}, {5, 5}};
@@ -119,20 +115,7 @@ public final class TestClusterEvaluator
}
private void printRepPoints(int numIterations) throws IOException {
- printRepPoints(output, numIterations);
- }
-
- private void printRepPoints(Path output, int numIterations) throws IOException {
- for (int i = 0; i <= numIterations; i++) {
- Path out = new Path(output, "representativePoints-" + i);
- System.out.println("Representative Points for iteration " + i);
- Configuration conf = new Configuration();
- for (Pair<IntWritable,VectorWritable> record : new SequenceFileDirIterable<IntWritable,VectorWritable>(out,
- PathType.LIST, PathFilters.logsCRCFilter(), null, true, conf)) {
- System.out.println("\tC-" + record.getFirst().get() + ": "
- + AbstractCluster.formatVector(record.getSecond().get(), null));
- }
- }
+ RepresentativePointsDriver.printRepresentativePoints(output, numIterations);
}
/**
@@ -301,7 +284,7 @@ public final class TestClusterEvaluator
Path clustersIn = new Path(kmeansOutput, "clusters-2");
RepresentativePointsDriver.run(conf, clustersIn, new Path(kmeansOutput, "clusteredPoints"), kmeansOutput, measure,
numIterations, true);
- printRepPoints(kmeansOutput, numIterations);
+ RepresentativePointsDriver.printRepresentativePoints(kmeansOutput, numIterations);
ClusterEvaluator evaluator = new ClusterEvaluator(conf, clustersIn);
// now print out the Results
System.out.println("Intra-cluster density = " + evaluator.intraClusterDensity());
@@ -323,7 +306,7 @@ public final class TestClusterEvaluator
Path clustersIn = new Path(fuzzyKMeansOutput, "clusters-4");
RepresentativePointsDriver.run(conf, clustersIn, new Path(fuzzyKMeansOutput, "clusteredPoints"), fuzzyKMeansOutput,
measure, numIterations, true);
- printRepPoints(fuzzyKMeansOutput, numIterations);
+ RepresentativePointsDriver.printRepresentativePoints(fuzzyKMeansOutput, numIterations);
ClusterEvaluator evaluator = new ClusterEvaluator(conf, clustersIn);
// now print out the Results
System.out.println("Intra-cluster density = " + evaluator.intraClusterDensity());
Modified: mahout/trunk/integration/src/test/java/org/apache/mahout/clustering/cdbw/TestCDbwEvaluator.java
URL: http://svn.apache.org/viewvc/mahout/trunk/integration/src/test/java/org/apache/mahout/clustering/cdbw/TestCDbwEvaluator.java?rev=1345375&r1=1345374&r2=1345375&view=diff
==============================================================================
--- mahout/trunk/integration/src/test/java/org/apache/mahout/clustering/cdbw/TestCDbwEvaluator.java (original)
+++ mahout/trunk/integration/src/test/java/org/apache/mahout/clustering/cdbw/TestCDbwEvaluator.java Fri Jun 1 22:19:54 2012
@@ -22,13 +22,9 @@ import java.util.Collection;
import java.util.List;
import java.util.Map;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
-import org.apache.hadoop.io.IntWritable;
-import org.apache.mahout.clustering.AbstractCluster;
import org.apache.mahout.clustering.Cluster;
import org.apache.mahout.clustering.ClusteringTestUtils;
import org.apache.mahout.clustering.TestClusterEvaluator;
@@ -44,12 +40,8 @@ import org.apache.mahout.clustering.kmea
import org.apache.mahout.clustering.kmeans.TestKmeansClustering;
import org.apache.mahout.clustering.meanshift.MeanShiftCanopyDriver;
import org.apache.mahout.common.MahoutTestCase;
-import org.apache.mahout.common.Pair;
import org.apache.mahout.common.distance.DistanceMeasure;
import org.apache.mahout.common.distance.EuclideanDistanceMeasure;
-import org.apache.mahout.common.iterator.sequencefile.PathFilters;
-import org.apache.mahout.common.iterator.sequencefile.PathType;
-import org.apache.mahout.common.iterator.sequencefile.SequenceFileDirIterable;
import org.apache.mahout.common.kernel.IKernelProfile;
import org.apache.mahout.common.kernel.TriangularKernelProfile;
import org.apache.mahout.math.DenseVector;
@@ -60,6 +52,9 @@ import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
public final class TestCDbwEvaluator extends MahoutTestCase {
private static final double[][] REFERENCE = { {1, 1}, {2, 1}, {1, 2}, {2, 2}, {3, 3}, {4, 4}, {5, 4}, {4, 5}, {5, 5}};
@@ -151,19 +146,6 @@ public final class TestCDbwEvaluator ext
generateSamples(300, 0, 2, 0.1);
}
- private void printRepPoints(Path output, int numIterations) throws IOException {
- for (int i = 0; i <= numIterations; i++) {
- Path out = new Path(output, "representativePoints-" + i);
- System.out.println("Representative Points for iteration " + i);
- Configuration conf = new Configuration();
- for (Pair<IntWritable,VectorWritable> record : new SequenceFileDirIterable<IntWritable,VectorWritable>(out,
- PathType.LIST, PathFilters.logsCRCFilter(), null, true, conf)) {
- System.out.println("\tC-" + record.getFirst().get() + ": "
- + AbstractCluster.formatVector(record.getSecond().get(), null));
- }
- }
- }
-
@Test
public void testCDbw0() throws IOException {
ClusteringTestUtils.writePointsToFile(referenceData, getTestTempFilePath("testdata/file1"), fs, conf);
@@ -318,7 +300,7 @@ public final class TestCDbwEvaluator ext
RepresentativePointsDriver.run(conf, clustersIn, new Path(kmeansOutput, "clusteredPoints"), kmeansOutput, measure,
numIterations, true);
CDbwEvaluator evaluator = new CDbwEvaluator(conf, clustersIn);
- printRepPoints(kmeansOutput, numIterations);
+ RepresentativePointsDriver.printRepresentativePoints(kmeansOutput, numIterations);
// now print out the Results
System.out.println("K-Means CDbw = " + evaluator.getCDbw());
System.out.println("Intra-cluster density = " + evaluator.intraClusterDensity());
@@ -338,10 +320,10 @@ public final class TestCDbwEvaluator ext
true, true, 0, true);
int numIterations = 10;
Path clustersIn = new Path(fuzzyKMeansOutput, "clusters-4");
- RepresentativePointsDriver.run(conf, clustersIn, new Path(fuzzyKMeansOutput, "clusteredPoints"), fuzzyKMeansOutput, measure,
- numIterations, true);
+ RepresentativePointsDriver.run(conf, clustersIn, new Path(fuzzyKMeansOutput, "clusteredPoints"), fuzzyKMeansOutput,
+ measure, numIterations, true);
CDbwEvaluator evaluator = new CDbwEvaluator(conf, clustersIn);
- printRepPoints(fuzzyKMeansOutput, numIterations);
+ RepresentativePointsDriver.printRepresentativePoints(fuzzyKMeansOutput, numIterations);
// now print out the Results
System.out.println("Fuzzy K-Means CDbw = " + evaluator.getCDbw());
System.out.println("Intra-cluster density = " + evaluator.intraClusterDensity());
@@ -379,7 +361,7 @@ public final class TestCDbwEvaluator ext
RepresentativePointsDriver.run(conf, clustersIn, new Path(output, "clusteredPoints"), output,
new EuclideanDistanceMeasure(), numIterations, true);
CDbwEvaluator evaluator = new CDbwEvaluator(conf, clustersIn);
- printRepPoints(output, numIterations);
+ RepresentativePointsDriver.printRepresentativePoints(output, numIterations);
// now print out the Results
System.out.println("Dirichlet CDbw = " + evaluator.getCDbw());
System.out.println("Intra-cluster density = " + evaluator.intraClusterDensity());