use of uk.ac.sussex.gdsc.core.data.DataException 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.data.DataException in project GDSC-SMLM by aherbert.
the class BlinkEstimator method calculateAveragePrecision.
private double calculateAveragePrecision(MemoryPeakResults results, boolean verbose) {
double fittedAverage = 0;
try {
final PcPalmMolecules fitter = new PcPalmMolecules();
final List<Molecule> molecules = fitter.extractLocalisations(results);
final String title = (verbose) ? TITLE + " Localisation Precision" : null;
fittedAverage = fitter.calculateAveragePrecision(molecules, title, settings.histogramBins, true, true);
} catch (final DataException ex) {
// This is thrown when the data cannot be converted for precision computation
}
// Sense check the precision
if (fittedAverage < 5 || fittedAverage > 60) {
final GenericDialog gd = new GenericDialog(TITLE);
gd.addMessage("Estimated precision is not within expected bounds.\nPlease enter an estimate:");
gd.addSlider("Precision", 5, 60, fittedAverage);
gd.showDialog();
if (!gd.wasCanceled()) {
fittedAverage = gd.getNextNumber();
}
}
// The fitter does checks for a good fit to the histogram so just return the value
return fittedAverage;
}
use of uk.ac.sussex.gdsc.core.data.DataException in project GDSC-SMLM by aherbert.
the class SummariseResults method createSummary.
private static String createSummary(StringBuilder sb, MemoryPeakResults result, int[] removeNullResults) {
sb.setLength(0);
final DescriptiveStatistics[] stats = new DescriptiveStatistics[2];
for (int i = 0; i < stats.length; i++) {
stats[i] = new DescriptiveStatistics();
}
if (result.hasNullResults()) {
IJ.log("Null results in dataset: " + result.getName());
if (removeNullResults[0] == UNKNOWN) {
final GenericDialog gd = new GenericDialog(TITLE);
gd.addMessage("There are invalid results in memory.\n \nClean these results?");
gd.enableYesNoCancel();
gd.hideCancelButton();
gd.showDialog();
removeNullResults[0] = (gd.wasOKed()) ? YES : NO;
}
if (removeNullResults[0] == NO) {
result = result.copy();
}
result.removeNullResults();
}
final CalibrationReader calibration = result.getCalibrationReader();
PrecisionMethod precisionMethod = PrecisionMethod.PRECISION_METHOD_NA;
boolean stored = false;
final int size = result.size();
if (size > 0) {
// Precision
try {
final PrecisionResultProcedure p = new PrecisionResultProcedure(result);
// Use stored precision if possible
stored = result.hasPrecision();
precisionMethod = p.getPrecision(stored);
for (final double v : p.precisions) {
stats[0].addValue(v);
}
} catch (final DataException ex) {
// Ignore
}
// SNR
try {
final SnrResultProcedure p = new SnrResultProcedure(result);
p.getSnr();
for (final double v : p.snr) {
stats[1].addValue(v);
}
} catch (final DataException ex) {
// Ignore
}
}
sb.append(result.getName());
int maxT = 0;
if (result.size() == 0) {
sb.append("\t0\t0");
} else {
sb.append('\t').append(result.size());
maxT = result.getMaxFrame();
sb.append('\t').append(maxT);
}
if (calibration != null && calibration.hasExposureTime()) {
sb.append('\t').append(TextUtils.millisToString((long) Math.ceil(maxT * calibration.getExposureTime())));
} else {
sb.append("\t-");
}
if (size > 0) {
final boolean includeDeviations = result.hasDeviations();
final long memorySize = MemoryPeakResults.estimateMemorySize(size, includeDeviations);
final String memory = TextUtils.bytesToString(memorySize);
sb.append('\t').append(memory);
} else {
sb.append("\t-");
}
final Rectangle bounds = result.getBounds(true);
TextUtils.formatTo(sb, "\t%d,%d,%d,%d", bounds.x, bounds.y, bounds.x + bounds.width, bounds.y + bounds.height);
if (calibration != null) {
sb.append('\t').append(calibration.hasNmPerPixel() ? MathUtils.rounded(calibration.getNmPerPixel()) : '-');
sb.append('\t').append(calibration.hasExposureTime() ? MathUtils.rounded(calibration.getExposureTime()) : '-');
if (calibration.hasCameraType()) {
sb.append('\t').append(CalibrationProtosHelper.getName(calibration.getCameraType()));
if (calibration.isCcdCamera()) {
sb.append(" bias=").append(calibration.getBias());
sb.append(" gain=").append(calibration.getCountPerPhoton());
}
} else {
sb.append("\t-");
}
sb.append('\t').append(calibration.hasDistanceUnit() ? UnitHelper.getShortName(calibration.getDistanceUnit()) : '-');
sb.append('\t').append(calibration.hasIntensityUnit() ? UnitHelper.getShortName(calibration.getIntensityUnit()) : '-');
} else {
sb.append("\t\t\t\t\t");
}
if (result.is3D()) {
sb.append("\tY");
} else {
sb.append("\tN");
}
sb.append("\t").append(FitProtosHelper.getName(precisionMethod));
if (stored) {
sb.append(" (Stored)");
}
for (int i = 0; i < stats.length; i++) {
if (Double.isNaN(stats[i].getMean())) {
sb.append("\t-\t-\t-\t-");
} else {
sb.append('\t').append(IJ.d2s(stats[i].getMean(), 3));
sb.append('\t').append(IJ.d2s(stats[i].getPercentile(50), 3));
sb.append('\t').append(IJ.d2s(stats[i].getMin(), 3));
sb.append('\t').append(IJ.d2s(stats[i].getMax(), 3));
}
}
return sb.toString();
}
use of uk.ac.sussex.gdsc.core.data.DataException in project GDSC-SMLM by aherbert.
the class UpdateResultsBounds method showDialog.
private static boolean showDialog(MemoryPeakResults results) {
final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
gd.addHelp(HelpUrls.getUrl("update-results-bounds"));
// Force computation of bounds
Rectangle currentBounds = results.getBounds();
results.setBounds(null);
Rectangle autoBounds;
try {
autoBounds = results.getBounds(true);
} catch (final DataException ex) {
IJ.error(TITLE, "No calibration found to convert to pixel units");
return false;
} finally {
// Reset after forcing computation
if (currentBounds != null) {
results.setBounds(currentBounds);
}
}
// Re-acquire the bounds (either the existing bounds or the auto-bounds)
currentBounds = results.getBounds();
gd.addMessage(TextUtils.wrap("Set the bounds of the original source image.\n \nAuto-bounds = " + format(autoBounds) + "\n \nThe new bounds will be the union of the auto-bounds and specified bounds " + "to ensure all data is within the bounds.", 80));
gd.addNumericField("Min_x", currentBounds.x, 0, 6, "px");
gd.addNumericField("Min_y", currentBounds.y, 0, 6, "px");
gd.addNumericField("Width", currentBounds.width, 0, 6, "px");
gd.addNumericField("Height", currentBounds.height, 0, 6, "px");
gd.showDialog();
if (gd.wasCanceled()) {
return false;
}
final int x = (int) gd.getNextNumber();
final int y = (int) gd.getNextNumber();
final int width = (int) gd.getNextNumber();
final int height = (int) gd.getNextNumber();
// Check the bounds are not smaller than the auto-bounds
final Rectangle newBounds = new Rectangle(x, y, width, height).union(autoBounds);
if (newBounds.isEmpty()) {
IJ.error(TITLE, "New bounds are not valid: " + format(newBounds));
return false;
}
results.setBounds(newBounds);
return true;
}
use of uk.ac.sussex.gdsc.core.data.DataException in project GDSC-SMLM by aherbert.
the class FilterResults method analyseResults.
/**
* Analyse the results and determine the range for each filter.
*/
private boolean analyseResults() {
IJ.showStatus("Analysing results ...");
final ArrayList<String> error = new ArrayList<>();
try {
wp = new WidthResultProcedure(results, DistanceUnit.PIXEL);
wp.getW();
final float[] limits = MathUtils.limits(wp.wx);
maxWidth = limits[1];
minWidth = limits[0];
averageWidth = MathUtils.sum(wp.wx) / wp.size();
} catch (final DataException ex) {
error.add(ex.getMessage());
wp = null;
maxWidth = minWidth = 0;
}
try {
pp = new PrecisionResultProcedure(results);
pp.getPrecision();
final double[] limits = MathUtils.limits(pp.precisions);
maxPrecision = limits[1];
minPrecision = limits[0];
} catch (final DataException ex) {
error.add(ex.getMessage());
pp = null;
maxPrecision = minPrecision = 0;
}
try {
sp = new StandardResultProcedure(results, DistanceUnit.PIXEL);
sp.getXyr();
// Re-use for convenience
sp.intensity = new float[sp.x.length];
sp.background = new float[sp.x.length];
sp.z = new float[sp.x.length];
for (int i = 0; i < sp.size(); i++) {
if (i % 64 == 0) {
IJ.showProgress(i, sp.size());
}
final PeakResult result = sp.peakResults[i];
final float drift = getDrift(result, sp.x[i], sp.y[i]);
if (maxDrift < drift) {
maxDrift = drift;
}
if (minDrift > drift) {
minDrift = drift;
}
final float signal = result.getIntensity();
if (maxSignal < signal) {
maxSignal = signal;
}
if (minSignal > signal) {
minSignal = signal;
}
final float snr = getSnr(result);
if (maxSnr < snr) {
maxSnr = snr;
}
if (minSnr > snr) {
minSnr = snr;
}
// for convenience
sp.z[i] = drift;
sp.intensity[i] = signal;
sp.background[i] = snr;
}
} catch (final DataException ex) {
error.add(ex.getMessage());
sp = null;
}
if (error.size() == 3 || sp == null) {
final StringBuilder sb = new StringBuilder("Unable to analyse the results:\n");
for (final String s : error) {
sb.append(s).append(".\n");
}
IJ.error(TITLE, sb.toString());
return false;
}
ImageJUtils.finished();
return true;
}
Aggregations