Search in sources :

Example 6 with NonBlockingGenericDialog

use of ij.gui.NonBlockingGenericDialog in project GDSC-SMLM by aherbert.

the class BenchmarkFilterAnalysis method saveTemplate.

/**
	 * Save PeakFit configuration template using the current benchmark settings.
	 * 
	 * @param topFilterSummary
	 */
private void saveTemplate(String topFilterSummary) {
    FitEngineConfiguration config = new FitEngineConfiguration(new FitConfiguration());
    if (!updateAllConfiguration(config, true)) {
        IJ.log("Unable to create the template configuration");
        return;
    }
    // Remove the PSF width to make the template generic
    config.getFitConfiguration().setInitialPeakStdDev(0);
    String filename = getFilename("Template_File", templateFilename);
    if (filename != null) {
        templateFilename = filename;
        Prefs.set(KEY_TEMPLATE_FILENAME, filename);
        GlobalSettings settings = new GlobalSettings();
        settings.setNotes(getNotes(topFilterSummary));
        settings.setFitEngineConfiguration(config);
        if (!SettingsManager.saveSettings(settings, filename, true)) {
            IJ.log("Unable to save the template configuration");
            return;
        }
        // Save some random frames from the test image data
        ImagePlus imp = CreateData.getImage();
        if (imp == null)
            return;
        // Get the number of frames
        final ImageStack stack = imp.getImageStack();
        if (sampler == null || sampler.getResults() != results) {
            sampler = new ResultsImageSampler(results, stack, 32);
            sampler.analyse();
        }
        if (!sampler.isValid())
            return;
        // Iteratively show the example until the user is happy.
        // Yes = OK, No = Repeat, Cancel = Do not save
        String keyNo = "nNo";
        String keyLow = "nLower";
        String keyHigh = "nHigher";
        if (Utils.isMacro()) {
            // Collect the options if running in a macro
            String options = Macro.getOptions();
            nNo = Integer.parseInt(Macro.getValue(options, keyNo, Integer.toString(nNo)));
            nLow = Integer.parseInt(Macro.getValue(options, keyLow, Integer.toString(nLow)));
            nHigh = Integer.parseInt(Macro.getValue(options, keyHigh, Integer.toString(nHigh)));
        } else {
            if (nLow + nHigh == 0)
                nLow = nHigh = 1;
        }
        final ImagePlus[] out = new ImagePlus[1];
        out[0] = sampler.getSample(nNo, nLow, nHigh);
        if (!Utils.isMacro()) {
            // Show the template results
            final ConfigurationTemplate configTemplate = new ConfigurationTemplate();
            // Interactively show the sample image data
            final boolean[] close = new boolean[1];
            final ImagePlus[] outImp = new ImagePlus[1];
            if (out[0] != null) {
                outImp[0] = display(out[0]);
                if (Utils.isNewWindow()) {
                    close[0] = true;
                    // Zoom a bit
                    ImageWindow iw = outImp[0].getWindow();
                    for (int i = 7; i-- > 0 && Math.max(iw.getWidth(), iw.getHeight()) < 512; ) {
                        iw.getCanvas().zoomIn(0, 0);
                    }
                }
                configTemplate.createResults(outImp[0]);
            }
            // TODO - fix this when a second sample is made as the results are not updated.
            ImageListener listener = new ImageListener() {

                public void imageOpened(ImagePlus imp) {
                }

                public void imageClosed(ImagePlus imp) {
                }

                public void imageUpdated(ImagePlus imp) {
                    if (imp != null && imp == outImp[0]) {
                        configTemplate.updateResults(imp.getCurrentSlice());
                    }
                }
            };
            ImagePlus.addImageListener(listener);
            // For the dialog
            String msg = String.format("Showing image data for the template example.\n \nSample Frames:\nEmpty = %d\nLower density = %d\nHigher density = %d\n", sampler.getNumberOfEmptySamples(), sampler.getNumberOfLowDensitySamples(), sampler.getNumberOfHighDensitySamples());
            // Turn off the recorder when the dialog is showing
            boolean record = Recorder.record;
            Recorder.record = false;
            NonBlockingGenericDialog gd = new NonBlockingGenericDialog(TITLE);
            gd.addMessage(msg);
            //gd.enableYesNoCancel(" Save ", " Resample ");
            gd.addSlider(keyNo, 0, 10, nNo);
            gd.addSlider(keyLow, 0, 10, nLow);
            gd.addSlider(keyHigh, 0, 10, nHigh);
            gd.addDialogListener(new DialogListener() {

                public boolean dialogItemChanged(GenericDialog gd, AWTEvent e) {
                    // image the user has not seen.
                    if (e == null)
                        return true;
                    nNo = (int) gd.getNextNumber();
                    nLow = (int) gd.getNextNumber();
                    nHigh = (int) gd.getNextNumber();
                    out[0] = sampler.getSample(nNo, nLow, nHigh);
                    if (out[0] != null) {
                        outImp[0] = display(out[0]);
                        if (Utils.isNewWindow()) {
                            close[0] = true;
                            // Zoom a bit
                            ImageWindow iw = outImp[0].getWindow();
                            for (int i = 7; i-- > 0 && Math.max(iw.getWidth(), iw.getHeight()) < 512; ) {
                                iw.getCanvas().zoomIn(0, 0);
                            }
                        }
                        configTemplate.createResults(outImp[0]);
                    }
                    return true;
                }
            });
            gd.showDialog();
            if (gd.wasCanceled()) {
                out[0] = null;
                // For the recorder
                nNo = nLow = nHigh = 0;
            }
            if (close[0]) {
                // Because closing the image sets the stack pixels array to null
                if (out[0] != null)
                    out[0] = out[0].duplicate();
                outImp[0].close();
            }
            configTemplate.closeResults();
            ImagePlus.removeImageListener(listener);
            if (record) {
                Recorder.record = true;
                Recorder.recordOption(keyNo, Integer.toString(nNo));
                Recorder.recordOption(keyLow, Integer.toString(nLow));
                Recorder.recordOption(keyHigh, Integer.toString(nHigh));
            }
        }
        if (out[0] == null)
            return;
        ImagePlus example = out[0];
        filename = Utils.replaceExtension(filename, ".tif");
        IJ.save(example, filename);
    }
}
Also used : ResultsImageSampler(gdsc.smlm.ij.results.ResultsImageSampler) ImageWindow(ij.gui.ImageWindow) ImageStack(ij.ImageStack) ImageListener(ij.ImageListener) FitEngineConfiguration(gdsc.smlm.engine.FitEngineConfiguration) GlobalSettings(gdsc.smlm.ij.settings.GlobalSettings) NonBlockingGenericDialog(ij.gui.NonBlockingGenericDialog) ImagePlus(ij.ImagePlus) FitConfiguration(gdsc.smlm.fitting.FitConfiguration) DialogListener(ij.gui.DialogListener) GenericDialog(ij.gui.GenericDialog) NonBlockingGenericDialog(ij.gui.NonBlockingGenericDialog) AWTEvent(java.awt.AWTEvent)

