use of uk.ac.sussex.gdsc.test.junit5.RandomSeed in project GDSC-SMLM by aherbert.
the class BinomialFitterTest method sameFitZeroTruncatedBinomialWithKnownNUsingLseOrMle.
@SeededTest
void sameFitZeroTruncatedBinomialWithKnownNUsingLseOrMle(RandomSeed seed) {
Assumptions.assumeTrue(TestSettings.allow(nonEssentialTestComplexity));
final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
final boolean zeroTruncated = true;
for (final int n : N) {
for (final double p : P) {
fitBinomialUsingLseOrMle(rg, n, p, zeroTruncated, n, n);
}
}
}
use of uk.ac.sussex.gdsc.test.junit5.RandomSeed in project GDSC-SMLM by aherbert.
the class BinomialFitterTest method canFitZeroTruncatedBinomialWithUnknownNUsingLeastSquaresEstimator.
@SeededTest
void canFitZeroTruncatedBinomialWithUnknownNUsingLeastSquaresEstimator(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);
}
}
}
use of uk.ac.sussex.gdsc.test.junit5.RandomSeed in project GDSC-SMLM by aherbert.
the class NormaliserTest method nonNormaliserCanCopyToOutDataWithBorder.
@SeededTest
void nonNormaliserCanCopyToOutDataWithBorder(RandomSeed seed) {
final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
for (final int width : primes) {
for (final int height : primes) {
final float[] data = createData(rg, width, height);
for (final int boxSize : boxSizes) {
// logger.fine(() -> String.format("%dx%d : border=%d", width, height, boxSize);
// Assume fixed normaliser works
final FixedNormaliser n = new FixedNormaliser(1);
final NonNormaliser nn = new NonNormaliser();
final float[] e = new float[data.length];
final float[] o = new float[data.length];
n.normalise(data, e, width, height, boxSize);
nn.normalise(data, o, width, height, boxSize);
Assertions.assertArrayEquals(o, e, () -> String.format("%dx%d : border=%d", width, height, boxSize));
}
}
}
}
use of uk.ac.sussex.gdsc.test.junit5.RandomSeed in project GDSC-SMLM by aherbert.
the class GradientCalculatorSpeedTest method gradientCalculatorComputesSameOutputWithBias.
@SeededTest
void gradientCalculatorComputesSameOutputWithBias(RandomSeed seed) {
final Gaussian2DFunction func = new SingleEllipticalGaussian2DFunction(blockWidth, blockWidth);
final int nparams = func.getNumberOfGradients();
final GradientCalculator calc = new GradientCalculator(nparams);
final int n = func.size();
final int iter = 50;
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;
final boolean report = logger.isLoggable(Level.INFO);
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 double[][] alpha = new double[nparams][nparams];
final double[] beta = new double[nparams];
calc.findLinearised(n, y, a, alpha, beta, func);
alphaList.add(alpha);
betaList.add(beta.clone());
for (int j = 0; j < nparams; j++) {
if (Math.abs(beta[j]) < 1e-6) {
logger.info(FunctionUtils.getSupplier("[%d] Tiny beta %s %g", i, func.getGradientParameterName(j), beta[j]));
}
}
// Solve
if (!solver.solve(alpha, beta)) {
throw new AssertionError();
}
xList.add(beta);
// System.out.println(Arrays.toString(beta));
}
final double[][] alpha = new double[nparams][nparams];
final double[] beta = new double[nparams];
final Statistics[] rel = new Statistics[nparams];
final Statistics[] abs = new Statistics[nparams];
for (int i = 0; i < nparams; i++) {
rel[i] = new Statistics();
abs[i] = new Statistics();
}
final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-10, 0);
// for (double b : new double[] { -500, -100, -10, -1, -0.1, 0.1, 1, 10, 100, 500 })
for (final double b : new double[] { -10, -1, -0.1, 0.1, 1, 10 }) {
if (report) {
for (int i = 0; i < nparams; i++) {
rel[i].reset();
abs[i].reset();
}
}
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;
calc.findLinearised(n, y, a, alpha, beta, func);
final double[][] alpha2 = alphaList.get(i);
final double[] beta2 = betaList.get(i);
final double[] x2 = xList.get(i);
TestAssertions.assertArrayTest(beta2, beta, predicate, "Beta");
TestAssertions.assertArrayTest(alpha2, alpha, predicate, "Alpha");
// Solve
solver.solve(alpha, beta);
Assertions.assertArrayEquals(x2, beta, 1e-10, "X");
if (report) {
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 (report) {
for (int i = 0; i < nparams; i++) {
logger.info(FunctionUtils.getSupplier("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 GradientCalculatorSpeedTest method gradientCalculatorAssumedXIsFasterThanGradientCalculator.
@SeededTest
void gradientCalculatorAssumedXIsFasterThanGradientCalculator(RandomSeed seed) {
Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
final int iter = 10000;
final ArrayList<double[]> paramsList = new ArrayList<>(iter);
final ArrayList<double[]> yList = new ArrayList<>(iter);
final int[] x = createData(RngUtils.create(seed.getSeed()), 1, iter, paramsList, yList);
final GradientCalculator calc = new GradientCalculator6();
final GradientCalculator calc2 = new GradientCalculator6();
final SingleFreeCircularGaussian2DFunction func = new SingleFreeCircularGaussian2DFunction(blockWidth, blockWidth);
final int n = x.length;
final int ng = func.getNumberOfGradients();
final double[][] alpha = new double[ng][ng];
final double[] beta = new double[ng];
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;
logger.log(TestLogUtils.getTimingRecord("GradientCalculator", start1, "GradientCalculatorAssumed", start2));
}
Aggregations