use of uk.ac.sussex.gdsc.test.utils.functions.IntArrayFormatSupplier in project GDSC-SMLM by aherbert.
the class WPoissonGradientProcedureTest method gradientProcedureIsFasterUnrolledThanGradientProcedure.
private void gradientProcedureIsFasterUnrolledThanGradientProcedure(RandomSeed seed, final int nparams, final boolean precomputed) {
Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
final int iter = 100;
final ArrayList<double[]> paramsList = new ArrayList<>(iter);
final ArrayList<double[]> yList = new ArrayList<>(iter);
createFakeData(RngUtils.create(seed.getSeed()), nparams, iter, paramsList, yList);
// Remove the timing of the function call by creating a dummy function
final FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
final double[] v = (precomputed) ? dataCache.computeIfAbsent(seed, WPoissonGradientProcedureTest::createData) : null;
final IntArrayFormatSupplier msg = new IntArrayFormatSupplier("M [%d]", 1);
for (int i = 0; i < paramsList.size(); i++) {
final double[] y = yList.get(i);
final WPoissonGradientProcedure p1 = new WPoissonGradientProcedure(y, v, func);
p1.computeFisherInformation(paramsList.get(i));
final WPoissonGradientProcedure p2 = WPoissonGradientProcedureUtils.create(y, v, func);
p2.computeFisherInformation(paramsList.get(i));
// Check they are the same
Assertions.assertArrayEquals(p1.getLinear(), p2.getLinear(), msg.set(0, i));
}
// Realistic loops for an optimisation
final int loops = 15;
// Run till stable timing
final Timer t1 = new Timer() {
@Override
void run() {
for (int i = 0, k = 0; i < paramsList.size(); i++) {
final WPoissonGradientProcedure p1 = new WPoissonGradientProcedure(yList.get(i), v, func);
for (int j = loops; j-- > 0; ) {
p1.computeFisherInformation(paramsList.get(k++ % iter));
}
}
}
};
final long time1 = t1.getTime();
final Timer t2 = new Timer(t1.loops) {
@Override
void run() {
for (int i = 0, k = 0; i < paramsList.size(); i++) {
final WPoissonGradientProcedure p2 = WPoissonGradientProcedureUtils.create(yList.get(i), v, func);
for (int j = loops; j-- > 0; ) {
p2.computeFisherInformation(paramsList.get(k++ % iter));
}
}
}
};
final long time2 = t2.getTime();
logger.log(TestLogUtils.getTimingRecord("precomputed=" + precomputed + " Standard " + nparams, time1, "Unrolled", time2));
}
use of uk.ac.sussex.gdsc.test.utils.functions.IntArrayFormatSupplier in project GDSC-SMLM by aherbert.
the class LsqVarianceGradientProcedureTest method gradientProcedureComputesSameAsGradientCalculator.
private void gradientProcedureComputesSameAsGradientCalculator(RandomSeed seed, int nparams) {
final int iter = 10;
final ArrayList<double[]> paramsList = new ArrayList<>(iter);
createFakeParams(RngUtils.create(seed.getSeed()), nparams, iter, paramsList);
final int n = blockWidth * blockWidth;
final FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
final GradientCalculator calc = GradientCalculatorUtils.newCalculator(nparams, false);
final IntArrayFormatSupplier msg = new IntArrayFormatSupplier("[%d] Observations: Not same variance @ %d", 2);
msg.set(0, nparams);
for (int i = 0; i < paramsList.size(); i++) {
final LsqVarianceGradientProcedure p = LsqVarianceGradientProcedureUtils.create(func);
p.variance(paramsList.get(i));
final double[] e = calc.variance(n, paramsList.get(i), func);
Assertions.assertArrayEquals(e, p.variance, msg.set(1, i));
}
}
use of uk.ac.sussex.gdsc.test.utils.functions.IntArrayFormatSupplier in project GDSC-SMLM by aherbert.
the class PoissonGradientProcedureTest method gradientProcedureIsFasterUnrolledThanGradientProcedure.
private void gradientProcedureIsFasterUnrolledThanGradientProcedure(RandomSeed seed, final int nparams, final boolean precomputed) {
Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
final int iter = 100;
final ArrayList<double[]> paramsList = new ArrayList<>(iter);
createFakeParams(RngUtils.create(seed.getSeed()), nparams, iter, paramsList);
// Remove the timing of the function call by creating a dummy function
final FakeGradientFunction f = new FakeGradientFunction(blockWidth, nparams);
final Gradient1Function func = (precomputed) ? OffsetGradient1Function.wrapGradient1Function(f, SimpleArrayUtils.newArray(f.size(), 0.1, 1.3)) : f;
final IntArrayFormatSupplier msg = new IntArrayFormatSupplier("M [%d]", 1);
for (int i = 0; i < paramsList.size(); i++) {
final PoissonGradientProcedure p1 = new PoissonGradientProcedure(func);
p1.computeFisherInformation(paramsList.get(i));
p1.computeFisherInformation(paramsList.get(i));
final PoissonGradientProcedure p2 = PoissonGradientProcedureUtils.create(func);
p2.computeFisherInformation(paramsList.get(i));
p2.computeFisherInformation(paramsList.get(i));
// Check they are the same
Assertions.assertArrayEquals(p1.getLinear(), p2.getLinear(), msg.set(0, i));
}
// Realistic loops for an optimisation
final int loops = 15;
// Run till stable timing
final Timer t1 = new Timer() {
@Override
void run() {
for (int i = 0, k = 0; i < paramsList.size(); i++) {
final PoissonGradientProcedure p1 = new PoissonGradientProcedure(func);
for (int j = loops; j-- > 0; ) {
p1.computeFisherInformation(paramsList.get(k++ % iter));
}
}
}
};
final long time1 = t1.getTime();
final Timer t2 = new Timer(t1.loops) {
@Override
void run() {
for (int i = 0, k = 0; i < paramsList.size(); i++) {
final PoissonGradientProcedure p2 = PoissonGradientProcedureUtils.create(func);
for (int j = loops; j-- > 0; ) {
p2.computeFisherInformation(paramsList.get(k++ % iter));
}
}
}
};
final long time2 = t2.getTime();
logger.log(TestLogUtils.getTimingRecord("precomputed=" + precomputed + " Standard " + nparams, time1, "Unrolled", time2));
// Assertions.assertTrue(time2 < time1);
}
Aggregations