Search in sources :

Example 46 with TIntHashSet

use of gnu.trove.set.hash.TIntHashSet in project GDSC-SMLM by aherbert.

the class CreateData method combineSimulationSteps.

private List<LocalisationModelSet> combineSimulationSteps(List<LocalisationModel> localisations) {
    // Allow fractional integration steps
    final double simulationStepsPerFrame = (settings.getStepsPerSecond() * settings.getExposureTime()) / 1000.0;
    final List<LocalisationModelSet> newLocalisations = new ArrayList<>((int) (localisations.size() / simulationStepsPerFrame));
    // System.out.printf("combineSimulationSteps @ %f\n", simulationStepsPerFrame);
    // final double gain = new CreateDataSettingsHelper(settings).getTotalGainSafe();
    sortLocalisationsByIdThenTime(localisations);
    final int[] idList = getIds(localisations);
    movingMolecules = new TIntHashSet(idList.length);
    int index = 0;
    for (final int id : idList) {
        final int fromIndex = findIndexById(localisations, index, id);
        if (fromIndex > -1) {
            final int toIndex = findLastIndexById(localisations, fromIndex, id);
            final List<LocalisationModel> subset = localisations.subList(fromIndex, toIndex + 1);
            index = toIndex;
            // Store the IDs of any moving molecules
            if (isMoving(subset)) {
                movingMolecules.add(id);
            }
            // The frames may be longer or shorter than the simulation steps. Allocate the step
            // proportionately to each frame it overlaps:
            // 
            // Steps: |-- 0 --|-- 1 --|-- 2 --|--
            // Frames: |--- 0 ---|--- 1 ---|--- 2 ---|
            // 
            // ^ ^
            // | |
            // | End frame
            // |
            // Start frame
            final double firstFrame = getStartFrame(subset.get(0), simulationStepsPerFrame);
            final double lastFrame = getEndFrame(subset.get(subset.size() - 1), simulationStepsPerFrame);
            // Get the first frame offset and allocate space to store all potential frames
            final int intFirstFrame = (int) firstFrame;
            final int intLastFrame = (int) Math.ceil(lastFrame);
            final LocalisationModelSet[] sets = new LocalisationModelSet[intLastFrame - intFirstFrame + 1];
            // Process each step
            for (final LocalisationModel l : subset) {
                // Get the fractional start and end frames
                final double startFrame = getStartFrame(l, simulationStepsPerFrame);
                final double endFrame = getEndFrame(l, simulationStepsPerFrame);
                // Round down to get the actual frames that are overlapped
                final int start = (int) startFrame;
                int end = (int) endFrame;
                // that frame
                if (end > start && endFrame == end) {
                    // E.g. convert
                    // Steps: |-- 0 --|
                    // Frames: |- 0 -|- 1 -|- 2 -|
                    // to
                    // Steps: |-- 0 --|
                    // Frames: |- 0 -|- 1 -|
                    end--;
                }
                if (start == end) {
                    // If the step falls within one frame then add it to the set
                    final int tIndex = start - intFirstFrame;
                    if (sets[tIndex] == null) {
                        sets[tIndex] = new LocalisationModelSet(id, start);
                    }
                    sets[tIndex].add(l);
                } else {
                    // Add the localisation to all the frames that the step spans
                    final double total = endFrame - startFrame;
                    // double t = 0;
                    for (int frame = start; frame <= end; frame++) {
                        // Get the fraction to allocate to this frame
                        double fraction;
                        int state = (l.isContinuous() ? LocalisationModel.CONTINUOUS : 0);
                        if (frame == start) {
                            state |= LocalisationModel.NEXT | (l.hasPrevious() ? LocalisationModel.PREVIOUS : 0);
                            // start
                            if (startFrame == start) {
                                fraction = 1;
                            } else {
                                fraction = (Math.ceil(startFrame) - startFrame);
                            }
                        } else if (frame == end) {
                            state |= LocalisationModel.PREVIOUS | (l.hasNext() ? LocalisationModel.NEXT : 0);
                            // |=====|----|
                            // | |
                            // | endFrame
                            // end
                            fraction = (endFrame - end);
                        } else {
                            state |= LocalisationModel.CONTINUOUS;
                            fraction = 1;
                        }
                        // t += fraction;
                        // Add to the set
                        final int tIndex = frame - intFirstFrame;
                        if (sets[tIndex] == null) {
                            sets[tIndex] = new LocalisationModelSet(id, frame);
                        }
                        sets[tIndex].add(getFraction(l, fraction / total, state));
                    }
                // if (t < total * 0.98)
                // {
                // System.out.printf("Total error %g < %g : %f (%d) -> %f (%d)\n", t, total, startFrame,
                // start, endFrame, end);
                // }
                }
            }
            LocalisationModelSet previous = null;
            for (int i = 0; i < sets.length; i++) {
                if (sets[i] != null) {
                    sets[i].setPrevious(previous);
                    // Create a data array and store the current intensity.
                    // This is used later to filter based on SNR
                    sets[i].setData(new double[] { // * gain
                    0, // * gain
                    0, // * gain
                    0, // * gain
                    0, // * gain
                    sets[i].getIntensity() });
                    newLocalisations.add(sets[i]);
                }
                previous = sets[i];
            }
        }
    }
    // Sort by time
    Collections.sort(newLocalisations, (r1, r2) -> Integer.compare(r1.getTime(), r2.getTime()));
    return newLocalisations;
}
Also used : LocalisationModel(uk.ac.sussex.gdsc.smlm.model.LocalisationModel) ArrayList(java.util.ArrayList) TIntArrayList(gnu.trove.list.array.TIntArrayList) TFloatArrayList(gnu.trove.list.array.TFloatArrayList) LocalisationModelSet(uk.ac.sussex.gdsc.smlm.model.LocalisationModelSet) TIntHashSet(gnu.trove.set.hash.TIntHashSet) ReadHint(uk.ac.sussex.gdsc.smlm.results.ImageSource.ReadHint)

