Search in sources :

Example 6 with ConfigurationException

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

the class TraceDiffusion method calculatePrecision.

/**
 * Calculate the average precision of localisation in the traces.
 *
 * @param traces the traces
 * @param multi true if the traces were from multiple input results
 */
private void calculatePrecision(Trace[] traces, boolean multi) {
    // Check the diffusion simulation for a precision
    if (DiffusionRateTest.isSimulated(results.getName()) && !multi) {
        precision = DiffusionRateTest.getLastSimulationPrecision();
    } else {
        precision = 999;
        try {
            final Gaussian2DPeakResultCalculator calculator = Gaussian2DPeakResultHelper.create(results.getPsf(), results.getCalibration(), Gaussian2DPeakResultHelper.LSE_PRECISION);
            // Get the average precision of the localisations
            precision = 0;
            int count = 0;
            for (final Trace trace : traces) {
                for (int k = 0; k < trace.size(); k++) {
                    final PeakResult r = trace.get(k);
                    precision += calculator.getLsePrecision(r.getParameters(), r.getNoise());
                }
                count += trace.size();
            }
            precision /= count;
        } catch (final ConfigurationException ex) {
        // Ignore this and we will ask the user for the precision
        }
    }
    if (precision > 100) {
        final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
        gd.addMessage("The average precision of the traced results is " + MathUtils.rounded(precision, 4) + " nm.\nPlease verify the precision.");
        gd.addSlider("Precision (nm)", 5, 100, precision);
        gd.showDialog();
        if (!(gd.wasCanceled() || gd.invalidNumber())) {
            precision = Math.abs(gd.getNextNumber());
        }
    }
}
Also used : Trace(uk.ac.sussex.gdsc.smlm.results.Trace) Gaussian2DPeakResultCalculator(uk.ac.sussex.gdsc.smlm.results.Gaussian2DPeakResultCalculator) ConfigurationException(uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult)

Example 7 with ConfigurationException

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

the class ClassificationMatchCalculator method run.

