use of org.apache.commons.math3.geometry.euclidean.twod.Line in project GDSC-SMLM by aherbert.
the class MeanVarianceTest method run.
/*
* (non-Javadoc)
*
* @see ij.plugin.PlugIn#run(java.lang.String)
*/
public void run(String arg) {
SMLMUsageTracker.recordPlugin(this.getClass(), arg);
if (Utils.isExtraOptions()) {
ImagePlus imp = WindowManager.getCurrentImage();
if (imp.getStackSize() > 1) {
GenericDialog gd = new GenericDialog(TITLE);
gd.addMessage("Perform single image analysis on the current image?");
gd.addNumericField("Bias", _bias, 0);
gd.showDialog();
if (gd.wasCanceled())
return;
singleImage = true;
_bias = Math.abs(gd.getNextNumber());
} else {
IJ.error(TITLE, "Single-image mode requires a stack");
return;
}
}
List<ImageSample> images;
String inputDirectory = "";
if (singleImage) {
IJ.showStatus("Loading images...");
images = getImages();
if (images.size() == 0) {
IJ.error(TITLE, "Not enough images for analysis");
return;
}
} else {
inputDirectory = IJ.getDirectory("Select image series ...");
if (inputDirectory == null)
return;
SeriesOpener series = new SeriesOpener(inputDirectory, false, 0);
series.setVariableSize(true);
if (series.getNumberOfImages() < 3) {
IJ.error(TITLE, "Not enough images in the selected directory");
return;
}
if (!IJ.showMessageWithCancel(TITLE, String.format("Analyse %d images, first image:\n%s", series.getNumberOfImages(), series.getImageList()[0]))) {
return;
}
IJ.showStatus("Loading images");
images = getImages(series);
if (images.size() < 3) {
IJ.error(TITLE, "Not enough images for analysis");
return;
}
if (images.get(0).exposure != 0) {
IJ.error(TITLE, "First image in series must have exposure 0 (Bias image)");
return;
}
}
boolean emMode = (arg != null && arg.contains("em"));
GenericDialog gd = new GenericDialog(TITLE);
gd.addMessage("Set the output options:");
gd.addCheckbox("Show_table", showTable);
gd.addCheckbox("Show_charts", showCharts);
if (emMode) {
// Ask the user for the camera gain ...
gd.addMessage("Estimating the EM-gain requires the camera gain without EM readout enabled");
gd.addNumericField("Camera_gain (ADU/e-)", cameraGain, 4);
}
gd.showDialog();
if (gd.wasCanceled())
return;
showTable = gd.getNextBoolean();
showCharts = gd.getNextBoolean();
if (emMode) {
cameraGain = gd.getNextNumber();
}
IJ.showStatus("Computing mean & variance");
final double nImages = images.size();
for (int i = 0; i < images.size(); i++) {
IJ.showStatus(String.format("Computing mean & variance %d/%d", i + 1, images.size()));
images.get(i).compute(singleImage, i / nImages, (i + 1) / nImages);
}
IJ.showProgress(1);
IJ.showStatus("Computing results");
// Allow user to input multiple bias images
int start = 0;
Statistics biasStats = new Statistics();
Statistics noiseStats = new Statistics();
final double bias;
if (singleImage) {
bias = _bias;
} else {
while (start < images.size()) {
ImageSample sample = images.get(start);
if (sample.exposure == 0) {
biasStats.add(sample.means);
for (PairSample pair : sample.samples) {
noiseStats.add(pair.variance);
}
start++;
} else
break;
}
bias = biasStats.getMean();
}
// Get the mean-variance data
int total = 0;
for (int i = start; i < images.size(); i++) total += images.get(i).samples.size();
if (showTable && total > 2000) {
gd = new GenericDialog(TITLE);
gd.addMessage("Table output requires " + total + " entries.\n \nYou may want to disable the table.");
gd.addCheckbox("Show_table", showTable);
gd.showDialog();
if (gd.wasCanceled())
return;
showTable = gd.getNextBoolean();
}
TextWindow results = (showTable) ? createResultsWindow() : null;
double[] mean = new double[total];
double[] variance = new double[mean.length];
Statistics gainStats = (singleImage) ? new StoredDataStatistics(total) : new Statistics();
final WeightedObservedPoints obs = new WeightedObservedPoints();
for (int i = (singleImage) ? 0 : start, j = 0; i < images.size(); i++) {
StringBuilder sb = (showTable) ? new StringBuilder() : null;
ImageSample sample = images.get(i);
for (PairSample pair : sample.samples) {
if (j % 16 == 0)
IJ.showProgress(j, total);
mean[j] = pair.getMean();
variance[j] = pair.variance;
// Gain is in ADU / e
double gain = variance[j] / (mean[j] - bias);
gainStats.add(gain);
obs.add(mean[j], variance[j]);
if (emMode) {
gain /= (2 * cameraGain);
}
if (showTable) {
sb.append(sample.title).append("\t");
sb.append(sample.exposure).append("\t");
sb.append(pair.slice1).append("\t");
sb.append(pair.slice2).append("\t");
sb.append(IJ.d2s(pair.mean1, 2)).append("\t");
sb.append(IJ.d2s(pair.mean2, 2)).append("\t");
sb.append(IJ.d2s(mean[j], 2)).append("\t");
sb.append(IJ.d2s(variance[j], 2)).append("\t");
sb.append(Utils.rounded(gain, 4)).append("\n");
}
j++;
}
if (showTable)
results.append(sb.toString());
}
IJ.showProgress(1);
if (singleImage) {
StoredDataStatistics stats = (StoredDataStatistics) gainStats;
Utils.log(TITLE);
if (emMode) {
double[] values = stats.getValues();
MathArrays.scaleInPlace(0.5, values);
stats = new StoredDataStatistics(values);
}
if (showCharts) {
// Plot the gain over time
String title = TITLE + " Gain vs Frame";
Plot2 plot = new Plot2(title, "Slice", "Gain", Utils.newArray(gainStats.getN(), 1, 1.0), stats.getValues());
PlotWindow pw = Utils.display(title, plot);
// Show a histogram
String label = String.format("Mean = %s, Median = %s", Utils.rounded(stats.getMean()), Utils.rounded(stats.getMedian()));
int id = Utils.showHistogram(TITLE, stats, "Gain", 0, 1, 100, true, label);
if (Utils.isNewWindow()) {
Point point = pw.getLocation();
point.x = pw.getLocation().x;
point.y += pw.getHeight();
WindowManager.getImage(id).getWindow().setLocation(point);
}
}
Utils.log("Single-image mode: %s camera", (emMode) ? "EM-CCD" : "Standard");
final double gain = stats.getMedian();
if (emMode) {
final double totalGain = gain;
final double emGain = totalGain / cameraGain;
Utils.log(" Gain = 1 / %s (ADU/e-)", Utils.rounded(cameraGain, 4));
Utils.log(" EM-Gain = %s", Utils.rounded(emGain, 4));
Utils.log(" Total Gain = %s (ADU/e-)", Utils.rounded(totalGain, 4));
} else {
cameraGain = gain;
Utils.log(" Gain = 1 / %s (ADU/e-)", Utils.rounded(cameraGain, 4));
}
} else {
IJ.showStatus("Computing fit");
// Sort
int[] indices = rank(mean);
mean = reorder(mean, indices);
variance = reorder(variance, indices);
// Compute optimal coefficients.
// a - b x
final double[] init = { 0, 1 / gainStats.getMean() };
final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2).withStartPoint(init);
final double[] best = fitter.fit(obs.toList());
// Construct the polynomial that best fits the data.
final PolynomialFunction fitted = new PolynomialFunction(best);
if (showCharts) {
// Plot mean verses variance. Gradient is gain in ADU/e.
String title = TITLE + " results";
Plot2 plot = new Plot2(title, "Mean", "Variance");
double[] xlimits = Maths.limits(mean);
double[] ylimits = Maths.limits(variance);
double xrange = (xlimits[1] - xlimits[0]) * 0.05;
if (xrange == 0)
xrange = 0.05;
double yrange = (ylimits[1] - ylimits[0]) * 0.05;
if (yrange == 0)
yrange = 0.05;
plot.setLimits(xlimits[0] - xrange, xlimits[1] + xrange, ylimits[0] - yrange, ylimits[1] + yrange);
plot.setColor(Color.blue);
plot.addPoints(mean, variance, Plot2.CROSS);
plot.setColor(Color.red);
plot.addPoints(new double[] { mean[0], mean[mean.length - 1] }, new double[] { fitted.value(mean[0]), fitted.value(mean[mean.length - 1]) }, Plot2.LINE);
Utils.display(title, plot);
}
final double avBiasNoise = Math.sqrt(noiseStats.getMean());
Utils.log(TITLE);
Utils.log(" Directory = %s", inputDirectory);
Utils.log(" Bias = %s +/- %s (ADU)", Utils.rounded(bias, 4), Utils.rounded(avBiasNoise, 4));
Utils.log(" Variance = %s + %s * mean", Utils.rounded(best[0], 4), Utils.rounded(best[1], 4));
if (emMode) {
final double emGain = best[1] / (2 * cameraGain);
// Noise is standard deviation of the bias image divided by the total gain (in ADU/e-)
final double totalGain = emGain * cameraGain;
Utils.log(" Read Noise = %s (e-) [%s (ADU)]", Utils.rounded(avBiasNoise / totalGain, 4), Utils.rounded(avBiasNoise, 4));
Utils.log(" Gain = 1 / %s (ADU/e-)", Utils.rounded(1 / cameraGain, 4));
Utils.log(" EM-Gain = %s", Utils.rounded(emGain, 4));
Utils.log(" Total Gain = %s (ADU/e-)", Utils.rounded(totalGain, 4));
} else {
// Noise is standard deviation of the bias image divided by the gain (in ADU/e-)
cameraGain = best[1];
final double readNoise = avBiasNoise / cameraGain;
Utils.log(" Read Noise = %s (e-) [%s (ADU)]", Utils.rounded(readNoise, 4), Utils.rounded(readNoise * cameraGain, 4));
Utils.log(" Gain = 1 / %s (ADU/e-)", Utils.rounded(1 / cameraGain, 4));
}
}
IJ.showStatus("");
}
use of org.apache.commons.math3.geometry.euclidean.twod.Line in project GDSC-SMLM by aherbert.
the class SpotInspector method run.
/*
* (non-Javadoc)
*
* @see ij.plugin.PlugIn#run(java.lang.String)
*/
public void run(String arg) {
SMLMUsageTracker.recordPlugin(this.getClass(), arg);
if (MemoryPeakResults.isMemoryEmpty()) {
IJ.error(TITLE, "No localisations in memory");
return;
}
if (!showDialog())
return;
// Load the results
results = ResultsManager.loadInputResults(inputOption, false);
if (results == null || results.size() == 0) {
IJ.error(TITLE, "No results could be loaded");
IJ.showStatus("");
return;
}
// Check if the original image is open
ImageSource source = results.getSource();
if (source == null) {
IJ.error(TITLE, "Unknown original source image");
return;
}
source = source.getOriginal();
if (!source.open()) {
IJ.error(TITLE, "Cannot open original source image: " + source.toString());
return;
}
final float stdDevMax = getStandardDeviation(results);
if (stdDevMax < 0) {
// TODO - Add dialog to get the initial peak width
IJ.error(TITLE, "Fitting configuration (for initial peak width) is not available");
return;
}
// Rank spots
rankedResults = new ArrayList<PeakResultRank>(results.size());
final double a = results.getNmPerPixel();
final double gain = results.getGain();
final boolean emCCD = results.isEMCCD();
for (PeakResult r : results.getResults()) {
float[] score = getScore(r, a, gain, emCCD, stdDevMax);
rankedResults.add(new PeakResultRank(r, score[0], score[1]));
}
Collections.sort(rankedResults);
// Prepare results table. Get bias if necessary
if (showCalibratedValues) {
// Get a bias if required
Calibration calibration = results.getCalibration();
if (calibration.getBias() == 0) {
ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
gd.addMessage("Calibrated results requires a camera bias");
gd.addNumericField("Camera_bias (ADUs)", calibration.getBias(), 2);
gd.showDialog();
if (!gd.wasCanceled()) {
calibration.setBias(Math.abs(gd.getNextNumber()));
}
}
}
IJTablePeakResults table = new IJTablePeakResults(false, results.getName(), true);
table.copySettings(results);
table.setTableTitle(TITLE);
table.setAddCounter(true);
table.setShowCalibratedValues(showCalibratedValues);
table.begin();
// Add a mouse listener to jump to the frame for the clicked line
textPanel = table.getResultsWindow().getTextPanel();
// We must ignore old instances of this class from the mouse listeners
id = ++currentId;
textPanel.addMouseListener(this);
// Add results to the table
int n = 0;
for (PeakResultRank rank : rankedResults) {
rank.rank = n++;
PeakResult r = rank.peakResult;
table.add(r.getFrame(), r.origX, r.origY, r.origValue, r.error, r.noise, r.params, r.paramsStdDev);
}
table.end();
if (plotScore || plotHistogram) {
// Get values for the plots
float[] xValues = null, yValues = null;
double yMin, yMax;
int spotNumber = 0;
xValues = new float[rankedResults.size()];
yValues = new float[xValues.length];
for (PeakResultRank rank : rankedResults) {
xValues[spotNumber] = spotNumber + 1;
yValues[spotNumber++] = recoverScore(rank.score);
}
// Set the min and max y-values using 1.5 x IQR
DescriptiveStatistics stats = new DescriptiveStatistics();
for (float v : yValues) stats.addValue(v);
if (removeOutliers) {
double lower = stats.getPercentile(25);
double upper = stats.getPercentile(75);
double iqr = upper - lower;
yMin = FastMath.max(lower - iqr, stats.getMin());
yMax = FastMath.min(upper + iqr, stats.getMax());
IJ.log(String.format("Data range: %f - %f. Plotting 1.5x IQR: %f - %f", stats.getMin(), stats.getMax(), yMin, yMax));
} else {
yMin = stats.getMin();
yMax = stats.getMax();
IJ.log(String.format("Data range: %f - %f", yMin, yMax));
}
plotScore(xValues, yValues, yMin, yMax);
plotHistogram(yValues, yMin, yMax);
}
// Extract spots into a stack
final int w = source.getWidth();
final int h = source.getHeight();
final int size = 2 * radius + 1;
ImageStack spots = new ImageStack(size, size, rankedResults.size());
// To assist the extraction of data from the image source, process them in time order to allow
// frame caching. Then set the appropriate slice in the result stack
Collections.sort(rankedResults, new Comparator<PeakResultRank>() {
public int compare(PeakResultRank o1, PeakResultRank o2) {
if (o1.peakResult.getFrame() < o2.peakResult.getFrame())
return -1;
if (o1.peakResult.getFrame() > o2.peakResult.getFrame())
return 1;
return 0;
}
});
for (PeakResultRank rank : rankedResults) {
PeakResult r = rank.peakResult;
// Extract image
// Note that the coordinates are relative to the middle of the pixel (0.5 offset)
// so do not round but simply convert to int
final int x = (int) (r.params[Gaussian2DFunction.X_POSITION]);
final int y = (int) (r.params[Gaussian2DFunction.Y_POSITION]);
// Extract a region but crop to the image bounds
int minX = x - radius;
int minY = y - radius;
int maxX = FastMath.min(x + radius + 1, w);
int maxY = FastMath.min(y + radius + 1, h);
int padX = 0, padY = 0;
if (minX < 0) {
padX = -minX;
minX = 0;
}
if (minY < 0) {
padY = -minY;
minY = 0;
}
int sizeX = maxX - minX;
int sizeY = maxY - minY;
float[] data = source.get(r.getFrame(), new Rectangle(minX, minY, sizeX, sizeY));
// Prevent errors with missing data
if (data == null)
data = new float[sizeX * sizeY];
ImageProcessor spotIp = new FloatProcessor(sizeX, sizeY, data, null);
// Pad if necessary, i.e. the crop is too small for the stack
if (padX > 0 || padY > 0 || sizeX < size || sizeY < size) {
ImageProcessor spotIp2 = spotIp.createProcessor(size, size);
spotIp2.insert(spotIp, padX, padY);
spotIp = spotIp2;
}
int slice = rank.rank + 1;
spots.setPixels(spotIp.getPixels(), slice);
spots.setSliceLabel(Utils.rounded(rank.originalScore), slice);
}
source.close();
ImagePlus imp = Utils.display(TITLE, spots);
imp.setRoi((PointRoi) null);
// Make bigger
for (int i = 10; i-- > 0; ) imp.getWindow().getCanvas().zoomIn(imp.getWidth() / 2, imp.getHeight() / 2);
}
use of org.apache.commons.math3.geometry.euclidean.twod.Line in project GDSC-SMLM by aherbert.
the class PCPALMClusters method addToPlot.
private void addToPlot(int n, double p, String title, Plot2 plot, Color color) {
double[] x = new double[n + 1];
double[] y = new double[n + 1];
BinomialDistribution dist = new BinomialDistribution(n, p);
int startIndex = 1;
// Normalise optionally excluding the x=0 point
double total = 1;
if (startIndex > 0)
total -= dist.probability(0);
double cumul = 0;
for (int i = startIndex; i <= n; i++) {
cumul += dist.probability(i) / total;
x[i] = i;
y[i] = cumul;
}
plot.setColor(color);
plot.addPoints(x, y, Plot2.LINE);
//plot.addPoints(x, y, Plot2.CIRCLE);
Utils.display(title, plot);
}
use of org.apache.commons.math3.geometry.euclidean.twod.Line in project GDSC-SMLM by aherbert.
the class BFGSOptimizer method bfgs.
protected PointValuePair bfgs(ConvergenceChecker<PointValuePair> checker, double[] p, LineStepSearch lineSearch) {
final int n = p.length;
final double EPS = epsilon;
double[] hdg = new double[n];
double[] xi = new double[n];
double[][] hessian = new double[n][n];
// Get the gradient for the the bounded point
applyBounds(p);
double[] g = computeObjectiveGradient(p);
checkGradients(g, p);
// Initialise the hessian and search direction
for (int i = 0; i < n; i++) {
hessian[i][i] = 1.0;
xi[i] = -g[i];
}
PointValuePair current = null;
while (true) {
incrementIterationCount();
// Get the value of the point
double fp = computeObjectiveValue(p);
if (checker != null) {
PointValuePair previous = current;
current = new PointValuePair(p, fp);
if (previous != null && checker.converged(getIterations(), previous, current)) {
// We have found an optimum.
converged = CHECKER;
return current;
}
}
// Move along the search direction.
final double[] pnew;
try {
pnew = lineSearch.lineSearch(p, fp, g, xi);
} catch (LineSearchRoundoffException e) {
// This can happen if the Hessian is nearly singular or non-positive-definite.
// In this case the algorithm should be restarted.
converged = ROUNDOFF_ERROR;
//System.out.printf("Roundoff error, iter=%d\n", getIterations());
return new PointValuePair(p, fp);
}
// We assume the new point is on/within the bounds since the line search is constrained
double fret = lineSearch.f;
// Test for convergence on change in position
if (positionChecker.converged(p, pnew)) {
converged = POSITION;
return new PointValuePair(pnew, fret);
}
// Update the line direction
for (int i = 0; i < n; i++) {
xi[i] = pnew[i] - p[i];
}
p = pnew;
// Save the old gradient
double[] dg = g;
// Get the gradient for the new point
g = computeObjectiveGradient(p);
checkGradients(g, p);
// If necessary recompute the function value.
// Doing this after the gradient evaluation allows the value to be cached when
// computing the objective gradient
fp = fret;
// Test for convergence on zero gradient.
double test = 0;
for (int i = 0; i < n; i++) {
final double temp = Math.abs(g[i]) * FastMath.max(Math.abs(p[i]), 1);
//final double temp = Math.abs(g[i]);
if (test < temp)
test = temp;
}
// Compute the biggest gradient relative to the objective function
test /= FastMath.max(Math.abs(fp), 1);
if (test < gradientTolerance) {
converged = GRADIENT;
return new PointValuePair(p, fp);
}
for (int i = 0; i < n; i++) dg[i] = g[i] - dg[i];
for (int i = 0; i < n; i++) {
hdg[i] = 0.0;
for (int j = 0; j < n; j++) hdg[i] += hessian[i][j] * dg[j];
}
double fac = 0, fae = 0, sumdg = 0, sumxi = 0;
for (int i = 0; i < n; i++) {
fac += dg[i] * xi[i];
fae += dg[i] * hdg[i];
sumdg += dg[i] * dg[i];
sumxi += xi[i] * xi[i];
}
if (fac > Math.sqrt(EPS * sumdg * sumxi)) {
fac = 1.0 / fac;
final double fad = 1.0 / fae;
for (int i = 0; i < n; i++) dg[i] = fac * xi[i] - fad * hdg[i];
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
hessian[i][j] += fac * xi[i] * xi[j] - fad * hdg[i] * hdg[j] + fae * dg[i] * dg[j];
hessian[j][i] = hessian[i][j];
}
}
}
for (int i = 0; i < n; i++) {
xi[i] = 0.0;
for (int j = 0; j < n; j++) xi[i] -= hessian[i][j] * g[j];
}
}
}
use of org.apache.commons.math3.geometry.euclidean.twod.Line in project incubator-heron by apache.
the class ComponentMetricsHelper method computeBufferSizeTrend.
public void computeBufferSizeTrend() {
for (InstanceMetrics instanceMetrics : componentMetrics.getMetrics().values()) {
Map<Instant, Double> bufferMetrics = instanceMetrics.getMetrics().get(METRIC_BUFFER_SIZE.text());
if (bufferMetrics == null || bufferMetrics.size() < 3) {
// missing of insufficient data for creating a trend line
continue;
}
SimpleRegression simpleRegression = new SimpleRegression(true);
for (Instant timestamp : bufferMetrics.keySet()) {
simpleRegression.addData(timestamp.getEpochSecond(), bufferMetrics.get(timestamp));
}
double slope = simpleRegression.getSlope();
instanceMetrics.addMetric(METRIC_WAIT_Q_GROWTH_RATE.text(), slope);
if (maxBufferChangeRate < slope) {
maxBufferChangeRate = slope;
}
}
}
Aggregations