Example 47 with TIntHashSet

use of gnu.trove.set.hash.TIntHashSet in project GDSC-SMLM by aherbert.

the class BlinkEstimatorTest method findOptimalFittedPoints.

@SeededTest
void findOptimalFittedPoints(RandomSeed seed) {
    // Skip this as it is slow
    Assumptions.assumeTrue(false);
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    final int particles = 1000;
    final double fixedFraction = 1;
    for (final boolean timeAtLowerBound : new boolean[] { false }) {
        final int[] count = new int[MAX_FITTED_POINTS + 1];
        int tests = 0;
        for (int run = 0; run < 3; run++) {
            for (final double n : blinkingRate) {
                for (int i = 0; i < ton.length; i++) {
                    tests++;
                    final TIntHashSet ok = estimateBlinking(rg, n, ton[i], toff[i], particles, fixedFraction, timeAtLowerBound, false);
                    ok.forEach(new TIntProcedure() {

                        @Override
                        public boolean execute(int value) {
                            count[value]++;
                            return true;
                        }
                    });
                }
            }
        }
        logger.info(FunctionUtils.getSupplier("Time@LowerBound = %b", timeAtLowerBound));
        for (int n = MIN_FITTED_POINTS; n <= MAX_FITTED_POINTS; n++) {
            if (logger.isLoggable(Level.INFO)) {
                final StringBuilder sb = new StringBuilder();
                TextUtils.formatTo(sb, "%2d = %2d/%2d |", n, count[n], tests);
                for (int i = 0; i < count[n]; i++) {
                    sb.append('-');
                }
                logger.info(sb.toString());
            }
        }
    }
}
Also used : TIntProcedure(gnu.trove.procedure.TIntProcedure) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) TIntHashSet(gnu.trove.set.hash.TIntHashSet) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 48 with TIntHashSet

