use of uk.ac.sussex.gdsc.core.data.utils.ConversionException in project GDSC-SMLM by aherbert.
the class CreateData method createPsfModel.
private PsfModel createPsfModel(List<LocalisationModelSet> localisationSets) {
// Allow reuse of the cached model
if (psfModelCache != null) {
return psfModelCache;
}
if (psfModelType == PSF_MODEL_IMAGE) {
return createImagePsf(localisationSets);
}
if (psfModelType == PSF_MODEL_ASTIGMATISM) {
astigmatismModel = AstigmatismModelManager.getModel(settings.getAstigmatismModel());
if (astigmatismModel == null) {
throw new IllegalArgumentException("Failed to load model: " + settings.getAstigmatismModel());
}
// Convert for simulation
try {
if (DoubleEquality.relativeError(astigmatismModel.getNmPerPixel(), settings.getPixelPitch()) > 1e-6) {
final String message = String.format("Astigmatism model '%s' calibration (%s nm) does not match pixel pitch (%s nm)", settings.getAstigmatismModel(), MathUtils.rounded(astigmatismModel.getNmPerPixel()), MathUtils.rounded(settings.getPixelPitch()));
// Optionally convert
final GenericDialog gd = new GenericDialog(TITLE);
gd.addMessage(TextUtils.wrap(message + ". Created data is not suitable for fitting.", 80));
gd.addMessage(TextUtils.wrap("Click OK to continue anyway (i.e. draw the spot using the " + "correct nm width on the different sized pixels).", 80));
gd.showDialog();
if (gd.wasCanceled()) {
throw new IllegalArgumentException(message);
}
// Convert to nm
astigmatismModel = AstigmatismModelManager.convert(astigmatismModel, DistanceUnit.NM, DistanceUnit.NM);
// Reset pixel pitch. This will draw the spot using the correct size on the different size
// pixels.
astigmatismModel = astigmatismModel.toBuilder().setNmPerPixel(settings.getPixelPitch()).build();
}
// Convert for simulation in pixels
astigmatismModel = AstigmatismModelManager.convert(astigmatismModel, DistanceUnit.PIXEL, DistanceUnit.PIXEL);
return new GaussianPsfModel(AstigmatismModelManager.create(astigmatismModel));
} catch (final ConversionException ex) {
// Wrap so this can be caught as the same type
throw new IllegalArgumentException(ex);
}
}
final double d = settings.getDepthOfFocus() / settings.getPixelPitch();
if (psfModelType == PSF_MODEL_GAUSSIAN) {
final double sd = getPsfSd();
final double gamma = 0;
final HoltzerAstigmatismZModel zModel = HoltzerAstigmatismZModel.create(sd, sd, gamma, d, 0, 0, 0, 0);
final GaussianPsfModel m = new GaussianPsfModel(zModel);
// m.setRange(10);
return m;
}
// Default to Airy pattern
final double width = getPsfSd() / PsfCalculator.AIRY_TO_GAUSSIAN;
final AiryPsfModel m = new AiryPsfModel(width, width, d);
m.setRing(2);
return m;
}
use of uk.ac.sussex.gdsc.core.data.utils.ConversionException in project GDSC-SMLM by aherbert.
the class CreateData method createPsf.
private PSF createPsf(double psfSd) {
if (psf == null) {
if (psfModelType == PSF_MODEL_ASTIGMATISM) {
// Note: the astigmatismModel may not yet be created so create if necessary.
// This is used to store the best PSF to use to fit the data.
AstigmatismModel astigmatismModel = this.astigmatismModel;
if (astigmatismModel == null) {
astigmatismModel = AstigmatismModelManager.getModel(settings.getAstigmatismModel());
}
if (astigmatismModel == null) {
throw new IllegalArgumentException("Failed to load model: " + settings.getAstigmatismModel());
}
// Assume conversion for simulation
try {
if (DoubleEquality.relativeError(astigmatismModel.getNmPerPixel(), settings.getPixelPitch()) > 1e-6) {
// Convert to nm
astigmatismModel = AstigmatismModelManager.convert(astigmatismModel, DistanceUnit.NM, DistanceUnit.NM);
// Reset pixel pitch. This will draw the spot using the correct size on the different
// size pixels.
astigmatismModel = astigmatismModel.toBuilder().setNmPerPixel(settings.getPixelPitch()).build();
}
// Convert for simulation in pixels
astigmatismModel = AstigmatismModelManager.convert(astigmatismModel, DistanceUnit.PIXEL, DistanceUnit.PIXEL);
} catch (final ConversionException ex) {
// Wrap so this can be caught as the same type
throw new IllegalArgumentException(ex);
}
psf = PsfProtosHelper.createPsf(astigmatismModel, DistanceUnit.PIXEL, DistanceUnit.PIXEL);
psf = psf.toBuilder().setModelName(settings.getAstigmatismModel()).build();
} else {
PSF.Builder psfBuilder;
// Set the PSF as a Gaussian using the width at z=0.
// In future this could be improved for other PSFs.
psfBuilder = PsfProtosHelper.defaultOneAxisGaussian2DPSF.toBuilder();
psfBuilder.getParametersBuilder(PsfHelper.INDEX_SX).setValue(psfSd);
psf = psfBuilder.build();
}
}
return psf;
}
use of uk.ac.sussex.gdsc.core.data.utils.ConversionException in project GDSC-SMLM by aherbert.
the class ImageJTablePeakResults method begin.
@Override
public void begin() {
tableActive = false;
// Set-up unit processing that requires the calibration
toPixelConverter = new IdentityTypeConverter<>(null);
calculator = null;
canComputePrecision = false;
rounder = RounderUtils.create(roundingPrecision);
// We must correctly convert all the PSF parameter types
helper = new PeakResultConversionHelper(getCalibration(), getPsf());
helper.setIntensityUnit(intensityUnit);
helper.setDistanceUnit(distanceUnit);
helper.setAngleUnit(angleUnit);
converters = helper.getConverters();
if (hasCalibration()) {
if (showPrecision) {
if (computePrecision) {
try {
calculator = Gaussian2DPeakResultHelper.create(getPsf(), getCalibrationReader(), Gaussian2DPeakResultHelper.LSE_PRECISION);
canComputePrecision = true;
} catch (final ConfigurationException ex) {
// Cannot compute precision
} catch (final ConversionException ex) {
// Cannot compute precision
}
}
}
try {
if (helper.hasDistanceConverter()) {
toPixelConverter = UnitConverterUtils.createConverter(distanceUnit, DistanceUnit.PIXEL, getCalibrationReader().getNmPerPixel());
}
} catch (final ConversionException ex) {
// Gracefully fail so ignore this
}
}
ic = converters[PeakResult.INTENSITY];
outIndices = SimpleArrayUtils.natural(converters.length);
if (!showZ) {
final TIntArrayList list = new TIntArrayList(outIndices);
list.remove(PeakResult.Z);
outIndices = list.toArray();
}
// Update the calibration if converters were created
if (helper.isCalibrationChanged()) {
setCalibration(helper.getCalibration());
}
createSourceText();
createResultsWindow();
if (clearAtStart) {
tp.clear();
}
size = 0;
// Let some results appear before drawing.
// ImageJ will auto-layout columns if it has less than 10 rows
nextRepaintSize = 9;
tableActive = true;
}
use of uk.ac.sussex.gdsc.core.data.utils.ConversionException in project GDSC-SMLM by aherbert.
the class PeakResultTableModelFrame method doSourceOverlay.
private void doSourceOverlay() {
final PeakResultTableModel model = getModel();
if (model == null) {
return;
}
final PeakResult[] list = table.getSelectedData();
if (list.length == 0) {
return;
}
final ImageSource source = model.getSource();
if (source == null) {
return;
}
final String title = source.getOriginal().getName();
final ImagePlus imp = WindowManager.getImage(title);
if (imp == null) {
return;
}
// Assumes 3D stack (no channel/time)
if (imp.getNDimensions() > 3) {
return;
}
try {
final TypeConverter<DistanceUnit> converter = CalibrationHelper.getDistanceConverter(model.getCalibration(), DistanceUnit.PIXEL);
final Overlay o = new Overlay();
if (list.length == 1) {
final PeakResult p = list[0];
final PointRoi roi = new OffsetPointRoi(converter.convert(p.getXPosition()), converter.convert(p.getYPosition()));
roi.setPointType(3);
roi.setPosition(p.getFrame());
o.add(roi);
} else {
Arrays.sort(list, FramePeakResultComparator.INSTANCE);
final TFloatArrayList ox = new TFloatArrayList(list.length);
final TFloatArrayList oy = new TFloatArrayList(list.length);
int frame = list[0].getFrame() - 1;
for (int i = 0; i < list.length; i++) {
if (frame != list[i].getFrame()) {
if (ox.size() > 0) {
final PointRoi roi = new OffsetPointRoi(ox.toArray(), oy.toArray());
roi.setPointType(3);
roi.setPosition(frame);
ox.resetQuick();
oy.resetQuick();
o.add(roi);
}
frame = list[i].getFrame();
}
ox.add(converter.convert(list[i].getXPosition()));
oy.add(converter.convert(list[i].getYPosition()));
}
if (ox.size() > 0) {
final PointRoi roi = new OffsetPointRoi(ox.toArray(), oy.toArray());
roi.setPointType(3);
roi.setPosition(frame);
o.add(roi);
}
}
imp.setOverlay(o);
final PeakResult p = list[0];
imp.setSlice(p.getFrame());
ImageJUtils.adjustSourceRect(imp, 0, (int) converter.convert(p.getXPosition()), (int) converter.convert(p.getYPosition()));
imp.getWindow().toFront();
} catch (final ConversionException ex) {
// Ignore
}
}
use of uk.ac.sussex.gdsc.core.data.utils.ConversionException in project GDSC-SMLM by aherbert.
the class Fire method initialise.
/**
* Initialise this instance with localisation results for the FIRE computation.
*
* @param results the results
* @param results2 the second set of results (can be null)
*/
public void initialise(MemoryPeakResults results, MemoryPeakResults results2) {
this.results = verify(results);
this.results2 = verify(results2);
if (this.results == null) {
return;
}
nmPerUnit = 1;
DistanceUnit unit = null;
units = "unknown";
final CalibrationReader cal = results.getCalibrationReader();
if (cal != null) {
try {
nmPerUnit = cal.getDistanceConverter(DistanceUnit.NM).convert(1);
units = UnitHelper.getShortName(DistanceUnit.NM);
unit = DistanceUnit.NM;
} catch (final ConversionException ex) {
IJ.log(pluginTitle + " Warning: Ignoring invalid distance calibration for primary results");
}
} else {
IJ.log(pluginTitle + " Warning: No calibration exists for primary results");
}
// Calibration must match between datasets
if (this.results2 != null) {
CalibrationReader cal2 = results.getCalibrationReader();
if (unit == null) {
if (cal2 != null) {
IJ.log(pluginTitle + " Warning: Ignoring calibration for secondary results since no calibration" + " exists for primary results");
}
} else {
// The calibration must match
try {
// Try to create a converter and check it is the same conversion
if (cal2 != null && cal2.getDistanceConverter(DistanceUnit.NM).convert(1) != nmPerUnit) {
// Set to null to mark invalid
cal2 = null;
}
} catch (final ConversionException ex) {
// Set to null to mark invalid
cal2 = null;
} finally {
if (cal2 == null) {
this.results = null;
IJ.error(pluginTitle, "Error: Calibration between the two input datasets does not match");
}
}
}
}
// Use the float data bounds. This prevents problems if the data is far from the origin.
dataBounds = results.getDataBounds(null);
if (this.results2 != null) {
final Rectangle2D dataBounds2 = results.getDataBounds(null);
dataBounds = dataBounds.createUnion(dataBounds2);
}
}
Aggregations