Search in sources :

Example 1 with PeakResultsList

use of uk.ac.sussex.gdsc.smlm.results.PeakResultsList in project GDSC-SMLM by aherbert.

the class PeakFit method createFitEngine.

/**
 * Creates a fitting engine using the current configuration.
 *
 * @param numberOfThreads the number of threads
 * @param queue the queue
 * @param queueSize the queue size
 * @return The fiting engine
 */
public FitEngine createFitEngine(int numberOfThreads, FitQueue queue, int queueSize) {
    // Ensure thread safety
    final PeakResultsList list = (numberOfThreads > 1) ? results.getThreadSafeList() : results;
    // Reduce to single object for speed
    final PeakResults r = (results.numberOfOutputs() == 1) ? list.toArray()[0] : list;
    // Update the configuration
    if (!updateFitConfiguration(config)) {
        return null;
    }
    final FitEngine engine = FitEngine.create(config, r, numberOfThreads, queue, queueSize);
    // Write settings out to the IJ log
    if (resultsSettings.getLogProgress()) {
        IJ.log(LOG_SPACER);
        IJ.log("Peak Fit");
        IJ.log(LOG_SPACER);
        ImageJUtils.log("Initial Peak SD = %s,%s", MathUtils.rounded(fitConfig.getInitialXSd()), MathUtils.rounded(fitConfig.getInitialYSd()));
        final SpotFilter spotFilter = engine.getSpotFilter();
        IJ.log("Spot Filter = " + spotFilter.getDescription());
        final int w = 2 * engine.getFitting() + 1;
        ImageJUtils.log("Fit window = %d x %d", w, w);
        if (!fitConfig.isDisableSimpleFilter()) {
            IJ.log("Coordinate shift = " + MathUtils.rounded(config.getFitConfiguration().getCoordinateShift()));
            IJ.log("Signal strength = " + MathUtils.rounded(fitConfig.getSignalStrength()));
        }
        if (fitConfig.isDirectFilter()) {
            IJ.log("Smart filter = " + fitConfig.getSmartFilter().getDescription());
        }
        if (extraOptions) {
            IJ.log("Noise = " + MathUtils.rounded(fitConfig.getNoise()));
        }
        IJ.log("Width factor = " + MathUtils.rounded(fitConfig.getMaxWidthFactor()));
        IJ.log(LOG_SPACER);
    }
    return engine;
}
Also used : PeakResultsList(uk.ac.sussex.gdsc.smlm.results.PeakResultsList) FitEngine(uk.ac.sussex.gdsc.smlm.engine.FitEngine) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) FilePeakResults(uk.ac.sussex.gdsc.smlm.results.FilePeakResults) ImageJTablePeakResults(uk.ac.sussex.gdsc.smlm.ij.results.ImageJTablePeakResults) ImageJImagePeakResults(uk.ac.sussex.gdsc.smlm.ij.results.ImageJImagePeakResults) PeakResults(uk.ac.sussex.gdsc.smlm.results.PeakResults) SpotFilter(uk.ac.sussex.gdsc.smlm.filters.SpotFilter)

Example 2 with PeakResultsList

use of uk.ac.sussex.gdsc.smlm.results.PeakResultsList in project GDSC-SMLM by aherbert.

the class TcPalmAnalysis method run.

