Search in sources :

Example 6 with TurboList

use of gdsc.core.utils.TurboList in project GDSC-SMLM by aherbert.

the class CropResults method showDialog.

private boolean showDialog() {
    ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
    gd.addHelp(About.HELP_URL);
    // TODO - add option to crop using the bounding rectangle of an ROI on an open image.
    // See PC-PALM Molecules.
    // Build a list of all images with a region ROI
    TurboList<String> titles = new TurboList<String>(WindowManager.getWindowCount());
    if (WindowManager.getWindowCount() > 0) {
        for (int imageID : WindowManager.getIDList()) {
            ImagePlus imp = WindowManager.getImage(imageID);
            if (imp != null && imp.getRoi() != null && imp.getRoi().isArea())
                titles.add(imp.getTitle());
        }
    }
    Rectangle bounds = results.getBounds(true);
    gd.addMessage(String.format("x=%d,y=%d,w=%d,h=%d", bounds.x, bounds.y, bounds.width, bounds.height));
    gd.addNumericField("Border", border, 2);
    gd.addCheckbox("Select_region", selectRegion);
    gd.addNumericField("X", x, 2);
    gd.addNumericField("Y", y, 2);
    gd.addNumericField("Width", width, 2);
    gd.addNumericField("Height", height, 2);
    if (!titles.isEmpty()) {
        gd.addCheckbox("Use_ROI", useRoi);
        String[] items = titles.toArray(new String[titles.size()]);
        gd.addChoice("Image", items, roiImage);
    }
    gd.addCheckbox("Overwrite", overwrite);
    gd.showDialog();
    if (gd.wasCanceled())
        return false;
    border = Math.max(0, gd.getNextNumber());
    selectRegion = gd.getNextBoolean();
    x = gd.getNextNumber();
    y = gd.getNextNumber();
    width = Math.max(0, gd.getNextNumber());
    height = Math.max(0, gd.getNextNumber());
    if (!titles.isEmpty()) {
        myUseRoi = useRoi = gd.getNextBoolean();
        roiImage = gd.getNextChoice();
    }
    overwrite = gd.getNextBoolean();
    return true;
}
Also used : TurboList(gdsc.core.utils.TurboList) Rectangle(java.awt.Rectangle) ExtendedGenericDialog(ij.gui.ExtendedGenericDialog) ImagePlus(ij.ImagePlus)

Example 7 with TurboList

use of gdsc.core.utils.TurboList in project GDSC-SMLM by aherbert.

the class CMOSAnalysis method run.

