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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# src/visualizers/base_visualizer.py
|
| 2 |
+
from abc import ABC, abstractmethod
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from typing import Dict, Type, Any, Optional, Callable, List
|
| 5 |
+
import streamlit as st
|
| 6 |
+
|
| 7 |
+
# --- Component Registry ---
|
| 8 |
+
VISUALIZER_REGISTRY: Dict[str, Type["VisualizationComponent"]] = {}
|
| 9 |
+
|
| 10 |
+
def register_visualizer(name: str):
|
| 11 |
+
"""
|
| 12 |
+
一个装饰器,用于将可视化组件类注册到全局注册表。
|
| 13 |
+
A decorator to register a visualization component class to the global registry.
|
| 14 |
+
"""
|
| 15 |
+
def decorator(cls: Type["VisualizationComponent"]):
|
| 16 |
+
if name in VISUALIZER_REGISTRY:
|
| 17 |
+
# 在调试或开发模式下可能是警告,生产模式下可能是错误
|
| 18 |
+
# In debug/dev mode this might be a warning, in production an error
|
| 19 |
+
print(f"警告: 可视化组件 '{name}' 已被注册,将被覆盖。Visualizer '{name}' already registered. Will be overridden.")
|
| 20 |
+
VISUALIZER_REGISTRY[name] = cls
|
| 21 |
+
return cls
|
| 22 |
+
return decorator
|
| 23 |
+
|
| 24 |
+
def get_visualizer_class(type_name: str) -> Optional[Type["VisualizationComponent"]]:
|
| 25 |
+
"""
|
| 26 |
+
从注册表中获取组件类。
|
| 27 |
+
Gets a component class from the registry.
|
| 28 |
+
"""
|
| 29 |
+
return VISUALIZER_REGISTRY.get(type_name)
|
| 30 |
+
|
| 31 |
+
# --- Abstract Base Class for Visualization Components ---
|
| 32 |
+
class VisualizationComponent(ABC):
|
| 33 |
+
"""
|
| 34 |
+
可视化组件的抽象基类。
|
| 35 |
+
Abstract base class for all visualization components.
|
| 36 |
+
"""
|
| 37 |
+
def __init__(self,
|
| 38 |
+
component_instance_id: str,
|
| 39 |
+
trial_root_path: Path,
|
| 40 |
+
# data_asset_info: Dict[str, Any], # 描述此组件主要关联的数据资产信息 (来自清单)
|
| 41 |
+
# Describes the data asset this component is primarily associated with (from manifest)
|
| 42 |
+
# ^ 将被更通用的 data_sources_map 替代
|
| 43 |
+
data_sources_map: Dict[str, Dict[str, Any]], # Key: 逻辑数据源名称, Value: 数据资产信息字典
|
| 44 |
+
# Key: logical data source name, Value: data asset info dict
|
| 45 |
+
# e.g., {"main_scalar_data": asset_info_for_loss, "reference_images": asset_info_for_images}
|
| 46 |
+
component_specific_config: Dict[str, Any] = None
|
| 47 |
+
):
|
| 48 |
+
"""
|
| 49 |
+
初始化可视化组件。
|
| 50 |
+
Initializes the visualization component.
|
| 51 |
+
|
| 52 |
+
Args:
|
| 53 |
+
component_instance_id (str): 此组件在仪表盘上的唯一实例ID。
|
| 54 |
+
Unique instance ID for this component on the dashboard.
|
| 55 |
+
trial_root_path (Path): 此组件所属的Trial的根目录路径。
|
| 56 |
+
Root directory path of the Trial this component belongs to.
|
| 57 |
+
data_sources_map (Dict[str, Dict[str, Any]]):
|
| 58 |
+
一个字典,映射逻辑数据源名称到具体的数据资产信息。
|
| 59 |
+
数据资产信息字典通常包含 'asset_id', 'display_name', 'data_type', 'path',
|
| 60 |
+
以及其他从 _trial_manifest.toml 中解析得到的元数据。
|
| 61 |
+
A dictionary mapping logical data source names to specific data asset information.
|
| 62 |
+
The data asset info dict typically contains 'asset_id', 'display_name', 'data_type', 'path',
|
| 63 |
+
and other metadata parsed from _trial_manifest.toml.
|
| 64 |
+
component_specific_config (Dict[str, Any], optional):
|
| 65 |
+
特定于此组件实例的配置 (例如,图表标题、颜色等)。
|
| 66 |
+
Configuration specific to this component instance (e.g., chart title, color, etc.).
|
| 67 |
+
"""
|
| 68 |
+
self.component_instance_id = component_instance_id
|
| 69 |
+
self.trial_root_path = trial_root_path
|
| 70 |
+
self.data_sources_map = data_sources_map
|
| 71 |
+
self.config = component_specific_config if component_specific_config is not None else {}
|
| 72 |
+
|
| 73 |
+
# 组件自身持久化配置或小型数据的路径
|
| 74 |
+
# Path for the component to persist its own configuration or small data
|
| 75 |
+
self.component_private_storage_path = self.trial_root_path / "visualizers_data" / self.component_instance_id
|
| 76 |
+
self.component_private_storage_path.mkdir(parents=True, exist_ok=True)
|
| 77 |
+
|
| 78 |
+
self._current_global_step: Optional[int] = None
|
| 79 |
+
self._on_global_step_change_request: Optional[Callable[[int], None]] = None
|
| 80 |
+
self._all_available_steps: Optional[List[int]] = None # 由主应用或数据加载器填充
|
| 81 |
+
|
| 82 |
+
def _get_data_asset_info(self, logical_name: str = "default") -> Optional[Dict[str, Any]]:
|
| 83 |
+
"""
|
| 84 |
+
辅助方法,获取指定逻辑名称的数据资产信息。
|
| 85 |
+
Helper method to get data asset info for a given logical name.
|
| 86 |
+
如果组件只处理一个主要数据源,可以使用 "default" 或在初始化时指定。
|
| 87 |
+
If a component handles one primary data source, "default" or a specific name can be used.
|
| 88 |
+
"""
|
| 89 |
+
return self.data_sources_map.get(logical_name)
|
| 90 |
+
|
| 91 |
+
def _get_data_asset_path(self, logical_name: str = "default") -> Optional[Path]:
|
| 92 |
+
"""获取指定逻辑数据源的绝对路径。Gets the absolute path for a given logical data source."""
|
| 93 |
+
asset_info = self._get_data_asset_info(logical_name)
|
| 94 |
+
if asset_info and "path" in asset_info:
|
| 95 |
+
# 路径在清单中是相对于trial_root_path的
|
| 96 |
+
# Path in manifest is relative to trial_root_path
|
| 97 |
+
return (self.trial_root_path / asset_info["path"]).resolve()
|
| 98 |
+
return None
|
| 99 |
+
|
| 100 |
+
def configure_global_step_interaction(self,
|
| 101 |
+
current_step: Optional[int],
|
| 102 |
+
all_available_steps: Optional[List[int]],
|
| 103 |
+
on_step_change_request_callback: Optional[Callable[[int], None]]):
|
| 104 |
+
"""
|
| 105 |
+
由主应用调用,以配置与全局步骤相关的交互。
|
| 106 |
+
Called by the main application to configure global step related interactions.
|
| 107 |
+
|
| 108 |
+
Args:
|
| 109 |
+
current_step (Optional[int]): 当前选中的全局步骤。
|
| 110 |
+
The currently selected global step.
|
| 111 |
+
all_available_steps (Optional[List[int]]): 此Trial中所有可用的全局步骤列表 (已排序)。
|
| 112 |
+
A sorted list of all available global steps in this Trial.
|
| 113 |
+
on_step_change_request_callback (Optional[Callable[[int], None]]):
|
| 114 |
+
当组件希望更改全局步骤时调用的回调函数。
|
| 115 |
+
Callback function to be called when the component wishes to change the global step.
|
| 116 |
+
"""
|
| 117 |
+
self._current_global_step = current_step
|
| 118 |
+
self._all_available_steps = sorted(list(set(all_available_steps))) if all_available_steps else []
|
| 119 |
+
self._on_global_step_change_request = on_step_change_request_callback
|
| 120 |
+
|
| 121 |
+
def _request_global_step_change(self, new_step: int) -> None:
|
| 122 |
+
"""
|
| 123 |
+
组件内部调用此方法来请求更改全局共享的global_step。
|
| 124 |
+
Component calls this internally to request a change to the shared global_step.
|
| 125 |
+
"""
|
| 126 |
+
if self._on_global_step_change_request:
|
| 127 |
+
if self._all_available_steps and new_step not in self._all_available_steps:
|
| 128 |
+
# 如果请求的步骤无效,可以选择寻找最近的有效步骤或忽略
|
| 129 |
+
# If requested step is invalid, can choose to find nearest valid step or ignore
|
| 130 |
+
# For now, let's assume the interaction (e.g., chart click) provides a valid step from its data
|
| 131 |
+
print(f"警告: 组件 {self.component_instance_id} 请求了一个无效的全局步骤 {new_step}。")
|
| 132 |
+
# Potentially find closest:
|
| 133 |
+
# if self._all_available_steps:
|
| 134 |
+
# new_step = min(self._all_available_steps, key=lambda x: abs(x - new_step))
|
| 135 |
+
|
| 136 |
+
self._on_global_step_change_request(new_step)
|
| 137 |
+
else:
|
| 138 |
+
st.warning(f"组件 {self.component_instance_id}: 尝试更改全局步骤,但未设置回调。")
|
| 139 |
+
|
| 140 |
+
def _get_closest_available_step(self, target_step: Optional[int]) -> Optional[int]:
|
| 141 |
+
"""
|
| 142 |
+
如果目标步骤无效或数据在该步骤不可用,则查找最近的可用步骤。
|
| 143 |
+
Finds the closest available step if the target step is invalid or data isn't available at that step.
|
| 144 |
+
组件的子类可以覆盖此逻辑以适应其特定的数据可用性。
|
| 145 |
+
Subclasses can override this logic for their specific data availability.
|
| 146 |
+
"""
|
| 147 |
+
if target_step is None:
|
| 148 |
+
return None
|
| 149 |
+
if not self._all_available_steps:
|
| 150 |
+
return None
|
| 151 |
+
if target_step in self._all_available_steps:
|
| 152 |
+
return target_step
|
| 153 |
+
|
| 154 |
+
# 寻找最接近的步骤 (简单的实现)
|
| 155 |
+
# Find the closest step (simple implementation)
|
| 156 |
+
try:
|
| 157 |
+
closest = min(self._all_available_steps, key=lambda x: abs(x - target_step))
|
| 158 |
+
return closest
|
| 159 |
+
except ValueError: # _all_available_steps为空
|
| 160 |
+
return None
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
@abstractmethod
|
| 164 |
+
def load_data(self) -> None:
|
| 165 |
+
"""
|
| 166 |
+
加载此组件渲染所需的数据。
|
| 167 |
+
Load data required by this component for rendering.
|
| 168 |
+
实现者应使用 self._get_data_asset_path() 来获取数据文件路径。
|
| 169 |
+
Implementers should use self._get_data_asset_path() to get data file paths.
|
| 170 |
+
数据加载后通常存储在实例变量中。
|
| 171 |
+
Loaded data is typically stored in instance variables.
|
| 172 |
+
"""
|
| 173 |
+
pass
|
| 174 |
+
|
| 175 |
+
@abstractmethod
|
| 176 |
+
def render(self) -> None:
|
| 177 |
+
"""
|
| 178 |
+
将组件渲染为Streamlit UI元素。
|
| 179 |
+
Renders the component as Streamlit UI elements.
|
| 180 |
+
应使用 self._current_global_step (可能通过 self._get_closest_available_step 调整) 来显示对应步骤的数据。
|
| 181 |
+
Should use self._current_global_step (possibly adjusted by self._get_closest_available_step)
|
| 182 |
+
to display data for the corresponding step.
|
| 183 |
+
任何可以触发全局步骤更改的交互都应调用 self._request_global_step_change()。
|
| 184 |
+
Any interaction that can trigger a global step change should call self._request_global_step_change().
|
| 185 |
+
"""
|
| 186 |
+
pass
|
| 187 |
+
|
| 188 |
+
@classmethod
|
| 189 |
+
@abstractmethod
|
| 190 |
+
def can_handle_data_types(cls, data_type_names: List[str]) -> bool:
|
| 191 |
+
"""
|
| 192 |
+
类方法:判断此组件类型是否能处理清单中声明的一个或多个数据类型。
|
| 193 |
+
Class method: Determines if this component type can handle one or more data types
|
| 194 |
+
declared in the manifest.
|
| 195 |
+
|
| 196 |
+
Args:
|
| 197 |
+
data_type_names (List[str]): 从数据资产清单中获取的数据类型名称列表。
|
| 198 |
+
A list of data type names from a data asset manifest.
|
| 199 |
+
(通常,主应用会为每个数据资产调用此方法,列表只包含一个元素)
|
| 200 |
+
(Usually, the main app calls this for each data asset, so the list has one element)
|
| 201 |
+
|
| 202 |
+
Returns:
|
| 203 |
+
bool: True 如果此组件可以处理至少一种给定的数据类型。
|
| 204 |
+
True if this component can handle at least one of the given data types.
|
| 205 |
+
"""
|
| 206 |
+
pass
|
| 207 |
+
|
| 208 |
+
@classmethod
|
| 209 |
+
def get_display_name(cls) -> str:
|
| 210 |
+
"""
|
| 211 |
+
类方法:返回此组件类型的用户友好显示名称。
|
| 212 |
+
Class method: Returns a user-friendly display name for this component type.
|
| 213 |
+
"""
|
| 214 |
+
# 简单的实现:将类名从CamelCase转换为带空格的标题
|
| 215 |
+
# Simple implementation: Convert CamelCase class name to space-separated title
|
| 216 |
+
name = cls.__name__
|
| 217 |
+
if name.endswith("Visualizer"):
|
| 218 |
+
name = name[:-len("Visualizer")]
|
| 219 |
+
s1 = VISUALIZER_REGISTRY.get(name, name) # Fallback to class name if not in registry (should not happen with decorator)
|
| 220 |
+
# Add spaces before capital letters (simple version)
|
| 221 |
+
import re
|
| 222 |
+
return re.sub(r'(?<!^)(?=[A-Z])', ' ', s1)
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
def save_component_config(self) -> None:
|
| 226 |
+
"""
|
| 227 |
+
将当前组件的特定配置 (self.config) 保存到其私有存储路径。
|
| 228 |
+
Saves the current component-specific configuration (self.config) to its private storage path.
|
| 229 |
+
"""
|
| 230 |
+
config_file = self.component_private_storage_path / "_component_config.toml"
|
| 231 |
+
try:
|
| 232 |
+
import tomli_w # 确保已安装 Ensure tomli_w is installed
|
| 233 |
+
with open(config_file, "wb") as f:
|
| 234 |
+
tomli_w.dump(self.config, f)
|
| 235 |
+
# st.toast(f"组件 '{self.component_instance_id}' 配置已保存。")
|
| 236 |
+
except Exception as e:
|
| 237 |
+
st.error(f"保存组件 '{self.component_instance_id}' 配置失败: {e}")
|
| 238 |
+
|
| 239 |
+
def load_component_config(self) -> None:
|
| 240 |
+
"""
|
| 241 |
+
从其私有存储路径加载组件的特定配置,并更新 self.config。
|
| 242 |
+
Loads the component-specific configuration from its private storage path and updates self.config.
|
| 243 |
+
"""
|
| 244 |
+
config_file = self.component_private_storage_path / "_component_config.toml"
|
| 245 |
+
if config_file.exists():
|
| 246 |
+
try:
|
| 247 |
+
import tomli # 确保已安装 Ensure tomli is installed
|
| 248 |
+
with open(config_file, "rb") as f:
|
| 249 |
+
loaded_config = tomli.load(f)
|
| 250 |
+
self.config.update(loaded_config) # 合并加载的配置 Merge loaded config
|
| 251 |
+
# st.toast(f"组件 '{self.component_instance_id}' 配置已加载。")
|
| 252 |
+
except Exception as e:
|
| 253 |
+
st.error(f"加载组件 '{self.component_instance_id}' 配置失败: {e}")
|
| 254 |
+
|
src/visualizers/ide_app.py
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
+
import streamlit as st
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import tempfile
|
| 5 |
+
import json
|
| 6 |
+
import shutil
|
| 7 |
+
from typing import Dict, Any, Optional, Type, List
|
| 8 |
+
import tomli # 确保导入 tomli Ensure tomli is imported
|
| 9 |
+
|
| 10 |
+
# 动态确定根目录,以便能够导入src下的模块
|
| 11 |
+
# Dynamically determine root directory to import modules from src
|
| 12 |
+
try:
|
| 13 |
+
current_file_path = Path(__file__).resolve()
|
| 14 |
+
# 假设 ide_app.py 在 src/debugger/ 目录下
|
| 15 |
+
# Assumes ide_app.py is in src/debugger/
|
| 16 |
+
# src_path = current_file_path.parent.parent
|
| 17 |
+
# import sys
|
| 18 |
+
# if str(src_path) not in sys.path:
|
| 19 |
+
# sys.path.insert(0, str(src_path))
|
| 20 |
+
except NameError:
|
| 21 |
+
# project_root = Path(".") # 默认为当前工作目录
|
| 22 |
+
pass # 在某些环境中 __file__ 可能未定义 In some environments __file__ might be undefined
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
from visualizers.base_visualizer import (
|
| 26 |
+
VISUALIZER_REGISTRY,
|
| 27 |
+
get_visualizer_class,
|
| 28 |
+
VisualizationComponent
|
| 29 |
+
)
|
| 30 |
+
# 确保所有组件都被导入和注册
|
| 31 |
+
# Ensure all components are imported and registered
|
| 32 |
+
import visualizers.scalar_dashboard_visualizer
|
| 33 |
+
|
| 34 |
+
except ImportError as e:
|
| 35 |
+
st.error(
|
| 36 |
+
"错误:无法导入可视化组件模块。请确保此IDE应用位于正确的项目结构中,"
|
| 37 |
+
f"并且 `visualizers` 目录及其内容可访问。\n详细信息: {e}"
|
| 38 |
+
"Error: Could not import visualization component modules. Ensure this IDE app is in the correct "
|
| 39 |
+
f"project structure and the `visualizers` directory and its contents are accessible.\nDetails: {e}"
|
| 40 |
+
)
|
| 41 |
+
st.stop()
|
| 42 |
+
|
| 43 |
+
# --- 应用标题和配置 ---
|
| 44 |
+
st.set_page_config(layout="wide", page_title="Flowillower - 组件IDE (Component IDE)")
|
| 45 |
+
st.title("🔬 Flowillower - 可视化组件IDE (Visualization Component IDE)")
|
| 46 |
+
st.markdown("在此环境中独立测试、调试和预览您的可视化组件。")
|
| 47 |
+
|
| 48 |
+
# --- 会话状态初始化 ---
|
| 49 |
+
if "selected_visualizer_type_name" not in st.session_state:
|
| 50 |
+
st.session_state.selected_visualizer_type_name = None
|
| 51 |
+
if "component_instance_id" not in st.session_state:
|
| 52 |
+
st.session_state.component_instance_id = "test_instance_001"
|
| 53 |
+
if "trial_root_path_str" not in st.session_state:
|
| 54 |
+
st.session_state.trial_root_path_str = tempfile.mkdtemp(prefix="flowillower_ide_trial_")
|
| 55 |
+
if "component_specific_config_str" not in st.session_state:
|
| 56 |
+
st.session_state.component_specific_config_str = "{}"
|
| 57 |
+
if "active_visualizer_instance" not in st.session_state:
|
| 58 |
+
st.session_state.active_visualizer_instance = None
|
| 59 |
+
if "generated_data_sources_map" not in st.session_state:
|
| 60 |
+
st.session_state.generated_data_sources_map = None
|
| 61 |
+
if "current_simulated_global_step" not in st.session_state:
|
| 62 |
+
st.session_state.current_simulated_global_step = 0 # 默认值 Default value
|
| 63 |
+
if "all_simulated_steps" not in st.session_state:
|
| 64 |
+
st.session_state.all_simulated_steps = []
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def cleanup_temp_dir(path_str):
|
| 68 |
+
try:
|
| 69 |
+
if path_str and Path(path_str).exists() and "flowillower_ide_trial_" in path_str:
|
| 70 |
+
shutil.rmtree(path_str)
|
| 71 |
+
st.toast(f"临时目录已清理: {path_str}")
|
| 72 |
+
except Exception as e:
|
| 73 |
+
st.warning(f"清理临时目录失败 {path_str}: {e}")
|
| 74 |
+
|
| 75 |
+
with st.sidebar:
|
| 76 |
+
st.header("组件选择与配置")
|
| 77 |
+
|
| 78 |
+
registered_type_names = list(VISUALIZER_REGISTRY.keys())
|
| 79 |
+
if not registered_type_names:
|
| 80 |
+
st.error("错误:没有已注册的可视化组件类型。")
|
| 81 |
+
st.stop()
|
| 82 |
+
|
| 83 |
+
if st.session_state.selected_visualizer_type_name is None and registered_type_names:
|
| 84 |
+
st.session_state.selected_visualizer_type_name = registered_type_names[0]
|
| 85 |
+
|
| 86 |
+
st.session_state.selected_visualizer_type_name = st.selectbox(
|
| 87 |
+
"选择可视化组件类型",
|
| 88 |
+
options=registered_type_names,
|
| 89 |
+
index=registered_type_names.index(st.session_state.selected_visualizer_type_name)
|
| 90 |
+
if st.session_state.selected_visualizer_type_name in registered_type_names else 0,
|
| 91 |
+
help="选择您想要测试的可视化组件。"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
SelectedVisualizerClass: Optional[Type[VisualizationComponent]] = get_visualizer_class(st.session_state.selected_visualizer_type_name)
|
| 95 |
+
|
| 96 |
+
if SelectedVisualizerClass:
|
| 97 |
+
st.caption(f"显示名称: `{SelectedVisualizerClass.get_display_name()}`")
|
| 98 |
+
else:
|
| 99 |
+
st.error(f"无法加载组件类 '{st.session_state.selected_visualizer_type_name}'。")
|
| 100 |
+
st.stop()
|
| 101 |
+
|
| 102 |
+
st.session_state.component_instance_id = st.text_input(
|
| 103 |
+
"组件实例ID", value=st.session_state.component_instance_id
|
| 104 |
+
)
|
| 105 |
+
st.markdown(f"**临时Trial根路径:** `{st.session_state.trial_root_path_str}`")
|
| 106 |
+
|
| 107 |
+
example_data_target_dir = Path(st.session_state.trial_root_path_str) / "example_assets_for_ide"
|
| 108 |
+
|
| 109 |
+
if st.button("生成示例数据"):
|
| 110 |
+
if SelectedVisualizerClass:
|
| 111 |
+
try:
|
| 112 |
+
if example_data_target_dir.exists():
|
| 113 |
+
shutil.rmtree(example_data_target_dir)
|
| 114 |
+
example_data_target_dir.mkdir(parents=True, exist_ok=True)
|
| 115 |
+
|
| 116 |
+
st.session_state.generated_data_sources_map = SelectedVisualizerClass.generate_example_data(
|
| 117 |
+
example_data_path=example_data_target_dir
|
| 118 |
+
)
|
| 119 |
+
st.success(f"'{SelectedVisualizerClass.get_display_name()}' 的示例数据已生成。")
|
| 120 |
+
|
| 121 |
+
temp_all_steps = set()
|
| 122 |
+
if st.session_state.generated_data_sources_map:
|
| 123 |
+
for ds_name, ds_info in st.session_state.generated_data_sources_map.items():
|
| 124 |
+
if "path" in ds_info:
|
| 125 |
+
try:
|
| 126 |
+
# 路径是相对于 trial_root_path 的
|
| 127 |
+
# Path is relative to trial_root_path
|
| 128 |
+
full_path = Path(st.session_state.trial_root_path_str) / ds_info["path"]
|
| 129 |
+
if full_path.exists() and full_path.suffix == ".toml":
|
| 130 |
+
with open(full_path, "rb") as f:
|
| 131 |
+
d = tomli.load(f)
|
| 132 |
+
if "metrics" in d and isinstance(d["metrics"], list):
|
| 133 |
+
for point in d["metrics"]:
|
| 134 |
+
if "global_step" in point:
|
| 135 |
+
temp_all_steps.add(int(point["global_step"]))
|
| 136 |
+
except Exception as e_load:
|
| 137 |
+
st.warning(f"解析示例数据中的步骤时出错 ({ds_info['path']}): {e_load}")
|
| 138 |
+
|
| 139 |
+
st.session_state.all_simulated_steps = sorted(list(temp_all_steps))
|
| 140 |
+
if not st.session_state.all_simulated_steps: # 如果没有解析到步骤,至少放一个0
|
| 141 |
+
st.session_state.all_simulated_steps = [0]
|
| 142 |
+
|
| 143 |
+
# 更新 current_simulated_global_step 为有效值
|
| 144 |
+
# Update current_simulated_global_step to a valid value
|
| 145 |
+
if st.session_state.all_simulated_steps:
|
| 146 |
+
st.session_state.current_simulated_global_step = st.session_state.all_simulated_steps[0]
|
| 147 |
+
else: # 理论上不会到这里,因为上面保证了至少有[0] Theoretically won't reach here as [0] is guaranteed above
|
| 148 |
+
st.session_state.current_simulated_global_step = 0
|
| 149 |
+
st.rerun() # 重新运行以更新UI中的步骤滑块 Rerun to update step slider in UI
|
| 150 |
+
|
| 151 |
+
except Exception as e:
|
| 152 |
+
st.error(f"生成示例数据失败: {e}")
|
| 153 |
+
st.exception(e)
|
| 154 |
+
st.session_state.generated_data_sources_map = None
|
| 155 |
+
else:
|
| 156 |
+
st.warning("请先选择一个组件类型。")
|
| 157 |
+
|
| 158 |
+
st.session_state.component_specific_config_str = st.text_area(
|
| 159 |
+
"组件特定配置 (JSON)", value=st.session_state.component_specific_config_str, height=100
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
if st.button("🚀 实例化组件", type="primary"):
|
| 163 |
+
if SelectedVisualizerClass and st.session_state.component_instance_id and st.session_state.generated_data_sources_map:
|
| 164 |
+
try:
|
| 165 |
+
specific_config = json.loads(st.session_state.component_specific_config_str)
|
| 166 |
+
st.session_state.active_visualizer_instance = SelectedVisualizerClass(
|
| 167 |
+
component_instance_id=st.session_state.component_instance_id,
|
| 168 |
+
trial_root_path=Path(st.session_state.trial_root_path_str),
|
| 169 |
+
data_sources_map=st.session_state.generated_data_sources_map,
|
| 170 |
+
component_specific_config=specific_config
|
| 171 |
+
)
|
| 172 |
+
st.success(f"组件 '{st.session_state.component_instance_id}' 已实例化。")
|
| 173 |
+
active_viz_instance_for_load = st.session_state.active_visualizer_instance
|
| 174 |
+
active_viz_instance_for_load.load_data()
|
| 175 |
+
|
| 176 |
+
# 从组件加载数据后获取实际的all_available_steps
|
| 177 |
+
# Get actual all_available_steps after component loads data
|
| 178 |
+
if hasattr(active_viz_instance_for_load, '_all_available_steps') and active_viz_instance_for_load._all_available_steps:
|
| 179 |
+
st.session_state.all_simulated_steps = active_viz_instance_for_load._all_available_steps
|
| 180 |
+
if st.session_state.current_simulated_global_step not in st.session_state.all_simulated_steps:
|
| 181 |
+
st.session_state.current_simulated_global_step = st.session_state.all_simulated_steps[0]
|
| 182 |
+
|
| 183 |
+
active_viz_instance_for_load.configure_global_step_interaction(
|
| 184 |
+
current_step=st.session_state.current_simulated_global_step,
|
| 185 |
+
all_available_steps=st.session_state.all_simulated_steps,
|
| 186 |
+
on_step_change_request_callback=lambda step: st.session_state.update({"current_simulated_global_step": step})
|
| 187 |
+
)
|
| 188 |
+
st.rerun() # 确保UI更新 Ensure UI updates
|
| 189 |
+
|
| 190 |
+
except Exception as e:
|
| 191 |
+
st.error(f"实例化组件失败: {e}")
|
| 192 |
+
st.exception(e)
|
| 193 |
+
st.session_state.active_visualizer_instance = None
|
| 194 |
+
else:
|
| 195 |
+
st.warning("请先选择组件类型,输入实例ID,并生成示例数据。")
|
| 196 |
+
|
| 197 |
+
st.header("组件预览与交互")
|
| 198 |
+
active_viz_instance: Optional[VisualizationComponent] = st.session_state.active_visualizer_instance
|
| 199 |
+
|
| 200 |
+
if active_viz_instance:
|
| 201 |
+
st.markdown(f"**当前活动组件:** `{active_viz_instance.component_instance_id}` "
|
| 202 |
+
f"(类型: `{SelectedVisualizerClass.get_display_name() if SelectedVisualizerClass else 'N/A'}`)")
|
| 203 |
+
st.markdown(f"**Trial根路径:** `{active_viz_instance.trial_root_path}`")
|
| 204 |
+
|
| 205 |
+
st.markdown("---")
|
| 206 |
+
st.subheader("全局步骤模拟")
|
| 207 |
+
col_step1, col_step2 = st.columns([3,1])
|
| 208 |
+
|
| 209 |
+
# --- 全局步骤模拟 (FIXED RangeError) ---
|
| 210 |
+
# --- Global Step Simulation (FIXED RangeError) ---
|
| 211 |
+
current_step_for_ui = st.session_state.current_simulated_global_step
|
| 212 |
+
all_steps_for_ui = st.session_state.all_simulated_steps
|
| 213 |
+
|
| 214 |
+
# 确保 current_step_for_ui 在 all_steps_for_ui 中 (如果 all_steps_for_ui 非空)
|
| 215 |
+
# Ensure current_step_for_ui is in all_steps_for_ui (if all_steps_for_ui is not empty)
|
| 216 |
+
if all_steps_for_ui and current_step_for_ui not in all_steps_for_ui:
|
| 217 |
+
# 尝试寻找最近的,或者直接用第一个/最后一个
|
| 218 |
+
# Try to find the closest, or just use the first/last
|
| 219 |
+
current_step_for_ui = min(all_steps_for_ui, key=lambda x:abs(x-current_step_for_ui)) if all_steps_for_ui else 0
|
| 220 |
+
# st.session_state.current_simulated_global_step = current_step_for_ui # 避免在渲染中直接修改会话状态 Avoid direct session state modification in render path
|
| 221 |
+
|
| 222 |
+
with col_step1:
|
| 223 |
+
if all_steps_for_ui:
|
| 224 |
+
if len(all_steps_for_ui) == 1:
|
| 225 |
+
st.markdown(f"当前模拟全局步骤: **{all_steps_for_ui[0]}** (只有一步可用)")
|
| 226 |
+
# 如果只有一步,确保会话状态也正确
|
| 227 |
+
# If only one step, ensure session state is also correct
|
| 228 |
+
if st.session_state.current_simulated_global_step != all_steps_for_ui[0]:
|
| 229 |
+
st.session_state.current_simulated_global_step = all_steps_for_ui[0]
|
| 230 |
+
# st.rerun() # 可能会导致循环,让 configure_global_step_interaction 处理 Might cause loop, let configure_global_step_interaction handle
|
| 231 |
+
new_sim_step = all_steps_for_ui[0] # 保持一致 Keep consistent
|
| 232 |
+
else: # 多于一个步骤 More than one step
|
| 233 |
+
new_sim_step = st.select_slider(
|
| 234 |
+
"当前模拟全局步骤",
|
| 235 |
+
options=all_steps_for_ui,
|
| 236 |
+
value=current_step_for_ui, # 使用已验证的值 Use validated value
|
| 237 |
+
key="sim_step_slider_corrected"
|
| 238 |
+
)
|
| 239 |
+
else: # 没有从数据中解析到步骤 No steps parsed from data
|
| 240 |
+
new_sim_step = st.number_input(
|
| 241 |
+
"当前模拟全局步骤 (无可用步骤)",
|
| 242 |
+
value=current_step_for_ui,
|
| 243 |
+
key="sim_step_input_empty_corrected"
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
# 如果用户通过UI更改了步骤,则更新会话状态
|
| 247 |
+
# If user changed step via UI, update session state
|
| 248 |
+
if new_sim_step != st.session_state.current_simulated_global_step:
|
| 249 |
+
st.session_state.current_simulated_global_step = new_sim_step
|
| 250 |
+
# st.rerun() # select_slider/number_input 通常会在值变化时自动 rerun
|
| 251 |
+
|
| 252 |
+
# 每次渲染都更新组件的步骤信息
|
| 253 |
+
# Update component's step info on every render
|
| 254 |
+
active_viz_instance.configure_global_step_interaction(
|
| 255 |
+
current_step=st.session_state.current_simulated_global_step,
|
| 256 |
+
all_available_steps=all_steps_for_ui, # 使用从数据加载的步骤 Use steps loaded from data
|
| 257 |
+
on_step_change_request_callback=lambda step: st.session_state.update({"current_simulated_global_step": step}) # 更新会话状态,Streamlit会自动rerun
|
| 258 |
+
)
|
| 259 |
+
|
| 260 |
+
with col_step2:
|
| 261 |
+
if st.button("🔄 重新加载数据"):
|
| 262 |
+
try:
|
| 263 |
+
active_viz_instance.load_data()
|
| 264 |
+
st.toast("组件数据已重新加载。")
|
| 265 |
+
# 重新加载数据后,可能需要更新 all_simulated_steps
|
| 266 |
+
# After reloading data, all_simulated_steps might need an update
|
| 267 |
+
if hasattr(active_viz_instance, '_all_available_steps') and active_viz_instance._all_available_steps:
|
| 268 |
+
st.session_state.all_simulated_steps = active_viz_instance._all_available_steps
|
| 269 |
+
# 确保当前步骤仍然有效
|
| 270 |
+
# Ensure current step is still valid
|
| 271 |
+
if st.session_state.current_simulated_global_step not in st.session_state.all_simulated_steps:
|
| 272 |
+
st.session_state.current_simulated_global_step = st.session_state.all_simulated_steps[0] if st.session_state.all_simulated_steps else 0
|
| 273 |
+
|
| 274 |
+
active_viz_instance.configure_global_step_interaction( # 再次配置以防步骤列表变化 Reconfigure in case step list changed
|
| 275 |
+
current_step=st.session_state.current_simulated_global_step,
|
| 276 |
+
all_available_steps=st.session_state.all_simulated_steps,
|
| 277 |
+
on_step_change_request_callback=lambda step: st.session_state.update({"current_simulated_global_step": step})
|
| 278 |
+
)
|
| 279 |
+
st.rerun() # 强制刷新UI Force UI refresh
|
| 280 |
+
except Exception as e:
|
| 281 |
+
st.error(f"重新加载数据失败: {e}")
|
| 282 |
+
|
| 283 |
+
st.markdown("---")
|
| 284 |
+
st.subheader("渲染输出")
|
| 285 |
+
try:
|
| 286 |
+
with st.container(border=True):
|
| 287 |
+
active_viz_instance.render()
|
| 288 |
+
except Exception as e:
|
| 289 |
+
st.error(f"渲染组件 '{active_viz_instance.component_instance_id}' 时出错: {e}")
|
| 290 |
+
st.exception(e)
|
| 291 |
+
else:
|
| 292 |
+
st.info("请在侧边栏中选择一个组件类型,生成示例数据,然后点击“实例化组件”以开始调试。")
|
| 293 |
+
|
| 294 |
+
st.sidebar.markdown("---")
|
| 295 |
+
st.sidebar.caption(f"IDE 会话临时路径: {st.session_state.trial_root_path_str}")
|
| 296 |
+
if st.sidebar.button("清理当前会话的临时Trial目录"):
|
| 297 |
+
cleanup_temp_dir(st.session_state.trial_root_path_str)
|
| 298 |
+
st.session_state.trial_root_path_str = tempfile.mkdtemp(prefix="flowillower_ide_trial_")
|
| 299 |
+
st.session_state.active_visualizer_instance = None
|
| 300 |
+
st.session_state.generated_data_sources_map = None
|
| 301 |
+
st.session_state.all_simulated_steps = []
|
| 302 |
+
st.session_state.current_simulated_global_step = 0
|
| 303 |
+
st.rerun()
|
src/visualizers/scalar_dashboard_visualizer.py
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# src/visualizers/scalar_dashboard_visualizer.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import plotly.graph_objects as go
|
| 5 |
+
import plotly.express as px
|
| 6 |
+
import tomli
|
| 7 |
+
import tomli_w
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
from typing import Dict, Any, Optional, List, Tuple
|
| 10 |
+
|
| 11 |
+
from .base_visualizer import VisualizationComponent, register_visualizer
|
| 12 |
+
|
| 13 |
+
@register_visualizer(name="scalar_metrics_dashboard")
|
| 14 |
+
class ScalarMetricsDashboardVisualizer(VisualizationComponent):
|
| 15 |
+
"""
|
| 16 |
+
一个用于显示多个标量指标(每个指标可能包含多个track)的仪表盘组件。
|
| 17 |
+
它会为每个指标在选定的global_step显示st.metric摘要,并绘制一个Plotly折线图。
|
| 18 |
+
A dashboard component to display multiple scalar metrics, each potentially with multiple tracks.
|
| 19 |
+
It shows st.metric summaries for the selected global_step and a Plotly line chart for each metric.
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
_raw_data: Optional[pd.DataFrame] = None
|
| 23 |
+
_processed_metrics_data: Dict[str, pd.DataFrame] = None
|
| 24 |
+
|
| 25 |
+
DEFAULT_CHARTS_PER_ROW = 2
|
| 26 |
+
|
| 27 |
+
def __init__(self,
|
| 28 |
+
component_instance_id: str,
|
| 29 |
+
trial_root_path: Path,
|
| 30 |
+
data_sources_map: Dict[str, Dict[str, Any]],
|
| 31 |
+
component_specific_config: Dict[str, Any] = None):
|
| 32 |
+
super().__init__(component_instance_id, trial_root_path, data_sources_map, component_specific_config)
|
| 33 |
+
self._raw_data = None
|
| 34 |
+
self._processed_metrics_data = {}
|
| 35 |
+
self.load_component_config()
|
| 36 |
+
|
| 37 |
+
self.charts_per_row = self.config.get("charts_per_row", self.DEFAULT_CHARTS_PER_ROW)
|
| 38 |
+
self.chart_height = self.config.get("chart_height", 400)
|
| 39 |
+
|
| 40 |
+
@classmethod
|
| 41 |
+
def can_handle_data_types(cls, data_type_names: List[str]) -> bool:
|
| 42 |
+
return "multi_metric_multi_track_scalars" in data_type_names
|
| 43 |
+
|
| 44 |
+
@classmethod
|
| 45 |
+
def generate_example_data(cls, example_data_path: Path, # This path is like .../trial_root/example_assets_for_ide
|
| 46 |
+
data_sources_config: Optional[Dict[str, Dict[str, Any]]] = None
|
| 47 |
+
) -> Dict[str, Dict[str, Any]]:
|
| 48 |
+
example_data_path.mkdir(parents=True, exist_ok=True)
|
| 49 |
+
metrics_file_name = "example_scalar_metrics.toml"
|
| 50 |
+
# 文件实际写入位置: example_data_path / metrics_file_name
|
| 51 |
+
# Actual file write location: example_data_path / metrics_file_name
|
| 52 |
+
metrics_file_full_path = example_data_path / metrics_file_name
|
| 53 |
+
|
| 54 |
+
data_points = []
|
| 55 |
+
for step in range(20):
|
| 56 |
+
data_points.append({"global_step": step, "track": "train", "loss": 1.0 / (step + 1) + 0.1, "accuracy": 0.6 + step * 0.015})
|
| 57 |
+
data_points.append({"global_step": step, "track": "validation", "loss": 1.0 / (step + 1) + 0.2, "accuracy": 0.55 + step * 0.01})
|
| 58 |
+
if step % 5 == 0:
|
| 59 |
+
data_points.append({"global_step": step, "track": "system", "learning_rate": 0.001 * (0.9**step)})
|
| 60 |
+
|
| 61 |
+
try:
|
| 62 |
+
with open(metrics_file_full_path, "wb") as f:
|
| 63 |
+
tomli_w.dump({"metrics": data_points}, f)
|
| 64 |
+
except Exception as e:
|
| 65 |
+
st.error(f"生成示例数据失败: {e}")
|
| 66 |
+
raise
|
| 67 |
+
|
| 68 |
+
# *** 修正点 (FIXED POINT) ***
|
| 69 |
+
# 返回的路径应该是相对于 trial_root_path 的。
|
| 70 |
+
# The returned path should be relative to trial_root_path.
|
| 71 |
+
# example_data_path 的父目录是 trial_root_path。
|
| 72 |
+
# The parent of example_data_path is trial_root_path in the IDE's context.
|
| 73 |
+
# 所以,相对路径是 example_data_path 的名称(即 "example_assets_for_ide")加上文件名。
|
| 74 |
+
# So, the relative path is the name of example_data_path (i.e., "example_assets_for_ide") plus the filename.
|
| 75 |
+
# path_relative_to_trial_root = example_data_path.name / metrics_file_name
|
| 76 |
+
# 更稳健的方式是,如果 example_data_path 是绝对路径,而我们需要相对于 trial_root_path 的路径,
|
| 77 |
+
# 并且我们知道 trial_root_path 是 example_data_path 的父目录(或更早的祖先)。
|
| 78 |
+
# A more robust way, if example_data_path is absolute, and we need path relative to trial_root_path,
|
| 79 |
+
# and we know trial_root_path is a parent (or earlier ancestor) of example_data_path.
|
| 80 |
+
# 在IDE的上下文中,example_data_path = trial_root_path / "example_assets_for_ide"
|
| 81 |
+
# In the IDE's context, example_data_path = trial_root_path / "example_assets_for_ide"
|
| 82 |
+
# 所以,相对于 trial_root_path 的路径就是 "example_assets_for_ide" / metrics_file_name
|
| 83 |
+
# So, path relative to trial_root_path is "example_assets_for_ide" / metrics_file_name
|
| 84 |
+
|
| 85 |
+
path_for_manifest = Path(example_data_path.name) / metrics_file_name
|
| 86 |
+
|
| 87 |
+
return {
|
| 88 |
+
"main_metrics_source": {
|
| 89 |
+
"asset_id": "example_metrics_dashboard_data",
|
| 90 |
+
"data_type": "multi_metric_multi_track_scalars",
|
| 91 |
+
"path": str(path_for_manifest), # 使用修正后的相对路径 Use the corrected relative path
|
| 92 |
+
"display_name": "示例综合指标数据 (Example Comprehensive Metrics)"
|
| 93 |
+
}
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
def load_data(self) -> None:
|
| 97 |
+
data_asset_path = self._get_data_asset_path("main_metrics_source")
|
| 98 |
+
if data_asset_path is None or not data_asset_path.exists():
|
| 99 |
+
st.warning(f"组件 {self.component_instance_id}: 未找到数据源 'main_metrics_source' 或路径无效: {data_asset_path}")
|
| 100 |
+
self._raw_data = pd.DataFrame()
|
| 101 |
+
self._processed_metrics_data = {}
|
| 102 |
+
return
|
| 103 |
+
|
| 104 |
+
try:
|
| 105 |
+
with open(data_asset_path, "rb") as f:
|
| 106 |
+
data = tomli.load(f)
|
| 107 |
+
|
| 108 |
+
metrics_list = data.get("metrics", [])
|
| 109 |
+
if not metrics_list:
|
| 110 |
+
self._raw_data = pd.DataFrame()
|
| 111 |
+
else:
|
| 112 |
+
self._raw_data = pd.DataFrame(metrics_list)
|
| 113 |
+
|
| 114 |
+
self._process_raw_data()
|
| 115 |
+
|
| 116 |
+
except Exception as e:
|
| 117 |
+
st.error(f"组件 {self.component_instance_id}: 加载数据 '{data_asset_path}' 失败: {e}")
|
| 118 |
+
self._raw_data = pd.DataFrame()
|
| 119 |
+
self._processed_metrics_data = {}
|
| 120 |
+
|
| 121 |
+
def _process_raw_data(self) -> None:
|
| 122 |
+
self._processed_metrics_data = {}
|
| 123 |
+
if self._raw_data is None or self._raw_data.empty:
|
| 124 |
+
return
|
| 125 |
+
|
| 126 |
+
if "global_step" not in self._raw_data.columns:
|
| 127 |
+
st.warning(f"组件 {self.component_instance_id}: 数据缺少 'global_step' 列。")
|
| 128 |
+
return
|
| 129 |
+
|
| 130 |
+
potential_metric_cols = [
|
| 131 |
+
col for col in self._raw_data.columns if col not in ["global_step", "track"]
|
| 132 |
+
]
|
| 133 |
+
|
| 134 |
+
for metric_col_name in potential_metric_cols:
|
| 135 |
+
metric_df_cols = ["global_step", metric_col_name]
|
| 136 |
+
if "track" in self._raw_data.columns:
|
| 137 |
+
metric_df_cols.append("track")
|
| 138 |
+
|
| 139 |
+
# 确保所有需要的列都存在于self._raw_data中
|
| 140 |
+
# Ensure all needed columns exist in self._raw_data
|
| 141 |
+
if not all(col in self._raw_data.columns for col in metric_df_cols):
|
| 142 |
+
# st.warning(f"组件 {self.component_instance_id}: 指标 '{metric_col_name}' 的原始数据缺少某些列: {metric_df_cols}")
|
| 143 |
+
continue
|
| 144 |
+
|
| 145 |
+
metric_df = self._raw_data[metric_df_cols].copy()
|
| 146 |
+
|
| 147 |
+
if "track" not in metric_df.columns: # 如果原始数据就没有track列
|
| 148 |
+
metric_df["track"] = "default"
|
| 149 |
+
|
| 150 |
+
metric_df.rename(columns={metric_col_name: "value"}, inplace=True)
|
| 151 |
+
metric_df["value"] = pd.to_numeric(metric_df["value"], errors='coerce')
|
| 152 |
+
metric_df["global_step"] = pd.to_numeric(metric_df["global_step"], errors='coerce')
|
| 153 |
+
metric_df.dropna(subset=["value", "global_step"], inplace=True)
|
| 154 |
+
|
| 155 |
+
if not metric_df.empty:
|
| 156 |
+
metric_df = metric_df.sort_values(by=["track", "global_step"]).reset_index(drop=True)
|
| 157 |
+
self._processed_metrics_data[metric_col_name] = metric_df
|
| 158 |
+
|
| 159 |
+
if self._all_available_steps is None and not self._raw_data.empty and "global_step" in self._raw_data.columns:
|
| 160 |
+
valid_steps = pd.to_numeric(self._raw_data["global_step"], errors='coerce').dropna().astype(int).unique()
|
| 161 |
+
self._all_available_steps = sorted(list(valid_steps))
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def _render_metric_summary(self, metric_name: str, metric_df: pd.DataFrame, target_step: Optional[int]):
|
| 165 |
+
if target_step is None and self._all_available_steps: # 如果target_step是None,默认用最后一个step
|
| 166 |
+
target_step = self._all_available_steps[-1]
|
| 167 |
+
elif target_step is None or not self._all_available_steps:
|
| 168 |
+
st.metric(label=f"{metric_name}", value="无可用步骤", delta=None)
|
| 169 |
+
return
|
| 170 |
+
|
| 171 |
+
actual_display_step = self._get_closest_available_step(target_step)
|
| 172 |
+
if actual_display_step is None:
|
| 173 |
+
st.metric(label=f"{metric_name}", value="无数据", delta=None)
|
| 174 |
+
return
|
| 175 |
+
|
| 176 |
+
all_tracks = sorted(list(metric_df["track"].unique()))
|
| 177 |
+
num_tracks = len(all_tracks)
|
| 178 |
+
if num_tracks == 0: return
|
| 179 |
+
|
| 180 |
+
cols = st.columns(num_tracks) if num_tracks > 1 else [st.container()]
|
| 181 |
+
|
| 182 |
+
for idx, track_name in enumerate(all_tracks):
|
| 183 |
+
with cols[idx if num_tracks > 1 else 0]:
|
| 184 |
+
track_data = metric_df[metric_df["track"] == track_name]
|
| 185 |
+
if track_data.empty:
|
| 186 |
+
st.metric(label=f"{metric_name} ({track_name})", value="无数据", delta=None)
|
| 187 |
+
continue
|
| 188 |
+
|
| 189 |
+
current_value = None
|
| 190 |
+
delta_value = None
|
| 191 |
+
step_for_current_value = actual_display_step
|
| 192 |
+
|
| 193 |
+
current_step_data = track_data[track_data["global_step"] == step_for_current_value]
|
| 194 |
+
if current_step_data.empty:
|
| 195 |
+
prev_steps_for_track = track_data[track_data["global_step"] <= step_for_current_value]["global_step"]
|
| 196 |
+
if not prev_steps_for_track.empty:
|
| 197 |
+
step_for_current_value = prev_steps_for_track.max()
|
| 198 |
+
current_step_data = track_data[track_data["global_step"] == step_for_current_value]
|
| 199 |
+
|
| 200 |
+
if not current_step_data.empty:
|
| 201 |
+
current_value = current_step_data["value"].iloc[0]
|
| 202 |
+
prev_steps_for_delta = track_data[track_data["global_step"] < step_for_current_value]["global_step"]
|
| 203 |
+
if not prev_steps_for_delta.empty:
|
| 204 |
+
step_for_prev_value = prev_steps_for_delta.max()
|
| 205 |
+
prev_value_data = track_data[track_data["global_step"] == step_for_prev_value]
|
| 206 |
+
if not prev_value_data.empty:
|
| 207 |
+
prev_value = prev_value_data["value"].iloc[0]
|
| 208 |
+
delta_value = current_value - prev_value
|
| 209 |
+
|
| 210 |
+
metric_label = f"{metric_name} ({track_name})"
|
| 211 |
+
if step_for_current_value != target_step and current_value is not None: # 仅当找到的值的步骤与目标步骤不同时才显示
|
| 212 |
+
metric_label += f" @S{int(step_for_current_value)}"
|
| 213 |
+
|
| 214 |
+
st.metric(
|
| 215 |
+
label=metric_label,
|
| 216 |
+
value=f"{current_value:.4f}" if current_value is not None else "无数据",
|
| 217 |
+
delta=f"{delta_value:.4f}" if delta_value is not None and current_value is not None else None,
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
def _render_plotly_chart(self, metric_name: str, metric_df: pd.DataFrame, chart_key: str):
|
| 221 |
+
fig = go.Figure()
|
| 222 |
+
all_tracks = sorted(list(metric_df["track"].unique()))
|
| 223 |
+
colors = px.colors.qualitative.Plotly
|
| 224 |
+
|
| 225 |
+
for i, track_name in enumerate(all_tracks):
|
| 226 |
+
track_data = metric_df[metric_df["track"] == track_name]
|
| 227 |
+
fig.add_trace(go.Scatter(
|
| 228 |
+
x=track_data["global_step"],
|
| 229 |
+
y=track_data["value"],
|
| 230 |
+
mode="lines+markers",
|
| 231 |
+
name=track_name,
|
| 232 |
+
line=dict(color=colors[i % len(colors)]),
|
| 233 |
+
marker=dict(size=6, color=colors[i % len(colors)], line=dict(width=1, color="white")),
|
| 234 |
+
customdata=track_data[["global_step", "value", "track"]],
|
| 235 |
+
hovertemplate="<b>%{customdata[2]}</b><br>Step: %{customdata[0]}<br>Value: %{customdata[1]:.4f}<extra></extra>"
|
| 236 |
+
))
|
| 237 |
+
|
| 238 |
+
current_step_to_highlight = self._get_closest_available_step(self._current_global_step)
|
| 239 |
+
if current_step_to_highlight is not None:
|
| 240 |
+
fig.add_vline(
|
| 241 |
+
x=current_step_to_highlight,
|
| 242 |
+
line_width=1.5, line_dash="solid", line_color="firebrick", opacity=0.7
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
fig.update_layout(
|
| 246 |
+
xaxis_title="Global Step",
|
| 247 |
+
yaxis_title=metric_name,
|
| 248 |
+
height=self.chart_height,
|
| 249 |
+
margin=dict(l=10, r=10, t=30, b=10),
|
| 250 |
+
showlegend=len(all_tracks) > 1,
|
| 251 |
+
hovermode="closest",
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
event_data = st.plotly_chart(
|
| 255 |
+
fig,
|
| 256 |
+
use_container_width=True,
|
| 257 |
+
key=chart_key,
|
| 258 |
+
on_select="rerun"
|
| 259 |
+
)
|
| 260 |
+
|
| 261 |
+
current_selection = st.session_state.get(chart_key, {}).get("selection")
|
| 262 |
+
if current_selection and current_selection.get("points"):
|
| 263 |
+
clicked_point = current_selection["points"][0]
|
| 264 |
+
if "customdata" in clicked_point and isinstance(clicked_point["customdata"], list):
|
| 265 |
+
clicked_global_step = int(clicked_point["customdata"][0])
|
| 266 |
+
last_clicked_step_key = f"{chart_key}_last_clicked_step"
|
| 267 |
+
if st.session_state.get(last_clicked_step_key) != clicked_global_step:
|
| 268 |
+
st.session_state[last_clicked_step_key] = clicked_global_step
|
| 269 |
+
self._request_global_step_change(clicked_global_step)
|
| 270 |
+
elif current_selection and not current_selection.get("points"):
|
| 271 |
+
st.session_state[f"{chart_key}_last_clicked_step"] = None
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
def render_settings_ui(self):
|
| 275 |
+
st.markdown("##### 组件设置 (Component Settings)")
|
| 276 |
+
new_charts_per_row = st.slider(
|
| 277 |
+
"每行图表数 (Charts per row)", 1, 4, self.charts_per_row,
|
| 278 |
+
key=f"{self.component_instance_id}_charts_per_row"
|
| 279 |
+
)
|
| 280 |
+
if new_charts_per_row != self.charts_per_row:
|
| 281 |
+
self.charts_per_row = new_charts_per_row
|
| 282 |
+
self.config["charts_per_row"] = new_charts_per_row
|
| 283 |
+
self.save_component_config()
|
| 284 |
+
st.rerun()
|
| 285 |
+
|
| 286 |
+
new_chart_height = st.number_input(
|
| 287 |
+
"图表高度 (Chart Height)", min_value=200, max_value=1000, step=50,
|
| 288 |
+
value=self.chart_height,
|
| 289 |
+
key=f"{self.component_instance_id}_chart_height"
|
| 290 |
+
)
|
| 291 |
+
if new_chart_height != self.chart_height:
|
| 292 |
+
self.chart_height = new_chart_height
|
| 293 |
+
self.config["chart_height"] = new_chart_height
|
| 294 |
+
self.save_component_config()
|
| 295 |
+
st.rerun()
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
def render(self) -> None:
|
| 299 |
+
if self._processed_metrics_data is None or not self._processed_metrics_data:
|
| 300 |
+
if self._raw_data is None: # 尝试加载一次
|
| 301 |
+
self.load_data()
|
| 302 |
+
# 再次检查
|
| 303 |
+
if self._processed_metrics_data is None or not self._processed_metrics_data:
|
| 304 |
+
st.info(f"组件 {self.component_instance_id}: 没有处理好的指标数据可供显示。请先加载数据或生成示例数据。")
|
| 305 |
+
st.caption("如果已生成示例数据但仍看到此消息,请检查数据路径和格式是否正确。If example data was generated but you still see this, check data paths and format.")
|
| 306 |
+
return
|
| 307 |
+
|
| 308 |
+
with st.expander("图表显示设置 (Chart Display Settings)", expanded=False):
|
| 309 |
+
self.render_settings_ui()
|
| 310 |
+
|
| 311 |
+
metric_names_to_display = sorted(list(self._processed_metrics_data.keys()))
|
| 312 |
+
if not metric_names_to_display:
|
| 313 |
+
st.caption("没有可显示的指标。No metrics to display.")
|
| 314 |
+
return
|
| 315 |
+
|
| 316 |
+
num_metrics = len(metric_names_to_display)
|
| 317 |
+
cols_per_row = self.charts_per_row
|
| 318 |
+
|
| 319 |
+
for i in range(0, num_metrics, cols_per_row):
|
| 320 |
+
metric_chunk = metric_names_to_display[i : i + cols_per_row]
|
| 321 |
+
actual_cols_for_this_row = len(metric_chunk)
|
| 322 |
+
chart_cols = st.columns(actual_cols_for_this_row)
|
| 323 |
+
|
| 324 |
+
for j, metric_name in enumerate(metric_chunk):
|
| 325 |
+
with chart_cols[j]:
|
| 326 |
+
metric_df = self._processed_metrics_data[metric_name]
|
| 327 |
+
|
| 328 |
+
with st.container(border=True, height=self.chart_height + 200): # 增加容器高度以容纳metric和图表
|
| 329 |
+
st.subheader(metric_name)
|
| 330 |
+
self._render_metric_summary(metric_name, metric_df, self._current_global_step)
|
| 331 |
+
st.markdown("---")
|
| 332 |
+
chart_key = f"plotly_{self.component_instance_id}_{metric_name}"
|
| 333 |
+
self._render_plotly_chart(metric_name, metric_df, chart_key)
|