use of gnu.trove.set.hash.TIntHashSet in project GDSC-SMLM by aherbert.

the class TraceManager method getTraces.

/**
 * Gets the traces that have been found using {@link #traceMolecules(double, int)}.
 *
 * @return The traces
 */
public Trace[] getTraces() {
    final PeakResult[] peakResults = results.toArray();
    // No tracing yet performed or no thresholds
    if (totalTraces == localisations.length) {
        if (filterActivationFrames) {
            final ArrayList<Trace> traces = new ArrayList<>();
            for (int i = 0; i < totalTraces; i++) {
                final PeakResult peakResult = peakResults[localisations[i].id];
                if (!outsideActivationWindow(peakResult.getFrame())) {
                    traces.add(new Trace(peakResult));
                }
            }
            return traces.toArray(new Trace[0]);
        }
        final Trace[] traces = new Trace[localisations.length];
        for (int i = 0; i < traces.length; i++) {
            traces[i] = new Trace(peakResults[localisations[i].id]);
        }
        return traces;
    }
    if (tracker != null) {
        tracker.progress(0);
    }
    // Build the list of traces
    final Trace[] traces = new Trace[getTotalTraces()];
    int count = 0;
    // for (int index = 0; index < localisations.length; index++)
    // if (localisations[index].trace == 0)
    // System.out.printf("error @ %d\n", index);
    // Since the trace numbers are allocated by processing the spots in frames, each frame can have
    // trace number out-of-order. This occurs if re-allocation has been performed,
    // e.g. [1,2,2,1,3] => [1,2,5,4,3] when spots in group 1 are reallocated before spots in group
    // 2.
    final TIntHashSet processedTraces = new TIntHashSet(traces.length);
    for (int i = 0; i < localisations.length; i++) {
        if (tracker != null && i % 256 == 0) {
            tracker.progress(i, localisations.length);
        }
        final int traceId = localisations[i].trace;
        if (!processedTraces.add(traceId)) {
            // Already present
            continue;
        }
        if (filterActivationFrames && outsideActivationWindow(localisations[i].time)) {
            continue;
        }
        final PeakResult peakResult = peakResults[localisations[i].id];
        final Trace nextTrace = new Trace(peakResult);
        nextTrace.setId(traceId);
        final int tLimit = maxTime[traceId];
        // Check if the trace has later frames
        if (tLimit > localisations[i].time) {
            for (int j = i + 1; j < localisations.length; j++) {
                if (localisations[j].time > tLimit) {
                    break;
                }
                if (localisations[j].trace == traceId) {
                    nextTrace.add(peakResults[localisations[j].id]);
                }
            }
        }
        traces[count++] = nextTrace;
    }
    if (tracker != null) {
        tracker.progress(1.0);
    }
    return traces;
}
Also used : ArrayList(java.util.ArrayList) TIntHashSet(gnu.trove.set.hash.TIntHashSet)

Example 49 with TIntHashSet

use of gnu.trove.set.hash.TIntHashSet in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class SpatialDetector method calculateSpatialOccupation.

