Java Examples for org.apache.commons.math3.stat.regression.SimpleRegression
The following java examples will help you to understand the usage of org.apache.commons.math3.stat.regression.SimpleRegression. These source code samples are taken from different open source projects.
Example 1
| Project: svarog-master File: DetrendHelper.java View source code |
public static void detrend(double[][] v) {
if (v.length == 0)
return;
SimpleRegression regression = new SimpleRegression();
for (int i = 0; i < v.length; ++i) {
double y[] = v[i];
regression.clear();
for (int j = 0; j < y.length; ++j) {
regression.addData(j, y[j]);
}
double slope = regression.getSlope();
double intercept = regression.getIntercept();
for (int j = 0; j < y.length; ++j) {
y[j] -= intercept + slope * j;
}
}
}Example 2
| Project: Troia-Server-master File: QSPCalculators.java View source code |
@Override
public Double getWorkerWage(double qualifiedWage, double costThreshold, Map<String, Double> priors) {
SimpleRegression regression = new SimpleRegression();
for (int m = 1; m <= 41; m += 4) {
double cost = getWorkerCost(m, priors, 1000);
if (cost == 0)
break;
regression.addData(Math.log(cost), m);
}
double d = regression.predict(Math.log(costThreshold));
return qualifiedWage / d;
}Example 3
| Project: emlab-generation-master File: GeometricTrendRegressionTest.java View source code |
@Test
public void testLinearTrendEstimation() {
double[][] input = { { 0, 1 }, { 1, 1.1 }, { 2, 1.2 }, { 3, 1.3 }, { 4, 1.4 } };
double[] predictionYears = { 5, 6, 7, 8 };
double[] expectedResults = { 1.5, 1.6, 1.7, 1.8 };
SimpleRegression sr = new SimpleRegression();
sr.addData(input);
for (int i = 0; i < predictionYears.length; i++) {
assert (expectedResults[i] == sr.predict(predictionYears[i]));
}
}Example 4
| Project: common-java-cookbook-master File: SimpleRegressionExample.java View source code |
public static void main(String[] args) throws MathException {
SimpleRegression sr = new SimpleRegression();
// Add data points
sr.addData(0, 0);
sr.addData(1, 1.2);
sr.addData(2, 2.6);
sr.addData(3, 3.2);
sr.addData(4, 4);
sr.addData(5, 5);
NumberFormat format = NumberFormat.getInstance();
// Print the value of y when line intersects the y axis
System.out.println("Intercept: " + format.format(sr.getIntercept()));
// Print the number of data points
System.out.println("N: " + sr.getN());
// Print the Slope and the Slop Confidence
System.out.println("Slope: " + format.format(sr.getSlope()));
System.out.println("Slope Confidence: " + format.format(sr.getSlopeConfidenceInterval()));
// Print RSquare a measure of relatedness
System.out.println("RSquare: " + format.format(sr.getRSquare()));
sr.addData(400, 100);
sr.addData(300, 105);
sr.addData(350, 70);
sr.addData(200, 50);
sr.addData(150, 300);
sr.addData(50, 500);
System.out.println("Intercept: " + format.format(sr.getIntercept()));
System.out.println("N: " + sr.getN());
System.out.println("Slope: " + format.format(sr.getSlope()));
System.out.println("Slope Confidence: " + format.format(sr.getSlopeConfidenceInterval()));
System.out.println("RSquare: " + format.format(sr.getRSquare()));
}Example 5
| Project: presto-master File: TestDoubleRegrInterceptAggregation.java View source code |
private void testNonTrivialAggregation(Double[] y, Double[] x) {
SimpleRegression regression = new SimpleRegression();
for (int i = 0; i < x.length; i++) {
regression.addData(x[i], y[i]);
}
double expected = regression.getIntercept();
checkArgument(Double.isFinite(expected) && expected != 0., "Expected result is trivial");
testAggregation(expected, createDoublesBlock(y), createDoublesBlock(x));
}Example 6
| Project: github-analysis-master File: LinearModelApp.java View source code |
private boolean isPositiveSample(RepoTimeline record) {
SimpleRegression regression = new SimpleRegression();
int[] values = record.getDataPoints();
int count = values.length;
int prevValue = values[count - 2];
for (int i = 0; i < count - 1; i++) {
regression.addData(i, values[i]);
}
int lastValue = (int) regression.predict(count - 1);
return (lastValue - prevValue > 0);
}Example 7
| Project: kairosdb-master File: LeastSquaresAggregator.java View source code |
@Override
public Iterable<DataPoint> getNextDataPoints(long returnTime, Iterator<DataPoint> dataPointRange) {
long start = -1L;
long stop = -1L;
DataPoint first = null;
DataPoint second = null;
int count = 0;
SimpleRegression simpleRegression = new SimpleRegression(true);
while (dataPointRange.hasNext()) {
count++;
DataPoint dp = dataPointRange.next();
if (second == null) {
if (first == null)
first = dp;
else
second = dp;
}
stop = dp.getTimestamp();
if (start == -1L)
start = dp.getTimestamp();
simpleRegression.addData(dp.getTimestamp(), dp.getDoubleValue());
}
List<DataPoint> ret = new ArrayList<DataPoint>();
if (count == 1) {
ret.add(first);
} else if (count == 2) {
ret.add(first);
ret.add(second);
} else if (count != 0) {
ret.add(m_dataPointFactory.createDataPoint(start, simpleRegression.predict(start)));
ret.add(m_dataPointFactory.createDataPoint(stop, simpleRegression.predict(stop)));
}
return (ret);
}Example 8
| Project: ta4j-master File: SimpleLinearRegressionIndicatorTest.java View source code |
@Test
public void calculateLinearRegressionOn4Observations() {
SimpleLinearRegressionIndicator reg = new SimpleLinearRegressionIndicator(closePrice, 4);
assertDecimalEquals(reg.getValue(1), 20);
assertDecimalEquals(reg.getValue(2), 30);
SimpleRegression origReg = buildSimpleRegression(10, 20, 30, 40);
assertDecimalEquals(reg.getValue(3), 40);
assertDecimalEquals(reg.getValue(3), origReg.predict(3));
origReg = buildSimpleRegression(30, 40, 30, 40);
assertDecimalEquals(reg.getValue(5), origReg.predict(3));
origReg = buildSimpleRegression(30, 20, 30, 50);
assertDecimalEquals(reg.getValue(9), origReg.predict(3));
}Example 9
| Project: DynamicSpotter-Extensions-master File: LinearRegression.java View source code |
@Override
public boolean analyseOperationResponseTimes(Dataset dataset, String operation, SpotterResult result) {
ParameterSelection selectOperation = new ParameterSelection().select(ResponseTimeRecord.PAR_OPERATION, operation);
Dataset operationSpecificDataset = selectOperation.applyTo(dataset);
NumericPairList<Integer, Double> responseTimeSeries = Utils.toUserRTPairs(operationSpecificDataset);
SimpleRegression regression = LpeNumericUtils.linearRegression(responseTimeSeries);
double slope = regression.getSlope();
createChart(result, operation, responseTimeSeries, regression);
return slope > slopeThreshold;
}Example 10
| Project: jactr-master File: FitStatistics.java View source code |
private void compute(Object[] values) {
SimpleRegression regression = new SimpleRegression();
double sse = 0;
double chiSquare = 0;
long n = 0;
for (Object val : values) {
double[] value = (double[]) val;
regression.addData(value[0], value[1]);
double squareDiff = Math.pow(value[0] - value[1], 2);
double chiPartial = squareDiff / value[1];
chiSquare += chiPartial;
n++;
sse += squareDiff;
}
/*
* cant use regression.getMeanSquaredError() as that is in comparison to the
* regression line
*/
_rmse = Math.sqrt(sse / n);
_rSquare = regression.getRSquare();
_n = regression.getN();
_chiSquare = chiSquare;
}Example 11
| Project: scisoft-core-master File: SmoothGoldEdgeFunction.java View source code |
@Override
public void setData(IDataset x, IDataset data) {
this.xds = DatasetUtils.convertToDataset(x);
this.yds = DatasetUtils.convertToDataset(data);
try {
// Smooth the data by the real ammount for the smoothed section of the process
this.smoothed = ApachePolynomial.getPolynomialSmoothed(xds, yds, (int) Math.round(getParameterValue(0)), 3);
// Fit a polyline to this to allow for easy interpolation
IDataset arg2 = DatasetUtils.cast(smoothed, Dataset.FLOAT64);
this.polySplineFunction = new LinearInterpolator().interpolate(DatasetUtils.cast(DoubleDataset.class, xds).getData(), ((DoubleDataset) arg2).getData());
lowerFit = new SimpleRegression();
double lowerProp = xds.getShape()[0] * getParameterValue(1);
for (int i = 0; i < lowerProp; i++) {
lowerFit.addData(xds.getDouble(i), yds.getDouble(i));
}
lowerFit.regress();
upperFit = new SimpleRegression();
double upperProp = xds.getShape()[0] * (1.0 - getParameterValue(1));
for (int i = xds.getShape()[0] - 1; i > upperProp; i--) {
upperFit.addData(xds.getDouble(i), yds.getDouble(i));
}
upperFit.regress();
} catch (Exception e) {
e.printStackTrace();
}
}Example 12
| Project: lucene-solr-master File: RegressionEvaluator.java View source code |
public Tuple evaluate(Tuple tuple) throws IOException {
if (subEvaluators.size() != 2) {
throw new IOException("Regress expects 2 columns as parameters");
}
StreamEvaluator colEval1 = subEvaluators.get(0);
StreamEvaluator colEval2 = subEvaluators.get(1);
List<Number> numbers1 = (List<Number>) colEval1.evaluate(tuple);
List<Number> numbers2 = (List<Number>) colEval2.evaluate(tuple);
double[] column1 = new double[numbers1.size()];
double[] column2 = new double[numbers2.size()];
for (int i = 0; i < numbers1.size(); i++) {
column1[i] = numbers1.get(i).doubleValue();
}
for (int i = 0; i < numbers2.size(); i++) {
column2[i] = numbers2.get(i).doubleValue();
}
SimpleRegression regression = new SimpleRegression();
for (int i = 0; i < column1.length; i++) {
regression.addData(column1[i], column2[i]);
}
Map map = new HashMap();
map.put("slope", regression.getSlope());
map.put("intercept", regression.getIntercept());
map.put("R", regression.getR());
map.put("N", regression.getN());
map.put("regressionSumSquares", regression.getRegressionSumSquares());
map.put("slopeConfidenceInterval", regression.getSlopeConfidenceInterval());
map.put("interceptStdErr", regression.getInterceptStdErr());
map.put("totalSumSquares", regression.getTotalSumSquares());
map.put("significance", regression.getSignificance());
map.put("meanSquareError", regression.getMeanSquareError());
return new RegressionTuple(regression, map);
}Example 13
| Project: rascal-master File: SimpleRegressions.java View source code |
SimpleRegression make(IList dataValues) { if (dataValues.length() <= 2) throw RuntimeExceptionFactory.illegalArgument(dataValues, null, null, "SimpleRegression data should have more than 2 elements"); SimpleRegression simple = new SimpleRegression(); for (IValue v : dataValues) { ITuple t = (ITuple) v; INumber x = (INumber) t.get(0); INumber y = (INumber) t.get(1); simple.addData(x.toReal(values.getPrecision()).doubleValue(), y.toReal(values.getPrecision()).doubleValue()); } return simple; }
Example 14
| Project: novelang-master File: TimeMeasurer.java View source code |
/**
* Strain means last request exceeded of more than {@value #GUARD_FACTOR} times the time
* extrapolated from first half of measurements, using linear regression.
*
* @param previousMeasurements a non-null object, contains no null.
* @param lastMeasurement a non-null object.
* @return true if strain detected, false otherwise.
*/
private static boolean detectStrain(final List<TimeMeasurement> previousMeasurements, final TimeMeasurement lastMeasurement) {
final int measurementCount = previousMeasurements.size();
if (measurementCount < MINIMUM_MEASUREMENT_COUNT_FOR_REGRESSION) {
return false;
} else {
final SimpleRegression simpleRegression = new SimpleRegression();
for (int i = 0; i < measurementCount / 2; i++) {
final TimeMeasurement measurement = previousMeasurements.get(i);
simpleRegression.addData((double) i, (double) measurement.getTimeMilliseconds());
}
final double extrapolated = simpleRegression.predict((double) measurementCount);
if (Double.isNaN(extrapolated)) {
return false;
} else {
final double increasing = extrapolated - simpleRegression.getIntercept();
final double highLimit = simpleRegression.getIntercept() + increasing * GUARD_FACTOR;
return lastMeasurement.getTimeMilliseconds() > (long) highLimit;
}
}
}Example 15
| Project: Stock-Analysis-master File: VectorPoint.java View source code |
public static double vectorLength(int type, VectorPoint vp) {
if (vp.getKey() == 0)
return 0;
double change = vp.change();
if (type == 1) {
double pow = 10 * Math.abs(Math.log(change));
// }
return pow;
} else if (type == 2) {
return 10 * (change > 1 ? (change - 1) : (1 / change) - 1);
} else if (type == 3) {
SimpleRegression regression = new SimpleRegression();
for (int i = 0; i < vp.elements; i++) {
regression.addData(i, vp.numbers[i]);
}
return 10 * Math.abs(Math.log(Math.abs(regression.getSlope())));
}
return 0;
}Example 16
| Project: joinery-master File: Display.java View source code |
private static void addTrend(final Chart chart, final Series series, final List<Object> xdata) {
final SimpleRegression model = new SimpleRegression();
final Iterator<? extends Number> y = series.getYData().iterator();
for (int x = 0; y.hasNext(); x++) {
model.addData(x, y.next().doubleValue());
}
final Color mc = series.getMarkerColor();
final Color c = new Color(mc.getRed(), mc.getGreen(), mc.getBlue(), 0x60);
final Series trend = chart.addSeries(series.getName() + " (trend)", Arrays.asList(xdata.get(0), xdata.get(xdata.size() - 1)), Arrays.asList(model.predict(0), model.predict(xdata.size() - 1)));
trend.setLineColor(c);
trend.setMarker(SeriesMarker.NONE);
}Example 17
| Project: minha-master File: Calibrator.java View source code |
public static void main(String[] args) throws Throwable {
if (args.length != 1) {
logger.error("missing command line argument (--server | <servername>)");
} else if (args[0].equals("--server")) {
runServer();
} else {
Calibrator calib = new Calibrator(args[0]);
Properties props = new Properties();
SimpleRegression cpuoh = new SimpleRegression(true);
for (int i : new int[] { 100, 1000, 10000, 20000 }) {
Map<String, Object> p = new HashMap<String, Object>();
p.put("bench", CPUBenchmark.class.getName());
p.put("samples", 50000);
p.put("payload", i);
Result r = calib.runReal(p);
Result s = calib.runSimulated(p, props);
cpuoh.addData(s.meanCPU, r.meanCPU);
}
props.setProperty("cpuScaling", new Linear(cpuoh.getIntercept(), cpuoh.getSlope()).toString());
// Run
double max = Double.MIN_VALUE;
SimpleRegression netCPU = new SimpleRegression(true);
SimpleRegression netCPU_s = new SimpleRegression(true);
for (int i : new int[] { 1, 100, 1000, 4000, 8000, 16000 }) {
Map<String, Object> p = new HashMap<String, Object>();
p.put("bench", TCPOverheadBenchmark.class.getName());
p.put("server", new InetSocketAddress(args[0], 20000));
p.put("samples", 5000);
p.put("payload", i);
Result r = calib.runReal(p);
netCPU.addData(i, r.meanCPU);
Result s = calib.runSimulated(p, props);
netCPU_s.addData(i, s.meanCPU);
double bw = 8 * i * 1e9d / r.meanLatency;
if (bw > max)
max = bw;
}
props.setProperty("networkBandwidth", Long.toString((long) max));
props.setProperty("tcpOverhead", new Linear((netCPU.getIntercept() - netCPU_s.getIntercept()) / 2, (netCPU.getSlope() - netCPU_s.getSlope()) / 2).toString());
SimpleRegression udpCPU = new SimpleRegression(true);
SimpleRegression udpCPU_s = new SimpleRegression(true);
for (int i : new int[] { 1, 100, 1000, 4000, 8000, 16000 }) {
Map<String, Object> p = new HashMap<String, Object>();
p.put("bench", UDPOverheadBenchmark.class.getName());
p.put("server", new InetSocketAddress(args[0], 20000));
p.put("samples", 5000);
p.put("payload", i);
Result r = calib.runReal(p);
udpCPU.addData(i, r.meanCPU);
Result s = calib.runSimulated(p, props);
udpCPU_s.addData(i, s.meanCPU);
}
props.setProperty("udpOverhead", new Linear((udpCPU.getIntercept() - udpCPU_s.getIntercept()) / 2, (udpCPU.getSlope() - udpCPU_s.getSlope()) / 2).toString());
SimpleRegression rtt = new SimpleRegression(true);
SimpleRegression rtt_s = new SimpleRegression(true);
for (int i : new int[] { 1, 100, 1000, 4000, 8000, 16000 }) {
Map<String, Object> p = new HashMap<String, Object>();
p.put("bench", TCPLatencyBenchmark.class.getName());
p.put("server", new InetSocketAddress(args[0], 20000));
p.put("samples", 5000);
p.put("payload", i);
Result r = calib.runReal(p);
rtt.addData(i, r.meanLatency);
Result s = calib.runSimulated(p, props);
rtt_s.addData(i, s.meanLatency);
}
props.setProperty("networkLatency", new Linear((rtt.getIntercept() - rtt_s.getIntercept()) / 2, (rtt.getSlope() - rtt_s.getSlope()) / 2).toString());
calib.close();
for (String key : props.stringPropertyNames()) logger.info("result: {}={}", key, props.getProperty(key));
// Write results
FileOutputStream file = new FileOutputStream("calibration.properties");
props.store(file, "Generated calibration properties");
file.close();
}
}Example 18
| Project: jagger-master File: DefaultWorkloadSuggestionMaker.java View source code |
private static Integer findClosestPoint(BigDecimal desiredTps, Map<Integer, Pair<Long, BigDecimal>> stats) {
final int MAX_POINTS_FOR_REGRESSION = 10;
SortedMap<Long, Integer> map = Maps.newTreeMap(new Comparator<Long>() {
@Override
public int compare(Long first, Long second) {
return second.compareTo(first);
}
});
for (Map.Entry<Integer, Pair<Long, BigDecimal>> entry : stats.entrySet()) {
map.put(entry.getValue().getFirst(), entry.getKey());
}
if (map.size() < 2) {
throw new IllegalArgumentException("Not enough stats to calculate point");
}
// <time><number of threads> - sorted by time
Iterator<Map.Entry<Long, Integer>> iterator = map.entrySet().iterator();
SimpleRegression regression = new SimpleRegression();
Integer tempIndex;
double previousValue = -1.0;
double value;
double measuredTps;
log.debug("Selecting next point for balancing");
int indx = 0;
while (iterator.hasNext()) {
tempIndex = iterator.next().getValue();
if (previousValue < 0.0) {
previousValue = tempIndex.floatValue();
}
value = tempIndex.floatValue();
measuredTps = stats.get(tempIndex).getSecond().floatValue();
regression.addData(value, measuredTps);
log.debug(String.format(" %7.2f %7.2f", value, measuredTps));
indx++;
if (indx > MAX_POINTS_FOR_REGRESSION) {
break;
}
}
double intercept = regression.getIntercept();
double slope = regression.getSlope();
double approxPoint;
// if no slope => use previous number of threads
if (Math.abs(slope) > 1e-12) {
approxPoint = (desiredTps.doubleValue() - intercept) / slope;
} else {
approxPoint = previousValue;
}
// if approximation point is negative - ignore it
if (approxPoint < 0) {
approxPoint = previousValue;
}
log.debug(String.format("Next point %7d (target tps: %7.2f)", (int) Math.round(approxPoint), desiredTps.doubleValue()));
return (int) Math.round(approxPoint);
}Example 19
| Project: NewsStats-master File: Predictor.java View source code |
public int predict(HashMap<String, Integer> series) {
int indexToPredict = series.size();
List keys = new ArrayList(series.keySet());
Collections.sort(keys);
SimpleRegression simpleRegression = new SimpleRegression();
simpleRegression.clear();
int i;
for (i = 0; i < keys.size(); i++) {
simpleRegression.addData(i, series.get(keys.get(i)));
}
double intercept = simpleRegression.getIntercept();
double slope = simpleRegression.getSlope();
System.out.println(intercept);
System.out.println(slope);
double prediction = simpleRegression.predict(indexToPredict);
int output = (int) Math.round(prediction);
return (output > 0) ? output : 0;
}Example 20
| Project: javajoy-master File: PearsonsCorrelation.java View source code |
/**
* Computes the Pearson's product-moment correlation coefficient between the two arrays.
*
* </p>Throws IllegalArgumentException if the arrays do not have the same length
* or their common length is less than 2</p>
*
* @param xArray first data array
* @param yArray second data array
* @return Returns Pearson's correlation coefficient for the two arrays
* @throws IllegalArgumentException if the arrays lengths do not match or
* there is insufficient data
*/
public double correlation(final double[] xArray, final double[] yArray) throws IllegalArgumentException {
SimpleRegression regression = new SimpleRegression();
if (xArray.length != yArray.length) {
throw new DimensionMismatchException(xArray.length, yArray.length);
} else if (xArray.length < 2) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION, xArray.length, 2);
} else {
for (int i = 0; i < xArray.length; i++) {
regression.addData(xArray[i], yArray[i]);
}
return regression.getR();
}
}Example 21
| Project: WorkflowSim-1.0-master File: MathUtil.java View source code |
public static SimpleRegression createWeigthedLinearRegression(final double[] x, final double[] y, final double[] weigths) {
double[] xW = new double[x.length];
double[] yW = new double[y.length];
// As to Flanagan's documentation they perform weigthed regression if the
// number or non-zero weigths is more than 40%
int numZeroWeigths = 0;
for (int i = 0; i < weigths.length; i++) {
if (weigths[i] <= 0) {
numZeroWeigths++;
}
}
for (int i = 0; i < x.length; i++) {
if (numZeroWeigths >= 0.4 * weigths.length) {
// See: http://www.ncsu.edu/crsc/events/ugw07/Presentations/Crooks_Qiao/Crooks_Qiao_Alt_Presentation.pdf
xW[i] = Math.sqrt(weigths[i]) * x[i];
yW[i] = Math.sqrt(weigths[i]) * y[i];
} else {
xW[i] = x[i];
yW[i] = y[i];
}
}
return createLinearRegression(xW, yW);
}Example 22
| Project: GDSC-master File: IntensityAnalysis.java View source code |
/**
* Show a plot of the mean for each slice against exposure. Perform linear fit on a sliding window and draw the best
* fit line on the plot.
*/
private void showResults() {
int valid = 0;
float[] means2 = new float[means.length];
float[] exposures2 = new float[means.length];
for (int i = 0; i < means.length; i++) {
if (means[i] < 0) {
debug("Saturated pixels in slice %d : %s", i + 1, stack.getShortSliceLabel(i + 1));
means[i] = -means[i];
} else {
means2[valid] = means[i];
exposures2[valid] = exposures[i];
valid++;
}
}
means2 = Arrays.copyOf(means2, valid);
exposures2 = Arrays.copyOf(exposures2, valid);
String title = TITLE;
Plot plot = new Plot(title, "Exposure", "Mean");
double[] a = Tools.getMinMax(exposures);
double[] b = Tools.getMinMax(means);
// Add some space to the limits for plotting
double ra = (a[1] - a[0]) * 0.05;
double rb = (b[1] - b[0]) * 0.05;
plot.setLimits(a[0] - ra, a[1] + ra, b[0] - rb, b[1] + rb);
plot.setColor(Color.blue);
plot.addPoints(exposures, means, Plot.CIRCLE);
PlotWindow pw = Utils.display(title, plot);
// Report results to a table
if (results == null || !results.isVisible()) {
results = new TextWindow(TITLE + " Summary", "Image\tx\ty\tw\th\tN\tStart\tEnd\tE1\tE2\tSS\tIntercept\tGradient", "", 800, 300);
results.setVisible(true);
if (Utils.isNewWindow()) {
Point p = results.getLocation();
p.x = pw.getX();
p.y = pw.getY() + pw.getHeight();
results.setLocation(p);
}
}
// Initialise result output
StringBuilder sb = new StringBuilder();
sb.append(imp.getTitle());
sb.append('\t').append(bounds.x);
sb.append('\t').append(bounds.y);
sb.append('\t').append(bounds.width);
sb.append('\t').append(bounds.height);
sb.append('\t').append(n);
if (means2.length < window) {
IJ.error(TITLE, "Not enough unsaturated samples for the fit window: " + means2.length + " < " + window);
addNullFitResult(sb);
} else {
// Do a linear fit using a sliding window. Find the region with the best linear fit.
double bestSS = Double.POSITIVE_INFINITY;
int bestStart = 0;
double[] bestFit = null;
for (int start = 0; start < means2.length; start++) {
int end = start + window;
if (end > means2.length)
break;
// Linear fit
final WeightedObservedPoints obs = new WeightedObservedPoints();
final SimpleRegression r = new SimpleRegression();
// Extract the data
for (int i = start; i < end; i++) {
obs.add(exposures2[i], means2[i]);
r.addData(exposures2[i], means2[i]);
}
if (r.getN() > 0) {
// Do linear regression to get diffusion rate
final double[] init = { // a + b x
r.getIntercept(), // a + b x
r.getSlope() };
final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2).withStartPoint(init);
final double[] fit = fitter.fit(obs.toList());
final PolynomialFunction fitted = new PolynomialFunction(fit);
// Score the fit
double ss = 0;
for (int i = start; i < end; i++) {
final double residual = fitted.value(exposures2[i]) - means2[i];
ss += residual * residual;
}
debug("%d - %d = %f : y = %f + %f x t\n", start, end, ss, fit[0], fit[1]);
// Store best fit
if (ss < bestSS) {
bestSS = ss;
bestStart = start;
bestFit = fit;
}
}
}
if (bestFit == null) {
IJ.error(TITLE, "No valid linear fits");
addNullFitResult(sb);
} else {
plot.setColor(Color.red);
final PolynomialFunction fitted = new PolynomialFunction(bestFit);
double x1 = exposures2[bestStart];
double y1 = fitted.value(x1);
double x2 = exposures2[bestStart + window - 1];
double y2 = fitted.value(x2);
plot.drawLine(x1, y1, x2, y2);
pw = Utils.display(title, plot);
sb.append('\t').append(bestStart + 1);
sb.append('\t').append(bestStart + window);
sb.append('\t').append(Utils.rounded(x1));
sb.append('\t').append(Utils.rounded(x2));
sb.append('\t').append(Utils.rounded(bestSS));
sb.append('\t').append(Utils.rounded(bestFit[0]));
sb.append('\t').append(Utils.rounded(bestFit[1]));
}
}
results.append(sb.toString());
}Example 23
| Project: commons-math-master File: SimpleRegressionTest.java View source code |
/**
* Checks that adding data to a single model gives the same result
* as adding "parts" of the dataset to smaller models and using append
* to aggregate the smaller models.
*
* @param includeIntercept
*/
private void check(boolean includeIntercept) {
final int sets = 2;
// Seed can be changed
final UniformRandomProvider rand = RandomSource.create(RandomSource.ISAAC, 10L);
// regression of the whole set
final SimpleRegression whole = new SimpleRegression(includeIntercept);
// regression with parts.
final SimpleRegression parts = new SimpleRegression(includeIntercept);
for (int s = 0; s < sets; s++) {
// loop through each subset of data.
final double coef = rand.nextDouble();
// sub regression
final SimpleRegression sub = new SimpleRegression(includeIntercept);
for (int i = 0; i < 5; i++) {
// loop through individual samlpes.
final double x = rand.nextDouble();
// some noise
final double y = x * coef + rand.nextDouble();
sub.addData(x, y);
whole.addData(x, y);
}
parts.append(sub);
Assert.assertTrue(equals(parts, whole, 1E-6));
}
}Example 24
| Project: PerfCake-master File: Utils.java View source code |
/**
* Computes a linear regression trend of the data set provided.
*
* @param data
* Data on which to compute the trend.
* @return The linear regression trend.
*/
public static double computeRegressionTrend(final Collection<TimestampedRecord<Number>> data) {
final SimpleRegression simpleRegression = new SimpleRegression();
final Iterator<TimestampedRecord<Number>> iterator = data.iterator();
TimestampedRecord<Number> currentRecord;
while (iterator.hasNext()) {
currentRecord = iterator.next();
simpleRegression.addData(currentRecord.getTimestamp(), currentRecord.getValue().doubleValue());
}
return simpleRegression.getSlope();
}Example 25
| Project: GDSC-SMLM-master File: BenchmarkSpotFit.java View source code |
private void summariseResults(TIntObjectHashMap<FilterCandidates> filterCandidates, long runTime, final PreprocessedPeakResult[] preprocessedPeakResults, int nUniqueIDs) {
createTable();
// Summarise the fitting results. N fits, N failures.
// Optimal match statistics if filtering is perfect (since fitting is not perfect).
StoredDataStatistics distanceStats = new StoredDataStatistics();
StoredDataStatistics depthStats = new StoredDataStatistics();
// Get stats for all fitted results and those that match
// Signal, SNR, Width, xShift, yShift, Precision
createFilterCriteria();
StoredDataStatistics[][] stats = new StoredDataStatistics[3][filterCriteria.length];
for (int i = 0; i < stats.length; i++) for (int j = 0; j < stats[i].length; j++) stats[i][j] = new StoredDataStatistics();
final double nmPerPixel = simulationParameters.a;
double tp = 0, fp = 0;
int failcTP = 0, failcFP = 0;
int cTP = 0, cFP = 0;
int[] singleStatus = null, multiStatus = null, doubletStatus = null, multiDoubletStatus = null;
singleStatus = new int[FitStatus.values().length];
multiStatus = new int[singleStatus.length];
doubletStatus = new int[singleStatus.length];
multiDoubletStatus = new int[singleStatus.length];
// Easier to materialise the values since we have a lot of non final variables to manipulate
final int[] frames = new int[filterCandidates.size()];
final FilterCandidates[] candidates = new FilterCandidates[filterCandidates.size()];
final int[] counter = new int[1];
filterCandidates.forEachEntry(new TIntObjectProcedure<FilterCandidates>() {
public boolean execute(int a, FilterCandidates b) {
frames[counter[0]] = a;
candidates[counter[0]] = b;
counter[0]++;
return true;
}
});
for (FilterCandidates result : candidates) {
// Count the number of fit results that matched (tp) and did not match (fp)
tp += result.tp;
fp += result.fp;
for (int i = 0; i < result.fitResult.length; i++) {
if (result.spots[i].match)
cTP++;
else
cFP++;
final MultiPathFitResult fitResult = result.fitResult[i];
if (singleStatus != null && result.spots[i].match) {
// Debugging reasons for fit failure
addStatus(singleStatus, fitResult.getSingleFitResult());
addStatus(multiStatus, fitResult.getMultiFitResult());
addStatus(doubletStatus, fitResult.getDoubletFitResult());
addStatus(multiDoubletStatus, fitResult.getMultiDoubletFitResult());
}
if (noMatch(fitResult)) {
if (result.spots[i].match)
failcTP++;
else
failcFP++;
}
// We have multi-path results.
// We want statistics for:
// [0] all fitted spots
// [1] fitted spots that match a result
// [2] fitted spots that do not match a result
addToStats(fitResult.getSingleFitResult(), stats);
addToStats(fitResult.getMultiFitResult(), stats);
addToStats(fitResult.getDoubletFitResult(), stats);
addToStats(fitResult.getMultiDoubletFitResult(), stats);
}
// Statistics on spots that fit an actual result
for (int i = 0; i < result.match.length; i++) {
if (!result.match[i].isFitResult())
// For now just ignore the candidates that matched
continue;
FitMatch fitMatch = (FitMatch) result.match[i];
distanceStats.add(fitMatch.d * nmPerPixel);
depthStats.add(fitMatch.z * nmPerPixel);
}
}
// Store data for computing correlation
double[] i1 = new double[depthStats.getN()];
double[] i2 = new double[i1.length];
double[] is = new double[i1.length];
int ci = 0;
for (FilterCandidates result : candidates) {
for (int i = 0; i < result.match.length; i++) {
if (!result.match[i].isFitResult())
// For now just ignore the candidates that matched
continue;
FitMatch fitMatch = (FitMatch) result.match[i];
ScoredSpot spot = result.spots[fitMatch.i];
i1[ci] = fitMatch.predictedSignal;
i2[ci] = fitMatch.actualSignal;
is[ci] = spot.spot.intensity;
ci++;
}
}
// We want to compute the Jaccard against the spot metric
// Filter the results using the multi-path filter
ArrayList<MultiPathFitResults> multiPathResults = new ArrayList<MultiPathFitResults>(filterCandidates.size());
for (int i = 0; i < frames.length; i++) {
int frame = frames[i];
MultiPathFitResult[] multiPathFitResults = candidates[i].fitResult;
int totalCandidates = candidates[i].spots.length;
int nActual = actualCoordinates.get(frame).size();
multiPathResults.add(new MultiPathFitResults(frame, multiPathFitResults, totalCandidates, nActual));
}
// Score the results and count the number returned
List<FractionalAssignment[]> assignments = new ArrayList<FractionalAssignment[]>();
final TIntHashSet set = new TIntHashSet(nUniqueIDs);
FractionScoreStore scoreStore = new FractionScoreStore() {
public void add(int uniqueId) {
set.add(uniqueId);
}
};
MultiPathFitResults[] multiResults = multiPathResults.toArray(new MultiPathFitResults[multiPathResults.size()]);
// Filter with no filter
MultiPathFilter mpf = new MultiPathFilter(new SignalFilter(0), null, multiFilter.residualsThreshold);
FractionClassificationResult fractionResult = mpf.fractionScoreSubset(multiResults, Integer.MAX_VALUE, this.results.size(), assignments, scoreStore, CoordinateStoreFactory.create(imp.getWidth(), imp.getHeight(), fitConfig.getDuplicateDistance()));
double nPredicted = fractionResult.getTP() + fractionResult.getFP();
final double[][] matchScores = new double[set.size()][];
int count = 0;
for (int i = 0; i < assignments.size(); i++) {
FractionalAssignment[] a = assignments.get(i);
if (a == null)
continue;
for (int j = 0; j < a.length; j++) {
final PreprocessedPeakResult r = ((PeakFractionalAssignment) a[j]).peakResult;
set.remove(r.getUniqueId());
final double precision = Math.sqrt(r.getLocationVariance());
final double signal = r.getSignal();
final double snr = r.getSNR();
final double width = r.getXSDFactor();
final double xShift = r.getXRelativeShift2();
final double yShift = r.getYRelativeShift2();
// Since these two are combined for filtering and the max is what matters.
final double shift = (xShift > yShift) ? Math.sqrt(xShift) : Math.sqrt(yShift);
final double eshift = Math.sqrt(xShift + yShift);
final double[] score = new double[8];
score[FILTER_SIGNAL] = signal;
score[FILTER_SNR] = snr;
score[FILTER_MIN_WIDTH] = width;
score[FILTER_MAX_WIDTH] = width;
score[FILTER_SHIFT] = shift;
score[FILTER_ESHIFT] = eshift;
score[FILTER_PRECISION] = precision;
score[FILTER_PRECISION + 1] = a[j].getScore();
matchScores[count++] = score;
}
}
// Add the rest
set.forEach(new CustomTIntProcedure(count) {
public boolean execute(int uniqueId) {
// This should not be null or something has gone wrong
PreprocessedPeakResult r = preprocessedPeakResults[uniqueId];
if (r == null)
throw new RuntimeException("Missing result: " + uniqueId);
final double precision = Math.sqrt(r.getLocationVariance());
final double signal = r.getSignal();
final double snr = r.getSNR();
final double width = r.getXSDFactor();
final double xShift = r.getXRelativeShift2();
final double yShift = r.getYRelativeShift2();
// Since these two are combined for filtering and the max is what matters.
final double shift = (xShift > yShift) ? Math.sqrt(xShift) : Math.sqrt(yShift);
final double eshift = Math.sqrt(xShift + yShift);
final double[] score = new double[8];
score[FILTER_SIGNAL] = signal;
score[FILTER_SNR] = snr;
score[FILTER_MIN_WIDTH] = width;
score[FILTER_MAX_WIDTH] = width;
score[FILTER_SHIFT] = shift;
score[FILTER_ESHIFT] = eshift;
score[FILTER_PRECISION] = precision;
matchScores[c++] = score;
return true;
}
});
// Debug the reasons the fit failed
if (singleStatus != null) {
String name = PeakFit.getSolverName(fitConfig);
if (fitConfig.getFitSolver() == FitSolver.MLE && fitConfig.isModelCamera())
name += " Camera";
System.out.println("Failure counts: " + name);
printFailures("Single", singleStatus);
printFailures("Multi", multiStatus);
printFailures("Doublet", doubletStatus);
printFailures("Multi doublet", multiDoubletStatus);
}
StringBuilder sb = new StringBuilder(300);
// Add information about the simulation
//(simulationParameters.minSignal + simulationParameters.maxSignal) * 0.5;
final double signal = simulationParameters.signalPerFrame;
final int n = results.size();
sb.append(imp.getStackSize()).append("\t");
final int w = imp.getWidth();
final int h = imp.getHeight();
sb.append(w).append("\t");
sb.append(h).append("\t");
sb.append(n).append("\t");
double density = ((double) n / imp.getStackSize()) / (w * h) / (simulationParameters.a * simulationParameters.a / 1e6);
sb.append(Utils.rounded(density)).append("\t");
sb.append(Utils.rounded(signal)).append("\t");
sb.append(Utils.rounded(simulationParameters.s)).append("\t");
sb.append(Utils.rounded(simulationParameters.a)).append("\t");
sb.append(Utils.rounded(simulationParameters.depth)).append("\t");
sb.append(simulationParameters.fixedDepth).append("\t");
sb.append(Utils.rounded(simulationParameters.gain)).append("\t");
sb.append(Utils.rounded(simulationParameters.readNoise)).append("\t");
sb.append(Utils.rounded(simulationParameters.b)).append("\t");
sb.append(Utils.rounded(simulationParameters.b2)).append("\t");
// Compute the noise
double noise = simulationParameters.b2;
if (simulationParameters.emCCD) {
// The b2 parameter was computed without application of the EM-CCD noise factor of 2.
//final double b2 = backgroundVariance + readVariance
// = simulationParameters.b + readVariance
// This should be applied only to the background variance.
final double readVariance = noise - simulationParameters.b;
noise = simulationParameters.b * 2 + readVariance;
}
if (simulationParameters.fullSimulation) {
// The total signal is spread over frames
}
sb.append(Utils.rounded(signal / Math.sqrt(noise))).append("\t");
sb.append(Utils.rounded(simulationParameters.s / simulationParameters.a)).append("\t");
sb.append(spotFilter.getDescription());
// nP and nN is the fractional score of the spot candidates
addCount(sb, nP + nN);
addCount(sb, nP);
addCount(sb, nN);
addCount(sb, fP);
addCount(sb, fN);
String name = PeakFit.getSolverName(fitConfig);
if (fitConfig.getFitSolver() == FitSolver.MLE && fitConfig.isModelCamera())
name += " Camera";
add(sb, name);
add(sb, config.getFitting());
resultPrefix = sb.toString();
// Q. Should I add other fit configuration here?
// The fraction of positive and negative candidates that were included
add(sb, (100.0 * cTP) / nP);
add(sb, (100.0 * cFP) / nN);
// Score the fitting results compared to the original simulation.
// Score the candidate selection:
add(sb, cTP + cFP);
add(sb, cTP);
add(sb, cFP);
// TP are all candidates that can be matched to a spot
// FP are all candidates that cannot be matched to a spot
// FN = The number of missed spots
FractionClassificationResult m = new FractionClassificationResult(cTP, cFP, 0, simulationParameters.molecules - cTP);
add(sb, m.getRecall());
add(sb, m.getPrecision());
add(sb, m.getF1Score());
add(sb, m.getJaccard());
// Score the fitting results:
add(sb, failcTP);
add(sb, failcFP);
// TP are all fit results that can be matched to a spot
// FP are all fit results that cannot be matched to a spot
// FN = The number of missed spots
add(sb, tp);
add(sb, fp);
m = new FractionClassificationResult(tp, fp, 0, simulationParameters.molecules - tp);
add(sb, m.getRecall());
add(sb, m.getPrecision());
add(sb, m.getF1Score());
add(sb, m.getJaccard());
// Do it again but pretend we can perfectly filter all the false positives
//add(sb, tp);
m = new FractionClassificationResult(tp, 0, 0, simulationParameters.molecules - tp);
// Recall is unchanged
// Precision will be 100%
add(sb, m.getF1Score());
add(sb, m.getJaccard());
// The mean may be subject to extreme outliers so use the median
double median = distanceStats.getMedian();
add(sb, median);
WindowOrganiser wo = new WindowOrganiser();
String label = String.format("Recall = %s. n = %d. Median = %s nm. SD = %s nm", Utils.rounded(m.getRecall()), distanceStats.getN(), Utils.rounded(median), Utils.rounded(distanceStats.getStandardDeviation()));
int id = Utils.showHistogram(TITLE, distanceStats, "Match Distance (nm)", 0, 0, 0, label);
if (Utils.isNewWindow())
wo.add(id);
median = depthStats.getMedian();
add(sb, median);
// Sort by spot intensity and produce correlation
int[] indices = Utils.newArray(i1.length, 0, 1);
if (showCorrelation)
Sort.sort(indices, is, rankByIntensity);
double[] r = (showCorrelation) ? new double[i1.length] : null;
double[] sr = (showCorrelation) ? new double[i1.length] : null;
double[] rank = (showCorrelation) ? new double[i1.length] : null;
ci = 0;
FastCorrelator fastCorrelator = new FastCorrelator();
ArrayList<Ranking> pc1 = new ArrayList<Ranking>();
ArrayList<Ranking> pc2 = new ArrayList<Ranking>();
for (int ci2 : indices) {
fastCorrelator.add((long) Math.round(i1[ci2]), (long) Math.round(i2[ci2]));
pc1.add(new Ranking(i1[ci2], ci));
pc2.add(new Ranking(i2[ci2], ci));
if (showCorrelation) {
r[ci] = fastCorrelator.getCorrelation();
sr[ci] = Correlator.correlation(rank(pc1), rank(pc2));
if (rankByIntensity)
rank[ci] = is[0] - is[ci];
else
rank[ci] = ci;
}
ci++;
}
final double pearsonCorr = fastCorrelator.getCorrelation();
final double rankedCorr = Correlator.correlation(rank(pc1), rank(pc2));
// Get the regression
SimpleRegression regression = new SimpleRegression(false);
for (int i = 0; i < pc1.size(); i++) regression.addData(pc1.get(i).value, pc2.get(i).value);
//final double intercept = regression.getIntercept();
final double slope = regression.getSlope();
if (showCorrelation) {
String title = TITLE + " Intensity";
Plot plot = new Plot(title, "Candidate", "Spot");
double[] limits1 = Maths.limits(i1);
double[] limits2 = Maths.limits(i2);
plot.setLimits(limits1[0], limits1[1], limits2[0], limits2[1]);
label = String.format("Correlation=%s; Ranked=%s; Slope=%s", Utils.rounded(pearsonCorr), Utils.rounded(rankedCorr), Utils.rounded(slope));
plot.addLabel(0, 0, label);
plot.setColor(Color.red);
plot.addPoints(i1, i2, Plot.DOT);
if (slope > 1)
plot.drawLine(limits1[0], limits1[0] * slope, limits1[1], limits1[1] * slope);
else
plot.drawLine(limits2[0] / slope, limits2[0], limits2[1] / slope, limits2[1]);
PlotWindow pw = Utils.display(title, plot);
if (Utils.isNewWindow())
wo.add(pw);
title = TITLE + " Correlation";
plot = new Plot(title, "Spot Rank", "Correlation");
double[] xlimits = Maths.limits(rank);
double[] ylimits = Maths.limits(r);
ylimits = Maths.limits(ylimits, sr);
plot.setLimits(xlimits[0], xlimits[1], ylimits[0], ylimits[1]);
plot.setColor(Color.red);
plot.addPoints(rank, r, Plot.LINE);
plot.setColor(Color.blue);
plot.addPoints(rank, sr, Plot.LINE);
plot.setColor(Color.black);
plot.addLabel(0, 0, label);
pw = Utils.display(title, plot);
if (Utils.isNewWindow())
wo.add(pw);
}
add(sb, pearsonCorr);
add(sb, rankedCorr);
add(sb, slope);
label = String.format("n = %d. Median = %s nm", depthStats.getN(), Utils.rounded(median));
id = Utils.showHistogram(TITLE, depthStats, "Match Depth (nm)", 0, 1, 0, label);
if (Utils.isNewWindow())
wo.add(id);
// Plot histograms of the stats on the same window
double[] lower = new double[filterCriteria.length];
double[] upper = new double[lower.length];
min = new double[lower.length];
max = new double[lower.length];
for (int i = 0; i < stats[0].length; i++) {
double[] limits = showDoubleHistogram(stats, i, wo, matchScores, nPredicted);
lower[i] = limits[0];
upper[i] = limits[1];
min[i] = limits[2];
max[i] = limits[3];
}
// Reconfigure some of the range limits
// Make this a bit bigger
upper[FILTER_SIGNAL] *= 2;
// Make this a bit bigger
upper[FILTER_SNR] *= 2;
double factor = 0.25;
if (lower[FILTER_MIN_WIDTH] != 0)
// (assuming lower is less than 1)
upper[FILTER_MIN_WIDTH] = 1 - Math.max(0, factor * (1 - lower[FILTER_MIN_WIDTH]));
if (upper[FILTER_MIN_WIDTH] != 0)
// (assuming upper is more than 1)
lower[FILTER_MAX_WIDTH] = 1 + Math.max(0, factor * (upper[FILTER_MAX_WIDTH] - 1));
// Round the ranges
final double[] interval = new double[stats[0].length];
interval[FILTER_SIGNAL] = SignalFilter.DEFAULT_INCREMENT;
interval[FILTER_SNR] = SNRFilter.DEFAULT_INCREMENT;
interval[FILTER_MIN_WIDTH] = WidthFilter2.DEFAULT_MIN_INCREMENT;
interval[FILTER_MAX_WIDTH] = WidthFilter.DEFAULT_INCREMENT;
interval[FILTER_SHIFT] = ShiftFilter.DEFAULT_INCREMENT;
interval[FILTER_ESHIFT] = EShiftFilter.DEFAULT_INCREMENT;
interval[FILTER_PRECISION] = PrecisionFilter.DEFAULT_INCREMENT;
interval[FILTER_ITERATIONS] = 0.1;
interval[FILTER_EVALUATIONS] = 0.1;
// Create a range increment
double[] increment = new double[lower.length];
for (int i = 0; i < increment.length; i++) {
lower[i] = Maths.floor(lower[i], interval[i]);
upper[i] = Maths.ceil(upper[i], interval[i]);
double range = upper[i] - lower[i];
// Allow clipping if the range is small compared to the min increment
double multiples = range / interval[i];
// Use 8 multiples for the equivalent of +/- 4 steps around the centre
if (multiples < 8) {
multiples = Math.ceil(multiples);
} else
multiples = 8;
increment[i] = Maths.ceil(range / multiples, interval[i]);
if (i == FILTER_MIN_WIDTH)
// Requires clipping based on the upper limit
lower[i] = upper[i] - increment[i] * multiples;
else
upper[i] = lower[i] + increment[i] * multiples;
}
for (int i = 0; i < stats[0].length; i++) {
lower[i] = Maths.round(lower[i]);
upper[i] = Maths.round(upper[i]);
min[i] = Maths.round(min[i]);
max[i] = Maths.round(max[i]);
increment[i] = Maths.round(increment[i]);
sb.append("\t").append(min[i]).append(':').append(lower[i]).append('-').append(upper[i]).append(':').append(max[i]);
}
// Disable some filters
increment[FILTER_SIGNAL] = Double.POSITIVE_INFINITY;
//increment[FILTER_SHIFT] = Double.POSITIVE_INFINITY;
increment[FILTER_ESHIFT] = Double.POSITIVE_INFINITY;
wo.tile();
sb.append("\t").append(Utils.timeToString(runTime / 1000000.0));
summaryTable.append(sb.toString());
if (saveFilterRange) {
GlobalSettings gs = SettingsManager.loadSettings();
FilterSettings filterSettings = gs.getFilterSettings();
String filename = (silent) ? filterSettings.filterSetFilename : Utils.getFilename("Filter_range_file", filterSettings.filterSetFilename);
if (filename == null)
return;
// Remove extension to store the filename
filename = Utils.replaceExtension(filename, ".xml");
filterSettings.filterSetFilename = filename;
// Create a filter set using the ranges
ArrayList<Filter> filters = new ArrayList<Filter>(3);
filters.add(new MultiFilter2(lower[0], (float) lower[1], lower[2], lower[3], lower[4], lower[5], lower[6]));
filters.add(new MultiFilter2(upper[0], (float) upper[1], upper[2], upper[3], upper[4], upper[5], upper[6]));
filters.add(new MultiFilter2(increment[0], (float) increment[1], increment[2], increment[3], increment[4], increment[5], increment[6]));
if (saveFilters(filename, filters))
SettingsManager.saveSettings(gs);
// Create a filter set using the min/max and the initial bounds.
// Set sensible limits
min[FILTER_SIGNAL] = Math.max(min[FILTER_SIGNAL], 30);
max[FILTER_PRECISION] = Math.min(max[FILTER_PRECISION], 100);
// Commented this out so that the 4-set filters are the same as the 3-set filters.
// The difference leads to differences when optimising.
// // Use half the initial bounds (hoping this is a good starting guess for the optimum)
// final boolean[] limitToLower = new boolean[min.length];
// limitToLower[FILTER_SIGNAL] = true;
// limitToLower[FILTER_SNR] = true;
// limitToLower[FILTER_MIN_WIDTH] = true;
// limitToLower[FILTER_MAX_WIDTH] = false;
// limitToLower[FILTER_SHIFT] = false;
// limitToLower[FILTER_ESHIFT] = false;
// limitToLower[FILTER_PRECISION] = true;
// for (int i = 0; i < limitToLower.length; i++)
// {
// final double range = (upper[i] - lower[i]) / 2;
// if (limitToLower[i])
// upper[i] = lower[i] + range;
// else
// lower[i] = upper[i] - range;
// }
filters = new ArrayList<Filter>(4);
filters.add(new MultiFilter2(min[0], (float) min[1], min[2], min[3], min[4], min[5], min[6]));
filters.add(new MultiFilter2(lower[0], (float) lower[1], lower[2], lower[3], lower[4], lower[5], lower[6]));
filters.add(new MultiFilter2(upper[0], (float) upper[1], upper[2], upper[3], upper[4], upper[5], upper[6]));
filters.add(new MultiFilter2(max[0], (float) max[1], max[2], max[3], max[4], max[5], max[6]));
saveFilters(Utils.replaceExtension(filename, ".4.xml"), filters);
}
}Example 26
| Project: act-master File: SurfactantAnalysis.java View source code |
/**
* Perform linear regression over atoms' projection onto `lv` using their logP contributions as y-axis values.
*
* @return The slope of the regression line computed over the `lv`-projection.
*/
public Double performRegressionOverLVProjectionOfLogP() {
SimpleRegression regression = new SimpleRegression();
for (int i = 0; i < mol.getAtomCount(); i++) {
Double x = distancesAlongLongestVector.get(i);
Double y = plugin.getAtomlogPIncrement(i);
regression.addData(x, y);
}
regression.regress();
return regression.getSlope();
}Example 27
| Project: compomics-utilities-master File: XYPlottingDialog.java View source code |
@Override
public void run() {
plotPanel.removeAll();
// setup the dataset
String xAxisName = (String) xAxisComboBox.getSelectedItem();
String yAxisName = (String) yAxisComboBox.getSelectedItem();
double maxBubbleSize = 0.0;
double xAxisRange = 1.0;
// apply the data filters
filterData();
boolean selfUpdating = true;
if (tableModel instanceof SelfUpdatingTableModel) {
SelfUpdatingTableModel selfUpdatingTableModel = (SelfUpdatingTableModel) tableModel;
selfUpdating = selfUpdatingTableModel.isSelfUpdating();
selfUpdatingTableModel.setSelfUpdating(false);
}
if (histogramRadioButton.isSelected() || densityPlotRadioButton.isSelected()) {
((TitledBorder) xyPlotPanel.getBorder()).setTitle(xAxisName);
xyPlotPanel.revalidate();
xyPlotPanel.repaint();
progressDialog.setPrimaryProgressCounterIndeterminate(false);
progressDialog.setMaxPrimaryProgressCounter(tableModel.getRowCount());
progressDialog.setValue(0);
int xAxisIndex = xAxisComboBox.getSelectedIndex();
double[] values = new double[tableModel.getRowCount()];
for (int index = 0; index < tableModel.getRowCount(); index++) {
progressDialog.increasePrimaryProgressCounter();
if (rowsAfterDataFiltering.contains(index)) {
// @TODO: support more data types!!
if (tableModel.getValueAt(index, xAxisIndex) instanceof XYDataPoint) {
values[index] = ((XYDataPoint) tableModel.getValueAt(index, xAxisIndex)).getX();
} else if (tableModel.getValueAt(index, xAxisIndex) instanceof Integer) {
values[index] = ((Integer) tableModel.getValueAt(index, xAxisIndex)).doubleValue();
} else if (tableModel.getValueAt(index, xAxisIndex) instanceof Double) {
values[index] = ((Double) tableModel.getValueAt(index, xAxisIndex)).doubleValue();
} else if (tableModel.getValueAt(index, xAxisIndex) instanceof StartIndexes) {
if (((StartIndexes) tableModel.getValueAt(index, xAxisIndex)).getIndexes().size() > 0) {
values[index] = ((StartIndexes) tableModel.getValueAt(index, xAxisIndex)).getIndexes().get(0);
}
} else if (tableModel.getValueAt(index, xAxisIndex) instanceof ArrrayListDataPoints) {
ArrrayListDataPoints arrrayListDataPoints = (ArrrayListDataPoints) tableModel.getValueAt(index, xAxisIndex);
if (!arrrayListDataPoints.getData().isEmpty()) {
if (arrrayListDataPoints.getDataSortingType() == JSparklinesArrayListBarChartTableCellRenderer.ValueDisplayType.sumOfNumbers) {
values[index] = arrrayListDataPoints.getSum();
} else if (arrrayListDataPoints.getDataSortingType() == JSparklinesArrayListBarChartTableCellRenderer.ValueDisplayType.sumExceptLastNumber) {
values[index] = arrrayListDataPoints.getSumExceptLast();
} else if (arrrayListDataPoints.getDataSortingType() == JSparklinesArrayListBarChartTableCellRenderer.ValueDisplayType.firstNumberOnly) {
values[index] = arrrayListDataPoints.getData().get(0);
}
}
}
}
}
XYPlot plot;
JFreeChart chart;
if (densityPlotRadioButton.isSelected()) {
NormalKernelDensityEstimator kernelEstimator = new NormalKernelDensityEstimator();
ArrayList list = kernelEstimator.estimateDensityFunction(values);
XYSeriesCollection lineChartDataset = new XYSeriesCollection();
XYSeries tempSeries = new XYSeries("1");
double[] xValues = (double[]) list.get(0);
double[] yValues = (double[]) list.get(1);
for (int i = 0; i < xValues.length; i++) {
tempSeries.add(xValues[i], yValues[i]);
}
lineChartDataset.addSeries(tempSeries);
AreaRenderer renderer = new AreaRenderer();
renderer.setOutline(true);
renderer.setSeriesFillPaint(0, histogramColor);
renderer.setSeriesOutlinePaint(0, histogramColor.darker());
chart = ChartFactory.createXYLineChart(null, xAxisName, "Density", lineChartDataset, PlotOrientation.VERTICAL, false, true, false);
plot = chart.getXYPlot();
plot.setRenderer(renderer);
} else {
// histogram
HistogramDataset dataset = new HistogramDataset();
dataset.setType(HistogramType.FREQUENCY);
if (!userDefinedBinSize) {
numberOfBins = getNumberOfBins(values);
binSizeSpinner.setValue(numberOfBins);
}
userDefinedBinSize = false;
dataset.addSeries(xAxisName, values, numberOfBins);
chart = ChartFactory.createHistogram(null, xAxisName, "Frequency", dataset, PlotOrientation.VERTICAL, false, true, false);
plot = chart.getXYPlot();
// set up the chart renderer
XYBarRenderer renderer = new XYBarRenderer();
renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
renderer.setShadowVisible(false);
renderer.setSeriesPaint(0, histogramColor);
plot.setRenderer(renderer);
}
// linear or logarithmic axis
if (xAxisLogCheckBox.isSelected()) {
plot.setDomainAxis(new LogAxis(plot.getDomainAxis().getLabel()));
}
if (yAxisLogCheckBox.isSelected()) {
plot.setRangeAxis(new LogAxis(plot.getRangeAxis().getLabel()));
}
chartPanel = new ChartPanel(chart);
chartPanel.setBorder(null);
chart.setBorderVisible(false);
// hide unwanted chart details
plot.setOutlineVisible(false);
plot.setBackgroundPaint(Color.WHITE);
chartPanel.setBackground(Color.WHITE);
chart.setBackgroundPaint(Color.WHITE);
plotPanel.add(chartPanel);
plotPanel.revalidate();
plotPanel.repaint();
} else {
// xy plot
((TitledBorder) xyPlotPanel.getBorder()).setTitle(xAxisName + " vs. " + yAxisName);
xyPlotPanel.revalidate();
xyPlotPanel.repaint();
DefaultXYZDataset xyzDataset = new DefaultXYZDataset();
ArrayList<String> datasetNames = new ArrayList<String>();
HashMap<String, ArrayList<Integer>> datasets = new HashMap<String, ArrayList<Integer>>();
int colorIndex = colorsComboBox.getSelectedIndex();
double minColorValue = Double.MAX_VALUE;
double maxColorValue = Double.MIN_VALUE;
progressDialog.setPrimaryProgressCounterIndeterminate(false);
progressDialog.setMaxPrimaryProgressCounter(tableModel.getRowCount() * 2);
progressDialog.setValue(0);
for (int i = 0; i < tableModel.getRowCount(); i++) {
progressDialog.increasePrimaryProgressCounter();
if (rowsAfterDataFiltering.contains(i)) {
ArrayList<Integer> tempArray;
if (!datasets.containsKey(tableModel.getValueAt(i, colorIndex).toString())) {
tempArray = new ArrayList<Integer>();
datasetNames.add(tableModel.getValueAt(i, colorIndex).toString());
} else {
tempArray = datasets.get(tableModel.getValueAt(i, colorIndex).toString());
}
tempArray.add(i);
datasets.put(tableModel.getValueAt(i, colorIndex).toString(), tempArray);
// get the min and max values for the color column
if (tableModel.getValueAt(i, colorIndex) instanceof XYDataPoint) {
double tempColorValue = ((XYDataPoint) tableModel.getValueAt(i, colorIndex)).getX();
if (tempColorValue > maxColorValue) {
maxColorValue = tempColorValue;
}
if (tempColorValue < minColorValue) {
minColorValue = tempColorValue;
}
} else if (tableModel.getValueAt(i, colorIndex) instanceof Integer) {
int tempColorValue = (Integer) tableModel.getValueAt(i, colorIndex);
if (tempColorValue > maxColorValue) {
maxColorValue = tempColorValue;
}
if (tempColorValue < minColorValue) {
minColorValue = tempColorValue;
}
} else if (tableModel.getValueAt(i, colorIndex) instanceof Double) {
double tempColorValue = (Double) tableModel.getValueAt(i, colorIndex);
if (tempColorValue > maxColorValue) {
maxColorValue = tempColorValue;
}
if (tempColorValue < minColorValue) {
minColorValue = tempColorValue;
}
} else if (tableModel.getValueAt(i, colorIndex) instanceof StartIndexes) {
if (((StartIndexes) tableModel.getValueAt(i, colorIndex)).getIndexes().size() > 0) {
double tempColorValue = ((StartIndexes) tableModel.getValueAt(i, colorIndex)).getIndexes().get(0);
if (tempColorValue > maxColorValue) {
maxColorValue = tempColorValue;
}
if (tempColorValue < minColorValue) {
minColorValue = tempColorValue;
}
}
} else if (tableModel.getValueAt(i, colorIndex) instanceof ArrrayListDataPoints) {
ArrrayListDataPoints arrrayListDataPoints = (ArrrayListDataPoints) tableModel.getValueAt(i, colorIndex);
if (!arrrayListDataPoints.getData().isEmpty()) {
if (arrrayListDataPoints.getDataSortingType() == JSparklinesArrayListBarChartTableCellRenderer.ValueDisplayType.sumOfNumbers) {
double tempColorValue = arrrayListDataPoints.getSum();
if (tempColorValue > maxColorValue) {
maxColorValue = tempColorValue;
}
if (tempColorValue < minColorValue) {
minColorValue = tempColorValue;
}
} else if (arrrayListDataPoints.getDataSortingType() == JSparklinesArrayListBarChartTableCellRenderer.ValueDisplayType.sumExceptLastNumber) {
double tempColorValue = arrrayListDataPoints.getSumExceptLast();
if (tempColorValue > maxColorValue) {
maxColorValue = tempColorValue;
}
if (tempColorValue < minColorValue) {
minColorValue = tempColorValue;
}
} else if (arrrayListDataPoints.getDataSortingType() == JSparklinesArrayListBarChartTableCellRenderer.ValueDisplayType.firstNumberOnly) {
double tempColorValue = arrrayListDataPoints.getData().get(0);
if (tempColorValue > maxColorValue) {
maxColorValue = tempColorValue;
}
if (tempColorValue < minColorValue) {
minColorValue = tempColorValue;
}
}
}
}
}
}
int xAxisIndex = xAxisComboBox.getSelectedIndex();
int yAxisIndex = yAxisComboBox.getSelectedIndex();
int bubbleSizeIndex = bubbleSizeComboBox.getSelectedIndex();
HashMap<Integer, Color> datasetColors = new HashMap<Integer, Color>();
progressDialog.setPrimaryProgressCounterIndeterminate(false);
progressDialog.setMaxPrimaryProgressCounter(tableModel.getRowCount());
progressDialog.setValue(0);
int datasetCounter = 0;
double minXValue = Double.MAX_VALUE;
double maxXValue = Double.MIN_VALUE;
double sx = 0, sy = 0, sxx = 0, sxy = 0, syy = 0;
SimpleRegression simpleRegression = new SimpleRegression();
// split the data into the datasets
for (String dataset : datasetNames) {
double[][] tempDataXYZ = new double[3][datasets.get(dataset).size()];
int counter = 0;
for (Integer index : datasets.get(dataset)) {
progressDialog.increasePrimaryProgressCounter();
// @TODO: support more data types!!
if (tableModel.getValueAt(index, xAxisIndex) instanceof XYDataPoint) {
tempDataXYZ[0][counter] = ((XYDataPoint) tableModel.getValueAt(index, xAxisIndex)).getX();
} else if (tableModel.getValueAt(index, xAxisIndex) instanceof Integer) {
tempDataXYZ[0][counter] = (Integer) tableModel.getValueAt(index, xAxisIndex);
} else if (tableModel.getValueAt(index, xAxisIndex) instanceof Double) {
tempDataXYZ[0][counter] = (Double) tableModel.getValueAt(index, xAxisIndex);
} else if (tableModel.getValueAt(index, xAxisIndex) instanceof StartIndexes) {
if (((StartIndexes) tableModel.getValueAt(index, xAxisIndex)).getIndexes().size() > 0) {
tempDataXYZ[0][counter] = ((StartIndexes) tableModel.getValueAt(index, xAxisIndex)).getIndexes().get(0);
}
} else if (tableModel.getValueAt(index, xAxisIndex) instanceof ArrrayListDataPoints) {
ArrrayListDataPoints arrrayListDataPoints = (ArrrayListDataPoints) tableModel.getValueAt(index, xAxisIndex);
if (!arrrayListDataPoints.getData().isEmpty()) {
if (arrrayListDataPoints.getDataSortingType() == JSparklinesArrayListBarChartTableCellRenderer.ValueDisplayType.sumOfNumbers) {
tempDataXYZ[0][counter] = arrrayListDataPoints.getSum();
} else if (arrrayListDataPoints.getDataSortingType() == JSparklinesArrayListBarChartTableCellRenderer.ValueDisplayType.sumExceptLastNumber) {
tempDataXYZ[0][counter] = arrrayListDataPoints.getSumExceptLast();
} else if (arrrayListDataPoints.getDataSortingType() == JSparklinesArrayListBarChartTableCellRenderer.ValueDisplayType.firstNumberOnly) {
tempDataXYZ[0][counter] = arrrayListDataPoints.getData().get(0);
}
}
}
if (tableModel.getValueAt(index, yAxisIndex) instanceof XYDataPoint) {
tempDataXYZ[1][counter] = ((XYDataPoint) tableModel.getValueAt(index, yAxisIndex)).getX();
} else if (tableModel.getValueAt(index, yAxisIndex) instanceof Integer) {
tempDataXYZ[1][counter] = (Integer) tableModel.getValueAt(index, yAxisIndex);
} else if (tableModel.getValueAt(index, yAxisIndex) instanceof Double) {
tempDataXYZ[1][counter] = (Double) tableModel.getValueAt(index, yAxisIndex);
} else if (tableModel.getValueAt(index, yAxisIndex) instanceof StartIndexes) {
if (((StartIndexes) tableModel.getValueAt(index, yAxisIndex)).getIndexes().size() > 0) {
tempDataXYZ[1][counter] = ((StartIndexes) tableModel.getValueAt(index, yAxisIndex)).getIndexes().get(0);
}
} else if (tableModel.getValueAt(index, yAxisIndex) instanceof ArrrayListDataPoints) {
ArrrayListDataPoints arrrayListDataPoints = (ArrrayListDataPoints) tableModel.getValueAt(index, yAxisIndex);
if (!arrrayListDataPoints.getData().isEmpty()) {
if (arrrayListDataPoints.getDataSortingType() == JSparklinesArrayListBarChartTableCellRenderer.ValueDisplayType.sumOfNumbers) {
tempDataXYZ[1][counter] = arrrayListDataPoints.getSum();
} else if (arrrayListDataPoints.getDataSortingType() == JSparklinesArrayListBarChartTableCellRenderer.ValueDisplayType.sumExceptLastNumber) {
tempDataXYZ[1][counter] = arrrayListDataPoints.getSumExceptLast();
} else if (arrrayListDataPoints.getDataSortingType() == JSparklinesArrayListBarChartTableCellRenderer.ValueDisplayType.firstNumberOnly) {
tempDataXYZ[1][counter] = arrrayListDataPoints.getData().get(0);
}
}
}
// get the min and max x values
if (tempDataXYZ[0][counter] > maxXValue) {
maxXValue = tempDataXYZ[0][counter];
}
if (tempDataXYZ[0][counter] < minXValue) {
minXValue = tempDataXYZ[0][counter];
}
// get the regression line data
simpleRegression.addData(tempDataXYZ[0][counter], tempDataXYZ[1][counter]);
if (bubbleSizeIndex == 0) {
tempDataXYZ[2][counter] = bubbleSize;
} else {
if (tableModel.getValueAt(index, bubbleSizeIndex - 1) instanceof XYDataPoint) {
tempDataXYZ[2][counter] = ((XYDataPoint) tableModel.getValueAt(index, bubbleSizeIndex - 1)).getX();
} else if (tableModel.getValueAt(index, bubbleSizeIndex - 1) instanceof Integer) {
tempDataXYZ[2][counter] = ((Integer) tableModel.getValueAt(index, bubbleSizeIndex - 1));
} else if (tableModel.getValueAt(index, bubbleSizeIndex - 1) instanceof Double) {
tempDataXYZ[2][counter] = ((Double) tableModel.getValueAt(index, bubbleSizeIndex - 1));
} else if (tableModel.getValueAt(index, bubbleSizeIndex - 1) instanceof StartIndexes) {
if (((StartIndexes) tableModel.getValueAt(index, bubbleSizeIndex - 1)).getIndexes().size() > 0) {
tempDataXYZ[2][counter] = (((StartIndexes) tableModel.getValueAt(index, bubbleSizeIndex - 1)).getIndexes().get(0));
} else {
tempDataXYZ[2][counter] = 0;
}
} else if (tableModel.getValueAt(index, bubbleSizeIndex - 1) instanceof ArrrayListDataPoints) {
ArrrayListDataPoints arrrayListDataPoints = (ArrrayListDataPoints) tableModel.getValueAt(index, bubbleSizeIndex - 1);
if (!arrrayListDataPoints.getData().isEmpty()) {
if (arrrayListDataPoints.getDataSortingType() == JSparklinesArrayListBarChartTableCellRenderer.ValueDisplayType.sumOfNumbers) {
tempDataXYZ[2][counter] = arrrayListDataPoints.getSum();
} else if (arrrayListDataPoints.getDataSortingType() == JSparklinesArrayListBarChartTableCellRenderer.ValueDisplayType.sumExceptLastNumber) {
tempDataXYZ[2][counter] = arrrayListDataPoints.getSumExceptLast();
} else if (arrrayListDataPoints.getDataSortingType() == JSparklinesArrayListBarChartTableCellRenderer.ValueDisplayType.firstNumberOnly) {
tempDataXYZ[2][counter] = arrrayListDataPoints.getData().get(0);
}
} else {
tempDataXYZ[2][counter] = 0;
}
}
}
// add log scaling if selected
if (sizeLogCheckBox.isSelected()) {
tempDataXYZ[2][counter] = Math.log(tempDataXYZ[2][counter]) / Math.log(2);
}
// add the bubble scaling factor
tempDataXYZ[2][counter] = tempDataXYZ[2][counter] * bubbleScalingFactor;
// store the maximum bubble size
if (tempDataXYZ[2][counter] > maxBubbleSize) {
maxBubbleSize = tempDataXYZ[2][counter];
}
dataPointToRowNumber.put(datasetCounter + "_" + counter++, index);
}
// set the datasetcolor
int tableRowIndex = datasets.get(dataset).get(0);
Object tempObject = tableModel.getValueAt(tableRowIndex, colorIndex);
// get the color to use if using gradient color coding
if (tempObject instanceof Integer) {
datasetColors.put(datasetCounter, GradientColorCoding.findGradientColor(((Integer) tempObject).doubleValue(), minColorValue, maxColorValue, colorGradient, false));
} else if (tempObject instanceof Double) {
datasetColors.put(datasetCounter, GradientColorCoding.findGradientColor((Double) tempObject, minColorValue, maxColorValue, colorGradient, false));
} else if (tempObject instanceof XYDataPoint) {
datasetColors.put(datasetCounter, GradientColorCoding.findGradientColor(((XYDataPoint) tempObject).getX(), minColorValue, maxColorValue, colorGradient, false));
} else if (tempObject instanceof StartIndexes) {
if (((StartIndexes) tempObject).getIndexes().size() > 0) {
datasetColors.put(datasetCounter, GradientColorCoding.findGradientColor(((StartIndexes) tempObject).getIndexes().get(0).doubleValue(), minColorValue, maxColorValue, colorGradient, false));
} else {
datasetColors.put(datasetCounter, GradientColorCoding.findGradientColor(minColorValue, minColorValue, maxColorValue, colorGradient, false));
}
} else if (tempObject instanceof ArrrayListDataPoints) {
ArrrayListDataPoints arrrayListDataPoints = (ArrrayListDataPoints) tempObject;
if (!arrrayListDataPoints.getData().isEmpty()) {
if (arrrayListDataPoints.getDataSortingType() == JSparklinesArrayListBarChartTableCellRenderer.ValueDisplayType.sumOfNumbers) {
datasetColors.put(datasetCounter, GradientColorCoding.findGradientColor(arrrayListDataPoints.getSum(), minColorValue, maxColorValue, colorGradient, false));
} else if (arrrayListDataPoints.getDataSortingType() == JSparklinesArrayListBarChartTableCellRenderer.ValueDisplayType.sumExceptLastNumber) {
datasetColors.put(datasetCounter, GradientColorCoding.findGradientColor(arrrayListDataPoints.getSumExceptLast(), minColorValue, maxColorValue, colorGradient, false));
} else if (arrrayListDataPoints.getDataSortingType() == JSparklinesArrayListBarChartTableCellRenderer.ValueDisplayType.firstNumberOnly) {
datasetColors.put(datasetCounter, GradientColorCoding.findGradientColor(arrrayListDataPoints.getData().get(0), minColorValue, maxColorValue, colorGradient, false));
}
} else {
datasetColors.put(datasetCounter, GradientColorCoding.findGradientColor(minColorValue, minColorValue, maxColorValue, colorGradient, false));
}
} else {
datasetColors.put(datasetCounter, GradientColorCoding.findGradientColor(minColorValue, minColorValue, maxColorValue, colorGradient, false));
}
xyzDataset.addSeries(dataset, tempDataXYZ);
datasetCounter++;
}
progressDialog.setPrimaryProgressCounterIndeterminate(true);
// create the plot
JFreeChart chart = ChartFactory.createBubbleChart(null, xAxisName, yAxisName, xyzDataset, PlotOrientation.VERTICAL, false, true, false);
XYPlot plot = chart.getXYPlot();
// add the regression line
if (showRegressionLine) {
XYSeriesCollection regressionData = new XYSeriesCollection();
XYSeries regr = new XYSeries("RegressionLine");
regr.add(minXValue, simpleRegression.predict(minXValue));
regr.add(maxXValue, simpleRegression.predict(maxXValue));
regressionData.addSeries(regr);
// show the r squared value
plot.addAnnotation(new XYTextAnnotation("R2=" + Util.roundDouble(simpleRegression.getRSquare(), 2), maxXValue * 0.93, simpleRegression.predict(maxXValue) * 0.99));
StandardXYItemRenderer regressionRenderer = new StandardXYItemRenderer();
regressionRenderer.setBaseSeriesVisibleInLegend(false);
regressionRenderer.setSeriesPaint(0, Color.GRAY);
plot.setDataset(1, regressionData);
plot.setRenderer(1, regressionRenderer);
}
// set up the renderer
XYBubbleRenderer renderer = new XYBubbleRenderer(XYBubbleRenderer.SCALE_ON_DOMAIN_AXIS) {
@Override
public Stroke getItemOutlineStroke(int row, int column) {
boolean selectedInTable = false;
int[] selectedRows = selectedValuesTable.getSelectedRows();
for (int tableRowIndex : selectedRows) {
if (dataPointToRowNumber.get(row + "_" + column).intValue() == selectedValuesTable.convertRowIndexToModel(tableRowIndex)) {
selectedInTable = true;
}
}
if (selectedInTable) {
BasicStroke stroke = (BasicStroke) super.getItemOutlineStroke(row, column);
return new BasicStroke(stroke.getLineWidth() * 10f);
} else {
if (!selectedDataPoints.isEmpty()) {
if (selectedDataPoints.containsKey(row) && selectedDataPoints.get(row).contains(column)) {
BasicStroke stroke = (BasicStroke) super.getItemOutlineStroke(row, column);
return new BasicStroke(stroke.getLineWidth() * 1.2f);
} else {
return super.getItemOutlineStroke(row, column);
}
} else {
return super.getItemOutlineStroke(row, column);
}
}
}
@Override
public Paint getItemOutlinePaint(int row, int column) {
boolean selectedInTable = false;
int[] selectedRows = selectedValuesTable.getSelectedRows();
for (int tableRowIndex : selectedRows) {
if (dataPointToRowNumber.get(row + "_" + column).intValue() == selectedValuesTable.convertRowIndexToModel(tableRowIndex)) {
selectedInTable = true;
}
}
if (selectedInTable) {
// @TODO: should not be hard coded here!!
return Color.BLUE;
} else {
if (!selectedDataPoints.isEmpty()) {
if (selectedDataPoints.containsKey(row) && selectedDataPoints.get(row).contains(column)) {
return Color.BLACK;
} else {
return Color.LIGHT_GRAY;
}
} else {
return super.getItemOutlinePaint(row, column);
}
}
}
@Override
public Paint getItemPaint(int row, int column) {
if (!selectedDataPoints.isEmpty()) {
Color tempColor = (Color) super.getItemPaint(row, column);
if (selectedDataPoints.containsKey(row) && selectedDataPoints.get(row).contains(column)) {
return new Color(tempColor.getRed(), tempColor.getGreen(), tempColor.getBlue(), 255);
} else {
// @TODO: should not be hard coded here?
return new Color(tempColor.getRed(), tempColor.getGreen(), tempColor.getBlue(), 30);
}
} else {
return super.getItemPaint(row, column);
}
}
@Override
public Paint getItemFillPaint(int row, int column) {
if (!selectedDataPoints.isEmpty()) {
Color tempColor = (Color) super.getItemFillPaint(row, column);
if (selectedDataPoints.containsKey(row) && selectedDataPoints.get(row).contains(column)) {
return new Color(tempColor.getRed(), tempColor.getGreen(), tempColor.getBlue(), 255);
} else {
// @TODO: should not be hard coded here?
return new Color(tempColor.getRed(), tempColor.getGreen(), tempColor.getBlue(), 30);
}
} else {
return super.getItemFillPaint(row, column);
}
}
};
// set the colors for the data series
boolean isIntegerColorRenderer = false;
if (cellRenderers.containsKey(Integer.valueOf(colorsComboBox.getSelectedIndex()))) {
if (cellRenderers.get(Integer.valueOf(colorsComboBox.getSelectedIndex())) instanceof JSparklinesIntegerColorTableCellRenderer) {
JSparklinesIntegerColorTableCellRenderer integerColorRenderer = (JSparklinesIntegerColorTableCellRenderer) cellRenderers.get(Integer.valueOf(colorsComboBox.getSelectedIndex()));
HashMap<Integer, Color> colors = integerColorRenderer.getColors();
for (int i = 0; i < datasetNames.size(); i++) {
Integer datasetInteger = Integer.valueOf(datasetNames.get(i));
renderer.setSeriesPaint(i, colors.get(Integer.valueOf(datasetInteger)));
}
isIntegerColorRenderer = true;
}
}
if (!isIntegerColorRenderer && useGradientColorCoding) {
for (int i = 0; i < datasetNames.size(); i++) {
renderer.setSeriesPaint(i, datasetColors.get(i));
}
}
renderer.setBaseToolTipGenerator(new StandardXYZToolTipGenerator());
plot.setRenderer(renderer);
// make all datapoints semitransparent
plot.setForegroundAlpha(0.5f);
// remove space before/after the domain axis
plot.getDomainAxis().setUpperMargin(0);
plot.getDomainAxis().setLowerMargin(0);
plot.setRangeGridlinePaint(Color.black);
// linear or logarithmic axis
if (xAxisLogCheckBox.isSelected()) {
plot.setDomainAxis(new LogAxis(plot.getDomainAxis().getLabel()));
}
if (yAxisLogCheckBox.isSelected()) {
plot.setRangeAxis(new LogAxis(plot.getRangeAxis().getLabel()));
}
// store the x axis range to see of the bubbles are too big
xAxisRange = plot.getDomainAxis().getUpperBound() - plot.getDomainAxis().getLowerBound();
// hide unwanted chart details
plot.setDomainGridlinesVisible(false);
chart.getPlot().setOutlineVisible(false);
// set background color
chart.getPlot().setBackgroundPaint(Color.WHITE);
chart.setBackgroundPaint(Color.WHITE);
chartPanel = new ChartPanel(chart) {
@Override
public void mouseReleased(MouseEvent e) {
if (selectionActive) {
setMouseZoomable(false);
super.mouseReleased(e);
setMouseZoomable(true);
} else {
super.mouseReleased(e);
}
}
};
chartPanel.setBackground(Color.WHITE);
// add the plot to the chart
plotPanel.add(chartPanel);
plotPanel.revalidate();
plotPanel.repaint();
// add chart mouse listener
chartPanel.addChartMouseListener(new ChartMouseListener() {
public void chartMouseClicked(ChartMouseEvent cme) {
mouseClickedInChart(cme);
}
public void chartMouseMoved(ChartMouseEvent cme) {
mouseMovedInChart(cme);
}
});
// add chart mouse motion listener
chartPanel.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (!dragToZoomRadioButton.isSelected()) {
selectionActive = true;
mouseDragged = true;
} else {
selectionActive = false;
super.mouseDragged(e);
}
}
});
// add more chart mouse listeners
chartPanel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
dragStart = e.getPoint();
mouseDragged = false;
super.mouseClicked(e);
}
@Override
public void mouseReleased(MouseEvent e) {
if (mouseDragged) {
dragEnd = e.getPoint();
// clear the old selection
selectedDataPoints = new HashMap<Integer, ArrayList<Integer>>();
selectedModelRows = new ArrayList<Integer>();
double dragStartX = (dragStart.getX() - chartPanel.getInsets().left) / chartPanel.getScaleX();
double dragStartY = (dragStart.getY() - chartPanel.getInsets().top) / chartPanel.getScaleY();
double dragEndX = (dragEnd.getX() - chartPanel.getInsets().left) / chartPanel.getScaleX();
double dragEndY = (dragEnd.getY() - chartPanel.getInsets().top) / chartPanel.getScaleY();
ArrayList<XYItemEntity> entitiesFound = new ArrayList<XYItemEntity>();
EntityCollection entities = chartPanel.getChartRenderingInfo().getEntityCollection();
Iterator<ChartEntity> iterator = entities.iterator();
while (iterator.hasNext()) {
ChartEntity entity = iterator.next();
if (entity instanceof XYItemEntity) {
if (entity.getArea().intersects(dragStartX, dragStartY, dragEndX - dragStartX, dragEndY - dragStartY)) {
if (!entitiesFound.contains((XYItemEntity) entity)) {
entitiesFound.add((XYItemEntity) entity);
}
}
}
}
for (XYItemEntity entity : entitiesFound) {
selectEntity(entity, false);
}
}
mouseDragged = false;
chartPanel.getChart().fireChartChanged();
filterTable();
super.mouseReleased(e);
}
});
}
if (tableModel instanceof SelfUpdatingTableModel) {
((SelfUpdatingTableModel) tableModel).setSelfUpdating(selfUpdating);
}
isPlotting = false;
filterTable();
progressDialog.setRunFinished();
if (maxBubbleSize > xAxisRange && !sizeLogCheckBox.isSelected()) {
int value = JOptionPane.showConfirmDialog(dialogParent, "Seems like your bubbles are too large.\nTurn on log scale?", "Log Scale?", JOptionPane.YES_NO_OPTION);
if (value == JOptionPane.YES_OPTION) {
sizeLogCheckBox.setSelected(true);
updatePlot();
}
}
}Example 28
| Project: ewjUtil-master File: PearsonsCorrelation.java View source code |
/**
* Computes the Pearson's product-moment correlation coefficient between the two arrays.
*
* </p>Throws IllegalArgumentException if the arrays do not have the same length
* or their common length is less than 2</p>
*
* @param xArray first data array
* @param yArray second data array
* @return Returns Pearson's correlation coefficient for the two arrays
* @throws IllegalArgumentException if the arrays lengths do not match or
* there is insufficient data
*/
public double correlation(final double[] xArray, final double[] yArray) throws IllegalArgumentException {
SimpleRegression regression = new SimpleRegression();
if (xArray.length != yArray.length) {
throw new DimensionMismatchException(xArray.length, yArray.length);
} else if (xArray.length < 2) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION, xArray.length, 2);
} else {
for (int i = 0; i < xArray.length; i++) {
regression.addData(xArray[i], yArray[i]);
}
return regression.getR();
}
}Example 29
| Project: CloudSim-master File: MathUtil.java View source code |
public static SimpleRegression createWeigthedLinearRegression(final double[] x, final double[] y, final double[] weigths) {
double[] xW = new double[x.length];
double[] yW = new double[y.length];
int numZeroWeigths = 0;
for (int i = 0; i < weigths.length; i++) {
if (weigths[i] <= 0) {
numZeroWeigths++;
}
}
for (int i = 0; i < x.length; i++) {
if (numZeroWeigths >= 0.4 * weigths.length) {
// See: http://www.ncsu.edu/crsc/events/ugw07/Presentations/Crooks_Qiao/Crooks_Qiao_Alt_Presentation.pdf
xW[i] = Math.sqrt(weigths[i]) * x[i];
yW[i] = Math.sqrt(weigths[i]) * y[i];
} else {
xW[i] = x[i];
yW[i] = y[i];
}
}
return createLinearRegression(xW, yW);
}Example 30
| Project: Jetfuel-CSharp-master File: PearsonsCorrelation.java View source code |
/**
* Computes the Pearson's product-moment correlation coefficient between the two arrays.
*
* </p>Throws IllegalArgumentException if the arrays do not have the same length
* or their common length is less than 2</p>
*
* @param xArray first data array
* @param yArray second data array
* @return Returns Pearson's correlation coefficient for the two arrays
* @throws DimensionMismatchException if the arrays lengths do not match
* @throws MathIllegalArgumentException if there is insufficient data
*/
public double correlation(final double[] xArray, final double[] yArray) {
SimpleRegression regression = new SimpleRegression();
if (xArray.length != yArray.length) {
throw new DimensionMismatchException(xArray.length, yArray.length);
} else if (xArray.length < 2) {
throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION, xArray.length, 2);
} else {
for (int i = 0; i < xArray.length; i++) {
regression.addData(xArray[i], yArray[i]);
}
return regression.getR();
}
}Example 31
| Project: CARMA-master File: PearsonsCorrelation.java View source code |
/**
* Computes the Pearson's product-moment correlation coefficient between two arrays.
*
* <p>Throws MathIllegalArgumentException if the arrays do not have the same length
* or their common length is less than 2. Returns {@code NaN} if either of the arrays
* has zero variance (i.e., if one of the arrays does not contain at least two distinct
* values).</p>
*
* @param xArray first data array
* @param yArray second data array
* @return Returns Pearson's correlation coefficient for the two arrays
* @throws DimensionMismatchException if the arrays lengths do not match
* @throws MathIllegalArgumentException if there is insufficient data
*/
public double correlation(final double[] xArray, final double[] yArray) {
SimpleRegression regression = new SimpleRegression();
if (xArray.length != yArray.length) {
throw new DimensionMismatchException(xArray.length, yArray.length);
} else if (xArray.length < 2) {
throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION, xArray.length, 2);
} else {
for (int i = 0; i < xArray.length; i++) {
regression.addData(xArray[i], yArray[i]);
}
return regression.getR();
}
}Example 32
| Project: OpenJML-master File: PearsonsCorrelation.java View source code |
/**
* Computes the Pearson's product-moment correlation coefficient between two arrays.
*
* <p>Throws MathIllegalArgumentException if the arrays do not have the same length
* or their common length is less than 2. Returns {@code NaN} if either of the arrays
* has zero variance (i.e., if one of the arrays does not contain at least two distinct
* values).</p>
*
* @param xArray first data array
* @param yArray second data array
* @return Returns Pearson's correlation coefficient for the two arrays
* @throws DimensionMismatchException if the arrays lengths do not match
* @throws MathIllegalArgumentException if there is insufficient data
*/
public double correlation(final double[] xArray, final double[] yArray) {
SimpleRegression regression = new SimpleRegression();
if (xArray.length != yArray.length) {
throw new DimensionMismatchException(xArray.length, yArray.length);
} else if (xArray.length < 2) {
throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION, xArray.length, 2);
} else {
for (int i = 0; i < xArray.length; i++) {
regression.addData(xArray[i], yArray[i]);
}
return regression.getR();
}
}Example 33
| Project: nbites-master File: PearsonsCorrelation.java View source code |
/**
* Computes the Pearson's product-moment correlation coefficient between two arrays.
*
* <p>Throws MathIllegalArgumentException if the arrays do not have the same length
* or their common length is less than 2. Returns {@code NaN} if either of the arrays
* has zero variance (i.e., if one of the arrays does not contain at least two distinct
* values).</p>
*
* @param xArray first data array
* @param yArray second data array
* @return Returns Pearson's correlation coefficient for the two arrays
* @throws DimensionMismatchException if the arrays lengths do not match
* @throws MathIllegalArgumentException if there is insufficient data
*/
public double correlation(final double[] xArray, final double[] yArray) {
SimpleRegression regression = new SimpleRegression();
if (xArray.length != yArray.length) {
throw new DimensionMismatchException(xArray.length, yArray.length);
} else if (xArray.length < 2) {
throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION, xArray.length, 2);
} else {
for (int i = 0; i < xArray.length; i++) {
regression.addData(xArray[i], yArray[i]);
}
return regression.getR();
}
}Example 34
| Project: geogebra-master File: PearsonsCorrelation.java View source code |
/**
* Computes the Pearson's product-moment correlation coefficient between two arrays.
*
* <p>Throws MathIllegalArgumentException if the arrays do not have the same length
* or their common length is less than 2. Returns {@code NaN} if either of the arrays
* has zero variance (i.e., if one of the arrays does not contain at least two distinct
* values).</p>
*
* @param xArray first data array
* @param yArray second data array
* @return Returns Pearson's correlation coefficient for the two arrays
* @throws DimensionMismatchException if the arrays lengths do not match
* @throws MathIllegalArgumentException if there is insufficient data
*/
public double correlation(final double[] xArray, final double[] yArray) {
SimpleRegression regression = new SimpleRegression();
if (xArray.length != yArray.length) {
throw new DimensionMismatchException(xArray.length, yArray.length);
} else if (xArray.length < 2) {
throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION, xArray.length, 2);
} else {
for (int i = 0; i < xArray.length; i++) {
regression.addData(xArray[i], yArray[i]);
}
return regression.getR();
}
}