use of ij.gui.PlotWindow in project GDSC-SMLM by aherbert.
the class BenchmarkFilterAnalysis method scoreAnalysis.
/**
* Score analysis.
*
* @param allAssignments
* The assignments generated from running the filter (or null)
* @param filter
* the filter
* @return the assignments
*/
private ArrayList<FractionalAssignment[]> scoreAnalysis(ArrayList<FractionalAssignment[]> allAssignments, DirectFilter filter) {
if (!scoreAnalysis)
return null;
// Build a histogram of the fitted spots that were available to be scored
double[] signal = signalFactorStats.getValues();
double[] distance = distanceStats.getValues();
double[] limits1;
if (BenchmarkSpotFit.signalFactor > 0 && upperSignalFactor > 0) {
double range = BenchmarkSpotFit.signalFactor * upperSignalFactor / 100.0;
limits1 = new double[] { -range, range };
} else {
limits1 = Maths.limits(signal);
// Prevent the auto-range being too big
final double bound = 3;
if (limits1[0] < -bound)
limits1[0] = -bound;
if (limits1[1] > bound)
limits1[1] = bound;
}
double[] limits2;
if (BenchmarkSpotFit.distanceInPixels > 0 && upperMatchDistance > 0) {
double range = simulationParameters.a * BenchmarkSpotFit.distanceInPixels * upperMatchDistance / 100.0;
limits2 = new double[] { 0, range };
} else {
limits2 = Maths.limits(distance);
}
//final int bins = Math.max(10, nActual / 100);
//final int bins = Utils.getBinsSturges(signal.length);
final int bins = Utils.getBinsSqrt(signal.length);
double[][] h1 = Utils.calcHistogram(signal, limits1[0], limits1[1], bins);
double[][] h2 = Utils.calcHistogram(distance, limits2[0], limits2[1], bins);
// Run the filter manually to get the results that pass.
if (allAssignments == null)
allAssignments = getAssignments(filter);
double[] signal2 = new double[results.size()];
double[] distance2 = new double[results.size()];
int count = 0;
double sumSignal = 0, sumDistance = 0;
for (FractionalAssignment[] assignments : allAssignments) {
if (assignments == null)
continue;
for (int i = 0; i < assignments.length; i++) {
final CustomFractionalAssignment c = (CustomFractionalAssignment) assignments[i];
sumDistance += distance2[count] = c.d;
sumSignal += signal2[count] = c.getSignalFactor();
count++;
}
}
signal2 = Arrays.copyOf(signal2, count);
distance2 = Arrays.copyOf(distance2, count);
// Build a histogram using the same limits
double[][] h1b = Utils.calcHistogram(signal2, limits1[0], limits1[1], bins);
double[][] h2b = Utils.calcHistogram(distance2, limits2[0], limits2[1], bins);
// Since the distance and signal factor are computed for all fits (single, multi, doublet)
// there will be far more of them so we normalise and just plot the histogram profile.
double s1 = 0, s2 = 0, s1b = 0, s2b = 0;
for (int i = 0; i < h1b[0].length; i++) {
s1 += h1[1][i];
s2 += h2[1][i];
s1b += h1b[1][i];
s2b += h2b[1][i];
}
for (int i = 0; i < h1b[0].length; i++) {
h1[1][i] /= s1;
h2[1][i] /= s2;
h1b[1][i] /= s1b;
h2b[1][i] /= s2b;
}
// Draw distance histogram first
String title2 = TITLE + " Distance Histogram";
Plot2 plot2 = new Plot2(title2, "Distance (nm)", "Frequency");
plot2.setLimits(limits2[0], limits2[1], 0, Maths.maxDefault(Maths.max(h2[1]), h2b[1]));
plot2.setColor(Color.black);
plot2.addLabel(0, 0, String.format("Blue = Fitted (%s); Red = Filtered (%s)", Utils.rounded(distanceStats.getMean()), Utils.rounded(sumDistance / count)));
plot2.setColor(Color.blue);
plot2.addPoints(h2[0], h2[1], Plot2.BAR);
plot2.setColor(Color.red);
plot2.addPoints(h2b[0], h2b[1], Plot2.BAR);
PlotWindow pw2 = Utils.display(title2, plot2);
if (Utils.isNewWindow())
wo.add(pw2);
// Draw signal factor histogram
String title1 = TITLE + " Signal Factor Histogram";
Plot2 plot1 = new Plot2(title1, "Signal Factor", "Frequency");
plot1.setLimits(limits1[0], limits1[1], 0, Maths.maxDefault(Maths.max(h1[1]), h1b[1]));
plot1.setColor(Color.black);
plot1.addLabel(0, 0, String.format("Blue = Fitted (%s); Red = Filtered (%s)", Utils.rounded(signalFactorStats.getMean()), Utils.rounded(sumSignal / count)));
plot1.setColor(Color.blue);
plot1.addPoints(h1[0], h1[1], Plot2.BAR);
plot1.setColor(Color.red);
plot1.addPoints(h1b[0], h1b[1], Plot2.BAR);
PlotWindow pw1 = Utils.display(title1, plot1);
if (Utils.isNewWindow())
wo.add(pw1);
return allAssignments;
}
use of ij.gui.PlotWindow in project GDSC-SMLM by aherbert.
the class BackgroundEstimator method plot.
private void plot(WindowOrganiser wo, double[] xValues, double[] data1, double[] data2, double[] data3, String title, String title1, String title2, String title3) {
// Get limits
double[] a = Maths.limits(xValues);
double[] b = Maths.limits(data1);
b = Maths.limits(b, data2);
if (data3 != null)
b = Maths.limits(b, data3);
title = imp.getTitle() + " " + title;
Plot2 plot = new Plot2(title, "Slice", title);
double range = b[1] - b[0];
if (range == 0)
range = 1;
plot.setLimits(a[0], a[1], b[0] - 0.05 * range, b[1] + 0.05 * range);
plot.setColor(Color.blue);
plot.addPoints(xValues, data1, Plot2.LINE);
plot.draw();
Statistics stats = new Statistics(data1);
String label = String.format("%s (Blue) = %s +/- %s", title1, Utils.rounded(stats.getMean()), Utils.rounded(stats.getStandardDeviation()));
plot.setColor(Color.red);
plot.addPoints(xValues, data2, Plot2.LINE);
stats = new Statistics(data2);
label += String.format(", %s (Red) = %s +/- %s", title2, Utils.rounded(stats.getMean()), Utils.rounded(stats.getStandardDeviation()));
if (data3 != null) {
plot.setColor(Color.green);
plot.addPoints(xValues, data3, Plot2.LINE);
stats = new Statistics(data3);
label += String.format(", %s (Green) = %s +/- %s", title3, Utils.rounded(stats.getMean()), Utils.rounded(stats.getStandardDeviation()));
}
plot.setColor(Color.black);
plot.addLabel(0, 0, label);
PlotWindow pw = Utils.display(title, plot);
if (Utils.isNewWindow())
wo.add(pw);
}
use of ij.gui.PlotWindow in project GDSC-SMLM by aherbert.
the class BenchmarkSpotFilter method showFailuresPlot.
private void showFailuresPlot(BenchmarkFilterResult filterResult) {
double[][] h = filterResult.cumul;
StoredData data = filterResult.stats;
String xTitle = "Failures";
final int id = Utils.showHistogram(TITLE, data, xTitle, 1, 0, 0);
if (Utils.isNewWindow())
windowOrganiser.add(id);
String title = TITLE + " " + xTitle + " Cumulative";
Plot2 plot = new Plot2(title, xTitle, "Frequency");
double xMin = (data.size() == 0) ? 1 : h[0][0];
double xMax = (data.size() == 0) ? 1 : h[0][h[0].length - 1] + 1;
double xPadding = 0.05 * (xMax - xMin);
plot.setLimits(xMin - xPadding, xMax, 0, 1.05);
plot.setColor(Color.blue);
plot.addPoints(h[0], h[1], Plot2.BAR);
PlotWindow pw = Utils.display(title, plot);
if (Utils.isNewWindow())
windowOrganiser.add(pw);
}
use of ij.gui.PlotWindow in project GDSC-SMLM by aherbert.
the class BenchmarkSpotFilter method showPlot.
private void showPlot(BenchmarkFilterResult filterResult) {
double[] r = filterResult.r;
double[] p = filterResult.p;
double[] j = filterResult.j;
double[] c = filterResult.c;
int fractionIndex = filterResult.fractionIndex;
int maxIndex = filterResult.maxIndex;
double auc = filterResult.auc;
double auc2 = filterResult.auc2;
double slope = filterResult.slope;
double[] i1 = filterResult.i1;
double[] i2 = filterResult.i2;
double[] intensity = filterResult.intensity;
double[] rank = new double[intensity.length];
final double topIntensity = (intensity.length == 1) ? 0 : intensity[1];
for (int i = 1; i < rank.length; i++) {
if (rankByIntensity)
rank[i] = topIntensity - intensity[i];
else
rank[i] = i;
}
String title = TITLE + " Performance";
Plot2 plot = new Plot2(title, (rankByIntensity) ? "Relative Intensity" : "Spot Rank", "");
double[] limits = Maths.limits(rank);
plot.setLimits(limits[0], limits[1], 0, 1.05);
plot.setColor(Color.blue);
plot.addPoints(rank, p, Plot2.LINE);
//plot.addPoints(rank, maxp, Plot2.DOT);
plot.setColor(Color.red);
plot.addPoints(rank, r, Plot2.LINE);
plot.setColor(Color.black);
plot.addPoints(rank, j, Plot2.LINE);
// Plot correlation - update the scale to be 0-1?
plot.setColor(Color.yellow);
plot.addPoints(rank, c, Plot2.LINE);
plot.setColor(Color.magenta);
plot.drawLine(rank[fractionIndex], 0, rank[fractionIndex], Maths.max(p[fractionIndex], r[fractionIndex], j[fractionIndex], c[fractionIndex]));
plot.setColor(Color.pink);
plot.drawLine(rank[maxIndex], 0, rank[maxIndex], Maths.max(p[maxIndex], r[maxIndex], j[maxIndex], c[maxIndex]));
plot.setColor(Color.black);
plot.addLabel(0, 0, "Precision=Blue, Recall=Red, Jaccard=Black, Correlation=Yellow");
PlotWindow pw = Utils.display(title, plot);
if (Utils.isNewWindow())
windowOrganiser.add(pw);
title = TITLE + " Precision-Recall";
plot = new Plot2(title, "Recall", "Precision");
plot.setLimits(0, 1, 0, 1.05);
plot.setColor(Color.red);
plot.addPoints(r, p, Plot2.LINE);
//plot.setColor(Color.magenta);
//plot.addPoints(r, maxp, Plot2.LINE);
plot.drawLine(r[r.length - 1], p[r.length - 1], r[r.length - 1], 0);
plot.setColor(Color.black);
plot.addLabel(0, 0, "AUC = " + Utils.rounded(auc) + ", AUC2 = " + Utils.rounded(auc2));
PlotWindow pw2 = Utils.display(title, plot);
if (Utils.isNewWindow())
windowOrganiser.add(pw2);
title = TITLE + " Intensity";
plot = new Plot2(title, "Candidate", "Spot");
double[] limits1 = Maths.limits(i1);
double[] limits2 = Maths.limits(i2);
plot.setLimits(limits1[0], limits1[1], limits2[0], limits2[1]);
plot.addLabel(0, 0, String.format("Correlation=%s; Slope=%s", Utils.rounded(c[c.length - 1]), Utils.rounded(slope)));
plot.setColor(Color.red);
plot.addPoints(i1, i2, Plot.DOT);
if (slope > 1)
plot.drawLine(limits1[0], limits1[0] * slope, limits1[1], limits1[1] * slope);
else
plot.drawLine(limits2[0] / slope, limits2[0], limits2[1] / slope, limits2[1]);
PlotWindow pw3 = Utils.display(title, plot);
if (Utils.isNewWindow())
windowOrganiser.add(pw3);
}
use of ij.gui.PlotWindow 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 };
}
Aggregations