use of uk.ac.sussex.gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.
the class SpotAnalysis method saveSpot.
private void saveSpot() {
if (onFrames.isEmpty() || !updated) {
return;
}
createResultsWindow();
id++;
double signal = 0;
Trace trace = null;
final float psfWidth = Float.parseFloat(widthTextField.getText());
final float cx = areaBounds.x + areaBounds.width / 2.0f;
final float cy = areaBounds.y + areaBounds.height / 2.0f;
for (final Spot s : onFrames) {
// Get the signal again since the background may have changed.
final double spotSignal = getSignal(s.frame);
signal += spotSignal;
final float[] params = Gaussian2DPeakResultHelper.createOneAxisParams(0, (float) (spotSignal), cx, cy, 0, psfWidth);
final PeakResult result = new PeakResult(s.frame, (int) cx, (int) cy, 0, 0, 0, 0, params, null);
if (trace == null) {
trace = new Trace(result);
} else {
trace.add(result);
}
}
if (trace == null) {
return;
}
final Statistics tOn = Statistics.create(trace.getOnTimes());
final Statistics tOff = Statistics.create(trace.getOffTimes());
resultsWindow.append(String.format("%d\t%.1f\t%.1f\t%s\t%s\t%s\t%d\t%s\t%s\t%s", id, cx, cy, MathUtils.rounded(signal, 4), MathUtils.rounded(tOn.getSum() * msPerFrame, 3), MathUtils.rounded(tOff.getSum() * msPerFrame, 3), trace.getBlinks() - 1, MathUtils.rounded(tOn.getMean() * msPerFrame, 3), MathUtils.rounded(tOff.getMean() * msPerFrame, 3), imp.getTitle()));
// Save the individual on/off times for use in creating a histogram
traces.put(id, trace);
updated = false;
}
use of uk.ac.sussex.gdsc.smlm.results.PeakResult 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.PeakResult in project GDSC-SMLM by aherbert.
the class DensityImage method cropBorder.
/**
* Remove any results which fall in the radius border added around the ROI. Results are modified
* in place and a new density array is returned.
*
* @param results the results
* @param density the density
* @return the density array
*/
private int[] cropBorder(MemoryPeakResults results, int[] density) {
if (roiBounds == null) {
return density;
}
final float minX = (int) (scaledRoiMinX);
final float maxX = (int) Math.ceil(scaledRoiMaxX);
final float minY = (int) (scaledRoiMinY);
final float maxY = (int) Math.ceil(scaledRoiMaxY);
// Clone the results then add back those that are within the bounds
final PeakResult[] peakResults = results.toArray();
results.begin();
int count = 0;
for (int i = 0; i < peakResults.length; i++) {
final PeakResult peakResult = peakResults[i];
final float x = peakResult.getXPosition();
final float y = peakResult.getYPosition();
if (x < minX || x > maxX || y < minY || y > maxY) {
continue;
}
results.add(peakResult);
density[count++] = density[i];
}
results.end();
results.setBounds(new Rectangle((int) minX, (int) minY, (int) (maxX - minX), (int) (maxY - minY)));
return Arrays.copyOf(density, count);
}
use of uk.ac.sussex.gdsc.smlm.results.PeakResult 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);
}
use of uk.ac.sussex.gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.
the class CropResults method cropResults.
/**
* Apply the filters to the data.
*/
private void cropResults() {
final MemoryPeakResults newResults = createNewResults();
// These bounds are integer. But this is because the results are meant to come from an image.
final Rectangle integerBounds = results.getBounds(true);
// The crop bounds can be floating point...
// Border
final double border = settings.getBorder();
final double xx = integerBounds.x + border;
final double yy = integerBounds.y + border;
final double w = Math.max(0, integerBounds.width - 2 * border);
final double h = Math.max(0, integerBounds.height - 2 * border);
Rectangle2D pixelBounds = new Rectangle2D.Double(xx, yy, w, h);
// Bounding box
if (settings.getSelectRegion()) {
final Rectangle2D boxBounds = new Rectangle2D.Double(settings.getX(), settings.getY(), settings.getWidth(), settings.getHeight());
pixelBounds = pixelBounds.createIntersection(boxBounds);
}
// and create another intersection
if (myUseRoi) {
final ImagePlus imp = WindowManager.getImage(settings.getRoiImage());
if (imp != null && imp.getRoi() != null) {
final Rectangle roi = imp.getRoi().getBounds();
final int roiImageWidth = imp.getWidth();
final int roiImageHeight = imp.getHeight();
final double xscale = (double) roiImageWidth / integerBounds.width;
final double yscale = (double) roiImageHeight / integerBounds.height;
final Rectangle2D roiBounds = new Rectangle2D.Double(roi.x / xscale, roi.y / yscale, roi.width / xscale, roi.height / yscale);
pixelBounds = pixelBounds.createIntersection(roiBounds);
}
}
final Rectangle2D bounds = pixelBounds;
final Predicate<PeakResult> testZ = getZFilter();
// Copy the results if the origin is reset
final Consumer<PeakResult> consumer = settings.getResetOrigin() ? r -> newResults.add(r.copy()) : newResults::add;
if (bounds.getWidth() > 0 && bounds.getHeight() > 0) {
results.forEach(DistanceUnit.PIXEL, (XyrResultProcedure) (x, y, result) -> {
if (bounds.contains(x, y) && testZ.test(result)) {
consumer.accept(result);
}
});
}
if (settings.getPreserveBounds()) {
newResults.setBounds(integerBounds);
} else {
// Get the lower and upper integer limits
final int x = (int) Math.floor(bounds.getX());
final int y = (int) Math.floor(bounds.getY());
final int ux = (int) Math.ceil(bounds.getX() + bounds.getWidth());
final int uy = (int) Math.ceil(bounds.getY() + bounds.getHeight());
// Ensure the width and height are at least 1
newResults.setBounds(new Rectangle(x, y, Math.max(1, ux - x), Math.max(1, uy - y)));
if (settings.getResetOrigin()) {
newResults.translate(-x, -y);
}
}
IJ.showStatus(newResults.size() + " Cropped localisations");
}
Aggregations