use of org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction 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.polynomials.PolynomialSplineFunction in project GDSC-SMLM by aherbert.
the class TraceMolecules method interpolateZeroCrossingPoints.
@SuppressWarnings("unused")
private void interpolateZeroCrossingPoints() {
double[] x = new double[zeroCrossingPoints.size()];
double[] y = new double[zeroCrossingPoints.size()];
for (int i = 0; i < x.length; i++) {
double[] point = zeroCrossingPoints.get(i);
x[i] = point[0];
y[i] = point[1];
}
PolynomialSplineFunction fx = new SplineInterpolator().interpolate(x, y);
double minX = x[0];
double maxX = x[x.length - 1];
double xinc = (maxX - minX) / 50;
for (minX = minX + xinc; minX < maxX; minX += xinc) {
zeroCrossingPoints.add(new double[] { minX, fx.value(minX) });
}
sortPoints();
}
use of org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction in project xDrip-plus by jamorham.
the class LibreAlarmReceiver method CalculateFromDataTransferObject.
public static void CalculateFromDataTransferObject(ReadingData.TransferObject object, boolean use_raw) {
// insert any recent data we can
final List<GlucoseData> mTrend = object.data.trend;
if (mTrend != null) {
Collections.sort(mTrend);
final long thisSensorAge = mTrend.get(mTrend.size() - 1).sensorTime;
sensorAge = Pref.getInt("nfc_sensor_age", 0);
if (thisSensorAge > sensorAge) {
sensorAge = thisSensorAge;
Pref.setInt("nfc_sensor_age", (int) sensorAge);
Pref.setBoolean("nfc_age_problem", false);
Log.d(TAG, "Sensor age advanced to: " + thisSensorAge);
} else if (thisSensorAge == sensorAge) {
Log.wtf(TAG, "Sensor age has not advanced: " + sensorAge);
JoH.static_toast_long("Sensor clock has not advanced!");
Pref.setBoolean("nfc_age_problem", true);
// do not try to insert again
return;
} else {
Log.wtf(TAG, "Sensor age has gone backwards!!! " + sensorAge);
JoH.static_toast_long("Sensor age has gone backwards!!");
sensorAge = thisSensorAge;
Pref.setInt("nfc_sensor_age", (int) sensorAge);
Pref.setBoolean("nfc_age_problem", true);
}
if (d)
Log.d(TAG, "Oldest cmp: " + JoH.dateTimeText(oldest_cmp) + " Newest cmp: " + JoH.dateTimeText(newest_cmp));
long shiftx = 0;
if (mTrend.size() > 0) {
shiftx = getTimeShift(mTrend);
if (shiftx != 0)
Log.d(TAG, "Lag Timeshift: " + shiftx);
for (GlucoseData gd : mTrend) {
if (d)
Log.d(TAG, "DEBUG: sensor time: " + gd.sensorTime);
if ((timeShiftNearest > 0) && ((timeShiftNearest - gd.realDate) < segmentation_timeslice) && (timeShiftNearest - gd.realDate != 0)) {
if (d)
Log.d(TAG, "Skipping record due to closeness: " + JoH.dateTimeText(gd.realDate));
continue;
}
if (use_raw) {
// not quick for recent
createBGfromGD(gd, false);
} else {
BgReading.bgReadingInsertFromInt(gd.glucoseLevel, gd.realDate, true);
}
}
} else {
Log.e(TAG, "Trend data was empty!");
}
// munge and insert the history data if any is missing
final List<GlucoseData> mHistory = object.data.history;
if ((mHistory != null) && (mHistory.size() > 1)) {
Collections.sort(mHistory);
// applyTimeShift(mTrend, shiftx);
final List<Double> polyxList = new ArrayList<Double>();
final List<Double> polyyList = new ArrayList<Double>();
for (GlucoseData gd : mHistory) {
if (d)
Log.d(TAG, "history : " + JoH.dateTimeText(gd.realDate) + " " + gd.glucose(false));
polyxList.add((double) gd.realDate);
if (use_raw) {
polyyList.add((double) gd.glucoseLevelRaw);
createBGfromGD(gd, true);
} else {
polyyList.add((double) gd.glucoseLevel);
// add in the actual value
BgReading.bgReadingInsertFromInt(gd.glucoseLevel, gd.realDate, false);
}
}
// ConstrainedSplineInterpolator splineInterp = new ConstrainedSplineInterpolator();
final SplineInterpolator splineInterp = new SplineInterpolator();
try {
PolynomialSplineFunction polySplineF = splineInterp.interpolate(Forecast.PolyTrendLine.toPrimitiveFromList(polyxList), Forecast.PolyTrendLine.toPrimitiveFromList(polyyList));
final long startTime = mHistory.get(0).realDate;
final long endTime = mHistory.get(mHistory.size() - 1).realDate;
for (long ptime = startTime; ptime <= endTime; ptime += 300000) {
if (d)
Log.d(TAG, "Spline: " + JoH.dateTimeText((long) ptime) + " value: " + (int) polySplineF.value(ptime));
if (use_raw) {
createBGfromGD(new GlucoseData((int) polySplineF.value(ptime), ptime), true);
} else {
BgReading.bgReadingInsertFromInt((int) polySplineF.value(ptime), ptime, false);
}
}
} catch (org.apache.commons.math3.exception.NonMonotonicSequenceException e) {
Log.e(TAG, "NonMonotonicSequenceException: " + e);
}
} else {
Log.e(TAG, "no librealarm history data");
}
} else {
Log.d(TAG, "Trend data is null!");
}
}
use of org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction in project triplea by triplea-game.
the class MapRouteDrawer method drawCurvedPath.
/**
* Draws a smooth curve through the given array of points
*
* <p>
* This algorithm is called Spline-Interpolation
* because the Apache-commons-math library we are using here does not accept
* values but {@code f(x)=y} with x having to increase all the time
* the idea behind this is to use a parameter array - the so called index
* as x array and splitting the points into a x and y coordinates array.
* </p>
*
* <p>
* Finally those 2 interpolated arrays get unified into a single {@linkplain Point2D} array and drawn to the Map
* </p>
*
* @param graphics The {@linkplain Graphics2D} Object to be drawn on
* @param points The Knot Points for the Spline-Interpolator aka the joints
*/
private void drawCurvedPath(final Graphics2D graphics, final Point2D[] points) {
final double[] index = createParameterizedIndex(points);
final PolynomialSplineFunction xcurve = splineInterpolator.interpolate(index, getValues(points, Point2D::getX));
final double[] xcoords = getCoords(xcurve, index);
final PolynomialSplineFunction ycurve = splineInterpolator.interpolate(index, getValues(points, Point2D::getY));
final double[] ycoords = getCoords(ycurve, index);
final List<Path2D> paths = routeCalculator.getAllNormalizedLines(xcoords, ycoords);
for (final Path2D path : paths) {
drawTransformedShape(graphics, path);
}
// draws the Line to the Cursor on every possible screen, so that the line ends at the cursor no matter what...
final List<Point2D[]> finishingPoints = routeCalculator.getAllPoints(new Point2D.Double(xcoords[xcoords.length - 1], ycoords[ycoords.length - 1]), points[points.length - 1]);
final boolean hasArrowEnoughSpace = points[points.length - 2].distance(points[points.length - 1]) > ARROW_LENGTH;
for (final Point2D[] finishingPointArray : finishingPoints) {
drawTransformedShape(graphics, new Line2D.Double(finishingPointArray[0], finishingPointArray[1]));
if (hasArrowEnoughSpace) {
drawArrow(graphics, finishingPointArray[0], finishingPointArray[1]);
}
}
}
use of org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction in project GDSC-SMLM by aherbert.
the class BenchmarkSpotFit method showDoubleHistogram.
private double[] showDoubleHistogram(StoredDataStatistics[][] stats, final int index, WindowOrganiser wo, double[][] matchScores) {
final String xLabel = filterCriteria[index].name;
LowerLimit lower = filterCriteria[index].lower;
UpperLimit upper = filterCriteria[index].upper;
double[] jaccard = null;
double[] metric = null;
double maxJaccard = 0;
if (index <= FILTER_PRECISION && (settings.showFilterScoreHistograms || upper.requiresJaccard || lower.requiresJaccard)) {
// Jaccard score verses the range of the metric
for (final double[] d : matchScores) {
if (!Double.isFinite(d[index])) {
System.out.printf("Error in fit data [%d]: %s%n", index, d[index]);
}
}
// Do not use Double.compare(double, double) so we get exceptions in the sort for inf/nan
Arrays.sort(matchScores, (o1, o2) -> {
if (o1[index] < o2[index]) {
return -1;
}
if (o1[index] > o2[index]) {
return 1;
}
return 0;
});
final int scoreIndex = FILTER_PRECISION + 1;
final int n = results.size();
double tp = 0;
double fp = 0;
jaccard = new double[matchScores.length + 1];
metric = new double[jaccard.length];
for (int k = 0; k < matchScores.length; k++) {
final double score = matchScores[k][scoreIndex];
tp += score;
fp += (1 - score);
jaccard[k + 1] = tp / (fp + n);
metric[k + 1] = matchScores[k][index];
}
metric[0] = metric[1];
maxJaccard = MathUtils.max(jaccard);
if (settings.showFilterScoreHistograms) {
final String title = TITLE + " Jaccard " + xLabel;
final Plot plot = new Plot(title, xLabel, "Jaccard");
plot.addPoints(metric, jaccard, Plot.LINE);
// Remove outliers
final double[] limitsx = MathUtils.limits(metric);
final Percentile p = new Percentile();
final double l = p.evaluate(metric, 25);
final double u = p.evaluate(metric, 75);
final double iqr = 1.5 * (u - l);
limitsx[1] = Math.min(limitsx[1], u + iqr);
plot.setLimits(limitsx[0], limitsx[1], 0, MathUtils.max(jaccard));
ImageJUtils.display(title, plot, wo);
}
}
// [0] is all
// [1] is matches
// [2] is no match
final StoredDataStatistics s1 = stats[0][index];
final StoredDataStatistics s2 = stats[1][index];
final StoredDataStatistics s3 = stats[2][index];
if (s1.getN() == 0) {
return new double[4];
}
final DescriptiveStatistics d = s1.getStatistics();
double median = 0;
Plot plot = null;
String title = null;
if (settings.showFilterScoreHistograms) {
median = d.getPercentile(50);
final String label = String.format("n = %d. Median = %s nm", s1.getN(), MathUtils.rounded(median));
final HistogramPlot histogramPlot = new HistogramPlotBuilder(TITLE, s1, xLabel).setMinBinWidth(filterCriteria[index].minBinWidth).setRemoveOutliersOption((filterCriteria[index].restrictRange) ? 1 : 0).setPlotLabel(label).build();
final PlotWindow plotWindow = histogramPlot.show(wo);
if (plotWindow == null) {
IJ.log("Failed to show the histogram: " + xLabel);
return new double[4];
}
title = plotWindow.getTitle();
// Reverse engineer the histogram settings
plot = histogramPlot.getPlot();
final double[] xvalues = histogramPlot.getPlotXValues();
final int bins = xvalues.length;
final double yMin = xvalues[0];
final double binSize = xvalues[1] - xvalues[0];
final double yMax = xvalues[0] + (bins - 1) * binSize;
if (s2.getN() > 0) {
final double[] values = s2.getValues();
final double[][] hist = HistogramPlot.calcHistogram(values, yMin, yMax, bins);
if (hist[0].length > 0) {
plot.setColor(Color.red);
plot.addPoints(hist[0], hist[1], Plot.BAR);
ImageJUtils.display(title, plot);
}
}
if (s3.getN() > 0) {
final double[] values = s3.getValues();
final double[][] hist = HistogramPlot.calcHistogram(values, yMin, yMax, bins);
if (hist[0].length > 0) {
plot.setColor(Color.blue);
plot.addPoints(hist[0], hist[1], Plot.BAR);
ImageJUtils.display(title, plot);
}
}
}
// Do cumulative histogram
final double[][] h1 = MathUtils.cumulativeHistogram(s1.getValues(), true);
final double[][] h2 = MathUtils.cumulativeHistogram(s2.getValues(), true);
final double[][] h3 = MathUtils.cumulativeHistogram(s3.getValues(), true);
if (settings.showFilterScoreHistograms) {
title = TITLE + " Cumul " + xLabel;
plot = new Plot(title, xLabel, "Frequency");
// Find limits
double[] xlimit = MathUtils.limits(h1[0]);
xlimit = MathUtils.limits(xlimit, h2[0]);
xlimit = MathUtils.limits(xlimit, h3[0]);
// Restrict using the inter-quartile range
if (filterCriteria[index].restrictRange) {
final double q1 = d.getPercentile(25);
final double q2 = d.getPercentile(75);
final double iqr = (q2 - q1) * 2.5;
xlimit[0] = MathUtils.max(xlimit[0], median - iqr);
xlimit[1] = MathUtils.min(xlimit[1], median + iqr);
}
plot.setLimits(xlimit[0], xlimit[1], 0, 1.05);
plot.addPoints(h1[0], h1[1], Plot.LINE);
plot.setColor(Color.red);
plot.addPoints(h2[0], h2[1], Plot.LINE);
plot.setColor(Color.blue);
plot.addPoints(h3[0], h3[1], Plot.LINE);
}
// Determine the maximum difference between the TP and FP
double maxx1 = 0;
double maxx2 = 0;
double max1 = 0;
double max2 = 0;
// We cannot compute the delta histogram, or use percentiles
if (s2.getN() == 0) {
upper = UpperLimit.ZERO;
lower = LowerLimit.ZERO;
}
final boolean requireLabel = (settings.showFilterScoreHistograms && filterCriteria[index].requireLabel);
if (requireLabel || upper.requiresDeltaHistogram() || lower.requiresDeltaHistogram()) {
if (s2.getN() != 0 && s3.getN() != 0) {
final LinearInterpolator li = new LinearInterpolator();
final PolynomialSplineFunction f1 = li.interpolate(h2[0], h2[1]);
final PolynomialSplineFunction f2 = li.interpolate(h3[0], h3[1]);
for (final double x : h1[0]) {
if (x < h2[0][0] || x < h3[0][0]) {
continue;
}
try {
final double v1 = f1.value(x);
final double v2 = f2.value(x);
final double diff = v2 - v1;
if (diff > 0) {
if (max1 < diff) {
max1 = diff;
maxx1 = x;
}
} else if (max2 > diff) {
max2 = diff;
maxx2 = x;
}
} catch (final OutOfRangeException ex) {
// Because we reached the end
break;
}
}
}
}
if (plot != null) {
// We use bins=1 on charts where we do not need a label
if (requireLabel) {
final String label = String.format("Max+ %s @ %s, Max- %s @ %s", MathUtils.rounded(max1), MathUtils.rounded(maxx1), MathUtils.rounded(max2), MathUtils.rounded(maxx2));
plot.setColor(Color.black);
plot.addLabel(0, 0, label);
}
ImageJUtils.display(title, plot, wo);
}
// Now compute the bounds using the desired limit
double lowerBound;
double upperBound;
switch(lower) {
case MAX_NEGATIVE_CUMUL_DELTA:
// Switch to percentiles if we have no delta histogram
if (maxx2 < 0) {
lowerBound = maxx2;
break;
}
// fall-through
case ONE_PERCENT:
lowerBound = getPercentile(h2, 0.01);
break;
case MIN:
lowerBound = getPercentile(h2, 0.0);
break;
case ZERO:
lowerBound = 0;
break;
case HALF_MAX_JACCARD_VALUE:
lowerBound = getXValue(metric, jaccard, maxJaccard * 0.5);
break;
default:
throw new IllegalStateException("Missing lower limit method");
}
switch(upper) {
case MAX_POSITIVE_CUMUL_DELTA:
// Switch to percentiles if we have no delta histogram
if (maxx1 > 0) {
upperBound = maxx1;
break;
}
// fall-through
case NINETY_NINE_PERCENT:
upperBound = getPercentile(h2, 0.99);
break;
case NINETY_NINE_NINE_PERCENT:
upperBound = getPercentile(h2, 0.999);
break;
case ZERO:
upperBound = 0;
break;
case MAX_JACCARD2:
upperBound = getXValue(metric, jaccard, maxJaccard) * 2;
// System.out.printf("MaxJ = %.4f @ %.3f\n", maxJ, u / 2);
break;
default:
throw new IllegalStateException("Missing upper limit method");
}
final double min = getPercentile(h1, 0);
final double max = getPercentile(h1, 1);
return new double[] { lowerBound, upperBound, min, max };
}
Aggregations