use of gnu.trove.set.hash.TIntHashSet in project Osmand by osmandapp.
the class RouteResultPreparation method findActiveIndex.
protected int findActiveIndex(int[] rawLanes, String[] splitLaneOptions, int lanes, boolean left, List<int[]> lanesInfo, int roads, int addRoads) {
int activeStartIndex = -1;
boolean lookupSlightTurn = addRoads > 0;
TIntHashSet addedTurns = new TIntHashSet();
// if we have information increase number of roads per each turn direction
int diffTurnRoads = roads;
int increaseTurnRoads = 0;
for (int[] li : lanesInfo) {
TIntHashSet set = new TIntHashSet();
if (li != null) {
for (int k = 0; k < li.length; k++) {
TurnType.collectTurnTypes(li[k], set);
}
}
increaseTurnRoads = Math.max(set.size() - 1, 0);
}
for (int i = 0; i < rawLanes.length; i++) {
int ind = left ? i : (rawLanes.length - i - 1);
if (!lookupSlightTurn || TurnType.hasAnySlightTurnLane(rawLanes[ind])) {
String[] laneTurns = splitLaneOptions[ind].split(";");
int cnt = 0;
for (String lTurn : laneTurns) {
boolean added = addedTurns.add(TurnType.convertType(lTurn));
if (added) {
cnt++;
diffTurnRoads--;
}
}
lanes -= cnt;
// lanes--;
// we already found slight turn others are turn in different direction
lookupSlightTurn = false;
}
if (lanes < 0 || diffTurnRoads + increaseTurnRoads < 0) {
activeStartIndex = ind;
break;
} else if (diffTurnRoads < 0 && activeStartIndex < 0) {
activeStartIndex = ind;
}
}
return activeStartIndex;
}
use of gnu.trove.set.hash.TIntHashSet in project BuildCraft by BuildCraft.
the class NbtSquishMapWriter method shouldPackList.
private boolean shouldPackList(NBTTagList list) {
if (packList != null)
return packList;
profiler.startSection("should_pack");
TIntHashSet indexes = new TIntHashSet();
for (int i = 0; i < list.tagCount(); i++) {
indexes.add(map.indexOfTag(list.get(i)));
}
profiler.endSection();
return indexes.size() * 2 < list.tagCount();
}
use of gnu.trove.set.hash.TIntHashSet in project GDSC-SMLM by aherbert.
the class BlinkEstimatorTest method estimateBlinking.
private TIntHashSet estimateBlinking(UniformRandomProvider rg, double blinkingRate, double ton, double toff, int particles, double fixedFraction, boolean timeAtLowerBound, boolean doAssert) {
Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MAXIMUM));
final SpatialIllumination activationIllumination = new UniformIllumination(100);
int totalSteps = 100;
final double eAct = totalSteps * 0.3 * activationIllumination.getAveragePhotons();
final ImageModel imageModel = new ActivationEnergyImageModel(eAct, activationIllumination, ton, 0, toff, 0, blinkingRate, rg);
final double[] max = new double[] { 256, 256, 32 };
final double[] min = new double[3];
final SpatialDistribution distribution = new UniformDistribution(min, max, rg.nextInt());
final List<CompoundMoleculeModel> compounds = new ArrayList<>(1);
final CompoundMoleculeModel c = new CompoundMoleculeModel(1, 0, 0, 0, Arrays.asList(new MoleculeModel(0, 0, 0, 0)));
c.setDiffusionRate(diffusionRate);
c.setDiffusionType(DiffusionType.RANDOM_WALK);
compounds.add(c);
final List<CompoundMoleculeModel> molecules = imageModel.createMolecules(compounds, particles, distribution, false);
// Activate fluorophores
final List<? extends FluorophoreSequenceModel> fluorophores = imageModel.createFluorophores(molecules, totalSteps);
totalSteps = checkTotalSteps(totalSteps, fluorophores);
final List<LocalisationModel> localisations = imageModel.createImage(molecules, fixedFraction, totalSteps, photons, 0.5, false);
// // Remove localisations to simulate missed counts.
// List<LocalisationModel> newLocalisations = new
// ArrayList<LocalisationModel>(localisations.size());
// boolean[] id = new boolean[fluorophores.size() + 1];
// Statistics photonStats = new Statistics();
// for (LocalisationModel l : localisations)
// {
// photonStats.add(l.getIntensity());
// // Remove by intensity threshold and optionally at random.
// if (l.getIntensity() < minPhotons || rand.nextDouble() < pDelete)
// continue;
// newLocalisations.add(l);
// id[l.getId()] = true;
// }
// localisations = newLocalisations;
// logger.info("Photons = %f", photonStats.getMean());
//
// List<FluorophoreSequenceModel> newFluorophores = new
// ArrayList<FluorophoreSequenceModel>(fluorophores.size());
// for (FluorophoreSequenceModel f : fluorophores)
// {
// if (id[f.getId()])
// newFluorophores.add(f);
// }
// fluorophores = newFluorophores;
final MemoryPeakResults results = new MemoryPeakResults();
final CalibrationWriter calibration = new CalibrationWriter();
calibration.setNmPerPixel(pixelPitch);
calibration.setExposureTime(msPerFrame);
calibration.setCountPerPhoton(1);
results.setCalibration(calibration.getCalibration());
results.setPsf(PsfHelper.create(PSFType.ONE_AXIS_GAUSSIAN_2D));
final float b = 0;
float intensity;
final float z = 0;
for (final LocalisationModel l : localisations) {
// Remove by intensity threshold and optionally at random.
if (l.getIntensity() < minPhotons || rg.nextDouble() < probabilityDelete) {
continue;
}
final int frame = l.getTime();
intensity = (float) l.getIntensity();
final float x = (float) l.getX();
final float y = (float) l.getY();
final float[] params = Gaussian2DPeakResultHelper.createParams(b, intensity, x, y, z, psfWidth);
results.add(frame, 0, 0, 0, 0, 0, 0, params, null);
}
// Add random localisations
// Intensity doesn't matter at the moment for tracing
intensity = (float) photons;
for (int i = (int) (localisations.size() * probabilityAdd); i-- > 0; ) {
final int frame = 1 + rg.nextInt(totalSteps);
final float x = (float) (rg.nextDouble() * max[0]);
final float y = (float) (rg.nextDouble() * max[1]);
final float[] params = Gaussian2DPeakResultHelper.createParams(b, intensity, x, y, z, psfWidth);
results.add(frame, 0, 0, 0, 0, 0, 0, params, null);
}
// Get actual simulated stats ...
final Statistics statsNBlinks = new Statistics();
final Statistics statsTOn = new Statistics();
final Statistics statsTOff = new Statistics();
final Statistics statsSampledNBlinks = new Statistics();
final Statistics statsSampledTOn = new Statistics();
final StoredDataStatistics statsSampledTOff = new StoredDataStatistics();
for (final FluorophoreSequenceModel f : fluorophores) {
statsNBlinks.add(f.getNumberOfBlinks());
statsTOn.add(f.getOnTimes());
statsTOff.add(f.getOffTimes());
final int[] on = f.getSampledOnTimes();
statsSampledNBlinks.add(on.length);
statsSampledTOn.add(on);
statsSampledTOff.add(f.getSampledOffTimes());
}
logger.info(FunctionUtils.getSupplier("N = %d (%d), N-blinks = %f, tOn = %f, tOff = %f, Fixed = %f", fluorophores.size(), localisations.size(), blinkingRate, ton, toff, fixedFraction));
logger.info(FunctionUtils.getSupplier("Actual N-blinks = %f (%f), tOn = %f (%f), tOff = %f (%f), 95%% = %f, max = %f", statsNBlinks.getMean(), statsSampledNBlinks.getMean(), statsTOn.getMean(), statsSampledTOn.getMean(), statsTOff.getMean(), statsSampledTOff.getMean(), statsSampledTOff.getStatistics().getPercentile(95), statsSampledTOff.getStatistics().getMax()));
logger.info("-=-=--=-");
final BlinkEstimator be = new BlinkEstimator();
be.setMaxDarkTime((int) (toff * 10));
be.setMsPerFrame(msPerFrame);
be.setRelativeDistance(false);
final double d = ImageModel.getRandomMoveDistance(diffusionRate);
be.setSearchDistance((fixedFraction < 1) ? Math.sqrt(2 * d * d) * 3 : 0);
be.setTimeAtLowerBound(timeAtLowerBound);
// Assertions.assertTrue("Max dark time must exceed the dark time of the data (otherwise no
// plateau)",
// be.maxDarkTime > statsSampledTOff.getStatistics().getMax());
final int nMolecules = fluorophores.size();
if (usePopulationStatistics) {
blinkingRate = statsNBlinks.getMean();
toff = statsTOff.getMean();
} else {
blinkingRate = statsSampledNBlinks.getMean();
toff = statsSampledTOff.getMean();
}
// See if any fitting regime gets a correct answer
final TIntHashSet ok = new TIntHashSet();
for (int numberOfFittedPoints = MIN_FITTED_POINTS; numberOfFittedPoints <= MAX_FITTED_POINTS; numberOfFittedPoints++) {
be.setNumberOfFittedPoints(numberOfFittedPoints);
be.computeBlinkingRate(results, true);
final double moleculesError = DoubleEquality.relativeError(nMolecules, be.getNMolecules());
final double blinksError = DoubleEquality.relativeError(blinkingRate, be.getNBlinks());
final double offError = DoubleEquality.relativeError(toff * msPerFrame, be.getTOff());
logger.info(FunctionUtils.getSupplier("Error %d: N = %f, blinks = %f, tOff = %f : %f", numberOfFittedPoints, moleculesError, blinksError, offError, (moleculesError + blinksError + offError) / 3));
if (moleculesError < relativeError && blinksError < relativeError && offError < relativeError) {
ok.add(numberOfFittedPoints);
logger.info("-=-=--=-");
logger.info(FunctionUtils.getSupplier("*** Correct at %d fitted points ***", numberOfFittedPoints));
if (doAssert) {
break;
}
}
// if (!be.isIncreaseNFittedPoints())
// break;
}
logger.info("-=-=--=-");
if (doAssert) {
Assertions.assertFalse(ok.isEmpty());
}
// relativeError);
return ok;
}
use of gnu.trove.set.hash.TIntHashSet 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);
}
}
use of gnu.trove.set.hash.TIntHashSet in project GDSC-SMLM by aherbert.
the class TraceMolecules method convert.
private static int[] convert(double[] intervals) {
final TIntHashSet set = new TIntHashSet(intervals.length);
for (final double d : intervals) {
set.add((int) Math.round(d));
}
// Do not allow zero
set.remove(0);
final int[] values = set.toArray();
Arrays.sort(values);
return values;
}
Aggregations