Search in sources :

Example 1 with AstigmatismModel

use of uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel in project GDSC-SMLM by aherbert.

the class CreateData method createPsf.

private PSF createPsf(double psfSd) {
    if (psf == null) {
        if (psfModelType == PSF_MODEL_ASTIGMATISM) {
            // Note: the astigmatismModel may not yet be created so create if necessary.
            // This is used to store the best PSF to use to fit the data.
            AstigmatismModel astigmatismModel = this.astigmatismModel;
            if (astigmatismModel == null) {
                astigmatismModel = AstigmatismModelManager.getModel(settings.getAstigmatismModel());
            }
            if (astigmatismModel == null) {
                throw new IllegalArgumentException("Failed to load model: " + settings.getAstigmatismModel());
            }
            // Assume conversion for simulation
            try {
                if (DoubleEquality.relativeError(astigmatismModel.getNmPerPixel(), settings.getPixelPitch()) > 1e-6) {
                    // Convert to nm
                    astigmatismModel = AstigmatismModelManager.convert(astigmatismModel, DistanceUnit.NM, DistanceUnit.NM);
                    // Reset pixel pitch. This will draw the spot using the correct size on the different
                    // size pixels.
                    astigmatismModel = astigmatismModel.toBuilder().setNmPerPixel(settings.getPixelPitch()).build();
                }
                // Convert for simulation in pixels
                astigmatismModel = AstigmatismModelManager.convert(astigmatismModel, DistanceUnit.PIXEL, DistanceUnit.PIXEL);
            } catch (final ConversionException ex) {
                // Wrap so this can be caught as the same type
                throw new IllegalArgumentException(ex);
            }
            psf = PsfProtosHelper.createPsf(astigmatismModel, DistanceUnit.PIXEL, DistanceUnit.PIXEL);
            psf = psf.toBuilder().setModelName(settings.getAstigmatismModel()).build();
        } else {
            PSF.Builder psfBuilder;
            // Set the PSF as a Gaussian using the width at z=0.
            // In future this could be improved for other PSFs.
            psfBuilder = PsfProtosHelper.defaultOneAxisGaussian2DPSF.toBuilder();
            psfBuilder.getParametersBuilder(PsfHelper.INDEX_SX).setValue(psfSd);
            psf = psfBuilder.build();
        }
    }
    return psf;
}
Also used : ConversionException(uk.ac.sussex.gdsc.core.data.utils.ConversionException) ImagePSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.ImagePSF) PSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF) AstigmatismModel(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel)

Example 2 with AstigmatismModel

use of uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel in project GDSC-SMLM by aherbert.

the class FitConfiguration method getAstigmatismZModel.

/**
 * Gets the astigmatism Z model. This is only valid if the PSF type is an astigmatic Gaussian 2D
 * and the parameters are correctly configured.
 *
 * @return the astigmatism Z model (or null)
 * @throws ConfigurationException if the model cannot be created from the parameters
 */
public AstigmatismZModel getAstigmatismZModel() {
    if (getPsfTypeValue() == PSFType.ASTIGMATIC_GAUSSIAN_2D_VALUE) {
        if (astigmatismZModel == null) {
            // Use the helper to convert the PSF parameters back to a model
            final AstigmatismModel model = PsfProtosHelper.createModel(getPsf(), DistanceUnit.PIXEL, DistanceUnit.PIXEL, calibration.getNmPerPixel());
            astigmatismZModel = HoltzerAstigmatismZModel.create(model.getS0X(), model.getS0Y(), model.getGamma(), model.getD(), model.getAx(), model.getBx(), model.getAy(), model.getBy());
        }
        return astigmatismZModel;
    }
    return null;
}
Also used : AstigmatismModel(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel)

Example 3 with AstigmatismModel

use of uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel in project GDSC-SMLM by aherbert.

the class PeakFit method configurePsfModel.

/**
 * Show a dialog to configure the PSF model. The updated settings are saved to the settings file.
 *
 * <p>If the configuration is for a 3D PSF then a dialog to configure the z model is shown.
 *
 * @param config the config
 * @param flags the flags
 * @return true, if successful
 */
