Search in sources :

Example 11 with FitConfiguration

use of gdsc.smlm.fitting.FitConfiguration in project GDSC-SMLM by aherbert.

the class BatchPeakFit method getDefaultSettingsXmlDocument.

private Document getDefaultSettingsXmlDocument() {
    // Create an XML document of the default settings
    Document doc = null;
    try {
        String configXml = xs.toXML(new FitEngineConfiguration(new FitConfiguration()));
        doc = loadDocument(configXml);
    } catch (XStreamException ex) {
        ex.printStackTrace();
    }
    return doc;
}
Also used : XStreamException(com.thoughtworks.xstream.XStreamException) FitEngineConfiguration(gdsc.smlm.engine.FitEngineConfiguration) FitConfiguration(gdsc.smlm.fitting.FitConfiguration) Document(org.w3c.dom.Document)

Example 12 with FitConfiguration

use of gdsc.smlm.fitting.FitConfiguration in project GDSC-SMLM by aherbert.

the class PeakFit method updateFitConfiguration.

/**
	 * Updates the configuration for peak fitting. Configures the calculation of residuals, logging and peak validation.
	 * 
	 * @return
	 */
private void updateFitConfiguration(FitEngineConfiguration config) {
    FitConfiguration fitConfig = config.getFitConfiguration();
    // Adjust the settings that are relevant within the fitting configuration. 
    fitConfig.setComputeResiduals(config.getResidualsThreshold() < 1);
    logger = (resultsSettings.logProgress) ? new IJLogger() : null;
    fitConfig.setLog(logger);
    fitConfig.setComputeDeviations(resultsSettings.showDeviations);
    // Add the calibration for precision filtering
    fitConfig.setNmPerPixel(calibration.getNmPerPixel());
    fitConfig.setGain(calibration.getGain());
    fitConfig.setBias(calibration.getBias());
    fitConfig.setEmCCD(calibration.isEmCCD());
}
Also used : FitConfiguration(gdsc.smlm.fitting.FitConfiguration) IJLogger(gdsc.core.ij.IJLogger)

Example 13 with FitConfiguration

use of gdsc.smlm.fitting.FitConfiguration in project GDSC-SMLM by aherbert.

the class PeakFit method configureSmartFilter.

/**
	 * Show a dialog to configure the smart filter. The updated settings are saved to the settings file.
	 * <p>
	 * If the fit configuration isSmartFilter is not enabled then this method returns true. If it is enabled then a
	 * dialog is shown to input the configuration for a smart filter. If no valid filter can be created from the input
	 * then the method returns false.
	 * <p>
	 * Note: If the smart filter is successfully configured then the use may want to disable the standard fit
	 * validation.
	 *
	 * @param settings
	 *            the settings
	 * @param filename
	 *            the filename
	 * @return true, if successful
	 */
public static boolean configureSmartFilter(GlobalSettings settings, String filename) {
    FitEngineConfiguration config = settings.getFitEngineConfiguration();
    FitConfiguration fitConfig = config.getFitConfiguration();
    Calibration calibration = settings.getCalibration();
    if (!fitConfig.isSmartFilter())
        return true;
    ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
    String xml = fitConfig.getSmartFilterXML();
    if (Utils.isNullOrEmpty(xml))
        xml = fitConfig.getDefaultSmartFilterXML();
    gd.addMessage("Smart filter (used to pick optimum results during fitting)");
    gd.addTextAreas(XmlUtils.convertQuotes(xml), null, 8, 60);
    // Currently we just collect it here even if not needed
    gd.addMessage("Smart filters using precision filtering may require a local background level.\n \nLocal background requires the camera bias:");
    gd.addNumericField("Camera_bias (ADUs)", calibration.getBias(), 2);
    gd.showDialog();
    if (gd.wasCanceled())
        return false;
    xml = gd.getNextText();
    Filter f = DirectFilter.fromXML(xml);
    if (f == null || !(f instanceof DirectFilter))
        return false;
    fitConfig.setDirectFilter((DirectFilter) f);
    calibration.setBias(Math.abs(gd.getNextNumber()));
    if (filename != null)
        SettingsManager.saveSettings(settings, filename);
    return true;
}
Also used : Filter(gdsc.smlm.results.filter.Filter) PlugInFilter(ij.plugin.filter.PlugInFilter) DirectFilter(gdsc.smlm.results.filter.DirectFilter) SpotFilter(gdsc.smlm.filters.SpotFilter) DataFilter(gdsc.smlm.engine.DataFilter) FitEngineConfiguration(gdsc.smlm.engine.FitEngineConfiguration) FitConfiguration(gdsc.smlm.fitting.FitConfiguration) DirectFilter(gdsc.smlm.results.filter.DirectFilter) Calibration(gdsc.smlm.results.Calibration) ExtendedGenericDialog(ij.gui.ExtendedGenericDialog)

