forked from longtao.wu/droboticflow
✨ Add UBT MCAP quality pipeline
This commit is contained in:
parent
71114db6e1
commit
81185f6d6f
49
lightweight-operators/ubt-mcap-pipeline/README.md
Normal file
49
lightweight-operators/ubt-mcap-pipeline/README.md
Normal file
@ -0,0 +1,49 @@
|
||||
# UBT MCAP pipeline
|
||||
|
||||
This lightweight operator package implements three stages over one shared
|
||||
Python 3.10 CPU image:
|
||||
|
||||
1. `python main.py prepare`
|
||||
2. `python main.py check`
|
||||
3. `python main.py report`
|
||||
|
||||
The code is mounted from
|
||||
`lightweight-operators/ubt-mcap-pipeline`. Prepared data is persisted under
|
||||
`/data/ubt-prepared/<dataset-id>/<fingerprint>` on the shared workspace PVC.
|
||||
Only manifests and reports flow through the artifact bus; raw episodes are not
|
||||
copied between workflow nodes.
|
||||
|
||||
The checker source is the minimal UBT-specific subset of RoboOrchard commit
|
||||
`c742b39e0821590605385ee4ddabcafb258432d8`. Data files, URDF files, credentials,
|
||||
and generated reports are intentionally excluded from this repository.
|
||||
|
||||
## Runtime parameters
|
||||
|
||||
Prepare:
|
||||
|
||||
- `DATASET_ID`
|
||||
- `USER_NAME`
|
||||
- `TASK_NAME`
|
||||
- `ARCHIVE_GLOB`
|
||||
- `URDF_SOURCE_PATH`
|
||||
- `REUSE_PREPARED=true`
|
||||
- `MIN_FREE_GIB=50`
|
||||
- `ON_DUPLICATE_EPISODE=error`
|
||||
|
||||
Check:
|
||||
|
||||
- `DATE_PREFIXES`
|
||||
- `USER_NAMES`
|
||||
- `TASK_NAMES`
|
||||
- `NUM_WORKERS=1`
|
||||
- `MAX_EPISODES=0`
|
||||
- `INSPECT_CONFIG_JSON`
|
||||
- `VIDEO_EXPORT=false`
|
||||
- `VIS_INTERVAL=3`
|
||||
- `VIS_RESIZE_SCALE=1.0`
|
||||
- `VIS_DEPTH=true`
|
||||
- `FAIL_ON_QUALITY_ERROR=false`
|
||||
|
||||
Report output paths are rooted at
|
||||
`results/<dataset>/<date-range>/<run-id>/`; the existing result uploader adds
|
||||
the configured BOS prefix.
|
||||
@ -0,0 +1,21 @@
|
||||
# Third-party notices
|
||||
|
||||
The files below are copied from the internal RoboOrchard project at commit
|
||||
`c742b39e0821590605385ee4ddabcafb258432d8`:
|
||||
|
||||
- `robo_orchard_lab/dataset/ubt_manipulation/tools/mcap_checker_ubt.py`
|
||||
- `robo_orchard_lab/dataset/ubt_manipulation/tools/utils.py`
|
||||
- `robo_orchard_lab/dataset/ubt_manipulation/tools/check_models.py`
|
||||
- `robo_orchard_lab/dataset/ubt_manipulation/tools/check_rules.py`
|
||||
- `robo_orchard_lab/dataset/ubt_manipulation/tools/check_config.py`
|
||||
- `robo_orchard_lab/dataset/ubt_manipulation/tools/ubt_common.py`
|
||||
- `robo_orchard_lab/dataset/ubt_manipulation/tools/templates/*`
|
||||
|
||||
Copyright (c) 2024-2025 Horizon Robotics. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0. A copy of the license is
|
||||
available at <https://www.apache.org/licenses/LICENSE-2.0>.
|
||||
|
||||
The copied Python files retain their original copyright and license headers.
|
||||
The vendored checker has one local compatibility patch: rendered resize
|
||||
dimensions are explicitly converted to integers before calling OpenCV.
|
||||
@ -0,0 +1,9 @@
|
||||
{
|
||||
"items": [
|
||||
{
|
||||
"operatorId": "operator-custom-ubt-mcap-runtime",
|
||||
"operatorSlug": "ubt_mcap_runtime",
|
||||
"dockerfile": "operator-images/lightweight/ubt-mcap-runtime/Dockerfile"
|
||||
}
|
||||
]
|
||||
}
|
||||
26
lightweight-operators/ubt-mcap-pipeline/main.py
Normal file
26
lightweight-operators/ubt-mcap-pipeline/main.py
Normal file
@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env python3
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
|
||||
from ubt_pipeline.check import run_check
|
||||
from ubt_pipeline.prepare import run_prepare
|
||||
from ubt_pipeline.report import run_report
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Drobodata UBT MCAP pipeline")
|
||||
parser.add_argument("stage", choices=("prepare", "check", "report"))
|
||||
args = parser.parse_args()
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
|
||||
)
|
||||
{"prepare": run_prepare, "check": run_check, "report": run_report}[
|
||||
args.stage
|
||||
]()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
201
lightweight-operators/ubt-mcap-pipeline/operators.template.json
Normal file
201
lightweight-operators/ubt-mcap-pipeline/operators.template.json
Normal file
@ -0,0 +1,201 @@
|
||||
[
|
||||
{
|
||||
"name": "UBT 数据准备",
|
||||
"type": "custom",
|
||||
"category": "ROBOTICS",
|
||||
"description": "从 BOS 对象清单安全下载并解压 UBT 数据,生成可复用的 fingerprint 缓存与 prepared manifest。",
|
||||
"version": "v1.0.0",
|
||||
"image": "__IMAGE_DIGEST__",
|
||||
"imageId": "__IMAGE_DIGEST__",
|
||||
"containerCommand": [
|
||||
"python",
|
||||
"/opt/droboticflow/lightweight/operator/main.py"
|
||||
],
|
||||
"containerArgs": [
|
||||
"prepare"
|
||||
],
|
||||
"inputs": [
|
||||
{
|
||||
"id": "source-objects",
|
||||
"name": "BOS 对象",
|
||||
"type": "file",
|
||||
"transmission": "bus"
|
||||
}
|
||||
],
|
||||
"outputs": [
|
||||
{
|
||||
"id": "prepared-manifest",
|
||||
"name": "准备清单",
|
||||
"type": "json",
|
||||
"transmission": "bus"
|
||||
}
|
||||
],
|
||||
"hyperparams": [
|
||||
{"key": "DATASET_ID", "value": "ubt-poc-2026-06"},
|
||||
{"key": "USER_NAME", "value": "ubt"},
|
||||
{"key": "TASK_NAME", "value": "test_task"},
|
||||
{"key": "ARCHIVE_GLOB", "value": "*.zip"},
|
||||
{
|
||||
"key": "URDF_SOURCE_PATH",
|
||||
"value": "bos://testxyz/projects/ubt-mcap-quality/raw/zbxzh060101_holobrain_train.urdf"
|
||||
},
|
||||
{"key": "REUSE_PREPARED", "value": "true"},
|
||||
{"key": "MIN_FREE_GIB", "value": "50"},
|
||||
{"key": "ON_DUPLICATE_EPISODE", "value": "error"}
|
||||
],
|
||||
"resources": {
|
||||
"cpuRequest": "1",
|
||||
"cpuLimit": "2",
|
||||
"memoryRequest": "2Gi",
|
||||
"memoryLimit": "4Gi"
|
||||
},
|
||||
"runtimeContract": {
|
||||
"lightweightOperator": {
|
||||
"enabled": true,
|
||||
"codeSubPath": "lightweight-operators/ubt-mcap-pipeline",
|
||||
"mountPath": "/opt/droboticflow/lightweight/operator",
|
||||
"codeReadOnly": true,
|
||||
"mounts": [
|
||||
{
|
||||
"name": "prepared-data",
|
||||
"pvcName": "droboticflow-workspace",
|
||||
"subPath": "datasets/ubt-prepared",
|
||||
"mountPath": "/data/ubt-prepared",
|
||||
"readOnly": false,
|
||||
"ideEnabled": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"skillDefinition": "安全下载并解压 UBT zip 和 URDF。开始前检查共享空间,拒绝目录穿越、重复 episode、缺失 MCAP、非法目录和无效 URDF。按对象 key/size/ETag 与 URDF SHA-256 生成 fingerprint;完整缓存复用,残缺缓存重建。"
|
||||
},
|
||||
{
|
||||
"name": "UBT MCAP 批量质检",
|
||||
"type": "custom",
|
||||
"category": "ROBOTICS",
|
||||
"description": "使用固定版本的 UBT Checker 对全部匹配 episode 执行 MCAP、topic、时间、图像、关节、夹爪、FK 与末端姿态检查。",
|
||||
"version": "v1.0.0",
|
||||
"image": "__IMAGE_DIGEST__",
|
||||
"imageId": "__IMAGE_DIGEST__",
|
||||
"containerCommand": [
|
||||
"python",
|
||||
"/opt/droboticflow/lightweight/operator/main.py"
|
||||
],
|
||||
"containerArgs": [
|
||||
"check"
|
||||
],
|
||||
"inputs": [
|
||||
{
|
||||
"id": "prepared-manifest",
|
||||
"name": "准备清单",
|
||||
"type": "json",
|
||||
"transmission": "bus"
|
||||
}
|
||||
],
|
||||
"outputs": [
|
||||
{
|
||||
"id": "checker-results",
|
||||
"name": "质检结果",
|
||||
"type": "file",
|
||||
"transmission": "bus"
|
||||
}
|
||||
],
|
||||
"hyperparams": [
|
||||
{"key": "DATE_PREFIXES", "value": "2026_06_16,2026_06_17"},
|
||||
{"key": "USER_NAMES", "value": "ubt"},
|
||||
{"key": "TASK_NAMES", "value": "test_task"},
|
||||
{"key": "NUM_WORKERS", "value": "1"},
|
||||
{"key": "MAX_EPISODES", "value": "1"},
|
||||
{"key": "INSPECT_CONFIG_JSON", "value": ""},
|
||||
{"key": "VIDEO_EXPORT", "value": "false"},
|
||||
{"key": "VIS_INTERVAL", "value": "3"},
|
||||
{"key": "VIS_RESIZE_SCALE", "value": "1.0"},
|
||||
{"key": "VIS_DEPTH", "value": "true"},
|
||||
{"key": "FAIL_ON_QUALITY_ERROR", "value": "false"}
|
||||
],
|
||||
"resources": {
|
||||
"cpuRequest": "2",
|
||||
"cpuLimit": "4",
|
||||
"memoryRequest": "8Gi",
|
||||
"memoryLimit": "16Gi",
|
||||
"sharedMemorySize": "2Gi"
|
||||
},
|
||||
"runtimeContract": {
|
||||
"lightweightOperator": {
|
||||
"enabled": true,
|
||||
"codeSubPath": "lightweight-operators/ubt-mcap-pipeline",
|
||||
"mountPath": "/opt/droboticflow/lightweight/operator",
|
||||
"codeReadOnly": true,
|
||||
"mounts": [
|
||||
{
|
||||
"name": "prepared-data",
|
||||
"pvcName": "droboticflow-workspace",
|
||||
"subPath": "datasets/ubt-prepared",
|
||||
"mountPath": "/data/ubt-prepared",
|
||||
"readOnly": true,
|
||||
"ideEnabled": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"skillDefinition": "读取 prepared manifest,日期支持 YYYY-MM-DD 或 YYYY_MM_DD 及逗号分隔。默认处理全部匹配 episode 且不导出视频。质量错误记录为 ERROR,不提前终止;运行异常记录为 RUNTIME_ERROR。"
|
||||
},
|
||||
{
|
||||
"name": "UBT 结果汇总",
|
||||
"type": "custom",
|
||||
"category": "ROBOTICS",
|
||||
"description": "汇总 UBT episode 结果,输出 JSON、CSV、pass/fail manifest、规则统计、自包含 HTML 与 Checker 原始报告。",
|
||||
"version": "v1.0.0",
|
||||
"image": "__IMAGE_DIGEST__",
|
||||
"imageId": "__IMAGE_DIGEST__",
|
||||
"containerCommand": [
|
||||
"python",
|
||||
"/opt/droboticflow/lightweight/operator/main.py"
|
||||
],
|
||||
"containerArgs": [
|
||||
"report"
|
||||
],
|
||||
"inputs": [
|
||||
{
|
||||
"id": "checker-results",
|
||||
"name": "质检结果",
|
||||
"type": "file",
|
||||
"transmission": "bus"
|
||||
}
|
||||
],
|
||||
"outputs": [
|
||||
{
|
||||
"id": "quality-bundle",
|
||||
"name": "质检结果包",
|
||||
"type": "file",
|
||||
"transmission": "bus"
|
||||
}
|
||||
],
|
||||
"hyperparams": [],
|
||||
"resources": {
|
||||
"cpuRequest": "500m",
|
||||
"cpuLimit": "1",
|
||||
"memoryRequest": "512Mi",
|
||||
"memoryLimit": "2Gi"
|
||||
},
|
||||
"runtimeContract": {
|
||||
"lightweightOperator": {
|
||||
"enabled": true,
|
||||
"codeSubPath": "lightweight-operators/ubt-mcap-pipeline",
|
||||
"mountPath": "/opt/droboticflow/lightweight/operator",
|
||||
"codeReadOnly": true,
|
||||
"mounts": [
|
||||
{
|
||||
"name": "prepared-data",
|
||||
"pvcName": "droboticflow-workspace",
|
||||
"subPath": "datasets/ubt-prepared",
|
||||
"mountPath": "/data/ubt-prepared",
|
||||
"readOnly": true,
|
||||
"ideEnabled": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"skillDefinition": "生成稳定的 PASS、ERROR、RUNTIME_ERROR 汇总,按日期和规则统计。pass/fail manifest 仅保留原始 BOS 路径与 prepared 路径,不复制 episode 数据。"
|
||||
}
|
||||
]
|
||||
9
lightweight-operators/ubt-mcap-pipeline/requirements.txt
Normal file
9
lightweight-operators/ubt-mcap-pipeline/requirements.txt
Normal file
@ -0,0 +1,9 @@
|
||||
bce-python-sdk==0.9.46
|
||||
matplotlib==3.10.3
|
||||
mcap==1.4.0
|
||||
mcap-ros2-support==0.5.7
|
||||
numpy==1.26.4
|
||||
opencv-python-headless==4.10.0.84
|
||||
pytorch-kinematics==0.10.0
|
||||
scipy==1.15.3
|
||||
torch==2.4.1+cpu
|
||||
@ -0,0 +1 @@
|
||||
"""Minimal Apache-2.0 RoboOrchard namespace vendored for the UBT checker."""
|
||||
@ -0,0 +1 @@
|
||||
"""Dataset tools namespace."""
|
||||
@ -0,0 +1 @@
|
||||
"""UBT manipulation dataset tools."""
|
||||
@ -0,0 +1 @@
|
||||
"""UBT MCAP checker tools."""
|
||||
@ -0,0 +1,71 @@
|
||||
# Project RoboOrchard
|
||||
#
|
||||
# Copyright (c) 2024-2025 Horizon Robotics. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied. See the License for the specific language governing
|
||||
# permissions and limitations under the License.
|
||||
|
||||
from __future__ import annotations
|
||||
import json
|
||||
from dataclasses import asdict, dataclass
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class InspectConfig:
|
||||
"""Thresholds used by rule-based MCAP inspection.
|
||||
|
||||
This dataclass centralizes the default thresholds shared by JSON config
|
||||
loading, rule evaluation, and validation reporting.
|
||||
"""
|
||||
|
||||
camera_topics_mean_fps_limit: float = 25.0
|
||||
camera_topics_min_fps_limit: float = 5.0
|
||||
robot_state_topics_mean_fps_limit: float = 180.0
|
||||
robot_state_topics_min_fps_limit: float = 50.0
|
||||
timestamp_limit: float = 0.5
|
||||
alignment_time_diff_limit: float = 0.5
|
||||
joint_limit_tolerance: float = 0.1
|
||||
joint_change_tolerance: float = 0.1
|
||||
master_slave_joint_tolerance: float = 0.1
|
||||
ee_pose_position_tolerance: float = 0.02
|
||||
ee_pose_orientation_tolerance: float = 0.05
|
||||
gripper_state_discrete_tolerance: float = 0.05
|
||||
gripper_position_range_tolerance: float = 0.0
|
||||
|
||||
|
||||
def build_default_inspect_config() -> InspectConfig:
|
||||
"""Build the default inspection thresholds.
|
||||
|
||||
Returns:
|
||||
InspectConfig: Default inspection thresholds.
|
||||
"""
|
||||
|
||||
return InspectConfig()
|
||||
|
||||
|
||||
def load_inspect_config(config_path: str | None = None) -> InspectConfig:
|
||||
"""Load inspection thresholds from JSON and merge them with defaults.
|
||||
|
||||
Args:
|
||||
config_path: Optional JSON file path that overrides default thresholds.
|
||||
|
||||
Returns:
|
||||
InspectConfig: Effective inspection thresholds.
|
||||
"""
|
||||
|
||||
if not config_path:
|
||||
return build_default_inspect_config()
|
||||
payload = json.loads(Path(config_path).read_text(encoding="utf-8"))
|
||||
default_payload = asdict(build_default_inspect_config())
|
||||
default_payload.update(payload)
|
||||
return InspectConfig(**default_payload)
|
||||
@ -0,0 +1,133 @@
|
||||
# Project RoboOrchard
|
||||
#
|
||||
# Copyright (c) 2024-2025 Horizon Robotics. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied. See the License for the specific language governing
|
||||
# permissions and limitations under the License.
|
||||
|
||||
from __future__ import annotations
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
@dataclass
|
||||
class EpisodeData:
|
||||
"""Normalized episode payload consumed by rule evaluation.
|
||||
|
||||
This dataclass keeps the normalized arrays, topic statistics, camera
|
||||
payloads, and parse warnings consumed by rule evaluation and reporting.
|
||||
"""
|
||||
|
||||
uuid: str
|
||||
meta: dict[str, Any]
|
||||
topic_counts: dict[str, int]
|
||||
topic_frequencies: dict[str, float | Any]
|
||||
base_time: np.ndarray
|
||||
joint_positions: np.ndarray
|
||||
master_joint_positions: np.ndarray
|
||||
fk_ee_poses: np.ndarray
|
||||
recorded_ee_poses: np.ndarray | None
|
||||
images: dict[str, dict[str, Any]]
|
||||
depths: dict[str, dict[str, Any]]
|
||||
required_topics: list[str]
|
||||
observed_topics: set[str] = field(default_factory=set)
|
||||
joint_limit_names: list[str] = field(default_factory=list)
|
||||
joint_lower_limits: np.ndarray | None = None
|
||||
joint_upper_limits: np.ndarray | None = None
|
||||
parse_warnings: list[str] = field(default_factory=list)
|
||||
cam_names: list[str] = field(default_factory=list)
|
||||
extrinsic: dict[str, np.ndarray] = field(default_factory=dict)
|
||||
intrinsic: dict[str, np.ndarray] = field(default_factory=dict)
|
||||
topic_summaries: dict[str, dict[str, Any]] = field(default_factory=dict)
|
||||
alignment_time_diff_stats: dict[str, dict[str, float]] = field(
|
||||
default_factory=dict
|
||||
)
|
||||
static_filter_applied: bool = False
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RuleResult:
|
||||
"""Structured result for one inspection rule.
|
||||
|
||||
It captures the stable rule id, status values such as ``pass`` or
|
||||
``warning``, the rule severity, a human-readable message, and optional
|
||||
metrics.
|
||||
"""
|
||||
|
||||
rule_id: str
|
||||
status: str
|
||||
severity: str
|
||||
message: str
|
||||
metrics: dict[str, Any] = field(default_factory=dict)
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
"""Convert the rule result to a plain dictionary.
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: Serializable rule payload.
|
||||
"""
|
||||
|
||||
return {
|
||||
"rule_id": self.rule_id,
|
||||
"status": self.status,
|
||||
"severity": self.severity,
|
||||
"message": self.message,
|
||||
"metrics": self.metrics,
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class EpisodeInspection:
|
||||
"""Aggregated rule output for one episode before rendering.
|
||||
|
||||
It groups the episode status, per-rule results, render hints, and
|
||||
aggregate metrics before serialization.
|
||||
"""
|
||||
|
||||
episode_status: str
|
||||
rule_results: list[RuleResult]
|
||||
render_hints: list[str]
|
||||
episode_metrics: dict[str, Any]
|
||||
|
||||
|
||||
@dataclass
|
||||
class EpisodeReport:
|
||||
"""Serializable episode-level report emitted by the checker.
|
||||
|
||||
It stores the serialized inspection outcome together with optional video
|
||||
export and runtime error metadata.
|
||||
"""
|
||||
|
||||
uuid: str
|
||||
episode_status: str
|
||||
rule_results: list[RuleResult]
|
||||
episode_metrics: dict[str, Any]
|
||||
video_file: str | None
|
||||
runtime_error: str | None = None
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
"""Convert the episode report to a plain dictionary.
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: Serializable episode report payload.
|
||||
"""
|
||||
|
||||
return {
|
||||
"uuid": self.uuid,
|
||||
"episode_status": self.episode_status,
|
||||
"rule_results": [item.to_dict() for item in self.rule_results],
|
||||
"episode_metrics": self.episode_metrics,
|
||||
"video_file": self.video_file,
|
||||
"runtime_error": self.runtime_error,
|
||||
}
|
||||
@ -0,0 +1,750 @@
|
||||
# Project RoboOrchard
|
||||
#
|
||||
# Copyright (c) 2024-2025 Horizon Robotics. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied. See the License for the specific language governing
|
||||
# permissions and limitations under the License.
|
||||
|
||||
from __future__ import annotations
|
||||
from collections import Counter
|
||||
|
||||
import numpy as np
|
||||
from scipy.spatial.transform import Rotation
|
||||
|
||||
from robo_orchard_lab.dataset.ubt_manipulation.tools.check_config import (
|
||||
InspectConfig,
|
||||
)
|
||||
from robo_orchard_lab.dataset.ubt_manipulation.tools.check_models import (
|
||||
EpisodeData,
|
||||
EpisodeInspection,
|
||||
RuleResult,
|
||||
)
|
||||
def gripper_state_joint_indices(joint_limit_names: list[str]) -> list[int]:
|
||||
"""Return column indices of gripper-state joints in a recorded joint vector."""
|
||||
return [
|
||||
idx
|
||||
for idx, name in enumerate(joint_limit_names)
|
||||
if name.endswith("_gripper_state")
|
||||
]
|
||||
|
||||
|
||||
def gripper_position_joint_indices(joint_limit_names: list[str]) -> list[int]:
|
||||
"""Return column indices of gripper-position joints in a recorded joint vector."""
|
||||
return [
|
||||
idx
|
||||
for idx, name in enumerate(joint_limit_names)
|
||||
if name.endswith("_gripper_position")
|
||||
]
|
||||
|
||||
|
||||
def _camera_topic(topic: str) -> bool:
|
||||
"""Return whether the topic belongs to a camera stream.
|
||||
|
||||
Args:
|
||||
topic: Topic name to classify.
|
||||
|
||||
Returns:
|
||||
bool: True when the topic is a camera stream.
|
||||
"""
|
||||
|
||||
return "/observation/cameras/" in topic
|
||||
|
||||
|
||||
def _robot_state_topic(topic: str) -> bool:
|
||||
"""Return whether the topic belongs to a robot-state stream.
|
||||
|
||||
Args:
|
||||
topic: Topic name to classify.
|
||||
|
||||
Returns:
|
||||
bool: True when the topic is a robot-state stream.
|
||||
"""
|
||||
|
||||
return "/observation/robot_state/" in topic
|
||||
|
||||
|
||||
def _to_scalar_frequency(value) -> float:
|
||||
"""Collapse scalar or windowed frequency values into one comparable mean.
|
||||
|
||||
Args:
|
||||
value: Scalar frequency or per-step frequency array.
|
||||
|
||||
Returns:
|
||||
float: Comparable mean frequency value.
|
||||
"""
|
||||
|
||||
if isinstance(value, np.ndarray):
|
||||
if value.size == 0:
|
||||
return 0.0
|
||||
finite_value = value[np.isfinite(value) & (value > 0)]
|
||||
if finite_value.size == 0:
|
||||
return 0.0
|
||||
return float(np.mean(finite_value))
|
||||
return float(value) if value else 0.0
|
||||
|
||||
|
||||
def _fps_rule_metrics(
|
||||
freq, mean_fps_limit: float, min_fps_limit: float
|
||||
) -> dict[str, float | int]:
|
||||
"""Build FPS-related metrics for threshold and dropped-frame checks.
|
||||
|
||||
Args:
|
||||
freq: Scalar or per-step frequency payload.
|
||||
mean_fps_limit: Configured mean FPS lower bound for the topic.
|
||||
min_fps_limit: Configured minimum FPS lower bound for the topic.
|
||||
|
||||
Returns:
|
||||
dict[str, float | int]: Metrics used by FPS-related rules and logs.
|
||||
"""
|
||||
|
||||
if isinstance(freq, np.ndarray):
|
||||
if freq.size == 0:
|
||||
mean_fps = 0.0
|
||||
min_fps = 0.0
|
||||
max_interval = 0.0
|
||||
exceed_count = 0
|
||||
else:
|
||||
finite = np.isfinite(freq) & (freq > 0)
|
||||
mean_fps = float(np.mean(freq[finite])) if finite.any() else 0.0
|
||||
min_fps = float(np.min(freq[finite])) if finite.any() else 0.0
|
||||
intervals = np.where(finite, 1.0 / freq, np.inf)
|
||||
if np.isfinite(intervals).any():
|
||||
max_interval = float(np.max(intervals))
|
||||
else:
|
||||
max_interval = float("inf")
|
||||
interval_limit = (
|
||||
1.0 / min_fps_limit if min_fps_limit > 0 else float("inf")
|
||||
)
|
||||
exceed_count = int(np.count_nonzero(intervals > interval_limit))
|
||||
else:
|
||||
mean_fps = (
|
||||
float(freq) if isinstance(freq, (int, float, np.number)) else 0.0
|
||||
)
|
||||
min_fps = mean_fps
|
||||
if mean_fps > 0:
|
||||
max_interval = 1.0 / mean_fps
|
||||
interval_limit = (
|
||||
1.0 / min_fps_limit if min_fps_limit > 0 else float("inf")
|
||||
)
|
||||
exceed_count = 1 if max_interval > interval_limit else 0
|
||||
else:
|
||||
max_interval = 0.0
|
||||
exceed_count = 0
|
||||
interval_limit = (
|
||||
1.0 / min_fps_limit if min_fps_limit > 0 else float("inf")
|
||||
)
|
||||
return {
|
||||
"mean_fps": round(mean_fps, 6),
|
||||
"min_fps": round(min_fps, 6),
|
||||
"mean_fps_limit": round(mean_fps_limit, 6),
|
||||
"min_fps_limit": round(min_fps_limit, 6),
|
||||
"max_interval": round(max_interval, 6),
|
||||
"max_interval_limit": round(interval_limit, 6)
|
||||
if np.isfinite(interval_limit)
|
||||
else float("inf"),
|
||||
"max_interval_exceed_count": exceed_count,
|
||||
}
|
||||
|
||||
|
||||
def _timestamp_metric(actual: float, expected: float) -> dict[str, float]:
|
||||
"""Build a timestamp comparison payload for logging and reporting.
|
||||
|
||||
Args:
|
||||
actual: Observed timestamp-derived value in seconds.
|
||||
expected: Reference value in seconds.
|
||||
|
||||
Returns:
|
||||
dict[str, float]: Timestamp comparison payload.
|
||||
"""
|
||||
|
||||
return {
|
||||
"actual": round(actual, 6),
|
||||
"expected": round(expected, 6),
|
||||
"delta": round(actual - expected, 6),
|
||||
}
|
||||
|
||||
|
||||
def _joint_limit_violation_metrics(
|
||||
joint_positions: np.ndarray,
|
||||
joint_names: list[str],
|
||||
joint_lower_limits: np.ndarray,
|
||||
joint_upper_limits: np.ndarray,
|
||||
tolerance: float,
|
||||
) -> dict[str, float | int | str] | None:
|
||||
"""Build URDF-based joint-limit violation metrics.
|
||||
|
||||
Args:
|
||||
joint_positions: Recorded joint positions with shape ``[T, J]``.
|
||||
joint_names: Recorded joint names aligned with ``joint_positions``.
|
||||
joint_lower_limits: Lower URDF joint limits.
|
||||
joint_upper_limits: Upper URDF joint limits.
|
||||
tolerance: Symmetric tolerance applied around the URDF limits.
|
||||
|
||||
Returns:
|
||||
dict[str, float | int | str] | None: Metrics for the largest
|
||||
violation, or ``None`` when all joints are within limits.
|
||||
"""
|
||||
|
||||
if (
|
||||
joint_lower_limits.shape != joint_upper_limits.shape
|
||||
or joint_lower_limits.ndim != 1
|
||||
or joint_lower_limits.shape[0] != joint_positions.shape[1]
|
||||
):
|
||||
raise ValueError(
|
||||
"joint limit arrays must be 1D and aligned with joint_positions"
|
||||
)
|
||||
|
||||
lower_with_tolerance = joint_lower_limits - tolerance
|
||||
upper_with_tolerance = joint_upper_limits + tolerance
|
||||
above_upper = joint_positions - upper_with_tolerance[None, :]
|
||||
below_lower = lower_with_tolerance[None, :] - joint_positions
|
||||
violation = np.maximum(above_upper, below_lower)
|
||||
max_violation = float(np.max(violation))
|
||||
if max_violation <= 0:
|
||||
return None
|
||||
|
||||
step_index, joint_index = np.unravel_index(
|
||||
int(np.argmax(violation)), violation.shape
|
||||
)
|
||||
return {
|
||||
"joint_name": (
|
||||
joint_names[joint_index]
|
||||
if joint_index < len(joint_names)
|
||||
else f"joint_{joint_index}"
|
||||
),
|
||||
"joint_index": int(joint_index),
|
||||
"step_index": int(step_index),
|
||||
"actual": round(float(joint_positions[step_index, joint_index]), 6),
|
||||
"lower_limit": round(float(lower_with_tolerance[joint_index]), 6),
|
||||
"upper_limit": round(float(upper_with_tolerance[joint_index]), 6),
|
||||
"max_violation": round(max_violation, 6),
|
||||
}
|
||||
|
||||
|
||||
def _gripper_position_range_metrics(
|
||||
joint_positions: np.ndarray,
|
||||
joint_names: list[str],
|
||||
lower: float,
|
||||
upper: float,
|
||||
tolerance: float,
|
||||
) -> dict[str, float | int] | None:
|
||||
"""Build metrics when gripper position values are outside [lower, upper]."""
|
||||
|
||||
position_indices = gripper_position_joint_indices(joint_names)
|
||||
if not position_indices:
|
||||
return None
|
||||
|
||||
pos_values = joint_positions[:, position_indices]
|
||||
above = pos_values - (upper + tolerance)
|
||||
below = (lower - tolerance) - pos_values
|
||||
violation = np.maximum(above, below)
|
||||
max_violation = float(np.max(violation))
|
||||
if max_violation <= 0:
|
||||
return None
|
||||
|
||||
step_index, local_index = np.unravel_index(
|
||||
int(np.argmax(violation)), violation.shape
|
||||
)
|
||||
joint_index = position_indices[local_index]
|
||||
return {
|
||||
"joint_name": (
|
||||
joint_names[joint_index]
|
||||
if joint_index < len(joint_names)
|
||||
else f"joint_{joint_index}"
|
||||
),
|
||||
"joint_index": int(joint_index),
|
||||
"step_index": int(step_index),
|
||||
"actual": round(float(pos_values[step_index, local_index]), 6),
|
||||
"lower_limit": round(lower - tolerance, 6),
|
||||
"upper_limit": round(upper + tolerance, 6),
|
||||
"max_violation": round(max_violation, 6),
|
||||
}
|
||||
|
||||
|
||||
def _gripper_state_discrete_metrics(
|
||||
joint_positions: np.ndarray,
|
||||
joint_names: list[str],
|
||||
tolerance: float,
|
||||
) -> dict[str, float | int] | None:
|
||||
"""Build metrics when gripper state values are not close to 0 or 1."""
|
||||
|
||||
state_indices = gripper_state_joint_indices(joint_names)
|
||||
if not state_indices:
|
||||
return None
|
||||
|
||||
state_values = joint_positions[:, state_indices]
|
||||
# Valid discrete states: 0 (open), 1 (closed), -1 (VR arm uninitialized).
|
||||
deviation = np.minimum(
|
||||
np.minimum(np.abs(state_values), np.abs(state_values - 1.0)),
|
||||
np.abs(state_values + 1.0),
|
||||
)
|
||||
max_deviation = float(np.max(deviation))
|
||||
if max_deviation <= tolerance:
|
||||
return None
|
||||
|
||||
step_index, local_index = np.unravel_index(
|
||||
int(np.argmax(deviation)), deviation.shape
|
||||
)
|
||||
joint_index = state_indices[local_index]
|
||||
return {
|
||||
"joint_name": (
|
||||
joint_names[joint_index]
|
||||
if joint_index < len(joint_names)
|
||||
else f"joint_{joint_index}"
|
||||
),
|
||||
"joint_index": int(joint_index),
|
||||
"step_index": int(step_index),
|
||||
"actual": round(float(state_values[step_index, local_index]), 6),
|
||||
"max_deviation": round(max_deviation, 6),
|
||||
"tolerance": round(tolerance, 6),
|
||||
}
|
||||
|
||||
|
||||
def _max_orientation_gap(
|
||||
recorded_ee_poses: np.ndarray, fk_ee_poses: np.ndarray
|
||||
) -> float:
|
||||
"""Return the largest rotation-angle gap between recorded and FK poses.
|
||||
|
||||
Args:
|
||||
recorded_ee_poses:
|
||||
Recorded EE poses in ``[..., x, y, z, qx, qy, qz, qw]``.
|
||||
fk_ee_poses: FK EE poses in the same layout.
|
||||
|
||||
Returns:
|
||||
float: Maximum angle difference in radians.
|
||||
"""
|
||||
|
||||
recorded_rot = Rotation.from_quat(
|
||||
recorded_ee_poses[..., 3:].reshape(-1, 4), scalar_first=False
|
||||
)
|
||||
fk_rot = Rotation.from_quat(
|
||||
fk_ee_poses[..., 3:].reshape(-1, 4), scalar_first=False
|
||||
)
|
||||
return float(np.max((fk_rot.inv() * recorded_rot).magnitude()))
|
||||
|
||||
|
||||
def _aggregate_rule_results(
|
||||
episode_data: EpisodeData, rule_results: list[RuleResult]
|
||||
) -> EpisodeInspection:
|
||||
"""Collapse rule hits into an episode-level status and summary metrics.
|
||||
|
||||
Args:
|
||||
episode_data: Normalized episode payload.
|
||||
rule_results: Rule results collected for the episode.
|
||||
|
||||
Returns:
|
||||
EpisodeInspection: Aggregate inspection output.
|
||||
"""
|
||||
|
||||
blocking_failed = any(
|
||||
item.status == "fail" and item.severity == "blocking"
|
||||
for item in rule_results
|
||||
)
|
||||
has_signal = any(
|
||||
item.status in {"warning", "fail"} for item in rule_results
|
||||
)
|
||||
if blocking_failed:
|
||||
episode_status = "fail"
|
||||
elif has_signal:
|
||||
episode_status = "warning"
|
||||
else:
|
||||
episode_status = "pass"
|
||||
|
||||
render_hints = []
|
||||
for item in rule_results:
|
||||
if item.status == "pass":
|
||||
continue
|
||||
metric_text = ", ".join(
|
||||
f"{key}={value}" for key, value in item.metrics.items()
|
||||
)
|
||||
hint = f"{item.status}: {item.rule_id}"
|
||||
if metric_text:
|
||||
hint = f"{hint} ({metric_text})"
|
||||
render_hints.append(hint)
|
||||
render_hints = render_hints[:3]
|
||||
status_counts = Counter(item.status for item in rule_results)
|
||||
episode_metrics = {
|
||||
"rule_hit_count": sum(item.status != "pass" for item in rule_results),
|
||||
"parse_warning_count": len(episode_data.parse_warnings),
|
||||
"warning_rule_count": status_counts.get("warning", 0),
|
||||
"failed_rule_count": status_counts.get("fail", 0),
|
||||
}
|
||||
return EpisodeInspection(
|
||||
episode_status=episode_status,
|
||||
rule_results=rule_results,
|
||||
render_hints=render_hints,
|
||||
episode_metrics=episode_metrics,
|
||||
)
|
||||
|
||||
|
||||
def evaluate_episode_rules(
|
||||
episode_data: EpisodeData, config: InspectConfig
|
||||
) -> EpisodeInspection:
|
||||
"""Run the configured rule set on one normalized episode.
|
||||
|
||||
Args:
|
||||
episode_data: Normalized episode payload.
|
||||
config: Inspection thresholds and tolerances.
|
||||
|
||||
Returns:
|
||||
EpisodeInspection: Aggregate inspection result for the episode.
|
||||
"""
|
||||
|
||||
rule_results: list[RuleResult] = []
|
||||
|
||||
observed_topics = episode_data.observed_topics or set(
|
||||
episode_data.topic_counts
|
||||
)
|
||||
missing_topics = [
|
||||
topic
|
||||
for topic in episode_data.required_topics
|
||||
if topic not in observed_topics
|
||||
]
|
||||
if missing_topics:
|
||||
rule_results.append(
|
||||
RuleResult(
|
||||
rule_id="missing_topic",
|
||||
status="fail",
|
||||
severity="blocking",
|
||||
message="required topics are missing",
|
||||
metrics={"missing_topics": ",".join(sorted(missing_topics))},
|
||||
)
|
||||
)
|
||||
empty_topics = [
|
||||
topic
|
||||
for topic in episode_data.required_topics
|
||||
if topic in observed_topics
|
||||
and episode_data.topic_counts.get(topic, 0) <= 0
|
||||
]
|
||||
if empty_topics:
|
||||
rule_results.append(
|
||||
RuleResult(
|
||||
rule_id="empty_stream",
|
||||
status="fail",
|
||||
severity="blocking",
|
||||
message="required topics have no usable messages",
|
||||
metrics={"empty_topics": ",".join(sorted(empty_topics))},
|
||||
)
|
||||
)
|
||||
|
||||
if episode_data.base_time.size > 1:
|
||||
deltas = np.diff(episode_data.base_time)
|
||||
if np.any(deltas < -config.timestamp_limit):
|
||||
rule_results.append(
|
||||
RuleResult(
|
||||
rule_id="timestamp_non_monotonic",
|
||||
status="fail",
|
||||
severity="blocking",
|
||||
message="base timestamps are not strictly increasing",
|
||||
metrics={"min_delta": float(np.min(deltas))},
|
||||
)
|
||||
)
|
||||
|
||||
fps_failures: dict[str, dict[str, float | int]] = {}
|
||||
interval_failures: dict[str, dict[str, float | int]] = {}
|
||||
for topic, fps in episode_data.topic_frequencies.items():
|
||||
if _camera_topic(topic):
|
||||
mean_fps_limit = config.camera_topics_mean_fps_limit
|
||||
min_fps_limit = config.camera_topics_min_fps_limit
|
||||
elif _robot_state_topic(topic):
|
||||
mean_fps_limit = config.robot_state_topics_mean_fps_limit
|
||||
min_fps_limit = config.robot_state_topics_min_fps_limit
|
||||
else:
|
||||
continue
|
||||
metrics = _fps_rule_metrics(fps, mean_fps_limit, min_fps_limit)
|
||||
if float(metrics.get("mean_fps", 0.0)) < mean_fps_limit:
|
||||
fps_failures[topic] = metrics
|
||||
elif float(metrics.get("min_fps", 0.0)) < min_fps_limit:
|
||||
interval_failures[topic] = {
|
||||
"mean_fps": float(metrics["mean_fps"]),
|
||||
"mean_fps_limit": float(metrics["mean_fps_limit"]),
|
||||
"min_fps": float(metrics["min_fps"]),
|
||||
"min_fps_limit": float(metrics["min_fps_limit"]),
|
||||
}
|
||||
if fps_failures:
|
||||
rule_results.append(
|
||||
RuleResult(
|
||||
rule_id="fps_out_of_range",
|
||||
status="fail",
|
||||
severity="blocking",
|
||||
message="stream fps is below configured threshold",
|
||||
metrics=fps_failures,
|
||||
)
|
||||
)
|
||||
if interval_failures:
|
||||
rule_results.append(
|
||||
RuleResult(
|
||||
rule_id="interval_spike_or_drop_frame",
|
||||
status="fail",
|
||||
severity="blocking",
|
||||
message="stream min fps is below configured threshold",
|
||||
metrics=interval_failures,
|
||||
)
|
||||
)
|
||||
if (
|
||||
not episode_data.static_filter_applied
|
||||
and episode_data.base_time.size
|
||||
and episode_data.topic_summaries
|
||||
):
|
||||
base_start = float(episode_data.base_time[0])
|
||||
base_end = float(episode_data.base_time[-1])
|
||||
base_duration = base_end - base_start
|
||||
start_mismatches: dict[str, dict[str, float]] = {}
|
||||
end_mismatches: dict[str, dict[str, float]] = {}
|
||||
duration_mismatches: dict[str, dict[str, float]] = {}
|
||||
|
||||
for topic, count in episode_data.topic_counts.items():
|
||||
if count <= 0:
|
||||
continue
|
||||
summary = episode_data.topic_summaries.get(topic)
|
||||
if not summary:
|
||||
continue
|
||||
start_time_ns = summary.get("start_time_ns")
|
||||
end_time_ns = summary.get("end_time_ns")
|
||||
if start_time_ns is None or end_time_ns is None:
|
||||
continue
|
||||
actual_start = float(start_time_ns) / 1e9
|
||||
actual_end = float(end_time_ns) / 1e9
|
||||
actual_duration = actual_end - actual_start
|
||||
|
||||
if abs(actual_start - base_start) > config.timestamp_limit:
|
||||
start_mismatches[topic] = _timestamp_metric(
|
||||
actual_start, base_start
|
||||
)
|
||||
if abs(actual_end - base_end) > config.timestamp_limit:
|
||||
end_mismatches[topic] = _timestamp_metric(actual_end, base_end)
|
||||
if abs(actual_duration - base_duration) > config.timestamp_limit:
|
||||
duration_mismatches[topic] = _timestamp_metric(
|
||||
actual_duration, base_duration
|
||||
)
|
||||
|
||||
if start_mismatches:
|
||||
rule_results.append(
|
||||
RuleResult(
|
||||
rule_id="start_ts_mismatch",
|
||||
status="warning",
|
||||
severity="non_blocking",
|
||||
message=(
|
||||
"stream start timestamps differ from base timeline"
|
||||
),
|
||||
metrics=start_mismatches,
|
||||
)
|
||||
)
|
||||
if end_mismatches:
|
||||
rule_results.append(
|
||||
RuleResult(
|
||||
rule_id="end_ts_mismatch",
|
||||
status="warning",
|
||||
severity="non_blocking",
|
||||
message="stream end timestamps differ from base timeline",
|
||||
metrics=end_mismatches,
|
||||
)
|
||||
)
|
||||
if duration_mismatches:
|
||||
rule_results.append(
|
||||
RuleResult(
|
||||
rule_id="duration_mismatch",
|
||||
status="warning",
|
||||
severity="non_blocking",
|
||||
message="stream durations differ from base timeline",
|
||||
metrics=duration_mismatches,
|
||||
)
|
||||
)
|
||||
|
||||
if not episode_data.static_filter_applied:
|
||||
alignment_mismatches: dict[str, dict[str, float]] = {}
|
||||
for topic, stats in episode_data.alignment_time_diff_stats.items():
|
||||
max_time_diff = float(stats.get("max_time_diff", 0.0))
|
||||
if max_time_diff <= config.alignment_time_diff_limit:
|
||||
continue
|
||||
alignment_mismatches[topic] = {
|
||||
"max_time_diff": round(max_time_diff, 6),
|
||||
"mean_time_diff": round(
|
||||
float(stats.get("mean_time_diff", 0.0)), 6
|
||||
),
|
||||
"limit": round(config.alignment_time_diff_limit, 6),
|
||||
}
|
||||
if alignment_mismatches:
|
||||
rule_results.append(
|
||||
RuleResult(
|
||||
rule_id="alignment_time_diff_out_of_range",
|
||||
status="warning",
|
||||
severity="non_blocking",
|
||||
message=(
|
||||
"aligned timestamps deviate too much from base "
|
||||
"timeline"
|
||||
),
|
||||
metrics=alignment_mismatches,
|
||||
)
|
||||
)
|
||||
|
||||
if episode_data.joint_positions.size:
|
||||
if (
|
||||
episode_data.joint_lower_limits is not None
|
||||
and episode_data.joint_upper_limits is not None
|
||||
):
|
||||
joint_limit_metrics = _joint_limit_violation_metrics(
|
||||
joint_positions=episode_data.joint_positions,
|
||||
joint_names=episode_data.joint_limit_names,
|
||||
joint_lower_limits=np.asarray(
|
||||
episode_data.joint_lower_limits, dtype=np.float64
|
||||
),
|
||||
joint_upper_limits=np.asarray(
|
||||
episode_data.joint_upper_limits, dtype=np.float64
|
||||
),
|
||||
tolerance=config.joint_limit_tolerance,
|
||||
)
|
||||
else:
|
||||
joint_limit_metrics = None
|
||||
if joint_limit_metrics is not None:
|
||||
rule_results.append(
|
||||
RuleResult(
|
||||
rule_id="joint_limit_violation",
|
||||
status="fail",
|
||||
severity="blocking",
|
||||
message="joint position exceeds URDF joint limit",
|
||||
metrics=joint_limit_metrics,
|
||||
)
|
||||
)
|
||||
gripper_state_metrics = _gripper_state_discrete_metrics(
|
||||
joint_positions=episode_data.joint_positions,
|
||||
joint_names=episode_data.joint_limit_names,
|
||||
tolerance=config.gripper_state_discrete_tolerance,
|
||||
)
|
||||
if gripper_state_metrics is not None:
|
||||
rule_results.append(
|
||||
RuleResult(
|
||||
rule_id="gripper_state_not_discrete",
|
||||
status="warning",
|
||||
severity="non_blocking",
|
||||
message="gripper state is not close to 0 or 1",
|
||||
metrics=gripper_state_metrics,
|
||||
)
|
||||
)
|
||||
gripper_pos_metrics = _gripper_position_range_metrics(
|
||||
joint_positions=episode_data.joint_positions,
|
||||
joint_names=episode_data.joint_limit_names,
|
||||
lower=0.0,
|
||||
upper=1.0,
|
||||
tolerance=config.gripper_position_range_tolerance,
|
||||
)
|
||||
if gripper_pos_metrics is not None:
|
||||
rule_results.append(
|
||||
RuleResult(
|
||||
rule_id="gripper_position_out_of_range",
|
||||
status="fail",
|
||||
severity="blocking",
|
||||
message="gripper position is outside [0, 1] range",
|
||||
metrics=gripper_pos_metrics,
|
||||
)
|
||||
)
|
||||
if episode_data.joint_positions.shape[0] > 1:
|
||||
jump_indices = [
|
||||
idx
|
||||
for idx, name in enumerate(episode_data.joint_limit_names)
|
||||
if not name.endswith("_gripper_state")
|
||||
]
|
||||
if jump_indices:
|
||||
joint_delta = np.abs(
|
||||
np.diff(
|
||||
episode_data.joint_positions[:, jump_indices],
|
||||
axis=0,
|
||||
)
|
||||
)
|
||||
max_joint_delta = float(np.max(joint_delta))
|
||||
else:
|
||||
max_joint_delta = float(
|
||||
np.max(
|
||||
np.abs(np.diff(episode_data.joint_positions, axis=0))
|
||||
)
|
||||
)
|
||||
if max_joint_delta > config.joint_change_tolerance:
|
||||
rule_results.append(
|
||||
RuleResult(
|
||||
rule_id="joint_jump_violation",
|
||||
status="warning",
|
||||
severity="non_blocking",
|
||||
message="adjacent joint delta exceeds tolerance",
|
||||
metrics={"max_joint_delta": round(max_joint_delta, 6)},
|
||||
)
|
||||
)
|
||||
|
||||
if (
|
||||
episode_data.master_joint_positions.size
|
||||
and episode_data.joint_positions.size
|
||||
and episode_data.master_joint_positions.shape
|
||||
== episode_data.joint_positions.shape
|
||||
):
|
||||
# UBT master arm joints are VR-empty; only gripper width is comparable.
|
||||
compare_indices = gripper_position_joint_indices(
|
||||
episode_data.joint_limit_names
|
||||
)
|
||||
if compare_indices:
|
||||
master_values = episode_data.master_joint_positions[
|
||||
:, compare_indices
|
||||
]
|
||||
slave_values = episode_data.joint_positions[:, compare_indices]
|
||||
else:
|
||||
master_values = episode_data.master_joint_positions
|
||||
slave_values = episode_data.joint_positions
|
||||
max_gap = float(np.max(np.abs(master_values - slave_values)))
|
||||
if max_gap > config.master_slave_joint_tolerance:
|
||||
rule_results.append(
|
||||
RuleResult(
|
||||
rule_id="master_slave_joint_gap",
|
||||
status="warning",
|
||||
severity="non_blocking",
|
||||
message="master and follower joints diverge",
|
||||
metrics={
|
||||
"max_gap": round(max_gap, 6),
|
||||
"compared_joint_indices": compare_indices or "all",
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
if (
|
||||
episode_data.recorded_ee_poses is not None
|
||||
and episode_data.recorded_ee_poses.size
|
||||
and episode_data.fk_ee_poses.size
|
||||
and episode_data.recorded_ee_poses.shape
|
||||
== episode_data.fk_ee_poses.shape
|
||||
):
|
||||
position_gap = float(
|
||||
np.max(
|
||||
np.abs(
|
||||
episode_data.recorded_ee_poses[..., :3]
|
||||
- episode_data.fk_ee_poses[..., :3]
|
||||
)
|
||||
)
|
||||
)
|
||||
orientation_gap = _max_orientation_gap(
|
||||
episode_data.recorded_ee_poses,
|
||||
episode_data.fk_ee_poses,
|
||||
)
|
||||
if (
|
||||
position_gap > config.ee_pose_position_tolerance
|
||||
or orientation_gap > config.ee_pose_orientation_tolerance
|
||||
):
|
||||
rule_results.append(
|
||||
RuleResult(
|
||||
rule_id="fk_ee_pose_mismatch",
|
||||
status="warning",
|
||||
severity="non_blocking",
|
||||
message="recorded EE pose differs from FK result",
|
||||
metrics={
|
||||
"position_gap": round(position_gap, 6),
|
||||
"orientation_gap": round(orientation_gap, 6),
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
return _aggregate_rule_results(episode_data, rule_results)
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,151 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Inspect Error Log</title>
|
||||
<style>
|
||||
:root {
|
||||
color-scheme: light;
|
||||
--bg: #f5efe7;
|
||||
--panel: rgba(255, 255, 255, 0.82);
|
||||
--line: rgba(76, 58, 45, 0.14);
|
||||
--text: #1f1712;
|
||||
--muted: #6f655d;
|
||||
--primary: #255f85;
|
||||
--primary-soft: rgba(37, 95, 133, 0.12);
|
||||
}
|
||||
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: "Segoe UI", "PingFang SC", sans-serif;
|
||||
background:
|
||||
radial-gradient(circle at top left, rgba(37, 95, 133, 0.14), transparent 38%),
|
||||
linear-gradient(180deg, #f8f3ed 0%, var(--bg) 100%);
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.shell {
|
||||
width: calc(100vw - 40px);
|
||||
margin: 0 auto;
|
||||
padding: 28px 0 36px;
|
||||
}
|
||||
|
||||
.hero,
|
||||
.error-block,
|
||||
.empty-state {
|
||||
width: 100%;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 20px;
|
||||
background: var(--panel);
|
||||
box-shadow: 0 16px 40px rgba(31, 23, 18, 0.08);
|
||||
}
|
||||
|
||||
.hero {
|
||||
padding: 24px;
|
||||
margin-bottom: 18px;
|
||||
}
|
||||
|
||||
.eyebrow {
|
||||
color: var(--primary);
|
||||
font-size: 13px;
|
||||
font-weight: 700;
|
||||
letter-spacing: 0.08em;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin: 10px 0 12px;
|
||||
font-size: 30px;
|
||||
}
|
||||
|
||||
.sub {
|
||||
margin: 0;
|
||||
color: var(--muted);
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.meta {
|
||||
margin-top: 14px;
|
||||
color: var(--muted);
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.log-list {
|
||||
display: grid;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.error-block {
|
||||
padding: 20px;
|
||||
scroll-margin-top: 20px;
|
||||
}
|
||||
|
||||
.error-head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
|
||||
.error-anchor {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border-radius: 999px;
|
||||
background: var(--primary-soft);
|
||||
color: var(--primary);
|
||||
text-decoration: none;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
code,
|
||||
pre {
|
||||
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
|
||||
}
|
||||
|
||||
code {
|
||||
font-size: 14px;
|
||||
word-break: break-all;
|
||||
}
|
||||
|
||||
.error-body {
|
||||
margin: 0;
|
||||
padding: 16px;
|
||||
overflow-x: auto;
|
||||
border-radius: 16px;
|
||||
border: 1px solid var(--line);
|
||||
background: #fffdfa;
|
||||
line-height: 1.6;
|
||||
white-space: pre;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
padding: 28px;
|
||||
text-align: center;
|
||||
color: var(--muted);
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<main class="shell">
|
||||
<section class="hero">
|
||||
<div class="eyebrow">Inspect Error Log</div>
|
||||
<h1>错误摘要跳转页</h1>
|
||||
<p class="sub">
|
||||
这个页面是 <code>inspect_error_log.log</code> 的 HTML 视图。人工复核页里的系统简写
|
||||
note 会直接跳到这里对应的错误 block。
|
||||
</p>
|
||||
<div class="meta">Generated At: __INSPECT_ERROR_LOG_GENERATED_AT__</div>
|
||||
</section>
|
||||
|
||||
<section class="log-list">__INSPECT_ERROR_LOG_BLOCKS__</section>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,383 @@
|
||||
# Project RoboOrchard
|
||||
#
|
||||
# Copyright (c) 2024-2025 Horizon Robotics. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied. See the License for the specific language governing
|
||||
# permissions and limitations under the License.
|
||||
|
||||
"""Shared UBT dual 7-axis arm constants and helpers for checker / packer."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import numpy as np
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
ARM_DIM = 7
|
||||
POSITION_INDEX = 7
|
||||
STATE_INDEX = 8
|
||||
RECORDED_JOINT_DIM_PER_ARM = 9
|
||||
PACKED_JOINT_DIM_PER_ARM = 8
|
||||
PACKED_JOINT_DIM_DUAL = 16
|
||||
|
||||
LEFT_EE_LINK_NAME = "8_Link"
|
||||
RIGHT_EE_LINK_NAME = "14_Link"
|
||||
LEFT_CAMERA_PARENT_LINK_NAME = "L_wrist_pitch_link"
|
||||
RIGHT_CAMERA_PARENT_LINK_NAME = "R_wrist_pitch_link"
|
||||
MIDDLE_CAMERA_PARENT_LINK_NAME = "prismatic_link"
|
||||
|
||||
LEFT_ARM_JOINT_NAMES = [
|
||||
"L_shoulder_pitch_joint",
|
||||
"L_shoulder_roll_joint",
|
||||
"L_shoulder_yaw_joint",
|
||||
"L_elbow_roll_joint",
|
||||
"L_elbow_yaw_joint",
|
||||
"L_wrist_roll_joint",
|
||||
"L_wrist_pitch_joint",
|
||||
]
|
||||
RIGHT_ARM_JOINT_NAMES = [
|
||||
"R_shoulder_pitch_joint",
|
||||
"R_shoulder_roll_joint",
|
||||
"R_shoulder_yaw_joint",
|
||||
"R_elbow_roll_joint",
|
||||
"R_elbow_yaw_joint",
|
||||
"R_wrist_roll_joint",
|
||||
"R_wrist_pitch_joint",
|
||||
]
|
||||
|
||||
GRIPPER_POSITION_LOWER = 0.0
|
||||
GRIPPER_POSITION_UPPER = 1.0
|
||||
# Gripper state: 0 = open, 1 = closed (right arm).
|
||||
# Left (VR) arm firmware reports -1 when uninitialized, so lower bound is -1.
|
||||
GRIPPER_STATE_LOWER = -1.0
|
||||
GRIPPER_STATE_UPPER = 1.0
|
||||
|
||||
LEFT_SLAVE_JOINT_TOPIC = "/observation/robot_state/left/joint"
|
||||
RIGHT_SLAVE_JOINT_TOPIC = "/observation/robot_state/right/joint"
|
||||
LEFT_MASTER_JOINT_TOPIC = "/observation/robot_state/left_master/joint"
|
||||
RIGHT_MASTER_JOINT_TOPIC = "/observation/robot_state/right_master/joint"
|
||||
|
||||
UBT_FRAME_ID_EXTRINSIC_MAP = {
|
||||
"middle_camera_color_optical_frame/prismatic_link": "middle",
|
||||
"left_camera_color_optical_frame/L_wrist_pitch_link": "left",
|
||||
"right_camera_color_optical_frame/R_wrist_pitch_link": "right",
|
||||
}
|
||||
|
||||
|
||||
def get_slave_joint_topics() -> dict[str, str]:
|
||||
"""Return logical slave joint stream names."""
|
||||
|
||||
return {
|
||||
"left": LEFT_SLAVE_JOINT_TOPIC,
|
||||
"right": RIGHT_SLAVE_JOINT_TOPIC,
|
||||
}
|
||||
|
||||
|
||||
def get_master_joint_topics() -> dict[str, str]:
|
||||
"""Return logical master joint stream names."""
|
||||
|
||||
return {
|
||||
"left": LEFT_MASTER_JOINT_TOPIC,
|
||||
"right": RIGHT_MASTER_JOINT_TOPIC,
|
||||
}
|
||||
|
||||
|
||||
def recorded_joint_names() -> list[str]:
|
||||
"""Return joint names aligned with the 18-D checker joint vector."""
|
||||
|
||||
return (
|
||||
LEFT_ARM_JOINT_NAMES
|
||||
+ ["left_gripper_position"]
|
||||
+ ["left_gripper_state"]
|
||||
+ RIGHT_ARM_JOINT_NAMES
|
||||
+ ["right_gripper_position"]
|
||||
+ ["right_gripper_state"]
|
||||
)
|
||||
|
||||
|
||||
def gripper_state_joint_indices(joint_limit_names: list[str]) -> list[int]:
|
||||
"""Return indices of gripper state columns in a recorded joint vector."""
|
||||
|
||||
return [
|
||||
idx
|
||||
for idx, name in enumerate(joint_limit_names)
|
||||
if name.endswith("_gripper_state")
|
||||
]
|
||||
|
||||
|
||||
def gripper_position_joint_indices(joint_limit_names: list[str]) -> list[int]:
|
||||
"""Return indices of gripper width columns in a recorded joint vector."""
|
||||
|
||||
return [
|
||||
idx
|
||||
for idx, name in enumerate(joint_limit_names)
|
||||
if name.endswith("_gripper_position")
|
||||
]
|
||||
|
||||
|
||||
def reshape_joint_component(
|
||||
values: Any,
|
||||
topic: str,
|
||||
field_name: str,
|
||||
uuid: str,
|
||||
parse_warnings: list[str] | None,
|
||||
*,
|
||||
recorded_joint_dim: int = RECORDED_JOINT_DIM_PER_ARM,
|
||||
) -> np.ndarray:
|
||||
"""Normalize UBT JointState components to 7 arm + position + state.
|
||||
|
||||
Layout per arm (9-dim canonical):
|
||||
|
||||
* ``idx 0–6``: arm joints
|
||||
* ``idx 7``: gripper position (width feedback / cmd)
|
||||
* ``idx 8``: gripper state (0=open, 1=closed)
|
||||
|
||||
Any ``N > 9`` layout keeps the first 9 columns and discards the rest.
|
||||
"""
|
||||
|
||||
component = np.asarray(values, dtype="float64")
|
||||
if component.ndim == 2 and component.shape[1] == 0:
|
||||
return np.zeros(
|
||||
(component.shape[0], recorded_joint_dim),
|
||||
dtype="float64",
|
||||
)
|
||||
if component.size == 0:
|
||||
return component.reshape(-1, recorded_joint_dim)
|
||||
|
||||
if component.ndim == 1:
|
||||
for width in (10, recorded_joint_dim):
|
||||
if component.size % width == 0:
|
||||
component = component.reshape(-1, width)
|
||||
break
|
||||
|
||||
if component.ndim == 2 and component.shape[1] > recorded_joint_dim:
|
||||
if field_name == "position":
|
||||
extra = component.shape[1] - recorded_joint_dim
|
||||
warning = (
|
||||
f"{uuid} | {topic} - position has {component.shape[1]} dims; "
|
||||
f"keep first {recorded_joint_dim} "
|
||||
f"(7 arm + position + state), drop {extra} trailing field(s)"
|
||||
)
|
||||
logger.warning(warning)
|
||||
if parse_warnings is not None:
|
||||
parse_warnings.append(warning)
|
||||
component = component[:, :recorded_joint_dim]
|
||||
|
||||
if component.ndim == 2 and component.shape[1] == recorded_joint_dim:
|
||||
return component
|
||||
|
||||
return component.reshape(-1, recorded_joint_dim)
|
||||
|
||||
|
||||
def zero_gripper_velocity_effort(component: np.ndarray) -> np.ndarray:
|
||||
"""Zero velocity/effort placeholders on gripper position/state columns."""
|
||||
|
||||
if component.ndim != 2 or component.shape[1] < RECORDED_JOINT_DIM_PER_ARM:
|
||||
return component
|
||||
|
||||
output = np.array(component, copy=True)
|
||||
for arm_offset in (0, RECORDED_JOINT_DIM_PER_ARM):
|
||||
for idx in (POSITION_INDEX, STATE_INDEX):
|
||||
col = arm_offset + idx
|
||||
if col < output.shape[1]:
|
||||
output[:, col] = 0.0
|
||||
return output
|
||||
|
||||
|
||||
def pack_slave_joint_positions(left: np.ndarray, right: np.ndarray) -> np.ndarray:
|
||||
"""Pack arm + position feedback into the 16-D stage-1 joint vector."""
|
||||
|
||||
return np.concatenate(
|
||||
[left[:, :PACKED_JOINT_DIM_PER_ARM], right[:, :PACKED_JOINT_DIM_PER_ARM]],
|
||||
axis=-1,
|
||||
)
|
||||
|
||||
|
||||
def pack_master_joint_positions(
|
||||
left_slave: np.ndarray,
|
||||
right_slave: np.ndarray,
|
||||
) -> np.ndarray:
|
||||
"""Build 16-D master joint vector with position cmd reused from slave."""
|
||||
|
||||
packed = np.zeros(
|
||||
(left_slave.shape[0], PACKED_JOINT_DIM_DUAL),
|
||||
dtype=np.float64,
|
||||
)
|
||||
packed[:, POSITION_INDEX] = left_slave[:, POSITION_INDEX]
|
||||
packed[:, PACKED_JOINT_DIM_PER_ARM + POSITION_INDEX] = right_slave[
|
||||
:, POSITION_INDEX
|
||||
]
|
||||
return packed
|
||||
|
||||
|
||||
def pack_gripper_state(left_slave: np.ndarray, right_slave: np.ndarray) -> np.ndarray:
|
||||
"""Pack left/right gripper state feedback into ``[T, 2]``."""
|
||||
|
||||
return np.concatenate(
|
||||
[left_slave[:, STATE_INDEX : STATE_INDEX + 1], right_slave[:, STATE_INDEX : STATE_INDEX + 1]],
|
||||
axis=-1,
|
||||
)
|
||||
|
||||
|
||||
def pack_master_gripper_state(
|
||||
left_master: np.ndarray,
|
||||
right_master: np.ndarray,
|
||||
) -> np.ndarray:
|
||||
"""Pack left/right gripper state cmd into ``[T, 2]``."""
|
||||
|
||||
return np.concatenate(
|
||||
[
|
||||
left_master[:, STATE_INDEX : STATE_INDEX + 1],
|
||||
right_master[:, STATE_INDEX : STATE_INDEX + 1],
|
||||
],
|
||||
axis=-1,
|
||||
)
|
||||
|
||||
|
||||
class UbtKinematicsMixin:
|
||||
"""Shared FK helpers for UBT checker and packer."""
|
||||
|
||||
left_ee_link_name = LEFT_EE_LINK_NAME
|
||||
right_ee_link_name = RIGHT_EE_LINK_NAME
|
||||
left_camera_parent_link_name = LEFT_CAMERA_PARENT_LINK_NAME
|
||||
right_camera_parent_link_name = RIGHT_CAMERA_PARENT_LINK_NAME
|
||||
middle_camera_parent_link_name = MIDDLE_CAMERA_PARENT_LINK_NAME
|
||||
left_arm_joint_names = LEFT_ARM_JOINT_NAMES
|
||||
right_arm_joint_names = RIGHT_ARM_JOINT_NAMES
|
||||
|
||||
def _joint_parameter_index(self) -> dict[str, int]:
|
||||
cached = getattr(self, "_joint_parameter_index_cache", None)
|
||||
if cached is not None:
|
||||
return cached
|
||||
|
||||
joint_names = list(
|
||||
self.chain.get_joint_parameter_names(exclude_fixed=True)
|
||||
)
|
||||
self._joint_parameter_index_cache = {
|
||||
name: idx for idx, name in enumerate(joint_names)
|
||||
}
|
||||
return self._joint_parameter_index_cache
|
||||
|
||||
def forward_kinematics(self, left_joint, right_joint):
|
||||
"""Run FK for UBT dual 7-axis arm state (FK consumes arm joints only)."""
|
||||
|
||||
index = self._joint_parameter_index()
|
||||
joint_names = list(
|
||||
self.chain.get_joint_parameter_names(exclude_fixed=True)
|
||||
)
|
||||
joint = np.zeros([left_joint.shape[0], len(joint_names)])
|
||||
|
||||
if left_joint is not None:
|
||||
for recorded_idx, name in enumerate(self.left_arm_joint_names):
|
||||
joint[:, index[name]] = left_joint[:, recorded_idx]
|
||||
if right_joint is not None:
|
||||
for recorded_idx, name in enumerate(self.right_arm_joint_names):
|
||||
joint[:, index[name]] = right_joint[:, recorded_idx]
|
||||
|
||||
return self.chain.forward_kinematics(joint)
|
||||
|
||||
def get_recorded_joint_limits(
|
||||
self,
|
||||
) -> tuple[list[str], np.ndarray, np.ndarray]:
|
||||
"""Return limits aligned with UBT's 18-D recorded joint state."""
|
||||
|
||||
cached = getattr(self, "_recorded_joint_limit_cache", None)
|
||||
if cached is not None:
|
||||
return cached
|
||||
|
||||
joint_names = list(
|
||||
self.chain.get_joint_parameter_names(exclude_fixed=True)
|
||||
)
|
||||
joint_lower_limits, joint_upper_limits = self.chain.get_joint_limits()
|
||||
lower = np.asarray(joint_lower_limits, dtype=np.float64)
|
||||
upper = np.asarray(joint_upper_limits, dtype=np.float64)
|
||||
index = {name: idx for idx, name in enumerate(joint_names)}
|
||||
|
||||
names = recorded_joint_names()
|
||||
missing = [
|
||||
name
|
||||
for name in self.left_arm_joint_names + self.right_arm_joint_names
|
||||
if name not in index
|
||||
]
|
||||
if missing:
|
||||
raise ValueError(
|
||||
"UBT URDF missing expected arm joints: " + ", ".join(missing)
|
||||
)
|
||||
|
||||
recorded_joint_lower_limits = np.array(
|
||||
[lower[index[name]] for name in self.left_arm_joint_names]
|
||||
+ [GRIPPER_POSITION_LOWER]
|
||||
+ [GRIPPER_STATE_LOWER]
|
||||
+ [lower[index[name]] for name in self.right_arm_joint_names]
|
||||
+ [GRIPPER_POSITION_LOWER]
|
||||
+ [GRIPPER_STATE_LOWER],
|
||||
dtype=np.float64,
|
||||
)
|
||||
recorded_joint_upper_limits = np.array(
|
||||
[upper[index[name]] for name in self.left_arm_joint_names]
|
||||
+ [GRIPPER_POSITION_UPPER]
|
||||
+ [GRIPPER_STATE_UPPER]
|
||||
+ [upper[index[name]] for name in self.right_arm_joint_names]
|
||||
+ [GRIPPER_POSITION_UPPER]
|
||||
+ [GRIPPER_STATE_UPPER],
|
||||
dtype=np.float64,
|
||||
)
|
||||
|
||||
self._recorded_joint_limit_cache = (
|
||||
names,
|
||||
recorded_joint_lower_limits,
|
||||
recorded_joint_upper_limits,
|
||||
)
|
||||
return self._recorded_joint_limit_cache
|
||||
|
||||
def _link_pose_matrix(self, poses, link_name: str) -> np.ndarray:
|
||||
return poses[link_name].get_matrix().numpy()
|
||||
|
||||
def _camera_parent_poses(
|
||||
self,
|
||||
poses,
|
||||
left_ee_pose: np.ndarray,
|
||||
right_ee_pose: np.ndarray,
|
||||
) -> dict[str, np.ndarray | None]:
|
||||
return {
|
||||
"left": self._link_pose_matrix(
|
||||
poses, self.left_camera_parent_link_name
|
||||
),
|
||||
"right": self._link_pose_matrix(
|
||||
poses, self.right_camera_parent_link_name
|
||||
),
|
||||
"middle": self._link_pose_matrix(
|
||||
poses, self.middle_camera_parent_link_name
|
||||
),
|
||||
}
|
||||
|
||||
def compute_camera_extrinsics(
|
||||
self,
|
||||
image_extrinsic: dict[str, np.ndarray],
|
||||
camera_parent_poses: dict[str, np.ndarray | None],
|
||||
) -> dict[str, np.ndarray]:
|
||||
"""Convert static camera extrinsics into per-step camera-to-base matrices."""
|
||||
|
||||
middle_camera_parent_pose = camera_parent_poses.get("middle")
|
||||
return {
|
||||
"left": image_extrinsic["left"]
|
||||
@ np.linalg.inv(camera_parent_poses["left"]),
|
||||
"right": image_extrinsic["right"]
|
||||
@ np.linalg.inv(camera_parent_poses["right"]),
|
||||
"middle": image_extrinsic["middle"]
|
||||
@ np.linalg.inv(middle_camera_parent_pose)
|
||||
if middle_camera_parent_pose is not None
|
||||
else np.copy(image_extrinsic["middle"]),
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,3 @@
|
||||
"""Drobodata UBT MCAP preparation, checking, and reporting pipeline."""
|
||||
|
||||
__version__ = "1.0.0"
|
||||
263
lightweight-operators/ubt-mcap-pipeline/ubt_pipeline/check.py
Normal file
263
lightweight-operators/ubt-mcap-pipeline/ubt_pipeline/check.py
Normal file
@ -0,0 +1,263 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import multiprocessing
|
||||
import os
|
||||
import pathlib
|
||||
import shutil
|
||||
from dataclasses import asdict
|
||||
from typing import Any
|
||||
|
||||
from .common import (
|
||||
artifact,
|
||||
csv_values,
|
||||
emit_result,
|
||||
env_bool,
|
||||
env_int,
|
||||
env_text,
|
||||
find_json_payload,
|
||||
load_input_artifacts,
|
||||
normalize_dates,
|
||||
write_json,
|
||||
)
|
||||
|
||||
OPERATOR_ID = "ubt-mcap-batch-check"
|
||||
ROBO_ORCHARD_SOURCE_COMMIT = "c742b39e0821590605385ee4ddabcafb258432d8"
|
||||
|
||||
|
||||
def _inspect_config():
|
||||
from robo_orchard_lab.dataset.ubt_manipulation.tools.check_config import (
|
||||
InspectConfig,
|
||||
build_default_inspect_config,
|
||||
)
|
||||
|
||||
value = env_text("INSPECT_CONFIG_JSON")
|
||||
defaults = asdict(build_default_inspect_config())
|
||||
if value:
|
||||
overrides = json.loads(value)
|
||||
if not isinstance(overrides, dict):
|
||||
raise ValueError("INSPECT_CONFIG_JSON must decode to an object")
|
||||
unknown = sorted(set(overrides) - set(defaults))
|
||||
if unknown:
|
||||
raise ValueError(
|
||||
f"INSPECT_CONFIG_JSON contains unknown keys: {', '.join(unknown)}"
|
||||
)
|
||||
defaults.update(overrides)
|
||||
return InspectConfig(**defaults)
|
||||
|
||||
|
||||
def _run_handlers(checker: Any) -> list[dict[str, Any]]:
|
||||
indexes = list(range(len(checker.episodes)))
|
||||
if checker.num_workers > 1:
|
||||
with multiprocessing.Pool(processes=checker.num_workers) as pool:
|
||||
results = pool.map(checker.handler, indexes)
|
||||
else:
|
||||
results = [checker.handler(index) for index in indexes]
|
||||
results.sort(key=lambda item: item["uuid"])
|
||||
return results
|
||||
|
||||
|
||||
def classify_report(item: dict[str, Any]) -> str:
|
||||
from robo_orchard_lab.dataset.ubt_manipulation.tools.utils import (
|
||||
build_report_row,
|
||||
is_quick_screen_pass_row,
|
||||
)
|
||||
|
||||
if item.get("runtime_error") or item.get("video_runtime_error"):
|
||||
return "RUNTIME_ERROR"
|
||||
return "PASS" if is_quick_screen_pass_row(build_report_row(item)) else "ERROR"
|
||||
|
||||
|
||||
def _episode_name(item: dict[str, Any]) -> str:
|
||||
source_path = pathlib.Path(
|
||||
str(item.get("source_path") or item.get("mcap_path") or item.get("uuid"))
|
||||
)
|
||||
for candidate in (source_path, *source_path.parents):
|
||||
if candidate.name.startswith("episode_"):
|
||||
return candidate.name
|
||||
uuid = str(item.get("uuid", ""))
|
||||
return uuid if uuid.startswith("episode_") else f"episode_{uuid}"
|
||||
|
||||
|
||||
def _result_payload(
|
||||
raw: dict[str, Any], prepared_entry: dict[str, Any] | None
|
||||
) -> dict[str, Any]:
|
||||
status = classify_report(raw)
|
||||
episode = _episode_name(raw)
|
||||
return {
|
||||
"episode": episode,
|
||||
"date": episode[len("episode_") : len("episode_") + 10],
|
||||
"status": status,
|
||||
"sourcePath": raw.get("source_path", ""),
|
||||
"mcapPath": raw.get("mcap_path", ""),
|
||||
"preparedPath": (
|
||||
prepared_entry.get("preparedPath", "")
|
||||
if prepared_entry
|
||||
else raw.get("source_path", "")
|
||||
),
|
||||
"source": prepared_entry.get("source", {}) if prepared_entry else {},
|
||||
"runtimeError": raw.get("runtime_error") or raw.get("video_runtime_error"),
|
||||
"episodeStatus": raw.get("episode_status"),
|
||||
"ruleResults": [
|
||||
rule if isinstance(rule, dict) else rule.to_dict()
|
||||
for rule in raw.get("rule_results", [])
|
||||
],
|
||||
"episodeMetrics": raw.get("episode_metrics", {}),
|
||||
"topicSummaries": raw.get("topic_summaries", {}),
|
||||
"numSteps": raw.get("num_steps", 0),
|
||||
"allSteps": raw.get("all_steps", 0),
|
||||
"nonStaticSteps": raw.get("non_static_steps", 0),
|
||||
"videoFile": raw.get("video_file"),
|
||||
}
|
||||
|
||||
|
||||
def _rule_statistics(results: list[dict[str, Any]]) -> dict[str, int]:
|
||||
counts: dict[str, int] = {}
|
||||
for result in results:
|
||||
for rule in result["ruleResults"]:
|
||||
if str(rule.get("status", "")).lower() in {"pass", "ok"}:
|
||||
continue
|
||||
rule_id = str(rule.get("rule_id", "unknown"))
|
||||
counts[rule_id] = counts.get(rule_id, 0) + 1
|
||||
return dict(sorted(counts.items()))
|
||||
|
||||
|
||||
def run_check() -> None:
|
||||
try:
|
||||
from robo_orchard_lab.dataset.ubt_manipulation.tools.mcap_checker_ubt import (
|
||||
UbtMcapChecker,
|
||||
)
|
||||
except ImportError as exc:
|
||||
raise RuntimeError(f"UBT checker dependency is missing: {exc}") from exc
|
||||
|
||||
prepared = find_json_payload(load_input_artifacts(), "prepared-manifest.json")
|
||||
prepared_root = pathlib.Path(str(prepared.get("preparedRoot", "")))
|
||||
urdf_path = pathlib.Path(str(prepared.get("urdfPath", "")))
|
||||
if not prepared_root.is_dir():
|
||||
raise RuntimeError(f"prepared data directory is unreadable: {prepared_root}")
|
||||
if not urdf_path.is_file():
|
||||
raise RuntimeError(f"prepared URDF is unreadable: {urdf_path}")
|
||||
|
||||
dates = normalize_dates(env_text("DATE_PREFIXES"))
|
||||
users = csv_values(env_text("USER_NAMES", prepared.get("userName", "ubt")))
|
||||
tasks = csv_values(env_text("TASK_NAMES", prepared.get("taskName", "test_task")))
|
||||
num_workers = max(1, env_int("NUM_WORKERS", 1))
|
||||
max_episodes = max(0, env_int("MAX_EPISODES", 0))
|
||||
video_export = env_bool("VIDEO_EXPORT", False)
|
||||
fail_on_quality_error = env_bool("FAIL_ON_QUALITY_ERROR", False)
|
||||
vis_interval = max(1, env_int("VIS_INTERVAL", 3))
|
||||
vis_resize_scale = float(env_text("VIS_RESIZE_SCALE", "1.0"))
|
||||
vis_depth = env_bool("VIS_DEPTH", True)
|
||||
|
||||
output_dir = pathlib.Path(env_text("OUTPUT_DIR", "/tmp/droboticflow-ubt-check"))
|
||||
raw_output = output_dir / "raw-checker"
|
||||
if raw_output.exists():
|
||||
shutil.rmtree(raw_output)
|
||||
raw_output.mkdir(parents=True)
|
||||
|
||||
checker = UbtMcapChecker(
|
||||
input_path=str(prepared_root),
|
||||
output_path=str(raw_output),
|
||||
urdf=str(urdf_path),
|
||||
task_names=tasks or None,
|
||||
user_names=users or None,
|
||||
date_prefix=",".join(dates) if dates else None,
|
||||
num_workers=num_workers,
|
||||
vis_interval=vis_interval,
|
||||
vis_resize_scale=vis_resize_scale,
|
||||
vis_depth=vis_depth,
|
||||
inspect_config=_inspect_config(),
|
||||
skip_video_export=not video_export,
|
||||
)
|
||||
matched_episode_count = len(checker.episodes)
|
||||
if matched_episode_count == 0:
|
||||
raise RuntimeError("UBT checker matched zero episodes")
|
||||
if max_episodes:
|
||||
checker.episodes = checker.episodes[:max_episodes]
|
||||
|
||||
raw_results = _run_handlers(checker)
|
||||
if video_export:
|
||||
checker.render_videos(raw_results)
|
||||
checker.concat_videos(
|
||||
[item["video_file"] for item in raw_results if item.get("video_file")]
|
||||
)
|
||||
checker.write_job_outputs(raw_results)
|
||||
checker.write_manual_review_artifacts(raw_results)
|
||||
|
||||
prepared_by_name = {
|
||||
str(item.get("episode")): item
|
||||
for item in prepared.get("episodes", [])
|
||||
if isinstance(item, dict)
|
||||
}
|
||||
episode_results = [
|
||||
_result_payload(item, prepared_by_name.get(_episode_name(item)))
|
||||
for item in raw_results
|
||||
]
|
||||
status_counts = {
|
||||
status: sum(1 for item in episode_results if item["status"] == status)
|
||||
for status in ("PASS", "ERROR", "RUNTIME_ERROR")
|
||||
}
|
||||
summary = {
|
||||
"schemaVersion": "1.0",
|
||||
"datasetId": prepared.get("datasetId"),
|
||||
"datasetFingerprint": prepared.get("datasetFingerprint"),
|
||||
"urdfFingerprint": prepared.get("urdfFingerprint"),
|
||||
"sourceCommit": ROBO_ORCHARD_SOURCE_COMMIT,
|
||||
"imageDigest": env_text("IMAGE_DIGEST", env_text("DROBOTICFLOW_IMAGE_DIGEST")),
|
||||
"datePrefixes": dates,
|
||||
"userNames": users,
|
||||
"taskNames": tasks,
|
||||
"matchedEpisodeCount": matched_episode_count,
|
||||
"processedEpisodeCount": len(episode_results),
|
||||
"maxEpisodes": max_episodes,
|
||||
"videoExport": video_export,
|
||||
"statusCounts": status_counts,
|
||||
"passRate": status_counts["PASS"] / len(episode_results),
|
||||
"ruleStatistics": _rule_statistics(episode_results),
|
||||
"inspectConfig": asdict(checker.inspect_config),
|
||||
}
|
||||
episode_results_path = output_dir / "episode-results.json"
|
||||
checker_summary_path = output_dir / "checker-summary.json"
|
||||
write_json(episode_results_path, episode_results)
|
||||
write_json(checker_summary_path, summary)
|
||||
|
||||
artifacts = [
|
||||
artifact(
|
||||
checker_summary_path,
|
||||
port_id="checker-results",
|
||||
port_name="质检结果",
|
||||
data=summary,
|
||||
),
|
||||
artifact(
|
||||
episode_results_path,
|
||||
port_id="checker-results",
|
||||
port_name="质检结果",
|
||||
data=episode_results,
|
||||
),
|
||||
]
|
||||
for path in sorted(raw_output.rglob("*")):
|
||||
if path.is_file():
|
||||
artifacts.append(
|
||||
artifact(
|
||||
path,
|
||||
port_id="checker-results",
|
||||
port_name="质检结果",
|
||||
relative_path=f"raw-checker/{path.relative_to(raw_output)}",
|
||||
)
|
||||
)
|
||||
emit_result(
|
||||
output_dir,
|
||||
OPERATOR_ID,
|
||||
artifacts,
|
||||
[
|
||||
f"matched episodes: {matched_episode_count}",
|
||||
f"processed episodes: {len(episode_results)}",
|
||||
f"status counts: {status_counts}",
|
||||
],
|
||||
)
|
||||
if status_counts["RUNTIME_ERROR"]:
|
||||
raise RuntimeError(
|
||||
f"{status_counts['RUNTIME_ERROR']} episode(s) failed at runtime"
|
||||
)
|
||||
if fail_on_quality_error and status_counts["ERROR"]:
|
||||
raise RuntimeError(f"{status_counts['ERROR']} episode(s) failed quality checks")
|
||||
221
lightweight-operators/ubt-mcap-pipeline/ubt_pipeline/common.py
Normal file
221
lightweight-operators/ubt-mcap-pipeline/ubt_pipeline/common.py
Normal file
@ -0,0 +1,221 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import mimetypes
|
||||
import os
|
||||
import pathlib
|
||||
import re
|
||||
import time
|
||||
from typing import Any, Iterable
|
||||
|
||||
DATE_PATTERN = re.compile(r"^\d{4}[-_]\d{2}[-_]\d{2}$")
|
||||
EPISODE_PATTERN = re.compile(r"^episode_(\d{4}_\d{2}_\d{2})-.+")
|
||||
RESULT_PREFIX = "DROBOTICFLOW_RESULT_PATH="
|
||||
|
||||
|
||||
def env_text(name: str, default: str = "") -> str:
|
||||
return os.environ.get(name, default).strip()
|
||||
|
||||
|
||||
def env_bool(name: str, default: bool = False) -> bool:
|
||||
value = env_text(name)
|
||||
if not value:
|
||||
return default
|
||||
if value.lower() in {"1", "true", "yes", "on"}:
|
||||
return True
|
||||
if value.lower() in {"0", "false", "no", "off"}:
|
||||
return False
|
||||
raise ValueError(f"{name} must be a boolean")
|
||||
|
||||
|
||||
def env_int(name: str, default: int) -> int:
|
||||
value = env_text(name)
|
||||
return int(value) if value else default
|
||||
|
||||
|
||||
def csv_values(value: str | None) -> list[str]:
|
||||
if not value:
|
||||
return []
|
||||
return [item.strip() for item in value.split(",") if item.strip()]
|
||||
|
||||
|
||||
def normalize_dates(value: str | None) -> list[str]:
|
||||
dates: list[str] = []
|
||||
for item in csv_values(value):
|
||||
if not DATE_PATTERN.fullmatch(item):
|
||||
raise ValueError(
|
||||
f"invalid date prefix {item!r}; expected YYYY-MM-DD or YYYY_MM_DD"
|
||||
)
|
||||
normalized = item.replace("-", "_")
|
||||
if normalized not in dates:
|
||||
dates.append(normalized)
|
||||
return dates
|
||||
|
||||
|
||||
def safe_identifier(value: str, field: str) -> str:
|
||||
normalized = value.strip()
|
||||
if not normalized or normalized in {".", ".."}:
|
||||
raise ValueError(f"{field} is required")
|
||||
if "/" in normalized or "\\" in normalized or "\x00" in normalized:
|
||||
raise ValueError(f"{field} must be a single path segment")
|
||||
return normalized
|
||||
|
||||
|
||||
def read_json(path: pathlib.Path) -> Any:
|
||||
with path.open("r", encoding="utf-8") as handle:
|
||||
return json.load(handle)
|
||||
|
||||
|
||||
def write_json(path: pathlib.Path, payload: Any) -> None:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with path.open("w", encoding="utf-8") as handle:
|
||||
json.dump(payload, handle, ensure_ascii=False, indent=2, sort_keys=True)
|
||||
handle.write("\n")
|
||||
|
||||
|
||||
def sha256_file(path: pathlib.Path) -> str:
|
||||
digest = hashlib.sha256()
|
||||
with path.open("rb") as handle:
|
||||
for chunk in iter(lambda: handle.read(4 * 1024 * 1024), b""):
|
||||
digest.update(chunk)
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def stable_fingerprint(items: Iterable[dict[str, Any]], urdf_sha256: str) -> str:
|
||||
normalized = [
|
||||
{
|
||||
"key": str(item.get("key", "")),
|
||||
"size": int(item.get("size", 0) or 0),
|
||||
"etag": str(item.get("etag", "")).strip('"'),
|
||||
}
|
||||
for item in items
|
||||
]
|
||||
normalized.sort(key=lambda item: item["key"])
|
||||
encoded = json.dumps(
|
||||
{"objects": normalized, "urdfSha256": urdf_sha256},
|
||||
ensure_ascii=False,
|
||||
separators=(",", ":"),
|
||||
sort_keys=True,
|
||||
).encode("utf-8")
|
||||
return hashlib.sha256(encoded).hexdigest()
|
||||
|
||||
|
||||
def normalize_input_artifacts(raw: Any) -> list[dict[str, Any]]:
|
||||
if isinstance(raw, list):
|
||||
return [item for item in raw if isinstance(item, dict)]
|
||||
if isinstance(raw, dict):
|
||||
for key in ("items", "artifacts", "inputArtifacts"):
|
||||
value = raw.get(key)
|
||||
if isinstance(value, list):
|
||||
return [item for item in value if isinstance(item, dict)]
|
||||
return []
|
||||
|
||||
|
||||
def load_input_artifacts() -> list[dict[str, Any]]:
|
||||
path = env_text("INPUT_ARTIFACTS_PATH")
|
||||
if not path:
|
||||
return []
|
||||
return normalize_input_artifacts(read_json(pathlib.Path(path)))
|
||||
|
||||
|
||||
def artifact_payload(artifact: dict[str, Any]) -> Any:
|
||||
data = artifact.get("data")
|
||||
if data is not None:
|
||||
return data
|
||||
raw_path = str(artifact.get("path", "")).strip()
|
||||
if raw_path and "://" not in raw_path:
|
||||
path = pathlib.Path(raw_path)
|
||||
if path.is_file() and path.suffix.lower() == ".json":
|
||||
return read_json(path)
|
||||
return None
|
||||
|
||||
|
||||
def find_json_payload(artifacts: list[dict[str, Any]], filename: str) -> dict[str, Any]:
|
||||
for artifact in artifacts:
|
||||
if str(artifact.get("name", "")).strip() != filename:
|
||||
continue
|
||||
payload = artifact_payload(artifact)
|
||||
if isinstance(payload, dict):
|
||||
return payload
|
||||
raise RuntimeError(f"required upstream artifact is missing: {filename}")
|
||||
|
||||
|
||||
def now_iso() -> str:
|
||||
return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
|
||||
|
||||
|
||||
def artifact(
|
||||
path: pathlib.Path,
|
||||
*,
|
||||
port_id: str,
|
||||
port_name: str,
|
||||
artifact_type: str | None = None,
|
||||
data: Any = None,
|
||||
relative_path: str = "",
|
||||
) -> dict[str, Any]:
|
||||
mime_type = mimetypes.guess_type(path.name)[0] or "application/octet-stream"
|
||||
kind = artifact_type or ("json" if path.suffix.lower() == ".json" else "file")
|
||||
result: dict[str, Any] = {
|
||||
"name": path.name,
|
||||
"type": kind,
|
||||
"path": str(path),
|
||||
"portId": port_id,
|
||||
"portName": port_name,
|
||||
"sizeBytes": path.stat().st_size,
|
||||
"metadata": {"mimeType": mime_type, "sha256": sha256_file(path)},
|
||||
}
|
||||
if data is not None:
|
||||
result["data"] = data
|
||||
if relative_path:
|
||||
result["relativePath"] = relative_path
|
||||
return result
|
||||
|
||||
|
||||
def emit_result(
|
||||
output_dir: pathlib.Path,
|
||||
operator_id: str,
|
||||
artifacts: list[dict[str, Any]],
|
||||
messages: list[str],
|
||||
) -> None:
|
||||
output_dir.mkdir(parents=True, exist_ok=True)
|
||||
timestamp = now_iso()
|
||||
result = {
|
||||
"logs": [
|
||||
{
|
||||
"timestamp": timestamp,
|
||||
"level": "INFO",
|
||||
"message": message,
|
||||
"operator": operator_id,
|
||||
}
|
||||
for message in messages
|
||||
],
|
||||
"artifacts": artifacts,
|
||||
"resourceSeries": [],
|
||||
}
|
||||
result_path = pathlib.Path(
|
||||
env_text("RESULT_OUTPUT_PATH", str(output_dir / "result.json"))
|
||||
)
|
||||
write_json(result_path, result)
|
||||
stage_manifest_path = pathlib.Path(
|
||||
env_text("STAGE_MANIFEST_PATH", str(output_dir / "stage-manifest.json"))
|
||||
)
|
||||
write_json(
|
||||
stage_manifest_path,
|
||||
{
|
||||
"operatorId": operator_id,
|
||||
"dataRefs": [
|
||||
{
|
||||
"name": item["name"],
|
||||
"path": item["path"],
|
||||
"type": item["type"],
|
||||
"portId": item["portId"],
|
||||
"portName": item["portName"],
|
||||
"sha256": item["metadata"]["sha256"],
|
||||
"sizeBytes": item["sizeBytes"],
|
||||
}
|
||||
for item in artifacts
|
||||
],
|
||||
},
|
||||
)
|
||||
print(f"{RESULT_PREFIX}{result_path}", flush=True)
|
||||
418
lightweight-operators/ubt-mcap-pipeline/ubt_pipeline/prepare.py
Normal file
418
lightweight-operators/ubt-mcap-pipeline/ubt_pipeline/prepare.py
Normal file
@ -0,0 +1,418 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import fnmatch
|
||||
import os
|
||||
import pathlib
|
||||
import shutil
|
||||
import stat
|
||||
import tempfile
|
||||
import urllib.parse
|
||||
import xml.etree.ElementTree as ET
|
||||
import zipfile
|
||||
from typing import Any
|
||||
|
||||
from .common import (
|
||||
artifact,
|
||||
emit_result,
|
||||
env_bool,
|
||||
env_int,
|
||||
env_text,
|
||||
load_input_artifacts,
|
||||
safe_identifier,
|
||||
sha256_file,
|
||||
stable_fingerprint,
|
||||
write_json,
|
||||
)
|
||||
|
||||
OPERATOR_ID = "ubt-data-prepare"
|
||||
|
||||
|
||||
def parse_bos_path(value: str) -> tuple[str, str]:
|
||||
parsed = urllib.parse.urlparse(value)
|
||||
if parsed.scheme != "bos" or not parsed.netloc or not parsed.path.lstrip("/"):
|
||||
raise ValueError(f"invalid BOS path: {value}")
|
||||
return parsed.netloc, parsed.path.lstrip("/")
|
||||
|
||||
|
||||
def build_bos_client():
|
||||
try:
|
||||
import baidubce.protocol as protocol
|
||||
from baidubce.auth.bce_credentials import BceCredentials
|
||||
from baidubce.bce_client_configuration import BceClientConfiguration
|
||||
from baidubce.services.bos.bos_client import BosClient
|
||||
except ImportError as exc:
|
||||
raise RuntimeError("BOS SDK is required for UBT data preparation") from exc
|
||||
|
||||
endpoint = env_text("BOS_ENDPOINT")
|
||||
access_key = env_text("BOS_ACCESS_KEY_ID")
|
||||
secret_key = env_text("BOS_SECRET_ACCESS_KEY")
|
||||
if not endpoint or not access_key or not secret_key:
|
||||
raise RuntimeError("BOS_ENDPOINT and BOS credentials are required")
|
||||
parsed = urllib.parse.urlparse(
|
||||
endpoint if "://" in endpoint else f"https://{endpoint}"
|
||||
)
|
||||
config = BceClientConfiguration(
|
||||
credentials=BceCredentials(access_key, secret_key),
|
||||
endpoint=parsed.netloc or parsed.path,
|
||||
protocol=(
|
||||
protocol.HTTPS
|
||||
if (parsed.scheme or "https").lower() == "https"
|
||||
else protocol.HTTP
|
||||
),
|
||||
)
|
||||
return BosClient(config)
|
||||
|
||||
|
||||
def safe_extract_zip(archive_path: pathlib.Path, destination: pathlib.Path) -> None:
|
||||
destination.mkdir(parents=True, exist_ok=True)
|
||||
root = destination.resolve()
|
||||
with zipfile.ZipFile(archive_path) as archive:
|
||||
for member in archive.infolist():
|
||||
target = (destination / member.filename).resolve()
|
||||
if target != root and root not in target.parents:
|
||||
raise RuntimeError(
|
||||
f"archive entry escapes extraction root: {member.filename}"
|
||||
)
|
||||
unix_mode = member.external_attr >> 16
|
||||
if stat.S_ISLNK(unix_mode):
|
||||
raise RuntimeError(f"archive symlink is not allowed: {member.filename}")
|
||||
archive.extractall(destination)
|
||||
|
||||
|
||||
def validate_urdf(path: pathlib.Path) -> str:
|
||||
try:
|
||||
root = ET.parse(path).getroot()
|
||||
except (ET.ParseError, OSError) as exc:
|
||||
raise RuntimeError(f"invalid URDF: {path.name}") from exc
|
||||
if root.tag != "robot" or not list(root.iter("joint")):
|
||||
raise RuntimeError("URDF must contain a robot root and at least one joint")
|
||||
return sha256_file(path)
|
||||
|
||||
|
||||
def discover_episode_dirs(root: pathlib.Path) -> list[pathlib.Path]:
|
||||
return sorted(
|
||||
path
|
||||
for path in root.rglob("episode_*")
|
||||
if path.is_dir() and path.name.startswith("episode_")
|
||||
)
|
||||
|
||||
|
||||
def validate_episode(path: pathlib.Path) -> None:
|
||||
mcap_files = list(path.glob("*.mcap"))
|
||||
if not mcap_files:
|
||||
raise RuntimeError(f"episode is missing MCAP: {path.name}")
|
||||
if any(item.is_dir() for item in path.iterdir()):
|
||||
raise RuntimeError(f"episode contains an unexpected directory: {path.name}")
|
||||
|
||||
|
||||
def claim_episode(path: pathlib.Path, seen: set[str]) -> str:
|
||||
validate_episode(path)
|
||||
episode_name = path.name
|
||||
if episode_name in seen:
|
||||
raise RuntimeError(f"duplicate episode: {episode_name}")
|
||||
seen.add(episode_name)
|
||||
return episode_name
|
||||
|
||||
|
||||
def _source_manifest_entries(artifacts: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
entries: list[dict[str, Any]] = []
|
||||
for item in artifacts:
|
||||
raw_path = str(item.get("path", "")).strip()
|
||||
if not raw_path.startswith("bos://"):
|
||||
continue
|
||||
bucket, key = parse_bos_path(raw_path)
|
||||
metadata = (
|
||||
item.get("metadata") if isinstance(item.get("metadata"), dict) else {}
|
||||
)
|
||||
entries.append(
|
||||
{
|
||||
"bucket": bucket,
|
||||
"key": key,
|
||||
"path": raw_path,
|
||||
"size": int(item.get("sizeBytes", item.get("size", 0)) or 0),
|
||||
"etag": str(metadata.get("etag", metadata.get("eTag", ""))),
|
||||
}
|
||||
)
|
||||
return entries
|
||||
|
||||
|
||||
def _metadata_value(response: Any, *names: str) -> Any:
|
||||
for name in names:
|
||||
if isinstance(response, dict) and response.get(name) is not None:
|
||||
return response[name]
|
||||
value = getattr(response, name, None)
|
||||
if value is not None:
|
||||
return value
|
||||
return None
|
||||
|
||||
|
||||
def enrich_object_metadata(client: Any, item: dict[str, Any]) -> dict[str, Any]:
|
||||
if item["size"] and item["etag"]:
|
||||
return item
|
||||
try:
|
||||
metadata = client.get_object_meta_data(item["bucket"], item["key"])
|
||||
except Exception as exc:
|
||||
raise RuntimeError(f"cannot read BOS object metadata: {item['path']}") from exc
|
||||
enriched = dict(item)
|
||||
enriched["size"] = int(
|
||||
enriched["size"]
|
||||
or _metadata_value(metadata, "content_length", "contentLength", "size")
|
||||
or 0
|
||||
)
|
||||
enriched["etag"] = str(
|
||||
enriched["etag"] or _metadata_value(metadata, "etag", "e_tag", "eTag") or ""
|
||||
).strip('"')
|
||||
return enriched
|
||||
|
||||
|
||||
def download_object(
|
||||
client: Any, item: dict[str, Any], destination: pathlib.Path
|
||||
) -> None:
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
partial = destination.with_name(destination.name + ".part")
|
||||
with contextlib.suppress(FileNotFoundError):
|
||||
partial.unlink()
|
||||
try:
|
||||
client.get_object_to_file(item["bucket"], item["key"], str(partial))
|
||||
if item["size"] and partial.stat().st_size != item["size"]:
|
||||
raise RuntimeError(
|
||||
f"download size mismatch for {item['path']}: "
|
||||
f"{partial.stat().st_size} != {item['size']}"
|
||||
)
|
||||
partial.replace(destination)
|
||||
finally:
|
||||
with contextlib.suppress(FileNotFoundError):
|
||||
partial.unlink()
|
||||
|
||||
|
||||
def verify_cache(cache_dir: pathlib.Path, fingerprint: str) -> dict[str, Any] | None:
|
||||
manifest_path = cache_dir / "prepared-manifest.json"
|
||||
complete_path = cache_dir / ".complete.json"
|
||||
if not manifest_path.is_file() or not complete_path.is_file():
|
||||
return None
|
||||
try:
|
||||
manifest = __import__("json").loads(manifest_path.read_text("utf-8"))
|
||||
complete = __import__("json").loads(complete_path.read_text("utf-8"))
|
||||
except Exception:
|
||||
return None
|
||||
if (
|
||||
manifest.get("datasetFingerprint") != fingerprint
|
||||
or complete.get("datasetFingerprint") != fingerprint
|
||||
):
|
||||
return None
|
||||
episode_paths = manifest.get("episodes")
|
||||
if not isinstance(episode_paths, list) or len(episode_paths) != complete.get(
|
||||
"episodeCount"
|
||||
):
|
||||
return None
|
||||
for episode in episode_paths:
|
||||
prepared = pathlib.Path(str(episode.get("preparedPath", "")))
|
||||
if not prepared.is_dir() or not list(prepared.glob("*.mcap")):
|
||||
return None
|
||||
return manifest
|
||||
|
||||
|
||||
def _disk_free_gib(path: pathlib.Path) -> float:
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
return shutil.disk_usage(path).free / (1024**3)
|
||||
|
||||
|
||||
def _select_objects(
|
||||
entries: list[dict[str, Any]], archive_glob: str, urdf_source_path: str
|
||||
) -> tuple[list[dict[str, Any]], dict[str, Any]]:
|
||||
archives = [
|
||||
item
|
||||
for item in entries
|
||||
if fnmatch.fnmatch(pathlib.PurePosixPath(item["key"]).name, archive_glob)
|
||||
]
|
||||
urdf_matches = [
|
||||
item
|
||||
for item in entries
|
||||
if item["path"] == urdf_source_path
|
||||
or item["key"] == urdf_source_path.lstrip("/")
|
||||
or pathlib.PurePosixPath(item["key"]).name
|
||||
== pathlib.PurePosixPath(urdf_source_path).name
|
||||
]
|
||||
if not archives:
|
||||
raise RuntimeError(f"no archive matches ARCHIVE_GLOB={archive_glob!r}")
|
||||
if len(urdf_matches) != 1:
|
||||
raise RuntimeError(
|
||||
f"URDF_SOURCE_PATH must match exactly one source object; got {len(urdf_matches)}"
|
||||
)
|
||||
return sorted(archives, key=lambda item: item["key"]), urdf_matches[0]
|
||||
|
||||
|
||||
def run_prepare() -> None:
|
||||
dataset_id = safe_identifier(env_text("DATASET_ID"), "DATASET_ID")
|
||||
user_name = safe_identifier(env_text("USER_NAME", "ubt"), "USER_NAME")
|
||||
task_name = safe_identifier(env_text("TASK_NAME", "test_task"), "TASK_NAME")
|
||||
archive_glob = env_text("ARCHIVE_GLOB", "*.zip")
|
||||
urdf_source_path = env_text("URDF_SOURCE_PATH")
|
||||
reuse_prepared = env_bool("REUSE_PREPARED", True)
|
||||
min_free_gib = env_int("MIN_FREE_GIB", 50)
|
||||
duplicate_policy = env_text("ON_DUPLICATE_EPISODE", "error").lower()
|
||||
if duplicate_policy != "error":
|
||||
raise ValueError("ON_DUPLICATE_EPISODE currently supports only 'error'")
|
||||
|
||||
prepared_root = pathlib.Path(
|
||||
env_text("PREPARED_ROOT", "/data/ubt-prepared")
|
||||
).resolve()
|
||||
free_gib = _disk_free_gib(prepared_root)
|
||||
if free_gib < min_free_gib:
|
||||
raise RuntimeError(
|
||||
f"shared prepared storage has {free_gib:.2f} GiB free; "
|
||||
f"MIN_FREE_GIB={min_free_gib}"
|
||||
)
|
||||
|
||||
client = build_bos_client()
|
||||
entries = [
|
||||
enrich_object_metadata(client, item)
|
||||
for item in _source_manifest_entries(load_input_artifacts())
|
||||
]
|
||||
archives, urdf_object = _select_objects(entries, archive_glob, urdf_source_path)
|
||||
|
||||
scratch_root = prepared_root / ".staging"
|
||||
scratch_root.mkdir(parents=True, exist_ok=True)
|
||||
with tempfile.TemporaryDirectory(
|
||||
prefix=f"{dataset_id}-", dir=scratch_root
|
||||
) as scratch_text:
|
||||
scratch = pathlib.Path(scratch_text)
|
||||
urdf_local = scratch / pathlib.PurePosixPath(urdf_object["key"]).name
|
||||
download_object(client, urdf_object, urdf_local)
|
||||
urdf_sha256 = validate_urdf(urdf_local)
|
||||
fingerprint = stable_fingerprint(archives, urdf_sha256)
|
||||
cache_dir = prepared_root / dataset_id / fingerprint
|
||||
|
||||
if reuse_prepared:
|
||||
cached = verify_cache(cache_dir, fingerprint)
|
||||
if cached is not None:
|
||||
cached = dict(cached)
|
||||
cached["cacheHit"] = True
|
||||
cached["freeGiBBefore"] = round(free_gib, 3)
|
||||
output_dir = pathlib.Path(
|
||||
env_text("OUTPUT_DIR", "/tmp/droboticflow-ubt-prepare")
|
||||
)
|
||||
output_manifest = output_dir / "prepared-manifest.json"
|
||||
write_json(output_manifest, cached)
|
||||
emit_result(
|
||||
output_dir,
|
||||
OPERATOR_ID,
|
||||
[
|
||||
artifact(
|
||||
output_manifest,
|
||||
port_id="prepared-manifest",
|
||||
port_name="准备清单",
|
||||
data=cached,
|
||||
)
|
||||
],
|
||||
[
|
||||
f"prepared cache hit: {fingerprint}",
|
||||
f"episode count: {cached['episodeCount']}",
|
||||
],
|
||||
)
|
||||
return
|
||||
|
||||
build_dir = prepared_root / dataset_id / f".{fingerprint}.building"
|
||||
if build_dir.exists():
|
||||
shutil.rmtree(build_dir)
|
||||
episode_root = build_dir / user_name / task_name
|
||||
episode_root.mkdir(parents=True)
|
||||
shutil.copy2(urdf_local, build_dir / urdf_local.name)
|
||||
|
||||
episodes: list[dict[str, Any]] = []
|
||||
seen: set[str] = set()
|
||||
for index, archive_object in enumerate(archives):
|
||||
local_archive = (
|
||||
scratch
|
||||
/ f"{index:02d}-{pathlib.PurePosixPath(archive_object['key']).name}"
|
||||
)
|
||||
extract_dir = scratch / f"extract-{index:02d}"
|
||||
download_object(client, archive_object, local_archive)
|
||||
safe_extract_zip(local_archive, extract_dir)
|
||||
with contextlib.suppress(FileNotFoundError):
|
||||
local_archive.unlink()
|
||||
discovered = discover_episode_dirs(extract_dir)
|
||||
if not discovered:
|
||||
raise RuntimeError(
|
||||
f"archive contains no episode directory: {archive_object['path']}"
|
||||
)
|
||||
for source_episode in discovered:
|
||||
episode_name = claim_episode(source_episode, seen)
|
||||
destination = episode_root / episode_name
|
||||
shutil.move(str(source_episode), destination)
|
||||
episodes.append(
|
||||
{
|
||||
"episode": episode_name,
|
||||
"date": episode_name[len("episode_") : len("episode_") + 10],
|
||||
"preparedPath": str(destination),
|
||||
"source": {
|
||||
"path": archive_object["path"],
|
||||
"key": archive_object["key"],
|
||||
"size": archive_object["size"],
|
||||
"etag": archive_object["etag"],
|
||||
},
|
||||
}
|
||||
)
|
||||
shutil.rmtree(extract_dir)
|
||||
|
||||
episodes.sort(key=lambda item: item["episode"])
|
||||
manifest = {
|
||||
"schemaVersion": "1.0",
|
||||
"datasetId": dataset_id,
|
||||
"datasetFingerprint": fingerprint,
|
||||
"urdfFingerprint": urdf_sha256,
|
||||
"urdfPath": str(build_dir / urdf_local.name),
|
||||
"preparedRoot": str(build_dir),
|
||||
"userName": user_name,
|
||||
"taskName": task_name,
|
||||
"archiveCount": len(archives),
|
||||
"episodeCount": len(episodes),
|
||||
"cacheHit": False,
|
||||
"freeGiBBefore": round(free_gib, 3),
|
||||
"sourceObjects": archives + [urdf_object],
|
||||
"episodes": episodes,
|
||||
}
|
||||
write_json(build_dir / "prepared-manifest.json", manifest)
|
||||
write_json(
|
||||
build_dir / ".complete.json",
|
||||
{
|
||||
"datasetFingerprint": fingerprint,
|
||||
"episodeCount": len(episodes),
|
||||
},
|
||||
)
|
||||
cache_dir.parent.mkdir(parents=True, exist_ok=True)
|
||||
if cache_dir.exists():
|
||||
shutil.rmtree(cache_dir)
|
||||
build_dir.replace(cache_dir)
|
||||
|
||||
# Paths are stable only after the atomic rename.
|
||||
old_root = manifest["preparedRoot"]
|
||||
manifest["preparedRoot"] = str(cache_dir)
|
||||
manifest["urdfPath"] = str(cache_dir / urdf_local.name)
|
||||
for episode in manifest["episodes"]:
|
||||
episode["preparedPath"] = episode["preparedPath"].replace(
|
||||
old_root, str(cache_dir), 1
|
||||
)
|
||||
write_json(cache_dir / "prepared-manifest.json", manifest)
|
||||
|
||||
output_dir = pathlib.Path(env_text("OUTPUT_DIR", "/tmp/droboticflow-ubt-prepare"))
|
||||
output_manifest = output_dir / "prepared-manifest.json"
|
||||
write_json(output_manifest, manifest)
|
||||
emit_result(
|
||||
output_dir,
|
||||
OPERATOR_ID,
|
||||
[
|
||||
artifact(
|
||||
output_manifest,
|
||||
port_id="prepared-manifest",
|
||||
port_name="准备清单",
|
||||
data=manifest,
|
||||
)
|
||||
],
|
||||
[
|
||||
f"prepared dataset fingerprint: {manifest['datasetFingerprint']}",
|
||||
f"archive count: {manifest['archiveCount']}",
|
||||
f"episode count: {manifest['episodeCount']}",
|
||||
],
|
||||
)
|
||||
301
lightweight-operators/ubt-mcap-pipeline/ubt_pipeline/report.py
Normal file
301
lightweight-operators/ubt-mcap-pipeline/ubt_pipeline/report.py
Normal file
@ -0,0 +1,301 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
import html
|
||||
import pathlib
|
||||
import shutil
|
||||
from collections import Counter, defaultdict
|
||||
from typing import Any
|
||||
|
||||
from .common import (
|
||||
artifact,
|
||||
artifact_payload,
|
||||
emit_result,
|
||||
env_text,
|
||||
find_json_payload,
|
||||
load_input_artifacts,
|
||||
write_json,
|
||||
)
|
||||
|
||||
OPERATOR_ID = "ubt-quality-report"
|
||||
|
||||
|
||||
def _episode_results(artifacts: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
for item in artifacts:
|
||||
if str(item.get("name", "")) != "episode-results.json":
|
||||
continue
|
||||
payload = artifact_payload(item)
|
||||
if isinstance(payload, list) and all(isinstance(row, dict) for row in payload):
|
||||
return payload
|
||||
raise RuntimeError("required upstream artifact is missing: episode-results.json")
|
||||
|
||||
|
||||
def _rule_failures(result: dict[str, Any]) -> list[str]:
|
||||
return [
|
||||
str(rule.get("rule_id", "unknown"))
|
||||
for rule in result.get("ruleResults", [])
|
||||
if str(rule.get("status", "")).lower() not in {"pass", "ok"}
|
||||
]
|
||||
|
||||
|
||||
def _date_statistics(results: list[dict[str, Any]]) -> dict[str, dict[str, int]]:
|
||||
output: dict[str, Counter[str]] = defaultdict(Counter)
|
||||
for item in results:
|
||||
output[str(item.get("date", "unknown"))][str(item["status"])] += 1
|
||||
return {
|
||||
date: {
|
||||
status: counts.get(status, 0)
|
||||
for status in ("PASS", "ERROR", "RUNTIME_ERROR")
|
||||
}
|
||||
for date, counts in sorted(output.items())
|
||||
}
|
||||
|
||||
|
||||
def _manifest_entry(item: dict[str, Any]) -> dict[str, Any]:
|
||||
return {
|
||||
"episode": item.get("episode"),
|
||||
"date": item.get("date"),
|
||||
"status": item.get("status"),
|
||||
"source": item.get("source", {}),
|
||||
"preparedPath": item.get("preparedPath", ""),
|
||||
"mcapPath": item.get("mcapPath", ""),
|
||||
"failedRules": _rule_failures(item),
|
||||
"runtimeError": item.get("runtimeError"),
|
||||
}
|
||||
|
||||
|
||||
def _write_csv(path: pathlib.Path, results: list[dict[str, Any]]) -> None:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with path.open("w", encoding="utf-8", newline="") as handle:
|
||||
writer = csv.DictWriter(
|
||||
handle,
|
||||
fieldnames=[
|
||||
"episode",
|
||||
"date",
|
||||
"status",
|
||||
"failed_rules",
|
||||
"runtime_error",
|
||||
"source_bos_path",
|
||||
"prepared_path",
|
||||
"mcap_path",
|
||||
"num_steps",
|
||||
],
|
||||
)
|
||||
writer.writeheader()
|
||||
for item in results:
|
||||
writer.writerow(
|
||||
{
|
||||
"episode": item.get("episode", ""),
|
||||
"date": item.get("date", ""),
|
||||
"status": item.get("status", ""),
|
||||
"failed_rules": ",".join(_rule_failures(item)),
|
||||
"runtime_error": item.get("runtimeError") or "",
|
||||
"source_bos_path": (item.get("source") or {}).get("path", ""),
|
||||
"prepared_path": item.get("preparedPath", ""),
|
||||
"mcap_path": item.get("mcapPath", ""),
|
||||
"num_steps": item.get("numSteps", 0),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _render_html(summary: dict[str, Any], results: list[dict[str, Any]]) -> str:
|
||||
rows = []
|
||||
for item in results:
|
||||
failed_rules = ", ".join(_rule_failures(item)) or "—"
|
||||
rows.append(
|
||||
"<tr>"
|
||||
f"<td>{html.escape(str(item.get('episode', '')))}</td>"
|
||||
f"<td>{html.escape(str(item.get('date', '')))}</td>"
|
||||
f"<td><span class=\"status {html.escape(str(item['status']).lower())}\">"
|
||||
f"{html.escape(str(item['status']))}</span></td>"
|
||||
f"<td>{html.escape(failed_rules)}</td>"
|
||||
f"<td>{html.escape(str(item.get('runtimeError') or '—'))}</td>"
|
||||
"</tr>"
|
||||
)
|
||||
counts = summary["statusCounts"]
|
||||
return f"""<!doctype html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>UBT MCAP 数据质检报告</title>
|
||||
<style>
|
||||
body{{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;margin:0;background:#f4f7fb;color:#172033}}
|
||||
main{{max-width:1200px;margin:32px auto;padding:0 20px}}
|
||||
h1{{margin-bottom:6px}} .muted{{color:#64748b}}
|
||||
.cards{{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:14px;margin:24px 0}}
|
||||
.card{{background:#fff;border:1px solid #e5eaf1;border-radius:12px;padding:18px;box-shadow:0 2px 10px #1e293b0d}}
|
||||
.value{{font-size:30px;font-weight:700;margin-top:8px}}
|
||||
table{{width:100%;border-collapse:collapse;background:#fff;border-radius:12px;overflow:hidden}}
|
||||
th,td{{padding:11px 12px;text-align:left;border-bottom:1px solid #eef2f7;font-size:13px}}
|
||||
th{{background:#f8fafc;position:sticky;top:0}} .status{{font-weight:700}}
|
||||
.pass{{color:#16803c}} .error{{color:#c2410c}} .runtime_error{{color:#b91c1c}}
|
||||
code{{word-break:break-all}} @media(max-width:760px){{.cards{{grid-template-columns:1fr 1fr}}}}
|
||||
</style>
|
||||
</head>
|
||||
<body><main>
|
||||
<h1>UBT MCAP 数据质检报告</h1>
|
||||
<div class="muted">数据集 {html.escape(str(summary.get("datasetId", "")))} ·
|
||||
日期 {html.escape(", ".join(summary.get("datePrefixes", [])))} ·
|
||||
源码 {html.escape(str(summary.get("sourceCommit", "")))}</div>
|
||||
<section class="cards">
|
||||
<div class="card">总数<div class="value">{summary["totalEpisodes"]}</div></div>
|
||||
<div class="card">PASS<div class="value pass">{counts["PASS"]}</div></div>
|
||||
<div class="card">ERROR<div class="value error">{counts["ERROR"]}</div></div>
|
||||
<div class="card">通过率<div class="value">{summary["passRate"]:.2%}</div></div>
|
||||
</section>
|
||||
<p><strong>数据 fingerprint:</strong><code>{html.escape(str(summary.get("datasetFingerprint", "")))}</code></p>
|
||||
<p><strong>URDF fingerprint:</strong><code>{html.escape(str(summary.get("urdfFingerprint", "")))}</code></p>
|
||||
<table><thead><tr><th>Episode</th><th>日期</th><th>状态</th><th>失败规则</th><th>运行异常</th></tr></thead>
|
||||
<tbody>{''.join(rows)}</tbody></table>
|
||||
</main></body></html>
|
||||
"""
|
||||
|
||||
|
||||
def _copy_raw_artifacts(
|
||||
artifacts: list[dict[str, Any]], destination: pathlib.Path
|
||||
) -> list[pathlib.Path]:
|
||||
copied: list[pathlib.Path] = []
|
||||
for item in artifacts:
|
||||
relative = str(item.get("relativePath", ""))
|
||||
if not relative.startswith("raw-checker/"):
|
||||
continue
|
||||
source = pathlib.Path(str(item.get("path", "")))
|
||||
if not source.is_file():
|
||||
continue
|
||||
target = destination / relative
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(source, target)
|
||||
copied.append(target)
|
||||
return copied
|
||||
|
||||
|
||||
def run_report() -> None:
|
||||
input_artifacts = load_input_artifacts()
|
||||
checker_summary = find_json_payload(input_artifacts, "checker-summary.json")
|
||||
results = _episode_results(input_artifacts)
|
||||
if not results:
|
||||
raise RuntimeError("cannot report an empty UBT result set")
|
||||
|
||||
status_counts = {
|
||||
status: sum(1 for item in results if item["status"] == status)
|
||||
for status in ("PASS", "ERROR", "RUNTIME_ERROR")
|
||||
}
|
||||
rule_counts = Counter(
|
||||
rule_id for item in results for rule_id in _rule_failures(item)
|
||||
)
|
||||
summary = {
|
||||
"schemaVersion": "1.0",
|
||||
"datasetId": checker_summary.get("datasetId"),
|
||||
"datasetFingerprint": checker_summary.get("datasetFingerprint"),
|
||||
"urdfFingerprint": checker_summary.get("urdfFingerprint"),
|
||||
"sourceCommit": checker_summary.get("sourceCommit"),
|
||||
"imageDigest": checker_summary.get("imageDigest"),
|
||||
"datePrefixes": checker_summary.get("datePrefixes", []),
|
||||
"totalEpisodes": len(results),
|
||||
"statusCounts": status_counts,
|
||||
"passRate": status_counts["PASS"] / len(results),
|
||||
"dateStatistics": _date_statistics(results),
|
||||
"ruleStatistics": dict(sorted(rule_counts.items())),
|
||||
"checkerSummary": checker_summary,
|
||||
}
|
||||
pass_manifest = {
|
||||
"datasetId": summary["datasetId"],
|
||||
"datasetFingerprint": summary["datasetFingerprint"],
|
||||
"episodeCount": status_counts["PASS"],
|
||||
"episodes": [
|
||||
_manifest_entry(item) for item in results if item["status"] == "PASS"
|
||||
],
|
||||
}
|
||||
fail_manifest = {
|
||||
"datasetId": summary["datasetId"],
|
||||
"datasetFingerprint": summary["datasetFingerprint"],
|
||||
"episodeCount": status_counts["ERROR"] + status_counts["RUNTIME_ERROR"],
|
||||
"episodes": [
|
||||
_manifest_entry(item) for item in results if item["status"] != "PASS"
|
||||
],
|
||||
}
|
||||
|
||||
output_dir = pathlib.Path(env_text("OUTPUT_DIR", "/tmp/droboticflow-ubt-report"))
|
||||
output_dir.mkdir(parents=True, exist_ok=True)
|
||||
paths = {
|
||||
"summary": output_dir / "ubt-quality-summary.json",
|
||||
"csv": output_dir / "episode-results.csv",
|
||||
"pass": output_dir / "pass-manifest.json",
|
||||
"fail": output_dir / "fail-manifest.json",
|
||||
"rules": output_dir / "rule-statistics.json",
|
||||
"html": output_dir / "ubt-quality-report.html",
|
||||
}
|
||||
write_json(paths["summary"], summary)
|
||||
_write_csv(paths["csv"], results)
|
||||
write_json(paths["pass"], pass_manifest)
|
||||
write_json(paths["fail"], fail_manifest)
|
||||
write_json(paths["rules"], summary["ruleStatistics"])
|
||||
paths["html"].write_text(_render_html(summary, results), encoding="utf-8")
|
||||
raw_paths = _copy_raw_artifacts(input_artifacts, output_dir)
|
||||
|
||||
dataset_id = str(summary["datasetId"] or "ubt")
|
||||
date_range = "_".join(summary["datePrefixes"]) or "all-dates"
|
||||
run_id = env_text("RUN_ID", "run")
|
||||
prefix = f"results/{dataset_id}/{date_range}/{run_id}"
|
||||
output_artifacts = [
|
||||
artifact(
|
||||
paths["summary"],
|
||||
port_id="quality-bundle",
|
||||
port_name="质检结果包",
|
||||
data=summary,
|
||||
relative_path=f"{prefix}/{paths['summary'].name}",
|
||||
),
|
||||
artifact(
|
||||
paths["csv"],
|
||||
port_id="quality-bundle",
|
||||
port_name="质检结果包",
|
||||
relative_path=f"{prefix}/{paths['csv'].name}",
|
||||
),
|
||||
artifact(
|
||||
paths["pass"],
|
||||
port_id="quality-bundle",
|
||||
port_name="质检结果包",
|
||||
data=pass_manifest,
|
||||
relative_path=f"{prefix}/{paths['pass'].name}",
|
||||
),
|
||||
artifact(
|
||||
paths["fail"],
|
||||
port_id="quality-bundle",
|
||||
port_name="质检结果包",
|
||||
data=fail_manifest,
|
||||
relative_path=f"{prefix}/{paths['fail'].name}",
|
||||
),
|
||||
artifact(
|
||||
paths["rules"],
|
||||
port_id="quality-bundle",
|
||||
port_name="质检结果包",
|
||||
data=summary["ruleStatistics"],
|
||||
relative_path=f"{prefix}/{paths['rules'].name}",
|
||||
),
|
||||
artifact(
|
||||
paths["html"],
|
||||
port_id="quality-bundle",
|
||||
port_name="质检结果包",
|
||||
relative_path=f"{prefix}/{paths['html'].name}",
|
||||
),
|
||||
]
|
||||
for path in raw_paths:
|
||||
output_artifacts.append(
|
||||
artifact(
|
||||
path,
|
||||
port_id="quality-bundle",
|
||||
port_name="质检结果包",
|
||||
relative_path=f"{prefix}/{path.relative_to(output_dir)}",
|
||||
)
|
||||
)
|
||||
emit_result(
|
||||
output_dir,
|
||||
OPERATOR_ID,
|
||||
output_artifacts,
|
||||
[
|
||||
f"quality summary: {status_counts}",
|
||||
f"pass rate: {summary['passRate']:.6f}",
|
||||
f"bundle prefix: {prefix}",
|
||||
],
|
||||
)
|
||||
25
operator-images/lightweight/ubt-mcap-runtime/Dockerfile
Normal file
25
operator-images/lightweight/ubt-mcap-runtime/Dockerfile
Normal file
@ -0,0 +1,25 @@
|
||||
FROM python:3.10-slim-bookworm
|
||||
|
||||
ARG ROBO_ORCHARD_SOURCE_COMMIT=c742b39e0821590605385ee4ddabcafb258432d8
|
||||
LABEL org.opencontainers.image.title="Drobodata UBT MCAP runtime" \
|
||||
org.opencontainers.image.source="robot-lab-internal/open-source/robo_orchard_lab" \
|
||||
org.opencontainers.image.revision="${ROBO_ORCHARD_SOURCE_COMMIT}" \
|
||||
org.opencontainers.image.licenses="Apache-2.0"
|
||||
|
||||
ENV PYTHONUNBUFFERED=1 \
|
||||
MPLBACKEND=Agg \
|
||||
PIP_NO_CACHE_DIR=1
|
||||
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends ffmpeg libgl1 libglib2.0-0 \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY lightweight-operators/ubt-mcap-pipeline/requirements.txt /tmp/requirements.txt
|
||||
RUN python -m pip install --upgrade pip==25.1.1 \
|
||||
&& python -m pip install \
|
||||
--extra-index-url https://download.pytorch.org/whl/cpu \
|
||||
-r /tmp/requirements.txt \
|
||||
&& python -m pip check
|
||||
|
||||
WORKDIR /opt/droboticflow/lightweight/operator
|
||||
CMD ["python", "/opt/droboticflow/lightweight/operator/main.py"]
|
||||
160
tests/backend/ubt_mcap_pipeline_test.py
Normal file
160
tests/backend/ubt_mcap_pipeline_test.py
Normal file
@ -0,0 +1,160 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
import json
|
||||
import pathlib
|
||||
import sys
|
||||
import zipfile
|
||||
|
||||
import pytest
|
||||
|
||||
PIPELINE_ROOT = (
|
||||
pathlib.Path(__file__).resolve().parents[2]
|
||||
/ "lightweight-operators"
|
||||
/ "ubt-mcap-pipeline"
|
||||
)
|
||||
sys.path.insert(0, str(PIPELINE_ROOT))
|
||||
|
||||
common = importlib.import_module("ubt_pipeline.common")
|
||||
prepare = importlib.import_module("ubt_pipeline.prepare")
|
||||
report = importlib.import_module("ubt_pipeline.report")
|
||||
|
||||
|
||||
def make_episode(root: pathlib.Path, name: str) -> pathlib.Path:
|
||||
episode = root / name
|
||||
episode.mkdir(parents=True)
|
||||
(episode / f"{name}_0.mcap").write_bytes(b"mcap")
|
||||
return episode
|
||||
|
||||
|
||||
def test_safe_extract_zip_rejects_directory_traversal(tmp_path: pathlib.Path):
|
||||
archive = tmp_path / "unsafe.zip"
|
||||
with zipfile.ZipFile(archive, "w") as handle:
|
||||
handle.writestr("../escape.txt", "no")
|
||||
|
||||
with pytest.raises(RuntimeError, match="escapes extraction root"):
|
||||
prepare.safe_extract_zip(archive, tmp_path / "output")
|
||||
assert not (tmp_path / "escape.txt").exists()
|
||||
|
||||
|
||||
def test_episode_validation_and_duplicate_detection(tmp_path: pathlib.Path):
|
||||
missing = tmp_path / "episode_2026_06_16-00_00_00"
|
||||
missing.mkdir()
|
||||
with pytest.raises(RuntimeError, match="missing MCAP"):
|
||||
prepare.validate_episode(missing)
|
||||
|
||||
episode = make_episode(tmp_path, "episode_2026_06_16-00_00_01")
|
||||
seen: set[str] = set()
|
||||
assert prepare.claim_episode(episode, seen) == episode.name
|
||||
with pytest.raises(RuntimeError, match="duplicate episode"):
|
||||
prepare.claim_episode(episode, seen)
|
||||
|
||||
|
||||
def test_date_prefixes_accept_dash_and_underscore_and_reject_other_formats():
|
||||
assert common.normalize_dates("2026-06-16,2026_06_17,2026-06-16") == [
|
||||
"2026_06_16",
|
||||
"2026_06_17",
|
||||
]
|
||||
with pytest.raises(ValueError, match="invalid date prefix"):
|
||||
common.normalize_dates("2026/06/16")
|
||||
|
||||
|
||||
def test_fingerprint_is_stable_and_changes_with_etag_or_urdf():
|
||||
objects = [
|
||||
{"key": "b.zip", "size": 2, "etag": '"two"'},
|
||||
{"key": "a.zip", "size": 1, "etag": "one"},
|
||||
]
|
||||
fingerprint = common.stable_fingerprint(objects, "urdf")
|
||||
assert fingerprint == common.stable_fingerprint(list(reversed(objects)), "urdf")
|
||||
assert fingerprint != common.stable_fingerprint(
|
||||
[{**objects[0], "etag": "changed"}, objects[1]], "urdf"
|
||||
)
|
||||
assert fingerprint != common.stable_fingerprint(objects, "different-urdf")
|
||||
|
||||
|
||||
def test_complete_cache_requires_every_episode(tmp_path: pathlib.Path):
|
||||
cache = tmp_path / "cache"
|
||||
episode = make_episode(cache, "episode_2026_06_16-00_00_00")
|
||||
manifest = {
|
||||
"datasetFingerprint": "fp",
|
||||
"episodeCount": 1,
|
||||
"episodes": [{"preparedPath": str(episode)}],
|
||||
}
|
||||
common.write_json(cache / "prepared-manifest.json", manifest)
|
||||
common.write_json(
|
||||
cache / ".complete.json",
|
||||
{"datasetFingerprint": "fp", "episodeCount": 1},
|
||||
)
|
||||
assert prepare.verify_cache(cache, "fp") == manifest
|
||||
next(episode.glob("*.mcap")).unlink()
|
||||
assert prepare.verify_cache(cache, "fp") is None
|
||||
|
||||
|
||||
def test_report_manifest_preserves_bos_and_prepared_references():
|
||||
item = {
|
||||
"episode": "episode_2026_06_17-14_36_27",
|
||||
"date": "2026_06_17",
|
||||
"status": "ERROR",
|
||||
"source": {
|
||||
"path": "bos://testxyz/projects/ubt/raw/test_task.zip",
|
||||
"key": "projects/ubt/raw/test_task.zip",
|
||||
},
|
||||
"preparedPath": "/data/ubt-prepared/dataset/fp/ubt/test_task/episode_x",
|
||||
"mcapPath": "/data/ubt-prepared/dataset/fp/ubt/test_task/episode_x/x.mcap",
|
||||
"ruleResults": [{"rule_id": "gripper_position_out_of_range", "status": "fail"}],
|
||||
}
|
||||
manifest = report._manifest_entry(item)
|
||||
assert manifest["source"]["path"].startswith("bos://testxyz/")
|
||||
assert manifest["preparedPath"].startswith("/data/ubt-prepared/")
|
||||
assert manifest["failedRules"] == ["gripper_position_out_of_range"]
|
||||
|
||||
|
||||
def test_html_report_escapes_untrusted_episode_and_error_text():
|
||||
summary = {
|
||||
"datasetId": "<dataset>",
|
||||
"datePrefixes": ["2026_06_16"],
|
||||
"sourceCommit": "commit",
|
||||
"totalEpisodes": 1,
|
||||
"statusCounts": {"PASS": 0, "ERROR": 0, "RUNTIME_ERROR": 1},
|
||||
"passRate": 0.0,
|
||||
"datasetFingerprint": "data",
|
||||
"urdfFingerprint": "urdf",
|
||||
}
|
||||
result = {
|
||||
"episode": "<script>alert(1)</script>",
|
||||
"date": "2026_06_16",
|
||||
"status": "RUNTIME_ERROR",
|
||||
"runtimeError": "<bad>",
|
||||
"ruleResults": [],
|
||||
}
|
||||
rendered = report._render_html(summary, [result])
|
||||
assert "<script>" not in rendered
|
||||
assert "<script>" in rendered
|
||||
assert "<bad>" in rendered
|
||||
|
||||
|
||||
def test_episode_status_vocabulary_is_fixed(monkeypatch: pytest.MonkeyPatch):
|
||||
check = importlib.import_module("ubt_pipeline.check")
|
||||
fake_utils = importlib.import_module(
|
||||
"robo_orchard_lab.dataset.ubt_manipulation.tools.utils"
|
||||
)
|
||||
monkeypatch.setattr(fake_utils, "build_report_row", lambda _: {"row": True})
|
||||
monkeypatch.setattr(fake_utils, "is_quick_screen_pass_row", lambda row: True)
|
||||
assert check.classify_report({}) == "PASS"
|
||||
monkeypatch.setattr(fake_utils, "is_quick_screen_pass_row", lambda row: False)
|
||||
assert check.classify_report({}) == "ERROR"
|
||||
assert check.classify_report({"runtime_error": "boom"}) == "RUNTIME_ERROR"
|
||||
|
||||
|
||||
def test_operator_json_templates_are_valid():
|
||||
payload = json.loads((PIPELINE_ROOT / "operators.template.json").read_text())
|
||||
assert [item["containerArgs"] for item in payload] == [
|
||||
["prepare"],
|
||||
["check"],
|
||||
["report"],
|
||||
]
|
||||
assert all(
|
||||
item["runtimeContract"]["lightweightOperator"]["codeSubPath"]
|
||||
== "lightweight-operators/ubt-mcap-pipeline"
|
||||
for item in payload
|
||||
)
|
||||
Loading…
x
Reference in New Issue
Block a user