|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| import json
|
| import os
|
| import sys
|
| import numpy as np
|
| from scipy.spatial.distance import cdist
|
| import pandas as pd
|
| from fastdtw import fastdtw
|
| from scipy.spatial.distance import euclidean
|
| import re
|
| from collections import defaultdict
|
| from tqdm import tqdm
|
|
|
|
|
|
|
| PAIR_RESAMPLE_METHOD = 'bootstrap'
|
|
|
| SAVE_DEDUP_JSON = True
|
|
|
|
|
|
|
| CAT_SHEET_LEVEL_MODE2 = 'full'
|
|
|
|
|
| def _sanitize_sheet_name(name: str) -> str:
|
| """清理Excel工作表名,最长31字符,去除非法字符。"""
|
| if not isinstance(name, str):
|
| name = str(name)
|
|
|
| name = re.sub(r'[\\/*?\[\]:]', '_', name)
|
|
|
| if len(name) > 31:
|
| name = name[:31]
|
|
|
| return name or "Sheet"
|
|
|
| def _parse_cli_with_flags(argv):
|
| """解析命令行,支持可选的 --segcat/--seg-cat/--seg-category-json。
|
| 返回 (positional_args, options);positional_args 不包含这些flag。
|
| 允许两种形式:--segcat=path 或 --segcat path
|
| """
|
| options = {}
|
| positional = []
|
| i = 1
|
| while i < len(argv):
|
| arg = argv[i]
|
| if isinstance(arg, str) and arg.startswith('--'):
|
| key = None
|
| if arg.startswith('--segcat='):
|
| options['segcat'] = arg.split('=', 1)[1].strip()
|
| elif arg in ('--segcat', '--seg-cat', '--seg-category-json'):
|
| if i + 1 < len(argv):
|
| options['segcat'] = argv[i+1]
|
| i += 1
|
| else:
|
|
|
| positional.append(arg)
|
| i += 1
|
| continue
|
| positional.append(arg)
|
| i += 1
|
| return positional, options
|
|
|
| def load_seg_categories(json_path):
|
| """加载多标签单句分类JSON,返回 {(episode_id:str, seg_idx:int): set(categories)} 映射。
|
| 兼容多种字段命名:
|
| - episode: 'episode_id' | 'episodeId' | 'ep_id'
|
| - segment index: 'seg_idx' | 'segment_index' | 'sentence_index' | 'instruction_index'
|
| - categories: 'categories' | 'labels' | 'label' | 'tags'
|
| 若 labels 为字典,则取 value 为 True 的键或转为列表。
|
| """
|
| mapping = {}
|
| if not json_path or not os.path.exists(json_path):
|
| return mapping
|
| try:
|
| with open(json_path, 'r', encoding='utf-8') as f:
|
| data = json.load(f)
|
| except Exception as e:
|
| print(f"Warning: Failed to load seg-category JSON: {e}")
|
| return mapping
|
|
|
|
|
| items = None
|
| if isinstance(data, dict) and 'data' in data and isinstance(data['data'], list):
|
| items = data['data']
|
| elif isinstance(data, list):
|
| items = data
|
| else:
|
|
|
| items = []
|
|
|
| def _norm_ep(v):
|
| try:
|
| return str(v)
|
| except Exception:
|
| return None
|
|
|
| for it in items:
|
| if not isinstance(it, dict):
|
| continue
|
|
|
| ep = it.get('episode_id')
|
| if ep is None:
|
| ep = it.get('episodeId', it.get('ep_id'))
|
| ep = _norm_ep(ep)
|
|
|
| seg_idx = (
|
| it.get('seg_idx') if 'seg_idx' in it else (
|
| it.get('segment_index', it.get('sentence_index', it.get('instruction_index', it.get('sentence_id'))))
|
| )
|
| )
|
| try:
|
| seg_idx = int(seg_idx) if seg_idx is not None else None
|
| except Exception:
|
| seg_idx = None
|
|
|
|
|
| cats = None
|
| if 'categories' in it:
|
| cats = it['categories']
|
| elif 'tags' in it:
|
| cats = it['tags']
|
| elif 'label' in it:
|
| cats = it['label']
|
| elif 'labels' in it:
|
| labs = it['labels']
|
| if isinstance(labs, dict):
|
| cats = [k for k, v in labs.items() if v]
|
| else:
|
| cats = labs
|
|
|
| if cats is None:
|
| cats = []
|
| elif isinstance(cats, str):
|
| cats = [cats]
|
| elif not isinstance(cats, (list, tuple, set)):
|
| cats = [str(cats)]
|
| cats = [str(c) for c in cats if c is not None]
|
|
|
| if ep is None or seg_idx is None:
|
| continue
|
| key = (ep, seg_idx)
|
| mapping.setdefault(key, set()).update(cats)
|
|
|
| return mapping
|
|
|
|
|
| def _filter_valid(values):
|
| """过滤掉None/NaN/Inf,返回numpy数组。"""
|
| valid = []
|
| for v in values:
|
| if v is None:
|
| continue
|
| try:
|
| vf = float(v)
|
| except Exception:
|
| continue
|
| if not (np.isnan(vf) or np.isinf(vf)):
|
| valid.append(vf)
|
| return np.array(valid, dtype=float)
|
|
|
| def bootstrap_stats(values, n_boot=2000, ci=0.95, random_state=42):
|
| """对给定样本做非参数Bootstrap,返回点估计、样本标准差、Bootstrap标准误、置信区间。
|
|
|
| 参数:
|
| values: 可迭代的数值(将自动清洗None/NaN/Inf)
|
| n_boot: bootstrap次数(默认2000)
|
| ci: 置信水平(默认0.95)
|
| random_state: 随机种子
|
| 返回:
|
| dict: { 'n','mean','std','se_boot','ci_low','ci_high','ci_level' }
|
| """
|
| x = _filter_valid(values)
|
| if x.size == 0:
|
| return {
|
| 'n': 0, 'mean': None, 'std': None, 'se_boot': None,
|
| 'ci_low': None, 'ci_high': None, 'ci_level': ci
|
| }
|
| rng = np.random.default_rng(random_state)
|
| point_mean = float(np.mean(x))
|
| sample_std = float(np.std(x, ddof=1)) if x.size > 1 else 0.0
|
| idx = rng.integers(0, x.size, size=(n_boot, x.size))
|
| boot_means = np.mean(x[idx], axis=1)
|
| alpha = (1 - ci) / 2
|
| ci_low, ci_high = np.quantile(boot_means, [alpha, 1 - alpha])
|
| se_boot = float(np.std(boot_means, ddof=1))
|
| return {
|
| 'n': int(x.size),
|
| 'mean': point_mean,
|
| 'std': sample_std,
|
| 'se_boot': se_boot,
|
| 'ci_low': float(ci_low),
|
| 'ci_high': float(ci_high),
|
| 'ci_level': ci
|
| }
|
|
|
| def bootstrap_one_sample_p(values, null_value, n_boot=2000, random_state=42):
|
| """一样本双侧Bootstrap检验p值: H0: E[X] = null_value;HA: E[X] != null_value"""
|
| x = _filter_valid(values)
|
| if x.size == 0 or null_value is None:
|
| return None
|
| rng = np.random.default_rng(random_state)
|
| idx = rng.integers(0, x.size, size=(n_boot, x.size))
|
| boot_means = np.mean(x[idx], axis=1)
|
| p_left = np.mean(boot_means <= null_value)
|
| p_right = np.mean(boot_means >= null_value)
|
| p_two = 2 * min(p_left, p_right)
|
| return float(min(1.0, max(0.0, p_two)))
|
|
|
| def paired_signflip_p(diffs, n_perm=2000, random_state=42):
|
| """成对样本的符号翻转检验(双侧)p值(按分布相对 0 的双尾概率)。
|
| 输入 diffs 为每个 episode 的成对差 A-B(已清洗)。
|
| H0: E[diff] = 0;HA: != 0。
|
| p = 2 * min(Pr(Δ* > 0), Pr(Δ* < 0)),Δ* 为符号翻转生成的均值分布。
|
| """
|
| d = _filter_valid(diffs)
|
| if d.size == 0:
|
| return None
|
| rng = np.random.default_rng(random_state)
|
|
|
| signs = rng.integers(0, 2, size=(n_perm, d.size)) * 2 - 1
|
| perm_means = np.mean(signs * d, axis=1)
|
| p_left = np.mean(perm_means < 0)
|
| p_right = np.mean(perm_means > 0)
|
| p_two = 2 * min(p_left, p_right)
|
| return float(min(1.0, max(0.0, p_two)))
|
|
|
| def paired_bootstrap_p(diffs, n_boot=2000, random_state=42):
|
| """成对样本的 Bootstrap p 值(双侧),对差分样本有放回重采样,取均值分布 Δ*。
|
| p = 2 * min(Pr(Δ* > 0), Pr(Δ* < 0))。
|
| """
|
| d = _filter_valid(diffs)
|
| if d.size == 0:
|
| return None
|
| rng = np.random.default_rng(random_state)
|
| idx = rng.integers(0, d.size, size=(n_boot, d.size))
|
| boot_means = np.mean(d[idx], axis=1)
|
| p_left = np.mean(boot_means < 0)
|
| p_right = np.mean(boot_means > 0)
|
| p_two = 2 * min(p_left, p_right)
|
| return float(min(1.0, max(0.0, p_two)))
|
|
|
| def paired_permutation_p(a_vals, b_vals, n_perm=2000, random_state=42):
|
| """配对置换检验(双侧):每对中以 0.5 概率交换 A 与 B,得到均值差分布 Δ*。
|
| p = 2 * min(Pr(Δ* > 0), Pr(Δ* < 0))。
|
| 注意:在“差值法”视角下,此检验与符号翻转等价;此处显式以对内交换实现。
|
| """
|
| a = _filter_valid(a_vals)
|
| b = _filter_valid(b_vals)
|
| if a.size == 0 or b.size == 0 or a.size != b.size:
|
| return None
|
| rng = np.random.default_rng(random_state)
|
| n = a.size
|
| perm_means = []
|
| for _ in range(n_perm):
|
| swap = rng.integers(0, 2, size=n)
|
| a_perm = np.where(swap == 1, b, a)
|
| b_perm = np.where(swap == 1, a, b)
|
| perm_means.append(float(np.mean(a_perm - b_perm)))
|
| perm_means = np.array(perm_means, dtype=float)
|
| p_left = np.mean(perm_means < 0)
|
| p_right = np.mean(perm_means > 0)
|
| p_two = 2 * min(p_left, p_right)
|
| return float(min(1.0, max(0.0, p_two)))
|
|
|
| def safe_nanmean(values):
|
| """安全的计算平均值,过滤掉None、NaN和无穷大值"""
|
| valid = []
|
| for val in values:
|
| if val is not None and not np.isnan(val) and not np.isinf(val):
|
| valid.append(val)
|
| return np.nanmean(valid) if valid else None
|
|
|
| def load_groundtruth_type2(gt_folder):
|
| """使用Fine标注参考轨迹,加载文件夹中的所有原始轨迹数据(支持递归遍历子文件夹),兼容多种结构"""
|
| gt_data = {}
|
| for root, dirs, files in os.walk(gt_folder):
|
| for filename in files:
|
| if filename.endswith('.json'):
|
| path = os.path.join(root, filename)
|
| try:
|
| with open(path, encoding='utf-8') as f:
|
| data = json.load(f)
|
| if isinstance(data, dict):
|
| if 'episode_id' in data:
|
| gt_data[data['episode_id']] = data
|
| elif 'episodes' in data and isinstance(data['episodes'], list):
|
| for episode in data['episodes']:
|
| ep_id = episode.get('episode_id')
|
| if ep_id is not None:
|
| gt_data[ep_id] = episode
|
| else:
|
| print(f"Warning: episode missing episode_id in {path}")
|
| else:
|
| print(f"Warning: Unrecognized gt json structure in {path}")
|
| else:
|
| print(f"Warning: gt json is not dict in {path}")
|
| except Exception as e:
|
| print(f"Error loading {path}: {str(e)}")
|
| return gt_data
|
|
|
| def load_reference_trajectory_type1(gt_folder):
|
| """使用原始参考轨迹,加载参考轨迹数据(包含多个episodes的JSON文件,支持递归遍历子文件夹)"""
|
| gt_data = {}
|
| for root, dirs, files in os.walk(gt_folder):
|
| for filename in files:
|
| if filename.endswith('.json'):
|
| path = os.path.join(root, filename)
|
| try:
|
| with open(path, encoding='utf-8') as f:
|
| data = json.load(f)
|
| for episode in data.get('episodes', []):
|
| ep_id = episode['episode_id']
|
| gt_data[ep_id] = episode
|
| except Exception as e:
|
| print(f"Error loading {path}: {str(e)}")
|
| return gt_data
|
|
|
| def load_predictions(mode, pred_path):
|
| """根据模式加载预测数据,支持同episode_id多条轨迹"""
|
| if mode == 1:
|
| with open(pred_path, encoding='utf-8') as f:
|
| data = json.load(f)
|
| pred_data = defaultdict(list)
|
| for k, v in data.items():
|
| ep_id = v.get('episode_id', k)
|
| pred_data[ep_id].append(v)
|
| return pred_data
|
| elif mode == 2:
|
| pred_data = defaultdict(list)
|
| for root, dirs, files in os.walk(pred_path):
|
| for filename in files:
|
| if filename.endswith('.json'):
|
| file_path = os.path.join(root, filename)
|
| try:
|
| with open(file_path, encoding='utf-8') as f:
|
| data = json.load(f)
|
| for k, v in data.items():
|
| ep_id = v.get('episode_id', k)
|
| pred_data[ep_id].append(v)
|
| except Exception as e:
|
| print(f"Error loading {file_path}: {str(e)}")
|
| return pred_data
|
| elif mode == 3:
|
| with open(pred_path, encoding='utf-8') as f:
|
| v = json.load(f)
|
| ep_id = v.get('episode_id', os.path.basename(pred_path).split('.')[0])
|
| pred_data = defaultdict(list)
|
| pred_data[ep_id].append(v)
|
| return pred_data
|
| elif mode == 4:
|
| return load_prediction_folder(pred_path)
|
| else:
|
| raise ValueError("Invalid mode. Use 1 (dict), 2 (multi-dict), 3 (single) or 4 (folder)")
|
|
|
| def load_prediction_folder(folder_path):
|
| """加载整个预测文件夹(支持递归遍历子文件夹),支持同episode_id多条轨迹"""
|
| pred_data = defaultdict(list)
|
| for root, dirs, files in os.walk(folder_path):
|
| for filename in files:
|
| if filename.endswith('.json'):
|
| file_path = os.path.join(root, filename)
|
| try:
|
| with open(file_path, encoding='utf-8') as f:
|
| data = json.load(f)
|
| ep_id = data.get('episode_id', filename.split('.')[0])
|
| pred_data[ep_id].append(data)
|
| except Exception as e:
|
| print(f"Error loading {file_path}: {str(e)}")
|
| return pred_data
|
|
|
| def split_segments(finished):
|
| """根据finished标记分割轨迹"""
|
| if len(finished) == 1:
|
|
|
| return [(0, 0)]
|
|
|
| indices = [i for i, val in enumerate(finished) if val]
|
| segments = []
|
| start = 0
|
| for end in indices:
|
| segments.append((start, end))
|
|
|
| start = end
|
| if start < len(finished) - 1:
|
| segments.append((start, len(finished) - 1))
|
| return segments
|
|
|
|
|
|
|
| def calculate_pl(points):
|
| """计算路径长度"""
|
| pl = 0.0
|
| for i in range(1, len(points)):
|
| current = points[i-1][:3]
|
| next_point = points[i][:3]
|
| pl += np.linalg.norm(np.array(next_point) - np.array(current))
|
| return pl
|
|
|
| def calculate_sr(points, goal, success_distance=20.0):
|
| """计算成功率"""
|
| if not points:
|
| return 0.0
|
| final_point = np.array(points[-1][:3])
|
| goal = np.array(goal[:3])
|
| distance = np.linalg.norm(final_point - goal)
|
| return 1.0 if distance <= success_distance else 0.0
|
|
|
| def calculate_sr_2d(points, goal, success_distance=20.0):
|
| """计算二维成功率"""
|
| if not points:
|
| return 0.0
|
| final_point = np.array(points[-1][:3])
|
| goal = np.array(goal[:3])
|
| distance = np.linalg.norm(final_point[:2] - goal[:2])
|
| return 1.0 if distance <= success_distance else 0.0
|
|
|
| def calculate_osr(points, goal, success_distance=20.0):
|
| """计算Oracle成功率"""
|
| if not points:
|
| return 0.0
|
| points = [np.array(p[:3]) for p in points]
|
| goal = np.array(goal[:3])
|
| distances = [np.linalg.norm(p - goal) for p in points]
|
| return 1.0 if min(distances) <= success_distance else 0.0
|
|
|
| def calculate_osr_2d(points, goal, success_distance=20.0):
|
| """计算二维Oracle成功率"""
|
| if not points:
|
| return 0.0
|
| points = [np.array(p[:3]) for p in points]
|
| goal = np.array(goal[:3])
|
| distances = [np.linalg.norm(p[:2] - goal[:2]) for p in points]
|
| return 1.0 if min(distances) <= success_distance else 0.0
|
|
|
| def calculate_pc(pred_points, ref_points, d_th=20.0):
|
| """计算路径覆盖度"""
|
| if not pred_points or not ref_points:
|
| return 0.0
|
| pred_coords = np.array([p[:3] for p in pred_points])
|
| ref_coords = np.array([r[:3] for r in ref_points])
|
| distances = cdist(ref_coords, pred_coords)
|
| min_distances = np.min(distances, axis=1)
|
| return np.mean(np.exp(-min_distances / d_th))
|
|
|
| def calculate_cls(pred_points, ref_points, d_th=20.0):
|
| """计算覆盖分数"""
|
| pc = calculate_pc(pred_points, ref_points, d_th)
|
| if pc == 0:
|
| return 0.0
|
| pl_ref = calculate_pl(ref_points)
|
| pl_pred = calculate_pl(pred_points)
|
| ls = (pc * pl_ref) / (pc * pl_ref + abs(pc * pl_ref - pl_pred))
|
| return pc * ls
|
|
|
| def calculate_spl(sr, ref_pl, pred_pl):
|
| """计算路径加权成功率"""
|
| if ref_pl == 0 or pred_pl == 0:
|
| return 0.0
|
| return sr * (ref_pl / max(pred_pl, ref_pl))
|
|
|
|
|
|
|
| def calculate_distance(p1, p2, weights=(1.0, 1.0, 1.0)):
|
| """三维加权欧氏距离计算"""
|
| dx = (p1[0] - p2[0]) * weights[0]
|
| dy = (p1[1] - p2[1]) * weights[1]
|
| dz = (p1[2] - p2[2]) * weights[2]
|
| return np.sqrt(dx**2 + dy**2 + dz**2)
|
|
|
|
|
| def calculate_ndtw_3d(pred_path, ref_path, d_h=20.0, z_weight=1.0):
|
| """三维归一化动态时间规整"""
|
| if not pred_path or not ref_path:
|
| return 0.0
|
|
|
|
|
| pred_coords = [p[:3] for p in pred_path]
|
| ref_coords = [r[:3] for r in ref_path]
|
|
|
|
|
| weights = (1.0, 1.0, z_weight)
|
|
|
| try:
|
| distance, _ = fastdtw(ref_coords, pred_coords, dist=lambda x,y: calculate_distance(x,y,weights))
|
| return np.exp(-distance / (len(ref_coords) * d_h))
|
| except:
|
| return 0.0
|
|
|
| def calculate_sdtw_3d(ne, ndtw, d_th=20.0):
|
| """三维加权成功率nDTW"""
|
| return ndtw if ne <= d_th else 0.0
|
|
|
| def extract_sentence_endpoints(trajectory, finished):
|
| """从轨迹中提取每个句子分段的终点坐标"""
|
| segments = split_segments(finished)
|
| endpoints = []
|
| for start, end in segments:
|
| if end < len(trajectory):
|
| endpoint = trajectory[end][:3]
|
| endpoints.append(endpoint)
|
| return endpoints
|
|
|
| def calculate_sentence_ndtw_3d(pred_path, ref_path, pred_finished, ref_finished, d_h=20.0, z_weight=1.0):
|
| """计算基于句子终点的nDTW
|
|
|
| Args:
|
| pred_path: 预测轨迹点列表
|
| ref_path: 参考轨迹点列表
|
| pred_finished: 预测轨迹的finished标记列表
|
| ref_finished: 参考轨迹的finished标记列表
|
| d_h: DTW的标准化参数
|
| z_weight: z轴权重
|
| """
|
| if not pred_path or not ref_path:
|
| return 0.0
|
|
|
|
|
| pred_endpoints = extract_sentence_endpoints(pred_path, pred_finished)
|
| ref_endpoints = extract_sentence_endpoints(ref_path, ref_finished)
|
|
|
| if not pred_endpoints or not ref_endpoints:
|
| return 0.0
|
|
|
|
|
| ref_coords = np.array(ref_endpoints)
|
| pred_coords = np.array(pred_endpoints)
|
|
|
|
|
| weights = np.array([1.0, 1.0, z_weight])
|
|
|
| def calculate_distance(p1, p2, weights):
|
| return np.sqrt(np.sum(((p1 - p2) * weights) ** 2))
|
|
|
|
|
| distance, _ = fastdtw(ref_coords, pred_coords, dist=lambda x,y: calculate_distance(x,y,weights))
|
|
|
|
|
| max_len = len(ref_endpoints)
|
|
|
|
|
| ndtw = np.exp(-distance / (d_h * max_len))
|
| return ndtw
|
|
|
|
|
| def bidirectional_ndtw_3d(pred_path, ref_path, d_h=20.0, z_weight=1.0):
|
| forward = calculate_ndtw_3d(pred_path, ref_path, d_h, z_weight)
|
| backward = calculate_ndtw_3d(ref_path, pred_path, d_h, z_weight)
|
| return (forward + backward) / 2
|
|
|
|
|
| def lcss_3d(pred_path, ref_path, delta=2.0, epsilon=5, z_weight=1.0):
|
| """三维最长公共子序列"""
|
| pred = np.array([p[:3] for p in pred_path])
|
| ref = np.array([r[:3] for r in ref_path])
|
|
|
| dp = np.zeros((len(pred)+1, len(ref)+1))
|
| for i in range(1, len(pred)+1):
|
| for j in range(max(1, i-epsilon), min(len(ref)+1, i+epsilon)):
|
| if calculate_distance(pred[i-1], ref[j-1], (1,1,z_weight)) <= delta:
|
| dp[i][j] = dp[i-1][j-1] + 1
|
| else:
|
| dp[i][j] = max(dp[i-1][j], dp[i][j-1])
|
| return dp[-1][-1] / max(len(pred), len(ref))
|
|
|
|
|
| def edr_3d(pred_path, ref_path, delta=2.0, z_weight=1.0):
|
| """三维编辑距离实数序列"""
|
| pred = np.array([p[:3] for p in pred_path])
|
| ref = np.array([r[:3] for r in ref_path])
|
|
|
| dp = np.zeros((len(pred)+1, len(ref)+1))
|
| for i in range(len(pred)+1):
|
| for j in range(len(ref)+1):
|
| if i == 0:
|
| dp[i][j] = j
|
| elif j == 0:
|
| dp[i][j] = i
|
| else:
|
| cost = 0 if calculate_distance(pred[i-1], ref[j-1], (1,1,z_weight)) <= delta else 1
|
| dp[i][j] = min(dp[i-1][j] + 1,
|
| dp[i][j-1] + 1,
|
| dp[i-1][j-1] + cost)
|
| return 1 - (dp[-1][-1] / max(len(pred), len(ref)))
|
|
|
|
|
| def erp_3d(pred_path, ref_path, delta=2.0, gap_penalty=20.0, z_weight=1.0):
|
| """三维带实际惩罚的编辑距离"""
|
| pred = np.array([p[:3] for p in pred_path])
|
| ref = np.array([r[:3] for r in ref_path])
|
| gap = np.array([gap_penalty]*3)
|
|
|
| dp = np.zeros((len(pred)+1, len(ref)+1))
|
| for i in range(len(pred)+1):
|
| for j in range(len(ref)+1):
|
| if i == 0 and j == 0:
|
| dp[i][j] = 0
|
| elif i == 0:
|
| dp[i][j] = dp[i][j-1] + calculate_distance(ref[j-1], gap, (1,1,z_weight))
|
| elif j == 0:
|
| dp[i][j] = dp[i-1][j] + calculate_distance(pred[i-1], gap, (1,1,z_weight))
|
| else:
|
| cost = calculate_distance(pred[i-1], ref[j-1], (1,1,z_weight))
|
| dp[i][j] = min(dp[i-1][j] + calculate_distance(pred[i-1], gap, (1,1,z_weight)),
|
| dp[i][j-1] + calculate_distance(ref[j-1], gap, (1,1,z_weight)),
|
| dp[i-1][j-1] + cost)
|
| return np.exp(-dp[-1][-1] / (len(ref) * delta))
|
|
|
|
|
| def hausdorff_3d(pred_path, ref_path, z_weight=1.0):
|
| """三维Hausdorff距离"""
|
| pred = np.array([p[:3] for p in pred_path]) if pred_path else np.empty((0,3))
|
| ref = np.array([r[:3] for r in ref_path]) if ref_path else np.empty((0,3))
|
|
|
| if pred.size == 0 or ref.size == 0:
|
| return 0.0
|
|
|
| def _hdistance(A, B):
|
| d_matrix = np.array([[calculate_distance(a, b, (1,1,z_weight)) for b in B] for a in A])
|
| return np.max(np.min(d_matrix, axis=1))
|
|
|
| return max(_hdistance(pred, ref), _hdistance(ref, pred))
|
|
|
| def calculate_ne_a(current_seg, current_goal, cumulative_pl_ref):
|
| """
|
| 计算当前段的最后一个点与目标点之间的归一化误差。
|
|
|
| 参数:
|
| current_seg (list or None): 当前段的点列表,每个点至少包含3个坐标。如果为空,则返回None。
|
| current_goal (list or array-like): 目标点,至少包含3个坐标。
|
| cumulative_pl_ref (float): 累积参考路径长度,用于归一化误差。
|
|
|
| 返回:
|
| float: 归一化误差 (ne_a)。如果累积路径长度为0或无效,则返回无穷大。
|
| """
|
| ne_a = None
|
| if current_seg:
|
| final_point = np.array(current_seg[-1][:3])
|
| goal_point = np.array(current_goal[:3])
|
| ne = np.linalg.norm(final_point - goal_point)
|
|
|
| ne_a = ne / cumulative_pl_ref if cumulative_pl_ref > 0 else float('inf')
|
| return ne_a
|
|
|
| def calculate_cesr(points, goal, success_distance=20.0):
|
| """计算指令结束判断成功率"""
|
| if not points:
|
| return 0.0
|
| final_point = np.array(points[-1][:3])
|
| goal = np.array(goal[:3])
|
| distance = np.linalg.norm(final_point - goal)
|
| return 1.0 if distance <= success_distance else 0.0
|
|
|
| def calculate_explore_ratio(ref_points):
|
| """计算探索比率:起终点直线距离 / 参考轨迹长度"""
|
| if len(ref_points) < 2:
|
| return 0.0
|
| start = np.array(ref_points[0][:3])
|
| end = np.array(ref_points[-1][:3])
|
| straight_dist = np.linalg.norm(end - start)
|
| path_length = calculate_pl(ref_points)
|
| return straight_dist / path_length if path_length > 0 else 0.0
|
|
|
|
|
| def evaluate_trajectory(orig, pred, ref_type=1):
|
|
|
| if 'pred' in pred and pred['pred']:
|
|
|
| full_pred_finished = [p.get('finished', False) for p in pred['pred']]
|
| else:
|
|
|
| full_pred_finished = pred.get('finished', [])
|
|
|
| if ref_type == 1:
|
|
|
| full_ref_traj = orig.get('reference_path', [])
|
|
|
| full_ref_finished = [False] * (len(full_ref_traj)-1) + [True] if full_ref_traj else []
|
| if orig.get('goals'):
|
| full_goal = orig['goals'][0]['position']
|
| else:
|
| full_goal = [0, 0, 0]
|
| sentences = []
|
| sub_goals = [full_goal]
|
| ref_paths = [full_ref_traj]
|
| else:
|
|
|
| sentences = orig.get('sentence_instructions', [])
|
| sub_goals = [s['end_position'] for s in sentences]
|
| ref_paths = [s['reference_path'] for s in sentences]
|
|
|
| full_ref_finished = []
|
| for path in ref_paths:
|
| full_ref_finished.extend([False] * (len(path)-1) + [True] if path else [])
|
|
|
| full_pred_traj = pred['trajectory']
|
| full_ref_traj = [p for path in ref_paths for p in path]
|
| finished = pred.get('finished', [])
|
| segments = split_segments(finished)
|
|
|
| sub_metrics = []
|
| pred_segment = []
|
| ref_segment = []
|
| cumulative_pred_traj = []
|
| cumulative_ref_traj = []
|
| cumulative_pl_ref = 0.0
|
| total_actions = 0
|
|
|
| pred_action_count = len(full_pred_traj)
|
| gt_action_count = len(full_ref_traj) if len(full_ref_traj) > 0 else 1
|
|
|
|
|
|
|
| global_segment_data = {}
|
|
|
| if ref_type == 2:
|
|
|
| for seg_idx in range(len(sentences)):
|
|
|
| if seg_idx < len(segments):
|
| start, end = segments[seg_idx]
|
| pred_segment = full_pred_traj[start:end+1]
|
| cumulative_pred_traj.extend(pred_segment)
|
| else:
|
|
|
| pred_segment = []
|
|
|
|
|
| if seg_idx < len(ref_paths):
|
| ref_segment = ref_paths[seg_idx] if seg_idx < len(ref_paths) else []
|
| cumulative_ref_traj.extend(ref_segment)
|
| else:
|
|
|
| ref_segment = []
|
|
|
| goal = sub_goals[seg_idx] if seg_idx < len(sub_goals) else None
|
|
|
|
|
| pl_pred = calculate_pl(pred_segment)
|
| pl_ref = calculate_pl(ref_segment)
|
| cumulative_pl_pred = calculate_pl(cumulative_pred_traj)
|
| cumulative_pl_ref = calculate_pl(cumulative_ref_traj)
|
| final_point = np.array(pred_segment[-1][:3] if pred_segment else [0, 0, 0])
|
| goal_point = np.array(goal[:3] if goal and pred_segment else [0, 0, 0])
|
|
|
|
|
| metrics = {
|
|
|
| 'PL': 0.0,
|
| 'Action_Count': 0.0,
|
| 'SR_3D-5': 0.0,
|
| 'SR_3D-10': 0.0,
|
| 'SR_3D-15': 0.0,
|
| 'SR_3D-20': 0.0,
|
| 'SR_3D-30': 0.0,
|
| 'SR_3D-40': 0.0,
|
| 'OSR_3D-5': 0.0,
|
| 'OSR_3D-10': 0.0,
|
| 'OSR_3D-15': 0.0,
|
| 'OSR_3D-20': 0.0,
|
| 'OSR_3D-30': 0.0,
|
| 'OSR_3D-40': 0.0,
|
| 'NE_3D': 0.0,
|
| 'NE-A': 0.0,
|
| 'Explore_Ratio': 0.0,
|
|
|
| 'PB': 0.0,
|
| 'CESR': 0.0,
|
| 'SPL': 0.0,
|
| 'CLS': 0.0,
|
|
|
| 'nDTW_3D': 0.0,
|
| 'sDTW_3D': 0.0,
|
| 'Backward-nDTW_3D': 0.0,
|
| 'Bi-nDTW_3D': 0.0,
|
| 'LCSS_3D': 0.0,
|
| 'EDR_3D': 0.0,
|
| 'ERP_3D': 0.0,
|
| 'Hausdorff_3D': None,
|
|
|
| 'SR_2D': 0.0,
|
| 'OSR_2D': 0.0,
|
| 'NE_2D': 0.0,
|
| }
|
|
|
|
|
| if pred_segment and ref_type == 2:
|
|
|
| metrics.update({
|
|
|
| 'SR_3D-15': calculate_sr(cumulative_pred_traj, goal, 15.0),
|
| 'OSR_3D-15': calculate_osr(cumulative_pred_traj, goal, 15.0),
|
| 'PL': pl_pred,
|
| 'Action_Count': len(pred_segment),
|
| 'SR_3D-5': calculate_sr(cumulative_pred_traj, goal, 5.0),
|
| 'SR_3D-10': calculate_sr(cumulative_pred_traj, goal, 10.0),
|
| 'SR_3D-20': calculate_sr(cumulative_pred_traj, goal),
|
| 'SR_3D-30': calculate_sr(cumulative_pred_traj, goal, 30.0),
|
| 'SR_3D-40': calculate_sr(cumulative_pred_traj, goal, 40.0),
|
| 'OSR_3D-5': calculate_osr(cumulative_pred_traj, goal, 5.0),
|
| 'OSR_3D-10': calculate_osr(cumulative_pred_traj, goal, 10.0),
|
| 'OSR_3D-20': calculate_osr(cumulative_pred_traj, goal),
|
| 'OSR_3D-30': calculate_osr(cumulative_pred_traj, goal, 30.0),
|
| 'OSR_3D-40': calculate_osr(cumulative_pred_traj, goal, 40.0),
|
| 'NE_3D': np.linalg.norm(final_point - goal_point) if pred_segment and goal else float('inf'),
|
| 'NE-A': calculate_ne_a(pred_segment, goal, cumulative_pl_ref) if pred_segment else None,
|
|
|
| 'PB': min(cumulative_pl_ref, cumulative_pl_pred) / max(cumulative_pl_ref, cumulative_pl_pred) if max(cumulative_pl_ref, cumulative_pl_pred) != 0 else 0.0,
|
| 'CESR': calculate_cesr(cumulative_pred_traj, goal),
|
| 'SPL': calculate_spl(calculate_sr(cumulative_pred_traj, goal), cumulative_pl_ref, cumulative_pl_pred),
|
| 'CLS': calculate_cls(cumulative_pred_traj, cumulative_ref_traj),
|
|
|
| 'nDTW_3D': calculate_ndtw_3d(cumulative_pred_traj, cumulative_ref_traj),
|
| 'sDTW_3D': calculate_sdtw_3d(np.linalg.norm(final_point - goal_point), calculate_ndtw_3d(cumulative_pred_traj, cumulative_ref_traj)) if cumulative_pred_traj else 0.0,
|
| 'Backward-nDTW_3D': calculate_ndtw_3d(cumulative_ref_traj, cumulative_pred_traj),
|
| 'Bi-nDTW_3D': bidirectional_ndtw_3d(cumulative_pred_traj, cumulative_ref_traj),
|
| 'LCSS_3D': lcss_3d(cumulative_pred_traj, cumulative_ref_traj),
|
| 'EDR_3D': edr_3d(cumulative_pred_traj, cumulative_ref_traj),
|
| 'ERP_3D': erp_3d(cumulative_pred_traj, cumulative_ref_traj),
|
| 'Hausdorff_3D': hausdorff_3d(cumulative_pred_traj, cumulative_ref_traj),
|
|
|
| 'SR_2D': calculate_sr_2d(cumulative_pred_traj, goal),
|
| 'OSR_2D': calculate_osr_2d(cumulative_pred_traj, goal),
|
| 'NE_2D': np.linalg.norm(final_point[:2] - goal_point[:2]) if pred_segment and goal else float('inf'),
|
| })
|
| elif ref_type == 2:
|
|
|
| if cumulative_pred_traj and goal is not None:
|
| final_point = np.array(cumulative_pred_traj[-1][:3])
|
| goal_point = np.array(goal[:3])
|
| metrics['NE_3D'] = np.linalg.norm(final_point - goal_point)
|
| metrics['NE-A'] = calculate_ne_a(full_pred_traj, goal, cumulative_pl_ref)
|
| else:
|
| metrics['NE_3D'] = float('inf')
|
| metrics['NE-A'] = None
|
|
|
|
|
| global_segment_data[seg_idx] = {
|
| 'SR_3D-5': metrics.get('SR_3D-5', 0.0),
|
| 'SR_3D-10': metrics.get('SR_3D-10', 0.0),
|
| 'SR_3D-15': metrics.get('SR_3D-15', 0.0),
|
| 'SR_3D-20': metrics.get('SR_3D-20', 0.0),
|
| 'OSR_3D-5': metrics.get('OSR_3D-5', 0.0),
|
| 'OSR_3D-10': metrics.get('OSR_3D-10', 0.0),
|
| 'OSR_3D-15': metrics.get('OSR_3D-15', 0.0),
|
| 'OSR_3D-20': metrics.get('OSR_3D-20', 0.0),
|
| 'SPL': metrics.get('SPL', 0.0),
|
| 'NE_3D': metrics.get('NE_3D', float('inf')),
|
| 'Min_Distance': metrics.get('Min_Distance', float('inf')),
|
| 'NE-A': metrics.get('NE-A', None)
|
| }
|
|
|
|
|
| sub_metrics.append(metrics)
|
| total_actions += len(pred_segment)
|
|
|
|
|
|
|
| full_metrics = {
|
| 'segment_metrics': global_segment_data,
|
| 'PL': calculate_pl(full_pred_traj),
|
| 'PB': min(calculate_pl(full_ref_traj), calculate_pl(full_pred_traj)) / max(calculate_pl(full_ref_traj), calculate_pl(full_pred_traj)),
|
| 'SR_3D-5': calculate_sr(full_pred_traj, sub_goals[-1], 5.0),
|
| 'SR_3D-10': calculate_sr(full_pred_traj, sub_goals[-1], 10.0),
|
| 'SR_3D-15': calculate_sr(full_pred_traj, sub_goals[-1], 15.0),
|
| 'SR_3D-20': calculate_sr(full_pred_traj, sub_goals[-1]),
|
| 'SR_3D-30': calculate_sr(full_pred_traj, sub_goals[-1], 30.0),
|
| 'SR_3D-40': calculate_sr(full_pred_traj, sub_goals[-1], 40.0),
|
| 'OSR_3D-5': calculate_osr(full_pred_traj, sub_goals[-1], 5.0),
|
| 'OSR_3D-10': calculate_osr(full_pred_traj, sub_goals[-1], 10.0),
|
| 'OSR_3D-15': calculate_osr(full_pred_traj, sub_goals[-1], 15.0),
|
| 'OSR_3D-20': calculate_osr(full_pred_traj, sub_goals[-1]),
|
| 'OSR_3D-30': calculate_osr(full_pred_traj, sub_goals[-1], 30.0),
|
| 'OSR_3D-40': calculate_osr(full_pred_traj, sub_goals[-1], 40.0),
|
| 'SPL': calculate_spl(calculate_sr(full_pred_traj, sub_goals[-1]), calculate_pl(full_ref_traj), calculate_pl(full_pred_traj)),
|
| 'CLS': calculate_cls(full_pred_traj, full_ref_traj),
|
| 'NE_3D': np.linalg.norm(np.array(full_pred_traj[-1][:3]) - np.array(sub_goals[-1][:3])) if full_pred_traj else None,
|
| 'NE-A': calculate_ne_a(full_pred_traj, sub_goals[-1], calculate_pl(full_ref_traj)) if full_pred_traj else None,
|
| 'nDTW_3D': calculate_ndtw_3d(full_pred_traj, full_ref_traj),
|
| 'Sentence-nDTW_3D': calculate_sentence_ndtw_3d(full_pred_traj, full_ref_traj, full_pred_finished, full_ref_finished) if (full_pred_traj and full_ref_traj and full_pred_finished and full_ref_finished) else 0.0,
|
| 'sDTW_3D': calculate_sdtw_3d(np.linalg.norm(np.array(full_pred_traj[-1][:3]) - np.array(sub_goals[-1][:3])), calculate_ndtw_3d(full_pred_traj, full_ref_traj)) if full_pred_traj else 0.0,
|
| 'Backward-nDTW_3D': calculate_ndtw_3d(full_ref_traj, full_pred_traj),
|
| 'Bi-nDTW_3D': bidirectional_ndtw_3d(full_pred_traj, full_ref_traj),
|
| 'LCSS_3D': lcss_3d(full_pred_traj, full_ref_traj),
|
| 'EDR_3D': edr_3d(full_pred_traj, full_ref_traj),
|
| 'ERP_3D': erp_3d(full_pred_traj, full_ref_traj),
|
| 'Hausdorff_3D': hausdorff_3d(full_pred_traj, full_ref_traj),
|
| 'Action_Count': len(full_pred_traj) if ref_type == 1 else total_actions,
|
| 'Collision': 1 if pred.get('is_collisioned', False) else 0,
|
| 'Explore_Ratio': calculate_explore_ratio(full_ref_traj),
|
| 'Explore_Rate': pred_action_count / gt_action_count,
|
|
|
| 'SR_2D': calculate_sr_2d(full_pred_traj, sub_goals[-1]),
|
| 'OSR_2D': calculate_osr_2d(full_pred_traj, sub_goals[-1]),
|
| 'NE_2D': np.linalg.norm(np.array(full_pred_traj[-1][:3])[:2] - np.array(sub_goals[-1][:3])[:2]) if full_pred_traj else None,
|
| }
|
|
|
|
|
| try:
|
| if full_pred_traj and sub_goals:
|
| full_metrics['Min_Distance'] = np.min([np.linalg.norm(np.array(p[:3]) - np.array(sub_goals[-1][:3])) for p in full_pred_traj])
|
| else:
|
| full_metrics['Min_Distance'] = float('inf')
|
| except Exception:
|
| full_metrics['Min_Distance'] = float('inf')
|
|
|
|
|
| success_count = sum(1 for sub in sub_metrics if sub['CESR'] == 1.0)
|
| full_metrics['CESR'] = success_count / len(sub_metrics) if len(sub_metrics) > 0 else 0.0
|
|
|
| return sub_metrics, full_metrics
|
|
|
| def main():
|
|
|
|
|
|
|
|
|
|
|
| pos_argv, opts = _parse_cli_with_flags(sys.argv)
|
|
|
| if len(pos_argv) not in (4, 6, 8):
|
| print("Usage:")
|
| print(" python eval_1013.py <ref_type_A> <pred_type_A> <gt_A> <pred_A> [--segcat <json>]")
|
| print(" python eval_1013.py <ref_type_A> <pred_type_A> <gt_A> <pred_A> <pred_type_B> <pred_B> [--segcat <json>]")
|
| print(" python eval_1013.py <ref_type_A> <pred_type_A> <gt_A> <pred_A> <ref_type_B> <pred_type_B> <gt_B> <pred_B> [--segcat <json>]")
|
| print(" ref_type: 1 (Full-level) | 2 (Sentence-level/Fine-level)")
|
| print(" pred_type: 1 (dict) | 2 (multi-dict) | 3 (single) | 4 (folder)")
|
| sys.exit(1)
|
|
|
|
|
| ref_type = int(pos_argv[0])
|
| mode = int(pos_argv[1])
|
| gt_folder = pos_argv[2]
|
| pred_path = pos_argv[3]
|
| segcat_json_path = opts.get('segcat')
|
|
|
| has_pair = False
|
| if len(pos_argv) == 6:
|
|
|
| ref_type_b = ref_type
|
| mode_b = int(pos_argv[4])
|
| gt_folder_b = gt_folder
|
| pred_path_b = pos_argv[5]
|
| has_pair = True
|
| elif len(pos_argv) == 8:
|
|
|
| ref_type_b = int(pos_argv[4])
|
| mode_b = int(pos_argv[5])
|
| gt_folder_b = pos_argv[6]
|
| pred_path_b = pos_argv[7]
|
| has_pair = True
|
|
|
|
|
| def get_safe_base_name(path: str) -> str:
|
| try:
|
| if os.path.isdir(path):
|
| base_name = os.path.basename(os.path.normpath(path))
|
| else:
|
| folder_name = os.path.basename(os.path.dirname(path.rstrip('/')))
|
| file_name = os.path.splitext(os.path.basename(path))[0]
|
| base_name = f"{folder_name}_{file_name}"
|
| except Exception:
|
| base_name = os.path.splitext(os.path.basename(path))[0]
|
|
|
| return re.sub(r'[\\/*?:"<>|]', "_", base_name)
|
|
|
|
|
| if has_pair:
|
| base_a = get_safe_base_name(pred_path)
|
| base_b = get_safe_base_name(pred_path_b)
|
| gt_base_a = get_safe_base_name(gt_folder)
|
| gt_base_b = get_safe_base_name(gt_folder_b)
|
| if gt_base_a == gt_base_b:
|
| output_file = f"{base_a}__VS__{base_b}_ON_{gt_base_a}_evaluation_results_v20251013.xlsx"
|
| else:
|
| output_file = f"{base_a}_ON_{gt_base_a}__VS__{base_b}_ON_{gt_base_b}_evaluation_results_v20251013.xlsx"
|
| else:
|
| base_a = get_safe_base_name(pred_path)
|
| gt_base = get_safe_base_name(gt_folder)
|
| output_file = f"{base_a}_ON_{gt_base}_evaluation_results_v20251013.xlsx"
|
|
|
|
|
| if mode == 3 and not os.path.isdir(pred_path):
|
| print("Error: Mode 3 requires a folder path")
|
| sys.exit(1)
|
| if ref_type == 1:
|
| gt_data = load_reference_trajectory_type1(gt_folder)
|
| else:
|
| gt_data = load_groundtruth_type2(gt_folder)
|
| pred_data = load_predictions(mode, pred_path)
|
|
|
|
|
| segcat_mapping = {}
|
| if segcat_json_path:
|
| segcat_mapping = load_seg_categories(segcat_json_path)
|
| if segcat_mapping:
|
| print(f"Loaded segment categories: {len(segcat_mapping)} keyed entries from {segcat_json_path}")
|
| else:
|
| print(f"Warning: No usable segment categories parsed from {segcat_json_path}")
|
|
|
|
|
| original_trajectory_count = sum(len(preds) for preds in pred_data.values())
|
|
|
|
|
|
|
| print("开始去重处理...")
|
|
|
|
|
| episode_groups = {}
|
| original_count = 0
|
| for ep_id, preds in pred_data.items():
|
| for pred in preds:
|
| if ep_id not in episode_groups:
|
| episode_groups[ep_id] = []
|
| episode_groups[ep_id].append({
|
| 'key': ep_id,
|
| 'pred': pred,
|
| 'ep_id': ep_id
|
| })
|
| original_count += 1
|
|
|
| print(f"收集到 {len(episode_groups)} 个不同的episode,共 {original_count} 条轨迹")
|
|
|
|
|
| temp_results = {}
|
| duplicate_count = 0
|
|
|
| for ep_id, trajectories in tqdm(episode_groups.items(), desc="处理每组轨迹"):
|
| if ep_id not in gt_data:
|
| print(f"Warning: Missing ground truth for {ep_id}, 跳过该组")
|
| continue
|
|
|
| if len(trajectories) > 1:
|
| duplicate_count += len(trajectories) - 1
|
| print(f"发现重复轨迹 {ep_id},共 {len(trajectories)} 条")
|
|
|
| best_trajectory = None
|
| best_metrics = None
|
|
|
|
|
| for i, traj_data in enumerate(trajectories):
|
| try:
|
|
|
| sub_metrics, full_metrics = evaluate_trajectory(gt_data[ep_id], traj_data['pred'], ref_type)
|
|
|
|
|
| sr_3d_20 = full_metrics.get('SR_3D-20', 0)
|
| osr_3d_20 = full_metrics.get('OSR_3D-20', 0)
|
| ne_3d = full_metrics.get('NE_3D', float('inf'))
|
|
|
| current_metrics = {
|
| 'sr_3d_20': sr_3d_20,
|
| 'osr_3d_20': osr_3d_20,
|
| 'ne_3d': ne_3d,
|
| 'sub_metrics': sub_metrics,
|
| 'full_metrics': full_metrics,
|
| 'traj_data': traj_data,
|
| 'index': i
|
| }
|
|
|
|
|
| if best_trajectory is None or is_better_trajectory(current_metrics, best_metrics):
|
| if best_trajectory is not None and best_metrics is not None and len(trajectories) > 1:
|
|
|
| reason = compare_trajectories(current_metrics, best_metrics)
|
| print(f" 轨迹 {i+1} 更好:{reason}")
|
| best_trajectory = traj_data
|
| best_metrics = current_metrics
|
| elif len(trajectories) > 1 and best_metrics is not None:
|
|
|
| reason = compare_trajectories(best_metrics, current_metrics)
|
| print(f" 保留轨迹 {best_metrics['index']+1}:{reason}")
|
|
|
| except Exception as e:
|
| print(f"计算轨迹 {ep_id}[{i}] 时出错: {str(e)}")
|
| continue
|
|
|
|
|
| if best_trajectory is not None and best_metrics is not None:
|
| temp_results[ep_id] = {
|
| 'pred': best_trajectory['pred'],
|
| 'sr_3d_20': best_metrics['sr_3d_20'],
|
| 'osr_3d_20': best_metrics['osr_3d_20'],
|
| 'ne_3d': best_metrics['ne_3d'],
|
| 'sub_metrics': best_metrics['sub_metrics'],
|
| 'full_metrics': best_metrics['full_metrics']
|
| }
|
|
|
|
|
| pred_data = {ep_id: data['pred'] for ep_id, data in temp_results.items()}
|
|
|
|
|
| if SAVE_DEDUP_JSON:
|
| dedup_json_path = os.path.splitext(output_file)[0] + '_dedup.json'
|
| with open(dedup_json_path, 'w', encoding='utf-8') as f:
|
| json.dump(pred_data, f, ensure_ascii=False, indent=2)
|
| print(f"去重后择优轨迹已保存至 {dedup_json_path}")
|
|
|
| print(f"去重完成,原始轨迹数: {original_trajectory_count}, 去重后轨迹数: {len(pred_data)}")
|
|
|
|
|
| total_gt = len(gt_data)
|
| total_pred = len(pred_data)
|
| computed = 0
|
| results = {}
|
| for ep_id in tqdm(pred_data.keys(), desc="构建最终结果"):
|
| if ep_id not in gt_data:
|
| print(f"Warning: Missing ground truth for {ep_id}")
|
| continue
|
|
|
|
|
| if ep_id in temp_results:
|
| results[ep_id] = {
|
| 'sub_metrics': temp_results[ep_id]['sub_metrics'],
|
| 'full_metrics': temp_results[ep_id]['full_metrics']
|
| }
|
| else:
|
|
|
| try:
|
| sub_metrics, full_metrics = evaluate_trajectory(gt_data[ep_id], pred_data[ep_id], ref_type)
|
| results[ep_id] = {
|
| 'sub_metrics': sub_metrics,
|
| 'full_metrics': full_metrics
|
| }
|
| except Exception as e:
|
| print(f"Error evaluating {ep_id}: {str(e)}")
|
| continue
|
|
|
|
|
| computed += 1
|
|
|
|
|
|
|
| if results:
|
| rows = []
|
| seg_avg_rows = []
|
| all_full_metrics = []
|
| all_sub_metrics = []
|
| global_segment_store = defaultdict(lambda: {
|
| 'SR_3D-5': [],
|
| 'SR_3D-10': [],
|
| 'SR_3D-15': [],
|
| 'SR_3D-20': [],
|
| 'SR_3D-30': [],
|
| 'SR_3D-40': [],
|
| 'OSR_3D-5': [],
|
| 'OSR_3D-10': [],
|
| 'OSR_3D-15': [],
|
| 'OSR_3D-20': [],
|
| 'OSR_3D-30': [],
|
| 'OSR_3D-40': [],
|
| 'SPL': [],
|
| 'NE_3D': [],
|
| 'NE-A': []
|
| })
|
|
|
|
|
| def sr_osr_collision(val, collision):
|
| return 0.0 if collision==1 else val
|
|
|
|
|
| columns_order = [
|
| 'Episode ID', 'Type',
|
| 'PL','Action_Count', 'Explore_Rate', 'SR_3D-5', 'SR_3D-10', 'SR_3D-15', 'SR_3D-15-Collision', 'SR_3D-20', 'SR_3D-20-Collision', 'SR_3D-30', 'SR_3D-40',
|
| 'OSR_3D-5', 'OSR_3D-10', 'OSR_3D-15', 'OSR_3D-15-Collision', 'OSR_3D-20', 'OSR_3D-20-Collision', 'OSR_3D-30', 'OSR_3D-40',
|
| 'NE_3D', 'Min_Distance', 'NE-A', 'nDTW_3D', 'Sentence-nDTW_3D', 'sDTW_3D', 'SPL', 'CESR', 'PB',
|
| 'CLS', 'Backward-nDTW_3D', 'Bi-nDTW_3D','LCSS_3D', 'EDR_3D', 'ERP_3D', 'Hausdorff_3D', 'Collision', 'SR_2D', 'OSR_2D', 'NE_2D', 'Explore_Ratio'
|
| ]
|
|
|
|
|
| for ep_id, metrics in results.items():
|
|
|
| full = metrics['full_metrics']
|
| all_full_metrics.append(full)
|
|
|
|
|
| sub_list = metrics['sub_metrics']
|
| all_sub_metrics.extend(sub_list)
|
|
|
|
|
| seg_metrics = metrics['full_metrics']['segment_metrics']
|
| for seg_num, seg_data in seg_metrics.items():
|
| for metric in ['SR_3D-5', 'SR_3D-10', 'SR_3D-15', 'SR_3D-20', 'SR_3D-30', 'SR_3D-40', 'OSR_3D-5', 'OSR_3D-10', 'OSR_3D-15', 'OSR_3D-20', 'OSR_3D-30', 'OSR_3D-40', 'SPL', 'NE_3D', 'NE-A']:
|
|
|
| value = seg_data.get(metric, None)
|
| if value is not None and not np.isnan(value):
|
| global_segment_store[seg_num][metric].append(value)
|
|
|
|
|
| rows.append({
|
| 'Episode ID': ep_id,
|
| 'Type': 'Full',
|
| 'PL': full['PL'],
|
| 'Action_Count': full['Action_Count'],
|
| 'SR_3D-5': full['SR_3D-5'],
|
| 'SR_3D-10': full['SR_3D-10'],
|
| 'SR_3D-15': full['SR_3D-15'],
|
| 'SR_3D-15-Collision': sr_osr_collision(full['SR_3D-15'], full['Collision']),
|
| 'SR_3D-20': full['SR_3D-20'],
|
| 'SR_3D-20-Collision': sr_osr_collision(full['SR_3D-20'], full['Collision']),
|
| 'SR_3D-30': full['SR_3D-30'],
|
| 'SR_3D-40': full['SR_3D-40'],
|
| 'OSR_3D-5': full['OSR_3D-5'],
|
| 'OSR_3D-10': full['OSR_3D-10'],
|
| 'OSR_3D-15': full['OSR_3D-15'],
|
| 'OSR_3D-15-Collision': sr_osr_collision(full['OSR_3D-15'], full['Collision']),
|
| 'OSR_3D-20': full['OSR_3D-20'],
|
| 'OSR_3D-20-Collision': sr_osr_collision(full['OSR_3D-20'], full['Collision']),
|
| 'OSR_3D-30': full['OSR_3D-30'],
|
| 'OSR_3D-40': full['OSR_3D-40'],
|
| 'NE_3D': full['NE_3D'],
|
| 'Min_Distance': full.get('Min_Distance', None),
|
| 'Explore_Rate': full.get('Explore_Rate', None),
|
|
|
| 'SPL': full['SPL'],
|
| 'CESR': full['CESR'],
|
| 'PB': full['PB'],
|
| 'CLS': full['CLS'],
|
| 'nDTW_3D': full['nDTW_3D'],
|
| 'Sentence-nDTW_3D': full['Sentence-nDTW_3D'],
|
| 'Backward-nDTW_3D': full['Backward-nDTW_3D'],
|
| 'Bi-nDTW_3D': full['Bi-nDTW_3D'],
|
| 'sDTW_3D': full['sDTW_3D'],
|
| 'LCSS_3D': full['LCSS_3D'],
|
| 'EDR_3D': full['EDR_3D'],
|
| 'ERP_3D': full['ERP_3D'],
|
| 'Hausdorff_3D': full['Hausdorff_3D'],
|
| 'Collision': full['Collision'],
|
|
|
| 'SR_2D': full['SR_2D'],
|
| 'OSR_2D': full['OSR_2D'],
|
| 'NE_2D': full['NE_2D'],
|
| 'Explore_Ratio': full.get('Explore_Ratio', None)
|
| })
|
|
|
|
|
| for seg_idx, sub in enumerate(metrics['sub_metrics']):
|
| rows.append({
|
| 'Episode ID': ep_id,
|
| 'Type': f'Seg_{seg_idx}',
|
| 'PL': sub['PL'],
|
| 'Action_Count': sub['Action_Count'],
|
| 'SR_3D-5': sub['SR_3D-5'],
|
| 'SR_3D-10': sub['SR_3D-10'],
|
| 'SR_3D-15': sub['SR_3D-15'],
|
| 'SR_3D-20': sub['SR_3D-20'],
|
| 'SR_3D-30': sub['SR_3D-30'],
|
| 'SR_3D-40': sub['SR_3D-40'],
|
| 'OSR_3D-5': sub['OSR_3D-5'],
|
| 'OSR_3D-10': sub['OSR_3D-10'],
|
| 'OSR_3D-15': sub['OSR_3D-15'],
|
| 'OSR_3D-20': sub['OSR_3D-20'],
|
| 'OSR_3D-30': sub['OSR_3D-30'],
|
| 'OSR_3D-40': sub['OSR_3D-40'],
|
| 'NE_3D': sub['NE_3D'],
|
| 'NE-A': sub['NE-A'],
|
|
|
| 'SR_2D': sub.get('SR_2D'),
|
| 'OSR_2D': sub.get('OSR_2D'),
|
| 'NE_2D': sub.get('NE_2D'),
|
| 'SPL': sub['SPL'],
|
| 'CESR': sub['CESR'],
|
| 'PB': sub['PB'],
|
| 'CLS': sub['CLS'],
|
| 'nDTW_3D': sub['nDTW_3D'],
|
| 'Backward-nDTW_3D': sub['Backward-nDTW_3D'],
|
| 'Bi-nDTW_3D': sub['Bi-nDTW_3D'],
|
| 'sDTW_3D': sub['sDTW_3D'],
|
| 'LCSS_3D': sub['LCSS_3D'],
|
| 'EDR_3D': sub['EDR_3D'],
|
| 'ERP_3D': sub['ERP_3D'],
|
| 'Hausdorff_3D': sub['Hausdorff_3D'],
|
| 'Collision': None,
|
| })
|
|
|
|
|
| if metrics['sub_metrics'] and ref_type == 2:
|
| avg_sub = {
|
| 'PL': safe_nanmean([s['PL'] for s in metrics['sub_metrics'] if s['PL'] is not None]),
|
| 'Action_Count': safe_nanmean([s['Action_Count'] for s in metrics['sub_metrics'] if s['Action_Count'] is not None]),
|
| 'SR_3D-5': safe_nanmean([s['SR_3D-5'] for s in metrics['sub_metrics'] if s['SR_3D-5'] is not None]),
|
| 'SR_3D-10': safe_nanmean([s['SR_3D-10'] for s in metrics['sub_metrics'] if s['SR_3D-10'] is not None]),
|
| 'SR_3D-15': safe_nanmean([s['SR_3D-15'] for s in metrics['sub_metrics'] if s['SR_3D-15'] is not None]),
|
| 'SR_3D-20': safe_nanmean([s['SR_3D-20'] for s in metrics['sub_metrics'] if s['SR_3D-20'] is not None]),
|
| 'SR_3D-30': safe_nanmean([s['SR_3D-30'] for s in metrics['sub_metrics'] if s['SR_3D-30'] is not None]),
|
| 'SR_3D-40': safe_nanmean([s['SR_3D-40'] for s in metrics['sub_metrics'] if s['SR_3D-40'] is not None]),
|
| 'OSR_3D-5': safe_nanmean([s['OSR_3D-5'] for s in metrics['sub_metrics'] if s['OSR_3D-5'] is not None]),
|
| 'OSR_3D-10': safe_nanmean([s['OSR_3D-10'] for s in metrics['sub_metrics'] if s['OSR_3D-10'] is not None]),
|
| 'OSR_3D-15': safe_nanmean([s['OSR_3D-15'] for s in metrics['sub_metrics'] if s['OSR_3D-15'] is not None]),
|
| 'OSR_3D-20': safe_nanmean([s['OSR_3D-20'] for s in metrics['sub_metrics'] if s['OSR_3D-20'] is not None]),
|
| 'OSR_3D-30': safe_nanmean([s['OSR_3D-30'] for s in metrics['sub_metrics'] if s['OSR_3D-30'] is not None]),
|
| 'OSR_3D-40': safe_nanmean([s['OSR_3D-40'] for s in metrics['sub_metrics'] if s['OSR_3D-40'] is not None]),
|
| 'NE_3D': safe_nanmean([s['NE_3D'] for s in metrics['sub_metrics'] if s['NE_3D'] is not None]),
|
| 'NE-A': safe_nanmean([s['NE-A'] for s in metrics['sub_metrics'] if s['NE-A'] is not None]),
|
|
|
| 'SR_2D': safe_nanmean([s.get('SR_2D') for s in metrics['sub_metrics'] if s.get('SR_2D') is not None]),
|
| 'OSR_2D': safe_nanmean([s.get('OSR_2D') for s in metrics['sub_metrics'] if s.get('OSR_2D') is not None]),
|
| 'NE_2D': safe_nanmean([s.get('NE_2D') for s in metrics['sub_metrics'] if s.get('NE_2D') is not None]),
|
| 'SPL': safe_nanmean([s['SPL'] for s in metrics['sub_metrics'] if s['SPL'] is not None]),
|
| 'CESR': safe_nanmean([s['CESR'] for s in metrics['sub_metrics'] if s['CESR'] is not None]),
|
| 'PB': safe_nanmean([s['PB'] for s in metrics['sub_metrics'] if s['PB'] is not None]),
|
| 'CLS': safe_nanmean([s['CLS'] for s in metrics['sub_metrics'] if s['CLS'] is not None]),
|
| 'nDTW_3D': safe_nanmean([s['nDTW_3D'] for s in metrics['sub_metrics'] if s['nDTW_3D'] is not None]),
|
| 'Backward-nDTW_3D': safe_nanmean([s['Backward-nDTW_3D'] for s in metrics['sub_metrics'] if s['Backward-nDTW_3D'] is not None]),
|
| 'Bi-nDTW_3D': safe_nanmean([s['Bi-nDTW_3D'] for s in metrics['sub_metrics'] if s['Bi-nDTW_3D'] is not None]),
|
| 'sDTW_3D': safe_nanmean([s['sDTW_3D'] for s in metrics['sub_metrics'] if s['sDTW_3D'] is not None]),
|
| 'LCSS_3D': safe_nanmean([s['LCSS_3D'] for s in metrics['sub_metrics'] if s['LCSS_3D'] is not None]),
|
| 'EDR_3D': safe_nanmean([s['EDR_3D'] for s in metrics['sub_metrics'] if s['EDR_3D'] is not None]),
|
| 'ERP_3D': safe_nanmean([s['ERP_3D'] for s in metrics['sub_metrics'] if s['ERP_3D'] is not None]),
|
| 'Hausdorff_3D': safe_nanmean([s['Hausdorff_3D'] for s in metrics['sub_metrics'] if s['Hausdorff_3D'] is not None])
|
| }
|
|
|
| rows.append({
|
| 'Episode ID': ep_id,
|
| 'Type': 'Sub_Avg',
|
| 'Collision': None,
|
| **avg_sub
|
| })
|
|
|
|
|
| def calculate_global(values, key):
|
| valid = []
|
| for v in values:
|
| val = v.get(key)
|
| if val is not None and not np.isnan(val) and not np.isinf(val):
|
| valid.append(val)
|
| return np.nanmean(valid) if valid else None
|
|
|
|
|
| if all_full_metrics:
|
| global_full = {
|
| 'Episode ID': 'Global',
|
| 'Type': 'Full_Avg',
|
| 'PL': calculate_global(all_full_metrics, 'PL'),
|
| 'SPL': calculate_global(all_full_metrics, 'SPL'),
|
| 'PB': calculate_global(all_full_metrics, 'PB'),
|
| 'SR_3D-5': calculate_global(all_full_metrics, 'SR_3D-5'),
|
| 'SR_3D-10': calculate_global(all_full_metrics, 'SR_3D-10'),
|
| 'SR_3D-15': calculate_global(all_full_metrics, 'SR_3D-15'),
|
| 'SR_3D-15-Collision': calculate_global(all_full_metrics, 'SR_3D-15') if all_full_metrics[0].get('Collision', 0) == 0 else 0.0,
|
| 'SR_3D-20': calculate_global(all_full_metrics, 'SR_3D-20'),
|
| 'SR_3D-20-Collision': calculate_global(all_full_metrics, 'SR_3D-20') if all_full_metrics[0].get('Collision', 0) == 0 else 0.0,
|
| 'SR_3D-30': calculate_global(all_full_metrics, 'SR_3D-30'),
|
| 'SR_3D-40': calculate_global(all_full_metrics, 'SR_3D-40'),
|
| 'OSR_3D-5': calculate_global(all_full_metrics, 'OSR_3D-5'),
|
| 'OSR_3D-10': calculate_global(all_full_metrics, 'OSR_3D-10'),
|
| 'OSR_3D-15': calculate_global(all_full_metrics, 'OSR_3D-15'),
|
| 'OSR_3D-15-Collision': calculate_global(all_full_metrics, 'OSR_3D-15') if all_full_metrics[0].get('Collision', 0) == 0 else 0.0,
|
| 'OSR_3D-20': calculate_global(all_full_metrics, 'OSR_3D-20'),
|
| 'OSR_3D-20-Collision': calculate_global(all_full_metrics, 'OSR_3D-20') if all_full_metrics[0].get('Collision', 0) == 0 else 0.0,
|
| 'OSR_3D-30': calculate_global(all_full_metrics, 'OSR_3D-30'),
|
| 'OSR_3D-40': calculate_global(all_full_metrics, 'OSR_3D-40'),
|
| 'CLS': calculate_global(all_full_metrics, 'CLS'),
|
| 'NE_3D': calculate_global(all_full_metrics, 'NE_3D'),
|
| 'Min_Distance': calculate_global(all_full_metrics, 'Min_Distance'),
|
|
|
| 'Explore_Rate': calculate_global(all_full_metrics, 'Explore_Rate'),
|
| 'Explore_Ratio': calculate_global(all_full_metrics, 'Explore_Ratio'),
|
| 'nDTW_3D': calculate_global(all_full_metrics, 'nDTW_3D'),
|
| 'Sentence-nDTW_3D': calculate_global(all_full_metrics, 'Sentence-nDTW_3D'),
|
| 'sDTW_3D': calculate_global(all_full_metrics, 'sDTW_3D'),
|
| 'Backward-nDTW_3D': calculate_global(all_full_metrics, 'Backward-nDTW_3D'),
|
| 'Bi-nDTW_3D': calculate_global(all_full_metrics, 'Bi-nDTW_3D'),
|
| 'LCSS_3D': calculate_global(all_full_metrics, 'LCSS_3D'),
|
| 'EDR_3D': calculate_global(all_full_metrics, 'EDR_3D'),
|
| 'ERP_3D': calculate_global(all_full_metrics, 'ERP_3D'),
|
| 'Hausdorff_3D': calculate_global(all_full_metrics, 'Hausdorff_3D'),
|
| 'Action_Count': calculate_global(all_full_metrics, 'Action_Count'),
|
| 'Collision': calculate_global(all_full_metrics, 'Collision'),
|
| 'CESR': calculate_global(all_full_metrics, 'CESR'),
|
|
|
| 'SR_2D': calculate_global(all_full_metrics, 'SR_2D'),
|
| 'OSR_2D': calculate_global(all_full_metrics, 'OSR_2D'),
|
| 'NE_2D': calculate_global(all_full_metrics, 'NE_2D')
|
| }
|
| rows.append(global_full)
|
|
|
|
|
| if all_sub_metrics and ref_type == 2:
|
| global_sub = {
|
| 'Episode ID': 'Global',
|
| 'Type': 'Avg_of_All_Sub',
|
| 'PL': calculate_global(all_sub_metrics, 'PL'),
|
| 'SPL': calculate_global(all_sub_metrics, 'SPL'),
|
| 'PB': calculate_global(all_sub_metrics, 'PB'),
|
| 'SR_3D-5': calculate_global(all_sub_metrics, 'SR_3D-5'),
|
| 'SR_3D-10': calculate_global(all_sub_metrics, 'SR_3D-10'),
|
| 'SR_3D-15': calculate_global(all_sub_metrics, 'SR_3D-15'),
|
| 'SR_3D-20': calculate_global(all_sub_metrics, 'SR_3D-20'),
|
| 'SR_3D-30': calculate_global(all_sub_metrics, 'SR_3D-30'),
|
| 'SR_3D-40': calculate_global(all_sub_metrics, 'SR_3D-40'),
|
| 'OSR_3D-5': calculate_global(all_sub_metrics, 'OSR_3D-5'),
|
| 'OSR_3D-10': calculate_global(all_sub_metrics, 'OSR_3D-10'),
|
| 'OSR_3D-15': calculate_global(all_sub_metrics, 'OSR_3D-15'),
|
| 'OSR_3D-20': calculate_global(all_sub_metrics, 'OSR_3D-20'),
|
| 'OSR_3D-30': calculate_global(all_sub_metrics, 'OSR_3D-30'),
|
| 'OSR_3D-40': calculate_global(all_sub_metrics, 'OSR_3D-40'),
|
| 'CLS': calculate_global(all_sub_metrics, 'CLS'),
|
| 'NE_3D': calculate_global(all_sub_metrics, 'NE_3D'),
|
| 'NE-A': calculate_global(all_sub_metrics, 'NE-A'),
|
| 'nDTW_3D': calculate_global(all_sub_metrics, 'nDTW_3D'),
|
| 'sDTW_3D': calculate_global(all_sub_metrics, 'sDTW_3D'),
|
| 'Backward-nDTW_3D': calculate_global(all_sub_metrics, 'Backward-nDTW_3D'),
|
| 'Bi-nDTW_3D': calculate_global(all_sub_metrics, 'Bi-nDTW_3D'),
|
| 'LCSS_3D': calculate_global(all_sub_metrics, 'LCSS_3D'),
|
| 'EDR_3D': calculate_global(all_sub_metrics, 'EDR_3D'),
|
| 'ERP_3D': calculate_global(all_sub_metrics, 'ERP_3D'),
|
| 'Hausdorff_3D': calculate_global(all_sub_metrics, 'Hausdorff_3D'),
|
| 'Action_Count': calculate_global(all_sub_metrics, 'Action_Count'),
|
| 'Collision': None,
|
|
|
| 'SR_2D': calculate_global(all_sub_metrics, 'SR_2D'),
|
| 'OSR_2D': calculate_global(all_sub_metrics, 'OSR_2D'),
|
| 'NE_2D': calculate_global(all_sub_metrics, 'NE_2D'),
|
| 'CESR': calculate_global(all_sub_metrics, 'CESR')
|
| }
|
| rows.append(global_sub)
|
|
|
|
|
| if len(rows) > 0 and ref_type == 2:
|
|
|
| sub_avg_rows = [row for row in rows if row['Type'] == 'Sub_Avg']
|
|
|
| if sub_avg_rows:
|
| sub_avg_mean = {
|
| 'Episode ID': 'Global',
|
| 'Type': 'Avg_of_All_Sub_Avg',
|
| 'PL': safe_nanmean([r['PL'] for r in sub_avg_rows]),
|
| 'Action_Count': safe_nanmean([r['Action_Count'] for r in sub_avg_rows]),
|
| 'SR_3D-5': safe_nanmean([r['SR_3D-5'] for r in sub_avg_rows]),
|
| 'SR_3D-10': safe_nanmean([r['SR_3D-10'] for r in sub_avg_rows]),
|
| 'SR_3D-15': safe_nanmean([r['SR_3D-15'] for r in sub_avg_rows]),
|
| 'SR_3D-20': safe_nanmean([r['SR_3D-20'] for r in sub_avg_rows]),
|
| 'SR_3D-30': safe_nanmean([r['SR_3D-30'] for r in sub_avg_rows]),
|
| 'SR_3D-40': safe_nanmean([r['SR_3D-40'] for r in sub_avg_rows]),
|
| 'OSR_3D-5': safe_nanmean([r['OSR_3D-5'] for r in sub_avg_rows]),
|
| 'OSR_3D-10': safe_nanmean([r['OSR_3D-10'] for r in sub_avg_rows]),
|
| 'OSR_3D-15': safe_nanmean([r['OSR_3D-15'] for r in sub_avg_rows]),
|
| 'OSR_3D-20': safe_nanmean([r['OSR_3D-20'] for r in sub_avg_rows]),
|
| 'OSR_3D-30': safe_nanmean([r['OSR_3D-30'] for r in sub_avg_rows]),
|
| 'OSR_3D-40': safe_nanmean([r['OSR_3D-40'] for r in sub_avg_rows]),
|
| 'NE_3D': safe_nanmean([r['NE_3D'] for r in sub_avg_rows]),
|
| 'NE-A': safe_nanmean([r['NE-A'] for r in sub_avg_rows]),
|
| 'SPL': safe_nanmean([r['SPL'] for r in sub_avg_rows]),
|
| 'CESR': safe_nanmean([r['CESR'] for r in sub_avg_rows]),
|
| 'PB': safe_nanmean([r['PB'] for r in sub_avg_rows]),
|
| 'CLS': safe_nanmean([r['CLS'] for r in sub_avg_rows]),
|
| 'nDTW_3D': safe_nanmean([r['nDTW_3D'] for r in sub_avg_rows]),
|
| 'Backward-nDTW_3D': safe_nanmean([r['Backward-nDTW_3D'] for r in sub_avg_rows]),
|
| 'Bi-nDTW_3D': safe_nanmean([r['Bi-nDTW_3D'] for r in sub_avg_rows]),
|
| 'sDTW_3D': safe_nanmean([r['sDTW_3D'] for r in sub_avg_rows]),
|
| 'LCSS_3D': safe_nanmean([r['LCSS_3D'] for r in sub_avg_rows]),
|
| 'EDR_3D': safe_nanmean([r['EDR_3D'] for r in sub_avg_rows]),
|
| 'ERP_3D': safe_nanmean([r['ERP_3D'] for r in sub_avg_rows]),
|
| 'Hausdorff_3D': safe_nanmean([r['Hausdorff_3D'] for r in sub_avg_rows]),
|
| }
|
| rows.append(sub_avg_mean)
|
|
|
|
|
| seg_avg_rows = []
|
| if ref_type == 2:
|
| for seg_num in sorted(global_segment_store.keys()):
|
| metrics = global_segment_store[seg_num]
|
| seg_avg_rows.append({
|
| 'Episode ID': 'Global',
|
| 'Type': f'Seg{seg_num}_Avg',
|
| 'SR_3D-5': safe_nanmean(metrics['SR_3D-5']) if metrics['SR_3D-5'] else None,
|
| 'SR_3D-10': safe_nanmean(metrics['SR_3D-10']) if metrics['SR_3D-10'] else None,
|
| 'SR_3D-15': safe_nanmean(metrics['SR_3D-15']) if metrics['SR_3D-15'] else None,
|
| 'SR_3D-20': safe_nanmean(metrics['SR_3D-20']) if metrics['SR_3D-20'] else None,
|
| 'SR_3D-30': safe_nanmean(metrics['SR_3D-30']) if metrics['SR_3D-30'] else None,
|
| 'SR_3D-40': safe_nanmean(metrics['SR_3D-40']) if metrics['SR_3D-40'] else None,
|
| 'OSR_3D-5': safe_nanmean(metrics['OSR_3D-5']) if metrics['OSR_3D-5'] else None,
|
| 'OSR_3D-10': safe_nanmean(metrics['OSR_3D-10']) if metrics['OSR_3D-10'] else None,
|
| 'OSR_3D-15': safe_nanmean(metrics['OSR_3D-15']) if metrics['OSR_3D-15'] else None,
|
| 'OSR_3D-20': safe_nanmean(metrics['OSR_3D-20']) if metrics['OSR_3D-20'] else None,
|
| 'OSR_3D-30': safe_nanmean(metrics['OSR_3D-30']) if metrics['OSR_3D-30'] else None,
|
| 'OSR_3D-40': safe_nanmean(metrics['OSR_3D-40']) if metrics['OSR_3D-40'] else None,
|
| 'SPL': safe_nanmean(metrics['SPL']) if metrics['SPL'] else None,
|
| 'NE_3D': safe_nanmean(metrics['NE_3D']) if metrics['NE_3D'] else None,
|
| 'NE-A': safe_nanmean(metrics['NE-A']) if metrics['NE-A'] else None
|
| })
|
|
|
|
|
| explore_groups = {
|
| 'explore-group-1': [],
|
| 'explore-group-2': [],
|
| 'explore-group-3': []
|
| }
|
|
|
|
|
| if ref_type == 2:
|
| for ep_id, data in results.items():
|
| ratio = data['full_metrics']['Explore_Ratio']
|
| if ratio <= 0.4:
|
| explore_groups['explore-group-1'].append(data)
|
| elif ratio <= 0.6:
|
| explore_groups['explore-group-2'].append(data)
|
| else:
|
| explore_groups['explore-group-3'].append(data)
|
|
|
|
|
| group_metrics = []
|
| for group_name, group_data in explore_groups.items():
|
| if not group_data:
|
| continue
|
|
|
|
|
| full_metrics_list = [d['full_metrics'] for d in group_data]
|
| sub_metrics_list = [m for d in group_data for m in d['sub_metrics']]
|
|
|
|
|
| def safe_mean(values, key):
|
| valid = [v[key] for v in values if v[key] is not None]
|
| return np.nanmean(valid) if valid else None
|
|
|
|
|
| group_full_avg = {
|
| 'Group': group_name,
|
| 'Type': 'Full_Avg',
|
| 'Count': len(full_metrics_list),
|
| 'PL': safe_mean(full_metrics_list, 'PL'),
|
| 'Action_Count': safe_mean(full_metrics_list, 'Action_Count'),
|
| 'SR_3D-20': safe_mean(full_metrics_list, 'SR_3D-20'),
|
| 'SR_3D-30': safe_mean(full_metrics_list, 'SR_3D-30'),
|
| 'SR_3D-40': safe_mean(full_metrics_list, 'SR_3D-40'),
|
| 'OSR_3D-20': safe_mean(full_metrics_list, 'OSR_3D-20'),
|
| 'OSR_3D-30': safe_mean(full_metrics_list, 'OSR_3D-30'),
|
| 'OSR_3D-40': safe_mean(full_metrics_list, 'OSR_3D-40'),
|
| 'NE_3D': safe_mean(full_metrics_list, 'NE_3D'),
|
|
|
| 'SPL': safe_mean(full_metrics_list, 'SPL'),
|
| 'CESR': safe_mean(full_metrics_list, 'CESR'),
|
| 'PB': safe_mean(full_metrics_list, 'PB'),
|
| 'CLS': safe_mean(full_metrics_list, 'CLS'),
|
| 'nDTW_3D': safe_mean(full_metrics_list, 'nDTW_3D'),
|
| 'sDTW_3D': safe_mean(full_metrics_list, 'sDTW_3D'),
|
| 'Backward-nDTW_3D': safe_mean(full_metrics_list, 'Backward-nDTW_3D'),
|
| 'Bi-nDTW_3D': safe_mean(full_metrics_list, 'Bi-nDTW_3D'),
|
| 'LCSS_3D': safe_mean(full_metrics_list, 'LCSS_3D'),
|
| 'EDR_3D': safe_mean(full_metrics_list, 'EDR_3D'),
|
| 'ERP_3D': safe_mean(full_metrics_list, 'ERP_3D'),
|
| 'Hausdorff_3D': safe_mean(full_metrics_list, 'Hausdorff_3D'),
|
| 'Collision': safe_mean(full_metrics_list, 'Collision'),
|
| 'Explore_Ratio': np.mean([d['full_metrics']['Explore_Ratio'] for d in group_data]),
|
| 'Explore_Rate': np.mean([d['full_metrics']['Explore_Rate'] for d in group_data])
|
| }
|
| group_metrics.append(group_full_avg)
|
|
|
|
|
| if sub_metrics_list:
|
| group_sub_avg = {
|
| 'Group': group_name,
|
| 'Type': 'Sub_Avg',
|
| 'Count': len(sub_metrics_list),
|
| 'PL': safe_mean(sub_metrics_list, 'PL'),
|
| 'Action_Count': safe_mean(sub_metrics_list, 'Action_Count'),
|
| 'SR_3D-20': safe_mean(sub_metrics_list, 'SR_3D-20'),
|
| 'SR_3D-30': safe_mean(sub_metrics_list, 'SR_3D-30'),
|
| 'SR_3D-40': safe_mean(sub_metrics_list, 'SR_3D-40'),
|
| 'OSR_3D-20': safe_mean(sub_metrics_list, 'OSR_3D-20'),
|
| 'OSR_3D-30': safe_mean(sub_metrics_list, 'OSR_3D-30'),
|
| 'OSR_3D-40': safe_mean(sub_metrics_list, 'OSR_3D-40'),
|
| 'NE_3D': safe_mean(sub_metrics_list, 'NE_3D'),
|
| 'NE-A': safe_mean(sub_metrics_list, 'NE-A'),
|
| 'SPL': safe_mean(sub_metrics_list, 'SPL'),
|
| 'CESR': safe_mean(sub_metrics_list, 'CESR'),
|
| 'PB': safe_mean(sub_metrics_list, 'PB'),
|
| 'CLS': safe_mean(sub_metrics_list, 'CLS'),
|
| 'nDTW_3D': safe_mean(sub_metrics_list, 'nDTW_3D'),
|
| 'Backward-nDTW_3D': safe_mean(sub_metrics_list, 'Backward-nDTW_3D'),
|
| 'Bi-nDTW_3D': safe_mean(sub_metrics_list, 'Bi-nDTW_3D'),
|
| 'sDTW_3D': safe_mean(sub_metrics_list, 'sDTW_3D'),
|
| 'LCSS_3D': safe_mean(sub_metrics_list, 'LCSS_3D'),
|
| 'EDR_3D': safe_mean(sub_metrics_list, 'EDR_3D'),
|
| 'ERP_3D': safe_mean(sub_metrics_list, 'ERP_3D'),
|
| 'Hausdorff_3D': safe_mean(sub_metrics_list, 'Hausdorff_3D'),
|
| 'Collision': None,
|
| 'Explore_Ratio': None,
|
| 'Explore_Rate': None
|
| }
|
| group_metrics.append(group_sub_avg)
|
|
|
|
|
| group_df = pd.DataFrame(group_metrics, columns=[
|
| 'Group', 'Type', 'Count', 'Explore_Rate', 'PL', 'Action_Count',
|
| 'SR_3D-5', 'SR_3D-10', 'SR_3D-15', 'SR_3D-15-Collision', 'SR_3D-20', 'SR_3D-20-Collision',
|
| 'OSR_3D-5', 'OSR_3D-10', 'OSR_3D-15', 'OSR_3D-15-Collision', 'OSR_3D-20', 'OSR_3D-20-Collision',
|
| 'NE_3D', 'NE-A', 'Min_Distance', 'SPL', 'CESR', 'PB',
|
| 'CLS', 'nDTW_3D', 'Backward-nDTW_3D', 'Bi-nDTW_3D', 'sDTW_3D',
|
| 'LCSS_3D', 'EDR_3D', 'ERP_3D', 'Hausdorff_3D', 'Collision', 'Explore_Ratio'
|
| ])
|
|
|
|
|
|
|
| rows += seg_avg_rows
|
|
|
|
|
| global_rows = [row for row in rows if row["Episode ID"] == "Global"]
|
| non_global_rows = [row for row in rows if row["Episode ID"] != "Global"]
|
|
|
|
|
| sorted_rows = global_rows + non_global_rows
|
|
|
|
|
| df = pd.DataFrame(sorted_rows, columns=columns_order)
|
|
|
|
|
| significance_summary = []
|
| significance_json = {}
|
| if 'has_pair' in locals() and has_pair:
|
|
|
| if ref_type_b == 1:
|
| gt_data_b = load_reference_trajectory_type1(gt_folder_b)
|
| else:
|
| gt_data_b = load_groundtruth_type2(gt_folder_b)
|
| pred_data_b = load_predictions(mode_b, pred_path_b)
|
|
|
|
|
| episode_groups_b = {}
|
| for ep_id_b, preds_b in pred_data_b.items():
|
| for pb in preds_b:
|
| episode_groups_b.setdefault(ep_id_b, []).append({'key': ep_id_b, 'pred': pb, 'ep_id': ep_id_b})
|
|
|
| temp_results_b = {}
|
| for ep_id_b, trajectories_b in tqdm(episode_groups_b.items(), desc="处理每组轨迹[B]"):
|
| if ep_id_b not in gt_data_b:
|
| continue
|
| best_traj_b = None
|
| best_metrics_b = None
|
| for i_b, traj_b in enumerate(trajectories_b):
|
| try:
|
| sub_b, full_b = evaluate_trajectory(gt_data_b[ep_id_b], traj_b['pred'], ref_type_b)
|
| cur_b = {
|
| 'sr_3d_20': full_b.get('SR_3D-20', 0),
|
| 'osr_3d_20': full_b.get('OSR_3D-20', 0),
|
| 'ne_3d': full_b.get('NE_3D', float('inf')),
|
| 'sub_metrics': sub_b,
|
| 'full_metrics': full_b,
|
| 'traj_data': traj_b,
|
| 'index': i_b
|
| }
|
| if best_traj_b is None or is_better_trajectory(cur_b, best_metrics_b):
|
| best_traj_b = traj_b
|
| best_metrics_b = cur_b
|
| except Exception:
|
| continue
|
| if best_traj_b is not None and best_metrics_b is not None:
|
| temp_results_b[ep_id_b] = best_metrics_b
|
|
|
|
|
| overlap_eps = sorted(set(results.keys()).intersection(set(temp_results_b.keys())))
|
| if overlap_eps:
|
|
|
| metrics_to_eval = ['SR_2D', 'SR_3D-20', 'OSR_3D-20', 'NE_3D', 'nDTW_3D', 'sDTW_3D']
|
|
|
| try:
|
| n_boot = int(os.environ.get('EVAL_BOOTSTRAP_N', '2000'))
|
| except Exception:
|
| n_boot = 2000
|
| try:
|
| ci_level = float(os.environ.get('EVAL_CI_LEVEL', '0.95'))
|
| except Exception:
|
| ci_level = 0.95
|
|
|
| name_map = {}
|
| pair_method = str(PAIR_RESAMPLE_METHOD).lower()
|
| for m in metrics_to_eval:
|
| a_vals = [results[e]['full_metrics'].get(m) for e in overlap_eps]
|
| b_vals = [temp_results_b[e]['full_metrics'].get(m) for e in overlap_eps]
|
| paired = [(a, b) for a, b in zip(a_vals, b_vals) if (a is not None and b is not None and not np.isnan(a) and not np.isnan(b) and not np.isinf(a) and not np.isinf(b))]
|
| if not paired:
|
| continue
|
| a_arr = np.array([p[0] for p in paired], dtype=float)
|
| b_arr = np.array([p[1] for p in paired], dtype=float)
|
| diffs = a_arr - b_arr
|
| stat_diff = bootstrap_stats(diffs, n_boot=n_boot, ci=ci_level)
|
| if pair_method == 'bootstrap':
|
| p_val = paired_bootstrap_p(diffs, n_boot=n_boot)
|
| p_method = 'Bootstrap'
|
| elif pair_method in ('perm', 'permutation', 'paired-permutation'):
|
| p_val = paired_permutation_p(a_arr, b_arr, n_perm=n_boot)
|
| p_method = 'Paired-Permutation'
|
| else:
|
| p_val = paired_signflip_p(diffs, n_perm=n_boot)
|
| p_method = 'Sign-Flip'
|
| disp_metric = name_map.get(m, m)
|
| row = {
|
| 'Metric': disp_metric,
|
| 'N_pairs': stat_diff['n'],
|
| 'Mean_A': float(np.mean(a_arr)),
|
| 'Mean_B': float(np.mean(b_arr)),
|
| 'Mean_Diff(A-B)': stat_diff['mean'],
|
| 'Std_Diff': stat_diff['std'],
|
| 'SE_Boot_Diff': stat_diff['se_boot'],
|
| 'CI_Diff_Low': stat_diff['ci_low'],
|
| 'CI_Diff_High': stat_diff['ci_high'],
|
| 'CI_Level': stat_diff['ci_level'],
|
| 'P_Method': p_method,
|
| 'P_Value': p_val,
|
| 'Direction': 'higher-better' if m in ['SR_2D','SR_3D-20','OSR_3D-20','nDTW_3D','sDTW_3D'] else 'lower-better',
|
| 'Episodes_Overlap': len(overlap_eps)
|
| }
|
| significance_summary.append(row)
|
| significance_json[disp_metric] = row
|
|
|
|
|
| def convert_to_nested_json(df):
|
| """将平铺的DataFrame转换为嵌套的JSON结构"""
|
| nested = {"Global": {}, "episodes": {}}
|
|
|
|
|
| global_rows = df[df['Episode ID'] == 'Global']
|
| for _, row in global_rows.iterrows():
|
| metrics = row.dropna().to_dict()
|
| metric_type = metrics.pop('Type')
|
| nested["Global"][metric_type] = metrics
|
|
|
|
|
| episode_rows = df[df['Episode ID'] != 'Global']
|
| for ep_id, group in episode_rows.groupby('Episode ID'):
|
| ep_dict = {}
|
| for _, row in group.iterrows():
|
| row_dict = row.dropna().to_dict()
|
| metric_type = row_dict.pop('Type')
|
| ep_dict[metric_type] = row_dict
|
| nested["episodes"][ep_id] = ep_dict
|
|
|
| return nested
|
|
|
|
|
| nested_data = convert_to_nested_json(df)
|
| if significance_summary:
|
| nested_data['Global']['Significance'] = significance_json
|
|
|
|
|
|
|
| try:
|
| n_boot = int(os.environ.get('EVAL_BOOTSTRAP_N', '2000'))
|
| except Exception:
|
| n_boot = 2000
|
| try:
|
| ci_level = float(os.environ.get('EVAL_CI_LEVEL', '0.95'))
|
| except Exception:
|
| ci_level = 0.95
|
|
|
| null_map = None
|
| try:
|
| env_nulls = os.environ.get('EVAL_NULLS')
|
| if env_nulls:
|
| null_map = json.loads(env_nulls)
|
| except Exception:
|
| null_map = None
|
|
|
|
|
|
|
| metrics_to_eval = ['SR_2D', 'SR_3D-20', 'OSR_3D-20', 'NE_3D', 'nDTW_3D', 'sDTW_3D', 'PL', 'Action_Count']
|
|
|
| def collect_metric(values, key):
|
| arr = []
|
| for v in values:
|
| if key in v:
|
| arr.append(v.get(key))
|
| return arr
|
|
|
| uncertainty_summary = []
|
| uncertainty_json = {}
|
| name_map_unc = {'Action_Count': 'Steps'}
|
| for m in metrics_to_eval:
|
| vals = collect_metric(all_full_metrics, m)
|
| stats = bootstrap_stats(vals, n_boot=n_boot, ci=ci_level)
|
| p_val = None
|
| if null_map and m in null_map:
|
| try:
|
| p_val = bootstrap_one_sample_p(vals, null_map[m], n_boot=n_boot)
|
| except Exception:
|
| p_val = None
|
| disp_m = name_map_unc.get(m, m)
|
| row = {
|
| 'Metric': disp_m,
|
| 'N': stats['n'],
|
| 'Mean': stats['mean'],
|
| 'Std': stats['std'],
|
| 'SE_Boot': stats['se_boot'],
|
| 'CI_Low': stats['ci_low'],
|
| 'CI_High': stats['ci_high'],
|
| 'CI_Level': stats['ci_level'],
|
| 'Null': (null_map[m] if (null_map and m in null_map) else None),
|
| 'P_Value_TwoSided': p_val
|
| }
|
| uncertainty_summary.append(row)
|
| uncertainty_json[disp_m] = row
|
|
|
|
|
| nested_data.setdefault('Global', {})
|
| nested_data['Global']['Uncertainty'] = uncertainty_json
|
|
|
|
|
| json_output_path = os.path.splitext(output_file)[0] + '_structured.json'
|
|
|
| def json_serializer(obj):
|
| if isinstance(obj, np.generic):
|
| return float(obj.item())
|
| if isinstance(obj, float):
|
| if np.isinf(obj) or np.isnan(obj):
|
| return None
|
| return obj
|
|
|
| with open(json_output_path, 'w', encoding='utf-8') as f:
|
| json.dump(nested_data, f, indent=4, default=json_serializer, ensure_ascii=False)
|
|
|
| print(f"结构化JSON结果已保存至 {json_output_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| writer = pd.ExcelWriter(output_file, engine='xlsxwriter')
|
| df.to_excel(writer, index=False, sheet_name='Results')
|
| if ref_type == 2:
|
| group_df.to_excel(writer, index=False, sheet_name='Explore_Group_Results')
|
|
|
|
|
| if ref_type == 1 and segcat_mapping:
|
|
|
| cat_to_eps = defaultdict(set)
|
| for (ep, _seg), cats in segcat_mapping.items():
|
| for c in cats:
|
| cat_to_eps[str(c)].add(str(ep))
|
|
|
| full_rows_df = df[df['Type'] == 'Full'].copy()
|
|
|
| numeric_cols = [
|
| 'PL','Action_Count','Explore_Rate',
|
| 'SR_3D-5','SR_3D-10','SR_3D-15','SR_3D-20','SR_3D-30','SR_3D-40',
|
| 'OSR_3D-5','OSR_3D-10','OSR_3D-15','OSR_3D-20','OSR_3D-30','OSR_3D-40',
|
| 'NE_3D','NE-A','nDTW_3D','sDTW_3D','SPL','CESR','PB','CLS',
|
| 'Backward-nDTW_3D','Bi-nDTW_3D','LCSS_3D','EDR_3D','ERP_3D','Hausdorff_3D',
|
| 'SR_2D','OSR_2D','NE_2D','Explore_Ratio','Min_Distance'
|
| ]
|
| created_sheets_full = 0
|
| total_full_rows = 0
|
| category_columns_order_full = ['Episode ID', 'Type'] + [c for c in columns_order if c not in ('Episode ID', 'Type')]
|
| for cat_name, eps in cat_to_eps.items():
|
| if not eps:
|
| continue
|
| cat_df = full_rows_df[full_rows_df['Episode ID'].astype(str).isin(eps)].copy()
|
| if cat_df.empty:
|
| continue
|
|
|
| avg_row: dict[str, object] = {'Episode ID': 'Global', 'Type': f'{cat_name}_Full_Avg'}
|
| for c in numeric_cols:
|
| if c in cat_df.columns:
|
| try:
|
| vals = [v for v in cat_df[c].values if v is not None and not (isinstance(v, float) and (np.isnan(v) or np.isinf(v)))]
|
| avg_row[c] = float(np.mean(vals)) if vals else None
|
| except Exception:
|
| avg_row[c] = None
|
| try:
|
| cat_df_sorted = cat_df.sort_values(by=['Episode ID'], ascending=[True], kind='mergesort', na_position='last')
|
| except Exception:
|
| cat_df_sorted = cat_df
|
| cat_df_out = pd.concat([pd.DataFrame([avg_row]), cat_df_sorted], ignore_index=True, sort=False)
|
| ordered_cols = [c for c in category_columns_order_full if c in cat_df_out.columns]
|
| ordered_cols += [c for c in cat_df_out.columns if c not in ordered_cols]
|
| cat_df_out = cat_df_out.reindex(columns=ordered_cols)
|
| sheet = _sanitize_sheet_name(f"CAT_{cat_name}_FULL")
|
| cat_df_out.to_excel(writer, index=False, sheet_name=sheet)
|
| ws = writer.sheets[sheet]
|
| ws.freeze_panes(1, 0)
|
| for col_num, value in enumerate(cat_df_out.columns.values):
|
| try:
|
| width = max(len(str(value)), cat_df_out[value].astype(str).map(len).max()) + 2
|
| except Exception:
|
| width = max(len(str(value)), 12)
|
| ws.set_column(col_num, col_num, width)
|
| created_sheets_full += 1
|
| total_full_rows += len(cat_df)
|
| if created_sheets_full > 0:
|
| print(f"Full-level category worksheets created: {created_sheets_full}, total rows assigned: {total_full_rows}")
|
|
|
|
|
| if ref_type == 2 and segcat_mapping:
|
|
|
| numeric_cols_common = [
|
| 'PL','Action_Count','Explore_Rate',
|
| 'SR_3D-5','SR_3D-10','SR_3D-15','SR_3D-20','SR_3D-30','SR_3D-40',
|
| 'OSR_3D-5','OSR_3D-10','OSR_3D-15','OSR_3D-20','OSR_3D-30','OSR_3D-40',
|
| 'NE_3D','NE-A','nDTW_3D','sDTW_3D','SPL','CESR','PB','CLS',
|
| 'Backward-nDTW_3D','Bi-nDTW_3D','LCSS_3D','EDR_3D','ERP_3D','Hausdorff_3D',
|
| 'SR_2D','OSR_2D','NE_2D','Explore_Ratio','Min_Distance'
|
| ]
|
| if CAT_SHEET_LEVEL_MODE2 == 'sentence':
|
|
|
| sub_rows_df = df[df['Type'].astype(str).str.startswith('Seg_')].copy()
|
|
|
| def _parse_seg_idx(val):
|
| try:
|
| if isinstance(val, str) and val.startswith('Seg_'):
|
| return int(val.split('_',1)[1])
|
| except Exception:
|
| return None
|
| return None
|
| sub_rows_df['Seg_Idx'] = sub_rows_df['Type'].apply(_parse_seg_idx)
|
|
|
| cat_buckets = defaultdict(list)
|
| for _, row in sub_rows_df.iterrows():
|
| ep = str(row['Episode ID'])
|
| seg_i = row['Seg_Idx']
|
| if seg_i is None:
|
| continue
|
| cats = segcat_mapping.get((ep, seg_i), set())
|
| for cat in cats:
|
| cat_buckets[cat].append(row.drop(labels=['Seg_Idx']).to_dict())
|
|
|
| total_cat_rows = 0
|
| created_sheets = 0
|
|
|
| category_columns_order = ['Episode ID', 'Segment', 'Type'] + [c for c in columns_order if c not in ('Episode ID', 'Type')]
|
| for cat_name, rows_list in cat_buckets.items():
|
| if not rows_list:
|
| continue
|
|
|
| processed_rows = []
|
| for r in rows_list:
|
| d = dict(r)
|
| seg_val = None
|
| if 'Seg_Idx' in d and d['Seg_Idx'] is not None:
|
| seg_val = d['Seg_Idx']
|
| else:
|
| t = d.get('Type')
|
| if isinstance(t, str) and t.startswith('Seg_'):
|
| try:
|
| seg_val = int(t.split('_', 1)[1])
|
| except Exception:
|
| seg_val = None
|
| d['Segment'] = seg_val
|
| d.pop('Seg_Idx', None)
|
| processed_rows.append(d)
|
| cat_df = pd.DataFrame(processed_rows)
|
|
|
| avg_row = {'Episode ID': 'Global', 'Segment': None, 'Type': f'{cat_name}_Sub_Avg'}
|
| for c in numeric_cols_common:
|
| if c in cat_df.columns:
|
| try:
|
| vals = [v for v in cat_df[c].values if v is not None and not (isinstance(v, float) and (np.isnan(v) or np.isinf(v)))]
|
| avg_row[c] = float(np.mean(vals)) if vals else None
|
| except Exception:
|
| avg_row[c] = None
|
|
|
| try:
|
| cat_df_sorted = cat_df.sort_values(by=['Episode ID','Segment'], ascending=[True, True], kind='mergesort', na_position='last')
|
| except Exception:
|
| cat_df_sorted = cat_df
|
| cat_df_out = pd.concat([pd.DataFrame([avg_row]), cat_df_sorted], ignore_index=True, sort=False)
|
| ordered_cols = [c for c in category_columns_order if c in cat_df_out.columns]
|
| ordered_cols += [c for c in cat_df_out.columns if c not in ordered_cols]
|
| cat_df_out = cat_df_out.reindex(columns=ordered_cols)
|
| sheet = _sanitize_sheet_name(f"CAT_{cat_name}")
|
| cat_df_out.to_excel(writer, index=False, sheet_name=sheet)
|
| ws = writer.sheets[sheet]
|
| ws.freeze_panes(1, 0)
|
| for col_num, value in enumerate(cat_df_out.columns.values):
|
| try:
|
| width = max(len(str(value)), cat_df_out[value].astype(str).map(len).max()) + 2
|
| except Exception:
|
| width = max(len(str(value)), 12)
|
| ws.set_column(col_num, col_num, width)
|
| total_cat_rows += len(rows_list)
|
| created_sheets += 1
|
| if created_sheets > 0:
|
| print(f"Segment category worksheets created: {created_sheets}, total rows assigned: {total_cat_rows}")
|
| elif CAT_SHEET_LEVEL_MODE2 == 'full':
|
|
|
| cat_to_eps = defaultdict(set)
|
| for (ep, _seg), cats in segcat_mapping.items():
|
| for c in cats:
|
| cat_to_eps[str(c)].add(str(ep))
|
| full_rows_df = df[df['Type'] == 'Full'].copy()
|
| created_sheets_full = 0
|
| total_full_rows = 0
|
| category_columns_order_full = ['Episode ID', 'Type'] + [c for c in columns_order if c not in ('Episode ID', 'Type')]
|
| for cat_name, eps in cat_to_eps.items():
|
| if not eps:
|
| continue
|
| cat_df = full_rows_df[full_rows_df['Episode ID'].astype(str).isin(eps)].copy()
|
| if cat_df.empty:
|
| continue
|
|
|
| avg_row: dict[str, object] = {'Episode ID': 'Global', 'Type': f'{cat_name}_Full_Avg'}
|
| for c in numeric_cols_common:
|
| if c in cat_df.columns:
|
| try:
|
| vals = [v for v in cat_df[c].values if v is not None and not (isinstance(v, float) and (np.isnan(v) or np.isinf(v)))]
|
| avg_row[c] = float(np.mean(vals)) if vals else None
|
| except Exception:
|
| avg_row[c] = None
|
| try:
|
| cat_df_sorted = cat_df.sort_values(by=['Episode ID'], ascending=[True], kind='mergesort', na_position='last')
|
| except Exception:
|
| cat_df_sorted = cat_df
|
| cat_df_out = pd.concat([pd.DataFrame([avg_row]), cat_df_sorted], ignore_index=True, sort=False)
|
| ordered_cols = [c for c in category_columns_order_full if c in cat_df_out.columns]
|
| ordered_cols += [c for c in cat_df_out.columns if c not in ordered_cols]
|
| cat_df_out = cat_df_out.reindex(columns=ordered_cols)
|
| sheet = _sanitize_sheet_name(f"CAT_{cat_name}")
|
| cat_df_out.to_excel(writer, index=False, sheet_name=sheet)
|
| ws = writer.sheets[sheet]
|
| ws.freeze_panes(1, 0)
|
| for col_num, value in enumerate(cat_df_out.columns.values):
|
| try:
|
| width = max(len(str(value)), cat_df_out[value].astype(str).map(len).max()) + 2
|
| except Exception:
|
| width = max(len(str(value)), 12)
|
| ws.set_column(col_num, col_num, width)
|
| created_sheets_full += 1
|
| total_full_rows += len(cat_df)
|
| if created_sheets_full > 0:
|
| print(f"Full-level category worksheets (mode2) created: {created_sheets_full}, total rows assigned: {total_full_rows}")
|
|
|
| uncertainty_df = pd.DataFrame(uncertainty_summary, columns=[
|
| 'Metric', 'N', 'Mean', 'Std', 'SE_Boot', 'CI_Low', 'CI_High', 'CI_Level', 'Null', 'P_Value_TwoSided'
|
| ])
|
| uncertainty_df.to_excel(writer, index=False, sheet_name='Uncertainty')
|
|
|
| if significance_summary:
|
| significance_df = pd.DataFrame(significance_summary, columns=[
|
| 'Metric','N_pairs','Mean_A','Mean_B','Mean_Diff(A-B)','Std_Diff','SE_Boot_Diff','CI_Diff_Low','CI_Diff_High','CI_Level','P_Method','P_Value','Direction','Episodes_Overlap'
|
| ])
|
| significance_df.to_excel(writer, index=False, sheet_name='Significance')
|
|
|
|
|
| workbook = writer.book
|
|
|
|
|
| results_worksheet = writer.sheets['Results']
|
| results_worksheet.freeze_panes(1, 0)
|
| for col_num, value in enumerate(df.columns.values):
|
| col_width = max(len(str(value)), df[value].astype(str).map(len).max()) + 2
|
| results_worksheet.set_column(col_num, col_num, col_width)
|
|
|
|
|
| uncertainty_ws = writer.sheets['Uncertainty']
|
| uncertainty_ws.freeze_panes(1, 0)
|
| for col_num, value in enumerate(uncertainty_df.columns.values):
|
| col_width = max(len(str(value)), uncertainty_df[value].astype(str).map(len).max()) + 2
|
| uncertainty_ws.set_column(col_num, col_num, col_width)
|
|
|
| if 'Significance' in writer.sheets:
|
| sig_ws = writer.sheets['Significance']
|
| sig_ws.freeze_panes(1, 0)
|
| cols = ['Metric','N_pairs','Mean_A','Mean_B','Mean_Diff(A-B)','Std_Diff','SE_Boot_Diff','CI_Diff_Low','CI_Diff_High','CI_Level','P_Method','P_Value','Direction','Episodes_Overlap']
|
| for i, name in enumerate(cols):
|
| sig_ws.set_column(i, i, max(len(name), 14))
|
|
|
| if ref_type == 2:
|
|
|
| group_worksheet = writer.sheets['Explore_Group_Results']
|
| group_worksheet.freeze_panes(1, 0)
|
| for col_num, value in enumerate(group_df.columns.values):
|
| col_width = max(len(str(value)), group_df[value].astype(str).map(len).max()) + 2
|
| group_worksheet.set_column(col_num, col_num, col_width)
|
|
|
|
|
| writer.close()
|
| print(f"\n评估结果已保存至 {output_file}")
|
|
|
|
|
| print("\n" + "="*50)
|
| print(f"真实轨迹总数: {total_gt}")
|
| print(f"预测轨迹总数: {total_pred}")
|
| print(f"成功计算轨迹数: {computed} (覆盖率: {computed/total_pred:.1%})")
|
| print("="*50 + "\n")
|
| else:
|
| print("\n无有效结果可导出")
|
|
|
| def is_better_trajectory(current_metrics, best_metrics):
|
| """
|
| 判断当前轨迹是否比最佳轨迹更好
|
| 优先级:1) SR_3D-20高 2) OSR_3D-20高 3) NE_3D低
|
| """
|
| if best_metrics is None:
|
| return True
|
| if current_metrics['sr_3d_20'] > best_metrics['sr_3d_20']:
|
| return True
|
| elif current_metrics['sr_3d_20'] < best_metrics['sr_3d_20']:
|
| return False
|
| if current_metrics['osr_3d_20'] > best_metrics['osr_3d_20']:
|
| return True
|
| elif current_metrics['osr_3d_20'] < best_metrics['osr_3d_20']:
|
| return False
|
| if current_metrics['ne_3d'] < best_metrics['ne_3d']:
|
| return True
|
| return False
|
|
|
| def compare_trajectories(better_metrics, worse_metrics):
|
| """
|
| 返回轨迹优选的原因描述
|
| """
|
| if better_metrics['sr_3d_20'] > worse_metrics['sr_3d_20']:
|
| return f"SR_3D-20更高 ({better_metrics['sr_3d_20']:.3f} > {worse_metrics['sr_3d_20']:.3f})"
|
| elif better_metrics['osr_3d_20'] > worse_metrics['osr_3d_20']:
|
| return f"OSR_3D-20更高 ({better_metrics['osr_3d_20']:.3f} > {worse_metrics['osr_3d_20']:.3f})"
|
| elif better_metrics['ne_3d'] < worse_metrics['ne_3d']:
|
| return f"NE_3D更低 ({better_metrics['ne_3d']:.3f} < {worse_metrics['ne_3d']:.3f})"
|
| else:
|
| return "指标相近"
|
|
|
| if __name__ == "__main__":
|
| main() |