use of gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.
the class BenchmarkFilterAnalysis method getCoordinates.
private TIntObjectHashMap<IdPeakResult[]> getCoordinates(List<PeakResult> list) {
final TIntObjectHashMap<IdPeakResult[]> coords = new TIntObjectHashMap<IdPeakResult[]>();
if (list.size() > 0) {
// Do not use HashMap directly to build the coords object since there
// will be many calls to getEntry(). Instead sort the results and use
// a new list for each time point
Collections.sort(list);
int last = -1;
int id = 0;
int uniqueId = 0;
ArrayList<PeakResult> tmp = new ArrayList<PeakResult>();
// Add the results to the lists
for (PeakResult p : list) {
if (last != p.getFrame()) {
if (!tmp.isEmpty()) {
coords.put(last, tmp.toArray(new IdPeakResult[tmp.size()]));
}
id = 0;
tmp.clear();
}
last = p.getFrame();
tmp.add(new IdPeakResult(id++, uniqueId++, p));
}
if (!tmp.isEmpty()) {
coords.put(last, tmp.toArray(new IdPeakResult[tmp.size()]));
}
}
return coords;
}
use of gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.
the class BenchmarkSpotFilter method run.
private BenchmarkFilterResult run(FitEngineConfiguration config, boolean relativeDistances, boolean batchSummary) {
if (Utils.isInterrupted())
return null;
MaximaSpotFilter spotFilter = config.createSpotFilter(relativeDistances);
// Extract all the results in memory into a list per frame. This can be cached
if (// || lastRelativeDistances != relativeDistances)
lastId != simulationParameters.id) {
// Always use float coordinates.
// The Worker adds a pixel offset for the spot coordinates.
TIntObjectHashMap<ArrayList<Coordinate>> coordinates = ResultsMatchCalculator.getCoordinates(results.getResults(), false);
actualCoordinates = new TIntObjectHashMap<PSFSpot[]>();
lastId = simulationParameters.id;
//lastRelativeDistances = relativeDistances;
// Store these so we can reset them
final int total = totalProgress;
final String prefix = progressPrefix;
// Spot PSFs may overlap so we must determine the amount of signal overlap and amplitude effect
// for each spot...
IJ.showStatus("Computing PSF overlap ...");
final int nThreads = Prefs.getThreads();
final BlockingQueue<Integer> jobs = new ArrayBlockingQueue<Integer>(nThreads * 2);
List<OverlapWorker> workers = new LinkedList<OverlapWorker>();
List<Thread> threads = new LinkedList<Thread>();
for (int i = 0; i < nThreads; i++) {
OverlapWorker worker = new OverlapWorker(jobs, coordinates);
Thread t = new Thread(worker);
workers.add(worker);
threads.add(t);
t.start();
}
// Process the frames
totalProgress = coordinates.size();
stepProgress = Utils.getProgressInterval(totalProgress);
progress = 0;
coordinates.forEachKey(new TIntProcedure() {
public boolean execute(int value) {
put(jobs, value);
return true;
}
});
// Finish all the worker threads by passing in a null job
for (int i = 0; i < threads.size(); i++) {
put(jobs, -1);
}
// Wait for all to finish
for (int i = 0; i < threads.size(); i++) {
try {
threads.get(i).join();
} catch (InterruptedException e) {
e.printStackTrace();
}
actualCoordinates.putAll(workers.get(i).coordinates);
}
threads.clear();
IJ.showProgress(-1);
IJ.showStatus("");
setupProgress(total, prefix);
}
if (!batchMode)
IJ.showStatus("Computing results ...");
final ImageStack stack = imp.getImageStack();
float background = 0;
if (spotFilter.isAbsoluteIntensity()) {
// To allow the signal factor to be computed we need to lower the image by the background so
// that the intensities correspond to the results amplitude.
// Just assume the background is uniform.
double sum = 0;
for (PeakResult r : results) sum += r.getBackground();
background = (float) (sum / results.size());
}
// Create a pool of workers
final int nThreads = Prefs.getThreads();
BlockingQueue<Integer> jobs = new ArrayBlockingQueue<Integer>(nThreads * 2);
List<Worker> workers = new LinkedList<Worker>();
List<Thread> threads = new LinkedList<Thread>();
for (int i = 0; i < nThreads; i++) {
Worker worker = new Worker(jobs, stack, spotFilter, background);
Thread t = new Thread(worker);
workers.add(worker);
threads.add(t);
t.start();
}
// Fit the frames
for (int i = 1; i <= stack.getSize(); i++) {
put(jobs, i);
}
// Finish all the worker threads by passing in a null job
for (int i = 0; i < threads.size(); i++) {
put(jobs, -1);
}
// Wait for all to finish
for (int i = 0; i < threads.size(); i++) {
try {
threads.get(i).join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
threads.clear();
if (Utils.isInterrupted())
return null;
if (!batchMode) {
IJ.showProgress(-1);
IJ.showStatus("Collecting results ...");
}
TIntObjectHashMap<FilterResult> filterResults = new TIntObjectHashMap<FilterResult>();
time = 0;
for (Worker w : workers) {
time += w.time;
filterResults.putAll(w.results);
}
// Show a table of the results
BenchmarkFilterResult filterResult = summariseResults(filterResults, config, spotFilter, relativeDistances, batchSummary);
if (!batchMode)
IJ.showStatus("");
return filterResult;
}
use of gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.
the class PCPALMMolecules method getLifetime.
/**
* Get the lifetime of the results using the earliest and latest frames and the calibrated exposure time.
*/
private void getLifetime() {
int start;
int end;
List<PeakResult> peakResults = results.getResults();
if (peakResults.isEmpty()) {
seconds = 0;
return;
}
start = end = peakResults.get(0).getFrame();
for (PeakResult r : peakResults) {
if (start > r.getFrame())
start = r.getFrame();
if (end < r.getEndFrame())
end = r.getEndFrame();
}
seconds = (end - start + 1) * results.getCalibration().getExposureTime() / 1000;
}
use of gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.
the class PCPALMMolecules method extractLocalisations.
/**
* Extract molecules for the PC-PALM analysis.
* <p>
* Estimate the localisation uncertainty (precision) of each molecule using the formula of Mortensen, et al (2010),
* Nature Methods 7, 377-381. Store distance in nm and signal in photons using the calibration
*
* @param results
* @return
*/
public ArrayList<Molecule> extractLocalisations(MemoryPeakResults results) {
ArrayList<Molecule> molecules = new ArrayList<Molecule>(results.size());
final double nmPerPixel = results.getNmPerPixel();
final double gain = results.getGain();
final boolean emCCD = results.isEMCCD();
for (PeakResult r : results.getResults()) {
double p = r.getPrecision(nmPerPixel, gain, emCCD);
// Remove EMCCD adjustment
//p /= Math.sqrt(PeakResult.F);
molecules.add(new Molecule(r.getXPosition() * nmPerPixel, r.getYPosition() * nmPerPixel, p, r.getSignal() / gain));
}
return molecules;
}
use of gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.
the class PCPALMMolecules method cropToRoi.
private MemoryPeakResults cropToRoi(MemoryPeakResults results) {
Rectangle bounds = results.getBounds(true);
area = (bounds.width * bounds.height * results.getNmPerPixel() * results.getNmPerPixel()) / 1e6;
if (roiBounds == null) {
return results;
}
// Adjust bounds relative to input results image
double xscale = (double) roiImageWidth / bounds.width;
double yscale = (double) roiImageHeight / bounds.height;
roiBounds.x /= xscale;
roiBounds.width /= xscale;
roiBounds.y /= yscale;
roiBounds.height /= yscale;
float minX = (int) (roiBounds.x);
float maxX = (int) Math.ceil(roiBounds.x + roiBounds.width);
float minY = (int) (roiBounds.y);
float maxY = (int) Math.ceil(roiBounds.y + roiBounds.height);
// Update the area with the cropped region
area *= (maxX - minX) / bounds.width;
area *= (maxY - minY) / bounds.height;
// Create a new set of results within the bounds
MemoryPeakResults newResults = new MemoryPeakResults();
newResults.begin();
for (PeakResult peakResult : results.getResults()) {
float x = peakResult.params[Gaussian2DFunction.X_POSITION];
float y = peakResult.params[Gaussian2DFunction.Y_POSITION];
if (x < minX || x > maxX || y < minY || y > maxY)
continue;
newResults.add(peakResult);
}
newResults.end();
newResults.copySettings(results);
newResults.setBounds(new Rectangle((int) minX, (int) minY, (int) (maxX - minX), (int) (maxY - minY)));
return newResults;
}
Aggregations