Skip to content

Implement ros2_svdd_monitor: SVDD anomaly detection for proprioceptive sensors#2

Draft
Copilot wants to merge 9 commits intomainfrom
copilot/create-ros2-svdd-monitor-package-again
Draft

Implement ros2_svdd_monitor: SVDD anomaly detection for proprioceptive sensors#2
Copilot wants to merge 9 commits intomainfrom
copilot/create-ros2-svdd-monitor-package-again

Conversation

Copy link

Copilot AI commented Dec 9, 2025

Implements a complete ROS 2 Python package for real-time anomaly detection using Support Vector Data Description (SVDD via OneClassSVM) on proprioceptive sensors (/cmd_vel and /imu). Detects anomalies by learning the expected relationship between commanded motion and IMU response.

Package Structure

  • Core modules: SVDD model wrapper (svdd_model.py), sliding-window feature extraction (features.py), ROS 2 monitor node (monitor.py), training CLI (train.py)
  • Utils: rosbag-to-CSV converter for data export
  • Config: YAML-based hyperparameters (window_size, nu, gamma, thresholds)
  • Tests: 17 unit tests covering feature extraction and model operations

Entry Points

train: Offline training from CSV

ros2 run ros2_svdd_monitor train --csv normal_operation.csv
# Extracts 20 features (cmd_vel stats + IMU stats + correlations)
# Trains OneClassSVM, saves model + scaler

monitor: Real-time anomaly detection node

ros2 run ros2_svdd_monitor monitor
# Subscribes: /cmd_vel, /imu
# Publishes: /svdd/anomaly (Bool), /svdd/anomaly_score (Float32)

Feature Engineering

Extracts 20 features from sliding windows:

  • 8 cmd_vel features: mean/std/min/max of linear.x and angular.z
  • 8 IMU features: mean/std/min/max of accel.x and gyro.z
  • 4 cross-features: correlations and ratios mapping commands to expected sensor response

Detects wheel slip, motor failures, collisions, sensor degradation.

Example Usage

# Generate training data
python examples/generate_sample_data.py --samples 100

# Train model
ros2 run ros2_svdd_monitor train --csv example_training_data.csv

# Monitor in real-time
ros2 run ros2_svdd_monitor monitor

# View results
ros2 topic echo /svdd/anomaly
ros2 topic echo /svdd/anomaly_score

Changes

  • Added complete ament_python package structure under src/ros2_svdd_monitor/
  • Implemented SVDD model wrapper using sklearn.svm.OneClassSVM with RBF kernel
  • Created feature extraction with configurable sliding windows and cross-feature correlations
  • Built ROS 2 node for real-time monitoring with anomaly score publishing
  • Added training CLI with CSV input and joblib model persistence
  • Configured pytest CI workflow with secure permissions
  • Updated LICENSE from Apache 2.0 to MIT
  • Preserved .devcontainer/ from template
  • Added examples with synthetic data generator

Configuration

All hyperparameters in config/config.yaml:

window_size: 10
nu: 0.1  # outlier fraction upper bound
gamma: 'scale'  # RBF kernel coefficient
anomaly_threshold: 0.0
Original prompt

I want a ROS 2 (Humble) Python package seeded from the LCAS/ros2_pkg_template. Create a repository and package named ros2_svdd_monitor (owner: LCAS) that implements an SVDD-style anomaly detection pipeline using ONLY proprioceptive sensors: /imu and /cmd_vel. Preserve the .devcontainer from the template. Target rclpy (Python) with console entry points train and monitor.

Requirements:

Create a ROS 2 ament_python package ros2_svdd_monitor under src/ with setup.cfg/setup.py/package.xml and entry points:
train: CLI that reads a CSV (exported from rosbag), extracts sliding-window features from /cmd_vel + /imu, scales features, trains sklearn.svm.OneClassSVM (RBF) as the SVDD proxy, and saves model+scaler (joblib).
monitor: rclpy node subscribing to /cmd_vel and /imu, maintaining configurable sliding windows, extracting features, loading the saved model, publishing /svdd/anomaly (std_msgs/Bool) and /svdd/anomaly_score (std_msgs/Float32).
Implement svdd_model.py (OneClassSVM wrapper), features.py (feature extraction and mapping logic: map cmd_vel.linear.x forward command to expected IMU forward accel/orientation response), utils/rosbag_to_csv.py (helper guidance or minimal script to export topics), config/config.yaml (window_size, nu, gamma, model_path, threshold).
Tests: unit tests for feature extraction and model save/load (pytest).
CI: GitHub Actions workflow to run pytest (and lint optionally) on push/PR.
requirements.txt listing scikit-learn, numpy, pandas, joblib, pyyaml, rclpy.
License: MIT.
Do NOT include rosbag files in the repo. Provide example usage in README and a short note on how to record/export CSV from rosbag (recommend a small subscriber to write synchronized CSV rows).
Commit all files to main with message "Initial ros2_svdd_monitor package (SVDD monitor)". Make hyperparameters configurable in config.yaml and keep outputs human-readable.
Deliverables: complete package files, tests, CI, README with quickstart for training and running the monitor, and devcontainer preserved for local development.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits December 9, 2025 15:10
Co-authored-by: jondave <6209386+jondave@users.noreply.github.com>
Co-authored-by: jondave <6209386+jondave@users.noreply.github.com>
Co-authored-by: jondave <6209386+jondave@users.noreply.github.com>
Copilot AI changed the title [WIP] Create ROS 2 SVDD monitor package using proprioceptive sensors Implement ros2_svdd_monitor: SVDD anomaly detection for proprioceptive sensors Dec 9, 2025
Copilot AI requested a review from jondave December 9, 2025 15:23
- Introduced a new metadata.yaml file containing detailed information about the dynium_system_filtered rosbag.
- Included bagfile information such as version, storage identifier, duration, starting time, and message count.
- Documented topics with their respective message counts, types, serialization formats, and QoS profiles.
- Specified the relative file paths and details of the associated database file.
- Updated config.yaml to include ROS 2 topic names and adjusted anomaly threshold.
- Modified monitor.py to use flexible QoS settings for subscriptions.
- Implemented warmup period for anomaly detection to stabilize windows.
- Improved logging for detected anomalies with detailed diagnostics.
- Implemented `record_features.py` to record /cmd_vel and /imu topics, compute sliding-window features, and save them to a .npz file.
- Created `train_ms_svdd.py` for training Mahalanobis-SVDD using extracted features from .npz or .csv files.
- Introduced `train_ms_svdd.py` for training Mahalanobis-SVDD models using extracted features.
- Added `svm_svdd` package containing:
  - `__init__.py` to define the package structure.
  - `svm_svdd_model.py` implementing a wrapper for sklearn's OneClassSVM for anomaly detection.
  - `train_svm_svdd.py` for training the OneClassSVM model and saving artifacts.
  - `eval_svm_svdd.py` for evaluating the trained model and generating anomaly scores.
- Implemented feature loading functions to support .npz, .csv, and .parquet formats.
- Ensured compatibility with both direct script execution and package imports.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants