Search in sources :

Example 21 with FitConfiguration

use of uk.ac.sussex.gdsc.smlm.engine.FitConfiguration in project GDSC-SMLM by aherbert.

the class GaussianFit method createGaussianFitter.

private Gaussian2DFitter createGaussianFitter(boolean simpleFiltering) {
    final FitConfiguration config = new FitConfiguration();
    config.setFitSolver(FitSolver.LVM_LSE);
    config.setPsf(PsfProtosHelper.getDefaultPsf(getPsfType()));
    config.setMaxIterations(getMaxIterations());
    config.setRelativeThreshold(settings.relativeThreshold);
    config.setAbsoluteThreshold(settings.absoluteThreshold);
    config.setInitialPeakStdDev(getInitialPeakStdDev());
    config.setComputeDeviations(settings.showDeviations);
    // Set-up peak filtering only for single fitting
    config.setDisableSimpleFilter(!simpleFiltering);
    setupPeakFiltering(config);
    if (isLogProgress()) {
        config.setLog(ImageJPluginLoggerHelper.getLogger(getClass()));
    }
    config.setBackgroundFitting(settings.fitBackground);
    return new Gaussian2DFitter(config);
}
Also used : FitConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitConfiguration) Gaussian2DFitter(uk.ac.sussex.gdsc.smlm.fitting.Gaussian2DFitter)

Example 22 with FitConfiguration

use of uk.ac.sussex.gdsc.smlm.engine.FitConfiguration in project GDSC-SMLM by aherbert.

the class DoubletAnalysis method showAnalysisDialog.

/**
 * Show analysis dialog.
 *
 * @return true, if successful
 */