public static boolean configurePsfModel(FitEngineConfiguration config, int flags) {
    final FitConfiguration fitConfig = config.getFitConfiguration();
    if (fitConfig.getPsfTypeValue() != PSFType.ASTIGMATIC_GAUSSIAN_2D_VALUE) {
        return true;
    }
    // Get the astigmatism z-model
    final AstigmatismModel model = AstigmatismModelManager.getModel(fitConfig.getPsfModelName());
    if (model == null) {
        IJ.error(TITLE, "Failed to load the model: " + fitConfig.getPsfModelName());
        return false;
    }
    // Conversion to the correct units in pixels is done within the FitConfiguration object.
    fitConfig.setAstigmatismModel(model);
    if (BitFlagUtils.anyNotSet(flags, FLAG_NO_SAVE)) {
        SettingsManager.writeSettings(config, 0);
    }
    return true;
}
Also used : FitConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitConfiguration) AstigmatismModel(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel)

Example 4 with AstigmatismModel

use of uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel in project GDSC-SMLM by aherbert.

the class AstigmatismModelManager method exportModel.

private void exportModel() {
    final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
    final String[] models = listAstigmatismModels(false);
    gd.addChoice("Model", models, pluginSettings.getSelected());
    gd.addFilenameField("Filename", pluginSettings.getFilename());
    gd.addHelp(HelpUrls.getUrl("astigmatism-model-manager-export"));
    gd.showDialog();
    if (gd.wasCanceled()) {
        return;
    }
    final String name = gd.getNextChoice();
    pluginSettings.setSelected(name);
    pluginSettings.setFilename(gd.getNextString());
    // Try and get the named resource
    final AstigmatismModel model = AstigmatismModelSettingsHolder.getSettings().getAstigmatismModelResourcesMap().get(name);
    if (model == null) {
        IJ.error(TITLE, "Failed to find astigmatism model: " + name);
        return;
    }
    if (!SettingsManager.toJson(model, new File(pluginSettings.getFilename()), 0)) {
        IJ.error(TITLE, "Failed to export astigmatism model: " + name);
        return;
    }
    ImageJUtils.log("Exported astigmatism model: %s to %s", name, pluginSettings.getFilename());
}
Also used : AstigmatismModel(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel) NonBlockingExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) File(java.io.File)

Example 5 with AstigmatismModel

use of uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel in project GDSC-SMLM by aherbert.

the class AstigmatismModelManager method viewModel.

