use of org.apache.commons.math3.stat.descriptive.rank.Max in project lucene-solr by apache.
the class HistogramEvaluator method evaluate.
public List<Map> evaluate(Tuple tuple) throws IOException {
StreamEvaluator colEval1 = subEvaluators.get(0);
List<Number> numbers1 = (List<Number>) colEval1.evaluate(tuple);
double[] column1 = new double[numbers1.size()];
for (int i = 0; i < numbers1.size(); i++) {
column1[i] = numbers1.get(i).doubleValue();
}
int bins = 10;
if (subEvaluators.size() == 2) {
StreamEvaluator binsEval = subEvaluators.get(1);
Number binsNum = (Number) binsEval.evaluate(tuple);
bins = binsNum.intValue();
}
EmpiricalDistribution empiricalDistribution = new EmpiricalDistribution(bins);
empiricalDistribution.load(column1);
List<Map> binList = new ArrayList();
List<SummaryStatistics> summaries = empiricalDistribution.getBinStats();
for (SummaryStatistics statisticalSummary : summaries) {
Map map = new HashMap();
map.put("max", statisticalSummary.getMax());
map.put("mean", statisticalSummary.getMean());
map.put("min", statisticalSummary.getMin());
map.put("stdev", statisticalSummary.getStandardDeviation());
map.put("sum", statisticalSummary.getSum());
map.put("N", statisticalSummary.getN());
map.put("var", statisticalSummary.getVariance());
binList.add(map);
}
return binList;
}
use of org.apache.commons.math3.stat.descriptive.rank.Max in project GDSC-SMLM by aherbert.
the class SpotInspector method run.
/*
* (non-Javadoc)
*
* @see ij.plugin.PlugIn#run(java.lang.String)
*/
public void run(String arg) {
SMLMUsageTracker.recordPlugin(this.getClass(), arg);
if (MemoryPeakResults.isMemoryEmpty()) {
IJ.error(TITLE, "No localisations in memory");
return;
}
if (!showDialog())
return;
// Load the results
results = ResultsManager.loadInputResults(inputOption, false);
if (results == null || results.size() == 0) {
IJ.error(TITLE, "No results could be loaded");
IJ.showStatus("");
return;
}
// Check if the original image is open
ImageSource source = results.getSource();
if (source == null) {
IJ.error(TITLE, "Unknown original source image");
return;
}
source = source.getOriginal();
if (!source.open()) {
IJ.error(TITLE, "Cannot open original source image: " + source.toString());
return;
}
final float stdDevMax = getStandardDeviation(results);
if (stdDevMax < 0) {
// TODO - Add dialog to get the initial peak width
IJ.error(TITLE, "Fitting configuration (for initial peak width) is not available");
return;
}
// Rank spots
rankedResults = new ArrayList<PeakResultRank>(results.size());
final double a = results.getNmPerPixel();
final double gain = results.getGain();
final boolean emCCD = results.isEMCCD();
for (PeakResult r : results.getResults()) {
float[] score = getScore(r, a, gain, emCCD, stdDevMax);
rankedResults.add(new PeakResultRank(r, score[0], score[1]));
}
Collections.sort(rankedResults);
// Prepare results table. Get bias if necessary
if (showCalibratedValues) {
// Get a bias if required
Calibration calibration = results.getCalibration();
if (calibration.getBias() == 0) {
ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
gd.addMessage("Calibrated results requires a camera bias");
gd.addNumericField("Camera_bias (ADUs)", calibration.getBias(), 2);
gd.showDialog();
if (!gd.wasCanceled()) {
calibration.setBias(Math.abs(gd.getNextNumber()));
}
}
}
IJTablePeakResults table = new IJTablePeakResults(false, results.getName(), true);
table.copySettings(results);
table.setTableTitle(TITLE);
table.setAddCounter(true);
table.setShowCalibratedValues(showCalibratedValues);
table.begin();
// Add a mouse listener to jump to the frame for the clicked line
textPanel = table.getResultsWindow().getTextPanel();
// We must ignore old instances of this class from the mouse listeners
id = ++currentId;
textPanel.addMouseListener(this);
// Add results to the table
int n = 0;
for (PeakResultRank rank : rankedResults) {
rank.rank = n++;
PeakResult r = rank.peakResult;
table.add(r.getFrame(), r.origX, r.origY, r.origValue, r.error, r.noise, r.params, r.paramsStdDev);
}
table.end();
if (plotScore || plotHistogram) {
// Get values for the plots
float[] xValues = null, yValues = null;
double yMin, yMax;
int spotNumber = 0;
xValues = new float[rankedResults.size()];
yValues = new float[xValues.length];
for (PeakResultRank rank : rankedResults) {
xValues[spotNumber] = spotNumber + 1;
yValues[spotNumber++] = recoverScore(rank.score);
}
// Set the min and max y-values using 1.5 x IQR
DescriptiveStatistics stats = new DescriptiveStatistics();
for (float v : yValues) stats.addValue(v);
if (removeOutliers) {
double lower = stats.getPercentile(25);
double upper = stats.getPercentile(75);
double iqr = upper - lower;
yMin = FastMath.max(lower - iqr, stats.getMin());
yMax = FastMath.min(upper + iqr, stats.getMax());
IJ.log(String.format("Data range: %f - %f. Plotting 1.5x IQR: %f - %f", stats.getMin(), stats.getMax(), yMin, yMax));
} else {
yMin = stats.getMin();
yMax = stats.getMax();
IJ.log(String.format("Data range: %f - %f", yMin, yMax));
}
plotScore(xValues, yValues, yMin, yMax);
plotHistogram(yValues, yMin, yMax);
}
// Extract spots into a stack
final int w = source.getWidth();
final int h = source.getHeight();
final int size = 2 * radius + 1;
ImageStack spots = new ImageStack(size, size, rankedResults.size());
// To assist the extraction of data from the image source, process them in time order to allow
// frame caching. Then set the appropriate slice in the result stack
Collections.sort(rankedResults, new Comparator<PeakResultRank>() {
public int compare(PeakResultRank o1, PeakResultRank o2) {
if (o1.peakResult.getFrame() < o2.peakResult.getFrame())
return -1;
if (o1.peakResult.getFrame() > o2.peakResult.getFrame())
return 1;
return 0;
}
});
for (PeakResultRank rank : rankedResults) {
PeakResult r = rank.peakResult;
// Extract image
// Note that the coordinates are relative to the middle of the pixel (0.5 offset)
// so do not round but simply convert to int
final int x = (int) (r.params[Gaussian2DFunction.X_POSITION]);
final int y = (int) (r.params[Gaussian2DFunction.Y_POSITION]);
// Extract a region but crop to the image bounds
int minX = x - radius;
int minY = y - radius;
int maxX = FastMath.min(x + radius + 1, w);
int maxY = FastMath.min(y + radius + 1, h);
int padX = 0, padY = 0;
if (minX < 0) {
padX = -minX;
minX = 0;
}
if (minY < 0) {
padY = -minY;
minY = 0;
}
int sizeX = maxX - minX;
int sizeY = maxY - minY;
float[] data = source.get(r.getFrame(), new Rectangle(minX, minY, sizeX, sizeY));
// Prevent errors with missing data
if (data == null)
data = new float[sizeX * sizeY];
ImageProcessor spotIp = new FloatProcessor(sizeX, sizeY, data, null);
// Pad if necessary, i.e. the crop is too small for the stack
if (padX > 0 || padY > 0 || sizeX < size || sizeY < size) {
ImageProcessor spotIp2 = spotIp.createProcessor(size, size);
spotIp2.insert(spotIp, padX, padY);
spotIp = spotIp2;
}
int slice = rank.rank + 1;
spots.setPixels(spotIp.getPixels(), slice);
spots.setSliceLabel(Utils.rounded(rank.originalScore), slice);
}
source.close();
ImagePlus imp = Utils.display(TITLE, spots);
imp.setRoi((PointRoi) null);
// Make bigger
for (int i = 10; i-- > 0; ) imp.getWindow().getCanvas().zoomIn(imp.getWidth() / 2, imp.getHeight() / 2);
}
use of org.apache.commons.math3.stat.descriptive.rank.Max in project gatk by broadinstitute.
the class OptimizationUtils method argmax.
public static double argmax(final Function<Double, Double> function, final double min, final double max, final double guess, final double relativeTolerance, final double absoluteTolerance, final int maxEvaluations) {
final BrentOptimizer optimizer = new BrentOptimizer(relativeTolerance, absoluteTolerance);
final SearchInterval interval = new SearchInterval(min, max, guess);
return optimizer.optimize(new UnivariateObjectiveFunction(function::apply), GoalType.MAXIMIZE, interval, new MaxEval(maxEvaluations)).getPoint();
}
use of org.apache.commons.math3.stat.descriptive.rank.Max in project gatk-protected by broadinstitute.
the class ReadCountCollectionUtilsUnitTest method testRemoveTargetsWithTooManyZeros.
@Test(dataProvider = "tooManyZerosData")
public void testRemoveTargetsWithTooManyZeros(final ReadCountCollection readCount) {
final RealMatrix counts = readCount.counts();
final int[] numberOfZeros = IntStream.range(0, counts.getRowDimension()).map(i -> (int) DoubleStream.of(counts.getRow(i)).filter(d -> d == 0.0).count()).toArray();
final int maximumNumberOfZeros = IntStream.of(numberOfZeros).max().getAsInt();
for (int maxZeros = 0; maxZeros < maximumNumberOfZeros; maxZeros++) {
final int maxZerosThres = maxZeros;
final int expectedRemainingCount = (int) IntStream.of(numberOfZeros).filter(i -> i <= maxZerosThres).count();
if (expectedRemainingCount == 0) {
try {
ReadCountCollectionUtils.removeTargetsWithTooManyZeros(readCount, maxZeros, false, NULL_LOGGER);
} catch (final UserException.BadInput ex) {
// expected.
continue;
}
Assert.fail("expects an exception");
}
final ReadCountCollection rc = ReadCountCollectionUtils.removeTargetsWithTooManyZeros(readCount, maxZeros, false, NULL_LOGGER);
Assert.assertEquals(rc.targets().size(), expectedRemainingCount);
int nextIndex = 0;
for (int i = 0; i < readCount.targets().size(); i++) {
final Target target = readCount.targets().get(i);
final int newIndex = rc.targets().indexOf(target);
if (numberOfZeros[i] <= maxZeros) {
Assert.assertTrue(newIndex >= 0, " " + numberOfZeros[i] + " " + maxZeros);
Assert.assertEquals(newIndex, nextIndex++);
} else {
Assert.assertEquals(newIndex, -1);
}
}
Assert.assertEquals(nextIndex, expectedRemainingCount);
}
}
use of org.apache.commons.math3.stat.descriptive.rank.Max in project gatk-protected by broadinstitute.
the class ReadCountCollectionUtilsUnitTest method testRemoveColumnsWithTooManyZeros.
@Test(dataProvider = "tooManyZerosData")
public void testRemoveColumnsWithTooManyZeros(final ReadCountCollection readCount) {
final RealMatrix counts = readCount.counts();
final int[] numberOfZeros = IntStream.range(0, counts.getColumnDimension()).map(i -> (int) DoubleStream.of(counts.getColumn(i)).filter(d -> d == 0.0).count()).toArray();
final int maximumNumberOfZeros = IntStream.of(numberOfZeros).max().getAsInt();
for (int maxZeros = 0; maxZeros < maximumNumberOfZeros; maxZeros++) {
final int maxZerosThres = maxZeros;
final int expectedRemainingCount = (int) IntStream.of(numberOfZeros).filter(i -> i <= maxZerosThres).count();
if (expectedRemainingCount == 0) {
try {
ReadCountCollectionUtils.removeColumnsWithTooManyZeros(readCount, maxZeros, false, NULL_LOGGER);
} catch (final UserException.BadInput ex) {
// expected.
continue;
}
Assert.fail("expects an exception");
}
final ReadCountCollection rc = ReadCountCollectionUtils.removeColumnsWithTooManyZeros(readCount, maxZeros, false, NULL_LOGGER);
Assert.assertEquals(rc.columnNames().size(), expectedRemainingCount);
final int[] newIndices = new int[expectedRemainingCount];
int nextIndex = 0;
for (int i = 0; i < readCount.columnNames().size(); i++) {
final String name = readCount.columnNames().get(i);
final int newIndex = rc.columnNames().indexOf(name);
if (numberOfZeros[i] <= maxZeros) {
Assert.assertTrue(newIndex >= 0);
newIndices[nextIndex++] = i;
} else {
Assert.assertEquals(newIndex, -1);
}
}
Assert.assertEquals(nextIndex, expectedRemainingCount);
for (int i = 1; i < newIndices.length; i++) {
Assert.assertTrue(newIndices[i - 1] < newIndices[i]);
}
}
}
Aggregations