@Test public void testLabelBindingSerialization() { assertNull("row bindings", test.getRowLabelBindings()); assertNull("col bindings", test.getColumnLabelBindings()); Map<String, Integer> rowBindings = Maps.newHashMap(); rowBindings.put("Fee", 0); rowBindings.put("Fie", 1); rowBindings.put("Foe", 2); test.setRowLabelBindings(rowBindings); assertEquals("row", rowBindings, test.getRowLabelBindings()); Map<String, Integer> colBindings = Maps.newHashMap(); colBindings.put("Foo", 0); colBindings.put("Bar", 1); colBindings.put("Baz", 2); test.setColumnLabelBindings(colBindings); assertEquals("col", colBindings, test.getColumnLabelBindings()); }
@Test public void testLabelBindingSerialization() { 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<>(); rowBindings.put("Fee", 0); rowBindings.put("Fie", 1); rowBindings.put("Foe", 2); m.setRowLabelBindings(rowBindings); assertEquals("row", rowBindings, m.getRowLabelBindings()); Map<String, Integer> colBindings = new HashMap<>(); colBindings.put("Foo", 0); colBindings.put("Bar", 1); colBindings.put("Baz", 2); m.setColumnLabelBindings(colBindings); assertEquals("col", colBindings, m.getColumnLabelBindings()); } }
flags |= FLAG_SEQUENTIAL; if (matrix.getRowLabelBindings() != null || matrix.getColumnLabelBindings() != null) { flags |= FLAG_LABELS; writeLabelBindings(out, matrix.getColumnLabelBindings(), matrix.getRowLabelBindings());
@Test(expected = IllegalStateException.class) public void testSettingLabelBindings() { assertNull("row bindings", test.getRowLabelBindings()); assertNull("col bindings", test.getColumnLabelBindings()); test.set("Fee", "Foo", 1, 1, 9); assertNotNull("row", test.getRowLabelBindings()); assertNotNull("row", test.getRowLabelBindings()); assertEquals("Fee", 1, test.getRowLabelBindings().get("Fee").intValue()); assertEquals("Foo", 1, test.getColumnLabelBindings().get("Foo").intValue()); assertEquals("FeeFoo", test.get(1, 1), test.get("Fee", "Foo"), EPSILON); test.get("Fie", "Foe"); }
private static void compareMatrices(Matrix m, Matrix m2) { assertEquals(m.numRows(), m2.numRows()); assertEquals(m.numCols(), m2.numCols()); for (int r = 0; r < m.numRows(); r++) { for (int c = 0; c < m.numCols(); c++) { assertEquals(m.get(r, c), m2.get(r, c), EPSILON); } } Map<String,Integer> bindings = m.getRowLabelBindings(); Map<String, Integer> bindings2 = m2.getRowLabelBindings(); assertEquals(bindings == null, bindings2 == null); if (bindings != null) { assertEquals(bindings.size(), m.numRows()); assertEquals(bindings.size(), bindings2.size()); for (Map.Entry<String,Integer> entry : bindings.entrySet()) { assertEquals(entry.getValue(), bindings2.get(entry.getKey())); } } bindings = m.getColumnLabelBindings(); bindings2 = m2.getColumnLabelBindings(); assertEquals(bindings == null, bindings2 == null); if (bindings != null) { assertEquals(bindings.size(), bindings2.size()); for (Map.Entry<String,Integer> entry : bindings.entrySet()) { assertEquals(entry.getValue(), bindings2.get(entry.getKey())); } } }
@Test(expected = IllegalStateException.class) public void testSettingLabelBindings() { 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); assertNotNull("row", m.getRowLabelBindings()); assertNotNull("row", m.getRowLabelBindings()); assertEquals("Fee", 1, m.getRowLabelBindings().get("Fee").intValue()); assertEquals("Fee", 2, m.getColumnLabelBindings().get("Foo").intValue()); assertEquals("FeeFoo", m.get(1, 2), m.get("Fee", "Foo"), EPSILON); m.get("Fie", "Foe"); }
@Test public void testLabelBindings() { 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<>(); rowBindings.put("Fee", 0); rowBindings.put("Fie", 1); rowBindings.put("Foe", 2); m.setRowLabelBindings(rowBindings); assertEquals("row", rowBindings, m.getRowLabelBindings()); Map<String, Integer> colBindings = new HashMap<>(); colBindings.put("Foo", 0); colBindings.put("Bar", 1); colBindings.put("Baz", 2); m.setColumnLabelBindings(colBindings); assertEquals("row", rowBindings, m.getRowLabelBindings()); assertEquals("Fee", m.get(0, 1), m.get("Fee", "Bar"), EPSILON); double[] newrow = {9, 8, 7}; m.set("Foe", newrow); assertEquals("FeeBaz", m.get(0, 2), m.get("Fee", "Baz"), EPSILON); }
@Test public void testLabelBindings() { assertNull("row bindings", test.getRowLabelBindings()); assertNull("col bindings", test.getColumnLabelBindings()); Map<String, Integer> rowBindings = Maps.newHashMap(); rowBindings.put("Fee", 0); rowBindings.put("Fie", 1); test.setRowLabelBindings(rowBindings); assertEquals("row", rowBindings, test.getRowLabelBindings()); Map<String, Integer> colBindings = Maps.newHashMap(); colBindings.put("Foo", 0); colBindings.put("Bar", 1); test.setColumnLabelBindings(colBindings); assertEquals("row", rowBindings, test.getRowLabelBindings()); assertEquals("Fee", test.get(0, 1), test.get("Fee", "Bar"), EPSILON); double[] newrow = {9, 8}; test.set("Fie", newrow); assertEquals("FeeBar", test.get(0, 1), test.get("Fee", "Bar"), EPSILON); }
private static void doTestMatrixWritableEquals(Matrix m) throws IOException { Writable matrixWritable = new MatrixWritable(m); MatrixWritable matrixWritable2 = new MatrixWritable(); writeAndRead(matrixWritable, matrixWritable2); Matrix m2 = matrixWritable2.get(); compareMatrices(m, m2); doCheckBindings(m2.getRowLabelBindings()); doCheckBindings(m2.getColumnLabelBindings()); }
public void setMatrix(Matrix m) { int length = confusionMatrix.length; if (m.numRows() != m.numCols()) { throw new IllegalArgumentException( "ConfusionMatrix: matrix(" + m.numRows() + ',' + m.numCols() + ") must be square"); } for (int r = 0; r < length; r++) { for (int c = 0; c < length; c++) { confusionMatrix[r][c] = (int) Math.round(m.get(r, c)); } } Map<String,Integer> labels = m.getRowLabelBindings(); if (labels == null) { labels = m.getColumnLabelBindings(); } if (labels != null) { String[] sorted = sortLabels(labels); verifyLabels(length, sorted); labelMap.clear(); for (int i = 0; i < length; i++) { labelMap.put(sorted[i], i); } } }
public void setMatrix(Matrix m) { int length = confusionMatrix.length; if (m.numRows() != m.numCols()) { throw new IllegalArgumentException( "ConfusionMatrix: matrix(" + m.numRows() + ',' + m.numCols() + ") must be square"); } for (int r = 0; r < length; r++) { for (int c = 0; c < length; c++) { confusionMatrix[r][c] = (int) Math.round(m.get(r, c)); } } Map<String,Integer> labels = m.getRowLabelBindings(); if (labels == null) { labels = m.getColumnLabelBindings(); } if (labels != null) { String[] sorted = sortLabels(labels); verifyLabels(length, sorted); labelMap.clear(); for (int i = 0; i < length; i++) { labelMap.put(sorted[i], i); } } }
public void setMatrix(Matrix m) { int length = confusionMatrix.length; if (m.numRows() != m.numCols()) { throw new IllegalArgumentException( "ConfusionMatrix: matrix(" + m.numRows() + ',' + m.numCols() + ") must be square"); } for (int r = 0; r < length; r++) { for (int c = 0; c < length; c++) { confusionMatrix[r][c] = (int) Math.round(m.get(r, c)); } } Map<String,Integer> labels = m.getRowLabelBindings(); if (labels == null) { labels = m.getColumnLabelBindings(); } if (labels != null) { String[] sorted = sortLabels(labels); verifyLabels(length, sorted); labelMap.clear(); for (int i = 0; i < length; i++) { labelMap.put(sorted[i], i); } } }
/** Writes a typed Matrix instance to the output stream */ public static void writeMatrix(DataOutput out, Matrix matrix) throws IOException { int flags = 0; Vector row = matrix.viewRow(0); if (row.isDense()) { flags |= FLAG_DENSE; } if (row.isSequentialAccess()) { flags |= FLAG_SEQUENTIAL; } if (matrix.getRowLabelBindings() != null || matrix.getColumnLabelBindings() != null) { flags |= FLAG_LABELS; } out.writeInt(flags); out.writeInt(matrix.rowSize()); out.writeInt(matrix.columnSize()); for (int i = 0; i < matrix.rowSize(); i++) { VectorWritable.writeVector(out, matrix.viewRow(i), false); } if ((flags & FLAG_LABELS) != 0) { writeLabelBindings(out, matrix.getColumnLabelBindings(), matrix.getRowLabelBindings()); } } }
@Test public void testLabelBindingSerialization() { assertNull("row bindings", test.getRowLabelBindings()); assertNull("col bindings", test.getColumnLabelBindings()); Map<String, Integer> rowBindings = Maps.newHashMap(); rowBindings.put("Fee", 0); rowBindings.put("Fie", 1); rowBindings.put("Foe", 2); test.setRowLabelBindings(rowBindings); assertEquals("row", rowBindings, test.getRowLabelBindings()); Map<String, Integer> colBindings = Maps.newHashMap(); colBindings.put("Foo", 0); colBindings.put("Bar", 1); colBindings.put("Baz", 2); test.setColumnLabelBindings(colBindings); assertEquals("col", colBindings, test.getColumnLabelBindings()); }
@Test public void testLabelBindingSerialization() { 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 = Maps.newHashMap(); rowBindings.put("Fee", 0); rowBindings.put("Fie", 1); rowBindings.put("Foe", 2); m.setRowLabelBindings(rowBindings); assertEquals("row", rowBindings, m.getRowLabelBindings()); Map<String, Integer> colBindings = Maps.newHashMap(); colBindings.put("Foo", 0); colBindings.put("Bar", 1); colBindings.put("Baz", 2); m.setColumnLabelBindings(colBindings); assertEquals("col", colBindings, m.getColumnLabelBindings()); } }
@Test(expected = IllegalStateException.class) public void testSettingLabelBindings() { assertNull("row bindings", test.getRowLabelBindings()); assertNull("col bindings", test.getColumnLabelBindings()); test.set("Fee", "Foo", 1, 1, 9); assertNotNull("row", test.getRowLabelBindings()); assertNotNull("row", test.getRowLabelBindings()); assertEquals("Fee", 1, test.getRowLabelBindings().get("Fee").intValue()); assertEquals("Foo", 1, test.getColumnLabelBindings().get("Foo").intValue()); assertEquals("FeeFoo", test.get(1, 1), test.get("Fee", "Foo"), EPSILON); test.get("Fie", "Foe"); }
@Test(expected = IllegalStateException.class) public void testSettingLabelBindings() { 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); assertNotNull("row", m.getRowLabelBindings()); assertNotNull("row", m.getRowLabelBindings()); assertEquals("Fee", 1, m.getRowLabelBindings().get("Fee").intValue()); assertEquals("Fee", 2, m.getColumnLabelBindings().get("Foo").intValue()); assertEquals("FeeFoo", m.get(1, 2), m.get("Fee", "Foo"), EPSILON); m.get("Fie", "Foe"); }
@Test public void testLabelBindings() { 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 = Maps.newHashMap(); rowBindings.put("Fee", 0); rowBindings.put("Fie", 1); rowBindings.put("Foe", 2); m.setRowLabelBindings(rowBindings); assertEquals("row", rowBindings, m.getRowLabelBindings()); Map<String, Integer> colBindings = Maps.newHashMap(); colBindings.put("Foo", 0); colBindings.put("Bar", 1); colBindings.put("Baz", 2); m.setColumnLabelBindings(colBindings); assertEquals("row", rowBindings, m.getRowLabelBindings()); assertEquals("Fee", m.get(0, 1), m.get("Fee", "Bar"), EPSILON); double[] newrow = {9, 8, 7}; m.set("Foe", newrow); assertEquals("FeeBaz", m.get(0, 2), m.get("Fee", "Baz"), EPSILON); }
@Test public void testLabelBindings() { assertNull("row bindings", test.getRowLabelBindings()); assertNull("col bindings", test.getColumnLabelBindings()); Map<String, Integer> rowBindings = Maps.newHashMap(); rowBindings.put("Fee", 0); rowBindings.put("Fie", 1); test.setRowLabelBindings(rowBindings); assertEquals("row", rowBindings, test.getRowLabelBindings()); Map<String, Integer> colBindings = Maps.newHashMap(); colBindings.put("Foo", 0); colBindings.put("Bar", 1); test.setColumnLabelBindings(colBindings); assertEquals("row", rowBindings, test.getRowLabelBindings()); assertEquals("Fee", test.get(0, 1), test.get("Fee", "Bar"), EPSILON); double[] newrow = {9, 8}; test.set("Fie", newrow); assertEquals("FeeBar", test.get(0, 1), test.get("Fee", "Bar"), EPSILON); }
private static void doTestMatrixWritableEquals(Matrix m) throws IOException { Writable matrixWritable = new MatrixWritable(m); MatrixWritable matrixWritable2 = new MatrixWritable(); writeAndRead(matrixWritable, matrixWritable2); Matrix m2 = matrixWritable2.get(); compareMatrices(m, m2); doCheckBindings(m2.getRowLabelBindings()); doCheckBindings(m2.getColumnLabelBindings()); }