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;
}
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());
}
}
}
}
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;
}
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);
}
}
}
}
Aggregations