Search in sources :

Example 1 with Converter

use of uk.ac.sussex.gdsc.core.data.utils.Converter in project GDSC-SMLM by aherbert.

the class MemoryPeakResults method convertToUnits.

/**
 * Convert to the specified units. If the units are null they will remain unchanged.
 *
 * @param distanceUnit the distance unit
 * @param intensityUnit the intensity unit
 * @param angleUnit the angle unit
 * @return true, if the data is now stored in the preferred units.
 */
public boolean convertToUnits(DistanceUnit distanceUnit, IntensityUnit intensityUnit, AngleUnit angleUnit) {
    if (!hasCalibration()) {
        return false;
    }
    final PeakResultConversionHelper helper = new PeakResultConversionHelper(getCalibration(), getPsf());
    helper.setIntensityUnit(intensityUnit);
    helper.setDistanceUnit(distanceUnit);
    if (PsfHelper.hasAngleParameters(getPsf())) {
        helper.setAngleUnit(angleUnit);
    }
    final Converter[] converters = helper.getConverters();
    if (!helper.isCalibrationChanged()) {
        // Check if already in the specified units
        return helper.isValidConversion();
    }
    // Update the calibration
    setCalibration(helper.getCalibration());
    // We must convert the noise and mean intensity
    final Converter intensityConverter = converters[PeakResult.INTENSITY];
    for (int i = 0, size = size(); i < size; i++) {
        final PeakResult p = getfX(i);
        p.setNoise(intensityConverter.convert(p.getNoise()));
        p.setMeanIntensity(intensityConverter.convert(p.getMeanIntensity()));
        if (p.hasParameterDeviations()) {
            for (int j = 0; j < converters.length; j++) {
                p.setParameter(j, converters[j].convert(p.getParameter(j)));
                p.setParameterDeviation(j, converters[j].convert(p.getParameterDeviation(j)));
            }
        } else {
            for (int j = 0; j < converters.length; j++) {
                p.setParameter(j, converters[j].convert(p.getParameter(j)));
            }
        }
    }
    return true;
}
Also used : IdentityTypeConverter(uk.ac.sussex.gdsc.core.data.utils.IdentityTypeConverter) Converter(uk.ac.sussex.gdsc.core.data.utils.Converter) TypeConverter(uk.ac.sussex.gdsc.core.data.utils.TypeConverter)

Example 2 with Converter

use of uk.ac.sussex.gdsc.core.data.utils.Converter in project GDSC-SMLM by aherbert.

the class TraceDiffusion method createTraceFunction.

/**
 * Creates the trace function for the configured trace diffusion mode.
 *
 * @return the function
 */