private boolean showAnalysisDialog() {
    final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
    gd.addHelp(HelpUrls.getUrl("doublet-filter-analysis"));
    settings = Settings.load();
    config = configRef.get().createCopy();
    final StringBuilder sb = new StringBuilder("Filters the doublet fits and reports the performance increase\n");
    // Show the fitting settings that will effect filters, i.e. fit standard deviation, fit width
    final FitConfiguration fitConfig = config.getFitConfiguration();
    sb.append("SD0 = ").append(MathUtils.rounded(fitConfig.getInitialXSd())).append("\n");
    // sb.append("SD1 =
    // ").append(MathUtils.rounded(fitConfig.getInitialPeakStdDev1())).append("\n");
    sb.append("Fit Width = ").append(config.getFittingWidth()).append("\n");
    gd.addMessage(sb.toString());
    // Collect options for filtering
    gd.addChoice("Selection_Criteria", Settings.SELECTION_CRITERIAS, settings.selectionCriteria);
    filterFitConfig = filterFitConfigRef.get().createCopy();
    // Copy the settings used when fitting
    filterFitConfig.setCalibration(fitConfig.getCalibration());
    filterFitConfig.setPsf(fitConfig.getPsf());
    filterFitConfig.setFitSolverSettings(fitConfig.getFitSolverSettings());
    final String[] templates = ConfigurationTemplate.getTemplateNames(true);
    gd.addChoice("Template", templates, templates[0]);
    // Allow the settings from the benchmark analysis to be used
    gd.addCheckbox("Benchmark_settings", settings.analysisUseBenchmarkSettings);
    gd.addCheckbox("Smart_filter", filterFitConfig.isSmartFilter());
    gd.addSlider("Shift_factor", 0.01, 2, filterFitConfig.getCoordinateShiftFactor());
    gd.addNumericField("Signal_strength", filterFitConfig.getSignalStrength(), 2);
    gd.addNumericField("Min_photons", filterFitConfig.getMinPhotons(), 0);
    gd.addSlider("Min_width_factor", 0, 0.99, filterFitConfig.getMinWidthFactor());
    gd.addSlider("Max_width_factor", 1.01, 5, filterFitConfig.getMaxWidthFactor());
    gd.addNumericField("Precision", filterFitConfig.getPrecisionThreshold(), 2);
    gd.addChoice("Precision_method", SettingsManager.getPrecisionMethodNames(), filterFitConfig.getPrecisionMethod().ordinal());
    gd.addNumericField("Drift_angle", settings.analysisDriftAngle, 2);
    gd.addNumericField("Min_gap", settings.minGap, 2);
    // Collect display options
    gd.addCheckbox("Show_results", settings.analysisShowResults);
    gd.addCheckbox("Show_Jaccard_Plot", settings.showJaccardPlot);
    gd.addCheckbox("Use_max_residuals", settings.useMaxResiduals);
    gd.addCheckbox("Logging", settings.analysisLogging);
    gd.addStringField("Title", settings.analysisTitle);
    gd.addCheckbox("Save_template", settings.saveTemplate);
    // Add a mouse listener to the config file field
    if (ImageJUtils.isShowGenericDialog()) {
        final Vector<TextField> numerics = gd.getNumericFields();
        final Vector<Checkbox> checkboxes = gd.getCheckboxes();
        final Vector<Choice> choices = gd.getChoices();
        choices.get(1).addItemListener(this);
        checkboxes.get(0).addItemListener(this);
        cbSmartFilter = checkboxes.get(1);
        final Iterator<TextField> nu = numerics.iterator();
        textCoordinateShiftFactor = nu.next();
        textSignalStrength = nu.next();
        textMinPhotons = nu.next();
        textMinWidthFactor = nu.next();
        textWidthFactor = nu.next();
        textPrecisionThreshold = nu.next();
        textPrecisionMethod = choices.get(2);
    }
    gd.addHelp(HelpUrls.getUrl("doublet-filter-analysis"));
    gd.showDialog();
    if (gd.wasCanceled()) {
        return false;
    }
    if (gd.invalidNumber()) {
        return false;
    }
    settings.selectionCriteria = gd.getNextChoiceIndex();
    // Ignore the template
    gd.getNextChoice();
    settings.analysisUseBenchmarkSettings = gd.getNextBoolean();
    filterFitConfig.setSmartFilter(gd.getNextBoolean());
    filterFitConfig.setCoordinateShiftFactor(gd.getNextNumber());
    filterFitConfig.setSignalStrength(gd.getNextNumber());
    filterFitConfig.setMinPhotons(gd.getNextNumber());
    filterFitConfig.setMinWidthFactor(gd.getNextNumber());
    filterFitConfig.setMaxWidthFactor(gd.getNextNumber());
    filterFitConfig.setPrecisionThreshold(gd.getNextNumber());
    filterFitConfig.setPrecisionMethod(gd.getNextChoiceIndex());
    settings.analysisDriftAngle = gd.getNextNumber();
    settings.minGap = gd.getNextNumber();
    settings.analysisShowResults = gd.getNextBoolean();
    settings.showJaccardPlot = gd.getNextBoolean();
    settings.useMaxResiduals = gd.getNextBoolean();
    settings.analysisLogging = gd.getNextBoolean();
    settings.analysisTitle = gd.getNextString();
    settings.saveTemplate = gd.getNextBoolean();
    settings.save();
    filterFitConfigRef.set(filterFitConfig);
    if (gd.invalidNumber()) {
        return false;
    }
    if (settings.analysisUseBenchmarkSettings) {
        return updateFilterConfiguration(filterFitConfig);
    } else if (filterFitConfig.isSmartFilter()) {
        return PeakFit.configureSmartFilter(filterFitConfig);
    }
    return true;
}
Also used : Choice(java.awt.Choice) FitConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitConfiguration) Checkbox(java.awt.Checkbox) TextField(java.awt.TextField) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)

Example 23 with FitConfiguration

use of uk.ac.sussex.gdsc.smlm.engine.FitConfiguration in project GDSC-SMLM by aherbert.

the class DoubletAnalysis method updateFitConfiguration.

