|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | | -import copy |
6 | 5 | import logging |
7 | 6 | from pathlib import Path |
8 | 7 | from typing import TYPE_CHECKING, Any, cast |
|
13 | 12 |
|
14 | 13 | import shapash.explainer.smart_predictor |
15 | 14 | from shapash.backend import BaseBackend |
16 | | -from shapash.style.style_utils import colors_loading, select_palette |
| 15 | +from shapash.style.style_utils import colors_loading, merge_palette, normalize_colors, select_palette |
17 | 16 | from shapash.utils.custom_thread import CustomThread |
18 | 17 | from shapash.utils.drift import SchemaDriftConfig, compute_schema_distribution, resolve_schema_drift_config |
19 | 18 | from shapash.utils.io import load_pickle, save_pickle |
@@ -116,10 +115,18 @@ class SmartExplainer: |
116 | 115 | title_story : str, optional |
117 | 116 | Custom title used in visualizations and reports. Default is empty. |
118 | 117 | palette_name : str, optional |
119 | | - Name of the color palette used for visualizations (see the `style` folder for options). |
| 118 | + Name of the color palette used for visualizations: ``"default"`` or ``"blues"``, the |
| 119 | + palettes defined in ``shapash/style/colors.json``. Defaults to ``"default"``. |
120 | 120 | colors_dict : dict, optional |
121 | | - Dictionary containing the full color palette configuration. |
122 | | - Can be used to override default plot colors. |
| 121 | + Colors overriding those of ``palette_name``, with the same keys as the palette (the |
| 122 | + keys of the chosen palette in ``shapash/style/colors.json``, e.g. ``title_color``, |
| 123 | + ``featureimp_bar``, ``contrib_colorscale``, ``webapp_bkg``). A nested mapping such as |
| 124 | + ``featureimp_bar`` can be given partially. Colors can be |
| 125 | + ``rgb(...)``/``rgba(...)`` strings, hex codes or color names. A continuous scale |
| 126 | + (``contrib_colorscale``, ``confusion_matrix_colorscale``, ``interaction_scale``, |
| 127 | + ``feature_contributions_cumulative``) also accepts a plotly scale name such as |
| 128 | + ``"RdBu"`` or ``"viridis_r"``, and a list of distinct colors (``interaction_discrete``, |
| 129 | + ``compare_plot``) a plotly qualitative palette name such as ``"Set2"``. |
123 | 130 | **backend_kwargs : dict |
124 | 131 | Additional keyword arguments passed to the backend. |
125 | 132 |
|
@@ -227,9 +234,7 @@ def __init__( |
227 | 234 | title_story = title_story if title_story is not None else "" |
228 | 235 | self.title_story = title_story |
229 | 236 | self.palette_name = palette_name if palette_name else "default" |
230 | | - self.colors_dict = copy.deepcopy(select_palette(colors_loading(), self.palette_name)) |
231 | | - if colors_dict is not None: |
232 | | - self.colors_dict.update(colors_dict) |
| 237 | + self.colors_dict = self._resolve_colors(select_palette(colors_loading(), self.palette_name), colors_dict) |
233 | 238 |
|
234 | 239 | self.explainer = Explainer( |
235 | 240 | model=model, |
@@ -326,16 +331,55 @@ def compile( |
326 | 331 | ) |
327 | 332 |
|
328 | 333 | def define_style(self, palette_name: str | None = None, colors_dict: dict[str, Any] | None = None) -> None: |
329 | | - """Set the color set to use in plots.""" |
| 334 | + """ |
| 335 | + Change the colors used in plots. |
| 336 | +
|
| 337 | + Parameters |
| 338 | + ---------- |
| 339 | + palette_name : str, optional |
| 340 | + Name of the palette to switch to: ``"default"`` or ``"blues"`` (see |
| 341 | + ``shapash/style/colors.json``). Switching palette discards the colors |
| 342 | + overridden earlier, in the constructor or in a previous call. |
| 343 | + colors_dict : dict[str, Any], optional |
| 344 | + Colors to override, merged on top of the palette and of the overrides already |
| 345 | + in place. Keys are those of ``shapash/style/colors.json``; accepts the same |
| 346 | + values as the constructor's ``colors_dict``. |
| 347 | +
|
| 348 | + Raises |
| 349 | + ------ |
| 350 | + ValueError |
| 351 | + If neither ``palette_name`` nor ``colors_dict`` is given, if the palette does |
| 352 | + not exist, or if a color in ``colors_dict`` is not valid. |
| 353 | + """ |
330 | 354 | if palette_name is None and colors_dict is None: |
331 | 355 | raise ValueError("At least one of palette_name or colors_dict parameters must be defined") |
332 | | - new_palette_name = palette_name or self.palette_name |
333 | | - new_colors_dict = copy.deepcopy(select_palette(colors_loading(), new_palette_name)) |
334 | | - if colors_dict is not None: |
335 | | - new_colors_dict.update(colors_dict) |
336 | | - self.colors_dict.update(new_colors_dict) |
| 356 | + if palette_name is not None: |
| 357 | + base = select_palette(colors_loading(), palette_name) |
| 358 | + self.palette_name = palette_name |
| 359 | + else: |
| 360 | + base = self.colors_dict |
| 361 | + self.colors_dict = self._resolve_colors(base, colors_dict) |
337 | 362 | self.plot.define_style_attributes(colors_dict=self.colors_dict) |
338 | 363 |
|
| 364 | + @staticmethod |
| 365 | + def _resolve_colors(base: dict[str, Any], colors_dict: dict[str, Any] | None) -> dict[str, Any]: |
| 366 | + """ |
| 367 | + Return ``base`` with the validated ``colors_dict`` overrides merged on top. |
| 368 | +
|
| 369 | + Parameters |
| 370 | + ---------- |
| 371 | + base : dict[str, Any] |
| 372 | + Colors the overrides apply to. |
| 373 | + colors_dict : dict[str, Any] or None |
| 374 | + Color overrides. |
| 375 | +
|
| 376 | + Returns |
| 377 | + ------- |
| 378 | + dict[str, Any] |
| 379 | + The resolved colors. |
| 380 | + """ |
| 381 | + return merge_palette(base, normalize_colors(colors_dict, base)) |
| 382 | + |
339 | 383 | def add( |
340 | 384 | self, |
341 | 385 | y_pred: pd.Series | pd.DataFrame | None = None, |
|
0 commit comments