private void viewModel() {
    final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
    final String[] models = listAstigmatismModels(false);
    gd.addChoice("Model", models, pluginSettings.getSelected());
    gd.addChoice("z_distance_unit", SettingsManager.getDistanceUnitNames(), pluginSettings.getZDistanceUnitValue());
    gd.addChoice("s_distance_unit", SettingsManager.getDistanceUnitNames(), pluginSettings.getSDistanceUnitValue());
    gd.addCheckbox("Show_depth_of_focus", pluginSettings.getShowDepthOfFocus());
    gd.addCheckbox("Show_combined_width", pluginSettings.getShowCombinedWidth());
    gd.addCheckbox("Show_PSF", pluginSettings.getShowPsf());
    gd.addHelp(HelpUrls.getUrl("astigmatism-model-manager-view"));
    gd.showDialog();
    if (gd.wasCanceled()) {
        return;
    }
    final String name = gd.getNextChoice();
    pluginSettings.setSelected(name);
    pluginSettings.setZDistanceUnitValue(gd.getNextChoiceIndex());
    pluginSettings.setSDistanceUnitValue(gd.getNextChoiceIndex());
    pluginSettings.setShowDepthOfFocus(gd.getNextBoolean());
    pluginSettings.setShowCombinedWidth(gd.getNextBoolean());
    pluginSettings.setShowPsf(gd.getNextBoolean());
    // Try and get the named resource
    AstigmatismModel model = AstigmatismModelSettingsHolder.getSettings().getAstigmatismModelResourcesMap().get(name);
    if (model == null) {
        IJ.error(TITLE, "Failed to find astigmatism model: " + name);
        return;
    }
    try {
        model = convert(model, pluginSettings.getZDistanceUnit(), pluginSettings.getSDistanceUnit());
    } catch (final ConversionException ex) {
        ImageJUtils.log("Bad conversion (%s), defaulting to native model units", ex.getMessage());
    }
    ImageJUtils.log("Astigmatism model: %s\n%s", name, model);
    // Plot the curve. Do this so we encompass twice the depth-of-field.
    final double gamma = model.getGamma();
    final double d = model.getD();
    final double s0x = model.getS0X();
    final double Ax = model.getAx();
    final double Bx = model.getBx();
    final double s0y = model.getS0Y();
    final double Ay = model.getAy();
    final double By = model.getBy();
    final double range = Math.abs(gamma) + 1.5 * d;
    final int n = 200;
    final double step = range / n;
    final double[] z = new double[2 * n + 1];
    final double[] sx = new double[z.length];
    final double[] sy = new double[z.length];
    // Use the same class that is used during fitting
    final HoltzerAstigmatismZModel m = HoltzerAstigmatismZModel.create(s0x, s0y, gamma, d, Ax, Bx, Ay, By);
    for (int i = 0; i < z.length; i++) {
        final double zz = -range + i * step;
        z[i] = zz;
        sx[i] = m.getSx(zz);
        sy[i] = m.getSy(zz);
    }
    final String title = TITLE + " Width Curve";
    final Plot plot = new Plot(title, "Z (" + UnitHelper.getShortName(model.getZDistanceUnit()) + ")", "Width (" + UnitHelper.getShortName(model.getSDistanceUnit()) + ")");
    double[] limits = MathUtils.limits(sx);
    limits = MathUtils.limits(limits, sy);
    final double rangex = (z[z.length - 1] - z[0]) * 0.05;
    final double rangey = (limits[1] - limits[0]) * 0.05;
    final double miny = limits[0] - rangey;
    final double maxy = limits[1] + rangey;
    plot.setLimits(z[0] - rangex, z[z.length - 1] + rangex, miny, maxy);
    plot.setColor(Color.RED);
    plot.addPoints(z, sx, Plot.LINE);
    plot.setColor(Color.BLUE);
    plot.addPoints(z, sy, Plot.LINE);
    plot.setColor(Color.YELLOW);
    if (pluginSettings.getShowDepthOfFocus()) {
        final double z0x = gamma;
        final double z0y = -gamma;
        plot.setColor(Color.RED.darker());
        plot.drawDottedLine(z0x - d, miny, z0x - d, maxy, 4);
        plot.drawDottedLine(z0x + d, miny, z0x + d, maxy, 4);
        plot.setColor(Color.BLUE.darker());
        plot.drawDottedLine(z0y - d, miny, z0y - d, maxy, 4);
        plot.drawDottedLine(z0y + d, miny, z0y + d, maxy, 4);
    }
    String legend = "Sx\nSy";
    if (pluginSettings.getShowCombinedWidth()) {
        final double[] s = new double[z.length];
        for (int i = 0; i < z.length; i++) {
            s[i] = Gaussian2DPeakResultHelper.getStandardDeviation(sx[i], sy[i]);
        }
        plot.setColor(Color.GREEN);
        plot.addPoints(z, s, Plot.LINE);
        legend += "\tS";
    }
    plot.setColor(Color.BLACK);
    plot.addLegend(legend);
    plot.addLabel(0, 0, String.format("Model = %s (%s nm/pixel)", name, MathUtils.rounded(model.getNmPerPixel())));
    ImageJUtils.display(title, plot);
    if (!pluginSettings.getShowPsf()) {
        return;
    }
    // Get pixel range using 3x[max SD]
    final int width = 1 + 2 * ((int) Math.ceil(limits[1] * 3));
    new ModelRenderer(name, model, m, range, width, plot).run();
}
Also used : ConversionException(uk.ac.sussex.gdsc.core.data.utils.ConversionException) AstigmatismModel(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel) Plot(ij.gui.Plot) NonBlockingExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) HoltzerAstigmatismZModel(uk.ac.sussex.gdsc.smlm.function.gaussian.HoltzerAstigmatismZModel)

Aggregations

AstigmatismModel (uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel)14 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)5 NonBlockingExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog)5 AstigmatismModelSettings (uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModelSettings)5 ConversionException (uk.ac.sussex.gdsc.core.data.utils.ConversionException)3 GenericDialog (ij.gui.GenericDialog)2 Map (java.util.Map)2 PSF (uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF)2 DistanceUnit (uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)2 AstigmatismZModel (uk.ac.sussex.gdsc.smlm.function.gaussian.AstigmatismZModel)2 HoltzerAstigmatismZModel (uk.ac.sussex.gdsc.smlm.function.gaussian.HoltzerAstigmatismZModel)2 Plot (ij.gui.Plot)1 File (java.io.File)1 Test (org.junit.jupiter.api.Test)1 ImagePSF (uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.ImagePSF)1 FitConfiguration (uk.ac.sussex.gdsc.smlm.engine.FitConfiguration)1 DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)1