MCPcopy Create free account
hub / github.com/Tong89/smartNode / compare

Method compare

backend/store.py:296–402  ·  view source on GitHub ↗

对两个场景的决策指标进行对比,返回差值分析报告。 Parameters ---------- name_a: 场景 A 的名称(通常为基线)。 name_b: 场景 B 的名称(通常为待对比场景)。 Returns ------- dict :: { "scenario_a": {"name": ..., "saved_at": ...},

(self, name_a: str, name_b: str)

Source from the content-addressed store, hash-verified

294 return ScenarioManager.load(engine, scene_dict)
295
296 def compare(self, name_a: str, name_b: str) -> Dict[str, Any]:
297 """对两个场景的决策指标进行对比,返回差值分析报告。
298
299 Parameters
300 ----------
301 name_a:
302 场景 A 的名称(通常为基线)。
303 name_b:
304 场景 B 的名称(通常为待对比场景)。
305
306 Returns
307 -------
308 dict
309 ::
310
311 {
312 "scenario_a": {"name": ..., "saved_at": ...},
313 "scenario_b": {"name": ..., "saved_at": ...},
314 "resource_diff": {
315 "gs_count": {"a": int, "b": int, "delta": int},
316 "leo_count": {"a": int, "b": int, "delta": int},
317 },
318 "metrics": {
319 "<field>": {
320 "label": str,
321 "a": float, "b": float,
322 "delta": float, # b - a
323 "delta_pct": float | None # (b-a)/a*100, None if a==0
324 },
325 ...
326 },
327 "summary": str # 人类可读的简短结论
328 }
329
330 Raises
331 ------
332 KeyError
333 任一场景不存在时抛出。
334 """
335 rec_a = self.load_scenario(name_a)
336 rec_b = self.load_scenario(name_b)
337 if rec_a is None:
338 raise KeyError(f"场景 '{name_a}' 不存在")
339 if rec_b is None:
340 raise KeyError(f"场景 '{name_b}' 不存在")
341
342 metrics_a = rec_a.get("run_stats", {}).get("decision_metrics", {})
343 metrics_b = rec_b.get("run_stats", {}).get("decision_metrics", {})
344
345 # 逐指标计算差值
346 metrics_report: Dict[str, Any] = {}
347 for field in COMPARE_METRIC_FIELDS:
348 val_a = float(metrics_a.get(field, 0.0))
349 val_b = float(metrics_b.get(field, 0.0))
350 delta = round(val_b - val_a, 6)
351 delta_pct: Optional[float] = None
352 if val_a != 0.0:
353 delta_pct = round((val_b - val_a) / abs(val_a) * 100, 2)

Callers 6

scenario_compareFunction · 0.80
test_compare_basicMethod · 0.80

Calls 2

load_scenarioMethod · 0.95
getMethod · 0.45

Tested by 5

test_compare_basicMethod · 0.64