Search in sources :

Example 1 with FitEngineConfiguration

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

the class BenchmarkSpotFit method showDialog.

private boolean showDialog() {
    ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
    gd.addHelp(HelpUrls.getUrl("fit-spot-data"));
    ImageJUtils.addMessage(gd, "Fit candidate spots in the benchmark image created by " + CreateData.TITLE + " plugin\nand identified by the " + BenchmarkSpotFilter.TITLE + " plugin.\nPSF width = %s nm (Square pixel adjustment = %s nm)\n \n" + "Configure the fitting:", MathUtils.rounded(simulationParameters.sd), MathUtils.rounded(getSa()));
    gd.addSlider("Fraction_positives", 50, 100, settings.fractionPositives);
    gd.addSlider("Fraction_negatives_after_positives", 0, 100, settings.fractionNegativesAfterAllPositives);
    gd.addSlider("Min_negatives_after_positives", 0, 30, settings.negativesAfterAllPositives);
    gd.addSlider("Match_distance", 0.5, 3.5, settings.distance);
    gd.addSlider("Lower_distance", 0, 3.5, settings.lowerDistance);
    gd.addSlider("Match_signal", 0, 3.5, settings.signalFactor);
    gd.addSlider("Lower_signal", 0, 3.5, settings.lowerSignalFactor);
    final FitEngineConfigurationProvider fitEngineConfigurationProvider = new PeakFit.SimpleFitEngineConfigurationProvider(config);
    // Collect options for fitting
    final double sa = getSa();
    final FitConfiguration fitConfig = config.getFitConfiguration();
    fitConfig.setInitialPeakStdDev(MathUtils.round(sa / simulationParameters.pixelPitch));
    PeakFit.addPsfOptions(gd, fitConfig);
    PeakFit.addFittingOptions(gd, fitEngineConfigurationProvider);
    gd.addChoice("Fit_solver", SettingsManager.getFitSolverNames(), FitProtosHelper.getName(fitConfig.getFitSolver()));
    gd.addMessage("Multi-path filter (used to pick optimum results during fitting)");
    // Allow loading the best filter for these results
    final boolean benchmarkSettingsCheckbox = BenchmarkSpotFitResult.fitResultsId.get() == BenchmarkFilterAnalysis.getLastFittingId();
    Checkbox cbBenchmark = null;
    if (benchmarkSettingsCheckbox) {
        // This should always be an opt-in decision. Otherwise the user cannot use the previous
        // settings.
        cbBenchmark = gd.addAndGetCheckbox("Benchmark_settings", false);
    }
    gd.addTextAreas(XmlUtils.convertQuotes(multiFilter.toXml()), null, 6, 60);
    textFailLimit = gd.addAndGetNumericField("Fail_limit", config.getFailuresLimit(), 0);
    cbIncludeNeighbours = gd.addAndGetCheckbox("Include_neighbours", config.isIncludeNeighbours());
    gd.addAndGetSlider("Neighbour_height", 0.01, 1, config.getNeighbourHeightThreshold());
    textNeighbourHeight = gd.getLastTextField();
    cbComputeDoublets = gd.addAndGetCheckbox("Compute_doublets", settings.computeDoublets);
    PeakFit.addDuplicateDistanceOptions(gd, fitEngineConfigurationProvider);
    gd.addCheckbox("Show_score_histograms", settings.showFilterScoreHistograms);
    gd.addCheckbox("Show_correlation", settings.showCorrelation);
    gd.addCheckbox("Plot_rank_by_intensity", settings.rankByIntensity);
    gd.addCheckbox("Save_filter_range", settings.saveFilterRange);
    if (extraOptions) {
    // No extra options
    }
    // Add a mouse listener to the config file field
    if (cbBenchmark != null && ImageJUtils.isShowGenericDialog()) {
        taFilterXml = gd.getTextArea1();
        cbBenchmark.addItemListener(this);
    }
    gd.showDialog();
    if (gd.wasCanceled()) {
        return false;
    }
    settings.fractionPositives = Math.abs(gd.getNextNumber());
    settings.fractionNegativesAfterAllPositives = Math.abs(gd.getNextNumber());
    settings.negativesAfterAllPositives = (int) Math.abs(gd.getNextNumber());
    settings.distance = Math.abs(gd.getNextNumber());
    settings.lowerDistance = Math.abs(gd.getNextNumber());
    settings.signalFactor = Math.abs(gd.getNextNumber());
    settings.lowerSignalFactor = Math.abs(gd.getNextNumber());
    fitConfig.setPsfType(PeakFit.getPsfTypeValues()[gd.getNextChoiceIndex()]);
    config.setFitting(gd.getNextNumber());
    // Some enum values are not supported
    fitConfig.setFitSolver(SettingsManager.getFitSolverValues()[gd.getNextChoiceIndex()]);
    boolean myUseBenchmarkSettings = false;
    if (benchmarkSettingsCheckbox) {
        // useBenchmarkSettings =
        myUseBenchmarkSettings = gd.getNextBoolean();
    }
    // Read dialog settings
    final String xml = gd.getNextText();
    final int failLimit = (int) gd.getNextNumber();
    final boolean includeNeighbours = gd.getNextBoolean();
    final double neighbourHeightThreshold = gd.getNextNumber();
    final boolean myComputeDoublets = gd.getNextBoolean();
    final double myDuplicateDistance = gd.getNextNumber();
    gd.collectOptions();
    MultiPathFilter myMultiFilter = null;
    if (myUseBenchmarkSettings && !ImageJUtils.isShowGenericDialog()) {
        // Only copy the benchmark settings if not interactive
        final FitEngineConfiguration tmp = new FitEngineConfiguration();
        final FitConfiguration tmpFitConfig = tmp.getFitConfiguration();
        // Collect the residuals threshold
        tmpFitConfig.setComputeResiduals(true);
        if (BenchmarkFilterAnalysis.updateConfiguration(tmp, false)) {
            config.setFailuresLimit(tmp.getFailuresLimit());
            config.setIncludeNeighbours(tmp.isIncludeNeighbours());
            config.setNeighbourHeightThreshold(tmp.getNeighbourHeightThreshold());
            settings.computeDoublets = (tmp.getResidualsThreshold() < 1);
            config.setDuplicateDistance(tmp.getDuplicateDistance());
            config.setDuplicateDistanceAbsolute(tmp.getDuplicateDistanceAbsolute());
            final DirectFilter primaryFilter = tmpFitConfig.getSmartFilter();
            final double residualsThreshold = tmp.getResidualsThreshold();
            myMultiFilter = new MultiPathFilter(primaryFilter, FitWorker.createMinimalFilter(tmpFitConfig.getFilterPrecisionMethod()), residualsThreshold);
        }
    } else {
        myMultiFilter = MultiPathFilter.fromXml(xml);
        config.setFailuresLimit(failLimit);
        config.setIncludeNeighbours(includeNeighbours);
        config.setNeighbourHeightThreshold(neighbourHeightThreshold);
        settings.computeDoublets = myComputeDoublets;
        config.setDuplicateDistance(myDuplicateDistance);
    }
    if (myMultiFilter == null) {
        gd = new ExtendedGenericDialog(TITLE);
        gd.addMessage("The multi-path filter was invalid.\n \nContinue with a default filter?");
        gd.enableYesNoCancel();
        gd.hideCancelButton();
        gd.showDialog();
        if (!gd.wasOKed()) {
            return false;
        }
    } else {
        multiFilter = myMultiFilter;
    }
    if (settings.computeDoublets) {
        config.setResidualsThreshold(0);
        fitConfig.setComputeResiduals(true);
    } else {
        config.setResidualsThreshold(1);
        fitConfig.setComputeResiduals(false);
    }
    settings.showFilterScoreHistograms = gd.getNextBoolean();
    settings.showCorrelation = gd.getNextBoolean();
    settings.rankByIntensity = gd.getNextBoolean();
    settings.saveFilterRange = gd.getNextBoolean();
    // Avoid stupidness, i.e. things that move outside the fit window and are bad widths
    // TODO - Fix this for simple or smart filter...
    fitConfig.setDisableSimpleFilter(false);
    // Realistically we cannot fit lower than this
    fitConfig.setMinPhotons(15);
    // Disable shift as candidates may be re-mapped to alternative candidates so the initial
    // position is wrong.
    fitConfig.setCoordinateShiftFactor(0);
    fitConfig.setMinWidthFactor(1.0 / 5);
    fitConfig.setMaxWidthFactor(5);
    // Disable the direct filter
    fitConfig.setDirectFilter(null);
    if (extraOptions) {
    // No extra options
    }
    if (gd.invalidNumber()) {
        return false;
    }
    if (settings.lowerDistance > settings.distance) {
        settings.lowerDistance = settings.distance;
    }
    if (settings.lowerSignalFactor > settings.signalFactor) {
        settings.lowerSignalFactor = settings.signalFactor;
    }
    // Distances relative to sa (not s) as this is the same as the BenchmarkSpotFilter plugin
    distanceInPixels = settings.distance * sa / simulationParameters.pixelPitch;
    lowerDistanceInPixels = settings.lowerDistance * sa / simulationParameters.pixelPitch;
    // Copy simulation defaults if a new simulation.
    // if (lastSimulationId.get() != simulationParameters.id) {
    // This is needed to configure the fit solver.
    fitConfig.setNmPerPixel(simulationParameters.pixelPitch);
    fitConfig.setGain(simulationParameters.gain);
    fitConfig.setQuantumEfficiency(simulationParameters.qe);
    fitConfig.setReadNoise(simulationParameters.readNoise);
    fitConfig.setBias(simulationParameters.bias);
    fitConfig.setCameraType(simulationParameters.cameraType);
    fitConfig.setCameraModel(CreateData.getCameraModel(simulationParameters));
    // }
    if (!PeakFit.configurePsfModel(config)) {
        return false;
    }
    return PeakFit.configureFitSolver(config, IJImageSource.getBounds(imp), null, (extraOptions) ? PeakFit.FLAG_EXTRA_OPTIONS : 0);
}
Also used : FitEngineConfigurationProvider(uk.ac.sussex.gdsc.smlm.ij.plugins.PeakFit.FitEngineConfigurationProvider) FitConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitConfiguration) Checkbox(java.awt.Checkbox) FitEngineConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration) DirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter) MultiPathFilter(uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFilter) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) PeakResultPoint(uk.ac.sussex.gdsc.smlm.results.PeakResultPoint) BasePoint(uk.ac.sussex.gdsc.core.match.BasePoint)

