use of uk.ac.sussex.gdsc.test.utils.TimingService in project GDSC-SMLM by aherbert.
the class CubicSplineFunctionTest method speedTest.
@SuppressWarnings("null")
private void speedTest(int n, int order) {
// No assertions, this is just a report
Assumptions.assumeTrue(logger.isLoggable(Level.INFO));
// Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
final CubicSplineFunction cf = (n == 2) ? f2 : f1;
Assumptions.assumeTrue(null != cf);
final CubicSplineFunction cff = (n == 2) ? f2f : f1f;
final ErfGaussian2DFunction gf = (ErfGaussian2DFunction) GaussianFunctionFactory.create2D(n, maxx, maxy, GaussianFunctionFactory.FIT_ASTIGMATISM, zModel);
final Gaussian2DFunction gf2 = (order < 2) ? GaussianFunctionFactory.create2D(n, maxx, maxy, GaussianFunctionFactory.FIT_SIMPLE_FREE_CIRCLE, zModel) : null;
final LocalList<double[]> l1 = new LocalList<>();
final LocalList<double[]> l2 = new LocalList<>();
final LocalList<double[]> l3 = new LocalList<>();
final double[] a = new double[1 + n * CubicSplineFunction.PARAMETERS_PER_PEAK];
final double[] b = new double[1 + n * Gaussian2DFunction.PARAMETERS_PER_PEAK];
double[] bb = null;
a[CubicSplineFunction.BACKGROUND] = 0.1;
b[Gaussian2DFunction.BACKGROUND] = 0.1;
for (int i = 0; i < n; i++) {
a[i * CubicSplineFunction.PARAMETERS_PER_PEAK + CubicSplineFunction.SIGNAL] = 10;
b[i * Gaussian2DFunction.PARAMETERS_PER_PEAK + Gaussian2DFunction.SIGNAL] = 10;
}
if (n == 2) {
// Fix second peak parameters
a[CubicSplineFunction.PARAMETERS_PER_PEAK + CubicSplineFunction.X_POSITION] = testcx1[0];
a[CubicSplineFunction.PARAMETERS_PER_PEAK + CubicSplineFunction.Y_POSITION] = testcy1[0];
a[CubicSplineFunction.PARAMETERS_PER_PEAK + CubicSplineFunction.Z_POSITION] = testcz1[0];
b[Gaussian2DFunction.PARAMETERS_PER_PEAK + Gaussian2DFunction.X_POSITION] = testcx1[0];
b[Gaussian2DFunction.PARAMETERS_PER_PEAK + Gaussian2DFunction.Y_POSITION] = testcy1[0];
b[Gaussian2DFunction.PARAMETERS_PER_PEAK + Gaussian2DFunction.Z_POSITION] = testcz1[0];
}
if (gf2 != null) {
bb = b.clone();
if (n == 2) {
// Fix second peak parameters
bb[Gaussian2DFunction.PARAMETERS_PER_PEAK + Gaussian2DFunction.X_SD] = zModel.getSx(testcz1[0]);
bb[Gaussian2DFunction.PARAMETERS_PER_PEAK + Gaussian2DFunction.Y_SD] = zModel.getSy(testcz1[0]);
}
}
for (int x = 0; x <= maxx; x++) {
a[CubicSplineFunction.X_POSITION] = x;
b[Gaussian2DFunction.X_POSITION] = x;
for (int y = 0; y <= maxy; y++) {
a[CubicSplineFunction.Y_POSITION] = y;
b[Gaussian2DFunction.Y_POSITION] = y;
for (int z = -zDepth; z <= zDepth; z++) {
a[CubicSplineFunction.Z_POSITION] = z;
b[Gaussian2DFunction.Z_POSITION] = z;
l1.add(a.clone());
l2.add(b.clone());
if (gf2 != null) {
bb[Gaussian2DFunction.X_SD] = zModel.getSx(z);
bb[Gaussian2DFunction.Y_SD] = zModel.getSy(z);
l3.add(bb.clone());
}
}
}
}
final double[][] x1 = l1.toArray(new double[0][]);
final double[][] x2 = l2.toArray(new double[0][]);
final double[][] x3 = l3.toArray(new double[0][]);
final TimingService ts = new TimingService(5);
ts.execute(new FunctionTimingTask(gf, x2, order));
if (gf2 != null) {
ts.execute(new FunctionTimingTask(gf2, x3, order));
}
ts.execute(new FunctionTimingTask(cf, x1, order));
ts.execute(new FunctionTimingTask(cff, x1, order, " single-precision"));
final int size = ts.getSize();
ts.repeat(size);
logger.info(ts.getReport(size));
}
use of uk.ac.sussex.gdsc.test.utils.TimingService in project GDSC-SMLM by aherbert.
the class FastLogTest method canTestDoubleSpeedLog1P.
@SpeedTag
@SeededTest
void canTestDoubleSpeedLog1P(RandomSeed seed) {
// No assertions, this is just a report
Assumptions.assumeTrue(logger.isLoggable(Level.INFO));
Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
final double[] x = new double[1000000];
for (int i = 0; i < x.length; i++) {
x[i] = nextUniformDouble(rng);
}
final MathLog fl = new MathLog();
final TimingService ts = new TimingService(5);
// ts.execute(new DoubleTimingTask(new TestLog(fl), 0, x));
ts.execute(new DoubleTimingTask(new Test1PLog(fl), 0, x));
ts.execute(new DoubleTimingTask(new TestLog1P(fl), 0, x));
ts.execute(new DoubleTimingTask(new TestLog1PApache(fl), 0, x));
// ts.execute(new DoubleTimingTask(new TestLog(fl), 0, x));
ts.execute(new DoubleTimingTask(new Test1PLog(fl), 0, x));
ts.execute(new DoubleTimingTask(new TestLog1P(fl), 0, x));
ts.execute(new DoubleTimingTask(new TestLog1PApache(fl), 0, x));
final int size = ts.getSize();
ts.repeat(size);
logger.info(ts.getReport(size));
}
use of uk.ac.sussex.gdsc.test.utils.TimingService in project GDSC-SMLM by aherbert.
the class FastLogTest method canTestFloatSpeed.
@SpeedTag
@SeededTest
void canTestFloatSpeed(RandomSeed seed) {
// No assertions, this is just a report
Assumptions.assumeTrue(logger.isLoggable(Level.INFO));
Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
final float[] x = new float[1000000];
for (int i = 0; i < x.length; i++) {
x[i] = nextUniformFloat(rng);
}
final TimingService ts = new TimingService(5);
ts.execute(new FloatTimingTask(new TestLog(new MathLog()), 0, x));
ts.execute(new FloatTimingTask(new TestLog(new FastMathLog()), 0, x));
for (final int q : new int[] { 11 }) {
final int n = 23 - q;
final IcsiFastLog fl = IcsiFastLog.create(n, DataType.FLOAT);
ts.execute(new FloatTimingTask(new TestLog(fl), q, x));
ts.execute(new FloatTimingTask(new TestFastLog(fl), q, x));
final FFastLog ff = new FFastLog(n);
ts.execute(new FloatTimingTask(new TestLog(ff), q, x));
ts.execute(new FloatTimingTask(new TestFastLog(ff), q, x));
final DFastLog df = new DFastLog(n);
ts.execute(new FloatTimingTask(new TestLog(df), q, x));
ts.execute(new FloatTimingTask(new TestFastLog(df), q, x));
final TurboLog tf = new TurboLog(n);
ts.execute(new FloatTimingTask(new TestLog(tf), q, x));
ts.execute(new FloatTimingTask(new TestFastLog(tf), q, x));
// TurboLog2 tf2 = new TurboLog2(n);
// ts.execute(new FloatTimingTask(new TestLog(tf2), q, x));
// ts.execute(new FloatTimingTask(new TestFastLog(tf2), q, x));
// For the same precision we can reduce n
final TurboLog2 tf3 = new TurboLog2(n - 1);
ts.execute(new FloatTimingTask(new TestLog(tf3), q + 1, x));
ts.execute(new FloatTimingTask(new TestFastLog(tf3), q + 1, x));
}
final int size = ts.getSize();
ts.repeat(size);
logger.info(ts.getReport(size));
}
use of uk.ac.sussex.gdsc.test.utils.TimingService in project GDSC-SMLM by aherbert.
the class MultivariateGaussianMixtureExpectationMaximizationTest method testExpectationMaximizationSpeed.
/**
* Test the speed of implementations of the expectation maximization algorithm with a mixture of n
* ND Gaussian distributions.
*
* @param seed the seed
*/
@SpeedTag
@SeededTest
void testExpectationMaximizationSpeed(RandomSeed seed) {
Assumptions.assumeTrue(TestSettings.allow(TestComplexity.HIGH));
final MultivariateGaussianMixtureExpectationMaximization.DoubleDoubleBiPredicate relChecker = TestHelper.doublesAreClose(1e-6)::test;
// Create data
final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
for (int n = 2; n <= 3; n++) {
for (int dim = 2; dim <= 4; dim++) {
final double[][][] data = new double[10][][];
final int nCorrelations = dim - 1;
for (int i = 0; i < data.length; i++) {
final double[] sampleWeights = createWeights(n, rng);
final double[][] sampleMeans = create(n, dim, rng, -5, 5);
final double[][] sampleStdDevs = create(n, dim, rng, 1, 10);
final double[][] sampleCorrelations = IntStream.range(0, n).mapToObj(component -> create(nCorrelations, rng, -0.9, 0.9)).toArray(double[][]::new);
data[i] = createDataNd(1000, rng, sampleWeights, sampleMeans, sampleStdDevs, sampleCorrelations);
}
final int numComponents = n;
// Time initial estimation and fitting
final TimingService ts = new TimingService();
ts.execute(new FittingSpeedTask("Commons n=" + n + " " + dim + "D", data) {
@Override
Object run(double[][] data) {
final MultivariateNormalMixtureExpectationMaximization fitter = new MultivariateNormalMixtureExpectationMaximization(data);
fitter.fit(MultivariateNormalMixtureExpectationMaximization.estimate(data, numComponents));
return fitter.getLogLikelihood();
}
});
ts.execute(new FittingSpeedTask("GDSC n=" + n + " " + dim + "D", data) {
@Override
Object run(double[][] data) {
final MultivariateGaussianMixtureExpectationMaximization fitter = new MultivariateGaussianMixtureExpectationMaximization(data);
fitter.fit(MultivariateGaussianMixtureExpectationMaximization.estimate(data, numComponents));
return fitter.getLogLikelihood();
}
});
ts.execute(new FittingSpeedTask("GDSC rel 1e-6 n=" + n + " " + dim + "D", data) {
@Override
Object run(double[][] data) {
final MultivariateGaussianMixtureExpectationMaximization fitter = new MultivariateGaussianMixtureExpectationMaximization(data);
fitter.fit(MultivariateGaussianMixtureExpectationMaximization.estimate(data, numComponents), 1000, relChecker);
return fitter.getLogLikelihood();
}
});
if (logger.isLoggable(Level.INFO)) {
logger.info(ts.getReport());
}
// More than twice as fast
Assertions.assertTrue(ts.get(-2).getMean() < ts.get(-3).getMean() / 2);
}
}
}
use of uk.ac.sussex.gdsc.test.utils.TimingService in project GDSC-SMLM by aherbert.
the class MultivariateGaussianMixtureExpectationMaximizationTest method testExpectationMaximizationSpeedWithDifferentNumberOfComponents.
/**
* Test the speed of implementations of the expectation maximization algorithm with a mixture of n
* 2D Gaussian distributions.
*
* @param seed the seed
*/
@SpeedTag
@SeededTest
void testExpectationMaximizationSpeedWithDifferentNumberOfComponents(RandomSeed seed) {
Assumptions.assumeTrue(TestSettings.allow(TestComplexity.HIGH));
// Create data
final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
for (int n = 2; n <= 4; n++) {
final double[][][] data = new double[10][][];
for (int i = 0; i < data.length; i++) {
final double[] sampleWeights = createWeights(n, rng);
final double[][] sampleMeans = create(n, 2, rng, -5, 5);
final double[][] sampleStdDevs = create(n, 2, rng, 1, 10);
final double[] sampleCorrelations = create(n, rng, -0.9, 0.9);
data[i] = createData2d(1000, rng, sampleWeights, sampleMeans, sampleStdDevs, sampleCorrelations);
}
final int numComponents = n;
// Time initial estimation and fitting
final TimingService ts = new TimingService();
ts.execute(new FittingSpeedTask("Commons n=" + n + " 2D", data) {
@Override
Object run(double[][] data) {
final MultivariateNormalMixtureExpectationMaximization fitter = new MultivariateNormalMixtureExpectationMaximization(data);
fitter.fit(MultivariateNormalMixtureExpectationMaximization.estimate(data, numComponents));
return fitter.getLogLikelihood();
}
});
ts.execute(new FittingSpeedTask("GDSC n=" + n + " 2D", data) {
@Override
Object run(double[][] data) {
final MultivariateGaussianMixtureExpectationMaximization fitter = new MultivariateGaussianMixtureExpectationMaximization(data);
fitter.fit(MultivariateGaussianMixtureExpectationMaximization.estimate(data, numComponents));
return fitter.getLogLikelihood();
}
});
if (logger.isLoggable(Level.INFO)) {
logger.info(ts.getReport());
}
// More than twice as fast
Assertions.assertTrue(ts.get(-1).getMean() < ts.get(-2).getMean() / 2);
}
}
Aggregations