private void run() {
    long start = System.currentTimeMillis();
    // Avoid all the file saves from updating the progress bar and status line
    Utils.setShowProgress(false);
    Utils.setShowStatus(false);
    JLabel statusLine = Utils.getStatusLine();
    progressBar = Utils.getProgressBar();
    // Create thread pool and workers
    ExecutorService executor = Executors.newFixedThreadPool(getThreads());
    TurboList<Future<?>> futures = new TurboList<Future<?>>(nThreads);
    TurboList<ImageWorker> workers = new TurboList<ImageWorker>(nThreads);
    double[][][] data = new double[subDirs.size()][2][];
    double[] pixelOffset = null, pixelVariance = null;
    Statistics statsOffset = null, statsVariance = null;
    // For each sub-directory compute the mean and variance
    final int nSubDirs = subDirs.size();
    boolean error = false;
    for (int n = 0; n < nSubDirs; n++) {
        SubDir sd = subDirs.getf(n);
        statusLine.setText("Analysing " + sd.name);
        // Open the series
        SeriesImageSource source = new SeriesImageSource(sd.name, sd.path.getPath());
        //source.setLogProgress(true);
        if (!source.open()) {
            error = true;
            IJ.error(TITLE, "Failed to open image series: " + sd.path.getPath());
            break;
        }
        // So the bar remains at 99% when workers have finished
        totalProgress = source.getFrames() + 1;
        stepProgress = Utils.getProgressInterval(totalProgress);
        progress = 0;
        progressBar.show(0);
        ArrayMoment moment = (rollingAlgorithm) ? new RollingArrayMoment() : new SimpleArrayMoment();
        final BlockingQueue<ImageJob> jobs = new ArrayBlockingQueue<ImageJob>(nThreads * 2);
        for (int i = 0; i < nThreads; i++) {
            final ImageWorker worker = new ImageWorker(jobs, moment);
            workers.add(worker);
            futures.add(executor.submit(worker));
        }
        // Process the data
        for (float[] pixels = source.next(); pixels != null; pixels = source.next()) {
            put(jobs, new ImageJob(pixels));
        }
        // Finish all the worker threads by passing in a null job
        for (int i = 0; i < nThreads; i++) {
            put(jobs, new ImageJob(null));
        }
        // Wait for all to finish
        for (int t = futures.size(); t-- > 0; ) {
            try {
                // The future .get() method will block until completed
                futures.get(t).get();
            } catch (Exception e) {
                // This should not happen. 
                e.printStackTrace();
            }
        }
        // Create the final aggregate statistics
        for (ImageWorker w : workers) moment.add(w.moment);
        data[n][0] = moment.getFirstMoment();
        data[n][1] = moment.getVariance();
        // Reset
        futures.clear();
        workers.clear();
        Statistics s = new Statistics(data[n][0]);
        if (n != 0) {
            // Compute mean ADU
            Statistics signal = new Statistics();
            double[] mean = data[n][0];
            for (int i = 0; i < pixelOffset.length; i++) signal.add(mean[i] - pixelOffset[i]);
            Utils.log("%s Mean = %s +/- %s. Signal = %s +/- %s ADU", sd.name, Utils.rounded(s.getMean()), Utils.rounded(s.getStandardDeviation()), Utils.rounded(signal.getMean()), Utils.rounded(signal.getStandardDeviation()));
            // TODO - optionally save
            ImageStack stack = new ImageStack(source.getWidth(), source.getHeight());
            stack.addSlice("Mean", Utils.toFloat(data[n][0]));
            stack.addSlice("Variance", Utils.toFloat(data[n][1]));
            IJ.save(new ImagePlus("PerPixel", stack), new File(directory, "perPixel" + sd.name + ".tif").getPath());
        } else {
            pixelOffset = data[0][0];
            pixelVariance = data[0][1];
            statsOffset = s;
            statsVariance = new Statistics(pixelVariance);
            Utils.log("%s Offset = %s +/- %s. Variance = %s +/- %s", sd.name, Utils.rounded(s.getMean()), Utils.rounded(s.getStandardDeviation()), Utils.rounded(statsVariance.getMean()), Utils.rounded(statsVariance.getStandardDeviation()));
        }
    }
    Utils.setShowStatus(true);
    Utils.setShowProgress(true);
    IJ.showProgress(1);
    executor.shutdown();
    if (error)
        return;
    // Compute the gain
    statusLine.setText("Computing gain");
    double[] pixelGain = new double[pixelOffset.length];
    // Ignore first as this is the 0 exposure image
    for (int i = 0; i < pixelGain.length; i++) {
        // Use equation 2.5 from the Huang et al paper.
        double bibiT = 0;
        double biaiT = 0;
        for (int n = 1; n < nSubDirs; n++) {
            double bi = data[n][0][i] - pixelOffset[i];
            double ai = data[n][1][i] - pixelVariance[i];
            bibiT += bi * bi;
            biaiT += bi * ai;
        }
        pixelGain[i] = biaiT / bibiT;
    }
    Statistics statsGain = new Statistics(pixelGain);
    Utils.log("Gain Mean = %s +/- %s", Utils.rounded(statsGain.getMean()), Utils.rounded(statsGain.getStandardDeviation()));
    // Histogram of offset, variance and gain
    int bins = Utils.getBinsSturges(pixelGain.length);
    WindowOrganiser wo = new WindowOrganiser();
    showHistogram("Offset", pixelOffset, bins, statsOffset, wo);
    showHistogram("Variance", pixelVariance, bins, statsVariance, wo);
    showHistogram("Gain", pixelGain, bins, statsGain, wo);
    wo.tile();
    // Save
    measuredStack = new ImageStack(size, size);
    measuredStack.addSlice("Offset", Utils.toFloat(pixelOffset));
    measuredStack.addSlice("Variance", Utils.toFloat(pixelVariance));
    measuredStack.addSlice("Gain", Utils.toFloat(pixelGain));
    IJ.save(new ImagePlus("PerPixel", measuredStack), new File(directory, "perPixel.tif").getPath());
    // Remove the status from the ij.io.ImageWriter class
    IJ.showStatus("");
    Utils.log("Analysis time = " + Utils.timeToString(System.currentTimeMillis() - start));
}
Also used : RollingArrayMoment(gdsc.core.math.RollingArrayMoment) ArrayMoment(gdsc.core.math.ArrayMoment) RollingArrayMoment(gdsc.core.math.RollingArrayMoment) SimpleArrayMoment(gdsc.core.math.SimpleArrayMoment) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) SimpleArrayMoment(gdsc.core.math.SimpleArrayMoment) TurboList(gdsc.core.utils.TurboList) ImageStack(ij.ImageStack) SeriesImageSource(gdsc.smlm.ij.SeriesImageSource) JLabel(javax.swing.JLabel) WindowOrganiser(ij.plugin.WindowOrganiser) Statistics(gdsc.core.utils.Statistics) ImagePlus(ij.ImagePlus) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) File(java.io.File)

