use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class ResultsMatchCalculator method savePairs.
private void savePairs(MemoryPeakResults results1, MemoryPeakResults results2, List<PointPair> allMatches) {
if (!settings.savePairs) {
return;
}
// Get the directory
final String directory = ImageJUtils.getDirectory("Pairs_directory", settings.pairsDirectory);
if (directory == null) {
return;
}
settings.pairsDirectory = directory;
final double[] distanceThresholds = getDistances(settings.distanceThreshold, settings.increments, settings.delta);
// Create output files for each distance band
final PeakResults[] output1 = new PeakResults[distanceThresholds.length];
final PeakResults[] output2 = new PeakResults[distanceThresholds.length];
double high = 0;
for (int i = 0; i < distanceThresholds.length; i++) {
final double low = high;
high = distanceThresholds[i];
output1[i] = createFilePeakResults(directory, 1, results1, low, high);
output2[i] = createFilePeakResults(directory, 2, results2, low, high);
}
// Square the thresholds
SimpleArrayUtils.apply(distanceThresholds, v -> v * v);
final double[] pairDistances = getPairDistances(allMatches);
int index = 0;
for (final PointPair pair : allMatches) {
final int insert = search(distanceThresholds, pairDistances[index++]);
if (insert != -1) {
final PeakResult r1 = ((PeakResultPoint) pair.getPoint1()).getPeakResult();
final PeakResult r2 = ((PeakResultPoint) pair.getPoint2()).getPeakResult();
output1[insert].add(r1);
output2[insert].add(r2);
}
}
for (int i = 0; i < output1.length; i++) {
output1[i].end();
output2[i].end();
}
}
use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class ResultsMatchCalculator method run.
@Override
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
final MemoryPeakResults results1 = ResultsManager.loadInputResults(settings.inputOption1, false, null, null);
final MemoryPeakResults results2 = ResultsManager.loadInputResults(settings.inputOption2, false, null, null);
IJ.showStatus("");
if (results1 == null || results1.size() == 0) {
IJ.error(TITLE, "No results 1 could be loaded");
return;
}
if (results2 == null || results2.size() == 0) {
IJ.error(TITLE, "No results 2 could be loaded");
return;
}
if (results1.getDistanceUnit() != results2.getDistanceUnit()) {
IJ.error(TITLE, "Distance unit should be the same for the results");
return;
}
final long start = System.nanoTime();
runCompareCoordinates(results1, results2);
final long nanoseconds = System.nanoTime() - start;
IJ.showStatus(String.format("%s = %s", TITLE, TextUtils.nanosToString(nanoseconds)));
}
use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class TraceDiffusion method getTraces.
private Trace[] getTraces(ArrayList<MemoryPeakResults> allResults) {
this.results = allResults.get(0);
// Results should be checked for calibration by this point
exposureTime = results.getCalibrationReader().getExposureTime() / 1000;
final Function<MemoryPeakResults, Trace[]> traceFunction = createTraceFunction();
final ArrayList<Trace> allTraces = new ArrayList<>();
additionalDatasets = -1;
for (final MemoryPeakResults r : allResults) {
additionalDatasets++;
Trace[] traces = traceFunction.apply(r);
traces = filterTraces(r.getName(), traces, clusteringSettings.getMinimumTraceLength(), clusteringSettings.getIgnoreEnds());
allTraces.addAll(Arrays.asList(traces));
// --- Save results ---
if (traces.length != 0) {
// Save the traces to memory
TraceMolecules.saveResults(r, traces, "Tracks");
if (clusteringSettings.getSaveTraces()) {
// Sort traces by time to assist the results source in extracting frames sequentially.
// Do this before saving to assist in debugging using the saved traces file.
TraceMolecules.sortByTime(traces);
final String newFilename = TraceMolecules.saveTraces(r, traces, createSettingsComment(), settings.tracesFilename, additionalDatasets);
// Only keep the main filename in memory
if (additionalDatasets == 0) {
settings.tracesFilename = newFilename;
}
}
}
}
final Trace[] all = allTraces.toArray(new Trace[0]);
if (additionalDatasets != 0) {
ImageJUtils.log("Multiple inputs provide %d traces", allTraces.size());
final MemoryPeakResults tracedResults = TraceManager.toPeakResults(all, results.getCalibration(), true);
tracedResults.copySettings(results);
tracedResults.setName(createCombinedName() + " Tracks");
MemoryPeakResults.addResults(tracedResults);
}
return all;
}
use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class DriftCalculator method run.
@Override
public void run(String arg) {
SmlmUsageTracker.recordPlugin(this.getClass(), arg);
// Require some fit results and selected regions
if (MemoryPeakResults.isMemoryEmpty()) {
IJ.error(TITLE, "There are no fitting results in memory");
return;
}
final Roi[] rois = getRois();
final String[] stackTitles = createStackImageList();
if (!showDialog(rois, stackTitles)) {
return;
}
final MemoryPeakResults results = ResultsManager.loadInputResults(settings.inputOption, false, DistanceUnit.PIXEL, null);
if (results == null || results.size() < 2) {
IJ.error(TITLE, "There are not enough fitting results for drift correction");
return;
}
double[][] drift = null;
final int[] limits = findTimeLimits(results);
try {
if (settings.method.equals(Settings.MARKED_ROIS)) {
drift = calculateUsingMarkers(results, limits, rois);
} else if (settings.method.equals(Settings.STACK_ALIGNMENT)) {
final ImageStack stack = showStackDialog(stackTitles);
if (stack == null) {
return;
}
drift = calculateUsingImageStack(stack, limits);
} else if (settings.method.equals(Settings.DRIFT_FILE)) {
drift = calculateUsingDriftFile(limits);
} else {
if (!showSubImageDialog()) {
return;
}
drift = calculateUsingFrames(results, limits, Integer.parseInt(settings.reconstructionSize));
}
} finally {
if (executor != null) {
executor.shutdown();
}
}
if (drift == null) {
return;
}
ImageJUtils.log("Drift correction interpolated for frames [%d - %d] of [%d - %d] (%s%%)", interpolationStart, interpolationEnd, limits[0], limits[1], MathUtils.rounded((100.0 * (interpolationEnd - interpolationStart + 1)) / (limits[1] - limits[0] + 1)));
applyDriftCorrection(results, drift);
}
use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class DiffusionRateTest method aggregateIntoFrames.
private void aggregateIntoFrames(ArrayList<Point> points, boolean addError, double precisionInPixels, NormalizedGaussianSampler gauss) {
if (myAggregateSteps < 1) {
return;
}
final MemoryPeakResults results = new MemoryPeakResults(points.size() / myAggregateSteps);
results.setCalibration(CalibrationHelper.create(settings.getPixelPitch(), 1, myAggregateSteps * 1000.0 / settings.getStepsPerSecond()));
results.setName(TITLE + " Aggregated");
results.setPsf(PsfHelper.create(PSFType.CUSTOM));
MemoryPeakResults.addResults(results);
simulation.dataset[1] = results.getName();
int id = 0;
int peak = 1;
int number = 0;
double cx = 0;
double cy = 0;
// Get the mean square distance
double sum = 0;
int count = 0;
PeakResult last = null;
for (final Point result : points) {
final boolean newId = result.id != id;
if (number >= myAggregateSteps || newId) {
if (number != 0) {
double[] xyz = new double[] { cx / number, cy / number };
if (addError) {
xyz = addError(xyz, precisionInPixels, gauss);
}
final float[] params = PeakResult.createParams(0, number, (float) xyz[0], (float) xyz[1], 0);
final float noise = 0.1f;
final PeakResult r = new ExtendedPeakResult(peak, (int) params[PeakResult.X], (int) params[PeakResult.Y], number, 0, noise, 0, params, null, peak, id);
results.add(r);
if (last != null) {
sum += last.distance2(r);
count++;
}
last = r;
number = 0;
cx = cy = 0;
peak++;
}
if (newId) {
// Increment the frame so that tracing analysis can distinguish traces
peak++;
last = null;
id = result.id;
}
}
number++;
cx += result.x;
cy += result.y;
}
// Final peak
if (number != 0) {
double[] xyz = new double[] { cx / number, cy / number };
if (addError) {
xyz = addError(xyz, precisionInPixels, gauss);
}
final float[] params = PeakResult.createParams(0, number, (float) xyz[0], (float) xyz[1], 0);
final float noise = 0.1f;
final PeakResult r = new ExtendedPeakResult(peak, (int) params[PeakResult.X], (int) params[PeakResult.Y], number, 0, noise, 0, params, null, peak, id);
results.add(r);
if (last != null) {
sum += last.distance2(r);
count++;
}
}
// MSD in pixels^2 / frame
final double msd = sum / count;
// Convert to um^2/second
ImageJUtils.log("Aggregated data D=%s um^2/s, Precision=%s nm, N=%d, step=%s s, mean=%s um^2, " + "MSD = %s um^2/s", MathUtils.rounded(settings.getDiffusionRate()), MathUtils.rounded(myPrecision), count, MathUtils.rounded(results.getCalibrationReader().getExposureTime() / 1000), MathUtils.rounded(msd / conversionFactor), MathUtils.rounded((msd / conversionFactor) / (results.getCalibrationReader().getExposureTime() / 1000)));
msdAnalysis(points);
}
Aggregations