Search in sources :

Example 1 with TypeConverter

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

the class CreateData method setNoise.

/**
 * Sets the noise in the results if missing.
 *
 * @param results the results
 * @param imp the imp
 */
private static void setNoise(MemoryPeakResults results, ImagePlus imp) {
    // Loaded results do not have noise
    if (results.hasNoise()) {
        return;
    }
    IJ.showStatus("Estimating noise ...");
    // Compute noise per frame
    final ImageStack stack = imp.getImageStack();
    final int width = stack.getWidth();
    final int height = stack.getHeight();
    final IJImageSource source = new IJImageSource(imp);
    final float[] noise = new float[source.getFrames() + 1];
    source.setReadHint(ReadHint.SEQUENTIAL);
    source.open();
    for (int slice = 1; slice < noise.length; slice++) {
        final float[] data = source.next();
        // Use the trimmed method as there may be a lot of spots in the frame
        noise[slice] = FitWorker.estimateNoise(data, width, height, NoiseEstimatorMethod.QUICK_RESIDUALS_LEAST_TRIMMED_OF_SQUARES);
    }
    // Statistics stats = Statistics.create(Arrays.copyOfRange(noise, 1, noise.length));
    // System.out.printf("Noise = %.3f +/- %.3f (%d)\n", stats.getMean(),
    // stats.getStandardDeviation(), stats.getN());
    // Convert noise units from counts to the result format
    final TypeConverter<IntensityUnit> c = results.getIntensityConverter(IntensityUnit.COUNT);
    for (int i = 0; i < noise.length; i++) {
        noise[i] = c.convertBack(noise[i]);
    }
    results.forEach((PeakResultProcedure) result -> {
        if (result.getFrame() < noise.length) {
            result.setNoise(noise[result.getFrame()]);
        }
    });
}
Also used : RandomUtils(uk.ac.sussex.gdsc.core.utils.rng.RandomUtils) LoadLocalisations(uk.ac.sussex.gdsc.smlm.ij.plugins.LoadLocalisations) Arrays(java.util.Arrays) PoissonSamplerUtils(uk.ac.sussex.gdsc.core.utils.rng.PoissonSamplerUtils) Molecule(uk.ac.sussex.gdsc.smlm.data.config.MoleculeProtos.Molecule) IdPeakResult(uk.ac.sussex.gdsc.smlm.results.IdPeakResult) ImageProcessor(ij.process.ImageProcessor) HistogramPlotBuilder(uk.ac.sussex.gdsc.core.ij.HistogramPlot.HistogramPlotBuilder) ConfigurationException(uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException) AiryPsfModel(uk.ac.sussex.gdsc.smlm.model.AiryPsfModel) GaussianPsfModel(uk.ac.sussex.gdsc.smlm.model.GaussianPsfModel) MemoryUtils(uk.ac.sussex.gdsc.core.utils.MemoryUtils) PSFType(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSFType) HelpUrls(uk.ac.sussex.gdsc.smlm.ij.plugins.HelpUrls) ResultsManager(uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsManager) GammaDistribution(org.apache.commons.math3.distribution.GammaDistribution) Future(java.util.concurrent.Future) Vector(java.util.Vector) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) Map(java.util.Map) DensityManager(uk.ac.sussex.gdsc.core.clustering.DensityManager) UnicodeReader(uk.ac.sussex.gdsc.core.utils.UnicodeReader) RadialFalloffIllumination(uk.ac.sussex.gdsc.smlm.model.RadialFalloffIllumination) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) RawResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.RawResultProcedure) ConcurrencyUtils(uk.ac.sussex.gdsc.core.utils.concurrent.ConcurrencyUtils) TextUtils(uk.ac.sussex.gdsc.core.utils.TextUtils) Gaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunction) Executors(java.util.concurrent.Executors) PeakFit(uk.ac.sussex.gdsc.smlm.ij.plugins.PeakFit) TIntHashSet(gnu.trove.set.hash.TIntHashSet) ImagePlus(ij.ImagePlus) LocalisationList(uk.ac.sussex.gdsc.smlm.ij.plugins.LoadLocalisations.LocalisationList) UniformDistribution(uk.ac.sussex.gdsc.smlm.model.UniformDistribution) ShortProcessor(ij.process.ShortProcessor) FileUtils(uk.ac.sussex.gdsc.core.utils.FileUtils) CameraModelFisherInformationAnalysis(uk.ac.sussex.gdsc.smlm.ij.plugins.CameraModelFisherInformationAnalysis) CalibrationProtosHelper(uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtosHelper) PlugIn(ij.plugin.PlugIn) LocalisationModelSet(uk.ac.sussex.gdsc.smlm.model.LocalisationModelSet) AiryPattern(uk.ac.sussex.gdsc.smlm.model.AiryPattern) Prefs(ij.Prefs) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) WindowManager(ij.WindowManager) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) CreateDataSettings(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.CreateDataSettings) GridDistribution(uk.ac.sussex.gdsc.smlm.model.GridDistribution) ArrayList(java.util.ArrayList) Atom(uk.ac.sussex.gdsc.smlm.data.config.MoleculeProtos.Atom) AutoThreshold(uk.ac.sussex.gdsc.core.threshold.AutoThreshold) GenericDialog(ij.gui.GenericDialog) ImagePsfModel(uk.ac.sussex.gdsc.smlm.model.ImagePsfModel) PsfModel(uk.ac.sussex.gdsc.smlm.model.PsfModel) PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) PoissonGaussianFisherInformation(uk.ac.sussex.gdsc.smlm.function.PoissonGaussianFisherInformation) ImagePsfHelper(uk.ac.sussex.gdsc.smlm.ij.settings.ImagePsfHelper) UnivariateLikelihoodFisherInformationCalculator(uk.ac.sussex.gdsc.smlm.fitting.UnivariateLikelihoodFisherInformationCalculator) SpatialIllumination(uk.ac.sussex.gdsc.smlm.model.SpatialIllumination) PeakResultsReader(uk.ac.sussex.gdsc.smlm.results.PeakResultsReader) PsfCombiner(uk.ac.sussex.gdsc.smlm.ij.plugins.PsfCombiner) DiffusionType(uk.ac.sussex.gdsc.smlm.model.DiffusionType) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) FitWorker(uk.ac.sussex.gdsc.smlm.engine.FitWorker) RandomGeneratorAdapter(uk.ac.sussex.gdsc.core.utils.rng.RandomGeneratorAdapter) AtomOrBuilder(uk.ac.sussex.gdsc.smlm.data.config.MoleculeProtos.AtomOrBuilder) IOException(java.io.IOException) SamplerUtils(uk.ac.sussex.gdsc.core.utils.rng.SamplerUtils) DiscreteUniformSampler(org.apache.commons.rng.sampling.distribution.DiscreteUniformSampler) File(java.io.File) SummaryStatistics(org.apache.commons.math3.stat.descriptive.SummaryStatistics) FloatProcessor(ij.process.FloatProcessor) BasePoissonFisherInformation(uk.ac.sussex.gdsc.smlm.function.BasePoissonFisherInformation) Paths(java.nio.file.Paths) AstigmatismZModel(uk.ac.sussex.gdsc.smlm.function.gaussian.AstigmatismZModel) TextFilePeakResults(uk.ac.sussex.gdsc.smlm.results.TextFilePeakResults) EmCcdCameraModel(uk.ac.sussex.gdsc.smlm.model.camera.EmCcdCameraModel) ImageStack(ij.ImageStack) PoissonSampler(org.apache.commons.rng.sampling.distribution.PoissonSampler) CameraModel(uk.ac.sussex.gdsc.smlm.model.camera.CameraModel) ExtendedPeakResult(uk.ac.sussex.gdsc.smlm.results.ExtendedPeakResult) PsfHelper(uk.ac.sussex.gdsc.smlm.data.config.PsfHelper) BufferedReader(java.io.BufferedReader) ImmutableMemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.ImmutableMemoryPeakResults) DoubleEquality(uk.ac.sussex.gdsc.core.utils.DoubleEquality) StandardResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.StandardResultProcedure) SphericalDistribution(uk.ac.sussex.gdsc.smlm.model.SphericalDistribution) TIntArrayList(gnu.trove.list.array.TIntArrayList) GaussianFilter(uk.ac.sussex.gdsc.smlm.filters.GaussianFilter) TextWindow(ij.text.TextWindow) StandardValueProcedure(uk.ac.sussex.gdsc.smlm.function.StandardValueProcedure) ImagePSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.ImagePSF) Formatter(java.util.Formatter) FixedLifetimeImageModel(uk.ac.sussex.gdsc.smlm.model.FixedLifetimeImageModel) StoredDataStatistics(uk.ac.sussex.gdsc.core.utils.StoredDataStatistics) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PeakResults(uk.ac.sussex.gdsc.smlm.results.PeakResults) DataException(uk.ac.sussex.gdsc.core.data.DataException) PsfCalculator(uk.ac.sussex.gdsc.smlm.ij.plugins.PsfCalculator) EmpiricalDistribution(org.apache.commons.math3.random.EmpiricalDistribution) MathUtils(uk.ac.sussex.gdsc.core.utils.MathUtils) BinomialDiscreteInverseCumulativeProbabilityFunction(uk.ac.sussex.gdsc.core.utils.rng.BinomialDiscreteInverseCumulativeProbabilityFunction) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) Offset(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.Offset) TextFormat(com.google.protobuf.TextFormat) PsfProtosHelper(uk.ac.sussex.gdsc.smlm.data.config.PsfProtosHelper) SettingsManager(uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager) ItemEvent(java.awt.event.ItemEvent) CameraType(uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.CameraType) SpatialDistribution(uk.ac.sussex.gdsc.smlm.model.SpatialDistribution) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) AstigmatismModelManager(uk.ac.sussex.gdsc.smlm.ij.plugins.AstigmatismModelManager) UniformRealDistribution(org.apache.commons.math3.distribution.UniformRealDistribution) MoleculeModel(uk.ac.sussex.gdsc.smlm.model.MoleculeModel) TIntIntHashMap(gnu.trove.map.hash.TIntIntHashMap) MarsagliaTsangGammaSampler(uk.ac.sussex.gdsc.core.utils.rng.MarsagliaTsangGammaSampler) WidthResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.WidthResultProcedure) Gaussian2DPeakResultHelper(uk.ac.sussex.gdsc.smlm.results.Gaussian2DPeakResultHelper) List(java.util.List) FluorophoreSequenceModel(uk.ac.sussex.gdsc.smlm.model.FluorophoreSequenceModel) BirResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.BirResultProcedure) Entry(java.util.Map.Entry) CompoundMoleculeModel(uk.ac.sussex.gdsc.smlm.model.CompoundMoleculeModel) RealDistribution(org.apache.commons.math3.distribution.RealDistribution) HoltzerAstigmatismZModel(uk.ac.sussex.gdsc.smlm.function.gaussian.HoltzerAstigmatismZModel) ImageModel(uk.ac.sussex.gdsc.smlm.model.ImageModel) MaskDistribution3D(uk.ac.sussex.gdsc.smlm.model.MaskDistribution3D) TypeConverter(uk.ac.sussex.gdsc.core.data.utils.TypeConverter) PrecisionResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PrecisionResultProcedure) Rectangle(java.awt.Rectangle) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF) WindowOrganiser(uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser) IntensityUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.IntensityUnit) Mixture(uk.ac.sussex.gdsc.smlm.data.config.MoleculeProtos.Mixture) AtomicReference(java.util.concurrent.atomic.AtomicReference) SobolSequenceGenerator(org.apache.commons.math3.random.SobolSequenceGenerator) MaskDistribution(uk.ac.sussex.gdsc.smlm.model.MaskDistribution) OptionListener(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog.OptionListener) OpenDialog(ij.io.OpenDialog) ParameterUtils(uk.ac.sussex.gdsc.smlm.ij.plugins.ParameterUtils) IJImageSource(uk.ac.sussex.gdsc.smlm.ij.IJImageSource) NoiseEstimatorMethod(uk.ac.sussex.gdsc.smlm.data.config.FitProtos.NoiseEstimatorMethod) ReadHint(uk.ac.sussex.gdsc.smlm.results.ImageSource.ReadHint) ExponentialDistribution(org.apache.commons.math3.distribution.ExponentialDistribution) FisherInformationMatrix(uk.ac.sussex.gdsc.smlm.fitting.FisherInformationMatrix) UnitHelper(uk.ac.sussex.gdsc.smlm.data.config.UnitHelper) LinkedList(java.util.LinkedList) CcdCameraModel(uk.ac.sussex.gdsc.smlm.model.camera.CcdCameraModel) FileSaver(ij.io.FileSaver) Statistics(uk.ac.sussex.gdsc.core.utils.Statistics) ExecutorService(java.util.concurrent.ExecutorService) CreateDataSettingsHelper(uk.ac.sussex.gdsc.smlm.ij.settings.CreateDataSettingsHelper) TFloatArrayList(gnu.trove.list.array.TFloatArrayList) FunctionHelper(uk.ac.sussex.gdsc.smlm.function.FunctionHelper) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) PoissonSamplerCache(org.apache.commons.rng.sampling.distribution.PoissonSamplerCache) AstigmatismModel(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel) ConversionException(uk.ac.sussex.gdsc.core.data.utils.ConversionException) SplitMix64(org.apache.commons.rng.core.source64.SplitMix64) Checkbox(java.awt.Checkbox) ActivationEnergyImageModel(uk.ac.sussex.gdsc.smlm.model.ActivationEnergyImageModel) LoadLocalisationsSettings(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.LoadLocalisationsSettings) FileInputStream(java.io.FileInputStream) Ticker(uk.ac.sussex.gdsc.core.logging.Ticker) PsfModelGradient1Function(uk.ac.sussex.gdsc.smlm.model.PsfModelGradient1Function) InterpolatedPoissonFisherInformation(uk.ac.sussex.gdsc.smlm.function.InterpolatedPoissonFisherInformation) Consumer(java.util.function.Consumer) CameraModelManager(uk.ac.sussex.gdsc.smlm.ij.plugins.CameraModelManager) LocalisationModel(uk.ac.sussex.gdsc.smlm.model.LocalisationModel) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) UniformIllumination(uk.ac.sussex.gdsc.smlm.model.UniformIllumination) SynchronizedPeakResults(uk.ac.sussex.gdsc.smlm.results.SynchronizedPeakResults) IJ(ij.IJ) NullArgumentException(org.apache.commons.math3.exception.NullArgumentException) SmlmUsageTracker(uk.ac.sussex.gdsc.smlm.ij.plugins.SmlmUsageTracker) InverseTransformDiscreteSampler(org.apache.commons.rng.sampling.distribution.InverseTransformDiscreteSampler) Comparator(java.util.Comparator) Collections(java.util.Collections) NormalizedGaussianSampler(org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) UniformRandomProviders(uk.ac.sussex.gdsc.core.utils.rng.UniformRandomProviders) IJImageSource(uk.ac.sussex.gdsc.smlm.ij.IJImageSource) ImageStack(ij.ImageStack) IntensityUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.IntensityUnit) ReadHint(uk.ac.sussex.gdsc.smlm.results.ImageSource.ReadHint)

