-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add a method .set_style to displays (#1336)
closes #1273 This PR is adding a method `.style` used to set the plotting style that can be reused each time the `display.plot()`.
- Loading branch information
Showing
8 changed files
with
150 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from typing import Any | ||
|
||
|
||
class StyleDisplayMixin: | ||
"""Mixin to control the style plot of a display.""" | ||
|
||
@property | ||
def _style_params(self) -> list[str]: | ||
"""Get the list of available style parameters. | ||
Returns | ||
------- | ||
list | ||
List of style parameter names (without '_default_' prefix). | ||
""" | ||
prefix = "_default_" | ||
suffix = "_kwargs" | ||
return [ | ||
attr[len(prefix) :] | ||
for attr in dir(self) | ||
if attr.startswith(prefix) and attr.endswith(suffix) | ||
] | ||
|
||
def set_style(self, **kwargs: Any): | ||
"""Set the style parameters for the display. | ||
Parameters | ||
---------- | ||
**kwargs : dict | ||
Style parameters to set. Each parameter name should correspond to a | ||
a style attribute passed to the plot method of the display. | ||
Returns | ||
------- | ||
self : object | ||
Returns the instance itself. | ||
Raises | ||
------ | ||
ValueError | ||
If a style parameter is unknown. | ||
""" | ||
for param_name, param_value in kwargs.items(): | ||
default_attr = f"_default_{param_name}" | ||
if not hasattr(self, default_attr): | ||
raise ValueError( | ||
f"Unknown style parameter: {param_name}. " | ||
f"The parameter name should be one of {self._style_params}." | ||
) | ||
setattr(self, default_attr, param_value) | ||
return self |
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import pytest | ||
from skore.sklearn._plot.style import StyleDisplayMixin | ||
|
||
|
||
class TestDisplay(StyleDisplayMixin): | ||
_default_some_kwargs = None | ||
|
||
|
||
def test_style_mixin(): | ||
display = TestDisplay() | ||
display.set_style(some_kwargs=1) | ||
assert display._default_some_kwargs == 1 | ||
|
||
with pytest.raises(ValueError, match="Unknown style parameter: unknown_param."): | ||
display.set_style(unknown_param=1) |