Search in sources :

Example 1 with DataFilterMethod

use of uk.ac.sussex.gdsc.smlm.data.config.FitProtos.DataFilterMethod in project GDSC-SMLM by aherbert.

the class PeakFit method addDataFilterOptions.

/**
 * Adds the data filter options for the first filter. Adds to the dialog: <ul> <li>a choice of
 * filter type (e.g. single, difference, etc)</li> <li>a choice of primary filter (e.g. mean,
 * Gaussian, etc)</li> <li>a single slider for the primary filter parameter</li> </ul>
 *
 * @param gd the dialog
 * @param fitEngineConfigurationProvider the fit engine configuration provider
 */
public static void addDataFilterOptions(final ExtendedGenericDialog gd, final FitEngineConfigurationProvider fitEngineConfigurationProvider) {
    final int n = 0;
    final DataFilterMethod defaultFilterMethod = DataFilterMethod.GAUSSIAN;
    final double defaultFilterSmoothing = 0.5;
    final FitEngineConfiguration config = fitEngineConfigurationProvider.getFitEngineConfiguration();
    gd.addChoice("Spot_filter_type", SettingsManager.getDataFilterTypeNames(), config.getDataFilterType().ordinal());
    gd.addChoice("Spot_filter", SettingsManager.getDataFilterMethodNames(), config.getDataFilterMethod(n, defaultFilterMethod).ordinal());
    addRelativeParameterOptions(gd, new RelativeParameterProvider(0, 2.5, "Smoothing", fitEngineConfigurationProvider, true) {

        @Override
        void setAbsolute(boolean absolute) {
            final FitEngineConfiguration c = fitEngineConfigurationProvider.getFitEngineConfiguration();
            final DataFilterMethod m = c.getDataFilterMethod(n, defaultFilterMethod);
            final double smooth = c.getDataFilterParameterValue(n, defaultFilterSmoothing);
            c.setDataFilter(m, smooth, absolute, n);
        }

        @Override
        boolean isAbsolute() {
            return fitEngineConfigurationProvider.getFitEngineConfiguration().getDataFilterParameterAbsolute(n, false);
        }

        @Override
        double getValue() {
            return fitEngineConfigurationProvider.getFitEngineConfiguration().getDataFilterParameterValue(n, defaultFilterSmoothing);
        }
    });
}
Also used : FitEngineConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration) DataFilterMethod(uk.ac.sussex.gdsc.smlm.data.config.FitProtos.DataFilterMethod)

Example 2 with DataFilterMethod

use of uk.ac.sussex.gdsc.smlm.data.config.FitProtos.DataFilterMethod in project GDSC-SMLM by aherbert.

the class SpotFinderPreview method showDialog.

