use of uk.ac.sussex.gdsc.smlm.function.Gradient2Procedure in project GDSC-SMLM by aherbert.
the class ErfGaussian2DFunctionTest method functionComputesGradientForEachWith2Peaks.
@Test
void functionComputesGradientForEachWith2Peaks() {
Assumptions.assumeTrue(null != f2);
final ErfGaussian2DFunction f2 = (ErfGaussian2DFunction) this.f2;
final int n = f2.size();
final double[] du_da = new double[f2.getNumberOfGradients()];
final double[] du_db = new double[f2.getNumberOfGradients()];
final double[] d2u_da2 = new double[f2.getNumberOfGradients()];
final double[] values = new double[n];
final double[][] jacobian = new double[n][];
final double[][] jacobian2 = new double[n][];
for (final double background : testbackground) {
// Peak 1
for (final double signal1 : testsignal1) {
for (final double cx1 : testcx1) {
for (final double cy1 : testcy1) {
for (final double cz1 : testcz1) {
for (final double[] w1 : testw1) {
for (final double angle1 : testangle1) {
// Peak 2
for (final double signal2 : testsignal2) {
for (final double cx2 : testcx2) {
for (final double cy2 : testcy2) {
for (final double cz2 : testcz2) {
for (final double[] w2 : testw2) {
for (final double angle2 : testangle2) {
final double[] a = createParameters(background, signal1, cx1, cy1, cz1, w1[0], w1[1], angle1, signal2, cx2, cy2, cz2, w2[0], w2[1], angle2);
f2.initialiseExtended2(a);
// Compute single
for (int i = 0; i < n; i++) {
final double o1 = f2.eval(i, du_da);
final double o2 = f2.eval2(i, du_db, d2u_da2);
Assertions.assertEquals(o1, o2, 1e-10, "Value");
Assertions.assertArrayEquals(du_da, du_db, 1e-10, "Jacobian!=Jacobian");
values[i] = o1;
jacobian[i] = du_da.clone();
jacobian2[i] = d2u_da2.clone();
}
// Use procedures
f2.forEach(new ValueProcedure() {
int index = 0;
@Override
public void execute(double value) {
Assertions.assertEquals(values[index], value, 1e-10, "Value ValueProcedure");
index++;
}
});
f2.forEach(new Gradient1Procedure() {
int index = 0;
@Override
public void execute(double value, double[] dyDa) {
Assertions.assertEquals(values[index], value, 1e-10, "Value Gradient1Procedure");
Assertions.assertArrayEquals(jacobian[index], dyDa, 1e-10, "du_da Gradient1Procedure");
index++;
}
});
f2.forEach(new Gradient2Procedure() {
int index = 0;
@Override
public void execute(double value, double[] dyDa, double[] d2yDa2) {
Assertions.assertEquals(values[index], value, 1e-10, "Value Gradient2Procedure");
Assertions.assertArrayEquals(jacobian[index], dyDa, 1e-10, "du_da Gradient2Procedure");
Assertions.assertArrayEquals(jacobian2[index], d2yDa2, 1e-10, "d2u_da2 Gradient2Procedure");
index++;
}
});
f2.forEach(new ExtendedGradient2Procedure() {
int index = 0;
@Override
public void executeExtended(double value, double[] dyDa, double[] d2yDaDb) {
Assertions.assertEquals(values[index], value, 1e-10, "Value ExtendedGradient2Procedure");
Assertions.assertArrayEquals(jacobian[index], dyDa, 1e-10, "du_da ExtendedGradient2Procedure");
for (int j = 0, k = 0; j < d2u_da2.length; j++, k += d2u_da2.length + 1) {
d2u_da2[j] = d2yDaDb[k];
}
Assertions.assertArrayEquals(jacobian2[index], d2u_da2, 1e-10, "d2u_da2 Gradient2Procedure");
index++;
}
});
}
}
}
}
}
}
}
}
}
}
}
}
}
}
use of uk.ac.sussex.gdsc.smlm.function.Gradient2Procedure in project GDSC-SMLM by aherbert.
the class ErfGaussian2DFunctionTest method functionComputesGradientForEach.
@Test
void functionComputesGradientForEach() {
final ErfGaussian2DFunction f1 = (ErfGaussian2DFunction) this.f1;
final int n = f1.size();
final double[] du_da = new double[f1.getNumberOfGradients()];
final double[] du_db = new double[f1.getNumberOfGradients()];
final double[] d2u_da2 = new double[f1.getNumberOfGradients()];
final double[] values = new double[n];
final double[][] jacobian = new double[n][];
final double[][] jacobian2 = new double[n][];
for (final double background : testbackground) {
// Peak 1
for (final double signal1 : testsignal1) {
for (final double cx1 : testcx1) {
for (final double cy1 : testcy1) {
for (final double cz1 : testcz1) {
for (final double[] w1 : testw1) {
for (final double angle1 : testangle1) {
final double[] a = createParameters(background, signal1, cx1, cy1, cz1, w1[0], w1[1], angle1);
f1.initialiseExtended2(a);
// Compute single
for (int i = 0; i < n; i++) {
final double o1 = f1.eval(i, du_da);
final double o2 = f1.eval2(i, du_db, d2u_da2);
Assertions.assertEquals(o1, o2, 1e-10, "Value");
Assertions.assertArrayEquals(du_da, du_db, 1e-10, "Jacobian!=Jacobian");
values[i] = o1;
jacobian[i] = du_da.clone();
jacobian2[i] = d2u_da2.clone();
}
// Use procedures
f1.forEach(new ValueProcedure() {
int index = 0;
@Override
public void execute(double value) {
Assertions.assertEquals(values[index], value, 1e-10, "Value ValueProcedure");
index++;
}
});
f1.forEach(new Gradient1Procedure() {
int index = 0;
@Override
public void execute(double value, double[] dyDa) {
Assertions.assertEquals(values[index], value, 1e-10, "Value Gradient1Procedure");
Assertions.assertArrayEquals(jacobian[index], dyDa, 1e-10, "du_da Gradient1Procedure");
index++;
}
});
f1.forEach(new Gradient2Procedure() {
int index = 0;
@Override
public void execute(double value, double[] dyDa, double[] d2yDa2) {
Assertions.assertEquals(values[index], value, 1e-10, "Value Gradient2Procedure");
Assertions.assertArrayEquals(jacobian[index], dyDa, 1e-10, "du_da Gradient2Procedure");
Assertions.assertArrayEquals(jacobian2[index], d2yDa2, 1e-10, "d2u_da2 Gradient2Procedure");
index++;
}
});
f1.forEach(new ExtendedGradient2Procedure() {
int index = 0;
@Override
public void executeExtended(double value, double[] dyDa, double[] d2yDaDb) {
Assertions.assertEquals(values[index], value, 1e-10, "Value ExtendedGradient2Procedure");
Assertions.assertArrayEquals(jacobian[index], dyDa, 1e-10, "du_da ExtendedGradient2Procedure");
for (int j = 0, k = 0; j < d2u_da2.length; j++, k += d2u_da2.length + 1) {
d2u_da2[j] = d2yDaDb[k];
}
Assertions.assertArrayEquals(jacobian2[index], d2u_da2, 1e-10, "d2u_da2 Gradient2Procedure");
index++;
}
});
}
}
}
}
}
}
}
}
Aggregations