use of uk.ac.sussex.gdsc.smlm.engine.FitParameters in project GDSC-SMLM by aherbert.
the class PeakFit method createMaximaFitJob.
/**
* Creates the fit job to fit all the given candidate maxima.
*
* @param sliceCandidates the slice candidates
* @param startFrame the start frame
* @param endFrame the end frame
* @param data the data
* @param bounds the bounds
* @param noise the noise
* @return the fit job
*/
private static FitJob createMaximaFitJob(int[] maxIndices, int startFrame, int endFrame, float[] data, Rectangle bounds, float noise) {
final FitParameters fitParams = new FitParameters();
fitParams.maxIndices = maxIndices;
if (startFrame != endFrame) {
fitParams.endT = endFrame;
}
fitParams.fitTask = FitTask.PSF_FITTING;
fitParams.noise = noise;
return new ParameterisedFitJob(fitParams, startFrame, data, bounds);
}
use of uk.ac.sussex.gdsc.smlm.engine.FitParameters in project GDSC-SMLM by aherbert.
the class FailCountManager method createData.
/**
* Creates the fail count data by running fitting on the current image.
*/
private void createData() {
final ImagePlus imp = WindowManager.getCurrentImage();
if (imp == null) {
IJ.error(TITLE, "No image for fitting");
return;
}
if (!showCreateDataDialog(imp)) {
return;
}
// Get the current fit configuration
final Configuration c = new Configuration();
if (!c.showDialog(false)) {
return;
}
final FitEngineConfiguration fitConfig = c.getFitEngineConfiguration();
// Update stopping criteria.
fitConfig.resetFailCounter();
fitConfig.setFailuresLimit(settings.getFailCountLimit());
final ImageSource source = new IJImageSource(imp);
final PeakFit peakFit = new PeakFit(fitConfig, ResultsSettings.getDefaultInstance());
peakFit.setResultsSuffix("(FailCountAnalysis)");
if (!peakFit.initialise(source, null, false)) {
IJ.error(TITLE, "Failed to initialise the fit engine");
return;
}
final FitEngine engine = peakFit.createFitEngine();
final Rectangle bounds = new Rectangle(source.getWidth(), source.getHeight());
// Run
final int totalFrames = Math.min(source.getFrames(), settings.getMaxFrames());
final int step = ImageJUtils.getProgressInterval(totalFrames);
IJ.showProgress(0);
boolean shutdown = false;
int slice = 0;
final LocalList<ParameterisedFitJob> jobs = new LocalList<>(totalFrames);
while (!shutdown && slice < totalFrames) {
final float[] data = source.next();
if (data == null) {
break;
}
if (slice++ % step == 0) {
final int frames = slice;
if (ImageJUtils.showStatus(() -> "Fitting slice: " + frames + " / " + totalFrames)) {
IJ.showProgress(slice, totalFrames);
}
}
final ParameterisedFitJob job = createJob(source.getStartFrameNumber(), data, bounds);
jobs.push(job);
engine.run(job);
shutdown = escapePressed();
}
ImageJUtils.showStatus("Extracting fail count data");
engine.end(shutdown);
IJ.showProgress(1);
source.close();
// Extract the fail count data
final LocalList<FailCountData> failCountData = new LocalList<>(jobs.size());
for (int i = 0; i < jobs.size(); i++) {
final ParameterisedFitJob job = jobs.unsafeGet(i);
if (job.getStatus() == Status.FINISHED) {
final FitParameters fitParams = job.getFitParameters();
// Find the last success
boolean[] results = fitParams.pass;
int end = results.length - 1;
while (end > 0 && !results[end]) {
end--;
}
// Add on the configured fail count limit
end = Math.min(end + 1 + settings.getFailCountLimit(), results.length);
results = Arrays.copyOf(results, end);
failCountData.add(new FailCountData(job.getSlice(), results));
}
}
failCountDataRef.set(failCountData);
ImageJUtils.showStatus("");
// Save for the future
if (settings.getSaveAfterFitting()) {
saveData();
}
}
use of uk.ac.sussex.gdsc.smlm.engine.FitParameters in project GDSC-SMLM by aherbert.
the class AstigmatismModelManager method fitRegion.
private boolean fitRegion() {
final int radius = config.getFittingWidth();
if (pluginSettings.getLogFitProgress()) {
fitConfig.setLog(ImageJPluginLoggerHelper.getLogger(getClass()));
}
// Create a fit engine
results = new MemoryPeakResults();
results.setCalibration(fitConfig.getCalibration());
results.setPsf(fitConfig.getPsf());
results.setSortAfterEnd(true);
results.begin();
final int threadCount = Prefs.getThreads();
final FitEngine engine = FitEngine.create(config, SynchronizedPeakResults.create(results, threadCount), threadCount, FitQueue.BLOCKING);
final IJImageSource source = new IJImageSource(imp);
source.open();
final Rectangle r1 = new Rectangle(cx - radius, cy - radius, 2 * radius + 1, 2 * radius + 1);
final Rectangle regionBounds = r1.intersection(new Rectangle(source.getWidth(), source.getHeight()));
// Fit only a spot in the centre
final int x = cx - regionBounds.x;
final int y = cy - regionBounds.y;
final int[] maxIndices = new int[] { y * regionBounds.width + x };
final Ticker ticker = ImageJUtils.createTicker(source.getFrames(), threadCount);
IJ.showStatus("Fitting ...");
boolean shutdown = false;
while (!shutdown) {
// Extract the region from each frame
final float[] region = source.next(regionBounds);
if (region == null) {
break;
}
final FitParameters params = new FitParameters();
params.maxIndices = maxIndices.clone();
final int slice = (int) ticker.getCurrent();
final ParameterisedFitJob job = new ParameterisedFitJob(slice, params, slice, region, regionBounds);
engine.run(job);
ticker.tick();
shutdown = IJ.escapePressed();
}
if (shutdown) {
IJ.showStatus("Cancelled");
}
engine.end(shutdown);
results.end();
IJ.showProgress(1);
if (!shutdown) {
ImageJUtils.log("Fit %d/%s", results.size(), TextUtils.pleural(source.getFrames(), "spot"));
}
return !shutdown;
}
use of uk.ac.sussex.gdsc.smlm.engine.FitParameters in project GDSC-SMLM by aherbert.
the class FailCountManager method createJob.
private static ParameterisedFitJob createJob(int startFrame, float[] data, Rectangle bounds) {
final FitParameters fitParams = new FitParameters();
fitParams.fitTask = FitTask.PSF_FITTING;
// Signal that the fail count should be recorded
fitParams.pass = new boolean[0];
return new ParameterisedFitJob(fitParams, startFrame, data, bounds);
}
use of uk.ac.sussex.gdsc.smlm.engine.FitParameters in project GDSC-SMLM by aherbert.
the class PsfCreator method fitSpot.
private MemoryPeakResults fitSpot(ImageStack stack, final int width, final int height, final int x, final int y) {
Rectangle regionBounds = null;
// Create a fit engine
final MemoryPeakResults results = new MemoryPeakResults();
final FitConfiguration fitConfig = config.getFitConfiguration();
results.setCalibration(fitConfig.getCalibration());
results.setPsf(fitConfig.getPsf());
results.setSortAfterEnd(true);
results.begin();
final int threadCount = Prefs.getThreads();
final FitEngine engine = FitEngine.create(config, SynchronizedPeakResults.create(results, threadCount), threadCount, FitQueue.BLOCKING);
for (int slice = 1; slice <= stack.getSize(); slice++) {
// Extract the region from each frame
final ImageExtractor ie = ImageExtractor.wrap((float[]) stack.getPixels(slice), width, height);
if (regionBounds == null) {
regionBounds = ie.getBoxRegionBounds(x, y, boxRadius);
}
final float[] region = ie.crop(regionBounds);
// Fit only a spot in the centre
final FitParameters params = new FitParameters();
params.maxIndices = new int[] { boxRadius * regionBounds.width + boxRadius };
final ParameterisedFitJob job = new ParameterisedFitJob(slice, params, slice, region, regionBounds);
// jobItems.add(job);
engine.run(job);
}
engine.end(false);
results.end();
return results;
}
Aggregations