Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(client): rich print null when table have too many cols #1534

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
1 change: 1 addition & 0 deletions client/starwhale/consts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class SWDSSubFileType:

DEFAULT_PAGE_IDX = 1
DEFAULT_PAGE_SIZE = 20
DEFAULT_REPORT_COLS = 20

RECOVER_DIRNAME = ".recover"
OBJECT_STORE_DIRNAME = ".objectstore"
Expand Down
20 changes: 17 additions & 3 deletions client/starwhale/core/eval/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
from starwhale.utils.cli import AliasedGroup
from starwhale.consts.env import SWEnv

from .view import JobTermView, get_term_view, DEFAULT_PAGE_IDX, DEFAULT_PAGE_SIZE
from .view import (
JobTermView,
get_term_view,
DEFAULT_PAGE_IDX,
DEFAULT_PAGE_SIZE,
DEFAULT_REPORT_COLS,
)


@click.group(
Expand Down Expand Up @@ -175,9 +181,17 @@ def _cancel(job: str, force: bool) -> None:
@click.option(
"--size", type=int, default=DEFAULT_PAGE_SIZE, help="Page size for tasks list"
)
@click.option(
"--max-report-cols",
type=int,
default=DEFAULT_REPORT_COLS,
help="Max table column size for print",
)
@click.pass_obj
def _info(view: t.Type[JobTermView], job: str, page: int, size: int) -> None:
view(job).info(page, size)
def _info(
view: t.Type[JobTermView], job: str, page: int, size: int, max_report_cols: int
) -> None:
view(job).info(page, size, max_report_cols)


@eval_job_cmd.command("compare", aliases=["cmp"])
Expand Down
2 changes: 1 addition & 1 deletion client/starwhale/core/eval/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _get_report(self) -> t.Dict[str, t.Any]:

if kind == MetricKind.MultiClassification.value:
ret["labels"] = {
str(i): l for i, l in enumerate(list(evaluation.get("labels")))
item["id"]: item for item in list(evaluation.get("labels"))
}
ret["confusion_matrix"] = {
"binarylabel": list(evaluation.get("confusion_matrix/binarylabel"))
Expand Down
54 changes: 42 additions & 12 deletions client/starwhale/core/eval/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
DEFAULT_PAGE_IDX,
DEFAULT_PAGE_SIZE,
SHORT_VERSION_CNT,
DEFAULT_REPORT_COLS,
DEFAULT_MANIFEST_NAME,
)
from starwhale.base.uri import URI
Expand Down Expand Up @@ -112,7 +113,12 @@ def compare(self, job_uris: t.List[str]) -> None:
console.print(table)

@BaseTermView._header
def info(self, page: int = DEFAULT_PAGE_IDX, size: int = DEFAULT_PAGE_SIZE) -> None:
def info(
self,
page: int = DEFAULT_PAGE_IDX,
size: int = DEFAULT_PAGE_SIZE,
max_report_cols: int = DEFAULT_REPORT_COLS,
) -> None:
_rt = self.job.info(page, size)
if not _rt:
console.print(":tea: not found info")
Expand Down Expand Up @@ -140,7 +146,9 @@ def info(self, page: int = DEFAULT_PAGE_IDX, size: int = DEFAULT_PAGE_SIZE) -> N
self._render_summary_report(_report["summary"], _kind)

if _kind == MetricKind.MultiClassification.value:
self._render_multi_classification_job_report(_rt["report"])
self._render_multi_classification_job_report(
_rt["report"], max_report_cols
)

def _render_summary_report(self, summary: t.Dict[str, t.Any], kind: str) -> None:
console.rule(f"[bold green]{kind.upper()} Summary")
Expand Down Expand Up @@ -175,7 +183,7 @@ def _print_tasks(self, tasks: t.List[t.Dict[str, t.Any]]) -> None:
console.print(table)

def _render_multi_classification_job_report(
self, report: t.Dict[str, t.Any]
self, report: t.Dict[str, t.Any], max_report_cols: int
) -> None:
if not report:
console.print(":turtle: no report")
Expand All @@ -191,14 +199,24 @@ def _print_labels() -> None:
table = Table(box=box.SIMPLE)
table.add_column("Label", style="cyan")
keys = labels[sort_label_names[0]]
for _k in keys:
for idx, _k in enumerate(keys):
if _k == "id":
continue
table.add_column(_k.capitalize())
# add 1: because "id" is first col and should be removed
if idx < max_report_cols + 1:
table.add_column(_k.capitalize())
else:
table.add_column("...")
break

for _k, _v in labels.items():
table.add_row(
_k, *(f"{float(_v[_k2]):.4f}" for _k2 in keys if _k2 != "id")
_k,
*(
f"{float(_v[_k2]):.4f}"
for idx, _k2 in enumerate(keys)
if _k2 != "id" and idx < max_report_cols + 1
),
)

console.rule(f"[bold green]{report['kind'].upper()} Label Metrics Report")
Expand All @@ -209,16 +227,23 @@ def _print_confusion_matrix() -> None:
if not cm:
return

btable = Table(box=box.SIMPLE)
btable = Table(box=box.ROUNDED)
btable.add_column("", style="cyan")
for n in sort_label_names:
btable.add_column(n)
for idx, n in enumerate(sort_label_names):
if idx < max_report_cols:
btable.add_column(n)
else:
btable.add_column("...")
break
for idx, bl in enumerate(cm.get("binarylabel", [])):
btable.add_row(
sort_label_names[idx],
*[f"{float(bl[i]):.4f}" for i in bl if i != "id"],
*[
f"{float(bl[i]):.4f}"
for idx, i in enumerate(bl)
if i != "id" and idx < max_report_cols + 1
],
)

console.rule(f"[bold green]{report['kind'].upper()} Confusion Matrix")
console.print(btable)

Expand Down Expand Up @@ -385,7 +410,12 @@ def list( # type: ignore
_data, _ = super().list(project_uri, fullname, show_removed, page, size)
cls.pretty_json(_data)

def info(self, page: int = DEFAULT_PAGE_IDX, size: int = DEFAULT_PAGE_SIZE) -> None:
def info(
self,
page: int = DEFAULT_PAGE_IDX,
size: int = DEFAULT_PAGE_SIZE,
max_report_cols: int = DEFAULT_REPORT_COLS,
) -> None:
_rt = self.job.info(page, size)
if not _rt:
console.print(":tea: not found info")
Expand Down
19 changes: 19 additions & 0 deletions client/tests/core/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ def test_info(
assert info["manifest"]["model"] == "mnist:meydczbrmi2g"
assert info["report"]["kind"] == "multi_classification"

@patch("starwhale.core.eval.view.console.print")
@patch("starwhale.core.eval.view.Table.add_column")
def test_render_with_limit(self, m_table_add_col: MagicMock, m_console: MagicMock):
report = {
"kind": "multi_classification",
"labels": {
"1": {"id": "1", "Precision": 1.00, "Recall": 1.00, "F1-score": 1.00},
"2": {"id": "2", "Precision": 1.00, "Recall": 1.00, "F1-score": 1.00},
"3": {"id": "3", "Precision": 1.00, "Recall": 1.00, "F1-score": 1.00},
},
}

JobTermView(self.job_name)._render_multi_classification_job_report(
report=report, max_report_cols=2
)

# 1(common col:"Label") + 2(max count) + 1(ignore col:"...")
assert m_table_add_col.call_count == 4

def test_remove(self):
uri = URI(f"local/project/self/{URIType.EVALUATION}/{self.job_name[:6]}")
job = StandaloneEvaluationJob(uri)
Expand Down