Example 2 with FitEngineConfiguration

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

the class BenchmarkSpotFit method itemStateChanged.

@Override
public void itemStateChanged(ItemEvent event) {
    if (event.getSource() instanceof Checkbox) {
        final Checkbox checkbox = (Checkbox) event.getSource();
        int failLimit;
        boolean includeNeighbours;
        double neighbourHeightThrehsold;
        boolean computeDoublets;
        MultiPathFilter myMultiFilter;
        if (checkbox.getState()) {
            final FitEngineConfiguration tmp = new FitEngineConfiguration();
            final FitConfiguration tmpFitConfig = tmp.getFitConfiguration();
            // Collect residuals threshold
            tmpFitConfig.setComputeResiduals(true);
            if (BenchmarkFilterAnalysis.updateConfiguration(tmp, false)) {
                failLimit = tmp.getFailuresLimit();
                includeNeighbours = tmp.isIncludeNeighbours();
                neighbourHeightThrehsold = tmp.getNeighbourHeightThreshold();
                computeDoublets = tmp.getResidualsThreshold() < 1;
                final DirectFilter primaryFilter = tmpFitConfig.getSmartFilter();
                final double residualsThreshold = tmp.getResidualsThreshold();
                myMultiFilter = new MultiPathFilter(primaryFilter, FitWorker.createMinimalFilter(tmpFitConfig.getFilterPrecisionMethod()), residualsThreshold);
            } else {
                IJ.log("Failed to update settings using the filter analysis");
                checkbox.setState(false);
                return;
            }
        } else {
            failLimit = config.getFailuresLimit();
            includeNeighbours = config.isIncludeNeighbours();
            neighbourHeightThrehsold = config.getNeighbourHeightThreshold();
            computeDoublets = BenchmarkSpotFit.this.settings.computeDoublets;
            myMultiFilter = multiFilter;
        }
        // Update the dialog
        taFilterXml.setText(myMultiFilter.toXml());
        textFailLimit.setText("" + failLimit);
        cbIncludeNeighbours.setState(includeNeighbours);
        textNeighbourHeight.setText(MathUtils.rounded(neighbourHeightThrehsold));
        cbComputeDoublets.setState(computeDoublets);
    }
}
Also used : Checkbox(java.awt.Checkbox) FitEngineConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration) FitConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitConfiguration) DirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter) MultiPathFilter(uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFilter) PeakResultPoint(uk.ac.sussex.gdsc.smlm.results.PeakResultPoint) BasePoint(uk.ac.sussex.gdsc.core.match.BasePoint)

