Search in sources :

Example 21 with ExtendedGenericDialog

use of uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog in project GDSC-SMLM by aherbert.

the class ResequenceResults method showDialog.

private boolean showDialog() {
    settings = Settings.load();
    final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
    gd.addHelp(HelpUrls.getUrl("resequence-results"));
    gd.addMessage("Resequence the results in memory (assumed to be continuous from 1).\n" + "Describe the regular repeat of the original image:\n" + "Start = The first frame that contained the data\n" + "Block = The number of continuous frames containing data\n" + "Skip = The number of continuous frames to ignore before the next data\n \n" + "E.G. 2:9:1 = Data was imaged from frame 2 for 9 frames, 1 frame to ignore," + " then repeat.");
    ResultsManager.addInput(gd, settings.inputOption, InputSource.MEMORY);
    gd.addNumericField("Start", settings.start, 0);
    gd.addNumericField("Block", settings.block, 0);
    gd.addNumericField("Skip", settings.skip, 0);
    gd.addCheckbox("Log_mapping", settings.logMapping);
    gd.showDialog();
    if (gd.wasCanceled()) {
        return false;
    }
    settings.inputOption = ResultsManager.getInputSource(gd);
    settings.start = (int) gd.getNextNumber();
    settings.block = (int) gd.getNextNumber();
    settings.skip = (int) gd.getNextNumber();
    settings.logMapping = gd.getNextBoolean();
    settings.save();
    // Check arguments
    try {
        ParameterUtils.isAboveZero("Start", settings.start);
        ParameterUtils.isAboveZero("Block", settings.block);
        ParameterUtils.isPositive("Skip", settings.skip);
    } catch (final IllegalArgumentException ex) {
        IJ.error(TITLE, ex.getMessage());
        return false;
    }
    return true;
}
Also used : ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)

Example 22 with ExtendedGenericDialog

use of uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog in project GDSC-SMLM by aherbert.

the class PsfDrift method run.