Example 7 with NonBlockingGenericDialog

use of ij.gui.NonBlockingGenericDialog in project GDSC-SMLM by aherbert.

the class OverlayResults method run.

/*
	 * (non-Javadoc)
	 * 
	 * @see ij.plugin.PlugIn#run(java.lang.String)
	 */
public void run(String arg) {
    SMLMUsageTracker.recordPlugin(this.getClass(), arg);
    if (MemoryPeakResults.isMemoryEmpty()) {
        IJ.error(TITLE, "There are no fitting results in memory");
        return;
    }
    names = new String[MemoryPeakResults.getResultNames().size() + 1];
    ids = new int[names.length];
    int c = 0;
    names[c++] = "(None)";
    for (MemoryPeakResults results : MemoryPeakResults.getAllResults()) {
        if (results.getSource().getOriginal() instanceof IJImageSource) {
            IJImageSource source = (IJImageSource) (results.getSource().getOriginal());
            ImagePlus imp = WindowManager.getImage(source.getName());
            if (imp != null) {
                ids[c] = imp.getID();
                names[c++] = results.getName();
            }
        }
    }
    if (c == 1) {
        IJ.error(TITLE, "There are no result images available");
        return;
    }
    names = Arrays.copyOf(names, c);
    Thread t = null;
    Worker w = null;
    NonBlockingGenericDialog gd = new NonBlockingGenericDialog(TITLE);
    gd.addMessage("Overlay results on current image frame");
    gd.addChoice("Results", names, (name == null) ? "" : name);
    gd.addCheckbox("Show_table", showTable);
    gd.addMessage("");
    gd.addHelp(About.HELP_URL);
    gd.hideCancelButton();
    gd.setOKLabel("Close");
    if (!(IJ.isMacro() || java.awt.GraphicsEnvironment.isHeadless())) {
        choice = (Choice) gd.getChoices().get(0);
        choice.addItemListener(this);
        checkbox = (Checkbox) gd.getCheckboxes().get(0);
        checkbox.addItemListener(this);
        label = (Label) gd.getMessage();
        // Listen for changes to an image
        ImagePlus.addImageListener(this);
        show();
        t = new Thread(w = new Worker());
        t.setDaemon(true);
        t.start();
    }
    gd.showDialog();
    if (!(IJ.isMacro() || java.awt.GraphicsEnvironment.isHeadless()))
        ImagePlus.removeImageListener(this);
    if (!gd.wasCanceled()) {
        name = gd.getNextChoice();
        showTable = gd.getNextBoolean();
    }
    if (t != null) {
        w.running = false;
        inbox.close();
        try {
            t.join(0);
        } catch (InterruptedException e) {
        }
        t = null;
    }
}
Also used : IJImageSource(gdsc.smlm.ij.IJImageSource) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) NonBlockingGenericDialog(ij.gui.NonBlockingGenericDialog) ImagePlus(ij.ImagePlus) Point(java.awt.Point)

