use of ij.gui.PlotWindow in project GDSC-SMLM by aherbert.
the class PsfCreator method plotCumulativeSignal.
/**
* Show a plot of the cumulative signal vs distance from the centre.
*
* @param z The slice to plot
* @param normalise normalise the sum to 1
* @param resetScale Reset the y-axis maximum
* @param distanceThreshold The distance threshold for the cumulative total shown in the plot
* label
*/
private void plotCumulativeSignal(int z, boolean normalise, boolean resetScale, double distanceThreshold) {
final float[] data = (float[]) psf.getProcessor(z).getPixels();
final int size = psf.getWidth();
if (indexLookup == null || indexLookup.length != data.length) {
// Precompute square distances
final double[] d2 = new double[size];
for (int y = 0, y2 = -size / 2; y < size; y++, y2++) {
d2[y] = y2 * y2;
}
// Precompute distances
final double[] d = new double[data.length];
for (int y = 0, i = 0; y < size; y++) {
for (int x = 0; x < size; x++, i++) {
d[i] = Math.sqrt(d2[y] + d2[x]);
}
}
// Sort
final int[] indices = SimpleArrayUtils.natural(d.length);
SortUtils.sortData(indices, d, true, false);
// Store a unique cumulative index for each distance
double lastD = d[0];
int lastI = 0;
int counter = 0;
final StoredData distance = new StoredData();
indexLookup = new int[indices.length];
for (int i = 0; i < indices.length; i++) {
if (lastD != d[i]) {
distance.add(lastD * psfNmPerPixel);
for (int j = lastI; j < i; j++) {
indexLookup[indices[j]] = counter;
}
lastD = d[i];
lastI = i;
counter++;
}
}
// Do the final distance
distance.add(lastD * psfNmPerPixel);
for (int j = lastI; j < indices.length; j++) {
indexLookup[indices[j]] = counter;
}
counter++;
distances = distance.getValues();
}
// Get the signal at each distance
final double[] signal = new double[distances.length];
for (int i = 0; i < data.length; i++) {
if (data[i] > 0) {
signal[indexLookup[i]] += data[i];
}
}
// Get the cumulative signal
for (int i = 1; i < signal.length; i++) {
signal[i] += signal[i - 1];
}
// Get the total up to the distance threshold
double sum = 0;
for (int i = 0; i < signal.length; i++) {
if (distances[i] > distanceThreshold) {
break;
}
sum = signal[i];
}
if (normalise && distanceThreshold > 0) {
for (int i = 0; i < signal.length; i++) {
signal[i] /= sum;
}
}
if (resetScale) {
maxCumulativeSignal = 0;
}
maxCumulativeSignal = MathUtils.maxDefault(maxCumulativeSignal, signal);
final String title = "Cumulative Signal";
final boolean alignWindows = (WindowManager.getFrame(title) == null);
final Plot plot = new Plot(title, "Distance (nm)", "Signal");
plot.addPoints(distances, signal, Plot.LINE);
plot.setLimits(0, distances[distances.length - 1], 0, maxCumulativeSignal);
plot.addLabel(0, 0, String.format("Total = %s (@ %s nm). z = %s nm", MathUtils.rounded(sum), MathUtils.rounded(distanceThreshold), MathUtils.rounded((z - zCentre) * settings.getNmPerSlice())));
plot.setColor(Color.green);
plot.drawLine(distanceThreshold, 0, distanceThreshold, maxCumulativeSignal);
plot.setColor(Color.blue);
final PlotWindow plotWindow = ImageJUtils.display(title, plot);
if (alignWindows && plotWindow != null) {
final PlotWindow otherWindow = getPlot(TITLE_PSF_PARAMETERS);
if (otherWindow != null) {
// Put the two plots tiled together so both are visible
final Point l = plotWindow.getLocation();
l.x = otherWindow.getLocation().x + otherWindow.getWidth();
l.y = otherWindow.getLocation().y + otherWindow.getHeight();
plotWindow.setLocation(l);
}
}
// Update the PSF to the correct slice
if (psfImp != null) {
psfImp.setSlice(z);
}
}
use of ij.gui.PlotWindow in project GDSC-SMLM by aherbert.
the class PsfCreator method showPlots.
private void showPlots(final double[] z, final double[] a, final double[] smoothAz, final double[] smoothA, final double[] xCoord, final double[] yCoord, final double[] sd, final double[] newZ, final double[] smoothX, final double[] smoothY, double[] smoothSd, final int cz) {
PlotWindow amplitudeWindow = null;
// Draw a plot of the amplitude
if (a != null) {
final Plot plot = new Plot(TITLE_AMPLITUDE, "z", "Amplitude");
plot.addPoints(smoothAz, smoothA, Plot.LINE);
final double[] limits2 = MathUtils.limits(MathUtils.limits(a), smoothA);
plot.setLimits(z[0], z[z.length - 1], limits2[0], limits2[1]);
plot.addPoints(z, a, Plot.CIRCLE);
// Add a line for the z-centre
plot.setColor(Color.GREEN);
plot.addPoints(new double[] { cz, cz }, limits2, Plot.LINE);
plot.setColor(Color.BLACK);
double amplitude = Double.NaN;
for (int i = 0; i < smoothAz.length; i++) {
if (smoothAz[i] == cz) {
amplitude = smoothA[i];
break;
}
}
double maxAmplitude = Double.NaN;
for (int i = 0; i < smoothAz.length; i++) {
if (smoothAz[i] == zCentre) {
maxAmplitude = smoothA[i];
break;
}
}
plot.addLabel(0, 0, String.format("Amplitude = %s (%sx). z = %s nm", MathUtils.rounded(amplitude), MathUtils.rounded(amplitude / maxAmplitude), MathUtils.rounded((slice - zCentre) * settings.getNmPerSlice())));
amplitudeWindow = ImageJUtils.display(TITLE_AMPLITUDE, plot);
}
// Show plot of width, X centre, Y centre
if (xCoord != null) {
final Plot plot = new Plot(TITLE_PSF_PARAMETERS, "z", "px");
plot.addPoints(newZ, smoothSd, Plot.LINE);
// Get the limits
final double[] sd2 = invert(sd);
final double[] limits = MathUtils.limits(MathUtils.limits(MathUtils.limits(MathUtils.limits(xCoord), yCoord), sd), sd2);
plot.setLimits(z[0], z[z.length - 1], limits[0], limits[1]);
plot.addPoints(newZ, invert(smoothSd), Plot.LINE);
plot.addPoints(z, sd, Plot.DOT);
plot.addPoints(z, sd2, Plot.DOT);
plot.setColor(Color.BLUE);
plot.addPoints(z, xCoord, Plot.DOT);
plot.addPoints(newZ, smoothX, Plot.LINE);
plot.setColor(Color.RED);
plot.addPoints(z, yCoord, Plot.DOT);
plot.addPoints(newZ, smoothY, Plot.LINE);
// Add a line for the z-centre
plot.setColor(Color.GREEN);
plot.addPoints(new double[] { cz, cz }, limits, Plot.LINE);
plot.setColor(Color.BLACK);
double width = Double.NaN;
for (int i = 0; i < smoothSd.length; i++) {
if (newZ[i] == cz) {
width = smoothSd[i];
break;
}
}
plot.addLabel(0, 0, String.format("Width = %s nm (%sx). z = %s nm", MathUtils.rounded(width * nmPerPixel), MathUtils.rounded(width * nmPerPixel / psfWidth), MathUtils.rounded((slice - zCentre) * settings.getNmPerSlice())));
// Check if the window will need to be aligned
final boolean alignWindows = (WindowManager.getFrame(TITLE_PSF_PARAMETERS) == null);
final PlotWindow psfWindow = ImageJUtils.display(TITLE_PSF_PARAMETERS, plot);
if (alignWindows && psfWindow != null && amplitudeWindow != null) {
// Put the two plots tiled together so both are visible
final Point l = psfWindow.getLocation();
l.x = amplitudeWindow.getLocation().x;
l.y = amplitudeWindow.getLocation().y + amplitudeWindow.getHeight();
psfWindow.setLocation(l);
}
}
}
use of ij.gui.PlotWindow in project GDSC-SMLM by aherbert.
the class FilterAnalysis method showPlots.
private void showPlots() {
if (plots.isEmpty()) {
return;
}
// Display the top N plots
final int[] list = new int[plots.size()];
int index = 0;
for (final NamedPlot p : plots) {
final Plot plot = new Plot(p.name, p.xAxisName, "Jaccard");
plot.addPoints(p.xValues, p.yValues, Plot.LINE);
plot.setLimits(p.xValues[0], p.xValues[p.xValues.length - 1], 0, 1);
plot.setColor(Color.RED);
plot.draw();
plot.setColor(Color.BLUE);
plot.addPoints(p.xValues, p.yValues, Plot.CROSS);
final PlotWindow plotWindow = ImageJUtils.display(p.name, plot);
list[index++] = plotWindow.getImagePlus().getID();
}
WindowOrganiser.tileWindows(list);
}
use of ij.gui.PlotWindow in project GDSC-SMLM by aherbert.
the class EmGainAnalysis method plotPmf.
@SuppressWarnings("unused")
private void plotPmf() {
if (!showPmfDialog()) {
return;
}
final double step = getStepSize(settings.settingPhotons, settings.settingGain, settings.settingNoise);
final Pdf pdf = pdf(0, step, settings.settingPhotons, settings.settingGain, settings.settingNoise);
double[] pmf = pdf.probability;
double yMax = MathUtils.max(pmf);
// Get the approximation
LikelihoodFunction fun;
switch(settings.approximationType) {
case 3:
fun = new PoissonFunction(1.0 / settings.settingGain);
break;
case 2:
// Use adaptive normalisation
fun = PoissonGaussianFunction2.createWithStandardDeviation(1.0 / settings.settingGain, settings.settingNoise);
break;
case 1:
// Create Poisson-Gamma (no Gaussian noise)
fun = createPoissonGammaGaussianFunction(0);
break;
case 0:
default:
fun = createPoissonGammaGaussianFunction(settings.settingNoise);
}
double expected = settings.settingPhotons;
if (settings.settingOffset != 0) {
expected += settings.settingOffset * expected / 100.0;
}
// Normalise
final boolean normalise = false;
if (normalise) {
final double sum = MathUtils.sum(pmf);
for (int i = pmf.length; i-- > 0; ) {
pmf[i] /= sum;
}
}
// Get CDF
double sum = 0;
double sum2 = 0;
double[] x = pdf.x;
double[] fvalues = new double[x.length];
double[] cdf1 = new double[pmf.length];
double[] cdf2 = new double[pmf.length];
for (int i = 0; i < cdf1.length; i++) {
sum += pmf[i] * step;
cdf1[i] = sum;
fvalues[i] = fun.likelihood(x[i], expected);
sum2 += fvalues[i] * step;
cdf2[i] = sum2;
}
// Truncate x for plotting
int max = 0;
double plimit = 1 - settings.tail;
while (sum < plimit && max < pmf.length) {
sum += pmf[max] * step;
if (sum > 0.5 && pmf[max] == 0) {
break;
}
max++;
}
int min = pmf.length;
sum = 0;
plimit = 1 - settings.head;
while (sum < plimit && min > 0) {
min--;
sum += pmf[min] * step;
if (sum > 0.5 && pmf[min] == 0) {
break;
}
}
pmf = Arrays.copyOfRange(pmf, min, max);
x = Arrays.copyOfRange(x, min, max);
fvalues = Arrays.copyOfRange(fvalues, min, max);
if (settings.showApproximation) {
yMax = MathUtils.maxDefault(yMax, fvalues);
}
final String label = String.format("Gain=%s, noise=%s, photons=%s", MathUtils.rounded(settings.settingGain), MathUtils.rounded(settings.settingNoise), MathUtils.rounded(settings.settingPhotons));
final Plot plot = new Plot("PMF", "ADUs", "p");
plot.setLimits(x[0], x[x.length - 1], 0, yMax);
plot.setColor(Color.red);
plot.addPoints(x, pmf, Plot.LINE);
if (settings.showApproximation) {
plot.setColor(Color.blue);
plot.addPoints(x, fvalues, Plot.LINE);
}
plot.setColor(Color.magenta);
plot.drawLine(settings.settingPhotons * settings.settingGain, 0, settings.settingPhotons * settings.settingGain, yMax);
plot.setColor(Color.black);
plot.addLabel(0, 0, label);
final PlotWindow win1 = ImageJUtils.display("PMF", plot);
// Plot the difference between the actual and approximation
final double[] delta = new double[fvalues.length];
for (int i = 0; i < fvalues.length; i++) {
if (pmf[i] == 0 && fvalues[i] == 0) {
continue;
}
if (settings.relativeDelta) {
delta[i] = DoubleEquality.relativeError(fvalues[i], pmf[i]) * Math.signum(fvalues[i] - pmf[i]);
} else {
delta[i] = fvalues[i] - pmf[i];
}
}
final Plot plot2 = new Plot("PMF delta", "ADUs", (settings.relativeDelta) ? "Relative delta" : "delta");
final double[] limits = MathUtils.limits(delta);
plot2.setLimits(x[0], x[x.length - 1], limits[0], limits[1]);
plot2.setColor(Color.red);
plot2.addPoints(x, delta, Plot.LINE);
plot2.setColor(Color.magenta);
plot2.drawLine(settings.settingPhotons * settings.settingGain, limits[0], settings.settingPhotons * settings.settingGain, limits[1]);
plot2.setColor(Color.black);
plot2.addLabel(0, 0, label + ((settings.settingOffset == 0) ? "" : ", expected = " + MathUtils.rounded(expected / settings.settingGain)));
final WindowOrganiser wo = new WindowOrganiser();
final PlotWindow win2 = ImageJUtils.display("PMF delta", plot2, wo);
if (wo.isNotEmpty()) {
final Point p2 = win1.getLocation();
p2.y += win1.getHeight();
win2.setLocation(p2);
}
// Plot the CDF of each distribution.
// Compute the Kolmogorov distance as the supremum (maximum)
// difference between the two cumulative probability distributions.
// https://en.wikipedia.org/wiki/Kolmogorov%E2%80%93Smirnov_test
double kolmogorovDistance = 0;
double xd = x[0];
for (int i = 0; i < cdf1.length; i++) {
final double dist = Math.abs(cdf1[i] - cdf2[i]);
if (kolmogorovDistance < dist) {
kolmogorovDistance = dist;
xd = pdf.x[i];
}
}
cdf1 = Arrays.copyOfRange(cdf1, min, max);
cdf2 = Arrays.copyOfRange(cdf2, min, max);
final Plot plot3 = new Plot("CDF", "ADUs", "p");
yMax = 1.05;
plot3.setLimits(x[0], x[x.length - 1], 0, yMax);
plot3.setColor(Color.red);
plot3.addPoints(x, cdf1, Plot.LINE);
plot3.setColor(Color.blue);
plot3.addPoints(x, cdf2, Plot.LINE);
plot3.setColor(Color.magenta);
plot3.drawLine(settings.settingPhotons * settings.settingGain, 0, settings.settingPhotons * settings.settingGain, yMax);
plot3.drawDottedLine(xd, 0, xd, yMax, 2);
plot3.setColor(Color.black);
plot3.addLabel(0, 0, label + ", Kolmogorov distance = " + MathUtils.rounded(kolmogorovDistance) + " @ " + xd);
plot3.addLegend("CDF\nApprox");
final int size = wo.size();
final PlotWindow win3 = ImageJUtils.display("CDF", plot3, wo);
if (size != wo.size()) {
final Point p2 = win1.getLocation();
p2.x += win1.getWidth();
win3.setLocation(p2);
}
}
use of ij.gui.PlotWindow in project GDSC-SMLM by aherbert.
the class BenchmarkFilterAnalysis method showPlots.
private void showPlots() {
if (filterAnalysisResult.plots.isEmpty()) {
return;
}
// Display the top N plots
final int[] list = new int[filterAnalysisResult.plots.size()];
int index = 0;
for (final NamedPlot p : filterAnalysisResult.plots) {
final Plot plot = new Plot(p.name, p.xAxisName, Settings.COLUMNS[settings.scoreIndex]);
plot.setLimits(p.xValues[0], p.xValues[p.xValues.length - 1], 0, 1);
plot.setColor(Color.RED);
plot.addPoints(p.xValues, p.yValues, Plot.LINE);
plot.setColor(Color.BLUE);
plot.addPoints(p.xValues, p.yValues, Plot.CROSS);
final PlotWindow plotWindow = ImageJUtils.display(p.name, plot);
list[index++] = plotWindow.getImagePlus().getID();
}
WindowOrganiser.tileWindows(list);
}
Aggregations