Search in sources :

Example 1 with PSFType

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

the class PeakResultsReader method simplifyTwoAxis.

/**
 * Simplify two axis Gaussian 2D data.
 *
 * @param results the results
 * @param removeTheta the remove theta flag
 */
private void simplifyTwoAxis(MemoryPeakResults results, boolean removeTheta) {
    final int[] indices = PsfHelper.getGaussian2DWxWyIndices(psf);
    final int isx = indices[0];
    final int isy = indices[1];
    // Columns to remove
    int remove = (removeTheta) ? 1 : 0;
    // New PSF type
    PSFType psfType;
    // Determine if sy is redundant
    if (results.forEach((PeakResultProcedureX) peakResult -> peakResult.getParameter(isx) != peakResult.getParameter(isy))) {
        if (!removeTheta) {
            // Already a TwoAxis Gaussian
            return;
        }
        // Otherwise this was a TwoAxisAndTheta with 1 column to remove
        // so it should be simplified
        psfType = PSFType.TWO_AXIS_GAUSSIAN_2D;
    } else {
        // sy is redundant so remove another column
        psfType = PSFType.ONE_AXIS_GAUSSIAN_2D;
        remove++;
    }
    // Update the PSF
    final PSF.Builder builder = psf.toBuilder();
    builder.setPsfType(psfType);
    psf = builder.build();
    results.setPsf(psf);
    // Update the results.
    // We can directly manipulate the params array
    final int newLength = results.getf(0).getNumberOfParameters() - remove;
    if (deviations) {
        results.forEach((PeakResultProcedure) peakResult -> {
            peakResult.resizeParameters(newLength);
            peakResult.resizeParameterDeviations(newLength);
        });
    } else {
        results.forEach((PeakResultProcedure) peakResult -> peakResult.resizeParameters(newLength));
    }
}
Also used : Rectangle(java.awt.Rectangle) DataInputStream(java.io.DataInputStream) AngleUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.AngleUnit) Calibration(uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.Calibration) PeakResultProcedureX(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedureX) Scanner(java.util.Scanner) PSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF) IntensityUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.IntensityUnit) PSFType(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSFType) TrackProgress(uk.ac.sussex.gdsc.core.logging.TrackProgress) Level(java.util.logging.Level) Matcher(java.util.regex.Matcher) NotNull(uk.ac.sussex.gdsc.core.annotation.NotNull) Locale(java.util.Locale) XStreamUtils(uk.ac.sussex.gdsc.smlm.utils.XStreamUtils) UnicodeReader(uk.ac.sussex.gdsc.core.utils.UnicodeReader) PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) NoSuchElementException(java.util.NoSuchElementException) Statistics(uk.ac.sussex.gdsc.core.utils.Statistics) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) CameraType(uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.CameraType) IOException(java.io.IOException) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) FileInputStream(java.io.FileInputStream) Logger(java.util.logging.Logger) EOFException(java.io.EOFException) TextUtils(uk.ac.sussex.gdsc.core.utils.TextUtils) TimeUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.TimeUnit) Objects(java.util.Objects) BitFlagUtils(uk.ac.sussex.gdsc.core.utils.BitFlagUtils) JsonFormat(com.google.protobuf.util.JsonFormat) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable) FileUtils(uk.ac.sussex.gdsc.core.utils.FileUtils) PsfHelper(uk.ac.sussex.gdsc.smlm.data.config.PsfHelper) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) FileChannel(java.nio.channels.FileChannel) PSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF) PSFType(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSFType)

Example 2 with PSFType

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

the class PeakFit method addPsfOptions.

/**
 * Adds the PSF options.
 *
 * <p>Note that if an astigmatic PSF is selected then the model must be created with
 * {@link #configurePsfModel(FitEngineConfiguration, int)}.
 *
 * @param gd the dialog
 * @param fitConfigurationProvider the fit configuration provider
 */
