use of gdsc.smlm.function.gaussian.SingleFreeCircularGaussian2DFunction in project GDSC-SMLM by aherbert.
the class GradientCalculatorSpeedTest method gradientCalculatorAssumedXIsFasterThanGradientCalculator.
@Test
public void gradientCalculatorAssumedXIsFasterThanGradientCalculator() {
org.junit.Assume.assumeTrue(speedTests || TestSettings.RUN_SPEED_TESTS);
int iter = 10000;
rdg = new RandomDataGenerator(new Well19937c(30051977));
double[][] alpha = new double[7][7];
double[] beta = new double[7];
ArrayList<double[]> paramsList = new ArrayList<double[]>(iter);
ArrayList<double[]> yList = new ArrayList<double[]>(iter);
int[] x = createData(1, iter, paramsList, yList);
GradientCalculator calc = new GradientCalculator6();
GradientCalculator calc2 = new GradientCalculator6();
SingleFreeCircularGaussian2DFunction func = new SingleFreeCircularGaussian2DFunction(blockWidth, blockWidth);
int n = x.length;
for (int i = 0; i < paramsList.size(); i++) calc.findLinearised(x, yList.get(i), paramsList.get(i), alpha, beta, func);
for (int i = 0; i < paramsList.size(); i++) calc2.findLinearised(n, yList.get(i), paramsList.get(i), alpha, beta, func);
long start1 = System.nanoTime();
for (int i = 0; i < paramsList.size(); i++) calc.findLinearised(x, yList.get(i), paramsList.get(i), alpha, beta, func);
start1 = System.nanoTime() - start1;
long start2 = System.nanoTime();
for (int i = 0; i < paramsList.size(); i++) calc2.findLinearised(n, yList.get(i), paramsList.get(i), alpha, beta, func);
start2 = System.nanoTime() - start2;
log("GradientCalculator = %d : GradientCalculatorAssumed = %d : %fx\n", start1, start2, (1.0 * start1) / start2);
if (TestSettings.ASSERT_SPEED_TESTS)
Assert.assertTrue(start2 < start1);
}
use of gdsc.smlm.function.gaussian.SingleFreeCircularGaussian2DFunction in project GDSC-SMLM by aherbert.
the class SingleFreeCircularGaussian2DFunctionTest method init.
protected void init() {
flags = GaussianFunctionFactory.FIT_SIMPLE_FREE_CIRCLE;
f1 = new SingleFreeCircularGaussian2DFunction(maxx, maxx);
}
use of gdsc.smlm.function.gaussian.SingleFreeCircularGaussian2DFunction in project GDSC-SMLM by aherbert.
the class SolverSpeedTest method createData.
private boolean createData(float[][] alpha, float[] beta, boolean positiveDifinite) {
// Generate a 2D Gaussian
SingleFreeCircularGaussian2DFunction func = new SingleFreeCircularGaussian2DFunction(10, 10);
double[] a = new double[] { // Background, Amplitude, Angle, Xpos, Ypos, Xwidth, yWidth
20 + rand.nextDouble() * 5, 10 + rand.nextDouble() * 5, 0, 5 + rand.nextDouble() * 2, 5 + rand.nextDouble() * 2, 5 + rand.nextDouble() * 2, 5 + rand.nextDouble() * 2 };
int[] x = new int[100];
double[] y = new double[100];
func.initialise(a);
for (int i = 0; i < x.length; i++) {
// Add random noise
y[i] = func.eval(i) + ((rand.nextDouble() < 0.5) ? -rand.nextDouble() * 5 : rand.nextDouble() * 5);
}
// Randomise parameters
for (int i = 0; i < a.length; i++) a[i] += (rand.nextDouble() < 0.5) ? -rand.nextDouble() : rand.nextDouble();
// Compute the Hessian and parameter gradient vector
GradientCalculator calc = new GradientCalculator(6);
double[][] alpha2 = new double[6][6];
double[] beta2 = new double[6];
calc.findLinearised(y.length, y, a, alpha2, beta2, func);
// Update the Hessian using a lambda shift
double lambda = 1.001;
for (int i = 0; i < alpha2.length; i++) alpha2[i][i] *= lambda;
// Copy back
for (int i = 0; i < beta.length; i++) {
beta[i] = (float) beta2[i];
for (int j = 0; j < beta.length; j++) {
alpha[i][j] = (float) alpha2[i][j];
}
}
// Check for a positive definite matrix
if (positiveDifinite) {
EJMLLinearSolver solver = new EJMLLinearSolver();
return solver.solveCholeskyLDLT(copydouble(alpha), copydouble(beta));
}
return true;
}
Aggregations