use of uk.ac.sussex.gdsc.core.ij.HistogramPlot in project GDSC-SMLM by aherbert.
the class BenchmarkSpotFit method showDoubleHistogram.
private double[] showDoubleHistogram(StoredDataStatistics[][] stats, final int index, WindowOrganiser wo, double[][] matchScores) {
final String xLabel = filterCriteria[index].name;
LowerLimit lower = filterCriteria[index].lower;
UpperLimit upper = filterCriteria[index].upper;
double[] jaccard = null;
double[] metric = null;
double maxJaccard = 0;
if (index <= FILTER_PRECISION && (settings.showFilterScoreHistograms || upper.requiresJaccard || lower.requiresJaccard)) {
// Jaccard score verses the range of the metric
for (final double[] d : matchScores) {
if (!Double.isFinite(d[index])) {
System.out.printf("Error in fit data [%d]: %s%n", index, d[index]);
}
}
// Do not use Double.compare(double, double) so we get exceptions in the sort for inf/nan
Arrays.sort(matchScores, (o1, o2) -> {
if (o1[index] < o2[index]) {
return -1;
}
if (o1[index] > o2[index]) {
return 1;
}
return 0;
});
final int scoreIndex = FILTER_PRECISION + 1;
final int n = results.size();
double tp = 0;
double fp = 0;
jaccard = new double[matchScores.length + 1];
metric = new double[jaccard.length];
for (int k = 0; k < matchScores.length; k++) {
final double score = matchScores[k][scoreIndex];
tp += score;
fp += (1 - score);
jaccard[k + 1] = tp / (fp + n);
metric[k + 1] = matchScores[k][index];
}
metric[0] = metric[1];
maxJaccard = MathUtils.max(jaccard);
if (settings.showFilterScoreHistograms) {
final String title = TITLE + " Jaccard " + xLabel;
final Plot plot = new Plot(title, xLabel, "Jaccard");
plot.addPoints(metric, jaccard, Plot.LINE);
// Remove outliers
final double[] limitsx = MathUtils.limits(metric);
final Percentile p = new Percentile();
final double l = p.evaluate(metric, 25);
final double u = p.evaluate(metric, 75);
final double iqr = 1.5 * (u - l);
limitsx[1] = Math.min(limitsx[1], u + iqr);
plot.setLimits(limitsx[0], limitsx[1], 0, MathUtils.max(jaccard));
ImageJUtils.display(title, plot, wo);
}
}
// [0] is all
// [1] is matches
// [2] is no match
final StoredDataStatistics s1 = stats[0][index];
final StoredDataStatistics s2 = stats[1][index];
final StoredDataStatistics s3 = stats[2][index];
if (s1.getN() == 0) {
return new double[4];
}
final DescriptiveStatistics d = s1.getStatistics();
double median = 0;
Plot plot = null;
String title = null;
if (settings.showFilterScoreHistograms) {
median = d.getPercentile(50);
final String label = String.format("n = %d. Median = %s nm", s1.getN(), MathUtils.rounded(median));
final HistogramPlot histogramPlot = new HistogramPlotBuilder(TITLE, s1, xLabel).setMinBinWidth(filterCriteria[index].minBinWidth).setRemoveOutliersOption((filterCriteria[index].restrictRange) ? 1 : 0).setPlotLabel(label).build();
final PlotWindow plotWindow = histogramPlot.show(wo);
if (plotWindow == null) {
IJ.log("Failed to show the histogram: " + xLabel);
return new double[4];
}
title = plotWindow.getTitle();
// Reverse engineer the histogram settings
plot = histogramPlot.getPlot();
final double[] xvalues = histogramPlot.getPlotXValues();
final int bins = xvalues.length;
final double yMin = xvalues[0];
final double binSize = xvalues[1] - xvalues[0];
final double yMax = xvalues[0] + (bins - 1) * binSize;
if (s2.getN() > 0) {
final double[] values = s2.getValues();
final double[][] hist = HistogramPlot.calcHistogram(values, yMin, yMax, bins);
if (hist[0].length > 0) {
plot.setColor(Color.red);
plot.addPoints(hist[0], hist[1], Plot.BAR);
ImageJUtils.display(title, plot);
}
}
if (s3.getN() > 0) {
final double[] values = s3.getValues();
final double[][] hist = HistogramPlot.calcHistogram(values, yMin, yMax, bins);
if (hist[0].length > 0) {
plot.setColor(Color.blue);
plot.addPoints(hist[0], hist[1], Plot.BAR);
ImageJUtils.display(title, plot);
}
}
}
// Do cumulative histogram
final double[][] h1 = MathUtils.cumulativeHistogram(s1.getValues(), true);
final double[][] h2 = MathUtils.cumulativeHistogram(s2.getValues(), true);
final double[][] h3 = MathUtils.cumulativeHistogram(s3.getValues(), true);
if (settings.showFilterScoreHistograms) {
title = TITLE + " Cumul " + xLabel;
plot = new Plot(title, xLabel, "Frequency");
// Find limits
double[] xlimit = MathUtils.limits(h1[0]);
xlimit = MathUtils.limits(xlimit, h2[0]);
xlimit = MathUtils.limits(xlimit, h3[0]);
// Restrict using the inter-quartile range
if (filterCriteria[index].restrictRange) {
final double q1 = d.getPercentile(25);
final double q2 = d.getPercentile(75);
final double iqr = (q2 - q1) * 2.5;
xlimit[0] = MathUtils.max(xlimit[0], median - iqr);
xlimit[1] = MathUtils.min(xlimit[1], median + iqr);
}
plot.setLimits(xlimit[0], xlimit[1], 0, 1.05);
plot.addPoints(h1[0], h1[1], Plot.LINE);
plot.setColor(Color.red);
plot.addPoints(h2[0], h2[1], Plot.LINE);
plot.setColor(Color.blue);
plot.addPoints(h3[0], h3[1], Plot.LINE);
}
// Determine the maximum difference between the TP and FP
double maxx1 = 0;
double maxx2 = 0;
double max1 = 0;
double max2 = 0;
// We cannot compute the delta histogram, or use percentiles
if (s2.getN() == 0) {
upper = UpperLimit.ZERO;
lower = LowerLimit.ZERO;
}
final boolean requireLabel = (settings.showFilterScoreHistograms && filterCriteria[index].requireLabel);
if (requireLabel || upper.requiresDeltaHistogram() || lower.requiresDeltaHistogram()) {
if (s2.getN() != 0 && s3.getN() != 0) {
final LinearInterpolator li = new LinearInterpolator();
final PolynomialSplineFunction f1 = li.interpolate(h2[0], h2[1]);
final PolynomialSplineFunction f2 = li.interpolate(h3[0], h3[1]);
for (final double x : h1[0]) {
if (x < h2[0][0] || x < h3[0][0]) {
continue;
}
try {
final double v1 = f1.value(x);
final double v2 = f2.value(x);
final double diff = v2 - v1;
if (diff > 0) {
if (max1 < diff) {
max1 = diff;
maxx1 = x;
}
} else if (max2 > diff) {
max2 = diff;
maxx2 = x;
}
} catch (final OutOfRangeException ex) {
// Because we reached the end
break;
}
}
}
}
if (plot != null) {
// We use bins=1 on charts where we do not need a label
if (requireLabel) {
final String label = String.format("Max+ %s @ %s, Max- %s @ %s", MathUtils.rounded(max1), MathUtils.rounded(maxx1), MathUtils.rounded(max2), MathUtils.rounded(maxx2));
plot.setColor(Color.black);
plot.addLabel(0, 0, label);
}
ImageJUtils.display(title, plot, wo);
}
// Now compute the bounds using the desired limit
double lowerBound;
double upperBound;
switch(lower) {
case MAX_NEGATIVE_CUMUL_DELTA:
// Switch to percentiles if we have no delta histogram
if (maxx2 < 0) {
lowerBound = maxx2;
break;
}
// fall-through
case ONE_PERCENT:
lowerBound = getPercentile(h2, 0.01);
break;
case MIN:
lowerBound = getPercentile(h2, 0.0);
break;
case ZERO:
lowerBound = 0;
break;
case HALF_MAX_JACCARD_VALUE:
lowerBound = getXValue(metric, jaccard, maxJaccard * 0.5);
break;
default:
throw new IllegalStateException("Missing lower limit method");
}
switch(upper) {
case MAX_POSITIVE_CUMUL_DELTA:
// Switch to percentiles if we have no delta histogram
if (maxx1 > 0) {
upperBound = maxx1;
break;
}
// fall-through
case NINETY_NINE_PERCENT:
upperBound = getPercentile(h2, 0.99);
break;
case NINETY_NINE_NINE_PERCENT:
upperBound = getPercentile(h2, 0.999);
break;
case ZERO:
upperBound = 0;
break;
case MAX_JACCARD2:
upperBound = getXValue(metric, jaccard, maxJaccard) * 2;
// System.out.printf("MaxJ = %.4f @ %.3f\n", maxJ, u / 2);
break;
default:
throw new IllegalStateException("Missing upper limit method");
}
final double min = getPercentile(h1, 0);
final double max = getPercentile(h1, 1);
return new double[] { lowerBound, upperBound, min, max };
}
use of uk.ac.sussex.gdsc.core.ij.HistogramPlot in project GDSC-SMLM by aherbert.
the class TraceLengthAnalysis method run.
@Override
public void run(String arg) {
SmlmUsageTracker.recordPlugin(this.getClass(), arg);
if (MemoryPeakResults.isMemoryEmpty()) {
IJ.error(TITLE, "No localisations in memory");
return;
}
if (!showDialog()) {
return;
}
// Load the results
MemoryPeakResults results = ResultsManager.loadInputResults(settings.inputOption, false, null, null);
if (MemoryPeakResults.isEmpty(results)) {
IJ.error(TITLE, "No results could be loaded");
return;
}
try {
distanceConverter = results.getDistanceConverter(DistanceUnit.UM);
timeConverter = results.getTimeConverter(TimeUnit.SECOND);
} catch (final Exception ex) {
IJ.error(TITLE, "Cannot convert units to um or seconds: " + ex.getMessage());
return;
}
// Get the localisation error (4s^2) in raw units^2
double precision = 0;
try {
final PrecisionResultProcedure p = new PrecisionResultProcedure(results);
p.getPrecision();
// Precision in nm using the median
precision = new Percentile().evaluate(p.precisions, 50);
// Convert from nm to um to raw units
final double rawPrecision = distanceConverter.convertBack(precision / 1e3);
// Get the localisation error (4s^2) in units^2
error = 4 * rawPrecision * rawPrecision;
} catch (final Exception ex) {
ImageJUtils.log(TITLE + " - Unable to compute precision: " + ex.getMessage());
}
// Analyse the track lengths
results = results.copy();
results.sort(IdFramePeakResultComparator.INSTANCE);
// Ensure the first result triggers an id change
lastid = results.getFirst().getId() - 1;
results.forEach(this::processTrackLength);
// For the final track
store();
msds = msdList.toArray();
lengths = lengthList.toArray();
ids = idList.toArray();
final int[] limits = MathUtils.limits(lengths);
h1 = new int[limits[1] + 1];
h2 = new int[h1.length];
x1 = SimpleArrayUtils.newArray(h1.length, 0, 1f);
y1 = new float[x1.length];
y2 = new float[x1.length];
// Sort by MSD
final int[] indices = SimpleArrayUtils.natural(msds.length);
SortUtils.sortIndices(indices, msds, false);
final double[] msds2 = msds.clone();
final int[] lengths2 = lengths.clone();
final int[] ids2 = ids.clone();
for (int i = 0; i < indices.length; i++) {
msds[i] = msds2[indices[i]];
lengths[i] = lengths2[indices[i]];
ids[i] = ids2[indices[i]];
}
// Interactive analysis
final NonBlockingExtendedGenericDialog gd = new NonBlockingExtendedGenericDialog(TITLE);
ImageJUtils.addMessage(gd, "Split traces into fixed or moving using the track diffusion coefficient (D).\n" + "Localisation error has been subtracted from jumps (%s nm).", MathUtils.rounded(precision));
final Statistics s = Statistics.create(msds);
final double av = s.getMean();
final String msg = String.format("Average D per track = %s um^2/s", MathUtils.rounded(av));
gd.addMessage(msg);
// Histogram the diffusion coefficients
final WindowOrganiser wo = new WindowOrganiser();
final HistogramPlot histogramPlot = new HistogramPlotBuilder("Trace diffusion coefficient", StoredData.create(msds), "D (um^2/s)").setRemoveOutliersOption(1).setPlotLabel(msg).build();
histogramPlot.show(wo);
final double[] xvalues = histogramPlot.getPlotXValues();
final double min = xvalues[0];
final double max = xvalues[xvalues.length - 1];
// see if we can build a nice slider range from the histogram limits
if (max - min < 5) {
// Because sliders are used when the range is <5 and floating point
gd.addSlider("D_threshold", min, max, settings.msdThreshold);
} else {
gd.addNumericField("D_threshold", settings.msdThreshold, 2, 6, "um^2/s");
}
gd.addCheckbox("Normalise", settings.normalise);
gd.addDialogListener((gd1, event) -> {
settings.msdThreshold = gd1.getNextNumber();
settings.normalise = gd1.getNextBoolean();
update();
return true;
});
if (ImageJUtils.isShowGenericDialog()) {
draw(wo);
wo.tile();
}
gd.setOKLabel("Save datasets");
gd.setCancelLabel("Close");
gd.addHelp(HelpUrls.getUrl("trace-length-analysis"));
gd.showDialog();
if (gd.wasCanceled()) {
return;
}
// Sort by ID
final PeakResult[] list = results.toArray();
Arrays.sort(list, IdFramePeakResultComparator.INSTANCE);
createResults(results, "Fixed", 0, lastIndex, list);
createResults(results, "Moving", lastIndex, msds.length, list);
}
use of uk.ac.sussex.gdsc.core.ij.HistogramPlot in project GDSC-SMLM by aherbert.
the class CmosAnalysis method showHistogram.
private static void showHistogram(String name, double[] values, int bins, Statistics stats, WindowOrganiser wo) {
final DoubleData data = DoubleData.wrap(values);
final double minWidth = 0;
final int removeOutliers = 0;
final int shape = Plot.CIRCLE;
final String label = String.format("Mean = %s +/- %s", MathUtils.rounded(stats.getMean()), MathUtils.rounded(stats.getStandardDeviation()));
final HistogramPlot histogramPlot = new HistogramPlotBuilder(TITLE, data, name).setMinBinWidth(minWidth).setRemoveOutliersOption(removeOutliers).setNumberOfBins(bins).setPlotShape(shape).setPlotLabel(label).build();
histogramPlot.show(wo);
// Redraw using a log scale. This requires a non-zero y-min
final Plot plot = histogramPlot.getPlot();
final double[] limits = plot.getLimits();
plot.setLimits(limits[0], limits[1], 1, limits[3]);
plot.setAxisYLog(true);
plot.updateImage();
}
use of uk.ac.sussex.gdsc.core.ij.HistogramPlot in project GDSC-SMLM by aherbert.
the class DiffusionRateTest method plotJumpDistances.
/**
* Plot a cumulative histogram and standard histogram of the jump distances.
*
* @param title the title
* @param jumpDistances the jump distances
* @param dimensions the number of dimensions for the jumps
*/
private void plotJumpDistances(String title, DoubleData jumpDistances, int dimensions) {
// Cumulative histogram
// --------------------
final double[] values = jumpDistances.values();
String title2 = title + " Cumulative Jump Distance " + dimensions + "D";
final double[][] jdHistogram = JumpDistanceAnalysis.cumulativeHistogram(values);
Plot jdPlot = new Plot(title2, "Distance (um^2)", "Cumulative Probability");
jdPlot.addPoints(jdHistogram[0], jdHistogram[1], Plot.LINE);
ImageJUtils.display(title2, jdPlot, windowOrganiser);
// Plot the expected function
// This is the Chi-squared distribution: The sum of the squares of k independent
// standard normal random variables with k = dimensions. It is a special case of
// the gamma distribution. If the normals have non-unit variance the distribution
// is scaled.
// Chi ~ Gamma(k/2, 2) // using the scale parameterisation of the gamma
// s^2 * Chi ~ Gamma(k/2, 2*s^2)
// So if s^2 = 2D:
// 2D * Chi ~ Gamma(k/2, 4D)
final double estimatedD = pluginSettings.simpleD * pluginSettings.simpleSteps;
final double max = MathUtils.max(values);
final double[] x = SimpleArrayUtils.newArray(1000, 0, max / 1000);
final double k = dimensions / 2.0;
final double mean = 4 * estimatedD;
final GammaDistribution dist = new GammaDistribution(null, k, mean);
final double[] y = new double[x.length];
for (int i = 0; i < x.length; i++) {
y[i] = dist.cumulativeProbability(x[i]);
}
jdPlot.setColor(Color.red);
jdPlot.addPoints(x, y, Plot.LINE);
ImageJUtils.display(title2, jdPlot);
// Histogram
// ---------
title2 = title + " Jump " + dimensions + "D";
final HistogramPlot histogramPlot = new HistogramPlotBuilder(title2, jumpDistances, "Distance (um^2)").build();
// Assume the plot works
histogramPlot.show(windowOrganiser);
// Recompute the expected function
for (int i = 0; i < x.length; i++) {
y[i] = dist.density(x[i]);
}
// Scale to have the same area
final double[] xvalues = histogramPlot.getPlotXValues();
if (xvalues.length > 1) {
final double area1 = jumpDistances.size() * (xvalues[1] - xvalues[0]);
final double area2 = dist.cumulativeProbability(x[x.length - 1]);
final double scale = area1 / area2;
for (int i = 0; i < y.length; i++) {
y[i] *= scale;
}
}
jdPlot = histogramPlot.getPlot();
jdPlot.setColor(Color.red);
jdPlot.addPoints(x, y, Plot.LINE);
ImageJUtils.display(histogramPlot.getPlotTitle(), jdPlot);
}
Aggregations