Search in sources :

Example 1 with FitEngineSettings

use of uk.ac.sussex.gdsc.smlm.data.config.FitProtos.FitEngineSettings in project GDSC-SMLM by aherbert.

the class SettingsManager method readFitEngineConfiguration.

/**
 * Read the FitEngineConfiguration from the settings file in the settings directory. This loads
 * the current Calibration, PSF and FitEngineSettings.
 *
 * @param flags the flags
 * @return the FitEngineConfiguration
 */
public static FitEngineConfiguration readFitEngineConfiguration(int flags) {
    final FitEngineSettings fitEngineSettings = readFitEngineSettings(flags);
    final Calibration calibration = readCalibration(flags);
    final PSF psf = readPsf(flags);
    return new FitEngineConfiguration(fitEngineSettings, calibration, psf);
}
Also used : PSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF) FitEngineConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration) Calibration(uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.Calibration) FitEngineSettings(uk.ac.sussex.gdsc.smlm.data.config.FitProtos.FitEngineSettings)

Example 2 with FitEngineSettings

use of uk.ac.sussex.gdsc.smlm.data.config.FitProtos.FitEngineSettings in project GDSC-SMLM by aherbert.

the class PsfDrift method run.

@Override
public void run(String arg) {
    SmlmUsageTracker.recordPlugin(this.getClass(), arg);
    if ("hwhm".equals(arg)) {
        showHwhm();
        return;
    }
    // Build a list of suitable images
    final List<String> titles = createImageList(true);
    if (titles.isEmpty()) {
        IJ.error(TITLE, "No suitable PSF images");
        return;
    }
    final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
    settings = Settings.load();
    fitConfig = fitConfigRef.get().createCopy();
    gd.addMessage("Select the input PSF image");
    gd.addChoice("PSF", titles.toArray(new String[0]), settings.title);
    gd.addCheckbox("Use_offset", settings.useOffset);
    gd.addNumericField("Scale", settings.scale, 2);
    gd.addNumericField("z_depth", settings.zDepth, 2, 6, "nm");
    gd.addNumericField("Grid_size", settings.gridSize, 0);
    gd.addSlider("Recall_limit", 0.01, 1, settings.recallLimit);
    gd.addSlider("Region_size", 2, 20, settings.regionSize);
    gd.addCheckbox("Background_fitting", settings.backgroundFitting);
    PeakFit.addPsfOptions(gd, fitConfig);
    gd.addChoice("Fit_solver", SettingsManager.getFitSolverNames(), FitProtosHelper.getName(fitConfig.getFitSolver()));
    // We need these to set bounds for any bounded fitters
    gd.addSlider("Min_width_factor", 0, 0.99, fitConfig.getMinWidthFactor());
    gd.addSlider("Width_factor", 1, 4.5, fitConfig.getMaxWidthFactor());
    gd.addCheckbox("Offset_fit", settings.offsetFitting);
    gd.addNumericField("Start_offset", settings.startOffset, 3);
    gd.addCheckbox("Include_CoM_fit", settings.comFitting);
    gd.addCheckbox("Use_sampling", settings.useSampling);
    gd.addNumericField("Photons", settings.photons, 0);
    gd.addSlider("Photon_limit", 0, 1, settings.photonLimit);
    gd.addSlider("Smoothing", 0, 0.5, settings.smoothing);
    gd.addHelp(HelpUrls.getUrl("psf-drift"));
    gd.showDialog();
    if (gd.wasCanceled()) {
        return;
    }
    settings.title = gd.getNextChoice();
    settings.useOffset = gd.getNextBoolean();
    settings.scale = gd.getNextNumber();
    settings.zDepth = gd.getNextNumber();
    settings.gridSize = (int) gd.getNextNumber();
    settings.recallLimit = gd.getNextNumber();
    settings.regionSize = (int) Math.abs(gd.getNextNumber());
    settings.backgroundFitting = gd.getNextBoolean();
    fitConfig.setPsfType(PeakFit.getPsfTypeValues()[gd.getNextChoiceIndex()]);
    // Some enum values are not supported
    fitConfig.setFitSolver(SettingsManager.getFitSolverValues()[gd.getNextChoiceIndex()]);
    fitConfig.setMinWidthFactor(gd.getNextNumber());
    fitConfig.setMaxWidthFactor(gd.getNextNumber());
    settings.offsetFitting = gd.getNextBoolean();
    settings.startOffset = Math.abs(gd.getNextNumber());
    settings.comFitting = gd.getNextBoolean();
    settings.useSampling = gd.getNextBoolean();
    settings.photons = Math.abs(gd.getNextNumber());
    settings.photonLimit = Math.abs(gd.getNextNumber());
    settings.smoothing = Math.abs(gd.getNextNumber());
    settings.save();
    fitConfigRef.set(fitConfig);
    gd.collectOptions();
    if (!settings.comFitting && !settings.offsetFitting) {
        IJ.error(TITLE, "No initial fitting positions");
        return;
    }
    if (settings.regionSize < 1) {
        settings.regionSize = 1;
    }
    if (gd.invalidNumber()) {
        return;
    }
    imp = WindowManager.getImage(settings.title);
    if (imp == null) {
        IJ.error(TITLE, "No PSF image for image: " + settings.title);
        return;
    }
    psfSettings = getPsfSettings(imp);
    if (psfSettings == null) {
        IJ.error(TITLE, "No PSF settings for image: " + settings.title);
        return;
    }
    // Configure the fit solver. We must wrap the settings with a
    // FitEngineConfiguration to pass to the PeakFit method
    final FitEngineSettings fitEngineSettings = FitProtosHelper.defaultFitEngineSettings;
    final FitEngineConfiguration config = new FitEngineConfiguration(fitEngineSettings, SettingsManager.readCalibration(0), PsfProtosHelper.defaultOneAxisGaussian2DPSF);
    config.getFitConfiguration().setFitSettings(fitConfig.getFitSettings());
    if (!PeakFit.configurePsfModel(config)) {
        return;
    }
    if (!PeakFit.configureFitSolver(config, IJImageSource.getBounds(imp), null, PeakFit.FLAG_NO_SAVE)) {
        return;
    }
    // Update configuration
    fitConfig = config.getFitConfiguration();
    fitConfigRef.set(fitConfig);
    computeDrift();
}
Also used : FitEngineConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration) NonBlockingExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) FitEngineSettings(uk.ac.sussex.gdsc.smlm.data.config.FitProtos.FitEngineSettings)