@Override
public int showDialog(ImagePlus imp, String command, PlugInFilterRunner pfr) {
    settings = Settings.load();
    this.overlay = imp.getOverlay();
    this.imp = imp;
    // Saved by reference so do it once now
    settings.save();
    // The image is locked by the PlugInFilterRunner so unlock it to allow scroll.
    // This should be OK as the image data is not modified and only the overlay is
    // adjusted. If another plugin changes the image then the preview should update
    // the overlay and it will be obvious to the user to turn this plugin off.
    imp.unlock();
    config = SettingsManager.readFitEngineConfiguration(0);
    fitConfig = config.getFitConfiguration();
    gd = new NonBlockingExtendedGenericDialog(TITLE);
    gd.addHelp(HelpUrls.getUrl("spot-finder-preview"));
    gd.addMessage("Preview candidate maxima");
    final String[] templates = ConfigurationTemplate.getTemplateNames(true);
    gd.addChoice("Template", templates, templates[0]);
    final String[] models = CameraModelManager.listCameraModels(true);
    gd.addChoice("Camera_model_name", models, fitConfig.getCameraModelName());
    PeakFit.addPsfOptions(gd, (FitConfigurationProvider) () -> fitConfig);
    final PeakFit.SimpleFitEngineConfigurationProvider provider = new PeakFit.SimpleFitEngineConfigurationProvider(config);
    PeakFit.addDataFilterOptions(gd, provider);
    gd.addChoice("Spot_filter_2", SettingsManager.getDataFilterMethodNames(), config.getDataFilterMethod(1, settings.defaultDataFilterMethod).ordinal());
    PeakFit.addRelativeParameterOptions(gd, new RelativeParameterProvider(2.5, 4.5, "Smoothing_2", provider) {

        @Override
        void setAbsolute(boolean absolute) {
            final FitEngineConfiguration c = fitEngineConfigurationProvider.getFitEngineConfiguration();
            final DataFilterMethod m = c.getDataFilterMethod(1, settings.defaultDataFilterMethod);
            final double smooth = c.getDataFilterParameterValue(1, settings.defaultSmooth);
            c.setDataFilter(m, smooth, absolute, 1);
        }

        @Override
        boolean isAbsolute() {
            return fitEngineConfigurationProvider.getFitEngineConfiguration().getDataFilterParameterAbsolute(1, false);
        }

        @Override
        double getValue() {
            return fitEngineConfigurationProvider.getFitEngineConfiguration().getDataFilterParameterValue(1, settings.defaultSmooth);
        }
    });
    PeakFit.addSearchOptions(gd, provider);
    PeakFit.addBorderOptions(gd, provider);
    // Find if this image was created with ground truth data
    if (imp.getID() == CreateData.getImageId()) {
        final MemoryPeakResults results = CreateData.getResults();
        if (results != null) {
            gd.addSlider("Match_distance", 0, 2.5, settings.distance);
            gd.addSlider("Lower_match_distance (%)", 0, 100, settings.lowerDistance);
            gd.addCheckbox("Multiple_matches", settings.multipleMatches);
            gd.addCheckbox("Show_TP", settings.showTP);
            gd.addCheckbox("Show_FP", settings.showFP);
            gd.addMessage("");
            label = (Label) gd.getMessage();
            final boolean integerCoords = false;
            actualCoordinates = ResultsMatchCalculator.getCoordinates(results, integerCoords);
        }
    }
    if (label == null) {
        // If no ground truth data add options to show the spots by their rank
        // and number of neighbours
        gd.addSlider("Top_N", 0, 100, settings.topN);
        topNScrollBar = gd.getLastScrollbar();
        gd.addSlider("Select", 0, 100, settings.select);
        selectScrollBar = gd.getLastScrollbar();
        gd.addSlider("Neigbour_radius", 0, 10, settings.neighbourRadius);
    }
    ImageListener imageListener = null;
    if (ImageJUtils.isShowGenericDialog()) {
        // Listen for changes in the dialog options
        gd.addOptionCollectedListener(event -> {
            // Just run on the current processor
            if (preview) {
                run(imp.getProcessor());
            }
        });
        // Listen for changes to an image
        imageListener = new ImageAdapter() {

            @Override
            public void imageUpdated(ImagePlus imp) {
                if (SpotFinderPreview.this.imp.getID() == imp.getID() && preview && imp.getCurrentSlice() != currentSlice && filter != null) {
                    run(imp.getProcessor(), filter);
                }
            }
        };
        ImagePlus.addImageListener(imageListener);
        // Support template settings
        final Vector<TextField> numerics = gd.getNumericFields();
        final Vector<Choice> choices = gd.getChoices();
        final Iterator<TextField> nu = numerics.iterator();
        final Iterator<Choice> ch = choices.iterator();
        final Choice textTemplate = ch.next();
        textTemplate.removeItemListener(gd);
        textTemplate.removeKeyListener(gd);
        textTemplate.addItemListener(this::itemStateChanged);
        textCameraModelName = ch.next();
        textPsf = ch.next();
        textDataFilterType = ch.next();
        textDataFilterMethod = ch.next();
        textSmooth = nu.next();
        textDataFilterMethod2 = ch.next();
        textSmooth2 = nu.next();
        textSearch = nu.next();
        textBorder = nu.next();
    }
    gd.addPreviewCheckbox(pfr);
    gd.addDialogListener(this::dialogItemChanged);
    gd.setOKLabel("Save");
    gd.setCancelLabel("Close");
    gd.showDialog();
    if (imageListener != null) {
        ImagePlus.removeImageListener(imageListener);
    }
    if (!gd.wasCanceled() && !SettingsManager.writeSettings(config, SettingsManager.FLAG_SILENT)) {
        IJ.error(TITLE, "Failed to save settings");
    }
    // Reset
    imp.setOverlay(overlay);
    return DONE;
}
Also used : Choice(java.awt.Choice) ImageListener(ij.ImageListener) FitEngineConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration) NonBlockingExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog) ImagePlus(ij.ImagePlus) DataFilterMethod(uk.ac.sussex.gdsc.smlm.data.config.FitProtos.DataFilterMethod) RelativeParameterProvider(uk.ac.sussex.gdsc.smlm.ij.plugins.PeakFit.RelativeParameterProvider) ImageAdapter(uk.ac.sussex.gdsc.core.ij.ImageAdapter) TextField(java.awt.TextField) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Example 3 with DataFilterMethod

use of uk.ac.sussex.gdsc.smlm.data.config.FitProtos.DataFilterMethod in project GDSC-SMLM by aherbert.

the class PeakFit method configureDataFilter.

/**
 * Show a dialog to configure the data filter. The data filter type and the first data filter must
 * ALREADY be set in the configuration. The subsequent filters are then configured, e.g. for
 * difference and jury filters.
 *
 * <p>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>If the configuration is for a per-pixel camera type (e.g. sCMOS) then the camera model will
 * be loaded using the configured camera model name. This will be used to validate the filter to
 * check the filter supports the per-pixel camera type.
 *
 * @param config the config
 * @param flags the flags
 * @return True if the configuration succeeded
 */
