use of uk.ac.sussex.gdsc.smlm.results.filter.BasePreprocessedPeakResult in project GDSC-SMLM by aherbert.
the class BenchmarkSpotFit method addToStats.
private static void addToStats(uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFitResult.FitResult fitResult, StoredDataStatistics[][] stats) {
if (fitResult == null) {
return;
}
final FitResult actualFitResult = (FitResult) fitResult.getData();
if (fitResult.status != 0) {
// Add the evaluations for spots that were not OK
stats[0][FILTER_ITERATIONS].add(actualFitResult.getIterations());
stats[0][FILTER_EVALUATIONS].add(actualFitResult.getEvaluations());
return;
}
if (fitResult.getResults() == null) {
return;
}
boolean isMatch = false;
for (int resultIndex = 0; resultIndex < fitResult.getResults().length; resultIndex++) {
final BasePreprocessedPeakResult result = (BasePreprocessedPeakResult) fitResult.getResults()[resultIndex];
// Q. Only build stats on new results?
if (!result.isNewResult()) {
continue;
}
// This was fit - Get statistics
final double precision = Math.sqrt(result.getLocationVariance());
final double signal = result.getSignal();
final double snr = result.getSnr();
final double width = result.getXSdFactor();
final double xShift = result.getXRelativeShift2();
final double yShift = result.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);
stats[0][FILTER_SIGNAL].add(signal);
stats[0][FILTER_SNR].add(snr);
if (width < 1) {
stats[0][FILTER_MIN_WIDTH].add(width);
} else {
stats[0][FILTER_MAX_WIDTH].add(width);
}
stats[0][FILTER_SHIFT].add(shift);
stats[0][FILTER_ESHIFT].add(eshift);
stats[0][FILTER_PRECISION].add(precision);
if (resultIndex == 0) {
stats[0][FILTER_ITERATIONS].add(actualFitResult.getIterations());
stats[0][FILTER_EVALUATIONS].add(actualFitResult.getEvaluations());
}
// Add to the TP or FP stats
// If it has assignments then it was a match to something
isMatch |= result.hasAssignments();
final int index = (result.hasAssignments()) ? 1 : 2;
stats[index][FILTER_SIGNAL].add(signal);
stats[index][FILTER_SNR].add(snr);
if (width < 1) {
stats[index][FILTER_MIN_WIDTH].add(width);
} else {
stats[index][FILTER_MAX_WIDTH].add(width);
}
stats[index][FILTER_SHIFT].add(shift);
stats[index][FILTER_ESHIFT].add(eshift);
stats[index][FILTER_PRECISION].add(precision);
if (resultIndex == 0) {
// Nothing else at current
}
}
final int index = (isMatch) ? 1 : 2;
stats[index][FILTER_ITERATIONS].add(actualFitResult.getIterations());
stats[index][FILTER_EVALUATIONS].add(actualFitResult.getEvaluations());
}
use of uk.ac.sussex.gdsc.smlm.results.filter.BasePreprocessedPeakResult in project GDSC-SMLM by aherbert.
the class BenchmarkSpotFit method store.
private static int store(uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFitResult.FitResult fitResult, int count, PreprocessedPeakResult[] preprocessedPeakResults) {
if (fitResult == null || fitResult.getResults() == null) {
return count;
}
for (int i = 0; i < fitResult.getResults().length; i++) {
final BasePreprocessedPeakResult result = (BasePreprocessedPeakResult) fitResult.getResults()[i];
if (result.isNewResult()) {
result.uniqueId = count++;
preprocessedPeakResults[result.uniqueId] = result;
}
}
return count;
}
use of uk.ac.sussex.gdsc.smlm.results.filter.BasePreprocessedPeakResult in project GDSC-SMLM by aherbert.
the class BenchmarkFilterAnalysis method showOverlay.
/**
* Show overlay.
*
* <ul>
*
* <li>Green = TP
*
* <li>Red = FP
*
* <li>Magenta = FP (Ignored from analysis)
*
* <li>Yellow = FN
*
* <li>Orange = FN (Outside border)
*
* </ul>
*
* @param allAssignments The assignments generated from running the filter (or null)
* @param filter the filter
* @return The results from running the filter (or null)
*/
@Nullable
@SuppressWarnings("null")
private PreprocessedPeakResult[] showOverlay(ArrayList<FractionalAssignment[]> allAssignments, DirectFilter filter) {
final ImagePlus imp = CreateData.getImage();
if (imp == null) {
return null;
}
// Run the filter manually to get the results that pass.
if (allAssignments == null) {
allAssignments = getAssignments(filter);
}
final Overlay o = new Overlay();
// Do TP
final TIntHashSet actual = new TIntHashSet();
final TIntHashSet predicted = new TIntHashSet();
for (final FractionalAssignment[] assignments : allAssignments) {
if (assignments == null || assignments.length == 0) {
continue;
}
float[] tx = null;
float[] ty = null;
int count = 0;
if (settings.showTP) {
tx = new float[assignments.length];
ty = new float[assignments.length];
}
int frame = 0;
for (int i = 0; i < assignments.length; i++) {
final CustomFractionalAssignment c = (CustomFractionalAssignment) assignments[i];
final UniqueIdPeakResult peak = (UniqueIdPeakResult) c.peak;
final BasePreprocessedPeakResult spot = (BasePreprocessedPeakResult) c.peakResult;
actual.add(peak.uniqueId);
predicted.add(spot.getUniqueId());
frame = spot.getFrame();
if (settings.showTP) {
tx[count] = spot.getX();
ty[count++] = spot.getY();
}
}
if (settings.showTP) {
SpotFinderPreview.addRoi(frame, o, tx, ty, count, Color.green);
}
}
float[] x = new float[10];
float[] y = new float[x.length];
float[] x2 = new float[10];
float[] y2 = new float[x2.length];
// Do FP (all remaining results that are not a TP)
PreprocessedPeakResult[] filterResults = null;
if (settings.showFP) {
final MultiPathFilter multiPathFilter = createMpf(filter, defaultMinimalFilter);
filterResults = filterResults(multiPathFilter);
int frame = 0;
int c1 = 0;
int c2 = 0;
for (int i = 0; i < filterResults.length; i++) {
if (frame != filterResults[i].getFrame()) {
if (c1 != 0) {
SpotFinderPreview.addRoi(frame, o, x, y, c1, Color.red);
}
if (c2 != 0) {
SpotFinderPreview.addRoi(frame, o, x2, y2, c2, Color.magenta);
}
c1 = c2 = 0;
}
frame = filterResults[i].getFrame();
if (predicted.contains(filterResults[i].getUniqueId())) {
continue;
}
if (filterResults[i].ignore()) {
if (x2.length == c2) {
x2 = Arrays.copyOf(x2, c2 * 2);
y2 = Arrays.copyOf(y2, c2 * 2);
}
x2[c2] = filterResults[i].getX();
y2[c2++] = filterResults[i].getY();
} else {
if (x.length == c1) {
x = Arrays.copyOf(x, c1 * 2);
y = Arrays.copyOf(y, c1 * 2);
}
x[c1] = filterResults[i].getX();
y[c1++] = filterResults[i].getY();
}
}
if (c1 != 0) {
SpotFinderPreview.addRoi(frame, o, x, y, c1, Color.red);
}
if (c2 != 0) {
SpotFinderPreview.addRoi(frame, o, x2, y2, c2, Color.magenta);
}
}
// Do FN (all remaining peaks that have not been matched)
if (settings.showFN) {
final boolean checkBorder = (filterResult.analysisBorder != null && filterResult.analysisBorder.x != 0);
final float border;
final float xlimit;
final float ylimit;
if (checkBorder) {
final Rectangle lastAnalysisBorder = filterResult.analysisBorder;
border = lastAnalysisBorder.x;
xlimit = lastAnalysisBorder.x + lastAnalysisBorder.width;
ylimit = lastAnalysisBorder.y + lastAnalysisBorder.height;
} else {
border = xlimit = ylimit = 0;
}
// Add the results to the lists
actualCoordinates.forEachEntry(new CustomTIntObjectProcedure(x, y, x2, y2) {
@Override
public boolean execute(int frame, UniqueIdPeakResult[] results) {
int c1 = 0;
int c2 = 0;
if (x.length <= results.length) {
x = new float[results.length];
y = new float[results.length];
}
if (x2.length <= results.length) {
x2 = new float[results.length];
y2 = new float[results.length];
}
for (int i = 0; i < results.length; i++) {
// Ignore those that were matched by TP
if (actual.contains(results[i].uniqueId)) {
continue;
}
if (checkBorder && outsideBorder(results[i], border, xlimit, ylimit)) {
x2[c2] = results[i].getXPosition();
y2[c2++] = results[i].getYPosition();
} else {
x[c1] = results[i].getXPosition();
y[c1++] = results[i].getYPosition();
}
}
if (c1 != 0) {
SpotFinderPreview.addRoi(frame, o, x, y, c1, Color.yellow);
}
if (c2 != 0) {
SpotFinderPreview.addRoi(frame, o, x2, y2, c2, Color.orange);
}
return true;
}
});
}
imp.setOverlay(o);
return filterResults;
}
Aggregations