protected void calculateSpatialOccupation() {
    nextQueue.add(firstBlock.getY() + maxRange * maxRangeHalved + maxRangeSquared * maxRangeHalved);
    MutableBlockPos inRealWorld = new MutableBlockPos();
    int hash;
    while (!nextQueue.isEmpty() && !cleanHouse) {
        TIntIterator queueIter = nextQueue.iterator();
        foundSet.addAll(nextQueue);
        nextQueue = new TIntHashSet();
        while (queueIter.hasNext()) {
            hash = queueIter.next();
            setPosWithRespectTo(hash, firstBlock, inRealWorld);
            if (corners) {
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY() - 1, inRealWorld.getZ() - 1, hash - maxRange - 1 - maxRangeSquared);
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY() - 1, inRealWorld.getZ(), hash - maxRange - 1);
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY() - 1, inRealWorld.getZ() + 1, hash - maxRange - 1 + maxRangeSquared);
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY(), inRealWorld.getZ() - 1, hash - maxRange - maxRangeSquared);
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY(), inRealWorld.getZ(), hash - maxRange);
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY(), inRealWorld.getZ() + 1, hash - maxRange + maxRangeSquared);
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY() + 1, inRealWorld.getZ() - 1, hash - maxRange + 1 - maxRangeSquared);
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY() + 1, inRealWorld.getZ(), hash - maxRange + 1);
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY() + 1, inRealWorld.getZ() + 1, hash - maxRange + 1 + maxRangeSquared);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY() - 1, inRealWorld.getZ() - 1, hash - 1 - maxRangeSquared);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY() - 1, inRealWorld.getZ(), hash - 1);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY() - 1, inRealWorld.getZ() + 1, hash - 1 + maxRangeSquared);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY(), inRealWorld.getZ() - 1, hash - maxRangeSquared);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY(), inRealWorld.getZ() + 1, hash + maxRangeSquared);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY() + 1, inRealWorld.getZ() - 1, hash + 1 - maxRangeSquared);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY() + 1, inRealWorld.getZ(), hash + 1);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY() + 1, inRealWorld.getZ() + 1, hash + 1 + maxRangeSquared);
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY() - 1, inRealWorld.getZ() - 1, hash + maxRange - 1 - maxRangeSquared);
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY() - 1, inRealWorld.getZ(), hash + maxRange - 1);
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY() - 1, inRealWorld.getZ() + 1, hash + maxRange - 1 + maxRangeSquared);
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY(), inRealWorld.getZ() - 1, hash + maxRange - maxRangeSquared);
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY(), inRealWorld.getZ(), hash + maxRange);
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY(), inRealWorld.getZ() + 1, hash + maxRange + maxRangeSquared);
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY() + 1, inRealWorld.getZ() - 1, hash + maxRange + 1 - maxRangeSquared);
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY() + 1, inRealWorld.getZ(), hash + maxRange + 1);
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY() + 1, inRealWorld.getZ() + 1, hash + maxRange + 1 + maxRangeSquared);
            } else {
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY(), inRealWorld.getZ(), hash + maxRange);
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY(), inRealWorld.getZ(), hash - maxRange);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY() + 1, inRealWorld.getZ(), hash + 1);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY() - 1, inRealWorld.getZ(), hash - 1);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY(), inRealWorld.getZ() + 1, hash + maxRangeSquared);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY(), inRealWorld.getZ() - 1, hash - maxRangeSquared);
            }
        }
    }
}
Also used : TIntIterator(gnu.trove.iterator.TIntIterator) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos) TIntHashSet(gnu.trove.set.hash.TIntHashSet)

Aggregations

TIntHashSet (gnu.trove.set.hash.TIntHashSet)49 ArrayList (java.util.ArrayList)16 TIntSet (gnu.trove.set.TIntSet)8 TIntArrayList (gnu.trove.list.array.TIntArrayList)7 TIntProcedure (gnu.trove.procedure.TIntProcedure)7 IJ (ij.IJ)6 PointPair (uk.ac.sussex.gdsc.core.match.PointPair)6 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)6 PeakResultPoint (uk.ac.sussex.gdsc.smlm.results.PeakResultPoint)6 PlugIn (ij.plugin.PlugIn)5 TextWindow (ij.text.TextWindow)5 List (java.util.List)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 ImageJUtils (uk.ac.sussex.gdsc.core.ij.ImageJUtils)5 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)5 TextUtils (uk.ac.sussex.gdsc.core.utils.TextUtils)5 TIntIterator (gnu.trove.iterator.TIntIterator)4 TIntObjectHashMap (gnu.trove.map.hash.TIntObjectHashMap)4 Prefs (ij.Prefs)4 Point (java.awt.Point)4