public static boolean configureDataFilter(final FitEngineConfiguration config, int flags) {
    int numberOfFilters = 1;
    int filterCount;
    switch(config.getDataFilterType()) {
        case JURY:
            filterCount = Integer.MAX_VALUE;
            break;
        case DIFFERENCE:
            filterCount = 2;
            break;
        case SINGLE:
        default:
            filterCount = 1;
    }
    final String[] filterNames = SettingsManager.getDataFilterMethodNames();
    final DataFilterMethod[] filterValues = SettingsManager.getDataFilterMethodValues();
    // Check we have at least the first filter.
    if (config.getDataFiltersCount() == 0) {
        throw new IllegalStateException("No primary filter is configured");
    }
    final FitEngineConfigurationProvider fitEngineConfigurationProvider = () -> config;
    for (int i = 1; i < filterCount; i++) {
        final int filter = i + 1;
        final int ii = i;
        final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
        if (filter == filterCount) {
            // This is maximum filter count so no continue option
            ImageJUtils.addMessage(gd, "Configure the %s filter.", FitProtosHelper.getName(config.getDataFilterType()));
        } else {
            gd.enableYesNoCancel("Add", "Continue");
            ImageJUtils.addMessage(gd, "Configure the %s filter.\nClick continue to proceed with the current set of %d.", FitProtosHelper.getName(config.getDataFilterType()), i);
        }
        final String fieldName = "Spot_filter" + filter;
        if (IJ.isMacro()) {
            // Use blank default value so bad macro parameters return nothing
            gd.addStringField(fieldName, "");
        } else {
            gd.addChoice(fieldName, filterNames, filterNames[config.getDataFilterMethod(ii, config.getDataFilterMethod(ii - 1)).ordinal()]);
        }
        addRelativeParameterOptions(gd, new RelativeParameterProvider(0, 4.5, "Smoothing" + filter, fitEngineConfigurationProvider, true) {

            @Override
            void setAbsolute(boolean absolute) {
                // Get the current settings
                final FitEngineConfiguration c = fitEngineConfigurationProvider.getFitEngineConfiguration();
                final DataFilterMethod m = c.getDataFilterMethod(ii);
                final double smooth = c.getDataFilterParameter(ii).getValue();
                // Reset with the new absolute value
                c.setDataFilter(m, smooth, absolute, ii);
            }

            @Override
            boolean isAbsolute() {
                final FitEngineConfiguration c = fitEngineConfigurationProvider.getFitEngineConfiguration();
                return c.getDataFilterParameterAbsolute(ii, c.getDataFilterParameterAbsolute(ii - 1));
            }

            @Override
            double getValue() {
                final FitEngineConfiguration c = fitEngineConfigurationProvider.getFitEngineConfiguration();
                return c.getDataFilterParameterValue(ii, c.getDataFilterParameterValue(ii - 1));
            }
        });
        gd.showDialog();
        if (gd.wasCanceled()) {
            return false;
        }
        if (gd.wasOKed()) {
            int filterIndex = -1;
            if (IJ.isMacro()) {
                final String filterName = gd.getNextString();
                for (int j = 0; j < filterNames.length; j++) {
                    if (filterNames[j].equals(filterName)) {
                        filterIndex = j;
                        break;
                    }
                }
                if (filterIndex < 0) {
                    break;
                }
            } else {
                filterIndex = gd.getNextChoiceIndex();
            }
            // Note: The absolute flag is set in extra options
            config.setDataFilter(filterValues[filterIndex], Math.abs(gd.getNextNumber()), i);
            gd.collectOptions();
            numberOfFilters++;
        } else {
            break;
        }
    }
    config.setNumberOfFilters(numberOfFilters);
    if (BitFlagUtils.anyNotSet(flags, FLAG_NO_SAVE)) {
        saveFitEngineSettings(config);
    }
    final FitConfiguration fitConfig = config.getFitConfiguration();
    final CalibrationReader calibration = fitConfig.getCalibrationReader();
    if (calibration.isScmos()) {
        fitConfig.setCameraModel(CameraModelManager.load(fitConfig.getCameraModelName()));
    }
    try {
        config.createSpotFilter();
    } catch (final IllegalStateException ex) {
        IJ.error(TITLE, ex.getMessage());
        return false;
    }
    return true;
}
Also used : FitEngineConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) CalibrationReader(uk.ac.sussex.gdsc.smlm.data.config.CalibrationReader) DataFilterMethod(uk.ac.sussex.gdsc.smlm.data.config.FitProtos.DataFilterMethod) FitConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitConfiguration)

Aggregations

DataFilterMethod (uk.ac.sussex.gdsc.smlm.data.config.FitProtos.DataFilterMethod)3 FitEngineConfiguration (uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration)3 ImageListener (ij.ImageListener)1 ImagePlus (ij.ImagePlus)1 Choice (java.awt.Choice)1 TextField (java.awt.TextField)1 ImageAdapter (uk.ac.sussex.gdsc.core.ij.ImageAdapter)1 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)1 NonBlockingExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog)1 CalibrationReader (uk.ac.sussex.gdsc.smlm.data.config.CalibrationReader)1 FitConfiguration (uk.ac.sussex.gdsc.smlm.engine.FitConfiguration)1 RelativeParameterProvider (uk.ac.sussex.gdsc.smlm.ij.plugins.PeakFit.RelativeParameterProvider)1 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)1