@Test public void testMatrixDiagonalizeMapper() throws Exception { MatrixDiagonalizeMapper mapper = new MatrixDiagonalizeMapper(); Configuration conf = getConfiguration(); conf.setInt(Keys.AFFINITY_DIMENSIONS, RAW_DIMENSIONS); // set up the dummy writers DummyRecordWriter<NullWritable, IntDoublePairWritable> writer = new DummyRecordWriter<>(); Mapper<IntWritable, VectorWritable, NullWritable, IntDoublePairWritable>.Context context = DummyRecordWriter.build(mapper, conf, writer); // perform the mapping for (int i = 0; i < RAW_DIMENSIONS; i++) { RandomAccessSparseVector toAdd = new RandomAccessSparseVector(RAW_DIMENSIONS); toAdd.assign(RAW[i]); mapper.map(new IntWritable(i), new VectorWritable(toAdd), context); } // check the number of the results assertEquals( "Number of map results", RAW_DIMENSIONS, writer.getValue(NullWritable.get()).size()); }
@Test public void testMatrixDiagonalizeReducer() throws Exception { MatrixDiagonalizeMapper mapper = new MatrixDiagonalizeMapper(); Configuration conf = getConfiguration(); conf.setInt(Keys.AFFINITY_DIMENSIONS, RAW_DIMENSIONS); // set up the dummy writers DummyRecordWriter<NullWritable, IntDoublePairWritable> mapWriter = new DummyRecordWriter<>(); Mapper<IntWritable, VectorWritable, NullWritable, IntDoublePairWritable>.Context mapContext = DummyRecordWriter.build(mapper, conf, mapWriter); // perform the mapping for (int i = 0; i < RAW_DIMENSIONS; i++) { RandomAccessSparseVector toAdd = new RandomAccessSparseVector(RAW_DIMENSIONS); toAdd.assign(RAW[i]); mapper.map(new IntWritable(i), new VectorWritable(toAdd), mapContext); } // now perform the reduction MatrixDiagonalizeReducer reducer = new MatrixDiagonalizeReducer(); DummyRecordWriter<NullWritable, VectorWritable> redWriter = new DummyRecordWriter<>(); Reducer<NullWritable, IntDoublePairWritable, NullWritable, VectorWritable>.Context redContext = DummyRecordWriter.build( reducer, conf, redWriter, NullWritable.class, IntDoublePairWritable.class); // only need one reduction reducer.reduce(NullWritable.get(), mapWriter.getValue(NullWritable.get()), redContext); // first, make sure there's only one result List<VectorWritable> list = redWriter.getValue(NullWritable.get()); assertEquals("Only a single resulting vector", 1, list.size()); Vector v = list.get(0).get(); for (int i = 0; i < v.size(); i++) { assertEquals("Element sum is correct", rowSum(RAW[i]), v.get(i), 0.01); } }