Example 2 with TypeConverter

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

the class CreateData method loadBenchmarkData.

/**
 * Load benchmark data using an open image and a XYZ text file.
 */
private void loadBenchmarkData() {
    if (!showLoadDialog()) {
        // resetMemory();
        return;
    }
    // Load the image
    final ImagePlus imp = WindowManager.getImage(benchmarkImage);
    if (imp == null) {
        IJ.error(TITLE, "No benchmark image: " + benchmarkImage);
        // resetMemory();
        return;
    }
    // Load the results
    final MemoryPeakResults results = getSimulationResults();
    if (results == null) {
        IJ.error(TITLE, "No benchmark results: " + benchmarkResultsName);
        // resetMemory();
        return;
    }
    results.setName(imp.getTitle() + " (Results)");
    results.setBounds(new Rectangle(0, 0, imp.getWidth(), imp.getHeight()));
    final IJImageSource imageSource = new IJImageSource(imp);
    results.setSource(imageSource);
    // Load the settings as these are used in the dialog
    settings = SettingsManager.readCreateDataSettings(0).toBuilder();
    simulationParameters = showSimulationParametersDialog(imp, results);
    if (simulationParameters != null) {
        // Convert data to allow analysis as if a Gaussian2D PSF
        final boolean isGaussian2D = PsfHelper.isGaussian2D(results.getPsf());
        if (isGaussian2D) {
            Gaussian2DPeakResultHelper.addMeanIntensity(results.getPsf(), results);
        } else if (simulationParameters.sd > 0) {
            final TypeConverter<DistanceUnit> dc = results.getDistanceConverter(DistanceUnit.NM);
            final PSF.Builder psf = PsfProtosHelper.getDefaultPsf(PSFType.ONE_AXIS_GAUSSIAN_2D).toBuilder();
            psf.getParametersBuilder(0).setValue(dc.convertBack(simulationParameters.sd));
            results.setPsf(psf.build());
            // Update all the results. This assumes the results do not have data for a custom PSF,
            // i.e. the parameters only have [t,i,x,y,z]
            final LocalList<PeakResult> newResults = new LocalList<>(results.size());
            final float sd = (float) dc.convertBack(simulationParameters.sd);
            final double meanFactor = Gaussian2DPeakResultHelper.getMeanSignalUsingP05(1, sd, sd);
            results.forEach((PeakResultProcedure) r -> {
                final PeakResult peak = r.resize(PeakResult.STANDARD_PARAMETERS + 1);
                peak.setMeanIntensity((float) (peak.getIntensity() * meanFactor));
                peak.setParameter(PeakResult.STANDARD_PARAMETERS, sd);
                newResults.add(peak);
            });
            results.begin();
            results.addAll(newResults);
            results.end();
        }
        setBackground(results);
        setNoise(results, imp);
        setBenchmarkResults(imp, results);
        IJ.showStatus("Loaded " + TextUtils.pleural(results.size(), "result"));
    } else {
        resetMemory();
    }
}
Also used : TypeConverter(uk.ac.sussex.gdsc.core.data.utils.TypeConverter) IJImageSource(uk.ac.sussex.gdsc.smlm.ij.IJImageSource) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) HistogramPlotBuilder(uk.ac.sussex.gdsc.core.ij.HistogramPlot.HistogramPlotBuilder) AtomOrBuilder(uk.ac.sussex.gdsc.smlm.data.config.MoleculeProtos.AtomOrBuilder) Rectangle(java.awt.Rectangle) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) ImmutableMemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.ImmutableMemoryPeakResults) ImagePlus(ij.ImagePlus) IdPeakResult(uk.ac.sussex.gdsc.smlm.results.IdPeakResult) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) ExtendedPeakResult(uk.ac.sussex.gdsc.smlm.results.ExtendedPeakResult)

