Spaces:
Running
Running
Commit ·
adc85f9
1
Parent(s): 7f05b17
Add regression model plot
Browse files
src/ai_dashboard/plot_plugin/regression_line.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Regression Plot plugin for AI-Dashboard"""
|
| 2 |
+
|
| 3 |
+
from typing import Any, List
|
| 4 |
+
import logging
|
| 5 |
+
|
| 6 |
+
import plotly.express as px
|
| 7 |
+
from dash import html, dcc
|
| 8 |
+
from dash.dependencies import Input, Output
|
| 9 |
+
|
| 10 |
+
from ..base import BasePlotPlugin
|
| 11 |
+
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class RegressionPlotPlugin(BasePlotPlugin):
|
| 16 |
+
"""Scatter + regression line plot plugin."""
|
| 17 |
+
|
| 18 |
+
name = "Regression Plot"
|
| 19 |
+
|
| 20 |
+
def dropdown(self, axis: str, label: str, options: List[str]) -> Any:
|
| 21 |
+
"""Create a dropdown control for the given axis."""
|
| 22 |
+
return html.Div(
|
| 23 |
+
[
|
| 24 |
+
html.Label(label),
|
| 25 |
+
dcc.Dropdown(
|
| 26 |
+
id={"type": "control", "plot": self.name, "axis": axis},
|
| 27 |
+
options=[{"label": c, "value": c} for c in options], # type: ignore
|
| 28 |
+
value=options[0],
|
| 29 |
+
clearable=False,
|
| 30 |
+
persistence=True,
|
| 31 |
+
),
|
| 32 |
+
],
|
| 33 |
+
style={"width": "200px", "marginBottom": "10px"},
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
def controls(self) -> Any:
|
| 37 |
+
"""Render the control panel for the plot."""
|
| 38 |
+
nums = self.numeric_columns()
|
| 39 |
+
cats = self.categorical_columns()
|
| 40 |
+
return html.Div(
|
| 41 |
+
[
|
| 42 |
+
self.dropdown("x", "X-Axis (numeric)", nums),
|
| 43 |
+
self.dropdown("y", "Y-Axis (numeric)", nums),
|
| 44 |
+
self.dropdown("color", "Color By", nums + cats),
|
| 45 |
+
],
|
| 46 |
+
style={"display": "flex", "flexDirection": "column"},
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
def render(self, **kwargs: Any) -> Any:
|
| 50 |
+
"""Render the regression plot based on selected axes."""
|
| 51 |
+
x = kwargs.get("x_axis")
|
| 52 |
+
y = kwargs.get("y_axis")
|
| 53 |
+
color = kwargs.get("color_axis")
|
| 54 |
+
|
| 55 |
+
fig = px.scatter(
|
| 56 |
+
self.dataframe,
|
| 57 |
+
x=x,
|
| 58 |
+
y=y,
|
| 59 |
+
color=color if color in self.dataframe.columns else None,
|
| 60 |
+
trendline="ols",
|
| 61 |
+
)
|
| 62 |
+
return dcc.Graph(figure=fig)
|
| 63 |
+
|
| 64 |
+
def register_callbacks(self, app: Any) -> None:
|
| 65 |
+
"""Register the callbacks for interactivity."""
|
| 66 |
+
|
| 67 |
+
@app.callback( # type: ignore
|
| 68 |
+
Output({"type": "plot-output", "plot": self.name}, "children"),
|
| 69 |
+
Input({"type": "control", "plot": self.name, "axis": "x"}, "value"),
|
| 70 |
+
Input({"type": "control", "plot": self.name, "axis": "y"}, "value"),
|
| 71 |
+
Input({"type": "control", "plot": self.name, "axis": "color"}, "value"),
|
| 72 |
+
)
|
| 73 |
+
def update(x_axis: str, y_axis: str, color_axis: str) -> Any:
|
| 74 |
+
"""Update the regression plot based on user selections."""
|
| 75 |
+
return self.render(
|
| 76 |
+
x_axis=x_axis,
|
| 77 |
+
y_axis=y_axis,
|
| 78 |
+
color_axis=color_axis,
|
| 79 |
+
)
|