use of org.apache.commons.math3.analysis.interpolation.LoessInterpolator in project GDSC-SMLM by aherbert.
the class DriftCalculator method smooth.
private static boolean smooth(double[] newDx, double[] newDy, double[] originalDriftTimePoints, double smoothing, int iterations) {
double[][] values = extractValues(originalDriftTimePoints, 0, newDx.length - 1, newDx, newDy);
// Smooth
LoessInterpolator loess = new LoessInterpolator(smoothing, iterations);
values[1] = loess.smooth(values[0], values[1]);
values[2] = loess.smooth(values[0], values[2]);
// Add back
int n = 0;
for (int t = 0; t < newDx.length; t++) {
if (originalDriftTimePoints[t] != 0) {
newDx[t] = values[1][n];
newDy[t] = values[2][n];
n++;
if (Double.isNaN(newDx[t])) {
Utils.log("ERROR : Loess smoothing created bad X-estimate at point %d/%d", t, newDx.length);
return false;
}
if (Double.isNaN(newDy[t])) {
Utils.log("ERROR : Loess smoothing created bad Y-estimate at point %d/%d", t, newDx.length);
return false;
}
}
}
return true;
}
use of org.apache.commons.math3.analysis.interpolation.LoessInterpolator in project GDSC-SMLM by aherbert.
the class SpotAnalysis method interpolate.
private double[] interpolate(double[] xValues2, double[] yValues) {
// Smooth the values not in the current on-frames
double[] newX = Arrays.copyOf(xValues, xValues.length);
double[] newY = Arrays.copyOf(yValues, yValues.length);
for (Spot s : onFrames) {
newX[s.frame - 1] = -1;
}
int c = 0;
for (int i = 0; i < newX.length; i++) {
if (newX[i] == -1)
continue;
newX[c] = newX[i];
newY[c] = newY[i];
c++;
}
newX = Arrays.copyOf(newX, c);
newY = Arrays.copyOf(newY, c);
double smoothing = 0.25;
try {
smoothing = Double.parseDouble(smoothingTextField.getText());
if (smoothing < 0.01 || smoothing > 0.9)
smoothing = 0.25;
} catch (NumberFormatException e) {
}
LoessInterpolator loess = new LoessInterpolator(smoothing, 1);
PolynomialSplineFunction f = loess.interpolate(newX, newY);
// Interpolate
double[] plotSmooth = new double[xValues.length];
for (int i = 0; i < xValues.length; i++) {
// Cannot interpolate outside the bounds of the input data
if (xValues[i] < newX[0])
plotSmooth[i] = newY[0];
else if (xValues[i] > newX[newX.length - 1])
plotSmooth[i] = newY[newX.length - 1];
else
plotSmooth[i] = f.value(xValues[i]);
}
return plotSmooth;
}
use of org.apache.commons.math3.analysis.interpolation.LoessInterpolator in project GDSC-SMLM by aherbert.
the class PSFCreator method run.
/*
* (non-Javadoc)
*
* @see ij.plugin.filter.PlugInFilter#run(ij.process.ImageProcessor)
*/
public void run(ImageProcessor ip) {
loadConfiguration();
BasePoint[] spots = getSpots();
if (spots.length == 0) {
IJ.error(TITLE, "No spots without neighbours within " + (boxRadius * 2) + "px");
return;
}
ImageStack stack = getImageStack();
final int width = imp.getWidth();
final int height = imp.getHeight();
final int currentSlice = imp.getSlice();
// Adjust settings for a single maxima
config.setIncludeNeighbours(false);
fitConfig.setDuplicateDistance(0);
ArrayList<double[]> centres = new ArrayList<double[]>(spots.length);
int iterations = 1;
LoessInterpolator loess = new LoessInterpolator(smoothing, iterations);
// TODO - The fitting routine may not produce many points. In this instance the LOESS interpolator
// fails to smooth the data very well. A higher bandwidth helps this but perhaps
// try a different smoothing method.
// For each spot
Utils.log(TITLE + ": " + imp.getTitle());
Utils.log("Finding spot locations...");
Utils.log(" %d spot%s without neighbours within %dpx", spots.length, ((spots.length == 1) ? "" : "s"), (boxRadius * 2));
StoredDataStatistics averageSd = new StoredDataStatistics();
StoredDataStatistics averageA = new StoredDataStatistics();
Statistics averageRange = new Statistics();
MemoryPeakResults allResults = new MemoryPeakResults();
allResults.setName(TITLE);
allResults.setBounds(new Rectangle(0, 0, width, height));
MemoryPeakResults.addResults(allResults);
for (int n = 1; n <= spots.length; n++) {
BasePoint spot = spots[n - 1];
final int x = (int) spot.getX();
final int y = (int) spot.getY();
MemoryPeakResults results = fitSpot(stack, width, height, x, y);
allResults.addAllf(results.getResults());
if (results.size() < 5) {
Utils.log(" Spot %d: Not enough fit results %d", n, results.size());
continue;
}
// Get the results for the spot centre and width
double[] z = new double[results.size()];
double[] xCoord = new double[z.length];
double[] yCoord = new double[z.length];
double[] sd = new double[z.length];
double[] a = new double[z.length];
int i = 0;
for (PeakResult peak : results.getResults()) {
z[i] = peak.getFrame();
xCoord[i] = peak.getXPosition() - x;
yCoord[i] = peak.getYPosition() - y;
sd[i] = FastMath.max(peak.getXSD(), peak.getYSD());
a[i] = peak.getAmplitude();
i++;
}
// Smooth the amplitude plot
double[] smoothA = loess.smooth(z, a);
// Find the maximum amplitude
int maximumIndex = findMaximumIndex(smoothA);
// Find the range at a fraction of the max. This is smoothed to find the X/Y centre
int start = 0, stop = smoothA.length - 1;
double limit = smoothA[maximumIndex] * amplitudeFraction;
for (int j = 0; j < smoothA.length; j++) {
if (smoothA[j] > limit) {
start = j;
break;
}
}
for (int j = smoothA.length; j-- > 0; ) {
if (smoothA[j] > limit) {
stop = j;
break;
}
}
averageRange.add(stop - start + 1);
// Extract xy centre coords and smooth
double[] smoothX = new double[stop - start + 1];
double[] smoothY = new double[smoothX.length];
double[] smoothSd = new double[smoothX.length];
double[] newZ = new double[smoothX.length];
for (int j = start, k = 0; j <= stop; j++, k++) {
smoothX[k] = xCoord[j];
smoothY[k] = yCoord[j];
smoothSd[k] = sd[j];
newZ[k] = z[j];
}
smoothX = loess.smooth(newZ, smoothX);
smoothY = loess.smooth(newZ, smoothY);
smoothSd = loess.smooth(newZ, smoothSd);
// Since the amplitude is not very consistent move from this peak to the
// lowest width which is the in-focus spot.
maximumIndex = findMinimumIndex(smoothSd, maximumIndex - start);
// Find the centre at the amplitude peak
double cx = smoothX[maximumIndex] + x;
double cy = smoothY[maximumIndex] + y;
int cz = (int) newZ[maximumIndex];
double csd = smoothSd[maximumIndex];
double ca = smoothA[maximumIndex + start];
// The average should weight the SD using the signal for each spot
averageSd.add(smoothSd[maximumIndex]);
averageA.add(ca);
if (ignoreSpot(n, z, a, smoothA, xCoord, yCoord, sd, newZ, smoothX, smoothY, smoothSd, cx, cy, cz, csd)) {
Utils.log(" Spot %d was ignored", n);
continue;
}
// Store result - it may have been moved interactively
maximumIndex += this.slice - cz;
cz = (int) newZ[maximumIndex];
csd = smoothSd[maximumIndex];
ca = smoothA[maximumIndex + start];
Utils.log(" Spot %d => x=%.2f, y=%.2f, z=%d, sd=%.2f, A=%.2f\n", n, cx, cy, cz, csd, ca);
centres.add(new double[] { cx, cy, cz, csd, n });
}
if (interactiveMode) {
imp.setSlice(currentSlice);
imp.setOverlay(null);
// Hide the amplitude and spot plots
Utils.hide(TITLE_AMPLITUDE);
Utils.hide(TITLE_PSF_PARAMETERS);
}
if (centres.isEmpty()) {
String msg = "No suitable spots could be identified centres";
Utils.log(msg);
IJ.error(TITLE, msg);
return;
}
// Find the limits of the z-centre
int minz = (int) centres.get(0)[2];
int maxz = minz;
for (double[] centre : centres) {
if (minz > centre[2])
minz = (int) centre[2];
else if (maxz < centre[2])
maxz = (int) centre[2];
}
IJ.showStatus("Creating PSF image");
// Create a stack that can hold all the data.
ImageStack psf = createStack(stack, minz, maxz, magnification);
// For each spot
Statistics stats = new Statistics();
boolean ok = true;
for (int i = 0; ok && i < centres.size(); i++) {
double progress = (double) i / centres.size();
final double increment = 1.0 / (stack.getSize() * centres.size());
IJ.showProgress(progress);
double[] centre = centres.get(i);
// Extract the spot
float[][] spot = new float[stack.getSize()][];
Rectangle regionBounds = null;
for (int slice = 1; slice <= stack.getSize(); slice++) {
ImageExtractor ie = new ImageExtractor((float[]) stack.getPixels(slice), width, height);
if (regionBounds == null)
regionBounds = ie.getBoxRegionBounds((int) centre[0], (int) centre[1], boxRadius);
spot[slice - 1] = ie.crop(regionBounds);
}
int n = (int) centre[4];
final float b = getBackground(n, spot);
if (!subtractBackgroundAndWindow(spot, b, regionBounds.width, regionBounds.height, centre, loess)) {
Utils.log(" Spot %d was ignored", n);
continue;
}
stats.add(b);
// Adjust the centre using the crop
centre[0] -= regionBounds.x;
centre[1] -= regionBounds.y;
// This takes a long time so this should track progress
ok = addToPSF(maxz, magnification, psf, centre, spot, regionBounds, progress, increment, centreEachSlice);
}
if (interactiveMode) {
Utils.hide(TITLE_INTENSITY);
}
IJ.showProgress(1);
if (threadPool != null) {
threadPool.shutdownNow();
threadPool = null;
}
if (!ok || stats.getN() == 0)
return;
final double avSd = getAverage(averageSd, averageA, 2);
Utils.log(" Average background = %.2f, Av. SD = %s px", stats.getMean(), Utils.rounded(avSd, 4));
normalise(psf, maxz, avSd * magnification, false);
IJ.showProgress(1);
psfImp = Utils.display("PSF", psf);
psfImp.setSlice(maxz);
psfImp.resetDisplayRange();
psfImp.updateAndDraw();
double[][] fitCom = new double[2][psf.getSize()];
Arrays.fill(fitCom[0], Double.NaN);
Arrays.fill(fitCom[1], Double.NaN);
double fittedSd = fitPSF(psf, loess, maxz, averageRange.getMean(), fitCom);
// Compute the drift in the PSF:
// - Use fitted centre if available; otherwise find CoM for each frame
// - express relative to the average centre
double[][] com = calculateCentreOfMass(psf, fitCom, nmPerPixel / magnification);
double[] slice = Utils.newArray(psf.getSize(), 1, 1.0);
String title = TITLE + " CoM Drift";
Plot2 plot = new Plot2(title, "Slice", "Drift (nm)");
plot.addLabel(0, 0, "Red = X; Blue = Y");
//double[] limitsX = Maths.limits(com[0]);
//double[] limitsY = Maths.limits(com[1]);
double[] limitsX = getLimits(com[0]);
double[] limitsY = getLimits(com[1]);
plot.setLimits(1, psf.getSize(), Math.min(limitsX[0], limitsY[0]), Math.max(limitsX[1], limitsY[1]));
plot.setColor(Color.red);
plot.addPoints(slice, com[0], Plot.DOT);
plot.addPoints(slice, loess.smooth(slice, com[0]), Plot.LINE);
plot.setColor(Color.blue);
plot.addPoints(slice, com[1], Plot.DOT);
plot.addPoints(slice, loess.smooth(slice, com[1]), Plot.LINE);
Utils.display(title, plot);
// TODO - Redraw the PSF with drift correction applied.
// This means that the final image should have no drift.
// This is relevant when combining PSF images. It doesn't matter too much for simulations
// unless the drift is large.
// Add Image properties containing the PSF details
final double fwhm = getFWHM(psf, maxz);
psfImp.setProperty("Info", XmlUtils.toXML(new PSFSettings(maxz, nmPerPixel / magnification, nmPerSlice, stats.getN(), fwhm, createNote())));
Utils.log("%s : z-centre = %d, nm/Pixel = %s, nm/Slice = %s, %d images, PSF SD = %s nm, FWHM = %s px\n", psfImp.getTitle(), maxz, Utils.rounded(nmPerPixel / magnification, 3), Utils.rounded(nmPerSlice, 3), stats.getN(), Utils.rounded(fittedSd * nmPerPixel, 4), Utils.rounded(fwhm));
createInteractivePlots(psf, maxz, nmPerPixel / magnification, fittedSd * nmPerPixel);
IJ.showStatus("");
}
use of org.apache.commons.math3.analysis.interpolation.LoessInterpolator in project GDSC-SMLM by aherbert.
the class PSFCreator method calculateCentreOfMass.
/**
* Calculate the centre of mass and express it relative to the average centre
*
* @param psf
* @param fitCom
* @param nmPerPixel
* @return The centre of mass
*/
private double[][] calculateCentreOfMass(ImageStack psf, double[][] fitCom, double nmPerPixel) {
final int size = psf.getSize();
double[][] com = new double[2][size];
final double offset = psf.getWidth() / 2.0;
for (int i = 0; i < size; i++) {
final double[] com2 = calculateCenterOfMass((FloatProcessor) psf.getProcessor(i + 1));
com[0][i] = com2[0] - offset;
com[1][i] = com2[1] - offset;
//if (!Double.isNaN(fitCom[0][i]))
//{
// // Interlacing the fit centre of mass is not consistent. There appears to be a large discrepancy
// // between the pixel centre-of-mass and the fit CoM. A small test shows correlation of
// // 0.11 and 0.066. Spearman's rank is 0.16. Basically it messes the data and effects smoothing.
// //System.out.printf("CoM = [ %f , %f ] == [ %f , %f ]\n", comX, comY, fitCom[0][i], fitCom[1][i]);
// //com[0][i] = fitCom[0][i];
// //com[1][i] = fitCom[1][i];
//}
}
// Smooth the curve ...
// LoessInterpolator loess = new LoessInterpolator(smoothing, 1);
// double[] slice = Utils.newArray(psf.getSize(), 1, 1.0);
// com[0] = loess.smooth(slice, com[0]);
// com[1] = loess.smooth(slice, com[1]);
// Express relative to the average centre
final double avX = new Statistics(com[0]).getMean();
final double avY = new Statistics(com[1]).getMean();
for (int i = 0; i < size; i++) {
com[0][i] = (com[0][i] - avX) * nmPerPixel;
com[1][i] = (com[1][i] - avY) * nmPerPixel;
}
return com;
}
use of org.apache.commons.math3.analysis.interpolation.LoessInterpolator in project GDSC-SMLM by aherbert.
the class BenchmarkFilterAnalysis method depthAnalysis.
/**
* Depth analysis.
*
* @param allAssignments
* The assignments generated from running the filter (or null)
* @param filter
* the filter
* @return the assignments
*/
private ArrayList<FractionalAssignment[]> depthAnalysis(ArrayList<FractionalAssignment[]> allAssignments, DirectFilter filter) {
if (!depthRecallAnalysis || simulationParameters.fixedDepth)
return null;
// Build a histogram of the number of spots at different depths
final double[] depths = depthStats.getValues();
double[] limits = Maths.limits(depths);
//final int bins = Math.max(10, nActual / 100);
//final int bins = Utils.getBinsSturges(depths.length);
final int bins = Utils.getBinsSqrt(depths.length);
double[][] h1 = Utils.calcHistogram(depths, limits[0], limits[1], bins);
double[][] h2 = Utils.calcHistogram(depthFitStats.getValues(), limits[0], limits[1], bins);
// manually to get the results that pass.
if (allAssignments == null)
allAssignments = getAssignments(filter);
double[] depths2 = new double[results.size()];
int count = 0;
for (FractionalAssignment[] assignments : allAssignments) {
if (assignments == null)
continue;
for (int i = 0; i < assignments.length; i++) {
final CustomFractionalAssignment c = (CustomFractionalAssignment) assignments[i];
depths2[count++] = c.peak.error;
}
}
depths2 = Arrays.copyOf(depths2, count);
// Build a histogram using the same limits
double[][] h3 = Utils.calcHistogram(depths2, limits[0], limits[1], bins);
// Convert pixel depth to nm
for (int i = 0; i < h1[0].length; i++) h1[0][i] *= simulationParameters.a;
limits[0] *= simulationParameters.a;
limits[1] *= simulationParameters.a;
// Produce a histogram of the number of spots at each depth
String title1 = TITLE + " Depth Histogram";
Plot2 plot1 = new Plot2(title1, "Depth (nm)", "Frequency");
plot1.setLimits(limits[0], limits[1], 0, Maths.max(h1[1]));
plot1.setColor(Color.black);
plot1.addPoints(h1[0], h1[1], Plot2.BAR);
plot1.addLabel(0, 0, "Black = Spots; Blue = Fitted; Red = Filtered");
plot1.setColor(Color.blue);
plot1.addPoints(h1[0], h2[1], Plot2.BAR);
plot1.setColor(Color.red);
plot1.addPoints(h1[0], h3[1], Plot2.BAR);
plot1.setColor(Color.magenta);
PlotWindow pw1 = Utils.display(title1, plot1);
if (Utils.isNewWindow())
wo.add(pw1);
// Interpolate
final double halfBinWidth = (h1[0][1] - h1[0][0]) * 0.5;
// Remove final value of the histogram as this is at the upper limit of the range (i.e. count zero)
h1[0] = Arrays.copyOf(h1[0], h1[0].length - 1);
h1[1] = Arrays.copyOf(h1[1], h1[0].length);
h2[1] = Arrays.copyOf(h2[1], h1[0].length);
h3[1] = Arrays.copyOf(h3[1], h1[0].length);
// TODO : Fix the smoothing since LOESS sometimes does not work.
// Perhaps allow configuration of the number of histogram bins and the smoothing bandwidth.
// Use minimum of 3 points for smoothing
// Ensure we use at least x% of data
double bandwidth = Math.max(3.0 / h1[0].length, 0.15);
LoessInterpolator loess = new LoessInterpolator(bandwidth, 1);
PolynomialSplineFunction spline1 = loess.interpolate(h1[0], h1[1]);
PolynomialSplineFunction spline2 = loess.interpolate(h1[0], h2[1]);
PolynomialSplineFunction spline3 = loess.interpolate(h1[0], h3[1]);
// Use a second interpolator in case the LOESS fails
LinearInterpolator lin = new LinearInterpolator();
PolynomialSplineFunction spline1b = lin.interpolate(h1[0], h1[1]);
PolynomialSplineFunction spline2b = lin.interpolate(h1[0], h2[1]);
PolynomialSplineFunction spline3b = lin.interpolate(h1[0], h3[1]);
// Increase the number of points to show a smooth curve
double[] points = new double[bins * 5];
limits = Maths.limits(h1[0]);
final double interval = (limits[1] - limits[0]) / (points.length - 1);
double[] v = new double[points.length];
double[] v2 = new double[points.length];
double[] v3 = new double[points.length];
for (int i = 0; i < points.length - 1; i++) {
points[i] = limits[0] + i * interval;
v[i] = getSplineValue(spline1, spline1b, points[i]);
v2[i] = getSplineValue(spline2, spline2b, points[i]);
v3[i] = getSplineValue(spline3, spline3b, points[i]);
points[i] += halfBinWidth;
}
// Final point on the limit of the spline range
int ii = points.length - 1;
v[ii] = getSplineValue(spline1, spline1b, limits[1]);
v2[ii] = getSplineValue(spline2, spline2b, limits[1]);
v3[ii] = getSplineValue(spline3, spline3b, limits[1]);
points[ii] = limits[1] + halfBinWidth;
// Calculate recall
for (int i = 0; i < v.length; i++) {
v2[i] = v2[i] / v[i];
v3[i] = v3[i] / v[i];
}
final double halfSummaryDepth = summaryDepth * 0.5;
String title2 = TITLE + " Depth Histogram (normalised)";
Plot2 plot2 = new Plot2(title2, "Depth (nm)", "Recall");
plot2.setLimits(limits[0] + halfBinWidth, limits[1] + halfBinWidth, 0, Maths.min(1, Maths.max(v2)));
plot2.setColor(Color.black);
plot2.addLabel(0, 0, "Blue = Fitted; Red = Filtered");
plot2.setColor(Color.blue);
plot2.addPoints(points, v2, Plot2.LINE);
plot2.setColor(Color.red);
plot2.addPoints(points, v3, Plot2.LINE);
plot2.setColor(Color.magenta);
if (-halfSummaryDepth - halfBinWidth >= limits[0]) {
plot2.drawLine(-halfSummaryDepth, 0, -halfSummaryDepth, getSplineValue(spline3, spline3b, -halfSummaryDepth - halfBinWidth) / getSplineValue(spline1, spline1b, -halfSummaryDepth - halfBinWidth));
}
if (halfSummaryDepth - halfBinWidth <= limits[1]) {
plot2.drawLine(halfSummaryDepth, 0, halfSummaryDepth, getSplineValue(spline3, spline3b, halfSummaryDepth - halfBinWidth) / getSplineValue(spline1, spline1b, halfSummaryDepth - halfBinWidth));
}
PlotWindow pw2 = Utils.display(title2, plot2);
if (Utils.isNewWindow())
wo.add(pw2);
return allAssignments;
}
Aggregations