Search in sources :

Example 61 with DoubleDoubleBiPredicate

use of uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate in project GDSC-SMLM by aherbert.

the class TensorTest method canComputeSameTensor.

@SeededTest
void canComputeSameTensor(RandomSeed seed) {
    final UniformRandomProvider random = RngUtils.create(seed.getSeed());
    final int w = 3;
    final int h = 4;
    final float[] data = new float[w * h];
    final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-6, 0);
    for (int i = 0; i < 10; i++) {
        for (int j = data.length; j-- > 0; ) {
            data[j] = random.nextFloat();
        }
        final Tensor2D t2 = new Tensor2D(data, w, h);
        final Tensor3D t3 = new Tensor3D(new float[][] { data }, w, h);
        final double[] com2 = t2.getCentreOfMass();
        final double[] v2 = t2.getEigenValues();
        final double[][] vv2 = t2.getEigenVectors();
        final double[] com3 = t3.getCentreOfMass();
        final double[] v3 = t3.getEigenValues();
        final double[][] vv3 = t3.getEigenVectors();
        for (int k = 0; k < 2; k++) {
            Assertions.assertEquals(com2[k], com3[k]);
            TestAssertions.assertTest(v2[k], v3[k + 1], predicate);
            for (int kk = 0; kk < 2; kk++) {
                // Swap vector direction
                if (Math.signum(vv2[k][kk]) != Math.signum(vv3[k + 1][kk])) {
                    vv2[k][0] = -vv2[k][0];
                    vv2[k][1] = -vv2[k][1];
                }
                TestAssertions.assertTest(vv2[k][kk], vv3[k + 1][kk], predicate);
            }
        }
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 62 with DoubleDoubleBiPredicate

use of uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate in project GDSC-SMLM by aherbert.

the class GaussianKernelTest method canGetConversionFactor.

@Test
void canGetConversionFactor() {
    final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-10, 0);
    for (int i = 0; i < 5; i++) {
        final double s = 0.33 * (1 << i);
        final double norm = 1.0 / (Math.sqrt(2 * Math.PI) * s);
        final double var2 = 2 * s * s;
        final GaussianKernel k = new GaussianKernel(s);
        final double[] o = k.getGaussianKernel(1, 5, false);
        final double f = k.getConversionFactor(o);
        for (int u = o.length / 2, x = u; x >= 0; x--) {
            final double e = norm * StdMath.exp(-(x - u) * (x - u) / var2);
            TestAssertions.assertTest(e, f * o[x], predicate);
        }
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) Test(org.junit.jupiter.api.Test)

Example 63 with DoubleDoubleBiPredicate

use of uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate in project GDSC-SMLM by aherbert.

the class GaussianKernelTest method canComputeDownscaleGaussianKernelIncScaleIncRange.

@Test
void canComputeDownscaleGaussianKernelIncScaleIncRange() {
    final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-10, 0);
    for (int i = 0; i < 5; i++) {
        final double s = 0.33 * (1 << i);
        final GaussianKernel k = new GaussianKernel(s);
        for (int scale = 1; scale <= 5; scale++) {
            for (int range = 3; range < 5; range++) {
                for (final boolean edgeCorrection : new boolean[] { true, false }) {
                    final double[] e = GaussianKernel.makeGaussianKernel(s / scale, range, edgeCorrection);
                    final double[] o = k.getDownscaleGaussianKernel(scale, range, edgeCorrection);
                    if (MathUtils.isPow2(scale)) {
                        Assertions.assertArrayEquals(e, o);
                    } else {
                        TestAssertions.assertArrayTest(e, o, predicate);
                    }
                }
            }
        }
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) Test(org.junit.jupiter.api.Test)

Aggregations

DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)63 SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)27 Test (org.junit.jupiter.api.Test)22 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)12 PoissonDistribution (org.apache.commons.math3.distribution.PoissonDistribution)6 TDoubleArrayList (gnu.trove.list.array.TDoubleArrayList)4 ArrayList (java.util.ArrayList)4 MixtureMultivariateGaussianDistribution (uk.ac.sussex.gdsc.smlm.math3.distribution.fitting.MultivariateGaussianMixtureExpectationMaximization.MixtureMultivariateGaussianDistribution)4 MultivariateGaussianDistribution (uk.ac.sussex.gdsc.smlm.math3.distribution.fitting.MultivariateGaussianMixtureExpectationMaximization.MixtureMultivariateGaussianDistribution.MultivariateGaussianDistribution)4 UnivariateFunction (org.apache.commons.math3.analysis.UnivariateFunction)3 SimpsonIntegrator (org.apache.commons.math3.analysis.integration.SimpsonIntegrator)3 ArrayRealVector (org.apache.commons.math3.linear.ArrayRealVector)3 RealMatrix (org.apache.commons.math3.linear.RealMatrix)3 RealVector (org.apache.commons.math3.linear.RealVector)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 BigDecimal (java.math.BigDecimal)2 MathContext (java.math.MathContext)2 MixtureMultivariateNormalDistribution (org.apache.commons.math3.distribution.MixtureMultivariateNormalDistribution)2 MultivariateNormalDistribution (org.apache.commons.math3.distribution.MultivariateNormalDistribution)2 MultivariateJacobianFunction (org.apache.commons.math3.fitting.leastsquares.MultivariateJacobianFunction)2