Example 3 with FitEngineConfiguration

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

the class DoubletAnalysis method updateFilterConfiguration.

private static boolean updateFilterConfiguration(FitConfiguration filterFitConfig) {
    final FitEngineConfiguration c = new FitEngineConfiguration();
    // TODO - add option to use latest or the best
    if (!BenchmarkFilterAnalysis.updateConfiguration(c, false)) {
        IJ.error(TITLE, "Unable to use the benchmark filter analysis configuration");
        return false;
    }
    filterFitConfig.setFitSettings(c.getFitEngineSettings().getFitSettings());
    return true;
}
Also used : FitEngineConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration)

Example 4 with FitEngineConfiguration

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

the class DoubletAnalysis method itemStateChanged.

@Override
public void itemStateChanged(ItemEvent event) {
    if (event.getSource() instanceof Choice) {
        // Update the settings from the template
        final Choice choice = (Choice) event.getSource();
        final String templateName = choice.getSelectedItem();
        // Get the configuration template
        final TemplateSettings template = ConfigurationTemplate.getTemplate(templateName);
        if (textCoordinateShiftFactor != null) {
            // Start with a default. This will cause a reset.
            final FitConfiguration fitConfig = new FitConfiguration();
            if (template != null) {
                fitConfig.setFitSettings(template.getFitEngineSettings().getFitSettings());
            }
            cbSmartFilter.setState(fitConfig.isSmartFilter());
            textCoordinateShiftFactor.setText("" + fitConfig.getCoordinateShiftFactor());
            textSignalStrength.setText("" + fitConfig.getSignalStrength());
            textMinPhotons.setText("" + fitConfig.getMinPhotons());
            textMinWidthFactor.setText("" + fitConfig.getMinWidthFactor());
            textWidthFactor.setText("" + fitConfig.getMaxWidthFactor());
            textPrecisionThreshold.setText("" + fitConfig.getPrecisionThreshold());
            textPrecisionMethod.select(fitConfig.getPrecisionMethod().ordinal());
        } else if (template != null) {
            if (template.hasFitEngineSettings()) {
                final boolean custom = ConfigurationTemplate.isCustomTemplate(templateName);
                final FitEngineConfiguration config2 = new FitEngineConfiguration(template.getFitEngineSettings(), template.getCalibration(), template.getPsf());
                final FitConfiguration fitConfig2 = config2.getFitConfiguration();
                if (custom && template.hasPsf()) {
                    textPsf.select(PeakFit.getPsfTypeNames()[fitConfig2.getPsfType().ordinal()]);
                }
                textDataFilterType.select(config2.getDataFilterType().ordinal());
                textDataFilter.select(config2.getDataFilterMethod(0).ordinal());
                textSmooth.setText("" + config2.getDataFilterParameterValue(0));
                textSearch.setText("" + config2.getSearch());
                textBorder.setText("" + config2.getBorder());
                textFitting.setText("" + config2.getFitting());
                textFitSolver.select(FitProtosHelper.getName(fitConfig2.getFitSolver()));
                // Copy settings not in the dialog for the fit solver
                final FitConfiguration fitConfig = config.getFitConfiguration();
                if (custom) {
                    fitConfig.setPsf(fitConfig2.getPsf());
                }
                fitConfig.setFitSolverSettings(fitConfig2.getFitSolverSettings());
            }
        } else {
        // Ignore
        }
    } else if (event.getSource() instanceof Checkbox) {
        final Checkbox checkbox = (Checkbox) event.getSource();
        if (!checkbox.getState()) {
            return;
        }
        if (textCoordinateShiftFactor != null) {
            if (!updateFilterConfiguration(filterFitConfig)) {
                return;
            }
            cbSmartFilter.setState(filterFitConfig.isSmartFilter());
            textCoordinateShiftFactor.setText("" + filterFitConfig.getCoordinateShiftFactor());
            textSignalStrength.setText("" + filterFitConfig.getSignalStrength());
            textMinPhotons.setText("" + filterFitConfig.getMinPhotons());
            textMinWidthFactor.setText("" + filterFitConfig.getMinWidthFactor());
            textWidthFactor.setText("" + filterFitConfig.getMaxWidthFactor());
            textPrecisionThreshold.setText("" + filterFitConfig.getPrecisionThreshold());
            textPrecisionMethod.select(filterFitConfig.getPrecisionMethod().ordinal());
        } else {
            if (!updateFitConfiguration(config)) {
                return;
            }
            final FitConfiguration fitConfig = config.getFitConfiguration();
            textPsf.select(PeakFit.getPsfTypeNames()[fitConfig.getPsfType().ordinal()]);
            textDataFilterType.select(config.getDataFilterType().ordinal());
            textDataFilter.select(config.getDataFilterMethod(0).ordinal());
            textSmooth.setText("" + config.getDataFilterParameterValue(0));
            textSearch.setText("" + config.getSearch());
            textBorder.setText("" + config.getBorder());
            textFitting.setText("" + config.getFitting());
            textFitSolver.select(FitProtosHelper.getName(fitConfig.getFitSolver()));
            textMatchDistance.setText("" + settings.matchDistance);
            textLowerDistance.setText("" + settings.lowerDistance);
            textSignalFactor.setText("" + settings.signalFactor);
            textLowerFactor.setText("" + settings.lowerSignalFactor);
        }
    }
}
Also used : TemplateSettings(uk.ac.sussex.gdsc.smlm.data.config.TemplateProtos.TemplateSettings) Choice(java.awt.Choice) FitConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitConfiguration) FitEngineConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration) Checkbox(java.awt.Checkbox)