Example 3 with TypeConverter

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

the class TraceExporter method exportSpotOn.

private void exportSpotOn(MemoryPeakResults results) {
    try (BufferedWriter out = Files.newBufferedWriter(Paths.get(settings.directory, results.getName() + ".csv"))) {
        out.write("frame,t,trajectory,x,y");
        out.newLine();
        final TypeConverter<TimeUnit> converter = UnitConverterUtils.createConverter(TimeUnit.FRAME, TimeUnit.SECOND, results.getCalibrationReader().getExposureTime());
        @SuppressWarnings("resource") final BufferedWriter writer = out;
        results.forEach(DistanceUnit.UM, (XyrResultProcedure) (x, y, result) -> {
            try {
                if (result.hasEndFrame()) {
                    final String sId = Integer.toString(result.getId());
                    final String sx = Float.toString(x);
                    final String sy = Float.toString(y);
                    for (int t = result.getFrame(); t <= result.getEndFrame(); t++) {
                        writer.write(Integer.toString(t));
                        writer.write(",");
                        writer.write(Float.toString(converter.convert(t)));
                        writer.write(",");
                        writer.write(sId);
                        writer.write(",");
                        writer.write(sx);
                        writer.write(",");
                        writer.write(sy);
                        writer.newLine();
                    }
                } else {
                    writer.write(Integer.toString(result.getFrame()));
                    writer.write(",");
                    writer.write(Float.toString(converter.convert(result.getFrame())));
                    writer.write(",");
                    writer.write(Integer.toString(result.getId()));
                    writer.write(",");
                    writer.write(Float.toString(x));
                    writer.write(",");
                    writer.write(Float.toString(y));
                    writer.newLine();
                }
            } catch (final IOException ex) {
                throw new RuntimeException(ex);
            }
        });
    } catch (final IOException | RuntimeException ex) {
        handleException(ex);
    }
}
Also used : MemoryResultsList(uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsManager.MemoryResultsList) Cell(us.hebi.matlab.mat.types.Cell) UnitConverterUtils(uk.ac.sussex.gdsc.smlm.data.config.UnitConverterUtils) IdFramePeakResultComparator(uk.ac.sussex.gdsc.smlm.results.sort.IdFramePeakResultComparator) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) MatFile(us.hebi.matlab.mat.types.MatFile) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matrix(us.hebi.matlab.mat.types.Matrix) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) MultiDialog(uk.ac.sussex.gdsc.core.ij.gui.MultiDialog) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) SettingsManager(uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager) XyrResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.XyrResultProcedure) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) IOException(java.io.IOException) NamedObject(uk.ac.sussex.gdsc.smlm.data.NamedObject) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) Logger(java.util.logging.Logger) SamplerUtils(uk.ac.sussex.gdsc.core.utils.rng.SamplerUtils) AttributePeakResult(uk.ac.sussex.gdsc.smlm.results.AttributePeakResult) TextUtils(uk.ac.sussex.gdsc.core.utils.TextUtils) Plot(ij.gui.Plot) TIntHashSet(gnu.trove.set.hash.TIntHashSet) TimeUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.TimeUnit) XyzrResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.XyzrResultProcedure) List(java.util.List) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) Paths(java.nio.file.Paths) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) IJ(ij.IJ) SimpleArrayUtils(uk.ac.sussex.gdsc.core.utils.SimpleArrayUtils) Mat5(us.hebi.matlab.mat.format.Mat5) PlugIn(ij.plugin.PlugIn) NormalizedGaussianSampler(org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler) TypeConverter(uk.ac.sussex.gdsc.core.data.utils.TypeConverter) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) UniformRandomProviders(uk.ac.sussex.gdsc.core.utils.rng.UniformRandomProviders) TimeUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.TimeUnit) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 4 with TypeConverter

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

