viz.py 943 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from __future__ import annotations
  2. from dataclasses import dataclass
  3. from typing import Any
  4. from wandb.data_types import Table
  5. from wandb.errors import Error
  6. @dataclass
  7. class VisualizeSpec:
  8. name: str
  9. key: str = ""
  10. @property
  11. def config_value(self) -> dict[str, Any]:
  12. return {
  13. "id": self.name,
  14. "historyFieldSettings": {"x-axis": "_step", "key": self.key},
  15. }
  16. @property
  17. def config_key(self) -> tuple[str, str, str]:
  18. return ("_wandb", "viz", self.key)
  19. @dataclass
  20. class Visualize:
  21. table: Table
  22. spec: VisualizeSpec
  23. def set_key(self, key: str) -> None:
  24. self.spec.key = key
  25. def visualize(id: str, value: Table) -> Visualize:
  26. if not isinstance(value, Table):
  27. raise Error(
  28. f"Expected `value` to be `wandb.Table` type, instead got {type(value).__name__}"
  29. )
  30. return Visualize(table=value, spec=VisualizeSpec(name=id))