Search in sources :

Example 16 with DistanceUnit

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

the class ConvertResults method showDialog.

private static boolean showDialog(MemoryPeakResults results) {
    final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
    gd.addMessage("Convert the current units for the results");
    gd.addHelp(HelpUrls.getUrl("convert-results"));
    final CalibrationReader cr = CalibrationWriter.create(results.getCalibration());
    gd.addChoice("Distance_unit", SettingsManager.getDistanceUnitNames(), cr.getDistanceUnitValue());
    gd.addNumericField("Calibration (nm/px)", cr.getNmPerPixel(), 2);
    gd.addChoice("Intensity_unit", SettingsManager.getIntensityUnitNames(), cr.getIntensityUnitValue());
    gd.addNumericField("Gain (Count/photon)", cr.getCountPerPhoton(), 2);
    gd.addChoice("Angle_unit", SettingsManager.getAngleUnitNames(), cr.getAngleUnitValue());
    gd.showDialog();
    if (gd.wasCanceled()) {
        return false;
    }
    final CalibrationWriter cw = results.getCalibrationWriterSafe();
    final DistanceUnit distanceUnit = SettingsManager.getDistanceUnitValues()[gd.getNextChoiceIndex()];
    cw.setNmPerPixel(Math.abs(gd.getNextNumber()));
    final IntensityUnit intensityUnit = SettingsManager.getIntensityUnitValues()[gd.getNextChoiceIndex()];
    cw.setCountPerPhoton(Math.abs(gd.getNextNumber()));
    final AngleUnit angleUnit = SettingsManager.getAngleUnitValues()[gd.getNextChoiceIndex()];
    // Don't set the calibration with bad values
    if (distanceUnit.getNumber() > 0 && !(cw.getNmPerPixel() > 0)) {
        IJ.error(TITLE, "Require positive nm/pixel for conversion");
        return false;
    }
    if (intensityUnit.getNumber() > 0 && !(cw.getCountPerPhoton() > 0)) {
        IJ.error(TITLE, "Require positive Count/photon for conversion");
        return false;
    }
    final Calibration newCalibration = cw.getCalibration();
    results.setCalibration(newCalibration);
    if (!results.convertToUnits(distanceUnit, intensityUnit, angleUnit)) {
        IJ.error(TITLE, "Conversion failed");
        return false;
    }
    return true;
}
Also used : AngleUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.AngleUnit) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) IntensityUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.IntensityUnit) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) Calibration(uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.Calibration) CalibrationReader(uk.ac.sussex.gdsc.smlm.data.config.CalibrationReader) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)

Example 17 with DistanceUnit

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

the class CropResults method addStandardFields.