private boolean updateFitConfiguration(FitEngineConfiguration config) {
    // Do this first as it sets the initial SD
    if (!BenchmarkSpotFit.updateConfiguration(config)) {
        IJ.error(TITLE, "Unable to use the benchmark spot fit configuration");
        return false;
    }
    final FitConfiguration fitConfig = config.getFitConfiguration();
    final CalibrationWriter cal = new CalibrationWriter(fitConfig.getCalibration());
    cal.setNmPerPixel(simulationParameters.pixelPitch);
    cal.setCountPerPhoton(simulationParameters.gain);
    cal.setQuantumEfficiency(simulationParameters.qe);
    cal.setExposureTime(100);
    cal.setReadNoise(simulationParameters.readNoise);
    cal.setBias(simulationParameters.bias);
    cal.setCameraType(simulationParameters.cameraType);
    fitConfig.setCalibration(cal.getCalibration());
    if (!BenchmarkSpotFilter.updateConfiguration(config)) {
        IJ.error(TITLE, "Unable to use the benchmark spot filter configuration");
        return false;
    }
    // Make sure all spots are fit
    config.setFailuresLimit(-1);
    // same as the filter analysis
    if (BenchmarkFilterAnalysis.getDistanceInPixels() > 0) {
        settings.matchDistance = BenchmarkFilterAnalysis.getDistanceInPixels();
        settings.lowerDistance = BenchmarkFilterAnalysis.getLowerDistanceInPixels();
        settings.signalFactor = BenchmarkFilterAnalysis.getSignalFactor();
        settings.lowerSignalFactor = BenchmarkFilterAnalysis.getLowerSignalFactor();
    } else {
        // Use the fit analysis distance if no filter analysis has been run
        settings.matchDistance = BenchmarkSpotFit.getDistanceInPixels();
        settings.lowerDistance = BenchmarkSpotFit.getLowerDistanceInPixels();
        settings.signalFactor = settings.lowerSignalFactor = BenchmarkSpotFit.getSignalFactor();
    }
    return true;
}
Also used : FitConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitConfiguration) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter)

Example 24 with FitConfiguration

use of uk.ac.sussex.gdsc.smlm.engine.FitConfiguration in project GDSC-SMLM by aherbert.

the class DoubletAnalysis method saveTemplate.

/**
 * Save PeakFit configuration template using the current benchmark settings.
 *
 * @param summary the summary
 */
private void saveTemplate(String summary) {
    if (!settings.saveTemplate) {
        return;
    }
    // Start with a clone of the filter settings
    final FitEngineConfiguration config = new FitEngineConfiguration();
    final FitConfiguration fitConfig = config.getFitConfiguration();
    fitConfig.setFitSettings(filterFitConfig.getFitSettings());
    // Copy settings used during fitting
    updateConfiguration(config);
    // Remove the PSF width to make the template generic
    fitConfig.setInitialPeakStdDev(0);
    fitConfig.setNmPerPixel(0);
    fitConfig.setGain(0);
    fitConfig.setNoise(0);
    // This was done fitting all the results
    config.setFailuresLimit(-1);
    if (settings.useBenchmarkSettings) {
        final FitEngineConfiguration pConfig = new FitEngineConfiguration();
        // TODO - add option to use latest or the best
        if (BenchmarkFilterAnalysis.updateConfiguration(pConfig, false)) {
            config.setFailuresLimit(pConfig.getFailuresLimit());
        }
    }
    // Set the residuals
    fitConfig.setComputeResiduals(true);
    // TODO - make the choice of the best residuals configurable
    config.setResidualsThreshold(residualsScore.bestResiduals[2]);
    final String filename = BenchmarkFilterAnalysis.getFilename("Template_File", settings.templateFilename);
    if (filename != null) {
        settings.templateFilename = filename;
        final TemplateSettings.Builder settings = TemplateSettings.newBuilder();
        getNotes(settings, summary);
        settings.setFitEngineSettings(config.getFitEngineSettings());
        if (!SettingsManager.toJson(settings.build(), filename, SettingsManager.FLAG_SILENT)) {
            IJ.log("Unable to save the template configuration");
        }
    }
}
Also used : TemplateSettings(uk.ac.sussex.gdsc.smlm.data.config.TemplateProtos.TemplateSettings) FitEngineConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration) FitConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitConfiguration)

