use of org.apache.commons.math3.stat.Frequency in project GDSC-SMLM by aherbert.
the class BenchmarkFilterAnalysis method depthAnalysis.
/**
* Depth analysis.
*
* @param allAssignments
* The assignments generated from running the filter (or null)
* @param filter
* the filter
* @return the assignments
*/
private ArrayList<FractionalAssignment[]> depthAnalysis(ArrayList<FractionalAssignment[]> allAssignments, DirectFilter filter) {
if (!depthRecallAnalysis || simulationParameters.fixedDepth)
return null;
// Build a histogram of the number of spots at different depths
final double[] depths = depthStats.getValues();
double[] limits = Maths.limits(depths);
//final int bins = Math.max(10, nActual / 100);
//final int bins = Utils.getBinsSturges(depths.length);
final int bins = Utils.getBinsSqrt(depths.length);
double[][] h1 = Utils.calcHistogram(depths, limits[0], limits[1], bins);
double[][] h2 = Utils.calcHistogram(depthFitStats.getValues(), limits[0], limits[1], bins);
// manually to get the results that pass.
if (allAssignments == null)
allAssignments = getAssignments(filter);
double[] depths2 = new double[results.size()];
int count = 0;
for (FractionalAssignment[] assignments : allAssignments) {
if (assignments == null)
continue;
for (int i = 0; i < assignments.length; i++) {
final CustomFractionalAssignment c = (CustomFractionalAssignment) assignments[i];
depths2[count++] = c.peak.error;
}
}
depths2 = Arrays.copyOf(depths2, count);
// Build a histogram using the same limits
double[][] h3 = Utils.calcHistogram(depths2, limits[0], limits[1], bins);
// Convert pixel depth to nm
for (int i = 0; i < h1[0].length; i++) h1[0][i] *= simulationParameters.a;
limits[0] *= simulationParameters.a;
limits[1] *= simulationParameters.a;
// Produce a histogram of the number of spots at each depth
String title1 = TITLE + " Depth Histogram";
Plot2 plot1 = new Plot2(title1, "Depth (nm)", "Frequency");
plot1.setLimits(limits[0], limits[1], 0, Maths.max(h1[1]));
plot1.setColor(Color.black);
plot1.addPoints(h1[0], h1[1], Plot2.BAR);
plot1.addLabel(0, 0, "Black = Spots; Blue = Fitted; Red = Filtered");
plot1.setColor(Color.blue);
plot1.addPoints(h1[0], h2[1], Plot2.BAR);
plot1.setColor(Color.red);
plot1.addPoints(h1[0], h3[1], Plot2.BAR);
plot1.setColor(Color.magenta);
PlotWindow pw1 = Utils.display(title1, plot1);
if (Utils.isNewWindow())
wo.add(pw1);
// Interpolate
final double halfBinWidth = (h1[0][1] - h1[0][0]) * 0.5;
// Remove final value of the histogram as this is at the upper limit of the range (i.e. count zero)
h1[0] = Arrays.copyOf(h1[0], h1[0].length - 1);
h1[1] = Arrays.copyOf(h1[1], h1[0].length);
h2[1] = Arrays.copyOf(h2[1], h1[0].length);
h3[1] = Arrays.copyOf(h3[1], h1[0].length);
// TODO : Fix the smoothing since LOESS sometimes does not work.
// Perhaps allow configuration of the number of histogram bins and the smoothing bandwidth.
// Use minimum of 3 points for smoothing
// Ensure we use at least x% of data
double bandwidth = Math.max(3.0 / h1[0].length, 0.15);
LoessInterpolator loess = new LoessInterpolator(bandwidth, 1);
PolynomialSplineFunction spline1 = loess.interpolate(h1[0], h1[1]);
PolynomialSplineFunction spline2 = loess.interpolate(h1[0], h2[1]);
PolynomialSplineFunction spline3 = loess.interpolate(h1[0], h3[1]);
// Use a second interpolator in case the LOESS fails
LinearInterpolator lin = new LinearInterpolator();
PolynomialSplineFunction spline1b = lin.interpolate(h1[0], h1[1]);
PolynomialSplineFunction spline2b = lin.interpolate(h1[0], h2[1]);
PolynomialSplineFunction spline3b = lin.interpolate(h1[0], h3[1]);
// Increase the number of points to show a smooth curve
double[] points = new double[bins * 5];
limits = Maths.limits(h1[0]);
final double interval = (limits[1] - limits[0]) / (points.length - 1);
double[] v = new double[points.length];
double[] v2 = new double[points.length];
double[] v3 = new double[points.length];
for (int i = 0; i < points.length - 1; i++) {
points[i] = limits[0] + i * interval;
v[i] = getSplineValue(spline1, spline1b, points[i]);
v2[i] = getSplineValue(spline2, spline2b, points[i]);
v3[i] = getSplineValue(spline3, spline3b, points[i]);
points[i] += halfBinWidth;
}
// Final point on the limit of the spline range
int ii = points.length - 1;
v[ii] = getSplineValue(spline1, spline1b, limits[1]);
v2[ii] = getSplineValue(spline2, spline2b, limits[1]);
v3[ii] = getSplineValue(spline3, spline3b, limits[1]);
points[ii] = limits[1] + halfBinWidth;
// Calculate recall
for (int i = 0; i < v.length; i++) {
v2[i] = v2[i] / v[i];
v3[i] = v3[i] / v[i];
}
final double halfSummaryDepth = summaryDepth * 0.5;
String title2 = TITLE + " Depth Histogram (normalised)";
Plot2 plot2 = new Plot2(title2, "Depth (nm)", "Recall");
plot2.setLimits(limits[0] + halfBinWidth, limits[1] + halfBinWidth, 0, Maths.min(1, Maths.max(v2)));
plot2.setColor(Color.black);
plot2.addLabel(0, 0, "Blue = Fitted; Red = Filtered");
plot2.setColor(Color.blue);
plot2.addPoints(points, v2, Plot2.LINE);
plot2.setColor(Color.red);
plot2.addPoints(points, v3, Plot2.LINE);
plot2.setColor(Color.magenta);
if (-halfSummaryDepth - halfBinWidth >= limits[0]) {
plot2.drawLine(-halfSummaryDepth, 0, -halfSummaryDepth, getSplineValue(spline3, spline3b, -halfSummaryDepth - halfBinWidth) / getSplineValue(spline1, spline1b, -halfSummaryDepth - halfBinWidth));
}
if (halfSummaryDepth - halfBinWidth <= limits[1]) {
plot2.drawLine(halfSummaryDepth, 0, halfSummaryDepth, getSplineValue(spline3, spline3b, halfSummaryDepth - halfBinWidth) / getSplineValue(spline1, spline1b, halfSummaryDepth - halfBinWidth));
}
PlotWindow pw2 = Utils.display(title2, plot2);
if (Utils.isNewWindow())
wo.add(pw2);
return allAssignments;
}
use of org.apache.commons.math3.stat.Frequency in project knime-core by knime.
the class WorkflowCoachView method updateFrequencyColumnHeadersAndToolTips.
/**
* Updates the names and tooltips of the frequency column headers.
*/
private void updateFrequencyColumnHeadersAndToolTips() {
m_namesAndToolTips = NodeRecommendationManager.getInstance().getNodeTripleProviders().stream().filter(p -> p.isEnabled()).map(p -> new Pair<>(p.getName(), p.getDescription())).collect(Collectors.toList());
if (m_namesAndToolTips == null || m_namesAndToolTips.isEmpty()) {
updateInputNoProvider();
return;
}
// reset table sorter
IElementComparer sorter = m_viewer.getComparer();
if (sorter != null && sorter instanceof TableColumnSorter) {
((TableColumnSorter) sorter).setColumn(null);
}
// enforce to change the viewer state to update the headers
m_viewerState = null;
m_lastSelection = "";
changeViewerStateTo(ViewerState.RECOMMENDATIONS);
// get current selection from the workbench and update the recommendation list
IEditorPart activeEditor = getViewSite().getPage().getActiveEditor();
if (activeEditor == null) {
// if no workflow is opened
updateInput(NO_WORKFLOW_OPENED_MESSAGE);
} else {
IWorkbenchPartSite site = activeEditor.getSite();
if (site != null) {
ISelectionProvider selectionProvider = site.getSelectionProvider();
if (selectionProvider != null) {
ISelection selection = selectionProvider.getSelection();
if (selection != null && selection instanceof IStructuredSelection) {
updateInput(selection);
return;
}
}
}
updateInput(StructuredSelection.EMPTY);
}
}
use of org.apache.commons.math3.stat.Frequency in project narchy by automenta.
the class BagTest method samplingPriDist.
public static Tensor samplingPriDist(@NotNull Bag<PLink<String>, PLink<String>> b, int batches, int batchSize, int bins) {
assert (bins > 1);
Set<String> hit = new TreeSet();
Frequency hits = new Frequency();
ArrayTensor f = new ArrayTensor(bins);
assertFalse(b.isEmpty());
Random rng = new XoRoShiRo128PlusRandom(1);
for (int i = 0; i < batches; i++) {
b.sample(rng, batchSize, x -> {
f.data[Util.bin(b.pri(x), bins)]++;
String s = x.get();
hits.addValue(s);
hit.add(s);
});
}
int total = batches * batchSize;
assertEquals(total, Util.sum(f.data), 0.001f);
if (hits.getUniqueCount() != b.size()) {
System.out.println(hits.getUniqueCount() + " != " + b.size());
Set<String> items = b.stream().map(PLink::get).collect(Collectors.toSet());
items.removeAll(hit);
System.out.println("not hit: " + items);
System.out.println(hits);
fail("all elements must have been sampled at least once");
}
return f.scale(1f / total);
}
use of org.apache.commons.math3.stat.Frequency in project ma-modules-public by infiniteautomation.
the class PointValueFftCalculator method streamData.
/* (non-Javadoc)
* @see com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.PointValueTimeStream#streamData(java.io.Writer)
*/
@Override
public void streamData(JsonGenerator jgen) {
this.setupDates();
DateTime startTime = new DateTime(from);
DateTime endTime = new DateTime(to);
FftGenerator generator = this.calculate(startTime, endTime);
double[] fftData = generator.getValues();
double sampleRateHz = 1000d / generator.getAverageSamplePeriodMs();
double dataLength = (double) fftData.length;
// Output The Real Steady State Mangitude
try {
jgen.writeStartObject();
// Amplitude
jgen.writeNumberField("value", fftData[0]);
double frequency = 0;
jgen.writeNumberField("frequency", frequency);
jgen.writeEndObject();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
double realComponent, imaginaryComponent, frequency;
if (fftData.length % 2 == 0) {
for (int i = 2; i < fftData.length / 2; i++) {
try {
realComponent = fftData[i * 2];
imaginaryComponent = fftData[2 * i + 1];
Complex c = new Complex(realComponent, imaginaryComponent);
jgen.writeStartObject();
// Amplitude
jgen.writeNumberField("value", c.abs());
if (this.returnFrequency)
frequency = (double) i * sampleRateHz / dataLength;
else
frequency = 1d / ((double) i * sampleRateHz / dataLength);
jgen.writeNumberField("frequency", frequency);
jgen.writeEndObject();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
for (int i = 2; i < (fftData.length - 1) / 2; i++) {
try {
realComponent = fftData[i * 2];
imaginaryComponent = fftData[2 * i + 1];
Complex c = new Complex(realComponent, imaginaryComponent);
jgen.writeStartObject();
// Amplitude
jgen.writeNumberField("value", c.abs());
if (this.returnFrequency)
frequency = (double) i * sampleRateHz / dataLength;
else
frequency = 1d / ((double) i * sampleRateHz / dataLength);
jgen.writeNumberField("frequency", frequency);
jgen.writeEndObject();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Write the last value out as it isn't in order in the array
try {
realComponent = fftData[fftData.length / 2];
imaginaryComponent = fftData[1];
Complex c = new Complex(realComponent, imaginaryComponent);
jgen.writeStartObject();
// Amplitude
jgen.writeNumberField("value", c.abs());
if (this.returnFrequency)
frequency = (double) (((fftData.length - 1) / 2) - 1) * sampleRateHz / dataLength;
else
frequency = 1d / ((double) (((fftData.length - 1) / 2) - 1) * sampleRateHz / dataLength);
jgen.writeNumberField("frequency", frequency);
jgen.writeEndObject();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Aggregations