@Override
public void run(String arg) {
    SmlmUsageTracker.recordPlugin(this.getClass(), arg);
    if (MemoryPeakResults.isMemoryEmpty()) {
        IJ.error(TITLE, "No localisations in memory");
        return;
    }
    // Only allow 1 instance to run
    if (!instanceLock.acquire()) {
        final Window w = WindowManager.getWindow(TITLE);
        if (w != null) {
            w.toFront();
            return;
        }
    // Fall through to allow the plugin to run. This may still have concurrency issues if
    // another version is running but the window is not currently showing/registered with
    // the window manager. Perhaps show a dialog asking to continue.
    }
    if (!showDialog()) {
        instanceLock.release();
        return;
    }
    // Load the results
    results = ResultsManager.loadInputResults(settings.getInputOption(), false, null, null);
    if (MemoryPeakResults.isEmpty(results)) {
        IJ.error(TITLE, "No results could be loaded");
        instanceLock.release();
        return;
    }
    // Allocate singles an id for analysis.
    results = results.copyAndAssignZeroIds();
    dataCalibration = new DataCalibration(results.getCalibration());
    // Show a super-resolution image where clusters can be selected.
    final Rectangle bounds = results.getBounds(true);
    final PeakResultsList resultsList = new PeakResultsList();
    resultsList.copySettings(results);
    ResultsManager.addImageResults(resultsList, settings.getResultsImageSettings(), bounds, 0);
    resultsList.begin();
    resultsList.addAll(results.toArray());
    resultsList.end();
    image = (ImageJImagePeakResults) resultsList.getOutput(0);
    // Note: Setting the lut name in the image only has an effect if the image is not showing
    // thus the lut is applied afterwards.
    final ImagePlus imp = image.getImagePlus();
    if (TextUtils.isNotEmpty(image.getLutName())) {
        imp.setLut(LutHelper.createLut(LutColour.forName(image.getLutName()), true));
    }
    // Set-up analysis processing:
    // Store latest image ROI bounds and analysis settings.
    // ConcurrentMonoStack to store next image ROI bounds and analysis settings.
    // When image is clicked submit for analysis.
    // When settings are changed submit for analysis.
    // Submit for analysis checks if ROI is area ROI. if so it:
    // - adds current settings to the next analysis monostack
    // - acquires a softlock and if available submits a runnable to do the analysis until
    // the monostack is empty
    lock = new SoftLock();
    workQueue = new ConcurrentMonoStack<>();
    // Use the current ROI (which may remain from previous plugin execution)
    previous = new Work(0, imp.getRoi(), settings.build());
    executor = Executors.newSingleThreadExecutor();
    // Create the bounds and activation times for each cluster
    clusterData = createClusterData(results);
    // Add interactive monitor to the image where clusters can be selected.
    // For all selected clusters show on an Activations-vs-Time plot.
    final RoiListener roiListener = new RoiListener() {

        @Override
        public void roiModified(ImagePlus imp2, int id) {
            if (imp2 != null && imp.getID() == imp2.getID()) {
                addWork(imp.getRoi());
            }
        }
    };
    Roi.addRoiListener(roiListener);
    // Add monitor for the selection of clusters in the current clusters table
    clusterSelectedListener = new ClusterSelectedListener();
    // Initialise the loop view
    loopImage = new LoopImage().setSettings(previous.settings).setColourMap(colourMap);
    // steepness parameter: local window size (sec) and activation rate (per sec)
    try {
        showAnalysisDialog();
    } finally {
        Roi.removeRoiListener(roiListener);
        // Remove the action from the single instance of the current clusters table
        removeListener(currentGroupsTable.get());
        removeListener(currentClustersTable.get());
        removeListener(allClustersTable.get());
        executor.shutdown();
        instanceLock.release();
        SettingsManager.writeSettings(settings);
    }
}
Also used : Window(java.awt.Window) PlotWindow(ij.gui.PlotWindow) PeakResultsList(uk.ac.sussex.gdsc.smlm.results.PeakResultsList) RoiListener(ij.gui.RoiListener) Rectangle(java.awt.Rectangle) ImagePlus(ij.ImagePlus) SoftLock(uk.ac.sussex.gdsc.core.utils.SoftLock)

Example 3 with PeakResultsList

use of uk.ac.sussex.gdsc.smlm.results.PeakResultsList in project GDSC-SMLM by aherbert.

the class ResultsManager method run.