Example 25 with FitConfiguration

use of uk.ac.sussex.gdsc.smlm.engine.FitConfiguration in project GDSC-SMLM by aherbert.

the class PsfEstimator method readDialog.

private boolean readDialog(ExtendedGenericDialog gd) {
    initialPeakStdDev0 = gd.getNextNumber();
    initialPeakStdDev1 = gd.getNextNumber();
    initialPeakAngle = gd.getNextNumber();
    settings.setNumberOfPeaks((int) gd.getNextNumber());
    settings.setPValue(gd.getNextNumber());
    settings.setUpdatePreferences(gd.getNextBoolean());
    settings.setDebugPsfEstimator(gd.getNextBoolean());
    settings.setIterate(gd.getNextBoolean());
    settings.setShowHistograms(gd.getNextBoolean());
    settings.setHistogramBins((int) gd.getNextNumber());
    final FitConfiguration fitConfig = config.getFitConfiguration();
    final CalibrationWriter calibration = fitConfig.getCalibrationWriter();
    calibration.setCameraType(SettingsManager.getCameraTypeValues()[gd.getNextChoiceIndex()]);
    fitConfig.setCalibration(calibration.getCalibration());
    fitConfig.setPsfType(PeakFit.getPsfTypeValues()[gd.getNextChoiceIndex()]);
    config.setDataFilterType(gd.getNextChoiceIndex());
    config.setDataFilter(gd.getNextChoiceIndex(), Math.abs(gd.getNextNumber()), false, 0);
    config.setSearch(gd.getNextNumber());
    config.setBorder(gd.getNextNumber());
    config.setFitting(gd.getNextNumber());
    if (extraOptions) {
        interlacedData = pluginSettings.interlacedData = gd.getNextBoolean();
        integrateFrames = pluginSettings.integrateFrames = (int) gd.getNextNumber();
    }
    // Some enum values are not supported
    fitConfig.setFitSolver(SettingsManager.getFitSolverValues()[gd.getNextChoiceIndex()]);
    config.setFailuresLimit((int) gd.getNextNumber());
    config.setPassRate(gd.getNextNumber());
    config.setIncludeNeighbours(gd.getNextBoolean());
    config.setNeighbourHeightThreshold(gd.getNextNumber());
    config.setResidualsThreshold(gd.getNextNumber());
    fitConfig.setSmartFilter(gd.getNextBoolean());
    fitConfig.setDisableSimpleFilter(gd.getNextBoolean());
    fitConfig.setCoordinateShiftFactor(gd.getNextNumber());
    fitConfig.setSignalStrength(gd.getNextNumber());
    fitConfig.setMinPhotons(gd.getNextNumber());
    fitConfig.setMinWidthFactor(gd.getNextNumber());
    fitConfig.setMaxWidthFactor(gd.getNextNumber());
    fitConfig.setPrecisionThreshold(gd.getNextNumber());
    gd.collectOptions();
    if (gd.invalidNumber()) {
        return false;
    }
    // Check arguments
    try {
        ParameterUtils.isAboveZero("Initial SD0", initialPeakStdDev0);
        ParameterUtils.isAboveZero("Initial SD1", initialPeakStdDev1);
        ParameterUtils.isPositive("Initial angle", initialPeakAngle);
        ParameterUtils.isPositive("Number of peaks", settings.getNumberOfPeaks());
        ParameterUtils.isAboveZero("P-value", settings.getPValue());
        ParameterUtils.isEqualOrBelow("P-value", settings.getPValue(), 0.5);
        if (settings.getShowHistograms()) {
            ParameterUtils.isPositive("Histogram bins", settings.getHistogramBins());
        }
        ParameterUtils.isAboveZero("Search width", config.getSearch());
        ParameterUtils.isAboveZero("Fitting width", config.getFitting());
        // Can be negative to disable
        // Parameters.isPositive("Failures limit", config.getFailuresLimit());
        ParameterUtils.isPositive("Neighbour height threshold", config.getNeighbourHeightThreshold());
        ParameterUtils.isPositive("Residuals threshold", config.getResidualsThreshold());
        ParameterUtils.isPositive("Coordinate Shift factor", fitConfig.getCoordinateShiftFactor());
        ParameterUtils.isPositive("Signal strength", fitConfig.getSignalStrength());
        ParameterUtils.isPositive("Min photons", fitConfig.getMinPhotons());
        ParameterUtils.isPositive("Min width factor", fitConfig.getMinWidthFactor());
        ParameterUtils.isPositive("Width factor", fitConfig.getMaxWidthFactor());
    } catch (final IllegalArgumentException ex) {
        IJ.error(TITLE, ex.getMessage());
        return false;
    }
    if (fitConfig.getPsfType() == PSFType.ONE_AXIS_GAUSSIAN_2D && fitConfig.isFixedPsf()) {
        final String msg = "ERROR: A width-fitting function must be selected (i.e. not fixed-width fitting)";
        IJ.error(TITLE, msg);
        log(msg);
        return false;
    }
    SettingsManager.writeSettings(config, 0);
    if (!PeakFit.configureSmartFilter(config, 0)) {
        return false;
    }
    if (!PeakFit.configureDataFilter(config, 0)) {
        return false;
    }
    if (!PeakFit.configureFitSolver(config, IJImageSource.getBounds(imp), null, 0)) {
        return false;
    }
    // Extra parameters are needed for interlaced data
    if (interlacedData) {
        gd = new ExtendedGenericDialog(TITLE);
        gd.addMessage("Interlaced data requires a repeating pattern of frames to process.\n" + "Describe the regular repeat of the data:\n \n" + "Start = The first frame that contains 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.");
        gd.addNumericField("Start", pluginSettings.dataStart, 0);
        gd.addNumericField("Block", pluginSettings.dataBlock, 0);
        gd.addNumericField("Skip", pluginSettings.dataSkip, 0);
        gd.showDialog();
        if (gd.wasCanceled()) {
            return false;
        }
        if (!gd.wasCanceled()) {
            dataStart = (int) gd.getNextNumber();
            dataBlock = (int) gd.getNextNumber();
            dataSkip = (int) gd.getNextNumber();
            if (dataStart > 0 && dataBlock > 0 && dataSkip > 0) {
                // Store options for next time
                pluginSettings.interlacedData = true;
                pluginSettings.dataStart = dataStart;
                pluginSettings.dataBlock = dataBlock;
                pluginSettings.dataSkip = dataSkip;
                pluginSettings.save();
            }
        } else {
            interlacedData = false;
        }
    }
    return true;
}
Also used : 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

FitConfiguration (uk.ac.sussex.gdsc.smlm.engine.FitConfiguration)32 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)11 FitEngineConfiguration (uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration)9 BasePoint (uk.ac.sussex.gdsc.core.match.BasePoint)8 PeakResultPoint (uk.ac.sussex.gdsc.smlm.results.PeakResultPoint)7 Checkbox (java.awt.Checkbox)6 Rectangle (java.awt.Rectangle)5 CalibrationWriter (uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter)5 ArrayList (java.util.ArrayList)4 ImageStack (ij.ImageStack)3 TextField (java.awt.TextField)3 ConcurrentRuntimeException (org.apache.commons.lang3.concurrent.ConcurrentRuntimeException)3 StoredDataStatistics (uk.ac.sussex.gdsc.core.utils.StoredDataStatistics)3 PSF (uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF)3 BenchmarkSpotFilterResult (uk.ac.sussex.gdsc.smlm.ij.plugins.benchmark.BenchmarkSpotFilter.BenchmarkSpotFilterResult)3 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)3 TIntObjectHashMap (gnu.trove.map.hash.TIntObjectHashMap)2 Choice (java.awt.Choice)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2