Search in sources :

Example 11 with StoredDataStatistics

use of uk.ac.sussex.gdsc.core.utils.StoredDataStatistics in project GDSC-SMLM by aherbert.

the class TraceDiffusion method calculateTraceLengths.

/**
 * Calculate trace lengths.
 *
 * @param distances the distances for each trace
 * @return the trace lengths
 */
private static StoredDataStatistics calculateTraceLengths(ArrayList<double[]> distances) {
    final StoredDataStatistics lengths = new StoredDataStatistics();
    for (final double[] trace : distances) {
        double sum = 0;
        for (final double d : trace) {
            sum += Math.sqrt(d);
        }
        lengths.add(sum);
    }
    return lengths;
}
Also used : StoredDataStatistics(uk.ac.sussex.gdsc.core.utils.StoredDataStatistics)

Example 12 with StoredDataStatistics

use of uk.ac.sussex.gdsc.core.utils.StoredDataStatistics in project GDSC-SMLM by aherbert.

the class BoundedLvmSteppingFunctionSolverTest method fitSingleGaussianWithoutBias.

private void fitSingleGaussianWithoutBias(RandomSeed seed, boolean applyBounds, int clamping) {
    Assumptions.assumeTrue(runTests);
    final double bias = 100;
    final SteppingFunctionSolver solver = getSolver(clamping, false);
    final SteppingFunctionSolver solver2 = getSolver(clamping, false);
    final String name = getLvmName(applyBounds, clamping, false);
    final int loops = 5;
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    final StoredDataStatistics[] stats = new StoredDataStatistics[6];
    for (final double s : signal) {
        final double[] expected = createParams(1, s, 0, 0, 1);
        double[] lower = null;
        double[] upper = null;
        if (applyBounds) {
            lower = createParams(0, s * 0.5, -0.2, -0.2, 0.8);
            upper = createParams(3, s * 2, 0.2, 0.2, 1.2);
            solver.setBounds(lower, upper);
        }
        final double[] expected2 = addBiasToParams(expected, bias);
        if (applyBounds) {
            final double[] lower2 = addBiasToParams(lower, bias);
            final double[] upper2 = addBiasToParams(upper, bias);
            solver2.setBounds(lower2, upper2);
        }
        for (int loop = loops; loop-- > 0; ) {
            final double[] data = drawGaussian(expected, rg);
            final double[] data2 = data.clone();
            for (int i = 0; i < data.length; i++) {
                data2[i] += bias;
            }
            for (int i = 0; i < stats.length; i++) {
                stats[i] = new StoredDataStatistics();
            }
            for (final double db : base) {
                for (final double dx : shift) {
                    for (final double dy : shift) {
                        for (final double dsx : factor) {
                            final double[] p = createParams(db, s, dx, dy, dsx);
                            final double[] p2 = addBiasToParams(p, bias);
                            final double[] fp = fitGaussian(solver, data, p, expected);
                            final double[] fp2 = fitGaussian(solver2, data2, p2, expected2);
                            // The result should be the same without a bias
                            Assertions.assertEquals(solver.getEvaluations(), solver2.getEvaluations(), () -> name + " Iterations");
                            fp2[0] -= bias;
                            Assertions.assertArrayEquals(fp, fp2, 1e-6, () -> name + " Solution");
                        }
                    }
                }
            }
        }
    }
}
Also used : StoredDataStatistics(uk.ac.sussex.gdsc.core.utils.StoredDataStatistics) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider)

Example 13 with StoredDataStatistics

use of uk.ac.sussex.gdsc.core.utils.StoredDataStatistics in project GDSC-SMLM by aherbert.

the class DiffusionRateTest method simpleTest.

/**
 * Perform a simple diffusion test. This can be used to understand the distributions that are
 * generated during 3D diffusion.
 */