Example 8 with TurboList

use of gdsc.core.utils.TurboList in project GDSC-SMLM by aherbert.

the class ResultsManager method run.

/*
	 * (non-Javadoc)
	 * 
	 * @see ij.plugin.PlugIn#run(java.lang.String)
	 */
public void run(String arg) {
    SMLMUsageTracker.recordPlugin(this.getClass(), arg);
    if (arg != null && arg.startsWith("clear")) {
        Collection<MemoryPeakResults> allResults;
        boolean removeAll = false;
        if (arg.contains("multi")) {
            MultiDialog md = new MultiDialog(TITLE, new MultiDialog.MemoryResultsItems());
            md.addSelected(selected);
            md.showDialog();
            if (md.wasCanceled())
                return;
            selected = md.getSelectedResults();
            if (selected.isEmpty())
                return;
            allResults = new ArrayList<MemoryPeakResults>(selected.size());
            for (String name : selected) {
                MemoryPeakResults r = MemoryPeakResults.getResults(name);
                if (r != null)
                    allResults.add(r);
            }
        } else {
            removeAll = true;
            allResults = MemoryPeakResults.getAllResults();
        }
        if (allResults.isEmpty())
            return;
        long memorySize = 0;
        int size = 0;
        for (MemoryPeakResults results : allResults) {
            memorySize += MemoryPeakResults.estimateMemorySize(results.getResults());
            size += results.size();
        }
        String memory = MemoryPeakResults.memorySizeString(memorySize);
        String count = Utils.pleural(size, "result");
        String sets = Utils.pleural(allResults.size(), "set");
        GenericDialog gd = new GenericDialog(TITLE);
        gd.addMessage(String.format("Do you want to remove %s from memory (%s, %s)?", count, sets, memory));
        gd.enableYesNoCancel();
        gd.showDialog();
        if (!gd.wasOKed())
            return;
        if (removeAll)
            MemoryPeakResults.clearMemory();
        else {
            for (MemoryPeakResults results : allResults) MemoryPeakResults.removeResults(results.getName());
        }
        SummariseResults.clearSummaryTable();
        IJ.log(String.format("Cleared %s (%s, %s)", count, sets, memory));
        return;
    }
    if (!showDialog())
        return;
    MemoryPeakResults results = loadResults(inputOption);
    if (results == null || results.size() == 0) {
        IJ.error(TITLE, "No results could be loaded");
        IJ.showStatus("");
        return;
    }
    results = cropToRoi(results);
    if (results.size() == 0) {
        IJ.error(TITLE, "No results within the crop region");
        return;
    }
    if (resultsSettings.resultsInMemory && fileInput)
        MemoryPeakResults.addResults(results);
    IJ.showStatus("Processing outputs ...");
    Rectangle bounds = results.getBounds(true);
    boolean showDeviations = resultsSettings.showDeviations && canShowDeviations(results);
    boolean showEndFrame = canShowEndFrame(results);
    boolean showId = canShowId(results);
    // Display the configured output
    PeakResultsList outputList = new PeakResultsList();
    outputList.copySettings(results);
    //String title = results.getSource();
    //if (title == null || title.length() == 0)
    //	output.setSource(TITLE);
    addTableResults(results, outputList, showDeviations, showEndFrame);
    addImageResults(outputList, results.getName(), bounds, results.getNmPerPixel(), results.getGain());
    addFileResults(outputList, showDeviations, showEndFrame, showId);
    // Reduce to single object for speed
    PeakResults output = (outputList.numberOfOutputs() == 1) ? outputList.toArray()[0] : outputList;
    output.begin();
    // Process in batches to provide progress
    List<PeakResult> list = results.getResults();
    int progress = 0;
    int totalProgress = list.size();
    int stepProgress = Utils.getProgressInterval(totalProgress);
    TurboList<PeakResult> batch = new TurboList<PeakResult>(stepProgress);
    for (PeakResult result : list) {
        if (progress % stepProgress == 0) {
            IJ.showProgress(progress, totalProgress);
        }
        progress++;
        batch.addf(result);
        if (batch.size() == stepProgress) {
            output.addAll(batch);
            batch.clearf();
            if (isInterrupted())
                break;
        }
    }
    IJ.showProgress(1);
    output.end();
    IJ.showStatus(String.format("Processed %d result%s", results.size(), (results.size() > 1) ? "s" : ""));
}
Also used : TurboList(gdsc.core.utils.TurboList) Rectangle(java.awt.Rectangle) PeakResult(gdsc.smlm.results.PeakResult) ExtendedPeakResult(gdsc.smlm.results.ExtendedPeakResult) PeakResultsList(gdsc.smlm.results.PeakResultsList) PeakResults(gdsc.smlm.results.PeakResults) IJImagePeakResults(gdsc.smlm.ij.results.IJImagePeakResults) IJTablePeakResults(gdsc.smlm.ij.results.IJTablePeakResults) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) BinaryFilePeakResults(gdsc.smlm.results.BinaryFilePeakResults) MALKFilePeakResults(gdsc.smlm.results.MALKFilePeakResults) FilePeakResults(gdsc.smlm.results.FilePeakResults) ExtendedGenericDialog(ij.gui.ExtendedGenericDialog) GenericDialog(ij.gui.GenericDialog) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults)