Example 14 with FitConfiguration

use of gdsc.smlm.fitting.FitConfiguration in project GDSC-SMLM by aherbert.

the class BenchmarkFilterAnalysis method createResults.

/**
	 * Create peak results.
	 *
	 * @param filterResults
	 *            The results from running the filter (or null)
	 * @param filter
	 *            the filter
	 */
private MemoryPeakResults createResults(PreprocessedPeakResult[] filterResults, DirectFilter filter, boolean withBorder) {
    if (filterResults == null) {
        final MultiPathFilter multiPathFilter = createMPF(filter, minimalFilter);
        //multiPathFilter.setDebugFile("/tmp/filter.txt");
        filterResults = filterResults(multiPathFilter);
    }
    MemoryPeakResults results = new MemoryPeakResults();
    results.copySettings(this.results);
    results.setName(TITLE);
    if (withBorder) {
        // To produce the same results as the PeakFit plugin we must implement the border
        // functionality used in the FitWorker. This respects the border of the spot filter.
        FitEngineConfiguration config = new FitEngineConfiguration(new FitConfiguration());
        updateAllConfiguration(config);
        MaximaSpotFilter spotFilter = config.createSpotFilter(true);
        final int border = spotFilter.getBorder();
        int[] bounds = getBounds();
        final int borderLimitX = bounds[0] - border;
        final int borderLimitY = bounds[1] - border;
        for (PreprocessedPeakResult spot : filterResults) {
            if (spot.getX() > border && spot.getX() < borderLimitX && spot.getY() > border && spot.getY() < borderLimitY) {
                double[] p = spot.toGaussian2DParameters();
                float[] params = new float[p.length];
                for (int j = 0; j < p.length; j++) params[j] = (float) p[j];
                int frame = spot.getFrame();
                int origX = (int) p[Gaussian2DFunction.X_POSITION];
                int origY = (int) p[Gaussian2DFunction.Y_POSITION];
                results.addf(frame, origX, origY, 0, 0, spot.getNoise(), params, null);
            }
        }
    } else {
        for (PreprocessedPeakResult spot : filterResults) {
            double[] p = spot.toGaussian2DParameters();
            float[] params = new float[p.length];
            for (int j = 0; j < p.length; j++) params[j] = (float) p[j];
            int frame = spot.getFrame();
            int origX = (int) p[Gaussian2DFunction.X_POSITION];
            int origY = (int) p[Gaussian2DFunction.Y_POSITION];
            results.addf(frame, origX, origY, 0, 0, spot.getNoise(), params, null);
        }
    }
    return results;
}
Also used : FitEngineConfiguration(gdsc.smlm.engine.FitEngineConfiguration) FitConfiguration(gdsc.smlm.fitting.FitConfiguration) MaximaSpotFilter(gdsc.smlm.filters.MaximaSpotFilter) MultiPathFilter(gdsc.smlm.results.filter.MultiPathFilter) BasePreprocessedPeakResult(gdsc.smlm.results.filter.BasePreprocessedPeakResult) PreprocessedPeakResult(gdsc.smlm.results.filter.PreprocessedPeakResult) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults)

Example 15 with FitConfiguration

use of gdsc.smlm.fitting.FitConfiguration in project GDSC-SMLM by aherbert.

