Search in sources :

Example 1 with FbmDiffusionFunction

use of uk.ac.sussex.gdsc.smlm.ij.plugins.TrackPopulationAnalysis.FbmDiffusionFunction in project GDSC-SMLM by aherbert.

the class TrackPopulationAnalysisTest method canComputeFbmDiffusionFunction.

@Test
void canComputeFbmDiffusionFunction() {
    final int size = 10;
    final double delta = 1e-6;
    final DoubleDoubleBiPredicate test = TestHelper.doublesAreClose(1e-5);
    for (final double t : new double[] { 0.8, 1, 1.2 }) {
        final MultivariateJacobianFunction f = new FbmDiffusionFunction(size, t);
        for (final double d : new double[] { 0.8, 0.9, 1, 1.1, 1.2 }) {
            for (final double s : new double[] { 2, 20 }) {
                for (final double alpha : new double[] { 1e-2, 0.8, 0.9, 1, 1.1, 1.2, 2.0 }) {
                    // Check the value and Jacobian
                    final RealVector point = new ArrayRealVector(new double[] { d, s, alpha }, false);
                    final Pair<RealVector, RealMatrix> p = f.value(point);
                    final double[] value = p.getFirst().toArray();
                    Assertions.assertEquals(size, value.length);
                    final double a1 = alpha + 1;
                    final double a2 = alpha + 2;
                    final double ta = Math.pow(t, alpha);
                    for (int n = 1; n <= size; n++) {
                        // MSD = [4Dt^a / (a+2)(a+1)] * [(n+1)^(a+2) + (n-1)^(a+2) - 2n^(a+2)]
                        // - [8Dt^a / (a+2)(a+1)] + 4s^2
                        // assume t=1.
                        final double msd = (4 * d * ta / (a2 * a1)) * (Math.pow(n + 1, a2) + Math.pow(n - 1, a2) - 2 * Math.pow(n, a2)) - 8 * d * ta / (a2 * a1) + 4 * s * s;
                        TestAssertions.assertTest(msd, value[n - 1], test, "value");
                    }
                    // Columns of the Jacobian
                    final double[] dfda1 = p.getSecond().getColumn(0);
                    final double[] dfdb1 = p.getSecond().getColumn(1);
                    final double[] dfdc1 = p.getSecond().getColumn(2);
                    point.setEntry(0, d - delta);
                    RealVector v1 = f.value(point).getFirst();
                    point.setEntry(0, d + delta);
                    RealVector v2 = f.value(point).getFirst();
                    final double[] dfda = v2.subtract(v1).mapDivide(2 * delta).toArray();
                    point.setEntry(0, d);
                    point.setEntry(1, s - delta);
                    v1 = f.value(point).getFirst();
                    point.setEntry(1, s + delta);
                    v2 = f.value(point).getFirst();
                    final double[] dfdb = v2.subtract(v1).mapDivide(2 * delta).toArray();
                    point.setEntry(1, s);
                    point.setEntry(2, alpha - delta);
                    v1 = f.value(point).getFirst();
                    point.setEntry(2, alpha + delta);
                    v2 = f.value(point).getFirst();
                    final double[] dfdc = v2.subtract(v1).mapDivide(2 * delta).toArray();
                    // Element-by-element relative error
                    TestAssertions.assertArrayTest(dfda, dfda1, test, "jacobian dfda");
                    TestAssertions.assertArrayTest(dfdb, dfdb1, test, "jacobian dfdb");
                    TestAssertions.assertArrayTest(dfdc, dfdc1, test, "jacobian dfdc");
                }
            }
        }
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) RealMatrix(org.apache.commons.math3.linear.RealMatrix) RealVector(org.apache.commons.math3.linear.RealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) FbmDiffusionFunction(uk.ac.sussex.gdsc.smlm.ij.plugins.TrackPopulationAnalysis.FbmDiffusionFunction) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) MultivariateJacobianFunction(org.apache.commons.math3.fitting.leastsquares.MultivariateJacobianFunction) Test(org.junit.jupiter.api.Test)

Example 2 with FbmDiffusionFunction

use of uk.ac.sussex.gdsc.smlm.ij.plugins.TrackPopulationAnalysis.FbmDiffusionFunction in project GDSC-SMLM by aherbert.

the class TrackPopulationAnalysisTest method canComputeBrownianModelUsingFbmFunction.

@Test
void canComputeBrownianModelUsingFbmFunction() {
    final int size = 10;
    final DoubleDoubleBiPredicate test = TestHelper.doublesAreClose(1e-5);
    final double alpha = 1.0;
    for (final double t : new double[] { 0.8, 1, 1.2 }) {
        final BrownianDiffusionFunction f1 = new BrownianDiffusionFunction(size, t);
        final FbmDiffusionFunction f2 = new FbmDiffusionFunction(size, t);
        for (final double d : new double[] { 0.8, 0.9, 1, 1.1, 1.2 }) {
            for (final double s : new double[] { 2, 20 }) {
                // Check the value and Jacobian
                final RealVector point1 = new ArrayRealVector(new double[] { d, s, alpha }, false);
                final Pair<RealVector, RealMatrix> p1 = f1.value(point1);
                final double[] value1 = p1.getFirst().toArray();
                final RealVector point2 = new ArrayRealVector(new double[] { d, s, alpha }, false);
                final Pair<RealVector, RealMatrix> p2 = f2.value(point2);
                final double[] value2 = p2.getFirst().toArray();
                TestAssertions.assertArrayTest(value1, value2, test, "value");
                final double[] dfda1 = p1.getSecond().getColumn(0);
                final double[] dfdb1 = p1.getSecond().getColumn(1);
                final double[] dfda2 = p2.getSecond().getColumn(0);
                final double[] dfdb2 = p2.getSecond().getColumn(1);
                TestAssertions.assertArrayTest(dfda1, dfda2, test, "jacobian dfda");
                TestAssertions.assertArrayTest(dfdb1, dfdb2, test, "jacobian dfdb");
            }
        }
    }
}
Also used : BrownianDiffusionFunction(uk.ac.sussex.gdsc.smlm.ij.plugins.TrackPopulationAnalysis.BrownianDiffusionFunction) DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) RealMatrix(org.apache.commons.math3.linear.RealMatrix) RealVector(org.apache.commons.math3.linear.RealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) FbmDiffusionFunction(uk.ac.sussex.gdsc.smlm.ij.plugins.TrackPopulationAnalysis.FbmDiffusionFunction) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) Test(org.junit.jupiter.api.Test)

Aggregations

ArrayRealVector (org.apache.commons.math3.linear.ArrayRealVector)2 RealMatrix (org.apache.commons.math3.linear.RealMatrix)2 RealVector (org.apache.commons.math3.linear.RealVector)2 Test (org.junit.jupiter.api.Test)2 FbmDiffusionFunction (uk.ac.sussex.gdsc.smlm.ij.plugins.TrackPopulationAnalysis.FbmDiffusionFunction)2 DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)2 MultivariateJacobianFunction (org.apache.commons.math3.fitting.leastsquares.MultivariateJacobianFunction)1 BrownianDiffusionFunction (uk.ac.sussex.gdsc.smlm.ij.plugins.TrackPopulationAnalysis.BrownianDiffusionFunction)1