use of uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction in project GDSC-SMLM by aherbert.
the class ParameterBoundsTest method canDoubleBoundParameter.
@Test
void canDoubleBoundParameter() {
final ParameterBounds bounds = new ParameterBounds(new FakeGradientFunction(1, 1, 1));
final double s = 2;
bounds.setBounds(new double[] { -s }, new double[] { s });
final double[] a1 = new double[1];
final double[] a2 = new double[1];
bounds.applyBounds(a1, new double[] { 10 }, a2);
Assertions.assertArrayEquals(a2, new double[] { s }, "Step 10");
bounds.applyBounds(a1, new double[] { -10 }, a2);
Assertions.assertArrayEquals(a2, new double[] { -s }, "Step -10");
}
use of uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction in project GDSC-SMLM by aherbert.
the class WPoissonGradientProcedureTest method gradientProcedureUnrolledComputesSameAsGradientProcedure.
private void gradientProcedureUnrolledComputesSameAsGradientProcedure(RandomSeed seed, int nparams, boolean precomputed) {
final int iter = 10;
final ArrayList<double[]> paramsList = new ArrayList<>(iter);
final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
createFakeParams(rng, nparams, iter, paramsList);
final Gradient1Function func = new FakeGradientFunction(blockWidth, nparams);
final double[] v = (precomputed) ? dataCache.computeIfAbsent(seed, WPoissonGradientProcedureTest::createData) : null;
final IntArrayFormatSupplier msg = getMessage(nparams, "[%d] Observations: Not same linear @ %d");
for (int i = 0; i < paramsList.size(); i++) {
final double[] y = createFakeData(rng);
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));
// Exactly the same ...
Assertions.assertArrayEquals(p1.getLinear(), p2.getLinear(), msg.set(1, i));
}
}
use of uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction in project GDSC-SMLM by aherbert.
the class WPoissonGradientProcedureTest method gradientProcedureIsFasterThanWLseGradientProcedure.
private void gradientProcedureIsFasterThanWLseGradientProcedure(RandomSeed seed, final int nparams) {
Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
final int iter = 100;
final ArrayList<double[]> paramsList = new ArrayList<>(iter);
final ArrayList<double[]> yList = new ArrayList<>(iter);
final double[] var = dataCache.computeIfAbsent(seed, WPoissonGradientProcedureTest::createData);
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 IntArrayFormatSupplier msg = new IntArrayFormatSupplier("M [%d]", 1);
for (int i = 0; i < paramsList.size(); i++) {
final double[] y = yList.get(i);
final WLsqLvmGradientProcedure p1 = WLsqLvmGradientProcedureUtils.create(y, var, func);
p1.gradient(paramsList.get(i));
final WPoissonGradientProcedure p2 = WPoissonGradientProcedureUtils.create(y, var, func);
p2.computeFisherInformation(paramsList.get(i));
// Check they are the same
Assertions.assertArrayEquals(p1.getAlphaLinear(), 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 WLsqLvmGradientProcedure p1 = WLsqLvmGradientProcedureUtils.create(yList.get(i), var, func);
for (int j = loops; j-- > 0; ) {
p1.gradient(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), var, func);
for (int j = loops; j-- > 0; ) {
p2.computeFisherInformation(paramsList.get(k++ % iter));
}
}
}
};
final long time2 = t2.getTime();
logger.log(TestLogUtils.getTimingRecord("WLsqLvmGradientProcedure " + nparams, time1, "WPoissonGradientProcedure", time2));
}
use of uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction in project GDSC-SMLM by aherbert.
the class LsqVarianceGradientProcedureTest 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 IndexSupplier msg = new IndexSupplier(1, "M ", null);
for (int i = 0; i < paramsList.size(); i++) {
final LsqVarianceGradientProcedure p1 = new LsqVarianceGradientProcedure(func);
p1.variance(paramsList.get(i));
p1.variance(paramsList.get(i));
final LsqVarianceGradientProcedure p2 = LsqVarianceGradientProcedureUtils.create(func);
p2.variance(paramsList.get(i));
p2.variance(paramsList.get(i));
// Check they are the same
Assertions.assertArrayEquals(p1.variance, p2.variance, 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 LsqVarianceGradientProcedure p1 = new LsqVarianceGradientProcedure(func);
for (int j = loops; j-- > 0; ) {
p1.variance(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 LsqVarianceGradientProcedure p2 = LsqVarianceGradientProcedureUtils.create(func);
for (int j = loops; j-- > 0; ) {
p2.variance(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.smlm.function.FakeGradientFunction in project GDSC-SMLM by aherbert.
the class LsqVarianceGradientProcedureTest method gradientProcedureUnrolledComputesSameAsGradientProcedure.
private void gradientProcedureUnrolledComputesSameAsGradientProcedure(RandomSeed seed, int nparams, boolean precomputed) {
final int iter = 10;
final ArrayList<double[]> paramsList = new ArrayList<>(iter);
createFakeParams(RngUtils.create(seed.getSeed()), nparams, iter, paramsList);
Gradient1Function func = new FakeGradientFunction(blockWidth, nparams);
if (precomputed) {
func = OffsetGradient1Function.wrapGradient1Function(func, SimpleArrayUtils.newArray(func.size(), 0.1, 1.3));
}
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 p1 = new LsqVarianceGradientProcedure(func);
p1.variance(paramsList.get(i));
final LsqVarianceGradientProcedure p2 = LsqVarianceGradientProcedureUtils.create(func);
p2.variance(paramsList.get(i));
// Exactly the same ...
Assertions.assertArrayEquals(p1.variance, p2.variance, msg.set(1, i));
}
}
Aggregations