Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions shapash/explainer/consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,26 @@
from plotly.subplots import make_subplots
from sklearn.manifold import MDS

from shapash.style.style_utils import colors_loading, define_style, select_palette
from shapash.style.style_utils import define_style, resolve_colors
from shapash.utils.dtypes import text_like_columns
from shapash.utils.utils import adjust_title_height


class Consistency:
"""Consistency class."""

def __init__(self, palette_name: str = "default") -> None:
"""
Compare the contributions computed by several explainability methods.

Parameters
----------
palette_name : str, default="default"
Name of the color palette, as for ``SmartExplainer``.
colors_dict : dict[str, Any] or None, default=None
Colors overriding the palette's, in the formats ``SmartExplainer`` accepts.
"""

def __init__(self, palette_name: str = "default", colors_dict: dict[str, Any] | None = None) -> None:
self._palette_name = palette_name
self._style_dict = define_style(select_palette(colors_loading(), self._palette_name))
self._style_dict = define_style(resolve_colors(palette_name, colors_dict))

def tuning_colorscale(self, values: pd.Series | pd.DataFrame) -> list[list[Any]]:
"""Adapts the color scale to the distribution of points.
Expand Down
72 changes: 58 additions & 14 deletions shapash/explainer/smart_explainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

import copy
import logging
from pathlib import Path
from typing import TYPE_CHECKING, Any, cast
Expand All @@ -13,7 +12,7 @@

import shapash.explainer.smart_predictor
from shapash.backend import BaseBackend
from shapash.style.style_utils import colors_loading, select_palette
from shapash.style.style_utils import colors_loading, merge_palette, normalize_colors, select_palette
from shapash.utils.custom_thread import CustomThread
from shapash.utils.drift import SchemaDriftConfig, compute_schema_distribution, resolve_schema_drift_config
from shapash.utils.io import load_pickle, save_pickle
Expand Down Expand Up @@ -116,10 +115,18 @@ class SmartExplainer:
title_story : str, optional
Custom title used in visualizations and reports. Default is empty.
palette_name : str, optional
Name of the color palette used for visualizations (see the `style` folder for options).
Name of the color palette used for visualizations: ``"default"`` or ``"blues"``, the
palettes defined in ``shapash/style/colors.json``. Defaults to ``"default"``.
colors_dict : dict, optional
Dictionary containing the full color palette configuration.
Can be used to override default plot colors.
Colors overriding those of ``palette_name``, with the same keys as the palette (the
keys of the chosen palette in ``shapash/style/colors.json``, e.g. ``title_color``,
``featureimp_bar``, ``contrib_colorscale``, ``webapp_bkg``). A nested mapping such as
``featureimp_bar`` can be given partially. Colors can be
``rgb(...)``/``rgba(...)`` strings, hex codes or color names. A continuous scale
(``contrib_colorscale``, ``confusion_matrix_colorscale``, ``interaction_scale``,
``feature_contributions_cumulative``) also accepts a plotly scale name such as
``"RdBu"`` or ``"viridis_r"``, and a list of distinct colors (``interaction_discrete``,
``compare_plot``) a plotly qualitative palette name such as ``"Set2"``.
**backend_kwargs : dict
Additional keyword arguments passed to the backend.

Expand Down Expand Up @@ -227,9 +234,7 @@ def __init__(
title_story = title_story if title_story is not None else ""
self.title_story = title_story
self.palette_name = palette_name if palette_name else "default"
self.colors_dict = copy.deepcopy(select_palette(colors_loading(), self.palette_name))
if colors_dict is not None:
self.colors_dict.update(colors_dict)
self.colors_dict = self._resolve_colors(select_palette(colors_loading(), self.palette_name), colors_dict)

self.explainer = Explainer(
model=model,
Expand Down Expand Up @@ -326,16 +331,55 @@ def compile(
)

def define_style(self, palette_name: str | None = None, colors_dict: dict[str, Any] | None = None) -> None:
"""Set the color set to use in plots."""
"""
Change the colors used in plots.

Parameters
----------
palette_name : str, optional
Name of the palette to switch to: ``"default"`` or ``"blues"`` (see
``shapash/style/colors.json``). Switching palette discards the colors
overridden earlier, in the constructor or in a previous call.
colors_dict : dict[str, Any], optional
Colors to override, merged on top of the palette and of the overrides already
in place. Keys are those of ``shapash/style/colors.json``; accepts the same
values as the constructor's ``colors_dict``.

Raises
------
ValueError
If neither ``palette_name`` nor ``colors_dict`` is given, if the palette does
not exist, or if a color in ``colors_dict`` is not valid.
"""
if palette_name is None and colors_dict is None:
raise ValueError("At least one of palette_name or colors_dict parameters must be defined")
new_palette_name = palette_name or self.palette_name
new_colors_dict = copy.deepcopy(select_palette(colors_loading(), new_palette_name))
if colors_dict is not None:
new_colors_dict.update(colors_dict)
self.colors_dict.update(new_colors_dict)
if palette_name is not None:
base = select_palette(colors_loading(), palette_name)
self.palette_name = palette_name
else:
base = self.colors_dict
self.colors_dict = self._resolve_colors(base, colors_dict)
self.plot.define_style_attributes(colors_dict=self.colors_dict)

@staticmethod
def _resolve_colors(base: dict[str, Any], colors_dict: dict[str, Any] | None) -> dict[str, Any]:
"""
Return ``base`` with the validated ``colors_dict`` overrides merged on top.

Parameters
----------
base : dict[str, Any]
Colors the overrides apply to.
colors_dict : dict[str, Any] or None
Color overrides.

