use of uk.ac.sussex.gdsc.smlm.engine.FitEngine 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;
}
use of uk.ac.sussex.gdsc.smlm.engine.FitEngine in project GDSC-SMLM by aherbert.
the class PeakFit method run.
/**
* Locate the peaks in the configured image source. Results are saved to the configured output.
*
* <p>This must be called after initialisation with an image source. Note that each call to this
* method must be preceded with initialisation to prepare the image and output options.
*/
@SuppressWarnings("null")
public void run() {
if (source == null) {
return;
}
final int totalFrames = source.getFrames();
final ImageStack stack = (extraSettings.showProcessedFrames) ? new ImageStack(bounds.width, bounds.height) : null;
// Do not crop the region from the source if the bounds match the source dimensions
final Rectangle cropBounds = (bounds.x == 0 && bounds.y == 0 && bounds.width == source.getWidth() && bounds.height == source.getHeight()) ? null : bounds;
// Use the FitEngine to allow multi-threading.
final FitEngine engine = createFitEngine(getNumberOfThreads(totalFrames));
if (engine == null) {
return;
}
final int step = ImageJUtils.getProgressInterval(totalFrames);
// To pre-process data for noise estimation
boolean isFitCameraCounts = false;
CameraModel cameraModel = null;
if (ignoreBoundsForNoise) {
isFitCameraCounts = fitConfig.isFitCameraCounts();
cameraModel = fitConfig.getCameraModel();
}
runTime = System.nanoTime();
boolean shutdown = false;
int slice = 0;
final String format = String.format("Slice: %%d / %d (Results=%%d)", totalFrames);
while (!shutdown) {
// Noise can optionally be estimated from the entire frame
float[] data = (ignoreBoundsForNoise) ? source.next() : source.next(cropBounds);
if (data == null) {
break;
}
if (++slice % step == 0) {
final int frames = slice;
if (ImageJUtils.showStatus(() -> String.format(format, frames, results.size()))) {
IJ.showProgress(slice, totalFrames);
}
}
float noise = Float.NaN;
if (ignoreBoundsForNoise) {
// We must pre-process the data before noise estimation
final float[] data2 = data.clone();
if (isFitCameraCounts) {
cameraModel.removeBias(data2);
} else {
cameraModel.removeBiasAndGain(data2);
}
noise = FitWorker.estimateNoise(data2, source.getWidth(), source.getHeight(), config.getNoiseMethod());
// Crop the data to the region
data = ImageJImageConverter.getData(data, source.getWidth(), source.getHeight(), bounds, null);
}
if (stack != null) {
stack.addSlice(String.format("Frame %d - %d", source.getStartFrameNumber(), source.getEndFrameNumber()), data);
}
// Get the frame number from the source to allow for interlaced and aggregated data
engine.run(createJob(source.getStartFrameNumber(), source.getEndFrameNumber(), data, bounds, noise));
shutdown = escapePressed();
}
engine.end(shutdown);
time = engine.getTime();
runTime = System.nanoTime() - runTime;
if (stack != null) {
ImageJUtils.display("Processed frames", stack);
}
showResults();
source.close();
}
Aggregations