the class TraceExporter method export.

private void export(MemoryPeakResults results) {
    // Copy to allow manipulation
    results = results.copy();
    // Strip results with no trace Id
    results.removeIf(result -> result.getId() <= 0);
    // Sort by ID then time
    results.sort(IdFramePeakResultComparator.INSTANCE);
    // Split traces with big jumps and long tracks into smaller tracks
    results = splitTraces(results);
    results = splitLongTraces(results);
    // Count each ID and remove short traces
    int id = 0;
    int count = 0;
    int tracks = 0;
    int maxLength = 0;
    final TIntHashSet remove = new TIntHashSet();
    for (int i = 0, size = results.size(); i < size; i++) {
        final PeakResult result = results.get(i);
        if (result.getId() != id) {
            if (count < settings.minLength) {
                remove.add(id);
            } else {
                tracks++;
                maxLength = Math.max(maxLength, count);
            }
            count = 0;
            id = result.getId();
        }
        count += getLength(result);
    }
    // Final ID
    if (count < settings.minLength) {
        remove.add(id);
    } else {
        tracks++;
        maxLength = Math.max(maxLength, count);
    }
    if (!remove.isEmpty()) {
        results.removeIf(result -> remove.contains(result.getId()));
        results.sort(IdFramePeakResultComparator.INSTANCE);
    }
    if (settings.wobble > 0) {
        // Just leave any exceptions to trickle up and kill the plugin
        final TypeConverter<DistanceUnit> c = results.getDistanceConverter(DistanceUnit.NM);
        final double w = c.convertBack(settings.wobble);
        final UniformRandomProvider rng = UniformRandomProviders.create();
        final NormalizedGaussianSampler gauss = SamplerUtils.createNormalizedGaussianSampler(rng);
        final boolean is3D = results.is3D();
        results.forEach((PeakResultProcedure) peakResult -> {
            peakResult.setXPosition((float) (peakResult.getXPosition() + w * gauss.sample()));
            peakResult.setYPosition((float) (peakResult.getYPosition() + w * gauss.sample()));
            if (is3D) {
                peakResult.setZPosition((float) (peakResult.getZPosition() + w * gauss.sample()));
            }
        });
    }
    if (settings.format == 2) {
        exportVbSpt(results);
    } else if (settings.format == 1) {
        exportAnaDda(results);
    } else {
        exportSpotOn(results);
    }
    ImageJUtils.log("Exported %s: %s in %s", results.getName(), TextUtils.pleural(results.size(), "localisation"), TextUtils.pleural(tracks, "track"));
    if (settings.showTraceLengths) {
        // We store and index (count-1)
        final int[] h = new int[maxLength];
        id = 0;
        for (int i = 0, size = results.size(); i < size; i++) {
            final PeakResult result = results.get(i);
            if (result.getId() != id) {
                h[count - 1]++;
                count = 0;
                id = result.getId();
            }
            count += getLength(result);
        }
        h[count - 1]++;
        final String title = TITLE + ": " + results.getName();
        final Plot plot = new Plot(title, "Length", "Frequency");
        plot.addPoints(SimpleArrayUtils.newArray(h.length, 1, 1.0f), SimpleArrayUtils.toFloat(h), Plot.BAR);
        plot.setLimits(SimpleArrayUtils.findIndex(h, i -> i != 0), maxLength + 1, 0, Double.NaN);
        ImageJUtils.display(title, plot);
    }
}
Also used : MemoryResultsList(uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsManager.MemoryResultsList) Cell(us.hebi.matlab.mat.types.Cell) UnitConverterUtils(uk.ac.sussex.gdsc.smlm.data.config.UnitConverterUtils) IdFramePeakResultComparator(uk.ac.sussex.gdsc.smlm.results.sort.IdFramePeakResultComparator) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) MatFile(us.hebi.matlab.mat.types.MatFile) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matrix(us.hebi.matlab.mat.types.Matrix) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) MultiDialog(uk.ac.sussex.gdsc.core.ij.gui.MultiDialog) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) SettingsManager(uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager) XyrResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.XyrResultProcedure) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) IOException(java.io.IOException) NamedObject(uk.ac.sussex.gdsc.smlm.data.NamedObject) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) Logger(java.util.logging.Logger) SamplerUtils(uk.ac.sussex.gdsc.core.utils.rng.SamplerUtils) AttributePeakResult(uk.ac.sussex.gdsc.smlm.results.AttributePeakResult) TextUtils(uk.ac.sussex.gdsc.core.utils.TextUtils) Plot(ij.gui.Plot) TIntHashSet(gnu.trove.set.hash.TIntHashSet) TimeUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.TimeUnit) XyzrResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.XyzrResultProcedure) List(java.util.List) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) Paths(java.nio.file.Paths) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) IJ(ij.IJ) SimpleArrayUtils(uk.ac.sussex.gdsc.core.utils.SimpleArrayUtils) Mat5(us.hebi.matlab.mat.format.Mat5) PlugIn(ij.plugin.PlugIn) NormalizedGaussianSampler(org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler) TypeConverter(uk.ac.sussex.gdsc.core.data.utils.TypeConverter) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) UniformRandomProviders(uk.ac.sussex.gdsc.core.utils.rng.UniformRandomProviders) Plot(ij.gui.Plot) TIntHashSet(gnu.trove.set.hash.TIntHashSet) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) AttributePeakResult(uk.ac.sussex.gdsc.smlm.results.AttributePeakResult) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) NormalizedGaussianSampler(org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler)

