From 8fea25daa8f1f1cc564497ddf4e3c9e15a64596c Mon Sep 17 00:00:00 2001 From: gmungoc Date: Wed, 18 Jan 2017 09:14:12 +0000 Subject: [PATCH] JAL-2379 more realistic matrices for unit tests --- test/jalview/math/MatrixTest.java | 102 +++++++++++++++++++++++++++++++ test/jalview/math/SparseMatrixTest.java | 57 ++++++++++++++--- 2 files changed, 149 insertions(+), 10 deletions(-) diff --git a/test/jalview/math/MatrixTest.java b/test/jalview/math/MatrixTest.java index 1500dc6..bd5614f 100644 --- a/test/jalview/math/MatrixTest.java +++ b/test/jalview/math/MatrixTest.java @@ -4,11 +4,15 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.fail; import java.util.Arrays; +import java.util.Random; import org.testng.annotations.Test; +import org.testng.internal.junit.ArrayAsserts; public class MatrixTest { + final static double DELTA = 0.0001d; + @Test(groups = "Timing") public void testPreMultiply_timing() { @@ -255,4 +259,102 @@ public class MatrixTest assertEquals(Matrix.sign(1, 2), 1d); assertEquals(Matrix.sign(1, 0), 1d); } + + /** + * Helper method to make values for a sparse, pseudo-random symmetric matrix + * + * @param rows + * @param cols + * @param occupancy + * one in 'occupancy' entries will be non-zero + * @return + */ + public double[][] getSparseValues(int rows, int cols, int occupancy) + { + Random r = new Random(1729); + + /* + * generate whole number values between -12 and +12 + * (to mimic score matrices used in Jalview) + */ + double[][] d = new double[rows][cols]; + int m = 0; + for (int i = 0; i < rows; i++) + { + if (++m % occupancy == 0) + { + d[i][i] = r.nextInt() % 13; // diagonal + } + for (int j = 0; j < i; j++) + { + if (++m % occupancy == 0) + { + d[i][j] = r.nextInt() % 13; + d[j][i] = d[i][j]; + } + } + } + return d; + + } + + /** + * Verify that the results of method tred() are the same if the calculation is + * redone + */ + @Test(groups = "Functional") + public void testTred_reproducible() + { + /* + * make a pseudo-random symmetric matrix as required for tred/tqli + */ + int rows = 10; + int cols = rows; + double[][] d = getSparseValues(rows, cols, 3); + + /* + * make a copy of the values so m1, m2 are not + * sharing arrays! + */ + double[][] d1 = new double[rows][cols]; + for (int row = 0; row < rows; row++) + { + for (int col = 0; col < cols; col++) + { + d1[row][col] = d[row][col]; + } + } + Matrix m1 = new Matrix(d); + Matrix m2 = new Matrix(d1); + assertMatricesMatch(m1, m2); // sanity check + m1.tred(); + m2.tred(); + assertMatricesMatch(m1, m2); + } + + private void assertMatricesMatch(MatrixI m1, MatrixI m2) + { + if (m1.height() != m2.height()) + { + fail("height mismatch"); + } + if (m1.width() != m2.width()) + { + fail("width mismatch"); + } + for (int row = 0; row < m1.height(); row++) + { + for (int col = 0; col < m1.width(); col++) + { + double v2 = m2.getValue(row, col); + double v1 = m1.getValue(row, col); + if (Math.abs(v1 - v2) > DELTA) + { + fail(String.format("At [%d, %d] %f != %f", row, col, v1, v2)); + } + } + } + ArrayAsserts.assertArrayEquals(m1.getD(), m2.getD(), 0.00001d); + ArrayAsserts.assertArrayEquals(m1.getE(), m2.getE(), 0.00001d); + } } diff --git a/test/jalview/math/SparseMatrixTest.java b/test/jalview/math/SparseMatrixTest.java index 607d415..5e32e18 100644 --- a/test/jalview/math/SparseMatrixTest.java +++ b/test/jalview/math/SparseMatrixTest.java @@ -200,10 +200,9 @@ public class SparseMatrixTest { /* * make a pseudo-random symmetric matrix as required for tred/tqli - * note: test fails for matrices larger than 6x6 due to double value - * rounding only (random values result in very small values) */ - int rows = 6; + // TODO why does this fail for rows > 9?? + int rows = 9; int cols = rows; double[][] d = getSparseValues(rows, cols, 3); @@ -316,25 +315,29 @@ public class SparseMatrixTest * * @param rows * @param cols - * @param fraction - * one n fraction entries will be non-zero + * @param occupancy + * one in 'occupancy' entries will be non-zero * @return */ - public double[][] getSparseValues(int rows, int cols, int fraction) + public double[][] getSparseValues(int rows, int cols, int occupancy) { + /* + * generate whole number values between -12 and +12 + * (to mimic score matrices used in Jalview) + */ double[][] d = new double[rows][cols]; int m = 0; for (int i = 0; i < rows; i++) { - if (++m % fraction == 0) + if (++m % occupancy == 0) { - d[i][i] = r.nextDouble(); // diagonal + d[i][i] = r.nextInt() % 13; // diagonal } for (int j = 0; j < i; j++) { - if (++m % fraction == 0) + if (++m % occupancy == 0) { - d[i][j] = r.nextDouble(); + d[i][j] = r.nextInt() % 13; d[j][i] = d[i][j]; } } @@ -377,4 +380,38 @@ public class SparseMatrixTest { 0, 6, 0, 0, 0 } }); assertEquals(m1.getFillRatio(), 0.4f); } + + /** + * Verify that the results of method tred() are the same if the calculation is + * redone + */ + @Test(groups = "Functional") + public void testTred_reproducible() + { + /* + * make a pseudo-random symmetric matrix as required for tred/tqli + */ + int rows = 10; + int cols = rows; + double[][] d = getSparseValues(rows, cols, 3); + + /* + * make a copy of the values so m1, m2 are not + * sharing arrays! + */ + double[][] d1 = new double[rows][cols]; + for (int row = 0; row < rows; row++) + { + for (int col = 0; col < cols; col++) + { + d1[row][col] = d[row][col]; + } + } + Matrix m1 = new SparseMatrix(d); + Matrix m2 = new SparseMatrix(d1); + assertMatricesMatch(m1, m2); // sanity check + m1.tred(); + m2.tred(); + assertMatricesMatch(m1, m2); + } } \ No newline at end of file -- 1.7.10.2