public static void addPsfOptions(final ExtendedGenericDialog gd, final FitConfigurationProvider fitConfigurationProvider) {
    final FitConfiguration fitConfig = fitConfigurationProvider.getFitConfiguration();
    gd.addChoice("PSF", getPsfTypeNames(), PsfProtosHelper.getName(fitConfig.getPsfType()), new OptionListener<Integer>() {

        @Override
        public boolean collectOptions(Integer field) {
            final FitConfiguration fitConfig = fitConfigurationProvider.getFitConfiguration();
            fitConfig.setPsfType(PeakFit.getPsfTypeValues()[field]);
            return collectOptions(false);
        }

        @Override
        public boolean collectOptions() {
            return collectOptions(true);
        }

        private boolean collectOptions(boolean silent) {
            final FitConfiguration localFitConfig = fitConfigurationProvider.getFitConfiguration();
            final PSFType psfType = localFitConfig.getPsfType();
            final ExtendedGenericDialog egd = new ExtendedGenericDialog("PSF Options", null);
            PSF oldPsf = null;
            if (psfType == PSFType.ASTIGMATIC_GAUSSIAN_2D) {
                // The PSF is entirely defined in the model
                String[] list = AstigmatismModelManager.listAstigmatismModels(false, localFitConfig.getCalibrationReader().getNmPerPixel(), 0.1);
                // In case the calibration has not been updated
                if (list.length == 0) {
                    list = AstigmatismModelManager.listAstigmatismModels(false, true);
                }
                egd.addChoice("Z-model", list, localFitConfig.getPsfModelName());
            } else {
                // Collect the PSF parameters
                oldPsf = localFitConfig.getPsf();
                for (int i = 0; i < oldPsf.getParametersCount(); i++) {
                    final PSFParameter p = oldPsf.getParameters(i);
                    egd.addNumericField(String.format("PSF_parameter_%d (%s)", i + 1, p.getName()), p.getValue(), 3);
                }
                if (psfType == PSFType.ONE_AXIS_GAUSSIAN_2D) {
                    egd.addCheckbox("Fixed", localFitConfig.isFixedPsf());
                }
            }
            egd.setSilent(silent);
            egd.showDialog(true, gd);
            if (egd.wasCanceled()) {
                return false;
            }
            if (psfType == PSFType.ASTIGMATIC_GAUSSIAN_2D) {
                // The PSF is entirely defined in the model
                localFitConfig.setPsfModelName(egd.getNextChoice());
                return true;
            }
            @SuppressWarnings("null") final PSF.Builder b = oldPsf.toBuilder();
            final int n = b.getParametersCount();
            for (int i = 0; i < n; i++) {
                b.getParametersBuilder(i).setValue(egd.getNextNumber());
            }
            final PSF newPsf = b.build();
            localFitConfig.setPsf(newPsf);
            boolean changed = !oldPsf.equals(newPsf);
            if (psfType == PSFType.ONE_AXIS_GAUSSIAN_2D) {
                final boolean newFixed = egd.getNextBoolean();
                changed = changed || (newFixed != localFitConfig.isFixedPsf());
                localFitConfig.setFixedPsf(newFixed);
            }
            return changed;
        }
    });
}
Also used : PSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF) FitConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitConfiguration) PSFParameter(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSFParameter) PSFType(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSFType) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)

Example 3 with PSFType

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

the class FitConfiguration method updatePsf.