private void addStandardFields(final ExtendedGenericDialog gd) {
    if (minMax != null) {
        // 3D crop options
        double min = minMax.getMinimum();
        double max = minMax.getMaximum();
        final double maxz = Math.min(settings.getMaxZ(), max);
        final double minz = Math.max(settings.getMinZ(), min);
        // Display in nm
        converter = new IdentityTypeConverter<>(null);
        String unit = "";
        final DistanceUnit nativeUnit = results.getDistanceUnit();
        if (nativeUnit != null) {
            unit = UnitHelper.getShortName(nativeUnit);
            try {
                converter = CalibrationHelper.getDistanceConverter(results.getCalibration(), DistanceUnit.NM);
                unit = UnitHelper.getShortName(DistanceUnit.NM);
            } catch (final ConversionException ex) {
            // No native units
            }
        }
        min = converter.convert(min);
        max = converter.convert(max);
        final String msg = String.format("%.2f <= z <= %.2f (%s)", min, max, unit);
        min = Math.floor(min);
        max = Math.ceil(max);
        gd.addMessage(msg);
        gd.addCheckbox("Limit Z-depth", settings.getLimitZ());
        gd.addSlider("minZ", min, max, converter.convert(minz));
        gd.addSlider("maxZ", min, max, converter.convert(maxz));
    }
    gd.addChoice("Name_option", NAME_OPTIONS, settings.getNameOption(), new OptionListener<Integer>() {

        @Override
        public boolean collectOptions(Integer value) {
            settings.setNameOption(value);
            return collectOptions(false);
        }

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

        private boolean collectOptions(boolean silent) {
            final ExtendedGenericDialog egd = new ExtendedGenericDialog(TITLE);
            if (settings.getNameOption() == NAME_OPTION_NAME) {
                final String name = (TextUtils.isNullOrEmpty(settings.getOutputName())) ? (results.getName() + " Cropped") : settings.getOutputName();
                egd.addStringField("Output_name", name, MathUtils.clip(60, 120, name.length()));
            } else if (settings.getNameOption() == NAME_OPTION_SUFFIX) {
                final String name = (TextUtils.isNullOrEmpty(settings.getNameSuffix())) ? " Cropped" : settings.getNameSuffix();
                egd.addStringField("Name_suffix", name, MathUtils.clip(20, 60, name.length()));
            } else if (settings.getNameOption() == NAME_OPTION_SEQUENCE) {
                final String name = settings.getNameSuffix();
                egd.addStringField("Name_suffix", name, MathUtils.clip(20, 60, name.length()));
                int counter = settings.getNameCounter();
                if (counter < 1) {
                    counter = 1;
                }
                egd.addNumericField("Name_counter", counter, 0);
            } else {
                throw new IllegalStateException("Unknown name option: " + settings.getNameOption());
            }
            egd.setSilent(silent);
            egd.showDialog(true, gd);
            if (egd.wasCanceled()) {
                return false;
            }
            if (settings.getNameOption() == NAME_OPTION_NAME) {
                settings.setOutputName(egd.getNextString());
            } else if (settings.getNameOption() == NAME_OPTION_SUFFIX) {
                settings.setNameSuffix(egd.getNextString());
            } else if (settings.getNameOption() == NAME_OPTION_SEQUENCE) {
                settings.setNameSuffix(egd.getNextString());
                settings.setNameCounter(Math.max(1, (int) egd.getNextNumber()));
            }
            return true;
        }
    });
    gd.addCheckbox("Preserve_bounds", settings.getPreserveBounds());
}
Also used : ConversionException(uk.ac.sussex.gdsc.core.data.utils.ConversionException) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)

Example 18 with DistanceUnit

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

the class ImageJ3DResultsViewer method createSphereSizeFromDeviations.

@Nullable
private static Point3f[] createSphereSizeFromDeviations(MemoryPeakResults results) {
    if (!results.hasDeviations()) {
        IJ.error(TITLE, "The results have no deviations");
        return null;
    }
    // Currently the rendering is in nm
    final TypeConverter<DistanceUnit> dc = results.getDistanceConverter(DistanceUnit.NM);
    final Point3f[] size = new Point3f[results.size()];
    final boolean failed = results.forEach(new PeakResultProcedureX() {

        int index;

        @Override
        public boolean execute(PeakResult peakResult) {
            final float x = peakResult.getParameterDeviation(PeakResult.X);
            final float y = peakResult.getParameterDeviation(PeakResult.Y);
            float z = peakResult.getParameterDeviation(PeakResult.Z);
            // This should be OK as 2D fitting should provide these.
            if (x == 0 || y == 0) {
                return true;
            }
            if (z == 0) {
                // Mean variance
                z = (float) Math.sqrt((x * x + y * y) / 2);
            }
            // z = (x + y) / 2; // Mean Std Dev
            size[index++] = new Point3f(dc.convert(x), dc.convert(y), dc.convert(z));
            return false;
        }
    });
    return (failed) ? null : size;
}
Also used : Point3f(org.scijava.vecmath.Point3f) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) PeakResultProcedureX(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedureX) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable)

Example 19 with DistanceUnit

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

the class AstigmatismModelManagerTest method canConvertModel.

