-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[ENH] Scatter Plot: Add ellipse/s #6752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9a66ca2
Scatter Plot: Draw ellipse/s
VesnaT 5cfa8d2
Scatter Plot: Refactor reg line and ellipse plotting
VesnaT f7167c5
Scatter Plot: Fix Hotelling's T2
VesnaT e6563c3
Scatter Plot: Rotate ellipse
VesnaT 93f4718
Scatter Plot: Handle small datasets
VesnaT 2029ce5
Scatter Plot: Lint
VesnaT cef04c9
Scatter Plot: Fix ellipse stretch
VesnaT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
| import math | ||
| from typing import List, Callable | ||
| from xml.sax.saxutils import escape | ||
|
|
||
| import numpy as np | ||
| import scipy.stats as ss | ||
| from scipy.stats import linregress | ||
| from sklearn.neighbors import NearestNeighbors | ||
| from sklearn.metrics import r2_score | ||
|
|
@@ -104,6 +107,8 @@ def update_lines(**settings): | |
| self.reg_line_settings.update(**settings) | ||
| Updater.update_inf_lines(self.reg_line_items, | ||
| **self.reg_line_settings) | ||
| Updater.update_lines(self.ellipse_items, | ||
| **self.reg_line_settings) | ||
| self.master.update_reg_line_label_colors() | ||
|
|
||
| def update_line_label(**settings): | ||
|
|
@@ -129,20 +134,27 @@ def reg_line_label_items(self): | |
| return [line.label for line in self.master.reg_line_items | ||
| if hasattr(line, "label")] | ||
|
|
||
| @property | ||
| def ellipse_items(self): | ||
| return self.master.ellipse_items | ||
|
|
||
|
|
||
| class OWScatterPlotGraph(OWScatterPlotBase): | ||
| show_reg_line = Setting(False) | ||
| orthonormal_regression = Setting(False) | ||
| show_ellipse = Setting(False) | ||
| jitter_continuous = Setting(False) | ||
|
|
||
| def __init__(self, scatter_widget, parent): | ||
| super().__init__(scatter_widget, parent) | ||
| self.parameter_setter = ParameterSetter(self) | ||
| self.reg_line_items = [] | ||
| self.ellipse_items: List[pg.PlotCurveItem] = [] | ||
|
|
||
| def clear(self): | ||
| super().clear() | ||
| self.reg_line_items.clear() | ||
| self.ellipse_items.clear() | ||
|
|
||
| def update_coordinates(self): | ||
| super().update_coordinates() | ||
|
|
@@ -153,6 +165,7 @@ def update_coordinates(self): | |
| def update_colors(self): | ||
| super().update_colors() | ||
| self.update_regression_line() | ||
| self.update_ellipse() | ||
|
|
||
| def jitter_coordinates(self, x, y): | ||
| def get_span(attr): | ||
|
|
@@ -255,17 +268,28 @@ def update_density(self): | |
| self.update_reg_line_label_colors() | ||
|
|
||
| def update_regression_line(self): | ||
| for line in self.reg_line_items: | ||
| self.plot_widget.removeItem(line) | ||
| self.reg_line_items.clear() | ||
| if not (self.show_reg_line | ||
| and self.master.can_draw_regresssion_line()): | ||
| self._update_curve(self.reg_line_items, | ||
| self.show_reg_line, | ||
| self._add_line) | ||
| self.update_reg_line_label_colors() | ||
|
|
||
| def update_ellipse(self): | ||
| self._update_curve(self.ellipse_items, | ||
| self.show_ellipse, | ||
| self._add_ellipse) | ||
|
|
||
| def _update_curve(self, items: List, show: bool, add: Callable): | ||
| for item in items: | ||
| self.plot_widget.removeItem(item) | ||
| items.clear() | ||
| if not (show and self.master.can_draw_regression_line()): | ||
| return | ||
| x, y = self.master.get_coordinates_data() | ||
| if x is None: | ||
| if x is None or len(x) < 2: | ||
| return | ||
| self._add_line(x, y, QColor("#505050")) | ||
| if self.master.is_continuous_color() or self.palette is None: | ||
| add(x, y, QColor("#505050")) | ||
| if self.master.is_continuous_color() or self.palette is None \ | ||
| or len(self.palette) == 0: | ||
| return | ||
| c_data = self.master.get_color_data() | ||
| if c_data is None: | ||
|
|
@@ -274,8 +298,41 @@ def update_regression_line(self): | |
| for val in range(c_data.max() + 1): | ||
| mask = c_data == val | ||
| if mask.sum() > 1: | ||
| self._add_line(x[mask], y[mask], self.palette[val].darker(135)) | ||
| self.update_reg_line_label_colors() | ||
| add(x[mask], y[mask], self.palette[val].darker(135)) | ||
|
|
||
| def _add_ellipse(self, x: np.ndarray, y: np.ndarray, color: QColor) -> np.ndarray: | ||
| # https://stats.stackexchange.com/questions/577628/trying-to-understand-how-to-calculate-a-hotellings-t2-confidence-ellipse | ||
| # https://stackoverflow.com/questions/66179256/how-to-check-if-a-point-is-in-an-ellipse-in-python | ||
| points = np.vstack([x, y]).T | ||
| mu = np.mean(points, axis=0) | ||
| cov = np.cov(*(points - mu).T) | ||
| _, vects = np.linalg.eig(cov) | ||
| angle = math.atan2(vects[1, 0], vects[0, 0]) | ||
| matrix = np.array([[np.cos(angle), -np.sin(angle)], | ||
| [np.sin(angle), np.cos(angle)]]) | ||
|
|
||
| n = len(x) | ||
| f = ss.f.ppf(0.95, 2, n - 2) | ||
| f = f * 2 * (n - 1) / (n - 2) | ||
| m = [np.pi * i / 100 for i in range(201)] | ||
| cx = np.cos(m) * np.sqrt(cov[0, 0] * f) | ||
| cy = np.sin(m) * np.sqrt(cov[1, 1] * f) | ||
|
||
|
|
||
| pts = np.vstack([cx, cy]) | ||
| pts = matrix.dot(pts) | ||
| cx = pts[0] + mu[0] | ||
| cy = pts[1] + mu[1] | ||
|
|
||
| width = self.parameter_setter.reg_line_settings[Updater.WIDTH_LABEL] | ||
| alpha = self.parameter_setter.reg_line_settings[Updater.ALPHA_LABEL] | ||
| style = self.parameter_setter.reg_line_settings[Updater.STYLE_LABEL] | ||
| style = Updater.LINE_STYLES[style] | ||
| color.setAlpha(alpha) | ||
|
|
||
| pen = pg.mkPen(color=color, width=width, style=style) | ||
| ellipse = pg.PlotCurveItem(cx, cy, pen=pen) | ||
| self.plot_widget.addItem(ellipse) | ||
| self.ellipse_items.append(ellipse) | ||
|
|
||
|
|
||
| class OWScatterPlot(OWDataProjectionWidget, VizRankMixin(ScatterPlotVizRank)): | ||
|
|
@@ -353,6 +410,11 @@ def _add_controls(self): | |
| "If checked, fit line to group (minimize distance from points);\n" | ||
| "otherwise fit y as a function of x (minimize vertical distances)", | ||
| disabledBy=self.cb_reg_line) | ||
| gui.checkBox( | ||
| self._plot_box, self, | ||
| value="graph.show_ellipse", | ||
| label="Show ellipse", | ||
| callback=self.graph.update_ellipse) | ||
|
|
||
| def _add_controls_axis(self): | ||
| common_options = dict( | ||
|
|
@@ -492,7 +554,7 @@ def _point_tooltip(self, point_id, skip_attrs=()): | |
| text = "<b>{}</b><br/><br/>{}".format(text, others) | ||
| return text | ||
|
|
||
| def can_draw_regresssion_line(self): | ||
| def can_draw_regression_line(self): | ||
| return self.data is not None and \ | ||
| self.data.domain is not None and \ | ||
| self.attr_x is not None and self.attr_y is not None and \ | ||
|
|
@@ -552,7 +614,9 @@ def handleNewSignals(self): | |
| if self._domain_invalidated: | ||
| self.graph.update_axes() | ||
| self._domain_invalidated = False | ||
| self.cb_reg_line.setEnabled(self.can_draw_regresssion_line()) | ||
| can_plot = self.can_draw_regression_line() | ||
| self.cb_reg_line.setEnabled(can_plot) | ||
| self.graph.controls.show_ellipse.setEnabled(can_plot) | ||
|
|
||
| @Inputs.features | ||
| def set_shown_attributes(self, attributes): | ||
|
|
@@ -578,7 +642,9 @@ def set_attr_from_combo(self): | |
| self.vizrankAutoSelect.emit([self.attr_x, self.attr_y]) | ||
|
|
||
| def attr_changed(self): | ||
| self.cb_reg_line.setEnabled(self.can_draw_regresssion_line()) | ||
| can_plot = self.can_draw_regression_line() | ||
| self.cb_reg_line.setEnabled(can_plot) | ||
| self.graph.controls.show_ellipse.setEnabled(can_plot) | ||
| self.setup_plot() | ||
| self.commit.deferred() | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe replace with the following reference for HotellingEllipse, from where we took the equation to scale for 95%
https://github.com/ChristianGoueguel/HotellingEllipse/blob/master/R/ellipseCoord.R