use of uk.ac.sussex.gdsc.test.junit5.RandomSeed in project GDSC-SMLM by aherbert.
the class ImageConverterTest method canGetCropData.
@SeededTest
void canGetCropData(RandomSeed seed) {
final ImageConverterTestData data = (ImageConverterTestData) dataCache.computeIfAbsent(seed, ImageConverterTest::createData);
final byte[] bdata = data.bdata;
final short[] sdata = data.sdata;
final float[] fdata = data.fdata;
final UniformRandomProvider rand = RngUtils.create(seed.getSeed());
final ImageExtractor ie = ImageExtractor.wrap(fdata, w, h);
for (int i = 0; i < 10; i++) {
final Rectangle bounds = ie.getBoxRegionBounds(10 + rand.nextInt(w - 20), 10 + rand.nextInt(h - 20), 5 + rand.nextInt(5));
final FloatProcessor ip = new FloatProcessor(w, h, fdata.clone());
ip.setRoi(bounds);
final float[] fe = (float[]) (ip.crop().getPixels());
Assertions.assertArrayEquals(fe, ImageJImageConverter.getData(bdata, w, h, bounds, null));
Assertions.assertArrayEquals(fe, ImageJImageConverter.getData(sdata, w, h, bounds, null));
Assertions.assertArrayEquals(fe, ImageJImageConverter.getData(fdata, w, h, bounds, null));
// Check the double format
final double[] de = SimpleArrayUtils.toDouble(fe);
Assertions.assertArrayEquals(de, ImageJImageConverter.getDoubleData(bdata, w, h, bounds, null));
Assertions.assertArrayEquals(de, ImageJImageConverter.getDoubleData(sdata, w, h, bounds, null));
Assertions.assertArrayEquals(de, ImageJImageConverter.getDoubleData(fdata, w, h, bounds, null));
}
}
use of uk.ac.sussex.gdsc.test.junit5.RandomSeed in project GDSC-SMLM by aherbert.
the class FastMleJacobianGradient2ProcedureTest method gradientCalculatorComputesGradient.
@Override
@SeededTest
void gradientCalculatorComputesGradient(RandomSeed seed) {
gradientCalculatorComputesGradient(seed, 1, new SingleFreeCircularErfGaussian2DFunction(blockWidth, blockWidth));
gradientCalculatorComputesGradient(seed, 2, new MultiFreeCircularErfGaussian2DFunction(2, blockWidth, blockWidth));
// Use a reasonable z-depth function from the Smith, et al (2010) paper (page 377)
final double sx = 1.08;
final double sy = 1.01;
final double gamma = 0.389;
final double d = 0.531;
final double Ax = -0.0708;
final double Bx = -0.073;
final double Ay = 0.164;
final double By = 0.0417;
final HoltzerAstigmatismZModel zModel = HoltzerAstigmatismZModel.create(sx, sy, gamma, d, Ax, Bx, Ay, By);
gradientCalculatorComputesGradient(seed, 1, new SingleAstigmatismErfGaussian2DFunction(blockWidth, blockWidth, zModel));
}
use of uk.ac.sussex.gdsc.test.junit5.RandomSeed in project GDSC-SMLM by aherbert.
the class LsqLvmGradientProcedureTest method gradientProcedureComputesSameOutputWithBias.
@SeededTest
void gradientProcedureComputesSameOutputWithBias(RandomSeed seed) {
final ErfGaussian2DFunction func = new SingleFreeCircularErfGaussian2DFunction(blockWidth, blockWidth);
final int nparams = func.getNumberOfGradients();
final int iter = 100;
final Level logLevel = Level.FINER;
final boolean debug = logger.isLoggable(logLevel);
final ArrayList<double[]> paramsList = new ArrayList<>(iter);
final ArrayList<double[]> yList = new ArrayList<>(iter);
final ArrayList<double[]> alphaList = new ArrayList<>(iter);
final ArrayList<double[]> betaList = new ArrayList<>(iter);
final ArrayList<double[]> xList = new ArrayList<>(iter);
// Manipulate the background
final double defaultBackground = background;
try {
background = 1e-2;
createData(RngUtils.create(seed.getSeed()), 1, iter, paramsList, yList, true);
final EjmlLinearSolver solver = new EjmlLinearSolver(1e-5, 1e-6);
for (int i = 0; i < paramsList.size(); i++) {
final double[] y = yList.get(i);
final double[] a = paramsList.get(i);
final BaseLsqLvmGradientProcedure p = LsqLvmGradientProcedureUtils.create(y, func);
p.gradient(a);
final double[] beta = p.beta;
alphaList.add(p.getAlphaLinear());
betaList.add(beta.clone());
for (int j = 0; j < nparams; j++) {
if (Math.abs(beta[j]) < 1e-6) {
logger.log(TestLogUtils.getRecord(Level.INFO, "[%d] Tiny beta %s %g", i, func.getGradientParameterName(j), beta[j]));
}
}
// Solve
if (!solver.solve(p.getAlphaMatrix(), beta)) {
throw new AssertionError();
}
xList.add(beta);
// System.out.println(Arrays.toString(beta));
}
// for (int b = 1; b < 1000; b *= 2)
for (final double b : new double[] { -500, -100, -10, -1, -0.1, 0, 0.1, 1, 10, 100, 500 }) {
final Statistics[] rel = new Statistics[nparams];
final Statistics[] abs = new Statistics[nparams];
if (debug) {
for (int i = 0; i < nparams; i++) {
rel[i] = new Statistics();
abs[i] = new Statistics();
}
}
for (int i = 0; i < paramsList.size(); i++) {
final double[] y = add(yList.get(i), b);
final double[] a = paramsList.get(i).clone();
a[0] += b;
final BaseLsqLvmGradientProcedure p = LsqLvmGradientProcedureUtils.create(y, func);
p.gradient(a);
final double[] beta = p.beta;
final double[] alpha2 = alphaList.get(i);
final double[] beta2 = betaList.get(i);
final double[] x2 = xList.get(i);
Assertions.assertArrayEquals(beta2, beta, 1e-10, "Beta");
Assertions.assertArrayEquals(alpha2, p.getAlphaLinear(), 1e-10, "Alpha");
// Solve
solver.solve(p.getAlphaMatrix(), beta);
Assertions.assertArrayEquals(x2, beta, 1e-10, "X");
if (debug) {
for (int j = 0; j < nparams; j++) {
rel[j].add(DoubleEquality.relativeError(x2[j], beta[j]));
abs[j].add(Math.abs(x2[j] - beta[j]));
}
}
}
if (debug) {
for (int i = 0; i < nparams; i++) {
logger.log(TestLogUtils.getRecord(logLevel, "Bias = %.2f : %s : Rel %g +/- %g: Abs %g +/- %g", b, func.getGradientParameterName(i), rel[i].getMean(), rel[i].getStandardDeviation(), abs[i].getMean(), abs[i].getStandardDeviation()));
}
}
}
} finally {
background = defaultBackground;
}
}
use of uk.ac.sussex.gdsc.test.junit5.RandomSeed in project GDSC-SMLM by aherbert.
the class LvmGradientProcedureTest method gradientProcedureFastLogMleCannotComputeGradientWithHighPrecision.
@Disabled("This test now passes as the tolerance for computing the gradient has been lowered " + " so that the tests pass under a stress test using many different random seeds.")
@SeededTest
void gradientProcedureFastLogMleCannotComputeGradientWithHighPrecision(RandomSeed seed) {
// Try different precision
for (int n = FastLog.N; n < 23; n++) {
try {
// logger.fine(FunctionUtils.getSupplier("Precision n=%d", n);
fastLog = new TurboLog2(n);
gradientProcedureComputesGradient(seed, new SingleFreeCircularErfGaussian2DFunction(blockWidth, blockWidth), Type.FAST_LOG_MLE, false);
} catch (final AssertionError ex) {
continue;
} finally {
// Reset
fastLog = null;
}
return;
}
Assertions.fail();
}
use of uk.ac.sussex.gdsc.test.junit5.RandomSeed in project GDSC-SMLM by aherbert.
the class BinomialFitterTest method canFitZeroTruncatedBinomialWithUnknownNUsingMaximumLikelihood.
@SeededTest
void canFitZeroTruncatedBinomialWithUnknownNUsingMaximumLikelihood(RandomSeed seed) {
Assumptions.assumeTrue(TestSettings.allow(nonEssentialTestComplexity));
final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
final boolean zeroTruncated = true;
final boolean maximumLikelihood = false;
for (final int n : N) {
for (final double p : P) {
fitBinomial(rg, n, p, zeroTruncated, maximumLikelihood, 1, n);
}
}
}
Aggregations