the class BenchmarkSpotFit method showDialog.

@SuppressWarnings("unchecked")
private boolean showDialog() {
    GenericDialog gd = new GenericDialog(TITLE);
    gd.addHelp(About.HELP_URL);
    gd.addMessage(String.format("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 \nConfigure the fitting:", Utils.rounded(simulationParameters.s), Utils.rounded(getSa())));
    gd.addSlider("Fraction_positives", 50, 100, fractionPositives);
    gd.addSlider("Fraction_negatives_after_positives", 0, 100, fractionNegativesAfterAllPositives);
    gd.addSlider("Min_negatives_after_positives", 0, 10, negativesAfterAllPositives);
    gd.addSlider("Match_distance", 0.5, 3.5, distance);
    gd.addSlider("Lower_distance", 0, 3.5, lowerDistance);
    gd.addSlider("Match_signal", 0, 3.5, signalFactor);
    gd.addSlider("Lower_signal", 0, 3.5, lowerSignalFactor);
    // Collect options for fitting
    final double sa = getSa();
    gd.addNumericField("Initial_StdDev", Maths.round(sa / simulationParameters.a), 3);
    gd.addSlider("Fitting_width", 2, 4.5, config.getFitting());
    String[] solverNames = SettingsManager.getNames((Object[]) FitSolver.values());
    gd.addChoice("Fit_solver", solverNames, solverNames[fitConfig.getFitSolver().ordinal()]);
    String[] functionNames = SettingsManager.getNames((Object[]) FitFunction.values());
    gd.addChoice("Fit_function", functionNames, functionNames[fitConfig.getFitFunction().ordinal()]);
    gd.addMessage("Multi-path filter (used to pick optimum results during fitting)");
    // Allow loading the best filter fot these results
    boolean benchmarkSettingsCheckbox = fitResultsId == BenchmarkFilterAnalysis.lastId;
    // This should always be an opt-in decision. Otherwise the user cannot use the previous settings
    useBenchmarkSettings = false;
    if (benchmarkSettingsCheckbox)
        gd.addCheckbox("Benchmark_settings", useBenchmarkSettings);
    gd.addTextAreas(XmlUtils.convertQuotes(multiFilter.toXML()), null, 6, 60);
    gd.addNumericField("Fail_limit", config.getFailuresLimit(), 0);
    gd.addCheckbox("Include_neighbours", config.isIncludeNeighbours());
    gd.addSlider("Neighbour_height", 0.01, 1, config.getNeighbourHeightThreshold());
    //gd.addSlider("Residuals_threshold", 0.01, 1, config.getResidualsThreshold());
    gd.addCheckbox("Compute_doublets", computeDoublets);
    gd.addNumericField("Duplicate_distance", fitConfig.getDuplicateDistance(), 2);
    gd.addCheckbox("Show_score_histograms", showFilterScoreHistograms);
    gd.addCheckbox("Show_correlation", showCorrelation);
    gd.addCheckbox("Plot_rank_by_intensity", rankByIntensity);
    gd.addCheckbox("Save_filter_range", saveFilterRange);
    if (extraOptions) {
    }
    // Add a mouse listener to the config file field
    if (benchmarkSettingsCheckbox && Utils.isShowGenericDialog()) {
        Vector<TextField> numerics = (Vector<TextField>) gd.getNumericFields();
        Vector<Checkbox> checkboxes = (Vector<Checkbox>) gd.getCheckboxes();
        taFilterXml = gd.getTextArea1();
        Checkbox b = checkboxes.get(0);
        b.addItemListener(this);
        textFailLimit = numerics.get(9);
        cbIncludeNeighbours = checkboxes.get(1);
        textNeighbourHeight = numerics.get(10);
        cbComputeDoublets = checkboxes.get(2);
        if (useBenchmarkSettings) {
            FitConfiguration tmpFitConfig = new FitConfiguration();
            FitEngineConfiguration tmp = new FitEngineConfiguration(tmpFitConfig);
            // Collect the residuals threshold
            tmpFitConfig.setComputeResiduals(true);
            if (BenchmarkFilterAnalysis.updateConfiguration(tmp, false)) {
                textFailLimit.setText("" + tmp.getFailuresLimit());
                cbIncludeNeighbours.setState(tmp.isIncludeNeighbours());
                textNeighbourHeight.setText(Utils.rounded(tmp.getNeighbourHeightThreshold()));
                cbComputeDoublets.setState(tmp.getResidualsThreshold() < 1);
                final DirectFilter primaryFilter = tmpFitConfig.getSmartFilter();
                final double residualsThreshold = tmp.getResidualsThreshold();
                taFilterXml.setText(new MultiPathFilter(primaryFilter, minimalFilter, residualsThreshold).toXML());
            }
        }
    }
    gd.showDialog();
    if (gd.wasCanceled())
        return false;
    fractionPositives = Math.abs(gd.getNextNumber());
    fractionNegativesAfterAllPositives = Math.abs(gd.getNextNumber());
    negativesAfterAllPositives = (int) Math.abs(gd.getNextNumber());
    distance = Math.abs(gd.getNextNumber());
    lowerDistance = Math.abs(gd.getNextNumber());
    signalFactor = Math.abs(gd.getNextNumber());
    lowerSignalFactor = Math.abs(gd.getNextNumber());
    fitConfig.setInitialPeakStdDev(gd.getNextNumber());
    config.setFitting(gd.getNextNumber());
    fitConfig.setFitSolver(gd.getNextChoiceIndex());
    fitConfig.setFitFunction(gd.getNextChoiceIndex());
    boolean myUseBenchmarkSettings = false;
    if (benchmarkSettingsCheckbox)
        //useBenchmarkSettings = 
        myUseBenchmarkSettings = gd.getNextBoolean();
    // Read dialog settings
    String xml = gd.getNextText();
    int failLimit = (int) gd.getNextNumber();
    boolean includeNeighbours = gd.getNextBoolean();
    double neighbourHeightThreshold = gd.getNextNumber();
    boolean myComputeDoublets = gd.getNextBoolean();
    double myDuplicateDistance = gd.getNextNumber();
    MultiPathFilter myMultiFilter = null;
    if (myUseBenchmarkSettings && !Utils.isShowGenericDialog()) {
        // Only copy the benchmark settings if not interactive
        FitConfiguration tmpFitConfig = new FitConfiguration();
        FitEngineConfiguration tmp = new FitEngineConfiguration(tmpFitConfig);
        // Collect the residuals threshold
        tmpFitConfig.setComputeResiduals(true);
        if (BenchmarkFilterAnalysis.updateConfiguration(tmp, false)) {
            config.setFailuresLimit(tmp.getFailuresLimit());
            config.setIncludeNeighbours(tmp.isIncludeNeighbours());
            config.setNeighbourHeightThreshold(tmp.getNeighbourHeightThreshold());
            computeDoublets = (tmp.getResidualsThreshold() < 1);
            fitConfig.setDuplicateDistance(tmpFitConfig.getDuplicateDistance());
            final DirectFilter primaryFilter = tmpFitConfig.getSmartFilter();
            final double residualsThreshold = tmp.getResidualsThreshold();
            myMultiFilter = new MultiPathFilter(primaryFilter, minimalFilter, residualsThreshold);
        }
    } else {
        myMultiFilter = MultiPathFilter.fromXML(xml);
        config.setFailuresLimit(failLimit);
        config.setIncludeNeighbours(includeNeighbours);
        config.setNeighbourHeightThreshold(neighbourHeightThreshold);
        computeDoublets = myComputeDoublets;
        fitConfig.setDuplicateDistance(myDuplicateDistance);
    }
    if (myMultiFilter == null) {
        gd = new GenericDialog(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 (computeDoublets) {
        //config.setComputeResiduals(true);
        config.setResidualsThreshold(0);
        fitConfig.setComputeResiduals(true);
    } else {
        config.setResidualsThreshold(1);
        fitConfig.setComputeResiduals(false);
    }
    showFilterScoreHistograms = gd.getNextBoolean();
    showCorrelation = gd.getNextBoolean();
    rankByIntensity = gd.getNextBoolean();
    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.setWidthFactor(5);
    // Disable the direct filter
    fitConfig.setDirectFilter(null);
    if (extraOptions) {
    }
    if (gd.invalidNumber())
        return false;
    if (lowerDistance > distance)
        lowerDistance = distance;
    if (lowerSignalFactor > signalFactor)
        lowerSignalFactor = signalFactor;
    // Distances relative to sa (not s) as this is the same as the BenchmarkSpotFilter plugin 
    distanceInPixels = distance * sa / simulationParameters.a;
    lowerDistanceInPixels = lowerDistance * sa / simulationParameters.a;
    GlobalSettings settings = new GlobalSettings();
    settings.setFitEngineConfiguration(config);
    settings.setCalibration(cal);
    // Copy simulation defaults if a new simulation
    if (lastId != simulationParameters.id) {
        cal.setNmPerPixel(simulationParameters.a);
        cal.setGain(simulationParameters.gain);
        cal.setAmplification(simulationParameters.amplification);
        cal.setExposureTime(100);
        cal.setReadNoise(simulationParameters.readNoise);
        cal.setBias(simulationParameters.bias);
        cal.setEmCCD(simulationParameters.emCCD);
        // This is needed to configure the fit solver
        fitConfig.setNmPerPixel(Maths.round(cal.getNmPerPixel()));
        fitConfig.setGain(Maths.round(cal.getGain()));
        fitConfig.setBias(Maths.round(cal.getBias()));
        fitConfig.setReadNoise(Maths.round(cal.getReadNoise()));
        fitConfig.setAmplification(Maths.round(cal.getAmplification()));
        fitConfig.setEmCCD(cal.isEmCCD());
    }
    if (!PeakFit.configureFitSolver(settings, null, extraOptions))
        return false;
    return true;
}
Also used : FitEngineConfiguration(gdsc.smlm.engine.FitEngineConfiguration) DirectFilter(gdsc.smlm.results.filter.DirectFilter) GlobalSettings(gdsc.smlm.ij.settings.GlobalSettings) PeakResultPoint(gdsc.smlm.ij.plugins.ResultsMatchCalculator.PeakResultPoint) BasePoint(gdsc.core.match.BasePoint) Checkbox(java.awt.Checkbox) FitConfiguration(gdsc.smlm.fitting.FitConfiguration) GenericDialog(ij.gui.GenericDialog) MultiPathFilter(gdsc.smlm.results.filter.MultiPathFilter) TextField(java.awt.TextField) Vector(java.util.Vector)

Aggregations

FitConfiguration (gdsc.smlm.fitting.FitConfiguration)32 FitEngineConfiguration (gdsc.smlm.engine.FitEngineConfiguration)16 GlobalSettings (gdsc.smlm.ij.settings.GlobalSettings)8 GenericDialog (ij.gui.GenericDialog)6 Rectangle (java.awt.Rectangle)5 BasePoint (gdsc.core.match.BasePoint)4 Calibration (gdsc.smlm.results.Calibration)4 MultiPathFilter (gdsc.smlm.results.filter.MultiPathFilter)4 ExtendedGenericDialog (ij.gui.ExtendedGenericDialog)4 IJLogger (gdsc.core.ij.IJLogger)3 Gaussian2DFitter (gdsc.smlm.fitting.Gaussian2DFitter)3 PeakResultPoint (gdsc.smlm.ij.plugins.ResultsMatchCalculator.PeakResultPoint)3 MemoryPeakResults (gdsc.smlm.results.MemoryPeakResults)3 DirectFilter (gdsc.smlm.results.filter.DirectFilter)3 Checkbox (java.awt.Checkbox)3 ImageExtractor (gdsc.core.utils.ImageExtractor)2 FitEngine (gdsc.smlm.engine.FitEngine)2 MaximaSpotFilter (gdsc.smlm.filters.MaximaSpotFilter)2 FitResult (gdsc.smlm.fitting.FitResult)2 FitStatus (gdsc.smlm.fitting.FitStatus)2