use of gdsc.smlm.engine.FitJob in project GDSC-SMLM by aherbert.
the class PeakFit method processResults.
private boolean processResults(FitEngine engine, ArrayList<PeakResult> sliceCandidates, int slice) {
// Process results
int[] maxIndices = new int[sliceCandidates.size()];
int count = 0;
ArrayList<PeakResult> processedResults = new ArrayList<PeakResult>(sliceCandidates.size());
for (PeakResult result : sliceCandidates) {
// Add ExtendedPeakResults to the results if they span multiple frames (they are the result of previous fitting).
if (result instanceof ExtendedPeakResult && result.getFrame() != result.getEndFrame()) {
processedResults.add(result);
} else {
// Fit single frame results.
maxIndices[count++] = result.origX + bounds.width * result.origY;
}
}
if (!processedResults.isEmpty())
this.results.addAll(processedResults);
if (count != 0) {
float[] data = source.get(slice);
if (data == null)
return false;
FitParameters fitParams = new FitParameters();
fitParams.maxIndices = Arrays.copyOf(maxIndices, count);
FitJob job = new ParameterisedFitJob(fitParams, slice, data, bounds);
engine.run(job);
}
return true;
}
use of gdsc.smlm.engine.FitJob in project GDSC-SMLM by aherbert.
the class PSFEstimator method calculateStatistics.
private boolean calculateStatistics(PeakFit fitter, double[] params, double[] params_dev) {
debug(" Fitting PSF");
swapStatistics();
// Create the fit engine using the PeakFit plugin
FitConfiguration fitConfig = config.getFitConfiguration();
fitConfig.setInitialAngle((float) params[0]);
fitConfig.setInitialPeakStdDev0((float) params[1]);
fitConfig.setInitialPeakStdDev1((float) params[2]);
ImageStack stack = imp.getImageStack();
Rectangle roi = stack.getProcessor(1).getRoi();
ImageSource source = new IJImageSource(imp);
// Allow interlaced data by wrapping the image source
if (interlacedData) {
source = new InterlacedImageSource(source, dataStart, dataBlock, dataSkip);
}
// Allow frame aggregation by wrapping the image source
if (integrateFrames > 1) {
source = new AggregatedImageSource(source, integrateFrames);
}
fitter.initialiseImage(source, roi, true);
fitter.addPeakResults(this);
fitter.initialiseFitting();
FitEngine engine = fitter.createFitEngine();
// Use random slices
int[] slices = new int[stack.getSize()];
for (int i = 0; i < slices.length; i++) slices[i] = i + 1;
Random rand = new Random();
rand.shuffle(slices);
IJ.showStatus("Fitting ...");
// Use multi-threaded code for speed
int i;
for (i = 0; i < slices.length; i++) {
int slice = slices[i];
//debug(" Processing slice = %d\n", slice);
IJ.showProgress(size(), settings.numberOfPeaks);
ImageProcessor ip = stack.getProcessor(slice);
// stack processor does not set the bounds required by ImageConverter
ip.setRoi(roi);
FitJob job = new FitJob(slice, ImageConverter.getData(ip), roi);
engine.run(job);
if (sampleSizeReached() || Utils.isInterrupted()) {
break;
}
}
if (Utils.isInterrupted()) {
IJ.showProgress(1);
engine.end(true);
return false;
}
// Wait until we have enough results
while (!sampleSizeReached() && !engine.isQueueEmpty()) {
IJ.showProgress(size(), settings.numberOfPeaks);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
break;
}
}
// End now if we have enough samples
engine.end(sampleSizeReached());
IJ.showStatus("");
IJ.showProgress(1);
// This count will be an over-estimate given that the provider is ahead of the consumer
// in this multi-threaded system
debug(" Processed %d/%d slices (%d peaks)", i, slices.length, size());
setParams(ANGLE, params, params_dev, sampleNew[ANGLE]);
setParams(X, params, params_dev, sampleNew[X]);
setParams(Y, params, params_dev, sampleNew[Y]);
if (settings.showHistograms) {
int[] idList = new int[NAMES.length];
int count = 0;
boolean requireRetile = false;
for (int ii = 0; ii < 3; ii++) {
if (sampleNew[ii].getN() == 0)
continue;
StoredDataStatistics stats = new StoredDataStatistics(sampleNew[ii].getValues());
idList[count++] = Utils.showHistogram(TITLE, stats, NAMES[ii], 0, 0, settings.histogramBins, "Mean = " + Utils.rounded(stats.getMean()) + ". Median = " + Utils.rounded(sampleNew[ii].getPercentile(50)));
requireRetile = requireRetile || Utils.isNewWindow();
}
if (requireRetile && count > 0) {
new WindowOrganiser().tileWindows(Arrays.copyOf(idList, count));
}
}
if (size() < 2) {
log("ERROR: Insufficient number of fitted peaks, terminating ...");
return false;
}
return true;
}
use of gdsc.smlm.engine.FitJob in project GDSC-SMLM by aherbert.
the class PeakFit method createJob.
private FitJob createJob(int slice, int endFrame, float[] data, Rectangle bounds2, float noise) {
FitParameters fitParams = null;
if (slice != endFrame) {
fitParams = new FitParameters();
fitParams.endT = endFrame;
}
if (maximaIdentification) {
if (fitParams == null)
fitParams = new FitParameters();
fitParams.fitTask = FitTask.MAXIMA_IDENITIFICATION;
fitParams.noise = noise;
} else if (!Float.isNaN(noise)) {
if (fitParams == null)
fitParams = new FitParameters();
fitParams.fitTask = FitTask.PSF_FITTING;
fitParams.noise = noise;
}
if (fitParams != null)
return new ParameterisedFitJob(fitParams, slice, data, bounds);
else
return new FitJob(slice, data, bounds);
}
Aggregations