Search in sources :

Example 1 with AstigmatismZModel

use of uk.ac.sussex.gdsc.smlm.function.gaussian.AstigmatismZModel in project GDSC-SMLM by aherbert.

the class GaussianPsfModelTest method canComputeValueAndGradient.

@Test
void canComputeValueAndGradient() {
    // 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 AstigmatismZModel zModel = HoltzerAstigmatismZModel.create(sx, sy, gamma, d, Ax, Bx, Ay, By);
    // zModel = new NullAstigmatismZModel(1.2, 1.2);
    final int maxx = 21;
    final int maxy = 21;
    final double[] o = new double[maxx * maxy];
    final double[] o2 = new double[maxx * maxy];
    final double[] o3 = new double[maxx * maxy];
    final double[][] g = new double[maxx * maxy][];
    final double[][] g2 = new double[maxx * maxy][3];
    final double[] dx = new double[] { 1e-4, 1e-4, 1e-4 };
    final PsfModel psf = new GaussianPsfModel(zModel);
    final double c = maxx * 0.5;
    for (int i = -1; i <= 1; i++) {
        final double x0 = c + i * 0.33;
        for (int j = -1; j <= 1; j++) {
            final double x1 = c + j * 0.33;
            for (int k = -1; k <= 1; k++) {
                final double x2 = k * 0.33;
                psf.getValue(maxx, maxy, x0, x1, x2, o);
                psf.getValueAndGradient(maxx, maxy, x0, x1, x2, o2, g);
                Assertions.assertArrayEquals(o, o2);
                // Check verses default manual gradient
                psf.computeValueAndGradient(maxx, maxy, x0, x1, x2, o3, g2, dx);
                Assertions.assertArrayEquals(o, o3);
                for (int ii = 0; ii < o.length; ii++) {
                    if (o[ii] > 1e-4) {
                        for (int l = 0; l < 3; l++) {
                            final double error = DoubleEquality.relativeError(g[ii][l], g2[ii][l]);
                            // g2[ii][l], error);
                            if (error > 5e-3) {
                                Assertions.fail(String.format("[%d] %s != %s  error = %f", ii, Double.toString(g[ii][l]), Double.toString(g2[ii][l]), error));
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : AstigmatismZModel(uk.ac.sussex.gdsc.smlm.function.gaussian.AstigmatismZModel) HoltzerAstigmatismZModel(uk.ac.sussex.gdsc.smlm.function.gaussian.HoltzerAstigmatismZModel) Test(org.junit.jupiter.api.Test)

Example 2 with AstigmatismZModel

use of uk.ac.sussex.gdsc.smlm.function.gaussian.AstigmatismZModel in project GDSC-SMLM by aherbert.

the class GaussianPsfModelTest method canComputeValue.

@Test
void canComputeValue() {
    // 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 AstigmatismZModel zModel = HoltzerAstigmatismZModel.create(sx, sy, gamma, d, Ax, Bx, Ay, By);
    final int maxx = 21;
    final int maxy = 21;
    final double[] e = new double[maxx * maxy];
    final double[] o = new double[maxx * maxy];
    final double[] o2 = new double[maxx * maxy];
    final double[][] g = new double[maxx * maxy][];
    final PsfModel psf = new GaussianPsfModel(zModel);
    final double c = maxx * 0.5;
    for (int i = -1; i <= 1; i++) {
        final double x0 = c + i * 0.33;
        for (int j = -1; j <= 1; j++) {
            final double x1 = c + j * 0.33;
            for (int k = -1; k <= 1; k++) {
                final double x2 = k * 0.33;
                Arrays.fill(e, 0);
                psf.create3D(e, maxx, maxy, 1, x0, x1, x2, null);
                psf.getValue(maxx, maxy, x0, x1, x2, o);
                Assertions.assertEquals(1, MathUtils.sum(o), 1e-3);
                psf.getValueAndGradient(maxx, maxy, x0, x1, x2, o2, g);
                for (int ii = 0; ii < e.length; ii++) {
                    if (e[ii] == 0) {
                        // Same insertion into the blank data region
                        Assertions.assertTrue(o[ii] == 0);
                    // Only check where there is a reasonable amount of signal
                    } else if (e[ii] > 1e-8) {
                        final double error = DoubleEquality.relativeError(e[ii], o[ii]);
                        // uses the Apache commons implementation.
                        if (error > 1e-8) {
                            Assertions.fail(String.format("[%d] %s != %s  error = %f", ii, Double.toString(e[ii]), Double.toString(o[ii]), error));
                        }
                    }
                }
            }
        }
    }
}
Also used : AstigmatismZModel(uk.ac.sussex.gdsc.smlm.function.gaussian.AstigmatismZModel) HoltzerAstigmatismZModel(uk.ac.sussex.gdsc.smlm.function.gaussian.HoltzerAstigmatismZModel) Test(org.junit.jupiter.api.Test)

Example 3 with AstigmatismZModel

use of uk.ac.sussex.gdsc.smlm.function.gaussian.AstigmatismZModel in project GDSC-SMLM by aherbert.

the class GaussianPsfModelTest method canComputeValueWithDifferentRange.

@Test
void canComputeValueWithDifferentRange() {
    // 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 AstigmatismZModel zModel = HoltzerAstigmatismZModel.create(sx, sy, gamma, d, Ax, Bx, Ay, By);
    final int maxx = 21;
    final int maxy = 21;
    final double[] o = new double[maxx * maxy];
    final double[] o2 = new double[maxx * maxy];
    final PsfModel psf = new GaussianPsfModel(zModel);
    final GaussianPsfModel psf2 = new GaussianPsfModel(zModel);
    psf2.setRange(40);
    final double c = maxx * 0.5;
    for (int i = -1; i <= 1; i++) {
        final double x0 = c + i * 0.33;
        for (int j = -1; j <= 1; j++) {
            final double x1 = c + j * 0.33;
            for (int k = -1; k <= 1; k++) {
                final double x2 = k * 0.33;
                psf.getValue(maxx, maxy, x0, x1, x2, o);
                psf2.getValue(maxx, maxy, x0, x1, x2, o2);
                int extra = 0;
                int zero = 0;
                for (int ii = 0; ii < o.length; ii++) {
                    if (o[ii] == 0) {
                        // PSF has a larger range
                        zero++;
                        if (o2[ii] != 0) {
                            extra++;
                        }
                    } else {
                        Assertions.assertEquals(o[ii], o2[ii]);
                    }
                }
                Assertions.assertFalse(0 == extra, "No extra");
                final double f = (double) extra / zero;
                // logger.fine(FunctionUtils.getSupplier("Extra %d/%d (%g)", extra, zero, f);
                Assertions.assertTrue(f > 0.4, "Not much extra");
            }
        }
    }
}
Also used : AstigmatismZModel(uk.ac.sussex.gdsc.smlm.function.gaussian.AstigmatismZModel) HoltzerAstigmatismZModel(uk.ac.sussex.gdsc.smlm.function.gaussian.HoltzerAstigmatismZModel) Test(org.junit.jupiter.api.Test)

Example 4 with AstigmatismZModel

use of uk.ac.sussex.gdsc.smlm.function.gaussian.AstigmatismZModel in project GDSC-SMLM by aherbert.

the class CreateData method getAstigmatismHwhm.

private double getAstigmatismHwhm() {
    AstigmatismModel model = AstigmatismModelManager.getModel(settings.getAstigmatismModel());
    if (model == null) {
        IJ.error(TITLE, "Unknown PSF model: " + settings.getAstigmatismModel());
        return -1;
    }
    try {
        // Get the width at z=0 in pixels
        model = AstigmatismModelManager.convert(model, model.getZDistanceUnit(), DistanceUnit.PIXEL);
        final AstigmatismZModel zModel = AstigmatismModelManager.create(model);
        final double sx = zModel.getSx(0);
        final double sy = zModel.getSy(0);
        return Gaussian2DPeakResultHelper.getStandardDeviation(sx, sy) * Gaussian2DFunction.SD_TO_HWHM_FACTOR * // Scale appropriately
        model.getNmPerPixel() / settings.getPixelPitch();
    } catch (final ConversionException ex) {
        IJ.error(TITLE, "Unknown PSF FWHM setting for model: " + settings.getAstigmatismModel());
        return -1;
    }
}
Also used : ConversionException(uk.ac.sussex.gdsc.core.data.utils.ConversionException) AstigmatismModel(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel) AstigmatismZModel(uk.ac.sussex.gdsc.smlm.function.gaussian.AstigmatismZModel) HoltzerAstigmatismZModel(uk.ac.sussex.gdsc.smlm.function.gaussian.HoltzerAstigmatismZModel)

Example 5 with AstigmatismZModel

use of uk.ac.sussex.gdsc.smlm.function.gaussian.AstigmatismZModel in project GDSC-SMLM by aherbert.

the class AstigmatismModelManagerTest method canConvertModel.

private static void canConvertModel(DistanceUnit zDistanceUnit, DistanceUnit sdDistanceUnit) {
    // 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 double nmPerPixel = 100;
    // Ax = Ay = 0;
    // Bx = By = 0;
    final AstigmatismModel.Builder builder = AstigmatismModel.newBuilder();
    builder.setGamma(gamma);
    builder.setD(d);
    builder.setS0X(sx);
    builder.setAx(Ax);
    builder.setBx(Bx);
    builder.setS0Y(sy);
    builder.setAy(Ay);
    builder.setBy(By);
    builder.setZDistanceUnit(DistanceUnit.UM);
    builder.setSDistanceUnit(DistanceUnit.PIXEL);
    builder.setNmPerPixel(nmPerPixel);
    final AstigmatismModel model1 = builder.build();
    final AstigmatismModel model2 = AstigmatismModelManager.convert(model1, zDistanceUnit, sdDistanceUnit);
    final AstigmatismZModel m1 = AstigmatismModelManager.create(model1);
    final AstigmatismZModel m2 = AstigmatismModelManager.create(model2);
    final TypeConverter<DistanceUnit> zc = UnitConverterUtils.createConverter(DistanceUnit.UM, zDistanceUnit, nmPerPixel);
    final TypeConverter<DistanceUnit> sc = UnitConverterUtils.createConverter(DistanceUnit.PIXEL, sdDistanceUnit, nmPerPixel);
    final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-8, 0);
    for (double z = -0.5; z <= 0.5; z += 0.1) {
        final double e = sc.convert(m1.getSx(z));
        final double o = m2.getSx(zc.convert(z));
        // logger.fine(FunctionUtils.getSupplier("%f vs %f", e, o);
        TestAssertions.assertTest(e, o, predicate);
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) AstigmatismModel(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel) AstigmatismZModel(uk.ac.sussex.gdsc.smlm.function.gaussian.AstigmatismZModel) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)

Aggregations

AstigmatismZModel (uk.ac.sussex.gdsc.smlm.function.gaussian.AstigmatismZModel)6 HoltzerAstigmatismZModel (uk.ac.sussex.gdsc.smlm.function.gaussian.HoltzerAstigmatismZModel)5 Test (org.junit.jupiter.api.Test)4 AstigmatismModel (uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel)2 DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)2 ConversionException (uk.ac.sussex.gdsc.core.data.utils.ConversionException)1 DistanceUnit (uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)1 Gradient1Procedure (uk.ac.sussex.gdsc.smlm.function.Gradient1Procedure)1 ErfGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.erf.ErfGaussian2DFunction)1 SingleAstigmatismErfGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.erf.SingleAstigmatismErfGaussian2DFunction)1