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));
}
}
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;
}
});
}
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();
}
Aggregations