Example 9 with TurboList

use of gdsc.core.utils.TurboList in project GDSC-SMLM by aherbert.

the class PulseActivationAnalysis method createActivations.

/**
	 * Creates the activations. This splits the input traces into continuous chains of localisations. Each chain is an
	 * activation. A new activation is created if there are more than the configured number of dark frames since the
	 * last localisation. The start frame for the activation defines the channel the activation is assigned to (this may
	 * be channel 0 if the start frame is not in a pulse start frame).
	 */
private void createActivations() {
    TurboList<Activation> activations = new TurboList<Activation>(traces.length);
    // Activations are only counted if there are at least 
    // n frames between localisations.
    final int n = darkFramesForNewActivation + 1;
    for (Trace trace : traces) {
        // Time-order
        trace.sort();
        ArrayList<PeakResult> points = trace.getPoints();
        // Define the frame for a new activation 
        int nextActivationStartFrame = Integer.MIN_VALUE;
        Trace current = null;
        int channel = 0;
        for (int j = 0; j < points.size(); j++) {
            PeakResult p = points.get(j);
            // Check if this is an activation
            if (p.getFrame() >= nextActivationStartFrame) {
                if (current != null)
                    // Store the last
                    activations.add(new Activation(current, channel));
                // Create a new activation
                current = new Trace(p);
                channel = getChannel(p);
            } else {
                // This is the same chain of localisations
                current.add(p);
            }
            nextActivationStartFrame = p.getEndFrame() + n;
        }
        if (current != null)
            activations.add(new Activation(current, channel));
    }
    save(activations);
}
Also used : Trace(gdsc.smlm.results.Trace) TurboList(gdsc.core.utils.TurboList) PeakResult(gdsc.smlm.results.PeakResult) IdPeakResult(gdsc.smlm.results.IdPeakResult)

Example 10 with TurboList

use of gdsc.core.utils.TurboList in project GDSC-SMLM by aherbert.