Example 5 with TypeConverter

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

the class SpotInspector method mouseClicked.

private void mouseClicked(MouseEvent event) {
    if (id != currentId.get()) {
        return;
    }
    // Show the result that was double clicked in the result table
    if (event.getClickCount() > 1) {
        final int rank = textPanel.getSelectionStart() + 1;
        // Show the spot that was double clicked
        final ImagePlus imp = WindowManager.getImage(TITLE);
        if (imp != null && rank > 0 && rank <= imp.getStackSize()) {
            imp.setSlice(rank);
            if (imp.getWindow() != null) {
                imp.getWindow().toFront();
            }
            final PeakResult r = rankedResults.get(rank - 1).peakResult;
            final TypeConverter<DistanceUnit> dc = results.getDistanceConverter(DistanceUnit.PIXEL);
            final float rx = dc.convert(r.getXPosition());
            final float ry = dc.convert(r.getYPosition());
            final int x = (int) rx;
            final int y = (int) ry;
            // Find bounds
            final int minX = x - settings.radius;
            final int minY = y - settings.radius;
            // Require the Shift key to add all spots
            if (!event.isShiftDown()) {
                // Add the single clicked spot
                imp.setRoi(new OffsetPointRoi(rx - minX, ry - minY));
                return;
            }
            // Add all the spots
            final int maxX = x + settings.radius + 1;
            final int maxY = y + settings.radius + 1;
            // Create ROIs
            final HashSet<Point2D.Float> spots = new HashSet<>();
            results.forEach(DistanceUnit.PIXEL, (XyResultProcedure) (xp, yp) -> {
                if (xp > minX && xp < maxX && yp > minY && yp < maxY) {
                    // Use only unique points
                    spots.add(new Point2D.Float(xp - minX, yp - minY));
                }
            });
            final int points = spots.size();
            final float[] ox = new float[points];
            final float[] oy = new float[points];
            final Counter c = new Counter();
            spots.forEach(p -> {
                ox[c.getCount()] = p.x;
                oy[c.getAndIncrement()] = p.y;
            });
            imp.setRoi(new OffsetPointRoi(ox, oy, points));
        }
    }
}
Also used : PrecisionResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PrecisionResultProcedure) Rectangle(java.awt.Rectangle) Point2D(java.awt.geom.Point2D) ImageProcessor(ij.process.ImageProcessor) HistogramPlotBuilder(uk.ac.sussex.gdsc.core.ij.HistogramPlot.HistogramPlotBuilder) PSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF) WindowManager(ij.WindowManager) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) IntensityUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.IntensityUnit) ImageSource(uk.ac.sussex.gdsc.smlm.results.ImageSource) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) PointRoi(ij.gui.PointRoi) HashSet(java.util.HashSet) XyResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.XyResultProcedure) StoredDataStatistics(uk.ac.sussex.gdsc.core.utils.StoredDataStatistics) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) ReadHint(uk.ac.sussex.gdsc.smlm.results.ImageSource.ReadHint) MouseAdapter(java.awt.event.MouseAdapter) PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) MathUtils(uk.ac.sussex.gdsc.core.utils.MathUtils) FitConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitConfiguration) TextPanel(ij.text.TextPanel) HeightResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.HeightResultProcedure) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) InputSource(uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsManager.InputSource) OffsetPointRoi(uk.ac.sussex.gdsc.core.ij.gui.OffsetPointRoi) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) WidthResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.WidthResultProcedure) Plot(ij.gui.Plot) MouseEvent(java.awt.event.MouseEvent) ImageJTablePeakResults(uk.ac.sussex.gdsc.smlm.ij.results.ImageJTablePeakResults) ImagePlus(ij.ImagePlus) FloatProcessor(ij.process.FloatProcessor) List(java.util.List) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) IJ(ij.IJ) DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) ImageStack(ij.ImageStack) PsfHelper(uk.ac.sussex.gdsc.smlm.data.config.PsfHelper) PlugIn(ij.plugin.PlugIn) Collections(java.util.Collections) TypeConverter(uk.ac.sussex.gdsc.core.data.utils.TypeConverter) StandardResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.StandardResultProcedure) OffsetPointRoi(uk.ac.sussex.gdsc.core.ij.gui.OffsetPointRoi) ImagePlus(ij.ImagePlus) ReadHint(uk.ac.sussex.gdsc.smlm.results.ImageSource.ReadHint) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) HashSet(java.util.HashSet)

Aggregations

TypeConverter (uk.ac.sussex.gdsc.core.data.utils.TypeConverter)6 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)6 PeakResult (uk.ac.sussex.gdsc.smlm.results.PeakResult)6 PeakResultProcedure (uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure)6 IJ (ij.IJ)5 PlugIn (ij.plugin.PlugIn)5 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)5 DistanceUnit (uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 ImageJUtils (uk.ac.sussex.gdsc.core.ij.ImageJUtils)4 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)4 SettingsManager (uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager)4 TIntHashSet (gnu.trove.set.hash.TIntHashSet)3 ImagePlus (ij.ImagePlus)3 Plot (ij.gui.Plot)3 Rectangle (java.awt.Rectangle)3 BufferedWriter (java.io.BufferedWriter)3 IOException (java.io.IOException)3