use of com.forstudy.efjava.ch03.item10.Point in project Lectures by FER-OOP.
the class Main method main.
public static void main(String[] args) {
List<Point> list = new ArrayList<>();
list.add(new Point(-5, 12));
list.add(new Point(3, -4));
list.add(new Point(12, 9));
list.add(new Point(3, 4));
list.add(new Point(4, 3));
list.add(new Point(-9, 12));
list.add(new Point(-5, -12));
DistanceFromOrigin dist = new DistanceFromOrigin();
List<String> newlist = list.stream().filter(new QuadrantPredicate(true, true, false, true)).sorted(new PointComparator().reversed()).map(p -> String.format("dist(%s) = %.2f", p, dist.apply(p))).collect(Collectors.toList());
System.out.println(newlist);
}
use of com.forstudy.efjava.ch03.item10.Point in project Lectures by FER-OOP.
the class Main method main.
public static void main(String[] args) {
List<Point> list = new ArrayList<>();
list.add(new Point(-5, 12));
list.add(new Point(3, -4));
list.add(new Point(12, 9));
list.add(new Point(3, 4));
list.add(new Point(4, 3));
list.add(new Point(-9, 12));
list.add(new Point(-5, -12));
DistanceFromOrigin dist = new DistanceFromOrigin();
OptionalDouble avg = list.stream().filter(new QuadrantPredicate(true, false, false, false)).mapToDouble(p -> dist.apply(p)).average();
avg.ifPresentOrElse(val -> System.out.println("Avg = " + val), () -> System.out.println("No data for selected quadrants"));
}
use of com.forstudy.efjava.ch03.item10.Point in project Lectures by FER-OOP.
the class Main method main.
public static void main(String[] args) {
List<Point> list = new ArrayList<>();
addPoints(list);
Map<Integer, Set<Point>> map = new TreeMap<>();
Comparator<Point> comp = new PointComparator();
comp = comp.thenComparing(Comparator.naturalOrder());
map.put(1, new TreeSet<>(comp));
map.put(2, new TreeSet<>(comp));
map.put(3, new TreeSet<>(comp));
map.put(4, new TreeSet<>(comp));
Function<Point, Integer> quadrantFunction = QuadrantCalculator::quadrant;
for (var point : list) {
int quadrant = quadrantFunction.apply(point);
if (quadrant != 0)
map.get(quadrant).add(point);
}
System.out.println(map);
}
use of com.forstudy.efjava.ch03.item10.Point in project mastodon-tracking by mastodon-sc.
the class SemiAutomaticTracker method compute.
@Override
public void compute(final Collection<Spot> input, final Map<String, Object> settings, final Model model) {
if (null == log)
log = logService;
if (null == input || input.isEmpty()) {
log.info("Spot collection to track is empty. Stopping.");
return;
}
final List<SourceAndConverter<?>> sources = data.getSources();
final ModelGraph graph = model.getGraph();
final SpatioTemporalIndex<Spot> spatioTemporalIndex = model.getSpatioTemporalIndex();
if (null != selectionModel)
selectionModel.clearSelection();
/*
* Quality and link-cost features. If they do not exist, create them and
* register them.
*/
final DetectionQualityFeature qualityFeature = DetectionQualityFeature.getOrRegister(model.getFeatureModel(), graph.vertices().getRefPool());
final LinkCostFeature linkCostFeature = LinkCostFeature.getOrRegister(model.getFeatureModel(), graph.edges().getRefPool());
/*
* Check settings map.
*/
ok = false;
final StringBuilder errorHandler = new StringBuilder();
if (!checkSettingsValidity(settings, errorHandler)) {
errorMessage = errorHandler.toString();
return;
}
/*
* Extract parameters from map.
*/
final int setup = (int) settings.get(KEY_SETUP_ID);
final double qualityFactor = (double) settings.get(KEY_QUALITY_FACTOR);
final double distanceFactor = (double) settings.get(KEY_DISTANCE_FACTOR);
final boolean forward = (boolean) settings.get(KEY_FORWARD_IN_TIME);
final int nTimepoints = (int) settings.get(KEY_N_TIMEPOINTS);
final boolean allowLinkingToExisting = (boolean) settings.get(KEY_ALLOW_LINKING_TO_EXISTING);
final boolean allowLinkingIfIncoming = (boolean) settings.get(KEY_ALLOW_LINKING_IF_HAS_INCOMING);
final boolean allowLinkingIfOutgoing = (boolean) settings.get(KEY_ALLOW_LINKING_IF_HAS_OUTGOING);
final boolean continueIfLinkExists = (boolean) settings.get(KEY_CONTINUE_IF_LINK_EXISTS);
final double neighborhoodFactor = Math.max(NEIGHBORHOOD_FACTOR, distanceFactor + 1.);
final boolean detectSpots = (boolean) settings.get(KEY_DETECT_SPOT);
/*
* Units.
*/
final String units = sources.get(setup).getSpimSource().getVoxelDimensions().unit();
/*
* First and last time-points.
*/
final int minTimepoint = 0;
final int maxTimepoint = data.getNumTimepoints() - 1;
/*
* Loop over each spot input.
*/
final double[][] cov = new double[3][3];
INPUT: for (final Spot first : input) {
graph.notifyGraphChanged();
/*
* Initialize motion-model for this spot.
*/
final MotionModel motionModel = initializeMotionModel(first, graph);
/*
* Loop over time.
*/
final int firstTimepoint = first.getTimepoint();
int tp = firstTimepoint;
final Spot source = model.getGraph().vertexRef();
source.refTo(first);
log.info("Semi-automatic tracking from spot " + first.getLabel() + ", going " + (forward ? "forward" : "backward") + " in time.");
TIME: while (Math.abs(tp - firstTimepoint) < nTimepoints && (forward ? tp < maxTimepoint : tp > minTimepoint)) {
// Are we canceled?
if (isCanceled()) {
log.warn("Canceled: " + getCancelReason());
return;
}
tp = (forward ? tp + 1 : tp - 1);
// Check if there is some data at this timepoint.
if (!DetectionUtil.isPresent(sources, setup, tp))
continue TIME;
// Best radius is smallest radius of ellipse.
source.getCovariance(cov);
eig.decomposeSymmetric(cov);
final double[] eigVals = eig.getRealEigenvalues();
double minEig = Double.POSITIVE_INFINITY;
for (int k = 0; k < eigVals.length; k++) minEig = Math.min(minEig, eigVals[k]);
final double radius = Math.sqrt(minEig);
// Does the source have a quality value?
final double threshold;
if (qualityFeature.isSet(source))
threshold = qualityFeature.value(source) * qualityFactor;
else
threshold = 0.;
/*
* Predict around what position to look for a candidate.
*/
final RealLocalizable predict = motionModel.predict();
/*
* Do we have an existing spot around this location, and do we
* have the right to link to it?
*/
spatioTemporalIndex.readLock().lock();
Spot target = null;
final double distance;
try {
final SpatialIndex<Spot> spatialIndex = spatioTemporalIndex.getSpatialIndex(tp);
final NearestNeighborSearch<Spot> nn = spatialIndex.getNearestNeighborSearch();
nn.search(predict);
target = nn.getSampler().get();
distance = nn.getDistance();
} finally {
spatioTemporalIndex.readLock().unlock();
}
final double[] pos = new double[3];
predict.localize(pos);
if (target != null && (distance < distanceFactor * radius)) {
// Select it.
if (null != selectionModel)
selectionModel.setSelected(target, true);
log.info(String.format(" - Found an exising spot at t=%d for spot %s close to candidate: %s.", tp, source.getLabel(), target.getLabel()));
if (!allowLinkingToExisting) {
log.info(" - Stopping semi-automatic tracking for spot " + first.getLabel() + ".");
continue INPUT;
}
// Are they connected?
final Link eref = graph.edgeRef();
final boolean connected = forward ? graph.getEdge(source, target, eref) != null : graph.getEdge(target, source, eref) != null;
if (!connected) {
// Should we link them?
if (!allowLinkingIfIncoming && !target.incomingEdges().isEmpty()) {
log.info(" - Existing spot has incoming links. Stopping semi-automatic tracking for spot " + first.getLabel() + ".");
continue INPUT;
}
if (!allowLinkingIfOutgoing && !target.outgoingEdges().isEmpty()) {
log.info(" - Existing spot has outgoing links. Stopping semi-automatic tracking for spot " + first.getLabel() + ".");
continue INPUT;
}
// Yes.
final Link edge;
graph.getLock().writeLock().lock();
try {
if (forward)
edge = graph.addEdge(source, target, eref).init();
else
edge = graph.addEdge(target, source, eref).init();
} finally {
graph.getLock().writeLock().unlock();
}
final double cost = motionModel.costTo(target);
log.info(String.format(" - Linking spot %s at t=%d to spot %s at t=%d with linking cost %.1f.", source.getLabel(), source.getTimepoint(), target.getLabel(), target.getTimepoint(), cost));
linkCostFeature.set(edge, cost);
if (null != navigationHandler)
navigationHandler.notifyNavigateToVertex(target);
if (null != focusModel)
focusModel.focusVertex(target);
} else {
// They are connected.
log.info(String.format(" - Spots %s at t=%d and %s at t=%d are already linked.", source.getLabel(), source.getTimepoint(), target.getLabel(), target.getTimepoint()));
if (!continueIfLinkExists) {
log.info(" - Stopping semi-automatic tracking for spot " + first.getLabel() + ".");
continue INPUT;
}
}
// Update tracker with the new target.
motionModel.update(target);
// Deselect source spot.
if (null != selectionModel)
selectionModel.setSelected(source, false);
// Select, focus and navigate to new spot.
if (null != navigationHandler)
navigationHandler.notifyNavigateToVertex(target);
if (null != selectionModel)
selectionModel.setSelected(target, true);
if (null != focusModel)
focusModel.focusVertex(target);
// Target becomes source and we loop over next time-point.
graph.releaseRef(eref);
source.refTo(target);
continue TIME;
} else if (detectSpots) {
/*
* There is no candidate around the predicted position, or
* we do not have the right to link to it. We therefore have
* to search for candidates from the image.
*/
/*
* Build ROI to process.
*/
final AffineTransform3D transform = DetectionUtil.getTransform(sources, tp, setup, 0);
final Point center = new Point(3);
transform.applyInverse(new Round<>(center), predict);
final long x = center.getLongPosition(0);
final long y = center.getLongPosition(1);
final long rx = (long) Math.ceil(neighborhoodFactor * radius);
final long ry = (long) Math.ceil(neighborhoodFactor * radius);
final FinalInterval roi;
if (DetectionUtil.numDimensions(sources, setup, tp) == 3) {
final long z = center.getLongPosition(2);
final long rz = (long) Math.ceil(neighborhoodFactor * radius);
final long[] min = new long[] { x - rx, y - ry, z - rz };
final long[] max = new long[] { x + rx, y + ry, z + rz };
roi = new FinalInterval(min, max);
} else {
final long[] min = new long[] { x - rx, y - ry };
final long[] max = new long[] { x + rx, y + ry };
roi = new FinalInterval(min, max);
}
/*
* User built-in detector.
*/
final List<Detection> detections = new ArrayList<>();
final DetectionCreatorFactory detectionCreator = createDetectionCreatorFactoryFor(detections);
// Configure detector.
final Map<String, Object> detectorSettings = DetectionUtil.getDefaultDetectorSettingsMap();
detectorSettings.put(KEY_RADIUS, Double.valueOf(radius));
detectorSettings.put(KEY_THRESHOLD, Double.valueOf(threshold));
detectorSettings.put(KEY_SETUP_ID, Integer.valueOf(setup));
detectorSettings.put(KEY_MIN_TIMEPOINT, Integer.valueOf(tp));
detectorSettings.put(KEY_MAX_TIMEPOINT, Integer.valueOf(tp));
detectorSettings.put(KEY_ROI, roi);
final DetectorOp detector = (DetectorOp) Inplaces.binary1(ops(), DoGDetectorOp.class, detectionCreator, sources, detectorSettings);
detector.mutate1(detectionCreator, sources);
if (detections.isEmpty()) {
log.info(String.format(" - No target spot found at t=%d for spot %s above desired quality threshold.", tp, source.getLabel()));
continue INPUT;
}
/*
* Find a suitable candidate with largest quality.
*/
// Sort peaks by quality.
detections.sort(detectionComparator);
boolean found = false;
Detection candidate = null;
double sqDist = 0.;
for (final Detection p : detections) {
// Compute square distance.
sqDist = 0.;
for (int d = 0; d < source.numDimensions(); d++) {
final double dx = p.getDoublePosition(d) - source.getDoublePosition(d);
sqDist += dx * dx;
}
if (sqDist < distanceFactor * distanceFactor * radius * radius) {
found = true;
candidate = p;
break;
}
}
if (!found) {
log.info(String.format(" - Suitable spot found at t=%d, but outside the tolerance radius for spot %s (at a distance of %.1f %s).", tp, source.getLabel(), Math.sqrt(sqDist), units));
log.info(" - Stopping semi-automatic tracking for spot " + first.getLabel() + ".");
continue INPUT;
}
candidate.localize(pos);
/*
* Let's keep this candidate.
*/
graph.getLock().writeLock().lock();
final Link eref = graph.edgeRef();
final Spot vref = graph.vertexRef();
final Link edge;
try {
target = graph.addVertex(vref).init(tp, pos, radius);
if (forward)
edge = graph.addEdge(source, target, eref).init();
else
edge = graph.addEdge(target, source, eref).init();
} finally {
graph.getLock().writeLock().unlock();
}
final double cost = motionModel.costTo(target);
final double quality = candidate.quality;
log.info(String.format(" - Linking spot %s at t=%d to spot %s at t=%d with linking cost %.1f.", source.getLabel(), source.getTimepoint(), target.getLabel(), target.getTimepoint(), cost));
linkCostFeature.set(edge, cost);
qualityFeature.set(target, quality);
// Update tracker with the new target.
motionModel.update(target);
// Deselect source spot.
if (null != selectionModel)
selectionModel.setSelected(source, false);
// Select, focus and navigate to new spot.
if (null != navigationHandler)
navigationHandler.notifyNavigateToVertex(target);
if (null != selectionModel)
selectionModel.setSelected(target, true);
if (null != focusModel)
focusModel.focusVertex(target);
source.refTo(target);
graph.releaseRef(eref);
graph.releaseRef(vref);
} else {
/*
* Could not find an existing spot and cannot create new
* one. We have to stop.
*/
log.info(" - No spot to link to. Stopping semi-automatic tracking for spot " + first.getLabel() + ".");
continue INPUT;
}
}
log.info(" - Finished semi-automatic tracking for spot " + first.getLabel() + ".");
}
/*
* Return.
*/
graph.notifyGraphChanged();
ok = true;
}
use of com.forstudy.efjava.ch03.item10.Point in project mastodon-tracking by mastodon-sc.
the class LoGDetectorOp method mutate1.
@Override
public void mutate1(final DetectionCreatorFactory detectionCreatorFactory, final List<SourceAndConverter<?>> sources) {
ok = false;
final long start = System.currentTimeMillis();
final StringBuilder str = new StringBuilder();
if (!DetectionUtil.checkSettingsValidity(settings, str)) {
processingTime = System.currentTimeMillis() - start;
statusService.clearStatus();
errorMessage = str.toString();
return;
}
final int minTimepoint = (int) settings.get(KEY_MIN_TIMEPOINT);
final int maxTimepoint = (int) settings.get(KEY_MAX_TIMEPOINT);
final int setup = (int) settings.get(KEY_SETUP_ID);
// um
final double radius = (double) settings.get(KEY_RADIUS);
final double threshold = (double) settings.get(KEY_THRESHOLD);
final Interval roi = (Interval) settings.get(KEY_ROI);
statusService.showStatus("LoG detection");
for (int tp = minTimepoint; tp <= maxTimepoint; tp++) {
statusService.showProgress(tp - minTimepoint + 1, maxTimepoint - minTimepoint + 1);
// Did we get canceled?
if (isCanceled())
break;
// Check if there is some data at this timepoint.
if (!DetectionUtil.isPresent(sources, setup, tp))
continue;
/*
* Determine optimal level for detection.
*/
final int level = DetectionUtil.determineOptimalResolutionLevel(sources, radius, MIN_SPOT_PIXEL_SIZE / 2., tp, setup);
/*
* Load and extends image data.
*/
@SuppressWarnings("rawtypes") final RandomAccessibleInterval img = DetectionUtil.getImage(sources, tp, setup, level);
if (!DetectionUtil.isReallyPresent(img))
continue;
@SuppressWarnings("unchecked") final RandomAccessibleInterval<?> zeroMin = Views.dropSingletonDimensions(Views.zeroMin(img));
/*
* Transform ROI in higher level.
*/
final Interval interval;
if (null == roi) {
interval = zeroMin;
} else {
final double[] minSource = new double[3];
final double[] maxSource = new double[3];
roi.realMin(minSource);
roi.realMax(maxSource);
final double[] minTarget = new double[3];
final double[] maxTarget = new double[3];
final AffineTransform3D mipmapTransform = DetectionUtil.getMipmapTransform(sources, tp, setup, level);
mipmapTransform.applyInverse(minTarget, minSource);
mipmapTransform.applyInverse(maxTarget, maxSource);
final long[] tmin = new long[zeroMin.numDimensions()];
final long[] tmax = new long[zeroMin.numDimensions()];
for (int d = 0; d < zeroMin.numDimensions(); d++) {
tmin[d] = (long) Math.ceil(minTarget[d]);
tmax[d] = (long) Math.floor(maxTarget[d]);
}
final FinalInterval transformedRoi = new FinalInterval(tmin, tmax);
interval = Intervals.intersect(transformedRoi, zeroMin);
}
/*
* Filter image.
*/
final double[] pixelSize = DetectionUtil.getPixelSize(sources, tp, setup, level);
final RandomAccessibleInterval<FloatType> kernel = createLoGKernel(radius, zeroMin.numDimensions(), pixelSize);
@SuppressWarnings("rawtypes") final IntervalView source = Views.interval(zeroMin, interval);
@SuppressWarnings("unchecked") final RandomAccessibleInterval<FloatType> output = ops().filter().convolve(source, kernel);
/*
* LoG normalization factor, so that the filtered peak have the
* maximal value for spots that have the size this kernel is tuned
* to. With this value, the peak value will be of the same order of
* magnitude than the raw spot (if it has the right size). This
* value also ensures that if the image has its calibration changed,
* one will retrieve the same peak value than before scaling.
* However, I (JYT) could not derive the exact formula if the image
* is scaled differently across X, Y and Z.
*/
final double sigma = radius / Math.sqrt(img.numDimensions());
final double sigmaPixels = sigma / pixelSize[0];
final FloatType C = new FloatType((float) (1. / Math.PI / sigmaPixels / sigmaPixels));
Views.iterable(output).forEach((e) -> e.div(C));
/*
* Detect local maxima.
*/
final AffineTransform3D transform = DetectionUtil.getTransform(sources, tp, setup, level);
final DetectionCreator detectionCreator = detectionCreatorFactory.create(tp);
final List<Point> peaks = DetectionUtil.findLocalMaxima(output, threshold, threadService.getExecutorService());
if (doSubpixelLocalization) {
final int maxNumMoves = 10;
final boolean allowMaximaTolerance = true;
final boolean returnInvalidPeaks = true;
final boolean[] allowedToMoveInDim = new boolean[img.numDimensions()];
Arrays.fill(allowedToMoveInDim, true);
final float maximaTolerance = 0.01f;
final List<RefinedPeak<Point>> refined = SubpixelLocalization.refinePeaks(peaks, output, output, returnInvalidPeaks, maxNumMoves, allowMaximaTolerance, maximaTolerance, allowedToMoveInDim);
final RandomAccess<FloatType> ra = output.randomAccess();
final double[] pos = new double[3];
final RealPoint point = RealPoint.wrap(pos);
final RealPoint p3d = new RealPoint(3);
detectionCreator.preAddition();
try {
for (final RefinedPeak<Point> refinedPeak : refined) {
ra.setPosition(refinedPeak.getOriginalPeak());
final double q = ra.get().getRealDouble();
for (int d = 0; d < refinedPeak.numDimensions(); d++) p3d.setPosition(refinedPeak.getDoublePosition(d), d);
transform.apply(p3d, point);
detectionCreator.createDetection(pos, radius, q);
}
} finally {
detectionCreator.postAddition();
}
} else {
final RandomAccess<FloatType> ra = output.randomAccess();
final double[] pos = new double[3];
final RealPoint point = RealPoint.wrap(pos);
detectionCreator.preAddition();
try {
for (final Point peak : peaks) {
ra.setPosition(peak);
final double q = ra.get().getRealDouble();
transform.apply(peak, point);
detectionCreator.createDetection(pos, radius, q);
}
} finally {
detectionCreator.postAddition();
}
}
}
final long end = System.currentTimeMillis();
processingTime = end - start;
statusService.clearStatus();
ok = true;
}
Aggregations