use of uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure in project GDSC-SMLM by aherbert.
the class DriftCalculator method applyDriftCorrection.
private void applyDriftCorrection(MemoryPeakResults results, double[][] drift) {
final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
gd.addMessage("Apply drift correction to in-memory results?");
gd.addChoice("Update_method", Settings.UPDATE_METHODS, settings.updateMethod);
// Option to save the drift unless it was loaded from file
if (!Settings.DRIFT_FILE.equals(settings.method)) {
gd.addCheckbox("Save_drift", settings.saveDrift);
}
gd.showDialog();
if (gd.wasCanceled()) {
return;
}
settings.updateMethod = gd.getNextChoiceIndex();
if (!Settings.DRIFT_FILE.equals(settings.method)) {
settings.saveDrift = gd.getNextBoolean();
saveDrift(calculatedTimepoints, lastdx, lastdy);
}
if (settings.updateMethod == 0) {
return;
}
final double[] dx = drift[0];
final double[] dy = drift[1];
if (settings.updateMethod == 1) {
// Update the results in memory
ImageJUtils.log("Applying drift correction to the results set: " + results.getName());
results.forEach((PeakResultProcedure) result -> {
result.setXPosition((float) (result.getXPosition() + dx[result.getFrame()]));
result.setYPosition((float) (result.getYPosition() + dy[result.getFrame()]));
});
} else {
// Create a new set of results
final MemoryPeakResults newResults = new MemoryPeakResults(results.size());
newResults.copySettings(results);
newResults.setName(results.getName() + " (Corrected)");
MemoryPeakResults.addResults(newResults);
final boolean truncate = settings.updateMethod == 3;
ImageJUtils.log("Creating %sdrift corrected results set: " + newResults.getName(), (truncate) ? "truncated " : "");
results.forEach((PeakResultProcedure) result -> {
if (truncate && (result.getFrame() < interpolationStart || result.getFrame() > interpolationEnd)) {
return;
}
result.setXPosition((float) (result.getXPosition() + dx[result.getFrame()]));
result.setYPosition((float) (result.getYPosition() + dy[result.getFrame()]));
newResults.add(result);
});
}
}
use of uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure in project GDSC-SMLM by aherbert.
the class Filter method filter.
/**
* Filter the results.
*
* <p>The number of consecutive rejections are counted per frame. When the configured number of
* failures is reached all remaining results for the frame are rejected. This assumes the results
* are ordered by the frame.
*
* @param results the results
* @param failures the number of failures to allow per frame before all peaks are rejected
* @return the filtered results
*/
public MemoryPeakResults filter(MemoryPeakResults results, final int failures) {
final MemoryPeakResults newResults = new MemoryPeakResults();
final FrameCounter counter = new FrameCounter();
newResults.copySettings(results);
setup(results);
results.forEach((PeakResultProcedure) peak -> {
counter.advanceAndReset(peak.getFrame());
final boolean isPositive;
if (counter.getCount() > failures) {
isPositive = false;
} else {
isPositive = accept(peak);
}
if (isPositive) {
counter.reset();
newResults.add(peak);
} else {
counter.increment();
}
});
end();
return newResults;
}
use of uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure in project GDSC-SMLM by aherbert.
the class Filter method fractionScoreSubset.
/**
* Filter the results and return the performance score. Allows benchmarking the filter by marking
* the results as true or false.
*
* <p>Input PeakResults must be allocated a score for true positive, false positive, true negative
* and false negative (accessed via the object property get methods). The filter is run and
* results that pass accumulate scores for true positive and false positive, otherwise the scores
* are accumulated for true negative and false negative. The simplest scoring scheme is to mark
* valid results as tp=fn=1 and fp=tn=0 and invalid results the opposite.
*
* <p>The number of consecutive rejections are counted per frame. When the configured number of
* failures is reached all remaining results for the frame are rejected. This assumes the results
* are ordered by the frame.
*
* <p>Note that this method is to be used to score a subset that was generated using
* {@link #filterSubset(MemoryPeakResults, int, double[])} since the number of consecutive
* failures before each peak are expected to be stored in the origX property.
*
* @param resultsList a list of results to analyse
* @param failures the number of failures to allow per frame before all peaks are rejected
* @param tn The initial true negatives (used when the results have been pre-filtered)
* @param fn The initial false negatives (used when the results have been pre-filtered)
* @param initialNegatives The initial negatives (used when the results have been pre-filtered)
* @return the score
*/
public FractionClassificationResult fractionScoreSubset(List<MemoryPeakResults> resultsList, final int failures, double tn, double fn, int initialNegatives) {
final double[] s = new double[4];
s[TN] = tn;
s[FN] = fn;
final Counter p = new Counter();
int negatives = initialNegatives;
for (final MemoryPeakResults peakResults : resultsList) {
setup(peakResults);
final FrameCounter counter = new FrameCounter();
peakResults.forEach((PeakResultProcedure) peak -> {
counter.advanceAndReset(peak.getFrame());
counter.increment(peak.getOrigX());
final boolean isPositive;
if (counter.getCount() > failures) {
isPositive = false;
} else {
isPositive = accept(peak);
}
if (isPositive) {
counter.reset();
} else {
counter.increment();
}
if (isPositive) {
p.increment();
s[TP] += peak.getTruePositiveScore();
s[FP] += peak.getFalsePositiveScore();
} else {
s[FN] += peak.getFalseNegativeScore();
s[TN] += peak.getTrueNegativeScore();
}
});
negatives += peakResults.size();
end();
}
negatives -= p.getCount();
return new FractionClassificationResult(s[TP], s[FP], s[TN], s[FN], p.getCount(), negatives);
}
use of uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure in project GDSC-SMLM by aherbert.
the class Filter method filterSubset2.
/**
* Filter the results.
*
* <p>Input PeakResults must be allocated a score for true positive, false positive, true negative
* and false negative (accessed via the object property get methods). The filter is run and
* results that pass accumulate scores for true positive and false positive, otherwise the scores
* are accumulated for true negative and false negative. The simplest scoring scheme is to mark
* valid results as tp=fn=1 and fp=tn=0 and invalid results the opposite.
*
* <p>The number of consecutive rejections are counted per frame. When the configured number of
* failures is reached all remaining results for the frame are rejected. This assumes the results
* are ordered by the frame.
*
* <p>Note that this method is to be used to score a set of results that may have been extracted
* from a larger set since the number of consecutive failures before each peak are expected to be
* stored in the origY property. Set this to zero and the results should be identical to
* {@link #filterSubset(MemoryPeakResults, double[])}.
*
* <p>The number of failures before each peak is stored in the origX property of the PeakResult.
*
* @param results the results
* @param score If not null will be populated with the fraction score [ tp, fp, tn, fn, p, n ]
* @return the filtered results
*/
public MemoryPeakResults filterSubset2(MemoryPeakResults results, double[] score) {
final MemoryPeakResults newResults = new MemoryPeakResults();
final FrameCounter counter = new FrameCounter();
newResults.copySettings(results);
setup(results);
final double[] s = new double[4];
final Counter p = new Counter();
results.forEach((PeakResultProcedure) peak -> {
counter.advanceAndReset(peak.getFrame());
counter.increment(peak.getOrigY());
final boolean isPositive = accept(peak);
if (isPositive) {
peak.setOrigX(counter.getCount());
counter.reset();
newResults.add(peak);
} else {
counter.increment();
}
if (isPositive) {
p.increment();
s[TP] += peak.getTruePositiveScore();
s[FP] += peak.getFalsePositiveScore();
} else {
s[FN] += peak.getFalseNegativeScore();
s[TN] += peak.getTrueNegativeScore();
}
});
end();
if (score != null && score.length > 5) {
score[0] = s[TP];
score[1] = s[FP];
score[2] = s[TN];
score[3] = s[FN];
score[4] = p.getCount();
score[5] = (double) results.size() - p.getCount();
}
return newResults;
}
use of uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure in project GDSC-SMLM by aherbert.
the class Filter method fractionScore.
/**
* Filter the results and return the performance score. Allows benchmarking the filter by marking
* the results as true or false.
*
* <p>Input PeakResults must be allocated a score for true positive, false positive, true negative
* and false negative (accessed via the object property get methods). The filter is run and
* results that pass accumulate scores for true positive and false positive, otherwise the scores
* are accumulated for true negative and false negative. The simplest scoring scheme is to mark
* valid results as tp=fn=1 and fp=tn=0 and invalid results the opposite.
*
* <p>The number of consecutive rejections are counted per frame. When the configured number of
* failures is reached all remaining results for the frame are rejected. This assumes the results
* are ordered by the frame.
*
* @param resultsList a list of results to analyse
* @param failures the number of failures to allow per frame before all peaks are rejected
* @return the score
*/
public FractionClassificationResult fractionScore(List<MemoryPeakResults> resultsList, final int failures) {
final double[] s = new double[4];
final Counter p = new Counter();
int negatives = 0;
for (final MemoryPeakResults peakResults : resultsList) {
setup(peakResults);
final FrameCounter counter = new FrameCounter();
peakResults.forEach((PeakResultProcedure) peak -> {
counter.advanceAndReset(peak.getFrame());
final boolean isPositive;
if (counter.getCount() > failures) {
isPositive = false;
} else {
isPositive = accept(peak);
}
if (isPositive) {
counter.reset();
} else {
counter.increment();
}
if (isPositive) {
p.increment();
s[TP] += peak.getTruePositiveScore();
s[FP] += peak.getFalsePositiveScore();
} else {
s[FN] += peak.getFalseNegativeScore();
s[TN] += peak.getTrueNegativeScore();
}
});
negatives += peakResults.size();
end();
}
negatives -= p.getCount();
return new FractionClassificationResult(s[TP], s[FP], s[TN], s[FN], p.getCount(), negatives);
}
Aggregations