@Override
public void run(String arg) {
    SmlmUsageTracker.recordPlugin(this.getClass(), arg);
    if (MemoryPeakResults.isMemoryEmpty()) {
        IJ.error(TITLE, "No localisations in memory");
        return;
    }
    if (!showDialog()) {
        return;
    }
    // Load the results
    final MemoryPeakResults results1 = ResultsManager.loadInputResults(settings.inputOption1, false, null, null);
    final MemoryPeakResults results2 = ResultsManager.loadInputResults(settings.inputOption2, false, null, null);
    IJ.showStatus("");
    if (results1 == null || results1.size() == 0) {
        IJ.error(TITLE, "No results 1 could be loaded");
        return;
    }
    if (results2 == null || results2.size() == 0) {
        IJ.error(TITLE, "No results 2 could be loaded");
        return;
    }
    // Check the results can be loaded in the pixels
    try {
        results1.getDistanceConverter(DistanceUnit.PIXEL);
        results2.getDistanceConverter(DistanceUnit.PIXEL);
    } catch (ConversionException | ConfigurationException ex) {
        IJ.error(TITLE, "Distances cannot be loaded in pixels");
        return;
    }
    final long start = System.nanoTime();
    runCompareClassifications(results1, results2);
    final long nanoseconds = System.nanoTime() - start;
    IJ.showStatus(String.format("%s = %s", TITLE, TextUtils.nanosToString(nanoseconds)));
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) ConfigurationException(uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Example 8 with ConfigurationException

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

the class PeakResultTableModel method createTableStructure.

/**
 * Called when the structure of the table should be created. If the structure has not changed or
 * no live table are attached then this does nothing.
 *
 * @param changed Set to true if a property controlling the structure has changed
 */
private void createTableStructure(boolean changed) {
    if (changed) {
        columnsComputed.set(false);
    }
    // so it can be turned off by PeakResultTableModelFrame.
    if (liveCount.get() == 0 || columnsComputed.get()) {
        return;
    }
    columnsComputed.set(true);
    rounder = RounderUtils.create(tableSettings.getRoundingPrecision());
    // Create the converters
    final PeakResultConversionHelper helper = new PeakResultConversionHelper(calibration, psf);
    helper.setIntensityUnit(tableSettings.getIntensityUnit());
    helper.setDistanceUnit(tableSettings.getDistanceUnit());
    helper.setAngleUnit(tableSettings.getAngleUnit());
    final Converter[] converters = helper.getConverters();
    final Converter ic = converters[PeakResult.INTENSITY];
    final String[] paramNames = helper.getNames();
    final String[] unitNames = helper.getUnitNames();
    // Calibration tableCalibration = (helper.isCalibrationChanged()) ? helper.getCalibration() :
    // calibration;
    // Organise the data columns.
    // This is done as per the IJTablePeakResults for consistency
    final LocalList<PeakResultData<?>> valuesList = new LocalList<>();
    final LocalList<String> namesList = new LocalList<>();
    rowCounter = tableSettings.getShowRowCounter();
    if (rowCounter) {
        valuesList.add(new PeakResultDataFrame());
        namesList.add("#");
    }
    valuesList.add(new PeakResultDataFrame());
    addName(valuesList, namesList);
    if (showEndFrame) {
        valuesList.add(new PeakResultDataEndFrame());
        addName(valuesList, namesList);
    }
    if (showId) {
        valuesList.add(new PeakResultDataId());
        addName(valuesList, namesList);
    }
    if (showCategory) {
        valuesList.add(new PeakResultDataCategory());
        addName(valuesList, namesList);
    }
    if (tableSettings.getShowFittingData()) {
        valuesList.add(new PeakResultDataOrigX());
        addName(valuesList, namesList);
        valuesList.add(new PeakResultDataOrigY());
        addName(valuesList, namesList);
        valuesList.add(new PeakResultDataOrigValue());
        addName(valuesList, namesList);
        valuesList.add(new PeakResultDataError());
        addName(valuesList, namesList);
    }
    if (tableSettings.getShowNoiseData()) {
        // Must be converted
        valuesList.add(new PeakResultDataFloat() {

            @Override
            public Float getValue(PeakResult result) {
                return ic.convert(result.getNoise());
            }
        });
        addName("Noise", namesList, unitNames[PeakResult.INTENSITY]);
        valuesList.add(new PeakResultDataFloat() {

            @Override
            public Float getValue(PeakResult result) {
                return ic.convert(result.getMeanIntensity());
            }
        });
        addName("Mean" + paramNames[PeakResult.INTENSITY], namesList, unitNames[PeakResult.INTENSITY]);
        valuesList.add(new PeakResultDataSnr());
        addName(valuesList, namesList);
    }
    int[] outIndices = SimpleArrayUtils.natural(converters.length);
    if (!showZ) {
        final TIntArrayList list = new TIntArrayList(outIndices);
        list.remove(PeakResult.Z);
        outIndices = list.toArray();
    }
    for (final int i : outIndices) {
        // Must be converted
        valuesList.add(new PeakResultDataParameterConverter(converters[i], i));
        addName(paramNames[i], namesList, unitNames[i]);
        if (showDeviations) {
            valuesList.add(new PeakResultDataParameterDeviationConverter(converters[i], i));
            namesList.add("+/-");
        }
    }
    if (tableSettings.getShowPrecision()) {
        PeakResultDataPrecision precision = null;
        try {
            final Gaussian2DPeakResultCalculator calculator = Gaussian2DPeakResultHelper.create(getPsf(), calibration, Gaussian2DPeakResultHelper.LSE_PRECISION);
            precision = new PeakResultDataPrecision() {

                @Override
                public Double getValue(PeakResult result) {
                    if (result.hasPrecision()) {
                        return result.getPrecision();
                    }
                    if (calculator != null) {
                        return calculator.getLsePrecision(result.getParameters(), result.getNoise());
                    }
                    return 0.0;
                }
            };
        } catch (final ConfigurationException | ConversionException ex) {
        // Ignore
        }
        if (precision == null) {
            precision = new PeakResultDataPrecision();
        }
        valuesList.add(precision);
        namesList.add("Precision (nm)");
    }
    values = valuesList.toArray(new PeakResultData<?>[0]);
    names = namesList.toArray(new String[0]);
    fireTableStructureChanged();
}
Also used : PeakResultDataId(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataId) PeakResultDataParameterConverter(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataParameterConverter) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) Gaussian2DPeakResultCalculator(uk.ac.sussex.gdsc.smlm.results.Gaussian2DPeakResultCalculator) PeakResultDataOrigValue(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataOrigValue) ConfigurationException(uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException) PeakResultDataParameterDeviationConverter(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataParameterDeviationConverter) PeakResultDataParameterConverter(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataParameterConverter) Converter(uk.ac.sussex.gdsc.core.data.utils.Converter) PeakResultDataPrecision(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataPrecision) PeakResultData(uk.ac.sussex.gdsc.smlm.results.PeakResultData) ConversionException(uk.ac.sussex.gdsc.core.data.utils.ConversionException) PeakResultDataFrame(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataFrame) PeakResultDataFloat(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataFloat) PeakResultDataSnr(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataSnr) PeakResultDataParameterDeviationConverter(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataParameterDeviationConverter) TIntArrayList(gnu.trove.list.array.TIntArrayList) PeakResultDataEndFrame(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataEndFrame) PeakResultDataOrigX(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataOrigX) PeakResultDataError(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataError) PeakResultDataFloat(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataFloat) PeakResultDataOrigY(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataOrigY) PeakResultDataCategory(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataCategory) PeakResultConversionHelper(uk.ac.sussex.gdsc.smlm.results.PeakResultConversionHelper)