@Override
public void run(String arg) {
    extraOptions = ImageJUtils.isExtraOptions();
    SmlmUsageTracker.recordPlugin(this.getClass(), arg);
    if ("load".equals(arg)) {
        batchLoad();
        return;
    }
    if ("save".equals(arg)) {
        batchSave();
        return;
    }
    if (StringUtils.startsWith(arg, "clear")) {
        runClearMemory(arg);
        return;
    }
    if (!showDialog()) {
        return;
    }
    final MemoryPeakResults results = loadResults(settings.inputOption);
    if (MemoryPeakResults.isEmpty(results)) {
        IJ.error(TITLE, "No results could be loaded");
        IJ.showStatus("");
        return;
    }
    IJ.showStatus("Loaded " + TextUtils.pleural(results.size(), "result"));
    boolean saved = false;
    if (resultsSettings.getResultsInMemorySettings().getInMemory() && fileInput) {
        if (!addResultsToMemory(results, settings.inputFilename)) {
            IJ.showStatus("");
            return;
        }
        saved = true;
    }
    if (resultsSettings.getResultsTableSettings().getResultsTableFormatValue() <= 0 && resultsSettings.getResultsImageSettings().getImageTypeValue() <= 0 && TextUtils.isNullOrEmpty(resultsSettings.getResultsFileSettings().getResultsFilename())) {
        // No outputs. Error if results were not saved to memory
        if (!saved) {
            IJ.error(TITLE, "No output selected");
        }
        return;
    }
    final Rectangle bounds = results.getBounds(true);
    final boolean showDeviations = resultsSettings.getShowDeviations() && canShowDeviations(results);
    final boolean showEndFrame = canShowEndFrame(results);
    final boolean showId = canShowId(results);
    final boolean showCategory = canShowCategory(results);
    // Display the configured output
    final PeakResultsList outputList = new PeakResultsList();
    outputList.copySettings(results);
    final int tableFormat = resultsSettings.getResultsTableSettings().getResultsTableFormatValue();
    if (tableFormat == ResultsTableFormat.IMAGEJ_VALUE) {
        addImageJTableResults(outputList, resultsSettings.getResultsTableSettings(), showDeviations, showEndFrame, results.is3D(), showId, showCategory);
    } else if (tableFormat == ResultsTableFormat.INTERACTIVE_VALUE) {
        showInteractiveTable(results, resultsSettings.getResultsTableSettings());
    }
    addImageResults(outputList, resultsSettings.getResultsImageSettings(), bounds, (extraOptions) ? FLAG_EXTRA_OPTIONS : 0);
    addFileResults(outputList, showDeviations, showEndFrame, showId, showCategory);
    if (outputList.numberOfOutputs() == 0) {
        // This occurs when only using the interactive table
        IJ.showStatus("Processed " + TextUtils.pleural(results.size(), "result"));
        return;
    }
    IJ.showStatus("Processing outputs ...");
    // Reduce to single object for speed
    final PeakResults output = (outputList.numberOfOutputs() == 1) ? outputList.toArray()[0] : outputList;
    output.begin();
    // Note: We could add a batch iterator to the MemoryPeakResults.
    // However the speed increase will be marginal as the main time
    // taken is in processing the outputs.
    // Process in batches to provide progress
    final Counter progress = new Counter();
    final int totalProgress = results.size();
    final int batchSize = Math.max(100, totalProgress / 10);
    final FixedPeakResultList batch = new FixedPeakResultList(batchSize);
    IJ.showProgress(0);
    results.forEach((PeakResultProcedureX) result -> {
        batch.add(result);
        if (batch.size == batchSize) {
            if (IJ.escapePressed()) {
                batch.clear();
                return true;
            }
            output.addAll(batch.results);
            batch.clear();
            IJ.showProgress(progress.incrementAndGet(batchSize), totalProgress);
        }
        return false;
    });
    // Will be empty if interrupted
    if (batch.isNotEmpty()) {
        output.addAll(batch.toArray());
    }
    IJ.showProgress(1);
    output.end();
    if (output.size() == results.size()) {
        IJ.showStatus("Processed " + TextUtils.pleural(results.size(), "result"));
    } else {
        IJ.showStatus(String.format("A %d/%s", output.size(), TextUtils.pleural(results.size(), "result")));
    }
}
Also used : PeakResultsList(uk.ac.sussex.gdsc.smlm.results.PeakResultsList) Choice(java.awt.Choice) Java2(ij.util.Java2) Arrays(java.util.Arrays) Rectangle2D(java.awt.geom.Rectangle2D) ItemListener(java.awt.event.ItemListener) StringUtils(org.apache.commons.lang3.StringUtils) ResultsImageSettings(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsImageSettings) ResultsSettings(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsSettings) Panel(java.awt.Panel) ImageJPluginLoggerHelper(uk.ac.sussex.gdsc.core.ij.ImageJPluginLoggerHelper) YesNoCancelDialog(ij.gui.YesNoCancelDialog) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) ResultOption(uk.ac.sussex.gdsc.smlm.results.ResultOption) Map(java.util.Map) ImageJImagePeakResults(uk.ac.sussex.gdsc.smlm.ij.results.ImageJImagePeakResults) PeakResults(uk.ac.sussex.gdsc.smlm.results.PeakResults) JFileChooser(javax.swing.JFileChooser) MathUtils(uk.ac.sussex.gdsc.core.utils.MathUtils) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) EnumSet(java.util.EnumSet) LutHelper(uk.ac.sussex.gdsc.core.ij.process.LutHelper) SettingsManager(uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager) CameraType(uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.CameraType) Predicate(java.util.function.Predicate) Collection(java.util.Collection) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) ResultsInMemorySettings(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsInMemorySettings) PeakResultsList(uk.ac.sussex.gdsc.smlm.results.PeakResultsList) Set(java.util.Set) ResultsImageType(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsImageType) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) Logger(java.util.logging.Logger) TextUtils(uk.ac.sussex.gdsc.core.utils.TextUtils) BitFlagUtils(uk.ac.sussex.gdsc.core.utils.BitFlagUtils) List(java.util.List) Builder(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsSettings.Builder) PeakResultTableModelFrame(uk.ac.sussex.gdsc.smlm.ij.gui.PeakResultTableModelFrame) BinaryFilePeakResults(uk.ac.sussex.gdsc.smlm.results.BinaryFilePeakResults) FileUtils(uk.ac.sussex.gdsc.core.utils.FileUtils) PlugIn(ij.plugin.PlugIn) Rectangle(java.awt.Rectangle) PeakResultProcedureX(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedureX) Prefs(ij.Prefs) ValidationUtils(uk.ac.sussex.gdsc.core.utils.ValidationUtils) HashMap(java.util.HashMap) IntensityUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.IntensityUnit) FixedPeakResultList(uk.ac.sussex.gdsc.smlm.results.FixedPeakResultList) ResultsFileSettings(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsFileSettings) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) OptionListener(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog.OptionListener) OpenDialog(ij.io.OpenDialog) ArrayList(java.util.ArrayList) IJImageSource(uk.ac.sussex.gdsc.smlm.ij.IJImageSource) MultiDialog(uk.ac.sussex.gdsc.core.ij.gui.MultiDialog) ResultsProtosHelper(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtosHelper) ResultsFileFormat(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsFileFormat) ImagePeakResultsFactory(uk.ac.sussex.gdsc.smlm.ij.results.ImagePeakResultsFactory) GenericDialog(ij.gui.GenericDialog) PeakResultTableModel(uk.ac.sussex.gdsc.smlm.ij.gui.PeakResultTableModel) MalkFilePeakResults(uk.ac.sussex.gdsc.smlm.results.MalkFilePeakResults) PeakResultsReader(uk.ac.sussex.gdsc.smlm.results.PeakResultsReader) LinkedHashSet(java.util.LinkedHashSet) TsfPeakResultsWriter(uk.ac.sussex.gdsc.smlm.results.TsfPeakResultsWriter) EventQueue(java.awt.EventQueue) ResultsTableFormat(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsTableFormat) SimpleImageJTrackProgress(uk.ac.sussex.gdsc.core.ij.SimpleImageJTrackProgress) Checkbox(java.awt.Checkbox) Label(java.awt.Label) IOException(java.io.IOException) ResultsImageMode(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsImageMode) Recorder(ij.plugin.frame.Recorder) File(java.io.File) Constants(uk.ac.sussex.gdsc.smlm.ij.settings.Constants) ImageJTablePeakResults(uk.ac.sussex.gdsc.smlm.ij.results.ImageJTablePeakResults) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) TextFilePeakResults(uk.ac.sussex.gdsc.smlm.results.TextFilePeakResults) IJ(ij.IJ) ExtendedPeakResult(uk.ac.sussex.gdsc.smlm.results.ExtendedPeakResult) ResultsTableSettings(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsTableSettings) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) FixedPeakResultList(uk.ac.sussex.gdsc.smlm.results.FixedPeakResultList) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) ImageJImagePeakResults(uk.ac.sussex.gdsc.smlm.ij.results.ImageJImagePeakResults) PeakResults(uk.ac.sussex.gdsc.smlm.results.PeakResults) BinaryFilePeakResults(uk.ac.sussex.gdsc.smlm.results.BinaryFilePeakResults) MalkFilePeakResults(uk.ac.sussex.gdsc.smlm.results.MalkFilePeakResults) ImageJTablePeakResults(uk.ac.sussex.gdsc.smlm.ij.results.ImageJTablePeakResults) TextFilePeakResults(uk.ac.sussex.gdsc.smlm.results.TextFilePeakResults) Rectangle(java.awt.Rectangle) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Example 4 with PeakResultsList

