Search in sources :

Example 6 with DirectFilter

use of gdsc.smlm.results.filter.DirectFilter in project GDSC-SMLM by aherbert.

the class BenchmarkFilterAnalysis method scoreFilters.

private FilterScoreResult[] scoreFilters(FilterSet filterSet, boolean createTextResult) {
    if (filterSet.size() == 0)
        return null;
    initialiseScoring(filterSet);
    FilterScoreResult[] scoreResults = new FilterScoreResult[filterSet.size()];
    if (scoreResults.length == 1) {
        // No need to multi-thread this			
        scoreResults[0] = scoreFilter((DirectFilter) filterSet.getFilters().get(0), minimalFilter, createTextResult, coordinateStore);
    } else {
        // Multi-thread score all the result
        final int nThreads = getThreads(scoreResults.length);
        final BlockingQueue<ScoreJob> jobs = new ArrayBlockingQueue<ScoreJob>(nThreads * 2);
        final List<ScoreWorker> workers = new LinkedList<ScoreWorker>();
        final List<Thread> threads = new LinkedList<Thread>();
        for (int i = 0; i < nThreads; i++) {
            final ScoreWorker worker = new ScoreWorker(jobs, scoreResults, createTextResult, (coordinateStore == null) ? null : coordinateStore.newInstance());
            final Thread t = new Thread(worker);
            workers.add(worker);
            threads.add(t);
            t.start();
        }
        int index = 0;
        totalProgress = scoreResults.length;
        stepProgress = Utils.getProgressInterval(totalProgress);
        progress = 0;
        for (Filter filter : filterSet.getFilters()) {
            if (IJ.escapePressed())
                break;
            put(jobs, new ScoreJob((DirectFilter) filter, index++));
        }
        // Finish all the worker threads by passing in a null job
        for (int i = 0; i < threads.size(); i++) {
            put(jobs, new ScoreJob(null, -1));
        }
        // Wait for all to finish
        for (int i = 0; i < threads.size(); i++) {
            try {
                threads.get(i).join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        threads.clear();
        IJ.showProgress(1);
        // In case the threads were interrupted
        if (Utils.isInterrupted())
            scoreResults = null;
    }
    finishScoring();
    return scoreResults;
}
Also used : IDirectFilter(gdsc.smlm.results.filter.IDirectFilter) DirectFilter(gdsc.smlm.results.filter.DirectFilter) LinkedList(java.util.LinkedList) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) IDirectFilter(gdsc.smlm.results.filter.IDirectFilter) DirectFilter(gdsc.smlm.results.filter.DirectFilter) Filter(gdsc.smlm.results.filter.Filter) MultiPathFilter(gdsc.smlm.results.filter.MultiPathFilter) MaximaSpotFilter(gdsc.smlm.filters.MaximaSpotFilter)

Example 7 with DirectFilter

use of gdsc.smlm.results.filter.DirectFilter in project GDSC-SMLM by aherbert.

the class BenchmarkFilterAnalysis method shutdown.

/*
	 * (non-Javadoc)
	 * 
	 * @see gdsc.smlm.ga.FitnessFunction#shutdown()
	 */
public void shutdown() {
    // Report the score for the best filter
    List<? extends Chromosome<FilterScore>> individuals = ga_population.getIndividuals();
    FilterScore max = null;
    for (Chromosome<FilterScore> c : individuals) {
        final FilterScore f = c.getFitness();
        if (f != null && f.compareTo(max) < 0) {
            max = f;
        }
    }
    if (max == null)
        return;
    DirectFilter filter = (DirectFilter) ((SimpleFilterScore) max).filter;
    // This filter may not have been part of the scored subset so use the entire results set for reporting
    FractionClassificationResult r = scoreFilter(filter, minimalFilter, ga_resultsList, coordinateStore);
    final StringBuilder text = createResult(filter, r);
    add(text, ga_iteration);
    gaWindow.append(text.toString());
}
Also used : FilterScore(gdsc.smlm.results.filter.FilterScore) IDirectFilter(gdsc.smlm.results.filter.IDirectFilter) DirectFilter(gdsc.smlm.results.filter.DirectFilter) FractionClassificationResult(gdsc.core.match.FractionClassificationResult)

Example 8 with DirectFilter

use of gdsc.smlm.results.filter.DirectFilter 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 9 with DirectFilter

use of gdsc.smlm.results.filter.DirectFilter 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)

Example 10 with DirectFilter

use of gdsc.smlm.results.filter.DirectFilter in project GDSC-SMLM by aherbert.

the class BenchmarkSpotFit method itemStateChanged.

public void itemStateChanged(ItemEvent e) {
    if (e.getSource() instanceof Checkbox) {
        Checkbox checkbox = (Checkbox) e.getSource();
        int failLimit;
        boolean includeNeighbours;
        double neighbourHeightThrehsold;
        boolean computeDoublets;
        MultiPathFilter myMultiFilter;
        if (checkbox.getState()) {
            FitConfiguration tmpFitConfig = new FitConfiguration();
            FitEngineConfiguration tmp = new FitEngineConfiguration(tmpFitConfig);
            // 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, minimalFilter, 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.computeDoublets;
            myMultiFilter = multiFilter;
        }
        // Update the dialog
        taFilterXml.setText(myMultiFilter.toXML());
        textFailLimit.setText("" + failLimit);
        cbIncludeNeighbours.setState(includeNeighbours);
        textNeighbourHeight.setText(Utils.rounded(neighbourHeightThrehsold));
        cbComputeDoublets.setState(computeDoublets);
    }
}
Also used : Checkbox(java.awt.Checkbox) FitConfiguration(gdsc.smlm.fitting.FitConfiguration) FitEngineConfiguration(gdsc.smlm.engine.FitEngineConfiguration) DirectFilter(gdsc.smlm.results.filter.DirectFilter) MultiPathFilter(gdsc.smlm.results.filter.MultiPathFilter) PeakResultPoint(gdsc.smlm.ij.plugins.ResultsMatchCalculator.PeakResultPoint) BasePoint(gdsc.core.match.BasePoint)

Aggregations

DirectFilter (gdsc.smlm.results.filter.DirectFilter)17 IDirectFilter (gdsc.smlm.results.filter.IDirectFilter)14 MultiPathFilter (gdsc.smlm.results.filter.MultiPathFilter)8 Filter (gdsc.smlm.results.filter.Filter)7 MaximaSpotFilter (gdsc.smlm.filters.MaximaSpotFilter)6 FilterSet (gdsc.smlm.results.filter.FilterSet)6 FractionClassificationResult (gdsc.core.match.FractionClassificationResult)5 ArrayList (java.util.ArrayList)4 FitEngineConfiguration (gdsc.smlm.engine.FitEngineConfiguration)3 FitConfiguration (gdsc.smlm.fitting.FitConfiguration)3 FilterScore (gdsc.smlm.results.filter.FilterScore)3 GenericDialog (ij.gui.GenericDialog)3 BasePoint (gdsc.core.match.BasePoint)2 PeakResultPoint (gdsc.smlm.ij.plugins.ResultsMatchCalculator.PeakResultPoint)2 GlobalSettings (gdsc.smlm.ij.settings.GlobalSettings)2 SearchResult (gdsc.smlm.search.SearchResult)2 NonBlockingGenericDialog (ij.gui.NonBlockingGenericDialog)2 Checkbox (java.awt.Checkbox)2 LinkedList (java.util.LinkedList)2 BufferedTextWindow (gdsc.core.ij.BufferedTextWindow)1