private Function<MemoryPeakResults, Trace[]> createTraceFunction() {
    if (clusteringSettings.getTraceDiffusionMode() == 1) {
        final DmttConfiguration config = createDmttConfiguration();
        return r -> new DynamicMultipleTargetTracing(r).traceMolecules(config).toArray(new Trace[0]);
    }
    // Nearest neighbour
    // Convert from NM to the native units of the results
    final Converter c = CalibrationHelper.getDistanceConverter(results.getCalibration(), DistanceUnit.NM);
    final double distanceThreshold = c.convertBack(clusteringSettings.getDistanceThreshold());
    final double distanceExclusion = c.convertBack(clusteringSettings.getDistanceExclusion());
    return r -> {
        final TraceManager manager = new TraceManager(r);
        // Run the tracing
        manager.setTracker(SimpleImageJTrackProgress.getInstance());
        manager.setDistanceExclusion(distanceExclusion);
        manager.traceMolecules(distanceThreshold, 1);
        return manager.getTraces();
    };
}
Also used : Color(java.awt.Color) Arrays(java.util.Arrays) HistogramPlotBuilder(uk.ac.sussex.gdsc.core.ij.HistogramPlot.HistogramPlotBuilder) ConfigurationException(uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException) TextWindow(ij.text.TextWindow) RealVector(org.apache.commons.math3.linear.RealVector) ImageJPluginLoggerHelper(uk.ac.sussex.gdsc.core.ij.ImageJPluginLoggerHelper) StoredDataStatistics(uk.ac.sussex.gdsc.core.utils.StoredDataStatistics) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) MathUtils(uk.ac.sussex.gdsc.core.utils.MathUtils) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) Path(java.nio.file.Path) SettingsManager(uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager) ArrayPeakResultStore(uk.ac.sussex.gdsc.smlm.results.ArrayPeakResultStore) ClusteringSettings(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.ClusteringSettings) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) InputSource(uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsManager.InputSource) ConvergenceException(org.apache.commons.math3.exception.ConvergenceException) MultivariateVectorFunction(org.apache.commons.math3.analysis.MultivariateVectorFunction) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) Gaussian2DPeakResultHelper(uk.ac.sussex.gdsc.smlm.results.Gaussian2DPeakResultHelper) Logger(java.util.logging.Logger) TextUtils(uk.ac.sussex.gdsc.core.utils.TextUtils) Plot(ij.gui.Plot) CalibrationHelper(uk.ac.sussex.gdsc.smlm.data.config.CalibrationHelper) List(java.util.List) LeastSquaresBuilder(org.apache.commons.math3.fitting.leastsquares.LeastSquaresBuilder) Converter(uk.ac.sussex.gdsc.core.data.utils.Converter) SimpleArrayUtils(uk.ac.sussex.gdsc.core.utils.SimpleArrayUtils) FileUtils(uk.ac.sussex.gdsc.core.utils.FileUtils) PlugIn(ij.plugin.PlugIn) JumpDistanceAnalysis(uk.ac.sussex.gdsc.smlm.fitting.JumpDistanceAnalysis) TraceManager(uk.ac.sussex.gdsc.smlm.results.TraceManager) LevenbergMarquardtOptimizer(org.apache.commons.math3.fitting.leastsquares.LevenbergMarquardtOptimizer) WindowOrganiser(uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) DmttConfiguration(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing.DmttConfiguration) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) TextField(java.awt.TextField) OptionListener(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog.OptionListener) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Trace(uk.ac.sussex.gdsc.smlm.results.Trace) MultiDialog(uk.ac.sussex.gdsc.core.ij.gui.MultiDialog) CurveLogger(uk.ac.sussex.gdsc.smlm.fitting.JumpDistanceAnalysis.CurveLogger) Statistics(uk.ac.sussex.gdsc.core.utils.Statistics) SimpleImageJTrackProgress(uk.ac.sussex.gdsc.core.ij.SimpleImageJTrackProgress) Files(java.nio.file.Files) Checkbox(java.awt.Checkbox) BufferedWriter(java.io.BufferedWriter) StdMath(uk.ac.sussex.gdsc.smlm.utils.StdMath) IOException(java.io.IOException) LeastSquaresProblem(org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem) DynamicMultipleTargetTracing(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing) TooManyIterationsException(org.apache.commons.math3.exception.TooManyIterationsException) CalibrationReader(uk.ac.sussex.gdsc.smlm.data.config.CalibrationReader) Consumer(java.util.function.Consumer) Gaussian2DPeakResultCalculator(uk.ac.sussex.gdsc.smlm.results.Gaussian2DPeakResultCalculator) Paths(java.nio.file.Paths) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) IJ(ij.IJ) DiagonalMatrix(org.apache.commons.math3.linear.DiagonalMatrix) PeakResultStoreList(uk.ac.sussex.gdsc.smlm.results.PeakResultStoreList) Optimum(org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) DmttConfiguration(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing.DmttConfiguration) DynamicMultipleTargetTracing(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing) Converter(uk.ac.sussex.gdsc.core.data.utils.Converter) TraceManager(uk.ac.sussex.gdsc.smlm.results.TraceManager)

Example 3 with Converter

use of uk.ac.sussex.gdsc.core.data.utils.Converter 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 4 with Converter

use of uk.ac.sussex.gdsc.core.data.utils.Converter in project GDSC-SMLM by aherbert.

the class PeakResultConversionHelper method getConverters.

/**
 * Gets the converters for the peak results parameters. This includes the standard parameters and
 * any additional parameters defined in the PSF. If a parameter unit type is undefined then an
 * identity converter is created.
 *
 * @return the converters
 */
public Converter[] getConverters() {
    final LocalList<Converter> list = new LocalList<>(5);
    getIntensityConverter();
    getDistanceConverter();
    list.add(intensityConverter);
    list.add(intensityConverter);
    list.add(distanceConverter);
    list.add(distanceConverter);
    list.add(distanceConverter);
    if (psf != null) {
        try {
            for (final PSFParameter p : PsfHelper.getParameters(psf)) {
                switch(p.getUnit()) {
                    case DISTANCE:
                        list.add(distanceConverter);
                        break;
                    case INTENSITY:
                        list.add(intensityConverter);
                        break;
                    case ANGLE:
                        list.add(getAngleConverter());
                        break;
                    default:
                        list.add(new IdentityTypeConverter<>(p.getUnit()));
                }
            }
        } catch (final ConfigurationException ex) {
        // Ignore
        }
    }
    return list.toArray(new Converter[0]);
}
Also used : LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) ConfigurationException(uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException) IdentityTypeConverter(uk.ac.sussex.gdsc.core.data.utils.IdentityTypeConverter) Converter(uk.ac.sussex.gdsc.core.data.utils.Converter) TypeConverter(uk.ac.sussex.gdsc.core.data.utils.TypeConverter) PSFParameter(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSFParameter)

Aggregations

Converter (uk.ac.sussex.gdsc.core.data.utils.Converter)4 IdentityTypeConverter (uk.ac.sussex.gdsc.core.data.utils.IdentityTypeConverter)2 TypeConverter (uk.ac.sussex.gdsc.core.data.utils.TypeConverter)2 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)2 ConfigurationException (uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException)2 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 IJ (ij.IJ)1 Plot (ij.gui.Plot)1 PlugIn (ij.plugin.PlugIn)1 TextWindow (ij.text.TextWindow)1 Checkbox (java.awt.Checkbox)1 Color (java.awt.Color)1 TextField (java.awt.TextField)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1