@Override
public void run(String arg) {
    SmlmUsageTracker.recordPlugin(this.getClass(), arg);
    if ("hwhm".equals(arg)) {
        showHwhm();
        return;
    }
    // Build a list of suitable images
    final List<String> titles = createImageList(true);
    if (titles.isEmpty()) {
        IJ.error(TITLE, "No suitable PSF images");
        return;
    }
    final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
    settings = Settings.load();
    fitConfig = fitConfigRef.get().createCopy();
    gd.addMessage("Select the input PSF image");
    gd.addChoice("PSF", titles.toArray(new String[0]), settings.title);
    gd.addCheckbox("Use_offset", settings.useOffset);
    gd.addNumericField("Scale", settings.scale, 2);
    gd.addNumericField("z_depth", settings.zDepth, 2, 6, "nm");
    gd.addNumericField("Grid_size", settings.gridSize, 0);
    gd.addSlider("Recall_limit", 0.01, 1, settings.recallLimit);
    gd.addSlider("Region_size", 2, 20, settings.regionSize);
    gd.addCheckbox("Background_fitting", settings.backgroundFitting);
    PeakFit.addPsfOptions(gd, fitConfig);
    gd.addChoice("Fit_solver", SettingsManager.getFitSolverNames(), FitProtosHelper.getName(fitConfig.getFitSolver()));
    // We need these to set bounds for any bounded fitters
    gd.addSlider("Min_width_factor", 0, 0.99, fitConfig.getMinWidthFactor());
    gd.addSlider("Width_factor", 1, 4.5, fitConfig.getMaxWidthFactor());
    gd.addCheckbox("Offset_fit", settings.offsetFitting);
    gd.addNumericField("Start_offset", settings.startOffset, 3);
    gd.addCheckbox("Include_CoM_fit", settings.comFitting);
    gd.addCheckbox("Use_sampling", settings.useSampling);
    gd.addNumericField("Photons", settings.photons, 0);
    gd.addSlider("Photon_limit", 0, 1, settings.photonLimit);
    gd.addSlider("Smoothing", 0, 0.5, settings.smoothing);
    gd.addHelp(HelpUrls.getUrl("psf-drift"));
    gd.showDialog();
    if (gd.wasCanceled()) {
        return;
    }
    settings.title = gd.getNextChoice();
    settings.useOffset = gd.getNextBoolean();
    settings.scale = gd.getNextNumber();
    settings.zDepth = gd.getNextNumber();
    settings.gridSize = (int) gd.getNextNumber();
    settings.recallLimit = gd.getNextNumber();
    settings.regionSize = (int) Math.abs(gd.getNextNumber());
    settings.backgroundFitting = gd.getNextBoolean();
    fitConfig.setPsfType(PeakFit.getPsfTypeValues()[gd.getNextChoiceIndex()]);
    // Some enum values are not supported
    fitConfig.setFitSolver(SettingsManager.getFitSolverValues()[gd.getNextChoiceIndex()]);
    fitConfig.setMinWidthFactor(gd.getNextNumber());
    fitConfig.setMaxWidthFactor(gd.getNextNumber());
    settings.offsetFitting = gd.getNextBoolean();
    settings.startOffset = Math.abs(gd.getNextNumber());
    settings.comFitting = gd.getNextBoolean();
    settings.useSampling = gd.getNextBoolean();
    settings.photons = Math.abs(gd.getNextNumber());
    settings.photonLimit = Math.abs(gd.getNextNumber());
    settings.smoothing = Math.abs(gd.getNextNumber());
    settings.save();
    fitConfigRef.set(fitConfig);
    gd.collectOptions();
    if (!settings.comFitting && !settings.offsetFitting) {
        IJ.error(TITLE, "No initial fitting positions");
        return;
    }
    if (settings.regionSize < 1) {
        settings.regionSize = 1;
    }
    if (gd.invalidNumber()) {
        return;
    }
    imp = WindowManager.getImage(settings.title);
    if (imp == null) {
        IJ.error(TITLE, "No PSF image for image: " + settings.title);
        return;
    }
    psfSettings = getPsfSettings(imp);
    if (psfSettings == null) {
        IJ.error(TITLE, "No PSF settings for image: " + settings.title);
        return;
    }
    // Configure the fit solver. We must wrap the settings with a
    // FitEngineConfiguration to pass to the PeakFit method
    final FitEngineSettings fitEngineSettings = FitProtosHelper.defaultFitEngineSettings;
    final FitEngineConfiguration config = new FitEngineConfiguration(fitEngineSettings, SettingsManager.readCalibration(0), PsfProtosHelper.defaultOneAxisGaussian2DPSF);
    config.getFitConfiguration().setFitSettings(fitConfig.getFitSettings());
    if (!PeakFit.configurePsfModel(config)) {
        return;
    }
    if (!PeakFit.configureFitSolver(config, IJImageSource.getBounds(imp), null, PeakFit.FLAG_NO_SAVE)) {
        return;
    }
    // Update configuration
    fitConfig = config.getFitConfiguration();
    fitConfigRef.set(fitConfig);
    computeDrift();
}
Also used : FitEngineConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration) NonBlockingExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) FitEngineSettings(uk.ac.sussex.gdsc.smlm.data.config.FitProtos.FitEngineSettings)

Example 23 with ExtendedGenericDialog

use of uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog in project GDSC-SMLM by aherbert.

the class PeakFit method getExposureTime.

private static boolean getExposureTime(CalibrationWriter calibration) {
    final ExtendedGenericDialog gd = newWizardDialog("Enter the exposure time. Calibration of the exposure time allows correct reporting of on" + " and off times.", "This is the length of time for each frame in the image.");
    gd.addNumericField("Exposure_time", calibration.getExposureTime(), 2, 6, "ms");
    gd.showDialog();
    if (gd.wasCanceled()) {
        return false;
    }
    calibration.setExposureTime(Math.abs(gd.getNextNumber()));
    return true;
}
Also used : ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)

Example 24 with ExtendedGenericDialog

use of uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog in project GDSC-SMLM by aherbert.

the class PeakFit method getCameraType.

private static boolean getCameraType(CalibrationWriter calibration) {
    final ExtendedGenericDialog gd = newWizardDialog("Enter the type of camera.");
    gd.addChoice("Camera_type", SettingsManager.getCameraTypeNames(), CalibrationProtosHelper.getName(calibration.getCameraType()));
    gd.showDialog();
    if (gd.wasCanceled()) {
        return false;
    }
    calibration.setCameraType(SettingsManager.getCameraTypeValues()[gd.getNextChoiceIndex()]);
    if (!calibration.isCcdCamera()) {
        // TODO - Support sCMOS camera
        IJ.error("Unsupported camera type " + CalibrationProtosHelper.getName(calibration.getCameraType()));
        return false;
    }
    return true;
}
Also used : ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)

Example 25 with ExtendedGenericDialog

use of uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog in project GDSC-SMLM by aherbert.

the class PeakFit method configureFitSolver.