Example 5 with FitEngineConfiguration

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

the class SettingsManager method readFitEngineConfiguration.

/**
 * Read the FitEngineConfiguration from the settings file in the settings directory. This loads
 * the current Calibration, PSF and FitEngineSettings.
 *
 * @param flags the flags
 * @return the FitEngineConfiguration
 */
public static FitEngineConfiguration readFitEngineConfiguration(int flags) {
    final FitEngineSettings fitEngineSettings = readFitEngineSettings(flags);
    final Calibration calibration = readCalibration(flags);
    final PSF psf = readPsf(flags);
    return new FitEngineConfiguration(fitEngineSettings, calibration, psf);
}
Also used : PSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF) FitEngineConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration) Calibration(uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.Calibration) FitEngineSettings(uk.ac.sussex.gdsc.smlm.data.config.FitProtos.FitEngineSettings)

Aggregations

FitEngineConfiguration (uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration)26 FitConfiguration (uk.ac.sussex.gdsc.smlm.engine.FitConfiguration)9 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)6 TemplateSettings (uk.ac.sussex.gdsc.smlm.data.config.TemplateProtos.TemplateSettings)4 ImagePlus (ij.ImagePlus)3 Checkbox (java.awt.Checkbox)3 BasePoint (uk.ac.sussex.gdsc.core.match.BasePoint)3 DataFilterMethod (uk.ac.sussex.gdsc.smlm.data.config.FitProtos.DataFilterMethod)3 PeakResultPoint (uk.ac.sussex.gdsc.smlm.results.PeakResultPoint)3 MultiPathFilter (uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFilter)3 ImageListener (ij.ImageListener)2 Choice (java.awt.Choice)2 Rectangle (java.awt.Rectangle)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 NonBlockingExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog)2 Calibration (uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.Calibration)2 CalibrationWriter (uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter)2 FitEngineSettings (uk.ac.sussex.gdsc.smlm.data.config.FitProtos.FitEngineSettings)2 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)2 DirectFilter (uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter)2