Skip to content

Commit

Permalink
Fix output of the trace dataset statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
aherbert committed Aug 21, 2023
1 parent c68608e commit ff73db0
Showing 1 changed file with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,31 @@ private interface ObjIntToDoubleFunction<T> {
double apply(T o, int i);
}

/**
* Custom statistics class to collect mean, standard deviation and max.
*/
private static class MyStatistics {
private final Statistics s = new Statistics();
private double max;

void add(double value) {
s.add(value);
max = max > value ? max : value;
}

double mean() {
return s.getMean();
}

double sd() {
return s.getStandardDeviation();
}

double max() {
return max;
}
}

@Override
public void run(String arg) {
SmlmUsageTracker.recordPlugin(this.getClass(), arg);
Expand Down Expand Up @@ -375,17 +400,23 @@ private boolean showDialog() {
private int[] extractResidenceTimes(List<MemoryPeakResults> allResults) {
final IntArrayList times = new IntArrayList();
final Function<MemoryPeakResults, List<Trace>> traceFunction = createTraceFunction();
final double nmPerPixel = allResults.get(0).getNmPerPixel();
final CalibrationReader cr = allResults.get(0).getCalibrationReader();
final double nmPerPixel = cr.getNmPerPixel();
final double exposureTime = cr.getExposureTime() * 1e-3;
final ToIntFunction<Trace> framesFunction = createFramesFunction(nmPerPixel);

final Logger logger = ImageJPluginLoggerHelper.getLogger(getClass());
for (final MemoryPeakResults r : allResults) {
final List<Trace> traces = traceFunction.apply(r);
final int before = times.size();
final double[] max2 = {0, 0};
final MyStatistics frames = new MyStatistics();
final MyStatistics maxJumps = new MyStatistics();
final MyStatistics meanJumps = new MyStatistics();
traces.forEach(trace -> {
// Collect the largest jump distance and check for duplicate frames
PeakResult p = trace.getHead();
double max = 0;
for (int i = 1; i < trace.size(); i++) {
final PeakResult result = trace.get(i);
if (p.getFrame() == result.getFrame()) {
Expand All @@ -395,21 +426,23 @@ private int[] extractResidenceTimes(List<MemoryPeakResults> allResults) {
return;
}
max2[0] = Math.max(max2[0], p.distance2(result));
max = Math.max(max, p.distance2(result));
p = result;
}
frames.add(trace.getTail().getFrame() - trace.getHead().getFrame() + 1);
maxJumps.add(Math.sqrt(max));
meanJumps.add(trace.getMeanDistance());
max2[1] = Math.max(max2[1], trace.getMeanDistance());

final int t = framesFunction.applyAsInt(trace);
if (t >= 0) {
times.add(t);
}
});
logger.info(
() -> String.format("Dataset %s : %s : %s : Max jump = %s nm : Max mean jump = %s nm",
r.getName(), TextUtils.pleural(traces.size(), "trace"),
TextUtils.pleural(times.size() - before, "time"),
MathUtils.round(Math.sqrt(max2[0]) * nmPerPixel),
MathUtils.round(Math.sqrt(max2[1]) * nmPerPixel)));
logger.info(() -> String.format("Dataset %s : %s : %s : %s sec : %s nm : %s nm", r.getName(),
TextUtils.pleural(traces.size(), "trace"),
TextUtils.pleural(times.size() - before, "time"), format(frames, exposureTime, "Time"),
format(maxJumps, nmPerPixel, "Max jump"), format(meanJumps, nmPerPixel, "Mean jump")));
}

// Convert to histogram
Expand Down Expand Up @@ -518,12 +551,27 @@ public Predicate<Trace> and(Predicate<? super Trace> other) {
return t -> {
if (t.size() > min && traceFilter.test(t)) {
// frame length = end - start - offset
// (The shortest allowed trace should return 0)
return t.getTail().getFrame() - t.getHead().getFrame() - min;
}
return -1;
};
}

/**
* Format the statistics for display.
*
* @param stats the stats
* @param scale the scale
* @param name the name
* @return the string
*/
private static String format(MyStatistics stats, double scale, String name) {
return new StringBuilder(name).append(" = ").append(MathUtils.round(stats.mean() * scale))
.append(" +/- ").append(MathUtils.round(stats.sd() * scale)).append(" (")
.append(MathUtils.round(stats.max() * scale)).append(")").toString();
}

/**
* Do a simulation of bound molecules and then run analysis on the data.
*/
Expand Down

0 comments on commit ff73db0

Please sign in to comment.