afshin-dini's picture
Update the margins
3eaf500
"""Regression Plot plugin for AI-Dashboard"""
# pylint: disable=R0801
from typing import Any, List
import logging
import plotly.express as px
from dash import html, dcc
from dash.dependencies import Input, Output
from ..base import BasePlotPlugin
logger = logging.getLogger(__name__)
class RegressionPlotPlugin(BasePlotPlugin):
"""Scatter + regression line plot plugin."""
name = "Regression Plot"
def dropdown(self, axis: str, label: str, options: List[str]) -> Any:
"""Create a dropdown control for the given axis."""
return html.Div(
[
html.Label(label),
dcc.Dropdown(
id={"type": "control", "plot": self.name, "axis": axis},
options=[{"label": c, "value": c} for c in options], # type: ignore
value=options[0],
clearable=False,
persistence=True,
persistence_type="memory",
style={"color": "#000"},
),
],
style={"width": "130px"},
)
def controls(self) -> Any:
"""Render the control panel for the plot."""
nums = self.numeric_columns()
cats = self.categorical_columns()
return html.Div(
[
self.dropdown("x", "X-Axis (numeric)", nums),
self.dropdown("y", "Y-Axis (numeric)", nums),
self.dropdown("color", "Color By", nums + cats),
],
style={"display": "flex", "flexDirection": "column"},
)
def render(self, **kwargs: Any) -> Any:
"""Render the regression plot based on selected axes."""
x = kwargs.get("x_axis")
y = kwargs.get("y_axis")
color = kwargs.get("color_axis")
fig = px.scatter(
self.dataframe,
x=x,
y=y,
color=color if color in self.dataframe.columns else None,
trendline="ols",
)
return dcc.Graph(figure=fig)
def register_callbacks(self, app: Any) -> None:
"""Register the callbacks for interactivity."""
@app.callback( # type: ignore
Output({"type": "plot-output", "plot": self.name}, "children"),
Input({"type": "control", "plot": self.name, "axis": "x"}, "value"),
Input({"type": "control", "plot": self.name, "axis": "y"}, "value"),
Input({"type": "control", "plot": self.name, "axis": "color"}, "value"),
)
def update(x_axis: str, y_axis: str, color_axis: str) -> Any:
"""Update the regression plot based on user selections."""
return self.render(
x_axis=x_axis,
y_axis=y_axis,
color_axis=color_axis,
)