Example 3 with FitEngineSettings

use of uk.ac.sussex.gdsc.smlm.data.config.FitProtos.FitEngineSettings in project GDSC-SMLM by aherbert.

the class SpotFinderPreview method run.

@Override
public void run(ImageProcessor ip) {
    if (refreshing) {
        return;
    }
    final Rectangle bounds = ip.getRoi();
    // Only do this if the settings changed
    final Calibration calibration = fitConfig.getCalibration();
    final FitEngineSettings fitEngineSettings = config.getFitEngineSettings();
    final PSF psf = fitConfig.getPsf();
    boolean newCameraModel = filter == null;
    if (!calibration.equals(lastCalibration)) {
        newCameraModel = true;
        // Set a camera model.
        // We have to set the camera type too to avoid configuration errors.
        CameraModel cameraModel = CameraModelManager.load(fitConfig.getCameraModelName());
        if (cameraModel == null) {
            cameraModel = new FakePerPixelCameraModel(0, 1, 1);
            fitConfig.setCameraType(CameraType.EMCCD);
        } else {
            fitConfig.setCameraType(CameraType.SCMOS);
            // Support cropped origin selection.
            final Rectangle sourceBounds = IJImageSource.getBounds(imp);
            cameraModel = PeakFit.cropCameraModel(cameraModel, sourceBounds, null, true);
            if (cameraModel == null) {
                gd.getPreviewCheckbox().setState(false);
                return;
            }
        }
        fitConfig.setCameraModel(cameraModel);
    }
    if (newCameraModel || !fitEngineSettings.equals(lastFitEngineSettings) || !psf.equals(lastPsf)) {
        // Configure a jury filter
        if (config.getDataFilterType() == DataFilterType.JURY && !PeakFit.configureDataFilter(config, PeakFit.FLAG_NO_SAVE)) {
            gd.getPreviewCheckbox().setState(false);
            return;
        }
        try {
            filter = config.createSpotFilter();
        } catch (final Exception ex) {
            filter = null;
            this.imp.setOverlay(overlay);
            // Required for ImageJ to disable the preview
            throw new IllegalStateException("Unable to create spot filter", ex);
        }
        ImageJUtils.log(filter.getDescription());
    }
    lastCalibration = calibration;
    lastFitEngineSettings = fitEngineSettings;
    lastPsf = psf;
    // This code can probably be removed since the crop is done above.
    if (fitConfig.getCameraTypeValue() == CameraType.SCMOS_VALUE) {
        // Instead just warn if the roi cannot be extracted from the selected model
        // or there is a mismatch
        final Rectangle modelBounds = fitConfig.getCameraModel().getBounds();
        if (modelBounds != null) {
            if (!modelBounds.contains(bounds)) {
                // @formatter:off
                ImageJUtils.log("WARNING: Camera model bounds [x=%d,y=%d,width=%d,height=%d]" + " does not contain image target bounds [x=%d,y=%d,width=%d,height=%d]", modelBounds.x, modelBounds.y, modelBounds.width, modelBounds.height, bounds.x, bounds.y, bounds.width, bounds.height);
            // @formatter:on
            // Warn if the model bounds are mismatched than the image as this may be an incorrect
            // selection for the camera model
            } else if (modelBounds.x != 0 || modelBounds.y != 0 || modelBounds.width > ip.getWidth() || modelBounds.height > ip.getHeight()) {
                // @formatter:off
                ImageJUtils.log("WARNING: Probably an incorrect camera model!\n" + "Model bounds [x=%d,y=%d,width=%d,height=%d]\n" + "do not match the image target bounds [width=%d,height=%d].", modelBounds.x, modelBounds.y, modelBounds.width, modelBounds.height, ip.getWidth(), ip.getHeight());
            // @formatter:on
            }
        }
    }
    run(ip, filter);
}
Also used : PSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF) FakePerPixelCameraModel(uk.ac.sussex.gdsc.smlm.model.camera.FakePerPixelCameraModel) CameraModel(uk.ac.sussex.gdsc.smlm.model.camera.CameraModel) Rectangle(java.awt.Rectangle) Calibration(uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.Calibration) FitEngineSettings(uk.ac.sussex.gdsc.smlm.data.config.FitProtos.FitEngineSettings) FakePerPixelCameraModel(uk.ac.sussex.gdsc.smlm.model.camera.FakePerPixelCameraModel)

Aggregations

FitEngineSettings (uk.ac.sussex.gdsc.smlm.data.config.FitProtos.FitEngineSettings)3 Calibration (uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.Calibration)2 PSF (uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF)2 FitEngineConfiguration (uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration)2 Rectangle (java.awt.Rectangle)1 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)1 NonBlockingExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog)1 CameraModel (uk.ac.sussex.gdsc.smlm.model.camera.CameraModel)1 FakePerPixelCameraModel (uk.ac.sussex.gdsc.smlm.model.camera.FakePerPixelCameraModel)1