use of uk.ac.sussex.gdsc.smlm.results.PeakResultsList in project GDSC-SMLM by aherbert.

the class PeakFit method initialiseImage.

/**
 * Initialise a new image.
 *
 * <p>Does not set-up for fitting. This can be done using a subsequent call to
 * {@link #initialiseFitting()}.
 *
 * <p>This mechanism allows additional result outputs to be added after initialisation using
 * {@link #addPeakResults(PeakResults)}.
 *
 * @param imageSource The image source
 * @param bounds The region to process from the image
 * @param ignoreBoundsForNoise Set to true if the bounds should be ignored when computing the
 *        noise estimate for each frame
 * @return True if the image was valid and the initialisation was successful
 */
public boolean initialiseImage(ImageSource imageSource, Rectangle bounds, boolean ignoreBoundsForNoise) {
    // Initialise for image processing
    if (!setSource(imageSource)) {
        return false;
    }
    if (bounds == null) {
        // No region so no need to ignore the bounds.
        this.bounds = new Rectangle(0, 0, source.getWidth(), source.getHeight());
        this.ignoreBoundsForNoise = false;
    } else {
        // The bounds must fit in the image
        try {
            imageSource.checkBounds(bounds);
        } catch (final IllegalArgumentException ex) {
            return false;
        }
        this.bounds = bounds;
        this.ignoreBoundsForNoise = ignoreBoundsForNoise;
    }
    results = new PeakResultsList();
    time = 0;
    return true;
}
Also used : PeakResultsList(uk.ac.sussex.gdsc.smlm.results.PeakResultsList) Rectangle(java.awt.Rectangle)