private void simpleTest() {
    if (!showSimpleDialog()) {
        return;
    }
    final StoredDataStatistics[] stats2 = new StoredDataStatistics[3];
    final StoredDataStatistics[] stats = new StoredDataStatistics[3];
    final NormalizedGaussianSampler[] gauss = new NormalizedGaussianSampler[3];
    for (int i = 0; i < 3; i++) {
        stats2[i] = new StoredDataStatistics(pluginSettings.simpleParticles);
        stats[i] = new StoredDataStatistics(pluginSettings.simpleParticles);
        gauss[i] = SamplerUtils.createNormalizedGaussianSampler(UniformRandomProviders.create());
    }
    final double scale = Math.sqrt(2 * pluginSettings.simpleD);
    final int report = Math.max(1, pluginSettings.simpleParticles / 200);
    for (int particle = 0; particle < pluginSettings.simpleParticles; particle++) {
        if (particle % report == 0) {
            IJ.showProgress(particle, pluginSettings.simpleParticles);
        }
        final double[] xyz = new double[3];
        if (pluginSettings.linearDiffusion) {
            final double[] dir = nextVector(gauss[0]);
            for (int step = 0; step < pluginSettings.simpleSteps; step++) {
                final double d = gauss[0].sample();
                for (int i = 0; i < 3; i++) {
                    xyz[i] += dir[i] * d;
                }
            }
        } else {
            for (int step = 0; step < pluginSettings.simpleSteps; step++) {
                for (int i = 0; i < 3; i++) {
                    xyz[i] += gauss[i].sample();
                }
            }
        }
        for (int i = 0; i < 3; i++) {
            xyz[i] *= scale;
        }
        double msd = 0;
        for (int i = 0; i < 3; i++) {
            msd += xyz[i] * xyz[i];
            stats2[i].add(msd);
            // Store the actual distances
            stats[i].add(xyz[i]);
        }
    }
    IJ.showProgress(1);
    for (int i = 0; i < 3; i++) {
        plotJumpDistances(TITLE, stats2[i], i + 1);
        // Save stats to file for fitting
        save(stats2[i], i + 1, "msd");
        save(stats[i], i + 1, "d");
    }
    windowOrganiser.tile();
}
Also used : StoredDataStatistics(uk.ac.sussex.gdsc.core.utils.StoredDataStatistics) NormalizedGaussianSampler(org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler)

Example 14 with StoredDataStatistics

use of uk.ac.sussex.gdsc.core.utils.StoredDataStatistics in project GDSC-SMLM by aherbert.

the class TraceMolecules method summarise.