private static void canConvertModel(DistanceUnit zDistanceUnit, DistanceUnit sdDistanceUnit) {
    // Use a reasonable z-depth function from the Smith, et al (2010) paper (page 377)
    final double sx = 1.08;
    final double sy = 1.01;
    final double gamma = 0.389;
    final double d = 0.531;
    final double Ax = -0.0708;
    final double Bx = -0.073;
    final double Ay = 0.164;
    final double By = 0.0417;
    final double nmPerPixel = 100;
    // Ax = Ay = 0;
    // Bx = By = 0;
    final AstigmatismModel.Builder builder = AstigmatismModel.newBuilder();
    builder.setGamma(gamma);
    builder.setD(d);
    builder.setS0X(sx);
    builder.setAx(Ax);
    builder.setBx(Bx);
    builder.setS0Y(sy);
    builder.setAy(Ay);
    builder.setBy(By);
    builder.setZDistanceUnit(DistanceUnit.UM);
    builder.setSDistanceUnit(DistanceUnit.PIXEL);
    builder.setNmPerPixel(nmPerPixel);
    final AstigmatismModel model1 = builder.build();
    final AstigmatismModel model2 = AstigmatismModelManager.convert(model1, zDistanceUnit, sdDistanceUnit);
    final AstigmatismZModel m1 = AstigmatismModelManager.create(model1);
    final AstigmatismZModel m2 = AstigmatismModelManager.create(model2);
    final TypeConverter<DistanceUnit> zc = UnitConverterUtils.createConverter(DistanceUnit.UM, zDistanceUnit, nmPerPixel);
    final TypeConverter<DistanceUnit> sc = UnitConverterUtils.createConverter(DistanceUnit.PIXEL, sdDistanceUnit, nmPerPixel);
    final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-8, 0);
    for (double z = -0.5; z <= 0.5; z += 0.1) {
        final double e = sc.convert(m1.getSx(z));
        final double o = m2.getSx(zc.convert(z));
        // logger.fine(FunctionUtils.getSupplier("%f vs %f", e, o);
        TestAssertions.assertTest(e, o, predicate);
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) AstigmatismModel(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel) AstigmatismZModel(uk.ac.sussex.gdsc.smlm.function.gaussian.AstigmatismZModel) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)

Example 20 with DistanceUnit

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

the class MemoryPeakResults method translate.

/**
 * Apply a pixel translation to the results. The original coordinates are updated using a raw
 * pixel shift. The current XY coordinates are updated by converting the pixel shift to the
 * current distance units. If there are current bounds then the origin will be updated with a raw
 * pixel shift.
 *
 * @param x the x pixel shift
 * @param y the y pixel shift
 * @throws ConversionException if the conversion is not possible
 * @throws ConfigurationException if the configuration is invalid
 */
public void translate(int x, int y) {
    if (x == 0 && y == 0) {
        return;
    }
    checkCalibration();
    final Rectangle bounds = getBounds();
    if (bounds != null) {
        bounds.x += x;
        bounds.y += y;
        setBounds(bounds);
    }
    // Convert the pixel shift to the units of the results
    final TypeConverter<DistanceUnit> dc = getCalibrationReader().getDistanceConverter(getDistanceUnit());
    final float xx = dc.convert(x);
    final float yy = dc.convert(y);
    for (int i = 0, size = size(); i < size; i++) {
        final PeakResult r = getf(i);
        r.setOrigX(r.getOrigX() + x);
        r.setOrigY(r.getOrigY() + y);
        r.setXPosition(r.getXPosition() + xx);
        r.setYPosition(r.getYPosition() + yy);
    }
}
Also used : Rectangle(java.awt.Rectangle) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)

Aggregations

DistanceUnit (uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)21 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)7 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)6 IntensityUnit (uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.IntensityUnit)6 CalibrationReader (uk.ac.sussex.gdsc.smlm.data.config.CalibrationReader)5 PeakResult (uk.ac.sussex.gdsc.smlm.results.PeakResult)5 IJ (ij.IJ)3 Plot (ij.gui.Plot)3 PlugIn (ij.plugin.PlugIn)3 ArrayList (java.util.ArrayList)3 ConversionException (uk.ac.sussex.gdsc.core.data.utils.ConversionException)3 TypeConverter (uk.ac.sussex.gdsc.core.data.utils.TypeConverter)3 MultiDialog (uk.ac.sussex.gdsc.core.ij.gui.MultiDialog)3 PeakResultProcedure (uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure)3 ImagePlus (ij.ImagePlus)2 PointRoi (ij.gui.PointRoi)2 Rectangle (java.awt.Rectangle)2 List (java.util.List)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Test (org.junit.jupiter.api.Test)2