use of ij.gui.Plot2 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.Plot2 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.Plot2 in project GDSC-SMLM by aherbert.
the class PulseActivationAnalysis method runCrosstalkAnalysis.
private void runCrosstalkAnalysis() {
if (!showCrossTalkAnalysisDialog())
return;
double[] crosstalk = computeCrosstalk(count, targetChannel - 1);
// Store the cross talk.
// Crosstalk from M into N is defined as the number of times the molecule that should be
// activated by a pulse from channel M is activated by a pulse from channel N.
// targetChannel = M
// activationChannel = N
int index1, index2 = -1;
if (channels == 2) {
if (targetChannel == 1)
index1 = setCrosstalk(C12, crosstalk[1]);
else
index1 = setCrosstalk(C21, crosstalk[0]);
} else {
// 3-channel
if (targetChannel == 1) {
index1 = setCrosstalk(C12, crosstalk[1]);
index2 = setCrosstalk(C13, crosstalk[2]);
} else if (targetChannel == 2) {
index1 = setCrosstalk(C21, crosstalk[0]);
index2 = setCrosstalk(C23, crosstalk[2]);
} else {
index1 = setCrosstalk(C31, crosstalk[0]);
index2 = setCrosstalk(C32, crosstalk[1]);
}
}
// Show fraction activations histogram. So we have to set the sum to 1
double sum = Maths.sum(crosstalk);
for (int i = 0; i < crosstalk.length; i++) crosstalk[i] /= sum;
// Plot a histogram
double[] x = Utils.newArray(channels, 0.5, 1);
double[] y = crosstalk;
Plot2 plot = new Plot2(TITLE, "Channel", "Fraction activations");
plot.setLimits(0, channels + 1, 0, 1);
plot.setXMinorTicks(false);
plot.addPoints(x, y, Plot2.BAR);
String label = String.format("Crosstalk %s = %s", ctNames[index1], Maths.round(ct[index1]));
if (index2 > -1)
label += String.format(", %s = %s", ctNames[index2], Maths.round(ct[index2]));
plot.addLabel(0, 0, label);
Utils.display(TITLE, plot);
}
use of ij.gui.Plot2 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.Plot2 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);
}
Aggregations