use of uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser in project GDSC-SMLM by aherbert.
the class CmosAnalysis method computeError.
private void computeError() {
// Assume the simulation stack and measured stack are not null.
ImageJUtils.log("Comparison to simulation: %s (%dx%dpx)", simulationImp.getInfoProperty(), simulationImp.getWidth(), simulationImp.getHeight());
WindowOrganiser wo = new WindowOrganiser();
final ImageStack simulationStack = simulationImp.getImageStack();
for (int slice = 1; slice <= 3; slice++) {
computeError(slice, simulationStack, wo);
}
wo.tile();
}
use of uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser in project GDSC-SMLM by aherbert.
the class ConfigurationTemplate method showTemplateImage.
/**
* Show template image.
*
* @param name the name
*/
private void showTemplateImage(String name) {
final ImagePlus tmpImp = getTemplateImage(name);
if (tmpImp == null) {
IJ.error(title, "Failed to load example image for template: " + name);
} else {
final WindowOrganiser windowOrganiser = new WindowOrganiser();
this.imp = displayTemplate(title, tmpImp, null);
if (windowOrganiser.isNotEmpty()) {
// Zoom a bit
final ImageWindow iw = this.imp.getWindow();
for (int i = 7; i-- > 0 && Math.max(iw.getWidth(), iw.getHeight()) < 512; ) {
iw.getCanvas().zoomIn(0, 0);
}
}
createResults(this.imp);
showTemplateInfo(name);
}
}
use of uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser in project GDSC-SMLM by aherbert.
the class CameraModelAnalysis method execute.
/**
* Execute the analysis.
*/
private boolean execute() {
dirty = false;
final CameraModelAnalysisSettings settings = this.settings.build();
if (!(getGain(settings) > 0)) {
ImageJUtils.log(TITLE + "Error: No total gain");
return false;
}
if (!(settings.getPhotons() > 0)) {
ImageJUtils.log(TITLE + "Error: No photons");
return false;
}
// Avoid repeating the same analysis
if (settings.equals(lastSettings)) {
return true;
}
lastSettings = settings;
final IntHistogram h = getHistogram(settings);
// Build cumulative distribution
final double[][] cdf1 = cumulativeHistogram(h);
final double[] x1 = cdf1[0];
final double[] y1 = cdf1[1];
// Interpolate to 300 steps faster evaluation?
// Get likelihood function
final LikelihoodFunction f = getLikelihoodFunction(settings);
// Create likelihood cumulative distribution
final double[][] cdf2 = cumulativeDistribution(settings, cdf1, f);
// Compute Komolgorov distance
final double[] distanceAndValue = getDistance(cdf1, cdf2);
final double distance = distanceAndValue[0];
final double value = distanceAndValue[1];
final double area = distanceAndValue[2];
final double[] x2 = cdf2[0];
final double[] y2 = cdf2[1];
// Fill y1
int offset = 0;
while (x2[offset] < x1[0]) {
offset++;
}
final double[] y1b = new double[y2.length];
System.arraycopy(y1, 0, y1b, offset, y1.length);
Arrays.fill(y1b, offset + y1.length, y2.length, y1[y1.length - 1]);
// KolmogorovSmirnovTest
// n is the number of samples used to build the probability distribution.
final int n = (int) MathUtils.sum(h.histogramCounts);
// From KolmogorovSmirnovTest.kolmogorovSmirnovTest(RealDistribution distribution, double[]
// data, boolean exact):
// Returns the p-value associated with the null hypothesis that data is a sample from
// distribution.
// E.g. If p<0.05 then the null hypothesis is rejected and the data do not match the
// distribution.
double pvalue = Double.NaN;
try {
pvalue = 1d - kolmogorovSmirnovTest.cdf(distance, n);
} catch (final MathArithmeticException ex) {
// Cannot be computed to leave at NaN
}
// Plot
final WindowOrganiser wo = new WindowOrganiser();
String title = TITLE + " CDF";
Plot plot = new Plot(title, "Count", "CDF");
final double max = 1.05 * MathUtils.maxDefault(1, y2);
plot.setLimits(x2[0], x2[x2.length - 1], 0, max);
plot.setColor(Color.blue);
plot.addPoints(x2, y1b, Plot.BAR);
plot.setColor(Color.red);
plot.addPoints(x2, y2, Plot.BAR);
plot.setColor(Color.magenta);
plot.drawLine(value, 0, value, max);
plot.setColor(Color.black);
plot.addLegend("CDF\nModel");
plot.addLabel(0, 0, String.format("Distance=%s @ %.0f (Mean=%s) : p=%s", MathUtils.rounded(distance), value, MathUtils.rounded(area), MathUtils.rounded(pvalue)));
ImageJUtils.display(title, plot, ImageJUtils.NO_TO_FRONT, wo);
// Show the histogram
title = TITLE + " Histogram";
plot = new Plot(title, "Count", "Frequency");
plot.setLimits(x1[0] - 0.5, x1[x1.length - 1] + 1.5, 0, MathUtils.max(h.histogramCounts) * 1.05);
plot.setColor(Color.blue);
plot.addPoints(x1, SimpleArrayUtils.toDouble(h.histogramCounts), Plot.BAR);
plot.setColor(Color.red);
final double[] x = floatHistogram[0].clone();
final double[] y = floatHistogram[1].clone();
final double scale = n / (MathUtils.sum(y) * (x[1] - x[0]));
for (int i = 0; i < y.length; i++) {
y[i] *= scale;
}
plot.addPoints(x, y, Plot.LINE);
plot.setColor(Color.black);
plot.addLegend("Sample\nExpected");
ImageJUtils.display(title, plot, ImageJUtils.NO_TO_FRONT, wo);
wo.tile();
return true;
}
use of uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser in project GDSC-SMLM by aherbert.
the class BackgroundEstimator method drawPlot.
/**
* Build a plot of the noise estimate from the current frame. Limit the preview to 100 frames.
*/
private void drawPlot() {
IJ.showStatus("Estimating background ...");
final int start = imp.getCurrentSlice();
final int end = Math.min(imp.getStackSize(), start + 100);
final int size = end - start + 1;
final double[] xValues = new double[size];
final double[] noise1 = new double[size];
final double[] noise2 = new double[size];
final double[] background = new double[size];
final double[] threshold = new double[size];
final double[] percentile = new double[size];
final ImageStack stack = imp.getImageStack();
final Rectangle bounds = imp.getProcessor().getRoi();
float[] buffer = null;
for (int slice = start, i = 0; slice <= end; slice++, i++) {
IJ.showProgress(i, size);
final ImageProcessor ip = stack.getProcessor(slice);
buffer = ImageJImageConverter.getData(ip.getPixels(), ip.getWidth(), ip.getHeight(), bounds, buffer);
final DataEstimator de = new DataEstimator(buffer, bounds.width, bounds.height);
de.setFraction(settings.fraction);
de.setHistogramSize(settings.histogramSize);
de.setThresholdMethod(settings.thresholdMethod);
xValues[i] = slice;
try {
noise1[i] = de.getNoise();
noise2[i] = de.getNoise(FitProtosHelper.convertNoiseEstimatorMethod(settings.noiseMethod));
background[i] = de.getBackground();
threshold[i] = de.getThreshold();
percentile[i] = de.getPercentile(settings.percentile);
} catch (final Exception ex) {
throw new DataException("Failed to estimate the background", ex);
}
}
IJ.showProgress(1);
IJ.showStatus("Plotting background ...");
final WindowOrganiser wo = new WindowOrganiser();
plot(wo, xValues, noise1, noise2, null, "Noise", "Background Noise", "Global Noise", null);
plot(wo, xValues, background, threshold, percentile, "Background", "Background", "Threshold", "Percentile");
wo.tile();
IJ.showStatus("");
}
use of uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser in project GDSC-SMLM by aherbert.
the class TcPalmAnalysis method runPlot.
/**
* Create the activations and total activations plots.
*
* @param settings the settings
*/
private void runPlot(TcPalmAnalysisSettings settings) {
final WindowOrganiser wo = new WindowOrganiser();
String timeLabel = "Time";
UnaryOperator<float[]> timeConverter;
double timeScale;
if (settings.getTimeInSeconds()) {
timeLabel += " (s)";
timeScale = 1.0 / results.getCalibration().getTimeCalibration().getExposureTime();
timeConverter = frames -> {
SimpleArrayUtils.apply(frames, f -> f *= timeScale);
return frames;
};
} else {
timeLabel += " (frame)";
timeConverter = UnaryOperator.identity();
timeScale = 1;
}
final CumulativeCountData data = this.countData;
String title = TITLE + " Activations vs Time";
final Plot plot1 = new Plot(title, timeLabel, "Count");
plot1.addLabel(0, 0, TextUtils.pleural(clusters.size(), "cluster"));
for (int i = 0; i < data.frames.length; i++) {
final int t = data.frames[i];
final int c = data.counts[i];
plot1.addPoints(timeConverter.apply(new float[] { t, t }), new float[] { 0, c }, Plot.LINE);
}
plot1.draw();
plot1.setLimitsToFit(true);
if (settings.getFixedTimeAxis()) {
final double[] limits = plot1.getLimits();
limits[0] = timeScale * (minT - 1);
limits[1] = timeScale * (maxT + 1);
plot1.setLimits(limits);
plot1.updateImage();
}
final PlotWindow pw1 = ImageJUtils.display(title, plot1, ImageJUtils.NO_TO_FRONT, wo);
title = TITLE + " Total Activations vs Time";
final Plot plot2 = new Plot(title, timeLabel, "Cumulative count");
final int localisations = data.counts.length == 0 ? 0 : data.plotCounts[data.plotCounts.length - 1];
final int clashes = localisations - data.counts.length;
plot2.addLabel(0, 0, TextUtils.pleural(localisations, "localisation") + " : " + clashes + TextUtils.pleuralise(clashes, " clash", " clashes"));
plot2.addPoints(timeConverter.apply(SimpleArrayUtils.toFloat(data.plotFrames)), SimpleArrayUtils.toFloat(data.plotCounts), Plot.LINE);
if (settings.getFixedTimeAxis()) {
plot2.setLimits(timeScale * (minT - 1), timeScale * (maxT + 1), Double.NaN, Double.NaN);
}
final PlotWindow pw2 = ImageJUtils.display(title, plot2, ImageJUtils.NO_TO_FRONT, wo);
activationsPlotData = new ActivationsPlotData(plot2, timeConverter, data);
// Simple tile one window above the other only if both are new.
if (wo.size() == 2) {
final Point p = pw1.getLocation();
p.y += pw1.getHeight();
pw2.setLocation(p);
}
}
Aggregations