You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by sr...@apache.org on 2009/07/10 11:35:28 UTC
svn commit: r792856 [12/13] - in /lucene/mahout/trunk/core/src:
main/java/org/apache/mahout/cf/taste/common/
main/java/org/apache/mahout/cf/taste/eval/
main/java/org/apache/mahout/cf/taste/hadoop/
main/java/org/apache/mahout/cf/taste/impl/common/ main/...
Modified: lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/canopy/VisibleCanopy.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/canopy/VisibleCanopy.java?rev=792856&r1=792855&r2=792856&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/canopy/VisibleCanopy.java (original)
+++ lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/canopy/VisibleCanopy.java Fri Jul 10 09:35:19 2009
@@ -23,9 +23,8 @@
import java.util.List;
/**
- * This Canopy subclass maintains a list of points in the canopy so it can
- * include them in its toString method. Useful for debugging but not practical
- * for production use since it holds onto all its points.
+ * This Canopy subclass maintains a list of points in the canopy so it can include them in its toString method. Useful
+ * for debugging but not practical for production use since it holds onto all its points.
*/
public class VisibleCanopy extends Canopy {
@@ -47,17 +46,15 @@
points.add(point);
}
- /**
- * Return a printable representation of this object, using the user supplied
- * identifier
- *
- * @return
- */
+ /** Return a printable representation of this object, using the user supplied identifier */
@Override
public String toString() {
String out = super.toString() + ": ";
- for (Vector pt : points)
- out = pt.asFormatString();
+ for (Vector pt : points) {
+ {
+ out = pt.asFormatString();
+ }
+ }
return out;
}
Modified: lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/JsonModelHolderAdapter.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/JsonModelHolderAdapter.java?rev=792856&r1=792855&r2=792856&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/JsonModelHolderAdapter.java (original)
+++ lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/JsonModelHolderAdapter.java Fri Jul 10 09:35:19 2009
@@ -16,11 +16,6 @@
*/
package org.apache.mahout.clustering.dirichlet;
-import java.lang.reflect.Type;
-
-import org.apache.mahout.clustering.dirichlet.models.Model;
-import org.apache.mahout.matrix.Vector;
-
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
@@ -32,17 +27,21 @@
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;
+import org.apache.mahout.clustering.dirichlet.models.Model;
+import org.apache.mahout.matrix.Vector;
+
+import java.lang.reflect.Type;
@SuppressWarnings("unchecked")
-public class JsonModelHolderAdapter implements JsonSerializer<ModelHolder>,
- JsonDeserializer<ModelHolder> {
+public class JsonModelHolderAdapter implements JsonSerializer<ModelHolder<?>>,
+ JsonDeserializer<ModelHolder<?>> {
final Type typeOfModel = new TypeToken<Model<Vector>>() {
}.getType();
@Override
- public JsonElement serialize(ModelHolder src, Type typeOfSrc,
- JsonSerializationContext context) {
+ public JsonElement serialize(ModelHolder<?> src, Type typeOfSrc,
+ JsonSerializationContext context) {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Model.class, new JsonModelAdapter());
Gson gson = builder.create();
@@ -52,14 +51,14 @@
}
@Override
- public ModelHolder deserialize(JsonElement json, Type typeOfT,
- JsonDeserializationContext context) throws JsonParseException {
+ public ModelHolder<?> deserialize(JsonElement json, Type typeOfT,
+ JsonDeserializationContext context) throws JsonParseException {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Model.class, new JsonModelAdapter());
Gson gson = builder.create();
JsonObject obj = json.getAsJsonObject();
String value = obj.get("model").getAsString();
- Model m = (Model) gson.fromJson(value, typeOfModel);
+ Model<?> m = (Model<?>) gson.fromJson(value, typeOfModel);
return new ModelHolder(m);
}
Modified: lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/TestDirichletClustering.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/TestDirichletClustering.java?rev=792856&r1=792855&r2=792856&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/TestDirichletClustering.java (original)
+++ lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/TestDirichletClustering.java Fri Jul 10 09:35:19 2009
@@ -17,11 +17,7 @@
* limitations under the License.
*/
-import java.util.ArrayList;
-import java.util.List;
-
import junit.framework.TestCase;
-
import org.apache.mahout.clustering.dirichlet.models.AsymmetricSampledNormalDistribution;
import org.apache.mahout.clustering.dirichlet.models.Model;
import org.apache.mahout.clustering.dirichlet.models.NormalModelDistribution;
@@ -29,6 +25,9 @@
import org.apache.mahout.matrix.DenseVector;
import org.apache.mahout.matrix.Vector;
+import java.util.ArrayList;
+import java.util.List;
+
public class TestDirichletClustering extends TestCase {
private List<Vector> sampleData;
@@ -42,27 +41,30 @@
/**
* Generate random samples and add them to the sampleData
+ *
* @param num int number of samples to generate
- * @param mx double x-value of the sample mean
- * @param my double y-value of the sample mean
- * @param sd double standard deviation of the samples
+ * @param mx double x-value of the sample mean
+ * @param my double y-value of the sample mean
+ * @param sd double standard deviation of the samples
*/
private void generateSamples(int num, double mx, double my, double sd) {
System.out.println("Generating " + num + " samples m=[" + mx + ", " + my
+ "] sd=" + sd);
- for (int i = 0; i < num; i++)
- sampleData.add(new DenseVector(new double[] {
+ for (int i = 0; i < num; i++) {
+ sampleData.add(new DenseVector(new double[]{
UncommonDistributions.rNorm(mx, sd),
- UncommonDistributions.rNorm(my, sd) }));
+ UncommonDistributions.rNorm(my, sd)}));
+ }
}
- private void printResults(List<Model<Vector>[]> result, int significant) {
+ private static void printResults(List<Model<Vector>[]> result, int significant) {
int row = 0;
for (Model<Vector>[] r : result) {
System.out.print("sample[" + row++ + "]= ");
for (Model<Vector> model : r) {
- if (model.count() > significant)
+ if (model.count() > significant) {
System.out.print(model.toString() + ", ");
+ }
}
System.out.println();
}
Modified: lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/TestDistributions.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/TestDistributions.java?rev=792856&r1=792855&r2=792856&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/TestDistributions.java (original)
+++ lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/TestDistributions.java Fri Jul 10 09:35:19 2009
@@ -18,7 +18,6 @@
*/
import junit.framework.TestCase;
-
import org.apache.mahout.matrix.DenseVector;
import org.apache.mahout.matrix.Vector;
@@ -31,21 +30,24 @@
}
public void testRbeta() {
- for (double i = 0.01; i < 20; i += 0.25)
+ for (double i = 0.01; i < 20; i += 0.25) {
System.out.println("rBeta(6,1," + i + ")="
+ UncommonDistributions.rBeta(6, 1, i).asFormatString());
+ }
}
public void testRchisq() {
- for (int i = 0; i < 50; i++)
+ for (int i = 0; i < 50; i++) {
System.out
.println("rChisq(" + i + ")=" + UncommonDistributions.rChisq(i));
+ }
}
public void testRnorm() {
- for (int i = 1; i < 50; i++)
+ for (int i = 1; i < 50; i++) {
System.out.println("rNorm(6,1," + i + ")="
+ UncommonDistributions.rNorm(1, i));
+ }
}
public void testDnorm() {
@@ -53,8 +55,9 @@
double d = (i * 0.1);
double dnorm = UncommonDistributions.dNorm(d, 0, 1);
byte[] bar = new byte[(int) (dnorm * 100)];
- for (int j = 0; j < bar.length; j++)
+ for (int j = 0; j < bar.length; j++) {
bar[j] = '*';
+ }
String baz = new String(bar);
System.out.println(baz);
}
@@ -65,8 +68,9 @@
double d = (i * 0.1);
double dnorm = UncommonDistributions.dNorm(d, 0, 2);
byte[] bar = new byte[(int) (dnorm * 100)];
- for (int j = 0; j < bar.length; j++)
+ for (int j = 0; j < bar.length; j++) {
bar[j] = '*';
+ }
String baz = new String(bar);
System.out.println(baz);
}
@@ -77,15 +81,16 @@
double d = (i * 0.1);
double dnorm = UncommonDistributions.dNorm(d, 0, 0.2);
byte[] bar = new byte[(int) (dnorm * 100)];
- for (int j = 0; j < bar.length; j++)
+ for (int j = 0; j < bar.length; j++) {
bar[j] = '*';
+ }
String baz = new String(bar);
System.out.println(baz);
}
}
public void testRmultinom1() {
- double[] b = { 0.4, 0.6 };
+ double[] b = {0.4, 0.6};
Vector v = new DenseVector(b);
Vector t = v.like();
for (int i = 1; i <= 100; i++) {
@@ -98,7 +103,7 @@
}
public void testRmultinom2() {
- double[] b = { 0.1, 0.2, 0.7 };
+ double[] b = {0.1, 0.2, 0.7};
Vector v = new DenseVector(b);
Vector t = v.like();
for (int i = 1; i <= 100; i++) {
@@ -111,10 +116,11 @@
}
public void testRmultinom() {
- double[] b = { 0.1, 0.2, 0.8 };
+ double[] b = {0.1, 0.2, 0.8};
Vector v = new DenseVector(b);
- for (int i = 1; i <= 100; i++)
+ for (int i = 1; i <= 100; i++) {
System.out.println("rMultinom(" + 100 + ", [0.1, 0.2, 0.8])="
+ UncommonDistributions.rMultinom(100, v).asFormatString());
+ }
}
}
Modified: lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/TestMapReduce.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/TestMapReduce.java?rev=792856&r1=792855&r2=792856&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/TestMapReduce.java (original)
+++ lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/dirichlet/TestMapReduce.java Fri Jul 10 09:35:19 2009
@@ -16,14 +16,9 @@
*/
package org.apache.mahout.clustering.dirichlet;
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
import junit.framework.TestCase;
-
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.io.Text;
@@ -43,8 +38,11 @@
import org.apache.mahout.matrix.Vector;
import org.apache.mahout.utils.DummyOutputCollector;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
public class TestMapReduce extends TestCase {
@@ -56,45 +54,46 @@
/**
* Generate random samples and add them to the sampleData
- *
+ *
* @param num int number of samples to generate
- * @param mx double x-value of the sample mean
- * @param my double y-value of the sample mean
+ * @param mx double x-value of the sample mean
+ * @param my double y-value of the sample mean
* @param sdx double x-standard deviation of the samples
* @param sdy double y-standard deviation of the samples
*/
private void generateSamples(int num, double mx, double my, double sdx,
- double sdy) {
+ double sdy) {
System.out.println("Generating " + num + " samples m=[" + mx + ", " + my
- + "] sd=[" + sdx + ", " + sdy + "]");
+ + "] sd=[" + sdx + ", " + sdy + ']');
for (int i = 0; i < num; i++) {
- addSample(new double[] {
+ addSample(new double[]{
UncommonDistributions.rNorm(mx, sdx),
- UncommonDistributions.rNorm(my, sdy) });
+ UncommonDistributions.rNorm(my, sdy)});
}
}
private void addSample(double[] values) {
Vector v = new SparseVector(2);
- for (int j = 0; j < values.length; j++)
+ for (int j = 0; j < values.length; j++) {
v.setQuick(j, values[j]);
+ }
sampleData.add(v);
}
/**
* Generate random samples and add them to the sampleData
- *
+ *
* @param num int number of samples to generate
- * @param mx double x-value of the sample mean
- * @param my double y-value of the sample mean
- * @param sd double standard deviation of the samples
+ * @param mx double x-value of the sample mean
+ * @param my double y-value of the sample mean
+ * @param sd double standard deviation of the samples
*/
private void generateSamples(int num, double mx, double my, double sd) {
System.out.println("Generating " + num + " samples m=[" + mx + ", " + my
+ "] sd=" + sd);
for (int i = 0; i < num; i++) {
- addSample(new double[] { UncommonDistributions.rNorm(mx, sd),
- UncommonDistributions.rNorm(my, sd) });
+ addSample(new double[]{UncommonDistributions.rNorm(mx, sd),
+ UncommonDistributions.rNorm(my, sd)});
}
}
@@ -110,11 +109,7 @@
f.mkdir();
}
- /**
- * Test the basic Mapper
- *
- * @throws Exception
- */
+ /** Test the basic Mapper */
public void testMapper() throws Exception {
generateSamples(10, 0, 0, 1);
DirichletState<Vector> state = new DirichletState<Vector>(
@@ -123,18 +118,15 @@
mapper.configure(state);
DummyOutputCollector<Text, Vector> collector = new DummyOutputCollector<Text, Vector>();
- for (Vector v : sampleData)
+ for (Vector v : sampleData) {
mapper.map(null, v, collector, null);
+ }
Map<String, List<Vector>> data = collector.getData();
// this seed happens to produce two partitions, but they work
assertEquals("output size", 3, data.size());
}
- /**
- * Test the basic Reducer
- *
- * @throws Exception
- */
+ /** Test the basic Reducer */
public void testReducer() throws Exception {
generateSamples(100, 0, 0, 1);
generateSamples(100, 2, 0, 1);
@@ -146,8 +138,9 @@
mapper.configure(state);
DummyOutputCollector<Text, Vector> mapCollector = new DummyOutputCollector<Text, Vector>();
- for (Vector v : sampleData)
+ for (Vector v : sampleData) {
mapper.map(null, v, mapCollector, null);
+ }
Map<String, List<Vector>> data = mapCollector.getData();
// this seed happens to produce three partitions, but they work
assertEquals("output size", 7, data.size());
@@ -155,15 +148,16 @@
DirichletReducer reducer = new DirichletReducer();
reducer.configure(state);
DummyOutputCollector<Text, DirichletCluster<Vector>> reduceCollector = new DummyOutputCollector<Text, DirichletCluster<Vector>>();
- for (String key : mapCollector.getKeys())
+ for (String key : mapCollector.getKeys()) {
reducer.reduce(new Text(key), mapCollector.getValue(key).iterator(),
reduceCollector, null);
+ }
Model<Vector>[] newModels = reducer.newModels;
state.update(newModels);
}
- private void printModels(List<Model<Vector>[]> results, int significant) {
+ private static void printModels(List<Model<Vector>[]> results, int significant) {
int row = 0;
for (Model<Vector>[] r : results) {
System.out.print("sample[" + row++ + "]= ");
@@ -178,11 +172,7 @@
System.out.println();
}
- /**
- * Test the Mapper and Reducer in an iteration loop
- *
- * @throws Exception
- */
+ /** Test the Mapper and Reducer in an iteration loop */
public void testMRIterations() throws Exception {
generateSamples(100, 0, 0, 1);
generateSamples(100, 2, 0, 1);
@@ -197,15 +187,17 @@
DirichletMapper mapper = new DirichletMapper();
mapper.configure(state);
DummyOutputCollector<Text, Vector> mapCollector = new DummyOutputCollector<Text, Vector>();
- for (Vector v : sampleData)
+ for (Vector v : sampleData) {
mapper.map(null, v, mapCollector, null);
+ }
DirichletReducer reducer = new DirichletReducer();
reducer.configure(state);
DummyOutputCollector<Text, DirichletCluster<Vector>> reduceCollector = new DummyOutputCollector<Text, DirichletCluster<Vector>>();
- for (String key : mapCollector.getKeys())
+ for (String key : mapCollector.getKeys()) {
reducer.reduce(new Text(key), mapCollector.getValue(key).iterator(),
reduceCollector, null);
+ }
Model<Vector>[] newModels = reducer.newModels;
state.update(newModels);
@@ -216,40 +208,41 @@
@SuppressWarnings("unchecked")
public void testNormalModelSerialization() {
- double[] m = { 1.1, 2.2 };
- Model model = new NormalModel(new DenseVector(m), 3.3);
+ double[] m = {1.1, 2.2};
+ Model<?> model = new NormalModel(new DenseVector(m), 3.3);
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Vector.class, new JsonVectorAdapter());
Gson gson = builder.create();
String jsonString = gson.toJson(model);
- Model model2 = gson.fromJson(jsonString, NormalModel.class);
+ Model<?> model2 = gson.fromJson(jsonString, NormalModel.class);
assertEquals("models", model.toString(), model2.toString());
}
@SuppressWarnings("unchecked")
public void testNormalModelDistributionSerialization() {
NormalModelDistribution dist = new NormalModelDistribution();
- Model[] models = dist.sampleFromPrior(20);
+ Model<?>[] models = dist.sampleFromPrior(20);
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Vector.class, new JsonVectorAdapter());
Gson gson = builder.create();
String jsonString = gson.toJson(models);
- Model[] models2 = gson.fromJson(jsonString, NormalModel[].class);
+ Model<?>[] models2 = gson.fromJson(jsonString, NormalModel[].class);
assertEquals("models", models.length, models2.length);
- for (int i = 0; i < models.length; i++)
- assertEquals("model[" + i + "]", models[i].toString(), models2[i]
+ for (int i = 0; i < models.length; i++) {
+ assertEquals("model[" + i + ']', models[i].toString(), models2[i]
.toString());
+ }
}
@SuppressWarnings("unchecked")
public void testSampledNormalModelSerialization() {
- double[] m = { 1.1, 2.2 };
- Model model = new SampledNormalModel(new DenseVector(m), 3.3);
+ double[] m = {1.1, 2.2};
+ Model<?> model = new SampledNormalModel(new DenseVector(m), 3.3);
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Vector.class, new JsonVectorAdapter());
Gson gson = builder.create();
String jsonString = gson.toJson(model);
- Model model2 = gson.fromJson(jsonString, SampledNormalModel.class);
+ Model<?> model2 = gson.fromJson(jsonString, SampledNormalModel.class);
assertEquals("models", model.toString(), model2.toString());
}
@@ -263,22 +256,23 @@
String jsonString = gson.toJson(models);
Model[] models2 = gson.fromJson(jsonString, SampledNormalModel[].class);
assertEquals("models", models.length, models2.length);
- for (int i = 0; i < models.length; i++)
- assertEquals("model[" + i + "]", models[i].toString(), models2[i]
+ for (int i = 0; i < models.length; i++) {
+ assertEquals("model[" + i + ']', models[i].toString(), models2[i]
.toString());
+ }
}
@SuppressWarnings("unchecked")
public void testAsymmetricSampledNormalModelSerialization() {
- double[] m = { 1.1, 2.2 };
- double[] s = { 3.3, 4.4 };
- Model model = new AsymmetricSampledNormalModel(new DenseVector(m),
+ double[] m = {1.1, 2.2};
+ double[] s = {3.3, 4.4};
+ Model<?> model = new AsymmetricSampledNormalModel(new DenseVector(m),
new DenseVector(s));
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Vector.class, new JsonVectorAdapter());
Gson gson = builder.create();
String jsonString = gson.toJson(model);
- Model model2 = gson
+ Model<?> model2 = gson
.fromJson(jsonString, AsymmetricSampledNormalModel.class);
assertEquals("models", model.toString(), model2.toString());
}
@@ -294,9 +288,10 @@
Model[] models2 = gson.fromJson(jsonString,
AsymmetricSampledNormalModel[].class);
assertEquals("models", models.length, models2.length);
- for (int i = 0; i < models.length; i++)
- assertEquals("model[" + i + "]", models[i].toString(), models2[i]
+ for (int i = 0; i < models.length; i++) {
+ assertEquals("model[" + i + ']', models[i].toString(), models2[i]
.toString());
+ }
}
@SuppressWarnings("unchecked")
@@ -306,7 +301,7 @@
builder
.registerTypeAdapter(ModelHolder.class, new JsonModelHolderAdapter());
Gson gson = builder.create();
- double[] d = { 1.1, 2.2 };
+ double[] d = {1.1, 2.2};
ModelHolder mh = new ModelHolder(new NormalModel(new DenseVector(d), 3.3));
String format = gson.toJson(mh);
System.out.println(format);
@@ -321,8 +316,8 @@
builder
.registerTypeAdapter(ModelHolder.class, new JsonModelHolderAdapter());
Gson gson = builder.create();
- double[] d = { 1.1, 2.2 };
- double[] s = { 3.3, 4.4 };
+ double[] d = {1.1, 2.2};
+ double[] s = {3.3, 4.4};
ModelHolder mh = new ModelHolder(new AsymmetricSampledNormalModel(
new DenseVector(d), new DenseVector(s)));
String format = gson.toJson(mh);
@@ -351,15 +346,12 @@
assertEquals("dirichlet", state.offset, state2.offset);
}
- /**
- * Test the Mapper and Reducer using the Driver
- *
- * @throws Exception
- */
+ /** Test the Mapper and Reducer using the Driver */
public void testDriverMRIterations() throws Exception {
File f = new File("input");
- for (File g : f.listFiles())
+ for (File g : f.listFiles()) {
g.delete();
+ }
generateSamples(100, 0, 0, 0.5);
generateSamples(100, 2, 0, 0.2);
generateSamples(100, 0, 2, 0.3);
@@ -406,15 +398,12 @@
System.out.println();
}
- /**
- * Test the Mapper and Reducer using the Driver
- *
- * @throws Exception
- */
+ /** Test the Mapper and Reducer using the Driver */
public void testDriverMnRIterations() throws Exception {
File f = new File("input");
- for (File g : f.listFiles())
+ for (File g : f.listFiles()) {
g.delete();
+ }
generate4Datasets();
// Now run the driver
DirichletDriver
@@ -456,15 +445,12 @@
conf);
}
- /**
- * Test the Mapper and Reducer using the Driver
- *
- * @throws Exception
- */
+ /** Test the Mapper and Reducer using the Driver */
public void testDriverMnRnIterations() throws Exception {
File f = new File("input");
- for (File g : f.listFiles())
+ for (File g : f.listFiles()) {
g.delete();
+ }
generate4Datasets();
// Now run the driver
DirichletDriver
@@ -488,15 +474,12 @@
printResults(clusters, 0);
}
- /**
- * Test the Mapper and Reducer using the Driver
- *
- * @throws Exception
- */
+ /** Test the Mapper and Reducer using the Driver */
public void testDriverMnRnIterationsAsymmetric() throws Exception {
File f = new File("input");
- for (File g : f.listFiles())
+ for (File g : f.listFiles()) {
g.delete();
+ }
generateSamples(500, 0, 0, 0.5, 1.0);
ClusteringTestUtils.writePointsToFile(sampleData, "input/data1.txt", fs,
conf);
Modified: lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/fuzzykmeans/TestFuzzyKmeansClustering.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/fuzzykmeans/TestFuzzyKmeansClustering.java?rev=792856&r1=792855&r2=792856&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/fuzzykmeans/TestFuzzyKmeansClustering.java (original)
+++ lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/fuzzykmeans/TestFuzzyKmeansClustering.java Fri Jul 10 09:35:19 2009
@@ -17,14 +17,7 @@
package org.apache.mahout.clustering.fuzzykmeans;
-import java.io.File;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
import junit.framework.TestCase;
-
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
@@ -39,6 +32,12 @@
import org.apache.mahout.utils.DummyOutputCollector;
import org.apache.mahout.utils.EuclideanDistanceMeasure;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
public class TestFuzzyKmeansClustering extends TestCase {
FileSystem fs;
@@ -86,8 +85,8 @@
}
public static void referenceFuzzyKMeans(List<Vector> points,
- List<SoftCluster> clusterList, Map<String, String> pointClusterInfo,
- String distanceMeasureClass, double threshold, int numIter)
+ List<SoftCluster> clusterList, Map<String, String> pointClusterInfo,
+ String distanceMeasureClass, double threshold, int numIter)
throws Exception {
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
Class<?> cl = ccl.loadClass(distanceMeasureClass);
@@ -102,7 +101,7 @@
}
public static boolean iterateReference(List<Vector> points,
- List<SoftCluster> clusterList, DistanceMeasure measure) {
+ List<SoftCluster> clusterList, DistanceMeasure measure) {
// for each
for (Vector point : points) {
List<Double> clusterDistanceList = new ArrayList<Double>();
@@ -119,20 +118,23 @@
}
boolean converged = true;
for (SoftCluster cluster : clusterList) {
- if (!cluster.computeConvergence())
+ if (!cluster.computeConvergence()) {
converged = false;
+ }
}
// update the cluster centers
- if (!converged)
- for (SoftCluster cluster : clusterList)
+ if (!converged) {
+ for (SoftCluster cluster : clusterList) {
cluster.recomputeCenter();
+ }
+ }
return converged;
}
public static void computeCluster(List<Vector> points,
- List<SoftCluster> clusterList, DistanceMeasure measure,
- Map<String, String> pointClusterInfo) {
+ List<SoftCluster> clusterList, DistanceMeasure measure,
+ Map<String, String> pointClusterInfo) {
for (Vector point : points) {
StringBuilder outputValue = new StringBuilder("[");
@@ -198,11 +200,13 @@
List<Vector> points = TestKmeansClustering
.getPoints(TestKmeansClustering.reference);
File testData = new File("testdata");
- if (!testData.exists())
+ if (!testData.exists()) {
testData.mkdir();
+ }
testData = new File("testdata/points");
- if (!testData.exists())
+ if (!testData.exists()) {
testData.mkdir();
+ }
Configuration conf = new Configuration();
ClusteringTestUtils.writePointsToFile(points, "testdata/points/file1", fs, conf);
ClusteringTestUtils.writePointsToFile(points, "testdata/points/file2", fs, conf);
@@ -227,7 +231,7 @@
.forName("UTF-8")));
*/
SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, new Path("testdata/clusters/part-00000"),
- Text.class, SoftCluster.class);
+ Text.class, SoftCluster.class);
for (int i = 0; i < k + 1; i++) {
Vector vec = tweakValue(points.get(i));
@@ -273,7 +277,7 @@
assertEquals("Number of clusters", k + 1, clusterInfoList.length);
*/
double prob = 0.0;
- double [] probabilities = out.getProbabilities();
+ double[] probabilities = out.getProbabilities();
for (double probability : probabilities) {
//SoftCluster cluster = clusters[i];
prob += probability;
@@ -471,7 +475,7 @@
+ cluster.toString());
cluster.recomputeCenter();
assertTrue("key center: " + key.getCenter().asFormatString() + " does not equal cluster: " +
- cluster.getCenter().asFormatString(), key.getCenter().equals(cluster.getCenter()));
+ cluster.getCenter().asFormatString(), key.getCenter().equals(cluster.getCenter()));
}
}
}
@@ -578,7 +582,7 @@
FuzzyKMeansOutput kMeansOutput = value.get(0);
SoftCluster[] softClusters = kMeansOutput.getClusters();
- double [] probabilities = kMeansOutput.getProbabilities();
+ double[] probabilities = kMeansOutput.getProbabilities();
assertEquals("Number of clusters", k + 1, softClusters.length);
for (String clusterInfo : refClusterInfoList) {
String[] clusterProb = clusterInfo.split(":");
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=792856&r1=792855&r2=792856&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 Fri Jul 10 09:35:19 2009
@@ -48,17 +48,17 @@
public class TestKmeansClustering extends TestCase {
- public static final double[][] reference = { { 1, 1 }, { 2, 1 }, { 1, 2 },
- { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 4 }, { 4, 5 }, { 5, 5 } };
+ public static final double[][] reference = {{1, 1}, {2, 1}, {1, 2},
+ {2, 2}, {3, 3}, {4, 4}, {5, 4}, {4, 5}, {5, 5}};
- public static final int[][] expectedNumPoints = { { 9 }, { 4, 5 },
- { 4, 4, 1 }, { 1, 2, 1, 5 }, { 1, 1, 1, 2, 4 }, { 1, 1, 1, 1, 1, 4 },
- { 1, 1, 1, 1, 1, 2, 2 }, { 1, 1, 1, 1, 1, 1, 2, 1 },
- { 1, 1, 1, 1, 1, 1, 1, 1, 1 } };
+ public static final int[][] expectedNumPoints = {{9}, {4, 5},
+ {4, 4, 1}, {1, 2, 1, 5}, {1, 1, 1, 2, 4}, {1, 1, 1, 1, 1, 4},
+ {1, 1, 1, 1, 1, 2, 2}, {1, 1, 1, 1, 1, 1, 2, 1},
+ {1, 1, 1, 1, 1, 1, 1, 1, 1}};
FileSystem fs;
- private void rmr(String path) throws Exception {
+ private static void rmr(String path) throws Exception {
File f = new File(path);
if (f.exists()) {
if (f.isDirectory()) {
@@ -81,17 +81,16 @@
}
/**
- * This is the reference k-means implementation. Given its inputs it iterates
- * over the points and clusters until their centers converge or until the
- * maximum number of iterations is exceeded.
- *
- * @param points the input List<Vector> of points
+ * This is the reference k-means implementation. Given its inputs it iterates over the points and clusters until their
+ * centers converge or until the maximum number of iterations is exceeded.
+ *
+ * @param points the input List<Vector> of points
* @param clusters the initial List<Cluster> of clusters
- * @param measure the DistanceMeasure to use
- * @param maxIter the maximum number of iterations
+ * @param measure the DistanceMeasure to use
+ * @param maxIter the maximum number of iterations
*/
private void referenceKmeans(List<Vector> points, List<Cluster> clusters,
- DistanceMeasure measure, int maxIter) {
+ DistanceMeasure measure, int maxIter) {
boolean converged = false;
int iteration = 0;
while (!converged && iteration++ < maxIter) {
@@ -100,16 +99,15 @@
}
/**
- * Perform a single iteration over the points and clusters, assigning points
- * to clusters and returning if the iterations are completed.
- *
- * @param points the List<Vector> having the input points
+ * Perform a single iteration over the points and clusters, assigning points to clusters and returning if the
+ * iterations are completed.
+ *
+ * @param points the List<Vector> having the input points
* @param clusters the List<Cluster> clusters
- * @param measure a DistanceMeasure to use
- * @return
+ * @param measure a DistanceMeasure to use
*/
- private boolean iterateReference(List<Vector> points, List<Cluster> clusters,
- DistanceMeasure measure) {
+ private static boolean iterateReference(List<Vector> points, List<Cluster> clusters,
+ DistanceMeasure measure) {
// iterate through all points, assigning each to the nearest cluster
for (Vector point : points) {
Cluster closestCluster = null;
@@ -126,13 +124,16 @@
// test for convergence
boolean converged = true;
for (Cluster cluster : clusters) {
- if (!cluster.computeConvergence())
+ if (!cluster.computeConvergence()) {
converged = false;
+ }
}
// update the cluster centers
- if (!converged)
- for (Cluster cluster : clusters)
+ if (!converged) {
+ for (Cluster cluster : clusters) {
cluster.recomputeCenter();
+ }
+ }
return converged;
}
@@ -147,11 +148,7 @@
return points;
}
- /**
- * Story: Test the reference implementation
- *
- * @throws Exception
- */
+ /** Story: Test the reference implementation */
public void testReferenceImplementation() throws Exception {
List<Vector> points = getPoints(reference);
DistanceMeasure measure = new EuclideanDistanceMeasure();
@@ -177,7 +174,7 @@
}
}
- private Map<String, Cluster> loadClusterMap(List<Cluster> clusters) {
+ private static Map<String, Cluster> loadClusterMap(List<Cluster> clusters) {
Map<String, Cluster> clusterMap = new HashMap<String, Cluster>();
for (Cluster cluster : clusters) {
@@ -186,11 +183,7 @@
return clusterMap;
}
- /**
- * Story: test that the mapper will map input points to the nearest cluster
- *
- * @throws Exception
- */
+ /** Story: test that the mapper will map input points to the nearest cluster */
public void testKMeansMapper() throws Exception {
KMeansMapper mapper = new KMeansMapper();
EuclideanDistanceMeasure euclideanDistanceMeasure = new EuclideanDistanceMeasure();
@@ -223,20 +216,16 @@
for (KMeansInfo value : values) {
double distance = euclideanDistanceMeasure.distance(cluster
.getCenter(), value.getPointTotal());
- for (Cluster c : clusters)
+ for (Cluster c : clusters) {
assertTrue("distance error", distance <= euclideanDistanceMeasure
.distance(value.getPointTotal(), c.getCenter()));
+ }
}
}
}
}
- /**
- * Story: test that the combiner will produce partial cluster totals for all
- * of the clusters and points that it sees
- *
- * @throws Exception
- */
+ /** Story: test that the combiner will produce partial cluster totals for all of the clusters and points that it sees */
public void testKMeansCombiner() throws Exception {
KMeansMapper mapper = new KMeansMapper();
EuclideanDistanceMeasure euclideanDistanceMeasure = new EuclideanDistanceMeasure();
@@ -263,9 +252,10 @@
// now combine the data
KMeansCombiner combiner = new KMeansCombiner();
DummyOutputCollector<Text, KMeansInfo> collector2 = new DummyOutputCollector<Text, KMeansInfo>();
- for (String key : collector.getKeys())
+ for (String key : collector.getKeys()) {
combiner.reduce(new Text(key), collector.getValue(key).iterator(),
collector2, null);
+ }
assertEquals("Number of map results", k + 1, collector2.getData().size());
// now verify that all points are accounted for
@@ -286,12 +276,7 @@
}
}
- /**
- * Story: test that the reducer will sum the partial cluster totals for all of
- * the clusters and points that it sees
- *
- * @throws Exception
- */
+ /** Story: test that the reducer will sum the partial cluster totals for all of the clusters and points that it sees */
public void testKMeansReducer() throws Exception {
KMeansMapper mapper = new KMeansMapper();
EuclideanDistanceMeasure euclideanDistanceMeasure = new EuclideanDistanceMeasure();
@@ -318,17 +303,19 @@
// now combine the data
KMeansCombiner combiner = new KMeansCombiner();
DummyOutputCollector<Text, KMeansInfo> collector2 = new DummyOutputCollector<Text, KMeansInfo>();
- for (String key : collector.getKeys())
+ for (String key : collector.getKeys()) {
combiner.reduce(new Text(key), collector.getValue(key).iterator(),
collector2, null);
+ }
// now reduce the data
KMeansReducer reducer = new KMeansReducer();
reducer.config(clusters);
DummyOutputCollector<Text, Cluster> collector3 = new DummyOutputCollector<Text, Cluster>();
- for (String key : collector2.getKeys())
+ for (String key : collector2.getKeys()) {
reducer.reduce(new Text(key), collector2.getValue(key).iterator(),
collector3, null);
+ }
assertEquals("Number of map results", k + 1, collector3.getData().size());
@@ -340,10 +327,11 @@
}
boolean converged = iterateReference(points, reference,
euclideanDistanceMeasure);
- if (k == 8)
+ if (k == 8) {
assertTrue("not converged? " + k, converged);
- else
+ } else {
assertFalse("converged? " + k, converged);
+ }
// now verify that all clusters have correct centers
converged = true;
@@ -361,26 +349,25 @@
/*assertEquals(k + " center[" + key + "][1]", ref.getCenter().get(1),
cluster.getCenter().get(1));*/
}
- if (k == 8)
+ if (k == 8) {
assertTrue("not converged? " + k, converged);
- else
+ } else {
assertFalse("converged? " + k, converged);
+ }
}
}
- /**
- * Story: User wishes to run kmeans job on reference data
- *
- * @throws Exception
- */
+ /** Story: User wishes to run kmeans job on reference data */
public void testKMeansMRJob() throws Exception {
List<Vector> points = getPoints(reference);
File testData = new File("testdata");
- if (!testData.exists())
+ if (!testData.exists()) {
testData.mkdir();
+ }
testData = new File("testdata/points");
- if (!testData.exists())
+ if (!testData.exists()) {
testData.mkdir();
+ }
Configuration conf = new Configuration();
ClusteringTestUtils.writePointsToFile(points, "testdata/points/file1", fs, conf);
@@ -431,29 +418,28 @@
}
reader.close();
if (k == 2)
- // cluster 3 is empty so won't appear in output
- assertEquals("clusters[" + k + "]", expect.length - 1, collector
+ // cluster 3 is empty so won't appear in output
+ {
+ assertEquals("clusters[" + k + ']', expect.length - 1, collector
.getKeys().size());
- else
- assertEquals("clusters[" + k + "]", expect.length, collector.getKeys()
+ } else {
+ assertEquals("clusters[" + k + ']', expect.length, collector.getKeys()
.size());
+ }
}
}
- /**
- * Story: User wants to use canopy clustering to input the initial clusters
- * for kmeans job.
- *
- * @throws Exception
- */
+ /** Story: User wants to use canopy clustering to input the initial clusters for kmeans job. */
public void textKMeansWithCanopyClusterInput() throws Exception {
List<Vector> points = getPoints(reference);
File testData = new File("testdata");
- if (!testData.exists())
+ if (!testData.exists()) {
testData.mkdir();
+ }
testData = new File("testdata/points");
- if (!testData.exists())
+ if (!testData.exists()) {
testData.mkdir();
+ }
Configuration conf = new Configuration();
ClusteringTestUtils.writePointsToFile(points, "testdata/points/file1", fs, conf);
ClusteringTestUtils.writePointsToFile(points, "testdata/points/file2", fs, conf);
Modified: lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/VisibleCluster.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/VisibleCluster.java?rev=792856&r1=792855&r2=792856&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/VisibleCluster.java (original)
+++ lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/kmeans/VisibleCluster.java Fri Jul 10 09:35:19 2009
@@ -23,9 +23,8 @@
import java.util.List;
/**
- * This Cluster subclass maintains a list of points in the cluster so it can
- * include them in its toString method. Useful for debugging but not practical
- * for production use since it holds onto all its points.
+ * This Cluster subclass maintains a list of points in the cluster so it can include them in its toString method. Useful
+ * for debugging but not practical for production use since it holds onto all its points.
*/
public class VisibleCluster extends Cluster {
@@ -44,9 +43,8 @@
/**
* Add a point to the canopy
- *
- * @param point
- * a Double[]
+ *
+ * @param point a Double[]
*/
@Override
public void addPoint(Vector point) {
@@ -54,12 +52,7 @@
points.add(point);
}
- /**
- * Return a printable representation of this object, using the user supplied
- * identifier
- *
- * @return
- */
+ /** Return a printable representation of this object, using the user supplied identifier */
@Override
public String toString() {
StringBuilder result = new StringBuilder();
Modified: lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/meanshift/TestMeanShift.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/meanshift/TestMeanShift.java?rev=792856&r1=792855&r2=792856&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/meanshift/TestMeanShift.java (original)
+++ lucene/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/meanshift/TestMeanShift.java Fri Jul 10 09:35:19 2009
@@ -17,15 +17,7 @@
package org.apache.mahout.clustering.meanshift;
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
import junit.framework.TestCase;
-
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
@@ -33,13 +25,19 @@
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.JobConf;
-import org.apache.mahout.matrix.CardinalityException;
import org.apache.mahout.matrix.DenseVector;
import org.apache.mahout.matrix.Vector;
import org.apache.mahout.utils.DistanceMeasure;
import org.apache.mahout.utils.DummyOutputCollector;
import org.apache.mahout.utils.EuclideanDistanceMeasure;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
public class TestMeanShift extends TestCase {
Vector[] raw = null;
@@ -58,7 +56,7 @@
/**
* Print the canopies to the transcript
- *
+ *
* @param canopies a List<Canopy>
*/
private static void printCanopies(List<MeanShiftCanopy> canopies) {
@@ -67,21 +65,19 @@
}
}
- /**
- * Print a graphical representation of the clustered image points as a 10x10
- * character mask
- *
- * @param canopies
- */
+ /** Print a graphical representation of the clustered image points as a 10x10 character mask */
private static void printImage(List<MeanShiftCanopy> canopies) {
char[][] out = new char[10][10];
- for (int i = 0; i < out.length; i++)
- for (int j = 0; j < out[0].length; j++)
+ for (int i = 0; i < out.length; i++) {
+ for (int j = 0; j < out[0].length; j++) {
out[i][j] = ' ';
+ }
+ }
for (MeanShiftCanopy canopy : canopies) {
int ch = 'A' + canopy.getCanopyId() - 100;
- for (Vector pt : canopy.getBoundPoints())
+ for (Vector pt : canopy.getBoundPoints()) {
out[(int) pt.getQuick(0)][(int) pt.getQuick(1)] = (char) ch;
+ }
}
for (char[] anOut : out) {
System.out.println(anOut);
@@ -130,25 +126,25 @@
rmr("output");
rmr("testdata");
raw = new Vector[100];
- for (int i = 0; i < 10; i++)
+ for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
int ix = i * 10 + j;
Vector v = new DenseVector(3);
v.setQuick(0, i);
v.setQuick(1, j);
- if (i == j)
+ if (i == j) {
v.setQuick(2, 9);
- else if (i + j == 9)
+ } else if (i + j == 9) {
v.setQuick(2, 4.5);
+ }
raw[ix] = v;
}
+ }
}
/**
- * Story: User can exercise the reference implementation to verify that the
- * test datapoints are clustered in a reasonable manner.
- *
- * @throws CardinalityException
+ * Story: User can exercise the reference implementation to verify that the test datapoints are clustered in a
+ * reasonable manner.
*/
public void testReferenceImplementation() {
MeanShiftCanopy.config(new EuclideanDistanceMeasure(), 4.0, 1.0, 0.5);
@@ -174,11 +170,8 @@
}
/**
- * Story: User can produce initial canopy centers using a
- * EuclideanDistanceMeasure and a CanopyMapper/Combiner which clusters input
- * points to produce an output set of canopies.
- *
- * @throws Exception
+ * Story: User can produce initial canopy centers using a EuclideanDistanceMeasure and a CanopyMapper/Combiner which
+ * clusters input points to produce an output set of canopies.
*/
public void testCanopyMapperEuclidean() throws Exception {
MeanShiftCanopyMapper mapper = new MeanShiftCanopyMapper();
@@ -193,8 +186,9 @@
}
// map the data
- for (MeanShiftCanopy canopy : canopies)
+ for (MeanShiftCanopy canopy : canopies) {
mapper.map(new Text(), canopy, collector, null);
+ }
mapper.close();
// now verify the output
@@ -229,11 +223,8 @@
}
/**
- * Story: User can produce final canopy centers using a
- * EuclideanDistanceMeasure and a CanopyReducer which clusters input centroid
- * points to produce an output set of final canopy centroid points.
- *
- * @throws Exception
+ * Story: User can produce final canopy centers using a EuclideanDistanceMeasure and a CanopyReducer which clusters
+ * input centroid points to produce an output set of final canopy centroid points.
*/
public void testCanopyReducerEuclidean() throws Exception {
MeanShiftCanopyMapper mapper = new MeanShiftCanopyMapper();
@@ -260,8 +251,9 @@
}
// map the data
- for (MeanShiftCanopy canopy : canopies)
+ for (MeanShiftCanopy canopy : canopies) {
mapper.map(new Text(), canopy, mapCollector, null);
+ }
mapper.close();
assertEquals("Number of map results", 1, mapCollector.getData().size());
@@ -304,16 +296,12 @@
}
}
- /**
- * Story: User can produce final point clustering using a Hadoop map/reduce
- * job and a EuclideanDistanceMeasure.
- *
- * @throws Exception
- */
+ /** Story: User can produce final point clustering using a Hadoop map/reduce job and a EuclideanDistanceMeasure. */
public void testCanopyEuclideanMRJob() throws Exception {
File testData = new File("testdata");
- if (!testData.exists())
+ if (!testData.exists()) {
testData.mkdir();
+ }
writePointsToFile(raw, "testdata/file1");
writePointsToFile(raw, "testdata/file2");
// now run the Job
Modified: lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/EvalMapperTest.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/EvalMapperTest.java?rev=792856&r1=792855&r2=792856&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/EvalMapperTest.java (original)
+++ lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/EvalMapperTest.java Fri Jul 10 09:35:19 2009
@@ -18,9 +18,9 @@
package org.apache.mahout.ga.watchmaker;
import junit.framework.TestCase;
+import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
-import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.mapred.JobConf;
import org.apache.mahout.ga.watchmaker.utils.DummyCandidate;
import org.apache.mahout.ga.watchmaker.utils.DummyEvaluator;
@@ -45,7 +45,7 @@
// Mapper
EvalMapper mapper = new EvalMapper();
- DummyOutputCollector<LongWritable,DoubleWritable> collector = new DummyOutputCollector<LongWritable,DoubleWritable>();
+ DummyOutputCollector<LongWritable, DoubleWritable> collector = new DummyOutputCollector<LongWritable, DoubleWritable>();
// prepare configuration
JobConf conf = new JobConf();
Modified: lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/MahoutEvaluatorTest.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/MahoutEvaluatorTest.java?rev=792856&r1=792855&r2=792856&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/MahoutEvaluatorTest.java (original)
+++ lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/MahoutEvaluatorTest.java Fri Jul 10 09:35:19 2009
@@ -87,7 +87,7 @@
}
private static void loadPopulation(FileSystem fs, Path f,
- List<DummyCandidate> population) throws IOException {
+ List<DummyCandidate> population) throws IOException {
FSDataInputStream in = fs.open(f);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
Modified: lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/STFitnessEvaluatorTest.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/STFitnessEvaluatorTest.java?rev=792856&r1=792855&r2=792856&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/STFitnessEvaluatorTest.java (original)
+++ lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/STFitnessEvaluatorTest.java Fri Jul 10 09:35:19 2009
@@ -41,7 +41,7 @@
@Override
protected void evaluate(List<? extends T> population,
- List<Double> evaluations) {
+ List<Double> evaluations) {
nbcalls++;
evaluations.addAll(this.evaluations);
}
@@ -55,10 +55,8 @@
}
/**
- * Test method for
- * {@link org.apache.mahout.ga.watchmaker.STFitnessEvaluator#evaluate(java.util.List, java.util.List)}.<br>
- * <br>
- * Make sure that evaluate() is not called twice for the same population.
+ * Test method for {@link org.apache.mahout.ga.watchmaker.STFitnessEvaluator#evaluate(java.util.List,
+ * java.util.List)}.<br> <br> Make sure that evaluate() is not called twice for the same population.
*/
public void testEvaluateSamePopulation() {
STFitnessEvaluatorMock<Integer> mock = new STFitnessEvaluatorMock<Integer>();
@@ -80,11 +78,9 @@
}
/**
- * Test method for
- * {@link org.apache.mahout.ga.watchmaker.STFitnessEvaluator#evaluate(java.util.List, java.util.List)}.<br>
- * <br>
- * Make sure that evaluate() is called as many different populations are
- * passed to getFitness().
+ * Test method for {@link org.apache.mahout.ga.watchmaker.STFitnessEvaluator#evaluate(java.util.List,
+ * java.util.List)}.<br> <br> Make sure that evaluate() is called as many different populations are passed to
+ * getFitness().
*/
public void testEvaluateDifferentPopulations() {
STFitnessEvaluatorMock<Integer> mock = new STFitnessEvaluatorMock<Integer>();
@@ -112,16 +108,18 @@
private static List<Integer> randomInts(int size, Random rng) {
List<Integer> population = new ArrayList<Integer>();
- for (int index = 0; index < size; index++)
+ for (int index = 0; index < size; index++) {
population.add(rng.nextInt());
+ }
return population;
}
private static List<Double> randomFloats(int size, Random rng) {
List<Double> population = new ArrayList<Double>();
- for (int index = 0; index < size; index++)
+ for (int index = 0; index < size; index++) {
population.add(rng.nextDouble());
+ }
return population;
}
Modified: lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/utils/DummyCandidate.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/utils/DummyCandidate.java?rev=792856&r1=792855&r2=792856&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/utils/DummyCandidate.java (original)
+++ lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/utils/DummyCandidate.java Fri Jul 10 09:35:19 2009
@@ -34,10 +34,12 @@
@Override
public boolean equals(Object obj) {
- if (this == obj)
+ if (this == obj) {
return true;
- if (obj == null || !(obj instanceof DummyCandidate))
+ }
+ if (obj == null || !(obj instanceof DummyCandidate)) {
return false;
+ }
DummyCandidate dc = (DummyCandidate) obj;
return index == dc.index;
Modified: lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/utils/DummyEvaluator.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/utils/DummyEvaluator.java?rev=792856&r1=792855&r2=792856&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/utils/DummyEvaluator.java (original)
+++ lucene/mahout/trunk/core/src/test/java/org/apache/mahout/ga/watchmaker/utils/DummyEvaluator.java Fri Jul 10 09:35:19 2009
@@ -25,35 +25,36 @@
import java.util.Random;
/**
- * Dummy FitnessEvaluator that stores the evaluations it calculates. Uses a
- * static storage to handle the evaluator duplication when passed as a Job
- * parameter.
+ * Dummy FitnessEvaluator that stores the evaluations it calculates. Uses a static storage to handle the evaluator
+ * duplication when passed as a Job parameter.
*/
public class DummyEvaluator implements FitnessEvaluator<DummyCandidate> {
private final Random rng = new Random();
- private static final Map<Integer,Double> evaluations = new HashMap<Integer,Double>();
+ private static final Map<Integer, Double> evaluations = new HashMap<Integer, Double>();
public static double getFitness(Integer key) {
- if (!evaluations.containsKey(key))
+ if (!evaluations.containsKey(key)) {
throw new RuntimeException("Fitness not found");
+ }
return evaluations.get(key);
}
-
+
public static void clearEvaluations() {
evaluations.clear();
}
-
+
@Override
public double getFitness(DummyCandidate candidate,
- List<? extends DummyCandidate> population) {
- if (evaluations.containsKey(candidate.getIndex()))
+ List<? extends DummyCandidate> population) {
+ if (evaluations.containsKey(candidate.getIndex())) {
throw new RuntimeException("Duplicate Fitness");
-
+ }
+
double fitness = rng.nextDouble();
evaluations.put(candidate.getIndex(), fitness);
-
+
return fitness;
}
Modified: lucene/mahout/trunk/core/src/test/java/org/apache/mahout/matrix/MatrixTest.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/test/java/org/apache/mahout/matrix/MatrixTest.java?rev=792856&r1=792855&r2=792856&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/test/java/org/apache/mahout/matrix/MatrixTest.java (original)
+++ lucene/mahout/trunk/core/src/test/java/org/apache/mahout/matrix/MatrixTest.java Fri Jul 10 09:35:19 2009
@@ -17,24 +17,23 @@
package org.apache.mahout.matrix;
+import junit.framework.TestCase;
+import org.apache.hadoop.io.DataOutputBuffer;
+
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
-import junit.framework.TestCase;
-
-import org.apache.hadoop.io.DataOutputBuffer;
-
public abstract class MatrixTest extends TestCase {
protected static final int ROW = AbstractMatrix.ROW;
protected static final int COL = AbstractMatrix.COL;
- protected final double[][] values = { { 1.1, 2.2 }, { 3.3, 4.4 },
- { 5.5, 6.6 } };
+ protected final double[][] values = {{1.1, 2.2}, {3.3, 4.4},
+ {5.5, 6.6}};
protected Matrix test;
@@ -60,18 +59,22 @@
int[] c = test.size();
Matrix copy = test.clone();
assertEquals("wrong class", copy.getClass(), test.getClass());
- for (int row = 0; row < c[ROW]; row++)
- for (int col = 0; col < c[COL]; col++)
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
assertEquals("value[" + row + "][" + col + ']',
test.getQuick(row, col), copy.getQuick(row, col));
+ }
+ }
}
public void testGetQuick() {
int[] c = test.size();
- for (int row = 0; row < c[ROW]; row++)
- for (int col = 0; col < c[COL]; col++)
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
assertEquals("value[" + row + "][" + col + ']', values[row][col], test
.getQuick(row, col));
+ }
+ }
}
public void testHaveSharedCells() {
@@ -95,12 +98,13 @@
public void testSetQuick() {
int[] c = test.size();
- for (int row = 0; row < c[ROW]; row++)
+ for (int row = 0; row < c[ROW]; row++) {
for (int col = 0; col < c[COL]; col++) {
test.setQuick(row, col, 1.23);
assertEquals("value[" + row + "][" + col + ']', 1.23, test.getQuick(
row, col));
}
+ }
}
public void testSize() {
@@ -110,19 +114,21 @@
}
public void testViewPart() {
- int[] offset = { 1, 1 };
- int[] size = { 2, 1 };
+ int[] offset = {1, 1};
+ int[] size = {2, 1};
Matrix view = test.viewPart(offset, size);
int[] c = view.size();
- for (int row = 0; row < c[ROW]; row++)
- for (int col = 0; col < c[COL]; col++)
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
assertEquals("value[" + row + "][" + col + ']',
values[row + 1][col + 1], view.getQuick(row, col));
+ }
+ }
}
public void testViewPartCardinality() {
- int[] offset = { 1, 1 };
- int[] size = { 3, 3 };
+ int[] offset = {1, 1};
+ int[] size = {3, 3};
try {
test.viewPart(offset, size);
fail("exception expected");
@@ -134,8 +140,8 @@
}
public void testViewPartIndexOver() {
- int[] offset = { 1, 1 };
- int[] size = { 2, 2 };
+ int[] offset = {1, 1};
+ int[] size = {2, 2};
try {
test.viewPart(offset, size);
fail("exception expected");
@@ -147,8 +153,8 @@
}
public void testViewPartIndexUnder() {
- int[] offset = { -1, -1 };
- int[] size = { 2, 2 };
+ int[] offset = {-1, -1};
+ int[] size = {2, 2};
try {
test.viewPart(offset, size);
fail("exception expected");
@@ -162,19 +168,23 @@
public void testAssignDouble() {
int[] c = test.size();
test.assign(4.53);
- for (int row = 0; row < c[ROW]; row++)
- for (int col = 0; col < c[COL]; col++)
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
assertEquals("value[" + row + "][" + col + ']', 4.53, test.getQuick(
row, col));
+ }
+ }
}
public void testAssignDoubleArrayArray() {
int[] c = test.size();
test.assign(new double[3][2]);
- for (int row = 0; row < c[ROW]; row++)
- for (int col = 0; col < c[COL]; col++)
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
assertEquals("value[" + row + "][" + col + ']', 0.0, test.getQuick(row,
col));
+ }
+ }
}
public void testAssignDoubleArrayArrayCardinality() {
@@ -190,10 +200,12 @@
public void testAssignMatrixBinaryFunction() {
int[] c = test.size();
test.assign(test, new PlusFunction());
- for (int row = 0; row < c[ROW]; row++)
- for (int col = 0; col < c[COL]; col++)
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
assertEquals("value[" + row + "][" + col + ']', 2 * values[row][col],
test.getQuick(row, col));
+ }
+ }
}
public void testAssignMatrixBinaryFunctionCardinality() {
@@ -209,10 +221,12 @@
int[] c = test.size();
Matrix value = test.like();
value.assign(test);
- for (int row = 0; row < c[ROW]; row++)
- for (int col = 0; col < c[COL]; col++)
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
assertEquals("value[" + row + "][" + col + ']',
test.getQuick(row, col), value.getQuick(row, col));
+ }
+ }
}
public void testAssignMatrixCardinality() {
@@ -227,35 +241,43 @@
public void testAssignUnaryFunction() {
int[] c = test.size();
test.assign(new NegateFunction());
- for (int row = 0; row < c[ROW]; row++)
- for (int col = 0; col < c[COL]; col++)
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
assertEquals("value[" + row + "][" + col + ']', -values[row][col], test
.getQuick(row, col));
+ }
+ }
}
public void testDivide() {
int[] c = test.size();
Matrix value = test.divide(4.53);
- for (int row = 0; row < c[ROW]; row++)
- for (int col = 0; col < c[COL]; col++)
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
assertEquals("value[" + row + "][" + col + ']',
values[row][col] / 4.53, value.getQuick(row, col));
+ }
+ }
}
public void testGet() {
int[] c = test.size();
- for (int row = 0; row < c[ROW]; row++)
- for (int col = 0; col < c[COL]; col++)
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
assertEquals("value[" + row + "][" + col + ']', values[row][col], test
.get(row, col));
+ }
+ }
}
public void testGetIndexUnder() {
int[] c = test.size();
try {
- for (int row = -1; row < c[ROW]; row++)
- for (int col = 0; col < c[COL]; col++)
+ for (int row = -1; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
test.get(row, col);
+ }
+ }
fail("index exception expected");
} catch (IndexException e) {
assertTrue(true);
@@ -265,9 +287,11 @@
public void testGetIndexOver() {
int[] c = test.size();
try {
- for (int row = 0; row < c[ROW] + 1; row++)
- for (int col = 0; col < c[COL]; col++)
+ for (int row = 0; row < c[ROW] + 1; row++) {
+ for (int col = 0; col < c[COL]; col++) {
test.get(row, col);
+ }
+ }
fail("index exception expected");
} catch (IndexException e) {
assertTrue(true);
@@ -277,10 +301,12 @@
public void testMinus() {
int[] c = test.size();
Matrix value = test.minus(test);
- for (int row = 0; row < c[ROW]; row++)
- for (int col = 0; col < c[COL]; col++)
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
assertEquals("value[" + row + "][" + col + ']', 0.0, value.getQuick(
row, col));
+ }
+ }
}
public void testMinusCardinality() {
@@ -295,19 +321,23 @@
public void testPlusDouble() {
int[] c = test.size();
Matrix value = test.plus(4.53);
- for (int row = 0; row < c[ROW]; row++)
- for (int col = 0; col < c[COL]; col++)
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
assertEquals("value[" + row + "][" + col + ']',
values[row][col] + 4.53, value.getQuick(row, col));
+ }
+ }
}
public void testPlusMatrix() {
int[] c = test.size();
Matrix value = test.plus(test);
- for (int row = 0; row < c[ROW]; row++)
- for (int col = 0; col < c[COL]; col++)
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
assertEquals("value[" + row + "][" + col + ']', values[row][col] * 2,
value.getQuick(row, col));
+ }
+ }
}
public void testPlusMatrixCardinality() {
@@ -322,10 +352,11 @@
public void testSetUnder() {
int[] c = test.size();
try {
- for (int row = -1; row < c[ROW]; row++)
+ for (int row = -1; row < c[ROW]; row++) {
for (int col = 0; col < c[COL]; col++) {
test.set(row, col, 1.23);
}
+ }
fail("index exception expected");
} catch (IndexException e) {
assertTrue(true);
@@ -335,10 +366,11 @@
public void testSetOver() {
int[] c = test.size();
try {
- for (int row = 0; row < c[ROW] + 1; row++)
+ for (int row = 0; row < c[ROW] + 1; row++) {
for (int col = 0; col < c[COL]; col++) {
test.set(row, col, 1.23);
}
+ }
fail("index exception expected");
} catch (IndexException e) {
assertTrue(true);
@@ -348,10 +380,12 @@
public void testTimesDouble() {
int[] c = test.size();
Matrix value = test.times(4.53);
- for (int row = 0; row < c[ROW]; row++)
- for (int col = 0; col < c[COL]; col++)
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
assertEquals("value[" + row + "][" + col + ']',
values[row][col] * 4.53, value.getQuick(row, col));
+ }
+ }
}
public void testTimesMatrix() {
@@ -383,10 +417,12 @@
int[] t = transpose.size();
assertEquals("rows", c[COL], t[ROW]);
assertEquals("cols", c[ROW], t[COL]);
- for (int row = 0; row < c[ROW]; row++)
- for (int col = 0; col < c[COL]; col++)
+ for (int row = 0; row < c[ROW]; row++) {
+ for (int col = 0; col < c[COL]; col++) {
assertEquals("value[" + row + "][" + col + ']',
test.getQuick(row, col), transpose.getQuick(col, row));
+ }
+ }
}
public void testZSum() {
@@ -395,14 +431,14 @@
}
public void testAssignRow() {
- double[] data = { 2.1, 3.2 };
+ double[] data = {2.1, 3.2};
test.assignRow(1, new DenseVector(data));
assertEquals("test[1][0]", 2.1, test.getQuick(1, 0));
assertEquals("test[1][1]", 3.2, test.getQuick(1, 1));
}
public void testAssignRowCardinality() {
- double[] data = { 2.1, 3.2, 4.3 };
+ double[] data = {2.1, 3.2, 4.3};
try {
test.assignRow(1, new DenseVector(data));
fail("expecting cardinality exception");
@@ -412,7 +448,7 @@
}
public void testAssignColumn() {
- double[] data = { 2.1, 3.2, 4.3 };
+ double[] data = {2.1, 3.2, 4.3};
test.assignColumn(1, new DenseVector(data));
assertEquals("test[0][1]", 2.1, test.getQuick(0, 1));
assertEquals("test[1][1]", 3.2, test.getQuick(1, 1));
@@ -420,7 +456,7 @@
}
public void testAssignColumnCardinality() {
- double[] data = { 2.1, 3.2 };
+ double[] data = {2.1, 3.2};
try {
test.assignColumn(1, new DenseVector(data));
fail("expecting cardinality exception");
@@ -476,24 +512,26 @@
}
public void testDetermitant() {
- Matrix m = matrixFactory(new double[][] { { 1, 3, 4 }, { 5, 2, 3 },
- { 1, 4, 2 } });
+ Matrix m = matrixFactory(new double[][]{{1, 3, 4}, {5, 2, 3},
+ {1, 4, 2}});
assertEquals("determinant", 43.0, m.determinant());
}
public void testAsFormatString() {
String string = test.asFormatString();
- int[] cardinality = { values.length, values[0].length };
+ int[] cardinality = {values.length, values[0].length};
Matrix m = AbstractMatrix.decodeMatrix(string);
- for (int row = 0; row < cardinality[ROW]; row++)
- for (int col = 0; col < cardinality[COL]; col++)
- assertEquals("m[" + row + "," + col + "]", test.get(row, col), m.get(
+ for (int row = 0; row < cardinality[ROW]; row++) {
+ for (int col = 0; col < cardinality[COL]; col++) {
+ assertEquals("m[" + row + ',' + col + ']', test.get(row, col), m.get(
row, col));
+ }
+ }
}
public void testLabelBindings() {
- Matrix m = matrixFactory(new double[][] { { 1, 3, 4 }, { 5, 2, 3 },
- { 1, 4, 2 } });
+ Matrix m = matrixFactory(new double[][]{{1, 3, 4}, {5, 2, 3},
+ {1, 4, 2}});
assertNull("row bindings", m.getRowLabelBindings());
assertNull("col bindings", m.getColumnLabelBindings());
Map<String, Integer> rowBindings = new HashMap<String, Integer>();
@@ -510,14 +548,14 @@
assertEquals("row", rowBindings, m.getRowLabelBindings());
assertEquals("Fee", m.get(0, 1), m.get("Fee", "Bar"));
- double[] newrow = { 9, 8, 7 };
+ double[] newrow = {9, 8, 7};
m.set("Foe", newrow);
assertEquals("FeeBaz", m.get(0, 2), m.get("Fee", "Baz"));
}
public void testSettingLabelBindings() {
- Matrix m = matrixFactory(new double[][] { { 1, 3, 4 }, { 5, 2, 3 },
- { 1, 4, 2 } });
+ Matrix m = matrixFactory(new double[][]{{1, 3, 4}, {5, 2, 3},
+ {1, 4, 2}});
assertNull("row bindings", m.getRowLabelBindings());
assertNull("col bindings", m.getColumnLabelBindings());
m.set("Fee", "Foo", 1, 2, 9);
@@ -537,8 +575,8 @@
}
public void testLabelBindingSerialization() {
- Matrix m = matrixFactory(new double[][] { { 1, 3, 4 }, { 5, 2, 3 },
- { 1, 4, 2 } });
+ Matrix m = matrixFactory(new double[][]{{1, 3, 4}, {5, 2, 3},
+ {1, 4, 2}});
assertNull("row bindings", m.getRowLabelBindings());
assertNull("col bindings", m.getColumnLabelBindings());
Map<String, Integer> rowBindings = new HashMap<String, Integer>();
@@ -558,8 +596,8 @@
}
public void testMatrixWritable() throws IOException {
- Matrix m = matrixFactory(new double[][] { { 1, 3, 4 }, { 5, 2, 3 },
- { 1, 4, 2 } });
+ Matrix m = matrixFactory(new double[][]{{1, 3, 4}, {5, 2, 3},
+ {1, 4, 2}});
DataOutputBuffer out = new DataOutputBuffer();
m.write(out);
out.close();
Modified: lucene/mahout/trunk/core/src/test/java/org/apache/mahout/matrix/TestDenseVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/test/java/org/apache/mahout/matrix/TestDenseVector.java?rev=792856&r1=792855&r2=792856&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/test/java/org/apache/mahout/matrix/TestDenseVector.java (original)
+++ lucene/mahout/trunk/core/src/test/java/org/apache/mahout/matrix/TestDenseVector.java Fri Jul 10 09:35:19 2009
@@ -17,15 +17,15 @@
package org.apache.mahout.matrix;
+import junit.framework.TestCase;
+
import java.util.HashMap;
-import java.util.Map;
import java.util.Iterator;
-
-import junit.framework.TestCase;
+import java.util.Map;
public class TestDenseVector extends TestCase {
- final double[] values = { 1.1, 2.2, 3.3 };
+ final double[] values = {1.1, 2.2, 3.3};
final Vector test = new DenseVector(values);
@@ -47,13 +47,15 @@
public void testCopy() throws Exception {
Vector copy = test.clone();
- for (int i = 0; i < test.size(); i++)
+ for (int i = 0; i < test.size(); i++) {
assertEquals("copy [" + i + ']', test.get(i), copy.get(i));
+ }
}
public void testGet() throws Exception {
- for (int i = 0; i < test.size(); i++)
+ for (int i = 0; i < test.size(); i++) {
assertEquals("get [" + i + ']', values[i], test.get(i));
+ }
}
public void testGetOver() {
@@ -76,11 +78,13 @@
public void testSet() throws Exception {
test.set(2, 4.5);
- for (int i = 0; i < test.size(); i++)
- if (i == 2)
+ for (int i = 0; i < test.size(); i++) {
+ if (i == 2) {
assertEquals("set [" + i + ']', 4.5, test.get(i));
- else
+ } else {
assertEquals("set [" + i + ']', values[i], test.get(i));
+ }
+ }
}
@@ -91,7 +95,7 @@
iterator = test.iterateAll();
checkIterator(iterator, values, 3);
- double[] doubles = new double[]{0.0, 5.0, 0, 3.0};
+ double[] doubles = {0.0, 5.0, 0, 3.0};
DenseVector zeros = new DenseVector(doubles);
iterator = zeros.iterateNonZero();
checkIterator(iterator, doubles, 2);
@@ -107,11 +111,11 @@
}
- private void checkIterator(Iterator<Vector.Element> nzIter, double[] values, int expectedNum) {
+ private static void checkIterator(Iterator<Vector.Element> nzIter, double[] values, int expectedNum) {
int i = 0;
while (nzIter.hasNext()) {
Vector.Element elt = nzIter.next();
- assertTrue((elt.index()) + " Value: " + values[elt.index() ] + " does not equal: " + elt.get(), values[elt.index()] == elt.get());
+ assertTrue((elt.index()) + " Value: " + values[elt.index()] + " does not equal: " + elt.get(), values[elt.index()] == elt.get());
i++;
}
assertTrue(i + " does not equal: " + expectedNum, i == expectedNum);
@@ -124,8 +128,9 @@
public void testViewPart() throws Exception {
Vector part = test.viewPart(1, 2);
assertEquals("part size", 2, part.getNumNondefaultElements());
- for (int i = 0; i < part.size(); i++)
+ for (int i = 0; i < part.size(); i++) {
assertEquals("part[" + i + ']', values[i + 1], part.get(i));
+ }
}
public void testViewPartUnder() {
@@ -163,20 +168,23 @@
public void testDecodeVector() throws Exception {
Vector val = AbstractVector.decodeVector(test.asFormatString());
- for (int i = 0; i < test.size(); i++)
+ for (int i = 0; i < test.size(); i++) {
assertEquals("get [" + i + ']', test.get(i), val.get(i));
+ }
}
public void testDenseVectorDoubleArray() throws Exception {
- for (int i = 0; i < test.size(); i++)
+ for (int i = 0; i < test.size(); i++) {
assertEquals("test[" + i + ']', values[i], test.get(i));
+ }
}
public void testDenseVectorInt() throws Exception {
Vector val = new DenseVector(4);
assertEquals("cardinality", 4, val.size());
- for (int i = 0; i < test.size(); i++)
+ for (int i = 0; i < test.size(); i++) {
assertEquals("get [" + i + ']', 0.0, val.get(i));
+ }
}
public void testDot() throws Exception {
@@ -196,29 +204,33 @@
public void testNormalize() throws Exception {
Vector res = test.normalize();
double mag = Math.sqrt(1.1 * 1.1 + 2.2 * 2.2 + 3.3 * 3.3);
- for (int i = 0; i < test.size(); i++)
+ for (int i = 0; i < test.size(); i++) {
assertEquals("dot", values[i] / mag, res.get(i));
+ }
}
public void testMinus() throws Exception {
Vector val = test.minus(test);
assertEquals("cardinality", 3, val.size());
- for (int i = 0; i < test.size(); i++)
+ for (int i = 0; i < test.size(); i++) {
assertEquals("get [" + i + ']', 0.0, val.get(i));
+ }
}
public void testPlusDouble() throws Exception {
Vector val = test.plus(1);
assertEquals("cardinality", 3, val.size());
- for (int i = 0; i < test.size(); i++)
+ for (int i = 0; i < test.size(); i++) {
assertEquals("get [" + i + ']', values[i] + 1, val.get(i));
+ }
}
public void testPlusVector() throws Exception {
Vector val = test.plus(test);
assertEquals("cardinality", 3, val.size());
- for (int i = 0; i < test.size(); i++)
+ for (int i = 0; i < test.size(); i++) {
assertEquals("get [" + i + ']', values[i] * 2, val.get(i));
+ }
}
public void testPlusVectorCardinality() {
@@ -233,22 +245,25 @@
public void testTimesDouble() throws Exception {
Vector val = test.times(3);
assertEquals("cardinality", 3, val.size());
- for (int i = 0; i < test.size(); i++)
+ for (int i = 0; i < test.size(); i++) {
assertEquals("get [" + i + ']', values[i] * 3, val.get(i));
+ }
}
public void testDivideDouble() throws Exception {
Vector val = test.divide(3);
assertEquals("cardinality", 3, val.size());
- for (int i = 0; i < test.size(); i++)
+ for (int i = 0; i < test.size(); i++) {
assertEquals("get [" + i + ']', values[i] / 3, val.get(i));
+ }
}
public void testTimesVector() throws Exception {
Vector val = test.times(test);
assertEquals("cardinality", 3, val.size());
- for (int i = 0; i < test.size(); i++)
+ for (int i = 0; i < test.size(); i++) {
assertEquals("get [" + i + ']', values[i] * values[i], val.get(i));
+ }
}
public void testTimesVectorCardinality() {
@@ -334,8 +349,9 @@
public void testAssignBinaryFunction3() throws Exception {
test.assign(new TimesFunction(), 4);
- for (int i = 0; i < values.length; i++)
+ for (int i = 0; i < values.length; i++) {
assertEquals("value[" + i + ']', values[i] * 4, test.getQuick(i));
+ }
}
public void testAssignBinaryFunctionCardinality() {