/**
 * Show a dialog to configure the fit solver. The updated settings are saved to the settings file.
 * An error message is shown if the dialog is cancelled or the configuration is invalid.
 *
 * <p>The bounds are used to validate the camera model. The camera model must be large enough to
 * cover the source bounds. If larger then it will be cropped. Optionally an internal region of
 * the input image can be specified. This is relative to the width and height of the input image.
 * If no camera model is present then the bounds can be null.
 *
 * @param config the configuration
 * @param sourceBounds the source image bounds (used to validate the camera model dimensions)
 * @param bounds the crop bounds (relative to the input image, used to validate the camera model
 *        dimensions)
 * @param flags the flags
 * @return True if the configuration succeeded
 */
public static boolean configureFitSolver(FitEngineConfiguration config, Rectangle sourceBounds, Rectangle bounds, int flags) {
    final boolean extraOptions = BitFlagUtils.anySet(flags, FLAG_EXTRA_OPTIONS);
    final boolean ignoreCalibration = BitFlagUtils.anySet(flags, FLAG_IGNORE_CALIBRATION);
    final boolean saveSettings = BitFlagUtils.anyNotSet(flags, FLAG_NO_SAVE);
    final FitConfiguration fitConfig = config.getFitConfiguration();
    final CalibrationWriter calibration = fitConfig.getCalibrationWriter();
    final FitSolver fitSolver = fitConfig.getFitSolver();
    final boolean isLvm = fitSolver == FitSolver.LVM_LSE || fitSolver == FitSolver.LVM_WLSE || fitSolver == FitSolver.LVM_MLE;
    // Support the deprecated backtracking FastMLE solver as a plain FastMLE solver
    final boolean isFastMml = fitSolver == FitSolver.FAST_MLE || fitSolver == FitSolver.BACKTRACKING_FAST_MLE;
    final boolean isSteppingFunctionSolver = isLvm || isFastMml;
    if (fitSolver == FitSolver.MLE) {
        final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
        if (!ignoreCalibration) {
            gd.addMessage("Maximum Likelihood Estimation requires CCD-type camera parameters");
            gd.addNumericField("Camera_bias", calibration.getBias(), 2, 6, "count");
            gd.addCheckbox("Model_camera_noise", fitConfig.isModelCamera());
            gd.addNumericField("Read_noise", calibration.getReadNoise(), 2, 6, "count");
            gd.addNumericField("Quantum_efficiency", calibration.getQuantumEfficiency(), 2, 6, "electron/photon");
            gd.addCheckbox("EM-CCD", calibration.isEmCcd());
        } else {
            gd.addMessage("Maximum Likelihood Estimation requires additional parameters");
        }
        final String[] searchNames = SettingsManager.getSearchMethodNames();
        gd.addChoice("Search_method", searchNames, FitProtosHelper.getName(fitConfig.getSearchMethod()));
        gd.addStringField("Relative_threshold", MathUtils.rounded(fitConfig.getRelativeThreshold()));
        gd.addStringField("Absolute_threshold", MathUtils.rounded(fitConfig.getAbsoluteThreshold()));
        gd.addNumericField("Max_iterations", fitConfig.getMaxIterations(), 0);
        gd.addNumericField("Max_function_evaluations", fitConfig.getMaxFunctionEvaluations(), 0);
        if (extraOptions) {
            gd.addCheckbox("Gradient_line_minimisation", fitConfig.isGradientLineMinimisation());
        }
        gd.showDialog();
        if (gd.wasCanceled()) {
            return false;
        }
        if (!ignoreCalibration) {
            calibration.setBias(Math.abs(gd.getNextNumber()));
            fitConfig.setModelCamera(gd.getNextBoolean());
            calibration.setReadNoise(Math.abs(gd.getNextNumber()));
            calibration.setQuantumEfficiency(Math.abs(gd.getNextNumber()));
            calibration.setCameraType((gd.getNextBoolean()) ? CameraType.EMCCD : CameraType.CCD);
            fitConfig.setCalibration(calibration.getCalibration());
        }
        fitConfig.setSearchMethod(SettingsManager.getSearchMethodValues()[gd.getNextChoiceIndex()]);
        fitConfig.setRelativeThreshold(getThresholdNumber(gd));
        fitConfig.setAbsoluteThreshold(getThresholdNumber(gd));
        fitConfig.setMaxIterations((int) gd.getNextNumber());
        fitConfig.setMaxFunctionEvaluations((int) gd.getNextNumber());
        if (extraOptions) {
            fitConfig.setGradientLineMinimisation(gd.getNextBoolean());
        } else {
            // This option is for the Conjugate Gradient optimiser and makes it less stable
            fitConfig.setGradientLineMinimisation(false);
        }
        if (saveSettings) {
            saveFitEngineSettings(config);
        }
        try {
            ParameterUtils.isAboveZero("Relative threshold", fitConfig.getRelativeThreshold());
            ParameterUtils.isAboveZero("Absolute threshold", fitConfig.getAbsoluteThreshold());
            ParameterUtils.isAboveZero("Max iterations", fitConfig.getMaxIterations());
            ParameterUtils.isAboveZero("Max function evaluations", fitConfig.getMaxFunctionEvaluations());
            fitConfig.getFunctionSolver();
        } catch (final IllegalArgumentException | IllegalStateException ex) {
            IJ.error(TITLE, ex.getMessage());
            return false;
        }
    } else if (isSteppingFunctionSolver) {
        final boolean requireCalibration = !ignoreCalibration && fitSolver != FitSolver.LVM_LSE;
        // Collect options for LVM fitting
        final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
        final String fitSolverName = FitProtosHelper.getName(fitSolver);
        gd.addMessage(fitSolverName + " requires additional parameters");
        gd.addStringField("Relative_threshold", MathUtils.rounded(fitConfig.getRelativeThreshold()));
        gd.addStringField("Absolute_threshold", MathUtils.rounded(fitConfig.getAbsoluteThreshold()));
        gd.addStringField("Parameter_relative_threshold", MathUtils.rounded(fitConfig.getParameterRelativeThreshold()));
        gd.addStringField("Parameter_absolute_threshold", MathUtils.rounded(fitConfig.getParameterAbsoluteThreshold()));
        gd.addNumericField("Max_iterations", fitConfig.getMaxIterations(), 0);
        if (isLvm) {
            gd.addNumericField("Lambda", fitConfig.getLambda(), 4);
        }
        if (isFastMml) {
            gd.addCheckbox("Fixed_iterations", fitConfig.isFixedIterations());
            // This works because the proto configuration enum matches the named enum
            final String[] lineSearchNames = SettingsManager.getNames((Object[]) FastMleSteppingFunctionSolver.LineSearchMethod.values());
            gd.addChoice("Line_search_method", lineSearchNames, lineSearchNames[fitConfig.getLineSearchMethod().getNumber()]);
        }
        gd.addCheckbox("Use_clamping", fitConfig.isUseClamping());
        gd.addCheckbox("Dynamic_clamping", fitConfig.isUseDynamicClamping());
        final PSF psf = fitConfig.getPsf();
        final boolean isAstigmatism = psf.getPsfType() == PSFType.ASTIGMATIC_GAUSSIAN_2D;
        final int nParams = PsfHelper.getParameterCount(psf);
        if (extraOptions) {
            gd.addNumericField("Clamp_background", fitConfig.getClampBackground(), 2);
            gd.addNumericField("Clamp_signal", fitConfig.getClampSignal(), 2);
            gd.addNumericField("Clamp_x", fitConfig.getClampX(), 2);
            gd.addNumericField("Clamp_y", fitConfig.getClampY(), 2);
            if (isAstigmatism) {
                gd.addNumericField("Clamp_z", fitConfig.getClampZ(), 2);
            } else {
                if (nParams > 1 || !fitConfig.isFixedPsf()) {
                    gd.addNumericField("Clamp_sx", fitConfig.getClampXSd(), 2);
                }
                if (nParams > 1) {
                    gd.addNumericField("Clamp_sy", fitConfig.getClampYSd(), 2);
                }
                if (nParams > 2) {
                    gd.addNumericField("Clamp_angle", fitConfig.getClampAngle(), 2);
                }
            }
        }
        // Extra parameters are needed for calibrated fit solvers
        if (requireCalibration) {
            switch(calibration.getCameraType()) {
                case CCD:
                case EMCCD:
                case SCMOS:
                    break;
                default:
                    IJ.error(TITLE, fitSolverName + " requires camera calibration");
                    return false;
            }
            gd.addMessage(fitSolverName + " requires calibration for camera: " + CalibrationProtosHelper.getName(calibration.getCameraType()));
            if (calibration.isScmos()) {
                final String[] models = CameraModelManager.listCameraModels(true);
                gd.addChoice("Camera_model_name", models, fitConfig.getCameraModelName());
            } else {
                gd.addNumericField("Camera_bias", calibration.getBias(), 2, 6, "Count");
                gd.addNumericField("Gain", calibration.getCountPerPhoton(), 2, 6, "Count/photon");
                gd.addNumericField("Read_noise", calibration.getReadNoise(), 2, 6, "Count");
            }
        }
        gd.showDialog();
        if (gd.wasCanceled()) {
            return false;
        }
        fitConfig.setRelativeThreshold(getThresholdNumber(gd));
        fitConfig.setAbsoluteThreshold(getThresholdNumber(gd));
        fitConfig.setParameterRelativeThreshold(getThresholdNumber(gd));
        fitConfig.setParameterAbsoluteThreshold(getThresholdNumber(gd));
        fitConfig.setMaxIterations((int) gd.getNextNumber());
        if (isLvm) {
            fitConfig.setLambda(gd.getNextNumber());
        }
        if (isFastMml) {
            fitConfig.setFixedIterations(gd.getNextBoolean());
            fitConfig.setLineSearchMethod(gd.getNextChoiceIndex());
        }
        fitConfig.setUseClamping(gd.getNextBoolean());
        fitConfig.setUseDynamicClamping(gd.getNextBoolean());
        if (extraOptions) {
            fitConfig.setClampBackground(Math.abs(gd.getNextNumber()));
            fitConfig.setClampSignal(Math.abs(gd.getNextNumber()));
            fitConfig.setClampX(Math.abs(gd.getNextNumber()));
            fitConfig.setClampY(Math.abs(gd.getNextNumber()));
            if (isAstigmatism) {
                fitConfig.setClampZ(Math.abs(gd.getNextNumber()));
            } else {
                if (nParams > 1 || !fitConfig.isFixedPsf()) {
                    fitConfig.setClampXSd(Math.abs(gd.getNextNumber()));
                }
                if (nParams > 1) {
                    fitConfig.setClampYSd(Math.abs(gd.getNextNumber()));
                }
                if (nParams > 2) {
                    fitConfig.setClampAngle(Math.abs(gd.getNextNumber()));
                }
            }
        }
        if (requireCalibration) {
            if (calibration.isScmos()) {
                fitConfig.setCameraModelName(gd.getNextChoice());
            } else {
                calibration.setBias(Math.abs(gd.getNextNumber()));
                calibration.setCountPerPhoton(Math.abs(gd.getNextNumber()));
                calibration.setReadNoise(Math.abs(gd.getNextNumber()));
                fitConfig.setCalibration(calibration.getCalibration());
            }
        }
        // camera model is set.
        if (calibration.isScmos()) {
            fitConfig.setCameraModel(CameraModelManager.load(fitConfig.getCameraModelName()));
            if (!checkCameraModel(fitConfig, sourceBounds, bounds, true)) {
                return false;
            }
        }
        if (saveSettings) {
            saveFitEngineSettings(config);
        }
        try {
            if (isLvm) {
                ParameterUtils.isAboveZero("Lambda", fitConfig.getLambda());
            }
            // This call will check if the configuration is OK (including convergence criteria)
            fitConfig.getFunctionSolver();
        } catch (final IllegalArgumentException | IllegalStateException ex) {
            IJ.error(TITLE, ex.getMessage());
            return false;
        }
    } else {
        IJ.error(TITLE, "Unknown fit solver: " + fitSolver);
        return false;
    }
    if (config.isIncludeNeighbours() && !fitConfig.getFunctionSolver().isBounded()) {
        IJ.error(TITLE, "Including neighbours requires a bounded fit solver");
        return false;
    }
    return true;
}
Also used : FitSolver(uk.ac.sussex.gdsc.smlm.data.config.FitProtos.FitSolver) PSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF) FitConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitConfiguration) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)

Aggregations

ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)151 NonBlockingExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog)38 CalibrationWriter (uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter)21 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)14 Checkbox (java.awt.Checkbox)13 ImagePlus (ij.ImagePlus)12 File (java.io.File)11 Rectangle (java.awt.Rectangle)10 TextField (java.awt.TextField)10 ResultsImageSettings (uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsImageSettings)10 FitConfiguration (uk.ac.sussex.gdsc.smlm.engine.FitConfiguration)10 Choice (java.awt.Choice)9 ArrayList (java.util.ArrayList)9 DistanceUnit (uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)9 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)8 CalibrationReader (uk.ac.sussex.gdsc.smlm.data.config.CalibrationReader)7 ResultsSettings (uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsSettings)7 ResultsTableSettings (uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsTableSettings)7 IJ (ij.IJ)6 GenericDialog (ij.gui.GenericDialog)5