private void updatePsf(boolean resetAstigmatismModel) {
    invalidateGaussianFunction();
    // Reset the astigmatism model. It will be dynamically created from the PSF settings.
    if (resetAstigmatismModel) {
        astigmatismZModel = null;
    }
    int paramCount;
    final PSFType psfType = psf.getPsfType();
    switch(psfType) {
        case ASTIGMATIC_GAUSSIAN_2D:
            flags = GaussianFunctionFactory.FIT_ERF_ASTIGMATISM;
            // nParams = 2; // Store Sx and Sy
            // The PSF stores the full astigmatism model
            paramCount = 8;
            break;
        case ONE_AXIS_GAUSSIAN_2D:
            if (isFixedPsf()) {
                flags = GaussianFunctionFactory.FIT_ERF_FIXED;
            } else {
                flags = GaussianFunctionFactory.FIT_ERF_CIRCLE;
            }
            paramCount = 1;
            break;
        case TWO_AXIS_AND_THETA_GAUSSIAN_2D:
            flags = GaussianFunctionFactory.FIT_ELLIPTICAL;
            paramCount = 3;
            break;
        case TWO_AXIS_GAUSSIAN_2D:
            flags = GaussianFunctionFactory.FIT_ERF_FREE_CIRCLE;
            paramCount = 2;
            break;
        default:
            throw new IllegalStateException("FitSettings must be a Gaussian 2D PSF");
    }
    isTwoAxisGaussian2D = PsfHelper.isTwoAxisGaussian2D(psfType);
    final boolean changed = psf.getParametersCount() > paramCount;
    if (changed) {
        while (psf.getParametersCount() > paramCount) {
            psf.removeParameters(psf.getParametersCount() - 1);
        }
        // Updated names when changing from two-axis to one axis
        if (psf.getParametersCount() == 1) {
            psf.getParametersBuilder(PsfHelper.INDEX_SX).setName(PsfProtosHelper.defaultOneAxisGaussian2DPSF.getParameters(PsfHelper.INDEX_SX).getName());
        }
    }
    // Ensure we have enough parameters
    if (psf.getParametersCount() == 0 && paramCount > 0) {
        // Create a dummy Sx
        final PSFParameter.Builder p = psf.addParametersBuilder();
        p.setName(PsfProtosHelper.defaultOneAxisGaussian2DPSF.getParameters(PsfHelper.INDEX_SX).getName());
        p.setValue(1);
        p.setUnit(PSFParameterUnit.DISTANCE);
    }
    if (psf.getParametersCount() == 1 && paramCount > 1) {
        // Rename S to Sx
        psf.getParametersBuilder(PsfHelper.INDEX_SX).setName(PsfProtosHelper.defaultTwoAxisGaussian2DPSF.getParameters(PsfHelper.INDEX_SX).getName());
        // Duplicate the Sx to Sy
        final PSFParameter.Builder p = psf.addParametersBuilder();
        p.setName(PsfProtosHelper.defaultTwoAxisGaussian2DPSF.getParameters(PsfHelper.INDEX_SY).getName());
        p.setValue(psf.getParameters(PsfHelper.INDEX_SX).getValue());
        p.setUnit(PSFParameterUnit.DISTANCE);
    }
    if (psf.getParametersCount() == 2 && paramCount > 2) {
        // Create a dummy angle
        final PSFParameter.Builder p = psf.addParametersBuilder();
        p.setName(PsfProtosHelper.defaultTwoAxisAndThetaGaussian2DPSF.getParameters(PsfHelper.INDEX_THETA).getName());
        p.setUnit(PSFParameterUnit.ANGLE);
    }
    // These depend on the 2-axis Gaussian flag
    updateWidthThreshold();
    updateMinWidthThreshold();
    // This depends on the width.
    updateCoordinateShift();
}
Also used : PSFParameter(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSFParameter) PSFType(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSFType)

Aggregations

PSFType (uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSFType)3 PSF (uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF)2 PSFParameter (uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSFParameter)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 JsonFormat (com.google.protobuf.util.JsonFormat)1 Rectangle (java.awt.Rectangle)1 BufferedReader (java.io.BufferedReader)1 DataInputStream (java.io.DataInputStream)1 EOFException (java.io.EOFException)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1 Locale (java.util.Locale)1 NoSuchElementException (java.util.NoSuchElementException)1 Objects (java.util.Objects)1 Scanner (java.util.Scanner)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1