Returns
-------
dict[str, Any]
The resolved colors.
"""
return merge_palette(base, normalize_colors(colors_dict, base))

def add(
self,
y_pred: pd.Series | pd.DataFrame | None = None,
Expand Down
10 changes: 5 additions & 5 deletions shapash/plots/plot_backend_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
from dash import html
from plotly import graph_objs as go

from shapash.plots.plot_sentence_highlight import _parse_rgb, _shap_style
from shapash.style.style_utils import DEFAULT_NLP_THEME
from shapash.plots.plot_sentence_highlight import _shap_style
from shapash.style.style_utils import DEFAULT_NLP_THEME, parse_rgb

# Backend identity colors, assigned in this fixed order (never cycled) — the reference categorical
# palette's first slots, validated for colour-vision-deficiency separation. Only the grouped bar
Expand All @@ -52,7 +52,7 @@ def _check(tokens: Sequence[str], values_by_backend: Mapping[str, np.ndarray]) -


def _opaque(color: str) -> str:
r, g, b = _parse_rgb(color)
r, g, b = parse_rgb(color)
return f"rgb({r},{g},{b})"


Expand Down Expand Up @@ -247,15 +247,15 @@ def plot_backend_highlight(
subtitle : str, optional
A line shown under the legend (e.g. agreement scores).
color_positive, color_negative : str
``rgb(...)``/``rgba(...)`` strings, as for
Colors, as for
:func:`~shapash.plots.plot_sentence_highlight.plot_sentence_highlight`.

Returns
-------
html.Div
"""
values_by_backend = _check(tokens, values_by_backend)
pos_rgb, neg_rgb = _parse_rgb(color_positive), _parse_rgb(color_negative)
pos_rgb, neg_rgb = parse_rgb(color_positive), parse_rgb(color_negative)
span_base = {"padding": "3px 5px", "borderRadius": "3px", "margin": "2px 1px", "display": "inline-block"}
unattributed = {**span_base, "color": "#aaaaaa", "textDecoration": "line-through"}

Expand Down
12 changes: 7 additions & 5 deletions shapash/plots/plot_evaluation_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,8 @@ def plot_clustering_by_explainability(
marker_size : int, optional, default=10
Size of the data point markers in the plot.
style_dict : dict[str, Any] or None, optional, default=None
Custom styling options (e.g., fonts, layout settings).
Resolved style, such as ``SmartExplainer.plot._style_dict``. Its ``clustering`` entry
colors the selected cluster and the other ones. The default palette is used when None.
opacity : float, optional, default=0.8
Opacity of the data point markers (between 0 and 1).
width : int, optional, default=900
Expand Down Expand Up @@ -1007,6 +1008,7 @@ def plot_clustering_by_explainability(
"""
if style_dict is None:
style_dict = define_style(get_palette("default"))
cluster_colors = style_dict["clustering"]

dict_title = build_tsne_title(title, subtitle, addnote, style_dict=style_dict, height=height)

Expand Down Expand Up @@ -1092,12 +1094,12 @@ def plot_clustering_by_explainability(
# Couleurs dynamiques pour le hover
c_n = c # curve number
if c_n == active_cluster:
fill = "rgba(255,0,0,0.30)"
line_color = "rgba(200,0,0,1)"
fill = cluster_colors["active_fill"]
line_color = cluster_colors["active_line"]
else:
if show_points:
fill = "rgba(150,150,150,0.15)"
line_color = "rgba(200,200,200,0.8)"
fill = cluster_colors["inactive_fill"]
line_color = cluster_colors["inactive_line"]
else:
fill = value_to_rgba(float(cluster_color[c]), colorscale, float(cmin), float(cmax), alpha=0.99)
line_color = value_to_rgba(
Expand Down
15 changes: 4 additions & 11 deletions shapash/plots/plot_sentence_highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,11 @@
import numpy as np
from dash import html

from shapash.style.style_utils import DEFAULT_NLP_THEME
from shapash.style.style_utils import DEFAULT_NLP_THEME, parse_rgb

_SPECIAL_RE = re.compile(r"^\[.*\]$|^##|^\s*$")


def _parse_rgb(color: str) -> tuple[int, int, int]:
"""Parse a ``rgb(r, g, b)``/``rgba(r, g, b, a)`` string (the ``colors.json`` format) to ints."""
nums = color.replace("rgba", "").replace("rgb", "").replace("(", "").replace(")", "").split(",")
r, g, b = (int(float(n)) for n in nums[:3])
return r, g, b


def _shap_style(
val: float, max_abs: float, pos_rgb: tuple[int, int, int], neg_rgb: tuple[int, int, int]
) -> dict[str, str]:
Expand Down Expand Up @@ -74,8 +67,8 @@ def plot_sentence_highlight(
symmetry with the other plot functions.
color_positive, color_negative : str
Span background for a non-negative / negative contribution, interpolated from white by
magnitude. Must be a ``rgb(r, g, b)``/``rgba(r, g, b, a)`` string (the ``colors.json``
format). Default to the ``"default"`` palette's ``nlp_xpl_positive``/``nlp_xpl_negative``
magnitude. Any ``rgb(...)``/``rgba(...)`` string, hex code or color name; the
opacity is ignored. Default to the ``"default"`` palette's ``nlp_xpl_positive``/``nlp_xpl_negative``
— see :class:`~shapash.webapp.nlp_app.NlpWebApp`'s ``palette_name``/``colors_dict`` to
theme every NLP chart at once instead of overriding this one call.

Expand All @@ -92,7 +85,7 @@ def plot_sentence_highlight(
if max_abs == 0:
max_abs = 1.0

pos_rgb, neg_rgb = _parse_rgb(color_positive), _parse_rgb(color_negative)
pos_rgb, neg_rgb = parse_rgb(color_positive), parse_rgb(color_negative)

_span_base = {
"padding": "3px 5px",
Expand Down
Loading
Loading