Example 8 with NonBlockingGenericDialog

use of ij.gui.NonBlockingGenericDialog in project GDSC-SMLM by aherbert.

the class BackgroundEstimator method showDialog.

public int showDialog(ImagePlus imp, String command, PlugInFilterRunner pfr) {
    // If using a stack, provide a preview graph of the noise for two methods
    if (imp.getStackSize() > 1) {
        this.pfr = pfr;
        drawPlot();
        gd = new NonBlockingGenericDialog(TITLE);
        gd.addHelp(About.HELP_URL);
        gd.addSlider("Percential", 0, 100, percentile);
        String[] noiseMethods = SettingsManager.getNames((Object[]) NoiseEstimator.Method.values());
        gd.addChoice("Noise_method", noiseMethods, noiseMethods[noiseMethod.ordinal()]);
        // For background based on pixel below a threshold
        String[] thresholdMethods = AutoThreshold.getMethods(true);
        gd.addChoice("Threshold_method", thresholdMethods, thresholdMethods[thresholdMethod.ordinal() - 1]);
        gd.addSlider("Fraction", 0, 0.999, fraction);
        gd.addNumericField("Histogram_size", histogramSize, 0);
        gd.addDialogListener(this);
        gd.addMessage("Click OK to compute table for all slices");
        gd.showDialog();
        if (gd.wasCanceled() || !dialogItemChanged(gd, null))
            return DONE;
    }
    return IJ.setupDialog(imp, FLAGS);
}
Also used : NonBlockingGenericDialog(ij.gui.NonBlockingGenericDialog)

Example 9 with NonBlockingGenericDialog

use of ij.gui.NonBlockingGenericDialog in project GDSC-SMLM by aherbert.

the class SmoothImage method showDialog.

@Override
public int showDialog(ImagePlus imp, String command, PlugInFilterRunner pfr) {
    // Note: We cannot use a NonBlockingGenericDialog as scrolling through the image
    // throws away the snap shot. The pixel data for the previous slice is then fixed
    // with the preview. So we can only support a single slice.
    final NonBlockingGenericDialog gd = new NonBlockingGenericDialog(TITLE);
    gd.addHelp(HelpUrls.getUrl("smooth-image"));
    settings = Settings.load();
    gd.addMessage("Smooth image:");
    gd.addChoice("Spot_filter", filterNames, filterNames[settings.filter1]);
    gd.addSlider("Smoothing", 0, 4.5, settings.smooth1);
    gd.addCheckbox("Difference_filter", settings.differenceFilter);
    gd.addChoice("Spot_filter2", filterNames, filterNames[settings.filter2]);
    gd.addSlider("Smoothing2", 1.5, 6, settings.smooth2);
    gd.addCheckbox("Auto_adjust_contrast", settings.autoAdjustConstrast);
    gd.addCheckbox("Allow_inversion", settings.allowInversion);
    gd.addPreviewCheckbox(pfr);
    gd.addDialogListener(this);
    dialogVisible = true;
    gd.showDialog();
    dialogVisible = false;
    if (gd.wasCanceled() || !dialogItemChanged(gd, null)) {
        return DONE;
    }
    return IJ.setupDialog(imp, FLAGS);
}
Also used : NonBlockingGenericDialog(ij.gui.NonBlockingGenericDialog)

Example 10 with NonBlockingGenericDialog

use of ij.gui.NonBlockingGenericDialog in project GDSC-SMLM by aherbert.