Example 5 with PeakResultsList

use of uk.ac.sussex.gdsc.smlm.results.PeakResultsList in project GDSC-SMLM by aherbert.

the class PulseActivationAnalysis method createOutput.

private PeakResultsList createOutput(int channel) {
    final PeakResultsList outputList = new PeakResultsList();
    outputList.copySettings(results);
    if (settings.channels > 1) {
        outputList.setName(results.getName() + " " + title + " C" + channel);
    } else {
        outputList.setName(results.getName() + " " + title);
    }
    // Store the set in memory
    final MemoryPeakResults memoryResults = new MemoryPeakResults(this.results.size());
    outputList.addOutput(memoryResults);
    MemoryPeakResults.addResults(memoryResults);
    // Draw the super-resolution image
    final Rectangle bounds = results.getBounds(true);
    addImageResults(outputList, results.getName(), bounds, results.getNmPerPixel(), resultsSettingsBuilder.getResultsImageSettings());
    outputList.begin();
    return outputList;
}
Also used : PeakResultsList(uk.ac.sussex.gdsc.smlm.results.PeakResultsList) Rectangle(java.awt.Rectangle) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Aggregations

PeakResultsList (uk.ac.sussex.gdsc.smlm.results.PeakResultsList)5 Rectangle (java.awt.Rectangle)4 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)3 IJ (ij.IJ)1 ImagePlus (ij.ImagePlus)1 Prefs (ij.Prefs)1 GenericDialog (ij.gui.GenericDialog)1 PlotWindow (ij.gui.PlotWindow)1 RoiListener (ij.gui.RoiListener)1 YesNoCancelDialog (ij.gui.YesNoCancelDialog)1 OpenDialog (ij.io.OpenDialog)1 PlugIn (ij.plugin.PlugIn)1 Recorder (ij.plugin.frame.Recorder)1 Java2 (ij.util.Java2)1 Checkbox (java.awt.Checkbox)1 Choice (java.awt.Choice)1 EventQueue (java.awt.EventQueue)1 Label (java.awt.Label)1 Panel (java.awt.Panel)1 Window (java.awt.Window)1