the class ErfGaussian2DFunctionTest method functionIsFasterThanEquivalentGaussian2DFunction.

// Speed test verses equivalent Gaussian2DFunction
@Test
public void functionIsFasterThanEquivalentGaussian2DFunction() {
    int flags = this.flags & ~GaussianFunctionFactory.FIT_ERF;
    final Gaussian2DFunction gf = GaussianFunctionFactory.create2D(1, maxx, maxy, flags, zModel);
    boolean zDepth = (flags & GaussianFunctionFactory.FIT_Z) != 0;
    final TurboList<double[]> params = new TurboList<double[]>();
    final TurboList<double[]> params2 = new TurboList<double[]>();
    for (double background : testbackground) // Peak 1
    for (double amplitude1 : testamplitude1) for (double shape1 : testshape1) for (double cx1 : testcx1) for (double cy1 : testcy1) for (double[] w1 : testw1) {
        double[] a = createParameters(background, amplitude1, shape1, cx1, cy1, w1[0], w1[1]);
        params.add(a);
        if (zDepth) {
            // Change to a standard free circular function
            a = a.clone();
            a[Gaussian2DFunction.X_SD] *= zModel.getSx(a[Gaussian2DFunction.SHAPE]);
            a[Gaussian2DFunction.Y_SD] *= zModel.getSy(a[Gaussian2DFunction.SHAPE]);
            a[Gaussian2DFunction.SHAPE] = 0;
            params2.add(a);
        }
    }
    double[][] x = params.toArray(new double[params.size()][]);
    double[][] x2 = (zDepth) ? params2.toArray(new double[params2.size()][]) : x;
    int runs = 10000 / x.length;
    TimingService ts = new TimingService(runs);
    ts.execute(new FunctionTimingTask(gf, x2, 1));
    ts.execute(new FunctionTimingTask(gf, x2, 0));
    ts.execute(new FunctionTimingTask(f1, x, 2));
    ts.execute(new FunctionTimingTask(f1, x, 1));
    ts.execute(new FunctionTimingTask(f1, x, 0));
    int size = ts.getSize();
    ts.repeat(size);
    ts.report();
    // Sometimes this fails, probably due to JVM optimisations, so skip for now
    if (!TestSettings.ASSERT_SPEED_TESTS)
        return;
    int n = ts.getSize() - 1;
    Assert.assertTrue("0 order", ts.get(n).getMean() < ts.get(n - 3).getMean());
    n--;
    Assert.assertTrue("1 order", ts.get(n).getMean() < ts.get(n - 3).getMean());
}
Also used : TurboList(gdsc.core.utils.TurboList) Gaussian2DFunction(gdsc.smlm.function.gaussian.Gaussian2DFunction) TimingService(gdsc.core.test.TimingService) Gaussian2DFunctionTest(gdsc.smlm.function.gaussian.Gaussian2DFunctionTest) Test(org.junit.Test)

Aggregations

TurboList (gdsc.core.utils.TurboList)11 TimingService (gdsc.core.test.TimingService)4 Gaussian2DFunction (gdsc.smlm.function.gaussian.Gaussian2DFunction)3 ImagePlus (ij.ImagePlus)3 ExecutorService (java.util.concurrent.ExecutorService)3 Future (java.util.concurrent.Future)3 Statistics (gdsc.core.utils.Statistics)2 GradientCalculator (gdsc.smlm.fitting.nonlinear.gradient.GradientCalculator)2 ValueProcedure (gdsc.smlm.function.ValueProcedure)2 Gaussian2DFunctionTest (gdsc.smlm.function.gaussian.Gaussian2DFunctionTest)2 MemoryPeakResults (gdsc.smlm.results.MemoryPeakResults)2 PeakResult (gdsc.smlm.results.PeakResult)2 ImageStack (ij.ImageStack)2 ExtendedGenericDialog (ij.gui.ExtendedGenericDialog)2 WindowOrganiser (ij.plugin.WindowOrganiser)2 Rectangle (java.awt.Rectangle)2 File (java.io.File)2 JLabel (javax.swing.JLabel)2 Well19937c (org.apache.commons.math3.random.Well19937c)2 ArrayMoment (gdsc.core.math.ArrayMoment)1