Skip to content

Commit

Permalink
improve repr of the displays
Browse files Browse the repository at this point in the history
  • Loading branch information
glemaitre committed Jan 7, 2025
1 parent e2a3a74 commit aee7270
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
31 changes: 20 additions & 11 deletions skore/src/skore/sklearn/_plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from io import StringIO

from rich.console import Console
from rich.panel import Panel
from rich.tree import Tree
from sklearn.utils._plotting import check_consistent_length, check_matplotlib_support
from sklearn.utils.validation import _check_pos_label_consistency
Expand All @@ -15,7 +16,7 @@ def _get_attributes_for_help(self):
attributes = []
for name in dir(self):
if name.endswith("_") and not name.startswith("_"):
attributes.append(name)
attributes.append(f".{name}")
return sorted(attributes)

def _get_methods_for_help(self):
Expand All @@ -27,27 +28,27 @@ def _get_methods_for_help(self):
is_class_method = inspect.ismethod(method) and method.__self__ is type(self)
is_help_method = name == "help"
if not (is_private or is_class_method or is_help_method):
filtered_methods.append((name, method))
filtered_methods.append((f".{name}(...)", method))
return sorted(filtered_methods)

def _create_help_tree(self):
"""Create a rich Tree with attributes and methods."""
tree = Tree(f"📊 {self.__class__.__name__}")
tree = Tree("display")

attributes = self._get_attributes_for_help()
attr_branch = tree.add("📝 Attributes to tweak your plot")
attr_branch = tree.add("[bold cyan] Attributes[/bold cyan]")
# Ensure figure_ and ax_ are first
sorted_attrs = sorted(attributes)
sorted_attrs.remove("ax_")
sorted_attrs.remove("figure_")
sorted_attrs = ["figure_", "ax_"] + [
attr for attr in sorted_attrs if attr not in ["figure_", "ax_"]
sorted_attrs.remove(".ax_")
sorted_attrs.remove(".figure_")
sorted_attrs = [".figure_", ".ax_"] + [
attr for attr in sorted_attrs if attr not in [".figure_", ".ax_"]
]
for attr in sorted_attrs:
attr_branch.add(attr)

methods = self._get_methods_for_help()
method_branch = tree.add("🔧 Methods")
method_branch = tree.add("[bold cyan]Methods[/bold cyan]")
for name, method in methods:
description = (
method.__doc__.split("\n")[0]
Expand All @@ -58,16 +59,24 @@ def _create_help_tree(self):

return tree

def _create_help_panel(self):
return Panel(
self._create_help_tree(),
title=f"[bold cyan]📊 {self.__class__.__name__}[/bold cyan]",
border_style="orange1",
expand=False,
)

def help(self):
"""Display available attributes and methods using rich."""
from skore import console # avoid circular import

console.print(self._create_help_tree())
console.print(self._create_help_panel())

def __repr__(self):
"""Return a string representation using rich."""
console = Console(file=StringIO(), force_terminal=False)
console.print(self._create_help_tree())
console.print(self._create_help_panel())
return console.file.getvalue()


Expand Down
2 changes: 1 addition & 1 deletion skore/tests/unit/sklearn/plot/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_display_repr(pyplot, plot_func, estimator, dataset):
display = getattr(report.metrics.plot, plot_func)()

repr_str = repr(display)
assert repr_str.startswith(f"📊 {display.__class__.__name__}")
assert f"📊 {display.__class__.__name__}" in repr_str


@pytest.mark.parametrize(
Expand Down

0 comments on commit aee7270

Please sign in to comment.