private void summarise(Consumer<String> output, Trace[] traces, int filtered, double distanceThreshold, double timeThreshold) {
    IJ.showStatus("Calculating summary ...");
    final Statistics[] stats = new Statistics[NAMES.length];
    for (int i = 0; i < stats.length; i++) {
        stats[i] = (settings.getShowHistograms() || settings.getSaveTraceData()) ? new StoredDataStatistics() : new Statistics();
    }
    int singles = 0;
    for (final Trace trace : traces) {
        final int nBlinks = trace.getBlinks() - 1;
        stats[BLINKS].add(nBlinks);
        final int[] onTimes = trace.getOnTimes();
        final int[] offTimes = trace.getOffTimes();
        double timeOn = 0;
        for (final int t : onTimes) {
            stats[T_ON].add(t * exposureTime);
            timeOn += t * exposureTime;
        }
        stats[TOTAL_T_ON].add(timeOn);
        if (offTimes != null) {
            double timeOff = 0;
            for (final int t : offTimes) {
                stats[T_OFF].add(t * exposureTime);
                timeOff += t * exposureTime;
            }
            stats[TOTAL_T_OFF].add(timeOff);
        }
        final double signal = trace.getSignal() / results.getGain();
        stats[TOTAL_SIGNAL].add(signal);
        stats[SIGNAL_PER_FRAME].add(signal / trace.size());
        stats[DWELL_TIME].add((trace.getTail().getEndFrame() - trace.getHead().getFrame() + 1) * exposureTime);
        if (trace.size() == 1) {
            singles++;
        }
    }
    // Add to the summary table
    final StringBuilder sb = new StringBuilder();
    sb.append(results.getName()).append('\t');
    sb.append(outputName.equals("Cluster") ? getClusteringAlgorithm(settings.getClusteringAlgorithm()) : getTraceMode(settings.getTraceMode())).append('\t');
    sb.append(MathUtils.rounded(getExposureTimeInMilliSeconds(), 3)).append('\t');
    sb.append(MathUtils.rounded(distanceThreshold, 3)).append('\t');
    sb.append(MathUtils.rounded(timeThreshold, 3));
    if (settings.getSplitPulses()) {
        sb.append(" *");
    }
    sb.append('\t');
    sb.append(convertSecondsToFrames(timeThreshold)).append('\t');
    sb.append(traces.length).append('\t');
    sb.append(filtered).append('\t');
    sb.append(singles).append('\t');
    sb.append(traces.length - singles).append('\t');
    for (int i = 0; i < stats.length; i++) {
        sb.append(MathUtils.rounded(stats[i].getMean(), 3)).append('\t');
    }
    if (java.awt.GraphicsEnvironment.isHeadless()) {
        IJ.log(sb.toString());
        return;
    }
    output.accept(sb.toString());
    if (settings.getShowHistograms()) {
        IJ.showStatus("Calculating histograms ...");
        final WindowOrganiser windowOrganiser = new WindowOrganiser();
        final HistogramPlotBuilder builder = new HistogramPlotBuilder(pluginTitle).setNumberOfBins(settings.getHistogramBins());
        for (int i = 0; i < NAMES.length; i++) {
            if (pluginSettings.displayHistograms[i]) {
                builder.setData((StoredDataStatistics) stats[i]).setName(NAMES[i]).setIntegerBins(integerDisplay[i]).setRemoveOutliersOption((settings.getRemoveOutliers() || alwaysRemoveOutliers[i]) ? 2 : 0).show(windowOrganiser);
            }
        }
        windowOrganiser.tile();
    }
    if (settings.getSaveTraceData()) {
        saveTraceData(stats);
    }
    IJ.showStatus("");
}
Also used : Trace(uk.ac.sussex.gdsc.smlm.results.Trace) StoredDataStatistics(uk.ac.sussex.gdsc.core.utils.StoredDataStatistics) HistogramPlotBuilder(uk.ac.sussex.gdsc.core.ij.HistogramPlot.HistogramPlotBuilder) WindowOrganiser(uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser) StoredDataStatistics(uk.ac.sussex.gdsc.core.utils.StoredDataStatistics) Statistics(uk.ac.sussex.gdsc.core.utils.Statistics) SummaryStatistics(org.apache.commons.math3.stat.descriptive.SummaryStatistics) ClusterPoint(uk.ac.sussex.gdsc.core.clustering.ClusterPoint)

Example 15 with StoredDataStatistics

use of uk.ac.sussex.gdsc.core.utils.StoredDataStatistics in project GDSC-SMLM by aherbert.

the class TraceDiffusion method summarise.

