Search in sources :

Example 1 with ConversionException

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;
}
Also used : GaussianPsfModel(uk.ac.sussex.gdsc.smlm.model.GaussianPsfModel) ConversionException(uk.ac.sussex.gdsc.core.data.utils.ConversionException) GenericDialog(ij.gui.GenericDialog) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) AiryPsfModel(uk.ac.sussex.gdsc.smlm.model.AiryPsfModel) HoltzerAstigmatismZModel(uk.ac.sussex.gdsc.smlm.function.gaussian.HoltzerAstigmatismZModel)

Example 2 with ConversionException

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;
}
Also used : ConversionException(uk.ac.sussex.gdsc.core.data.utils.ConversionException) ImagePSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.ImagePSF) PSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF) AstigmatismModel(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel)

Example 3 with ConversionException

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;
}
Also used : ConversionException(uk.ac.sussex.gdsc.core.data.utils.ConversionException) ConfigurationException(uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException) PeakResultConversionHelper(uk.ac.sussex.gdsc.smlm.results.PeakResultConversionHelper) TIntArrayList(gnu.trove.list.array.TIntArrayList)

Example 4 with ConversionException

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
    }
}
Also used : ConversionException(uk.ac.sussex.gdsc.core.data.utils.ConversionException) OffsetPointRoi(uk.ac.sussex.gdsc.core.ij.gui.OffsetPointRoi) ImagePlus(ij.ImagePlus) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) ReadHint(uk.ac.sussex.gdsc.smlm.results.ImageSource.ReadHint) TFloatArrayList(gnu.trove.list.array.TFloatArrayList) ImageSource(uk.ac.sussex.gdsc.smlm.results.ImageSource) IJImageSource(uk.ac.sussex.gdsc.smlm.ij.IJImageSource) SeriesImageSource(uk.ac.sussex.gdsc.smlm.ij.SeriesImageSource) Overlay(ij.gui.Overlay) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) PointRoi(ij.gui.PointRoi) OffsetPointRoi(uk.ac.sussex.gdsc.core.ij.gui.OffsetPointRoi)

Example 5 with ConversionException

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);
    }
}
Also used : ConversionException(uk.ac.sussex.gdsc.core.data.utils.ConversionException) Rectangle2D(java.awt.geom.Rectangle2D) CalibrationReader(uk.ac.sussex.gdsc.smlm.data.config.CalibrationReader) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)

Aggregations

ConversionException (uk.ac.sussex.gdsc.core.data.utils.ConversionException)10 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)3 AstigmatismModel (uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel)3 DistanceUnit (uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)3 HoltzerAstigmatismZModel (uk.ac.sussex.gdsc.smlm.function.gaussian.HoltzerAstigmatismZModel)3 TIntArrayList (gnu.trove.list.array.TIntArrayList)2 ConfigurationException (uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException)2 PeakResult (uk.ac.sussex.gdsc.smlm.results.PeakResult)2 PeakResultConversionHelper (uk.ac.sussex.gdsc.smlm.results.PeakResultConversionHelper)2 TFloatArrayList (gnu.trove.list.array.TFloatArrayList)1 ImagePlus (ij.ImagePlus)1 GenericDialog (ij.gui.GenericDialog)1 Overlay (ij.gui.Overlay)1 Plot (ij.gui.Plot)1 PointRoi (ij.gui.PointRoi)1 Rectangle2D (java.awt.geom.Rectangle2D)1 Converter (uk.ac.sussex.gdsc.core.data.utils.Converter)1 NonBlockingExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog)1 OffsetPointRoi (uk.ac.sussex.gdsc.core.ij.gui.OffsetPointRoi)1 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)1