use of gdsc.smlm.function.gaussian.Gaussian2DFunction in project GDSC-SMLM by aherbert.
the class PoissonLikelihoodWrapperTest method functionComputesGradient.
private void functionComputesGradient(int flags) {
Gaussian2DFunction f1 = GaussianFunctionFactory.create2D(1, maxx, maxx, flags, null);
// Setup
testbackground = testbackground_;
testamplitude1 = testamplitude1_;
testangle1 = testangle1_;
testcx1 = testcx1_;
testcy1 = testcy1_;
testw1 = testw1_;
if (!f1.evaluatesBackground()) {
testbackground = new double[] { testbackground[0] };
}
if (!f1.evaluatesSignal()) {
testamplitude1 = new double[] { testamplitude1[0] };
}
if (!f1.evaluatesShape()) {
testangle1 = new double[] { 0 };
}
// Position is always evaluated
boolean noSecondWidth = false;
if (!f1.evaluatesSD0()) {
// Just use 1 width
testw1 = new double[][] { testw1[0] };
// If no width 0 then assume we have no width 1 as well
noSecondWidth = true;
} else if (!f1.evaluatesSD1()) {
// No evaluation of second width needs only variation in width 0 so truncate
testw1 = Arrays.copyOf(testw1, 2);
noSecondWidth = true;
}
if (noSecondWidth) {
for (int i = 0; i < testw1.length; i++) {
testw1[i][1] = testw1[i][0];
}
}
double fraction = 90;
if (f1.evaluatesBackground())
functionComputesTargetGradient(f1, Gaussian2DFunction.BACKGROUND, fraction);
if (f1.evaluatesSignal())
functionComputesTargetGradient(f1, Gaussian2DFunction.SIGNAL, fraction);
if (f1.evaluatesShape())
functionComputesTargetGradient(f1, Gaussian2DFunction.SHAPE, fraction);
functionComputesTargetGradient(f1, Gaussian2DFunction.X_POSITION, fraction);
functionComputesTargetGradient(f1, Gaussian2DFunction.Y_POSITION, fraction);
if (f1.evaluatesSD0())
functionComputesTargetGradient(f1, Gaussian2DFunction.X_SD, fraction);
if (f1.evaluatesSD1())
functionComputesTargetGradient(f1, Gaussian2DFunction.Y_SD, fraction);
}
use of gdsc.smlm.function.gaussian.Gaussian2DFunction in project GDSC-SMLM by aherbert.
the class SpeedTest method f1FasterThanf2.
void f1FasterThanf2(int npeaks, int flags1, int flags2) {
org.junit.Assume.assumeTrue(TestSettings.RUN_SPEED_TESTS);
ArrayList<double[]> paramsList2 = (npeaks == 1) ? paramsListSinglePeak : paramsListDoublePeak;
// Use the full list of parameters to build the functions
Gaussian2DFunction f1 = GaussianFunctionFactory.create2D(npeaks, blockWidth, blockWidth, flags1, null);
Gaussian2DFunction f2 = GaussianFunctionFactory.create2D(npeaks, blockWidth, blockWidth, flags2, null);
double[] dyda = new double[1 + npeaks * 6];
for (int i = 0; i < paramsList2.size(); i++) {
f1.initialise(paramsList2.get(i));
for (int j = 0; j < x.length; j++) f1.eval(x[j], dyda);
}
long start1 = System.nanoTime();
for (int i = 0; i < paramsList2.size(); i++) {
f1.initialise(paramsList2.get(i));
for (int j = 0; j < x.length; j++) f1.eval(x[j], dyda);
}
start1 = System.nanoTime() - start1;
for (int i = 0; i < paramsList2.size(); i++) {
f2.initialise(paramsList2.get(i));
for (int j = 0; j < x.length; j++) f2.eval(x[j], dyda);
}
long start2 = System.nanoTime();
for (int i = 0; i < paramsList2.size(); i++) {
f2.initialise(paramsList2.get(i));
for (int j = 0; j < x.length; j++) f2.eval(x[j], dyda);
}
start2 = System.nanoTime() - start2;
log("%s = %d : %s = %d : %fx\n", f1.getClass().getName(), start1, f2.getClass().getName(), start2, (1.0 * start2) / start1);
if (TestSettings.ASSERT_SPEED_TESTS)
Assert.assertTrue(start2 > start1);
}
use of gdsc.smlm.function.gaussian.Gaussian2DFunction in project GDSC-SMLM by aherbert.
the class Gaussian2DFunctionTest method functionComputesTargetWithAndWithoutGradientWith2Peaks.
@Test
public void functionComputesTargetWithAndWithoutGradientWith2Peaks() {
if (f2 == null)
return;
double[] dyda = new double[f2.gradientIndices().length];
double[] a;
boolean record = true;
for (double background : testbackground) // Peak 1
for (double amplitude1 : testamplitude1) for (double shape1 : testshape1) for (double cx1 : testcx1) for (double cy1 : testcy1) for (double[] w1 : testw1) // Peak 2
for (double amplitude2 : testamplitude2) for (double shape2 : testshape2) for (double cx2 : testcx2) for (double cy2 : testcy2) for (double[] w2 : testw2) {
a = createParameters(background, amplitude1, shape1, cx1, cy1, w1[0], w1[1], amplitude2, shape2, cx2, cy2, w2[0], w2[1]);
f2.initialise(a);
// Test the frozen version
int flags = GaussianFunctionFactory.freeze(2, this.flags, zModel, a);
Gaussian2DFunction f = GaussianFunctionFactory.create2D(2, f2.getMaxX(), f2.getMaxY(), flags, zModel);
f.initialise(a);
if (record) {
record = false;
log("%s %d frozen to %s\n", f2.getClass().getSimpleName(), 2, f.getClass().getSimpleName());
}
for (int x : testx) for (int y : testy) {
int xx = y * maxx + x;
double y1 = f2.eval(xx, dyda);
double y2 = f2.eval(xx);
Assert.assertTrue(y1 + " != " + y2, eq2.almostEqualRelativeOrAbsolute(y1, y2));
y2 = f.eval(xx);
Assert.assertTrue(y1 + " != frozen " + y2, eq2.almostEqualRelativeOrAbsolute(y1, y2));
}
}
}
use of gdsc.smlm.function.gaussian.Gaussian2DFunction in project GDSC-SMLM by aherbert.
the class Gaussian2DFunctionTest method functionComputesTargetGradientWith2Peaks.
private void functionComputesTargetGradientWith2Peaks(int targetParameter) {
int gradientIndex = findGradientIndex(f2, targetParameter);
double[] dyda = new double[f2.gradientIndices().length];
double[] dyda2 = new double[dyda.length];
double[] a;
Gaussian2DFunction f2a = GaussianFunctionFactory.create2D(2, maxx, maxy, flags, zModel);
Gaussian2DFunction f2b = GaussianFunctionFactory.create2D(2, maxx, maxy, flags, zModel);
Statistics s = new Statistics();
for (double background : testbackground) // Peak 1
for (double amplitude1 : testamplitude1) for (double shape1 : testshape1) for (double cx1 : testcx1) for (double cy1 : testcy1) for (double[] w1 : testw1) // Peak 2
for (double amplitude2 : testamplitude2) for (double shape2 : testshape2) for (double cx2 : testcx2) for (double cy2 : testcy2) for (double[] w2 : testw2) {
a = createParameters(background, amplitude1, shape1, cx1, cy1, w1[0], w1[1], amplitude2, shape2, cx2, cy2, w2[0], w2[1]);
f2.initialise(a);
// Numerically solve gradient.
// Calculate the step size h to be an exact numerical representation
final double xx = a[targetParameter];
// Get h to minimise roundoff error
double h = Precision.representableDelta(xx, h_);
// Evaluate at (x+h) and (x-h)
a = createParameters(background, amplitude1, shape1, cx1, cy1, w1[0], w1[1], amplitude2, shape2, cx2, cy2, w2[0], w2[1]);
a[targetParameter] = xx + h;
f2a.initialise(a);
a = createParameters(background, amplitude1, shape1, cx1, cy1, w1[0], w1[1], amplitude2, shape2, cx2, cy2, w2[0], w2[1]);
a[targetParameter] = xx - h;
f2b.initialise(a);
for (int x : testx) for (int y : testy) {
int i = y * maxx + x;
f2.eval(i, dyda);
double value2 = f2a.eval(i, dyda2);
double value3 = f2b.eval(i, dyda2);
double gradient = (value2 - value3) / (2 * h);
double error = DoubleEquality.relativeError(gradient, dyda2[gradientIndex]);
s.add(error);
Assert.assertTrue(gradient + " sign != " + dyda2[gradientIndex], (gradient * dyda2[gradientIndex]) >= 0);
//System.out.printf("[%d,%d] %f == [%d] %f? (%g)\n", x, y, gradient,
// gradientIndex, dyda2[gradientIndex], error);
//System.out.printf("[%d,%d] %f == [%d] %f?\n", x, y, gradient, gradientIndex, dyda[gradientIndex]);
Assert.assertTrue(gradient + " != " + dyda[gradientIndex], eq.almostEqualRelativeOrAbsolute(gradient, dyda[gradientIndex]));
}
}
System.out.printf("functionComputesTargetGradientWith2Peaks %s [%d] %s (error %s +/- %s)\n", f2.getClass().getSimpleName(), Gaussian2DFunction.getPeak(targetParameter), f2.getName(targetParameter), Utils.rounded(s.getMean()), Utils.rounded(s.getStandardDeviation()));
}
use of gdsc.smlm.function.gaussian.Gaussian2DFunction in project GDSC-SMLM by aherbert.
the class Gaussian2DFunctionTest method factoryCreatesCorrectFunction.
@Test
public void factoryCreatesCorrectFunction() {
Gaussian2DFunction f;
if (f2 != null) {
f = GaussianFunctionFactory.create2D(2, maxx, maxy, flags, zModel);
Assert.assertTrue("Incorrect function2", f.getClass() == f2.getClass());
} else {
f = GaussianFunctionFactory.create2D(1, maxx, maxy, flags, zModel);
Assert.assertTrue("Incorrect function1", f.getClass() == f1.getClass());
}
}
Aggregations