Example 9 with ConfigurationException

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

the class TsfPeakResultsWriter method begin.

@Override
public void begin() {
    out = null;
    size = 0;
    // Only support Gaussian 2D data
    if (getPsf() == null || !PsfHelper.isGaussian2D(getPsf())) {
        logger.log(Level.SEVERE, "TSF format requires a Gaussian 2D PSF");
        closeOutput();
        return;
    }
    final int[] indices = PsfHelper.getGaussian2DWxWyIndices(getPsf());
    isx = indices[0];
    isy = indices[1];
    try {
        ia = PsfHelper.getGaussian2DAngleIndex(getPsf());
        fitMode = FitMode.TWOAXISANDTHETA;
    } catch (final ConfigurationException ex) {
        // This is not an angled PSF. Revert to 1/2 axis:
        fitMode = (isx == isy) ? FitMode.ONEAXIS : FitMode.TWOAXIS;
    }
    id = new AtomicInteger();
    try {
        out = new BufferedOutputStream(new FileOutputStream(filename));
    } catch (final Exception ex) {
        logger.log(Level.SEVERE, ex, () -> "Failed to write open TSF file: " + filename);
        closeOutput();
        return;
    }
    // Write the offsets used in the TSF format
    try {
        final DataOutputStream dos = new DataOutputStream(out);
        dos.writeInt(0);
        dos.writeLong(0);
    } catch (final IOException ex) {
        logger.log(Level.SEVERE, "Failed to write TSF offset fields", ex);
        closeOutput();
    }
}
Also used : ConfigurationException(uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DataOutputStream(java.io.DataOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) ConfigurationException(uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) NotImplementedException(uk.ac.sussex.gdsc.core.data.NotImplementedException) IOException(java.io.IOException)

Example 10 with ConfigurationException

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

the class PeakResultConversionHelper method getNames.

/**
 * Gets the names for the peak results parameters. This includes the standard parameters and any
 * additional parameters defined in the PSF. If a parameter name is undefined then unknown is
 * returned.
 *
 * @return the converters
 */
public String[] getNames() {
    final LocalList<String> list = new LocalList<>(5);
    list.add("Background");
    list.add("Intensity");
    list.add("X");
    list.add("Y");
    list.add("Z");
    if (psf != null) {
        try {
            for (final PSFParameter p : PsfHelper.getParameters(psf)) {
                final String name = p.getName();
                list.add(TextUtils.isNullOrEmpty(name) ? "unknown" : name);
            }
        } catch (final ConfigurationException ex) {
        // Ignore
        }
    }
    return list.toArray(new String[0]);
}
Also used : LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) ConfigurationException(uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException) PSFParameter(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSFParameter)

Aggregations

ConfigurationException (uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException)14 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)5 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)4 ConversionException (uk.ac.sussex.gdsc.core.data.utils.ConversionException)3 PSFParameter (uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSFParameter)3 TIntArrayList (gnu.trove.list.array.TIntArrayList)2 GenericDialog (ij.gui.GenericDialog)2 IOException (java.io.IOException)2 Converter (uk.ac.sussex.gdsc.core.data.utils.Converter)2 Gaussian2DPeakResultCalculator (uk.ac.sussex.gdsc.smlm.results.Gaussian2DPeakResultCalculator)2 PeakResult (uk.ac.sussex.gdsc.smlm.results.PeakResult)2 PeakResultConversionHelper (uk.ac.sussex.gdsc.smlm.results.PeakResultConversionHelper)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 ConversionException (com.thoughtworks.xstream.converters.ConversionException)1 TIntIntHashMap (gnu.trove.map.hash.TIntIntHashMap)1 TextWindow (ij.text.TextWindow)1 BufferedOutputStream (java.io.BufferedOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1