| | """
|
| | FactoryNet Loader - Easy access to hackathon datasets.
|
| |
|
| | Usage:
|
| | from factorynet_loader import load_factorynet
|
| |
|
| | # Load AURSAD data
|
| | df, metadata = load_factorynet("aursad")
|
| |
|
| | # Get specific columns
|
| | setpoints = df[[c for c in df.columns if c.startswith("setpoint_")]]
|
| | efforts = df[[c for c in df.columns if c.startswith("effort_")]]
|
| | feedback = df[[c for c in df.columns if c.startswith("feedback_")]]
|
| | """
|
| |
|
| | import pandas as pd
|
| | import json
|
| | from pathlib import Path
|
| | from typing import Tuple, List, Optional, Dict
|
| | import numpy as np
|
| |
|
| | try:
|
| | from datasets import load_dataset
|
| | HF_AVAILABLE = True
|
| | except ImportError:
|
| | HF_AVAILABLE = False
|
| |
|
| |
|
| |
|
| | HF_REPO = "Forgis/factorynet-hackathon"
|
| |
|
| |
|
| | def load_factorynet(
|
| | dataset: str = "aursad",
|
| | split: str = "train",
|
| | from_hf: bool = True,
|
| | local_path: Optional[Path] = None,
|
| | ) -> Tuple[pd.DataFrame, List[Dict]]:
|
| | """
|
| | Load FactoryNet dataset.
|
| |
|
| | Args:
|
| | dataset: "aursad" or "voraus"
|
| | split: "train" (full data) or future splits
|
| | from_hf: If True, load from HuggingFace Hub
|
| | local_path: Local path override
|
| |
|
| | Returns:
|
| | df: DataFrame with time series
|
| | metadata: List of episode metadata dicts
|
| | """
|
| | if from_hf and HF_AVAILABLE:
|
| | return _load_from_hf(dataset, split)
|
| | elif local_path:
|
| | return _load_from_local(local_path, dataset)
|
| | else:
|
| | raise ValueError("Either set from_hf=True or provide local_path")
|
| |
|
| |
|
| | def _load_from_hf(dataset: str, split: str) -> Tuple[pd.DataFrame, List[Dict]]:
|
| | """Load from HuggingFace Hub."""
|
| | ds = load_dataset(HF_REPO, data_dir=dataset, split=split)
|
| | df = ds.to_pandas()
|
| |
|
| |
|
| | try:
|
| | from huggingface_hub import hf_hub_download
|
| | meta_file = hf_hub_download(
|
| | repo_id=HF_REPO,
|
| | filename=f"{dataset}/{dataset}_metadata.json",
|
| | repo_type="dataset"
|
| | )
|
| | with open(meta_file) as f:
|
| | metadata = json.load(f)
|
| | except:
|
| | metadata = []
|
| |
|
| | return df, metadata
|
| |
|
| |
|
| | def _load_from_local(local_path: Path, dataset: str) -> Tuple[pd.DataFrame, List[Dict]]:
|
| | """Load from local files."""
|
| | local_path = Path(local_path)
|
| |
|
| |
|
| | parquet_files = list(local_path.glob(f"**/*{dataset}*factorynet*.parquet"))
|
| | if not parquet_files:
|
| | parquet_files = list(local_path.glob("**/*.parquet"))
|
| |
|
| | if not parquet_files:
|
| | raise FileNotFoundError(f"No parquet files found in {local_path}")
|
| |
|
| | df = pd.read_parquet(parquet_files[0])
|
| |
|
| |
|
| | meta_files = list(local_path.glob(f"**/*{dataset}*metadata*.json"))
|
| | if meta_files:
|
| | with open(meta_files[0]) as f:
|
| | metadata = json.load(f)
|
| | else:
|
| | metadata = []
|
| |
|
| | return df, metadata
|
| |
|
| |
|
| | def get_episode(df: pd.DataFrame, episode_id: str) -> pd.DataFrame:
|
| | """Extract a single episode from the dataset."""
|
| | return df[df["episode_id"] == episode_id].copy()
|
| |
|
| |
|
| | def get_episodes_by_fault(df: pd.DataFrame, metadata: List[Dict], fault_type: str) -> pd.DataFrame:
|
| | """Get all episodes of a specific fault type."""
|
| | fault_episodes = [m["episode_id"] for m in metadata if m.get("fault_type") == fault_type]
|
| | return df[df["episode_id"].isin(fault_episodes)].copy()
|
| |
|
| |
|
| | def extract_features(df: pd.DataFrame, window_size: int = 100) -> np.ndarray:
|
| | """
|
| | Extract basic features for anomaly detection.
|
| |
|
| | Returns array of shape (n_windows, n_features).
|
| | """
|
| |
|
| | signal_cols = [c for c in df.columns if any(
|
| | c.startswith(p) for p in ["setpoint_", "effort_", "feedback_"]
|
| | )]
|
| |
|
| | data = df[signal_cols].values
|
| |
|
| |
|
| | n_windows = len(data) // window_size
|
| | features = []
|
| |
|
| | for i in range(n_windows):
|
| | window = data[i * window_size : (i + 1) * window_size]
|
| |
|
| | feat = np.concatenate([
|
| | window.mean(axis=0),
|
| | window.std(axis=0),
|
| | window.max(axis=0),
|
| | window.min(axis=0),
|
| | np.abs(np.diff(window, axis=0)).mean(axis=0),
|
| | ])
|
| | features.append(feat)
|
| |
|
| | return np.array(features)
|
| |
|
| |
|
| | def compute_causal_residual(df: pd.DataFrame, axis: int = 0) -> pd.Series:
|
| | """
|
| | Compute causal residual: effort that can't be explained by setpoint.
|
| |
|
| | High residual = anomaly (effort without command, or command without effort).
|
| | """
|
| | setpoint = df[f"setpoint_pos_{axis}"]
|
| | effort = df[f"effort_torque_{axis}"] if f"effort_torque_{axis}" in df.columns else df[f"effort_current_{axis}"]
|
| |
|
| |
|
| | setpoint_diff = setpoint.diff().abs()
|
| | effort_normalized = (effort - effort.mean()) / effort.std()
|
| |
|
| |
|
| | residual = effort_normalized - setpoint_diff / (setpoint_diff.max() + 1e-6)
|
| |
|
| | return residual
|
| |
|
| |
|
| |
|
| | if __name__ == "__main__":
|
| | print("Testing FactoryNet loader...")
|
| |
|
| |
|
| | try:
|
| | df, meta = load_factorynet("aursad", from_hf=False,
|
| | local_path=Path(__file__).parent.parent / "output" / "aursad_real")
|
| | print(f"Loaded {len(df)} rows, {len(df.columns)} columns")
|
| | print(f"Metadata for {len(meta)} episodes")
|
| | print(f"Columns: {df.columns.tolist()[:10]}...")
|
| |
|
| |
|
| | features = extract_features(df)
|
| | print(f"Extracted features: {features.shape}")
|
| |
|
| | except Exception as e:
|
| | print(f"Local load failed: {e}")
|
| | print("Try: pip install datasets && load with from_hf=True")
|
| |
|