the class BenchmarkFilterAnalysis method saveTemplate.

/**
 * Save PeakFit configuration template using the current benchmark settings.
 *
 * @param topFilterSummary the top filter summary
 */
private void saveTemplate(String topFilterSummary) {
    final FitEngineConfiguration config = new FitEngineConfiguration();
    if (!updateAllConfiguration(config, true)) {
        IJ.log("Unable to create the template configuration");
        return;
    }
    // Remove the PSF width to make the template generic
    config.getFitConfiguration().setInitialPeakStdDev(0);
    // Only get this once when doing iterative analysis
    String filename;
    final boolean localSaveTemplateIsSet = saveTemplateIsSet;
    if (localSaveTemplateIsSet) {
        filename = settings.templateFilename;
    } else {
        filename = getFilename("Template_File", settings.templateFilename);
        saveTemplateIsSet = true;
    }
    if (filename != null) {
        settings.templateFilename = filename;
        Prefs.set(Settings.KEY_TEMPLATE_FILENAME, filename);
        final TemplateSettings.Builder templateSettings = TemplateSettings.newBuilder();
        getNotes(templateSettings, topFilterSummary);
        templateSettings.setFitEngineSettings(config.getFitEngineSettings());
        if (!SettingsManager.toJson(templateSettings.build(), filename, SettingsManager.FLAG_SILENT | SettingsManager.FLAG_JSON_WHITESPACE)) {
            IJ.log("Unable to save the template configuration");
            return;
        }
        // This need only be performed once as the sample image is the same for all iterations.
        if (localSaveTemplateIsSet) {
            return;
        }
        // Save some random frames from the test image data
        final ImagePlus imp = CreateData.getImage();
        if (imp == null) {
            return;
        }
        // Get the number of frames
        final ResultsImageSampler sampler = getSampler(results, imp);
        if (!sampler.isValid()) {
            return;
        }
        // Iteratively show the example until the user is happy.
        // Yes = OK, No = Repeat, Cancel = Do not save
        final String keyNo = "nNo";
        final String keyLow = "nLower";
        final String keyHigh = "nHigher";
        if (ImageJUtils.isMacro()) {
            // Collect the options if running in a macro
            final String options = Macro.getOptions();
            settings.countNo = Integer.parseInt(Macro.getValue(options, keyNo, Integer.toString(settings.countNo)));
            settings.countLow = Integer.parseInt(Macro.getValue(options, keyLow, Integer.toString(settings.countLow)));
            settings.countHigh = Integer.parseInt(Macro.getValue(options, keyHigh, Integer.toString(settings.countHigh)));
        } else if (settings.countLow + settings.countHigh == 0) {
            settings.countLow = settings.countHigh = 1;
        }
        final ImagePlus[] out = new ImagePlus[1];
        out[0] = sampler.getSample(settings.countNo, settings.countLow, settings.countHigh);
        if (!ImageJUtils.isMacro()) {
            // Show the template results
            final ConfigurationTemplate configTemplate = new ConfigurationTemplate();
            // Interactively show the sample image data
            final boolean[] close = new boolean[1];
            final ImagePlus[] outImp = new ImagePlus[1];
            if (out[0] != null) {
                final WindowOrganiser windowOrganiser = new WindowOrganiser();
                outImp[0] = display(out[0], windowOrganiser);
                if (windowOrganiser.isNotEmpty()) {
                    close[0] = true;
                    // Zoom a bit
                    final ImageWindow iw = outImp[0].getWindow();
                    for (int i = 7; i-- > 0 && Math.max(iw.getWidth(), iw.getHeight()) < 512; ) {
                        iw.getCanvas().zoomIn(0, 0);
                    }
                }
                configTemplate.createResults(outImp[0]);
            }
            // TODO - fix this when a second sample is made as the results are not updated.
            final ImageListener listener = new ImageListener() {

                @Override
                public void imageOpened(ImagePlus imp) {
                // Do nothing
                }

                @Override
                public void imageClosed(ImagePlus imp) {
                // Do nothing
                }

                @Override
                public void imageUpdated(ImagePlus imp) {
                    if (imp != null && imp == outImp[0]) {
                        configTemplate.updateResults(imp.getCurrentSlice());
                    }
                }
            };
            ImagePlus.addImageListener(listener);
            // Turn off the recorder when the dialog is showing
            final boolean record = Recorder.record;
            Recorder.record = false;
            final NonBlockingGenericDialog gd = new NonBlockingGenericDialog(TITLE);
            ImageJUtils.addMessage(gd, "Showing image data for the template example.\n \nSample Frames:\nEmpty = %d\n" + "Lower density = %d\nHigher density = %d\n", sampler.getNumberOfEmptySamples(), sampler.getNumberOfLowDensitySamples(), sampler.getNumberOfHighDensitySamples());
            gd.addSlider(keyNo, 0, 10, settings.countNo);
            gd.addSlider(keyLow, 0, 10, settings.countLow);
            gd.addSlider(keyHigh, 0, 10, settings.countHigh);
            gd.addDialogListener((genDialog, event) -> {
                // image the user has not seen.
                if (event == null) {
                    return true;
                }
                settings.countNo = (int) genDialog.getNextNumber();
                settings.countLow = (int) genDialog.getNextNumber();
                settings.countHigh = (int) genDialog.getNextNumber();
                out[0] = sampler.getSample(settings.countNo, settings.countLow, settings.countHigh);
                if (out[0] != null) {
                    final WindowOrganiser windowOrganiser = new WindowOrganiser();
                    outImp[0] = display(out[0], windowOrganiser);
                    if (windowOrganiser.isNotEmpty()) {
                        close[0] = true;
                        // Zoom a bit
                        final ImageWindow iw = outImp[0].getWindow();
                        for (int i = 7; i-- > 0 && Math.max(iw.getWidth(), iw.getHeight()) < 512; ) {
                            iw.getCanvas().zoomIn(0, 0);
                        }
                    }
                    configTemplate.createResults(outImp[0]);
                }
                return true;
            });
            gd.showDialog();
            if (gd.wasCanceled()) {
                out[0] = null;
                // For the recorder
                settings.countNo = settings.countLow = settings.countHigh = 0;
            }
            if (close[0]) {
                // Because closing the image sets the stack pixels array to null
                if (out[0] != null) {
                    out[0] = out[0].duplicate();
                }
                outImp[0].close();
            }
            configTemplate.closeResults();
            ImagePlus.removeImageListener(listener);
            if (record) {
                Recorder.record = true;
                Recorder.recordOption(keyNo, Integer.toString(settings.countNo));
                Recorder.recordOption(keyLow, Integer.toString(settings.countLow));
                Recorder.recordOption(keyHigh, Integer.toString(settings.countHigh));
            }
        }
        if (out[0] == null) {
            return;
        }
        final ImagePlus example = out[0];
        filename = FileUtils.replaceExtension(filename, ".tif");
        IJ.save(example, filename);
    }
}
Also used : ResultsImageSampler(uk.ac.sussex.gdsc.smlm.ij.results.ResultsImageSampler) ImageWindow(ij.gui.ImageWindow) ImageListener(ij.ImageListener) FitEngineConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration) ConfigurationTemplate(uk.ac.sussex.gdsc.smlm.ij.plugins.ConfigurationTemplate) WindowOrganiser(uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser) NonBlockingGenericDialog(ij.gui.NonBlockingGenericDialog) ImagePlus(ij.ImagePlus) TemplateSettings(uk.ac.sussex.gdsc.smlm.data.config.TemplateProtos.TemplateSettings)

