Register a classmethod of Matplotlib Visualizer
(func: T.Callable[T_Input, Figure])
| 53 | |
| 54 | @staticmethod |
| 55 | def register(func: T.Callable[T_Input, Figure]) -> T.Callable[T.Concatenate[str, T_Input], None]: |
| 56 | """ |
| 57 | Register a classmethod of Matplotlib Visualizer |
| 58 | """ |
| 59 | @wraps(func) |
| 60 | def implement(file_prefix: str, *args: T_Input.args, **kwargs: T_Input.kwargs) -> None: |
| 61 | if func.__name__ not in Matplotlib_Visualizer.func_mode: |
| 62 | Matplotlib_Visualizer.func_mode[func.__name__] = "default" |
| 63 | |
| 64 | func_calls: int = implement.calls #type: ignore # PyRight does not support this. |
| 65 | implement.calls += 1 #type: ignore |
| 66 | func_mode = Matplotlib_Visualizer.func_mode[func.__name__] |
| 67 | if func_mode == 'default': func_mode = Matplotlib_Visualizer.default_mode |
| 68 | |
| 69 | match func_mode: |
| 70 | case "none": |
| 71 | return None |
| 72 | case "image": |
| 73 | fig = func(*args, **kwargs) |
| 74 | fig.savefig(Path(Matplotlib_Visualizer.default_save, (save_path := f"{file_prefix}_{func_calls:05}.png")), dpi=Matplotlib_Visualizer.default_dpi) |
| 75 | Logger.write("info", f"Save figure at {save_path}") |
| 76 | plt.close(fig) |
| 77 | case "rerun": |
| 78 | assert rr is not None |
| 79 | fig = func(*args, **kwargs) |
| 80 | buf = BytesIO() |
| 81 | fig.savefig(buf, format='jpg', dpi=Matplotlib_Visualizer.default_dpi) |
| 82 | buf.seek(0) |
| 83 | rr.log(file_prefix, rr.ImageEncoded(contents=buf, format=rr.ImageFormat.JPEG)) |
| 84 | implement.calls = 0 #type: ignore # PyRight does not support this. |
| 85 | return implement |
| 86 | |
| 87 | @staticmethod |
| 88 | def __plot_grid(dpi: int, n_rows: int, n_cols: int, plot_args: T.Sequence[T.Callable[[Axes], T.Any] | None], h_factor: float = 4, w_factor: float = 4) -> Figure: |
nothing calls this directly
no outgoing calls
no test coverage detected