From 187d68e613d3ff415492822d4d4e613da40b618e Mon Sep 17 00:00:00 2001 From: Lorenz Gerber Date: Fri, 15 Nov 2024 11:45:11 +0800 Subject: [PATCH] PCA, improve dbl click variable selection Signed-off-by: Lorenz Gerber --- .../pca/ui/swt/ExtendedLoadingsPlot.java | 30 ++++++++++++------- .../supplier/pca/model/FeatureDelta.java | 13 ++++++-- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/chemclipse/plugins/org.eclipse.chemclipse.xxd.process.supplier.pca.ui/src/org/eclipse/chemclipse/xxd/process/supplier/pca/ui/swt/ExtendedLoadingsPlot.java b/chemclipse/plugins/org.eclipse.chemclipse.xxd.process.supplier.pca.ui/src/org/eclipse/chemclipse/xxd/process/supplier/pca/ui/swt/ExtendedLoadingsPlot.java index 31a3a98e84..84a265ef02 100644 --- a/chemclipse/plugins/org.eclipse.chemclipse.xxd.process.supplier.pca.ui/src/org/eclipse/chemclipse/xxd/process/supplier/pca/ui/swt/ExtendedLoadingsPlot.java +++ b/chemclipse/plugins/org.eclipse.chemclipse.xxd.process.supplier.pca.ui/src/org/eclipse/chemclipse/xxd/process/supplier/pca/ui/swt/ExtendedLoadingsPlot.java @@ -321,17 +321,15 @@ public void handleEvent(BaseChart baseChart, Event event) { * Determine the x|y coordinates. */ Rectangle rectangle = baseChart.getPlotArea().getBounds(); - int x = event.x; - int y = event.y; - int width = rectangle.width; - int height = rectangle.height; + double x = event.x; + double y = event.y; + double width = rectangle.width; + double height = rectangle.height; /* * Calculate the selected point. */ Range rangeX = baseChart.getAxisSet().getXAxis(BaseChart.ID_PRIMARY_X_AXIS).getRange(); Range rangeY = baseChart.getAxisSet().getYAxis(BaseChart.ID_PRIMARY_Y_AXIS).getRange(); - double pX = rangeX.lower + (rangeX.upper - rangeX.lower) * ((1.0d / width) * x); - double pY = rangeY.lower + (rangeY.upper - rangeY.lower) * ((1.0d / height) * y); /* * Map the result deltas. */ @@ -346,15 +344,27 @@ public void handleEvent(BaseChart baseChart, Event event) { for(int i = 0; i < resultsPCA.getExtractedVariables().size(); i++) { double[] variableLoading = getVariableLoading(resultsPCA, i); IPoint pointResult = getPoint(variableLoading, pcX, pcY, i); - double deltaX = Math.abs(pointResult.getX() - pX); - double deltaY = Math.abs(pointResult.getY() + pY); - featureDeltas.add(new FeatureDelta(evaluationPCA.getFeatureDataMatrix().getFeatures().get(i), deltaX, deltaY)); + if(pointResult.getX() > rangeX.lower && pointResult.getX() < rangeX.upper && pointResult.getY() > rangeY.lower && pointResult.getY() < rangeY.upper) { + double deltaX = 0; + double deltaY = 0; + if(rangeX.upper < 0 || rangeX.lower > 0) { + deltaX = Math.abs(1.00 / Math.abs((Math.abs(rangeX.upper) - Math.abs(rangeX.lower))) * (pointResult.getX() - rangeX.lower) * width - x); + } else { + deltaX = Math.abs(1.00 / (rangeX.upper - rangeX.lower) * (pointResult.getX() - rangeX.lower) * width - x); + } + if(rangeY.upper < 0 || rangeY.lower > 0) { + deltaY = Math.abs(1.00 / Math.abs((Math.abs(rangeY.upper) - Math.abs(rangeY.lower))) * (pointResult.getY() - rangeY.lower) * height - (height - y)); + } else { + deltaY = Math.abs(1.00 / (rangeY.upper - rangeY.lower) * (pointResult.getY() - rangeY.lower) * height - (height - y)); + } + featureDeltas.add(new FeatureDelta(evaluationPCA.getFeatureDataMatrix().getFeatures().get(i), deltaX, deltaY)); + } } /* * Get the closest result. */ if(!featureDeltas.isEmpty()) { - Collections.sort(featureDeltas, Comparator.comparing(FeatureDelta::getDeltaX).thenComparing(FeatureDelta::getDeltaY)); + Collections.sort(featureDeltas, Comparator.comparing(FeatureDelta::getDistance)); FeatureDelta featureDelta = featureDeltas.get(0); List featureList = new ArrayList<>(); featureList.add(featureDelta.getFeature()); diff --git a/chemclipse/plugins/org.eclipse.chemclipse.xxd.process.supplier.pca/src/org/eclipse/chemclipse/xxd/process/supplier/pca/model/FeatureDelta.java b/chemclipse/plugins/org.eclipse.chemclipse.xxd.process.supplier.pca/src/org/eclipse/chemclipse/xxd/process/supplier/pca/model/FeatureDelta.java index 518bfed80f..6b7f045859 100644 --- a/chemclipse/plugins/org.eclipse.chemclipse.xxd.process.supplier.pca/src/org/eclipse/chemclipse/xxd/process/supplier/pca/model/FeatureDelta.java +++ b/chemclipse/plugins/org.eclipse.chemclipse.xxd.process.supplier.pca/src/org/eclipse/chemclipse/xxd/process/supplier/pca/model/FeatureDelta.java @@ -18,12 +18,14 @@ public class FeatureDelta { private Feature feature = null; private double deltaX = 0; private double deltaY = 0; + private double distance = 0; public FeatureDelta(Feature feature, double deltaX, double deltaY) { this.feature = feature; - this.deltaX = deltaX; - this.deltaY = deltaY; + this.deltaX = Math.abs(deltaX); + this.deltaY = Math.abs(deltaY); + this.distance = Math.sqrt(Math.pow(this.deltaX, 2) + Math.pow(this.deltaY, 2)); } public Feature getFeature() { @@ -41,6 +43,11 @@ public double getDeltaY() { return deltaY; } + public double getDistance() { + + return distance; + } + @Override public int hashCode() { @@ -63,6 +70,6 @@ public boolean equals(Object obj) { @Override public String toString() { - return "FeatureDelta [featurePCA=" + feature + ", deltaX=" + deltaX + ", deltaY=" + deltaY + "]"; + return "FeatureDelta [featurePCA=" + feature + ", deltaX=" + deltaX + ", deltaY=" + deltaY + ", distance=" + distance + "]"; } }