Aggregations

NonBlockingGenericDialog (ij.gui.NonBlockingGenericDialog)10 ImagePlus (ij.ImagePlus)5 ImageListener (ij.ImageListener)3 GlobalSettings (gdsc.smlm.ij.settings.GlobalSettings)2 MemoryPeakResults (gdsc.smlm.results.MemoryPeakResults)2 ImageWindow (ij.gui.ImageWindow)2 Choice (java.awt.Choice)2 Point (java.awt.Point)2 FitEngineConfiguration (gdsc.smlm.engine.FitEngineConfiguration)1 FitConfiguration (gdsc.smlm.fitting.FitConfiguration)1 IJImageSource (gdsc.smlm.ij.IJImageSource)1 ResultsImageSampler (gdsc.smlm.ij.results.ResultsImageSampler)1 ImageStack (ij.ImageStack)1 DialogListener (ij.gui.DialogListener)1 GenericDialog (ij.gui.GenericDialog)1 AWTEvent (java.awt.AWTEvent)1 ImageAdapter (uk.ac.sussex.gdsc.core.ij.ImageAdapter)1 WindowOrganiser (uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser)1 TemplateSettings (uk.ac.sussex.gdsc.smlm.data.config.TemplateProtos.TemplateSettings)1 FitEngineConfiguration (uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration)1