private void summarise(Trace[] traces, double[] fitMsdResult, int n, double[][] jdParams) {
    IJ.showStatus("Calculating summary ...");
    final Statistics[] stats = new Statistics[Settings.NAMES.length];
    for (int i = 0; i < stats.length; i++) {
        stats[i] = (clusteringSettings.getShowHistograms()) ? new StoredDataStatistics() : new Statistics();
    }
    for (final Trace trace : traces) {
        stats[Settings.T_ON].add(trace.getOnTime() * exposureTime);
        final double signal = trace.getSignal() / results.getGain();
        stats[Settings.TOTAL_SIGNAL].add(signal);
        stats[Settings.SIGNAL_PER_FRAME].add(signal / trace.size());
    }
    // Add to the summary table
    final StringBuilder sb = new StringBuilder(settings.title);
    sb.append('\t').append(createCombinedName());
    sb.append('\t');
    sb.append(MathUtils.rounded(exposureTime * 1000, 3)).append('\t');
    appendClusteringSettings(sb).append('\t');
    sb.append(clusteringSettings.getMinimumTraceLength()).append('\t');
    sb.append(clusteringSettings.getIgnoreEnds()).append('\t');
    sb.append(clusteringSettings.getTruncate()).append('\t');
    sb.append(clusteringSettings.getInternalDistances()).append('\t');
    sb.append(clusteringSettings.getFitLength()).append('\t');
    sb.append(clusteringSettings.getMsdCorrection()).append('\t');
    sb.append(clusteringSettings.getPrecisionCorrection()).append('\t');
    sb.append(clusteringSettings.getMle()).append('\t');
    sb.append(traces.length).append('\t');
    sb.append(MathUtils.rounded(precision, 4)).append('\t');
    // D
    double diffCoeff = 0;
    double precision = 0;
    if (fitMsdResult != null) {
        diffCoeff = fitMsdResult[0];
        precision = fitMsdResult[1];
    }
    sb.append(MathUtils.rounded(diffCoeff, 4)).append('\t');
    sb.append(MathUtils.rounded(precision * 1000, 4)).append('\t');
    sb.append(MathUtils.rounded(clusteringSettings.getJumpDistance() * exposureTime)).append('\t');
    sb.append(n).append('\t');
    sb.append(MathUtils.rounded(beta, 4)).append('\t');
    if (jdParams == null) {
        sb.append("\t\t\t");
    } else {
        sb.append(format(jdParams[0])).append('\t');
        sb.append(format(jdParams[1])).append('\t');
        sb.append(MathUtils.rounded(fitValue)).append('\t');
    }
    for (int i = 0; i < stats.length; i++) {
        sb.append(MathUtils.rounded(stats[i].getMean(), 3)).append('\t');
    }
    createSummaryTable().accept(sb.toString());
    if (java.awt.GraphicsEnvironment.isHeadless()) {
        return;
    }
    if (clusteringSettings.getShowHistograms()) {
        IJ.showStatus("Calculating histograms ...");
        for (int i = 0; i < Settings.NAMES.length; i++) {
            if (settings.displayHistograms[i]) {
                showHistogram((StoredDataStatistics) stats[i], Settings.NAMES[i], settings.alwaysRemoveOutliers[i], Settings.ROUNDED[i], false);
            }
        }
    }
    windowOrganiser.tile();
    IJ.showStatus("Finished " + TITLE);
}
Also used : Trace(uk.ac.sussex.gdsc.smlm.results.Trace) StoredDataStatistics(uk.ac.sussex.gdsc.core.utils.StoredDataStatistics) StoredDataStatistics(uk.ac.sussex.gdsc.core.utils.StoredDataStatistics) Statistics(uk.ac.sussex.gdsc.core.utils.Statistics)

Aggregations

StoredDataStatistics (uk.ac.sussex.gdsc.core.utils.StoredDataStatistics)29 Statistics (uk.ac.sussex.gdsc.core.utils.Statistics)11 ArrayList (java.util.ArrayList)10 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)9 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)9 Plot (ij.gui.Plot)7 Rectangle (java.awt.Rectangle)6 ImagePlus (ij.ImagePlus)5 ImageStack (ij.ImageStack)5 DescriptiveStatistics (org.apache.commons.math3.stat.descriptive.DescriptiveStatistics)5 HistogramPlotBuilder (uk.ac.sussex.gdsc.core.ij.HistogramPlot.HistogramPlotBuilder)5 WindowOrganiser (uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser)5 GenericDialog (ij.gui.GenericDialog)4 PlotWindow (ij.gui.PlotWindow)4 LinkedList (java.util.LinkedList)4 TIntHashSet (gnu.trove.set.hash.TIntHashSet)3 IJ (ij.IJ)3 Prefs (ij.Prefs)3 PlugIn (ij.plugin.PlugIn)3 ConcurrentRuntimeException (org.apache.commons.lang3.concurrent.ConcurrentRuntimeException)3