use of gnu.trove.set.hash.TIntHashSet in project GDSC-SMLM by aherbert.
the class ResultsMatchCalculator method getTimepoints.
/**
* Merge the time points from each map into a single sorted list of unique time points
*
* @param actualCoordinates
* @param predictedCoordinates
* @return a list of time points
*/
private static int[] getTimepoints(TIntObjectHashMap<ArrayList<Coordinate>> actualCoordinates, TIntObjectHashMap<ArrayList<Coordinate>> predictedCoordinates) {
//int[] set = SimpleArrayUtils.merge(actualCoordinates.keys(), predictedCoordinates.keys(), true);
// Do inline to avoid materialising the keys arrays
final TIntHashSet hashset = new TIntHashSet(Math.max(actualCoordinates.size(), predictedCoordinates.size()));
final TIntProcedure p = new TIntProcedure() {
public boolean execute(int value) {
hashset.add(value);
return true;
}
};
actualCoordinates.forEachKey(p);
predictedCoordinates.forEachKey(p);
int[] set = hashset.toArray();
Arrays.sort(set);
return set;
}
use of gnu.trove.set.hash.TIntHashSet in project GDSC-SMLM by aherbert.
the class BlinkEstimatorTest method findOptimalFittedPoints.
@Test
public void findOptimalFittedPoints() {
int particles = 1000;
double fixedFraction = 1;
for (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 (double n : nBlinks) {
for (int i = 0; i < tOn.length; i++) {
tests++;
TIntHashSet ok = estimateBlinking(n, tOn[i], tOff[i], particles, fixedFraction, timeAtLowerBound, false);
ok.forEach(new TIntProcedure() {
public boolean execute(int value) {
count[value]++;
return true;
}
});
}
}
}
System.out.printf("Time@LowerBound = %b\n", timeAtLowerBound);
for (int nFittedPoints = MIN_FITTED_POINTS; nFittedPoints <= MAX_FITTED_POINTS; nFittedPoints++) {
System.out.printf("%2d = %2d/%2d |", nFittedPoints, count[nFittedPoints], tests);
for (int i = 0; i < count[nFittedPoints]; i++) System.out.printf("-");
System.out.printf("\n");
}
}
}
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.stepsPerSecond * settings.exposureTime) / 1000.0;
List<LocalisationModelSet> newLocalisations = new ArrayList<LocalisationModelSet>((int) (localisations.size() / simulationStepsPerFrame));
//System.out.printf("combineSimulationSteps @ %f\n", simulationStepsPerFrame);
final double gain = (settings.getTotalGain() > 0) ? settings.getTotalGain() : 1;
sortLocalisationsByIdThenTime(localisations);
int[] idList = getIds(localisations);
movingMolecules = new TIntHashSet(idList.length);
int index = 0;
for (int id : idList) {
int fromIndex = findIndexById(localisations, index, id);
if (fromIndex > -1) {
int toIndex = findLastIndexById(localisations, fromIndex, id);
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);
LocalisationModelSet[] sets = new LocalisationModelSet[intLastFrame - intFirstFrame + 1];
// Process each step
for (LocalisationModel l : subset) {
// Get the fractional start and end frames
double startFrame = getStartFrame(l, simulationStepsPerFrame);
double endFrame = getEndFrame(l, simulationStepsPerFrame);
// Round down to get the actual frames that are overlapped
int start = (int) startFrame;
int end = (int) endFrame;
// Check if the span covers a fraction of the end frame, otherwise decrement to ignore 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
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
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 after gain.
// This is used later to filter based on SNR
sets[i].setData(new double[] { 0, 0, 0, 0, sets[i].getIntensity() * gain });
newLocalisations.add(sets[i]);
}
previous = sets[i];
}
}
}
// Sort by time
Collections.sort(newLocalisations);
return newLocalisations;
}
use of gnu.trove.set.hash.TIntHashSet in project GDSC-SMLM by aherbert.
the class TraceManager method getTraces.
/**
* @return The traces that have been found using {@link #traceMolecules(double, int)}
*/
public Trace[] getTraces() {
PeakResult[] peakResults = results.toArray();
// No tracing yet performed or no thresholds
if (totalTraces == localisations.length) {
if (filterActivationFrames) {
ArrayList<Trace> traces = new ArrayList<Trace>();
for (int index = 0; index < totalTraces; index++) {
PeakResult peakResult = peakResults[localisations[index].id];
if (!outsideActivationWindow(peakResult.getFrame()))
traces.add(new Trace(peakResult));
}
return traces.toArray(new Trace[traces.size()]);
} else {
Trace[] traces = new Trace[localisations.length];
for (int index = 0; index < traces.length; index++) traces[index] = new Trace(peakResults[localisations[index].id]);
return traces;
}
}
if (tracker != null)
tracker.progress(0);
// Build the list of traces
Trace[] traces = new Trace[getTotalTraces()];
int n = 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.
TIntHashSet processedTraces = new TIntHashSet(traces.length);
for (int index = 0; index < localisations.length; index++) {
if (tracker != null && index % 256 == 0)
tracker.progress(index, localisations.length);
final int traceId = localisations[index].trace;
if (processedTraces.contains(traceId))
continue;
processedTraces.add(traceId);
if (filterActivationFrames && outsideActivationWindow(localisations[index].t))
continue;
PeakResult peakResult = peakResults[localisations[index].id];
Trace nextTrace = new Trace(peakResult);
nextTrace.setId(traceId);
final int tLimit = maxT[traceId];
// Check if the trace has later frames
if (tLimit > localisations[index].t) {
for (int j = index + 1; j < localisations.length; j++) {
if (localisations[j].t > tLimit) {
// System.out.printf("missed %d\n", j);
break;
}
if (localisations[j].trace == traceId)
nextTrace.add(peakResults[localisations[j].id]);
}
}
//// DEBUG: Check the trace does not contain two localisations from the same time frame.
//// This should be handled by the findAlternativeForerunner code.
//int[] time = new int[nextTrace.size()];
//int count = 0;
//for (PeakResult p : nextTrace.getPoints())
//{
// for (int i = 0; i < count; i++)
// if (time[i] == p.peak)
// {
// System.out.printf("Trace %d contains multiple localisations from the same frame: %d\n", n + 1,
// p.peak);
// break;
// }
// time[count++] = p.peak;
//}
traces[n++] = nextTrace;
}
if (tracker != null)
tracker.progress(1.0);
return traces;
}
use of gnu.trove.set.hash.TIntHashSet in project GDSC-SMLM by aherbert.
the class TraceMolecules method convert.
private int[] convert(double[] intervals) {
TIntHashSet set = new TIntHashSet(intervals.length);
for (double d : intervals) set.add((int) Math.round(d));
// Do not allow zero
set.remove(0);
int[] values = set.toArray();
Arrays.sort(values);
return values;
}
Aggregations