#!/bin/bash
# Test script for extracting bad heading cases


# Set PYTHONPATH to project root for module imports
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
export PYTHONPATH="${PROJECT_ROOT}:${PYTHONPATH}"
echo "========================================="

echo "Bad Heading Cases Extraction Test"
echo "========================================="
echo ""

INPUT_FILE="eval_results_common_match_comparison/yolov5s-300w/20260203_210259/detailed_3d_matches.json"
OUTPUT_DIR="runs/heading_error_analysis"

# Check if input file exists
if [ ! -f "$INPUT_FILE" ]; then
    echo "Error: Input file not found: $INPUT_FILE"
    echo "Please update INPUT_FILE path in this script"
    exit 1
fi

mkdir -p "$OUTPUT_DIR"

echo "Test 1: Extract all cases with error > 1.5 rad (≈ 85°)"
echo "-----------------------------------------------------"
python eval_tools/heading_analysis/extract_bad_heading_cases.py \
    --input "$INPUT_FILE" \
    --threshold 1.5 \
    --output "$OUTPUT_DIR/all_bad_cases.json" \
    --stats

echo ""
echo ""
echo "Test 2: Extract top 50 worst cases"
echo "-----------------------------------------------------"
python eval_tools/heading_analysis/extract_bad_heading_cases.py \
    --input "$INPUT_FILE" \
    --threshold 1.5 \
    --top-k 50 \
    --output "$OUTPUT_DIR/top50_worst.json" \
    --stats

echo ""
echo ""
echo "Test 3: Extract only reversal errors (≈ 180°)"
echo "-----------------------------------------------------"
python eval_tools/heading_analysis/extract_bad_heading_cases.py \
    --input "$INPUT_FILE" \
    --threshold 1.5 \
    --reversal-only \
    --top-k 100 \
    --output "$OUTPUT_DIR/reversal_errors.json" \
    --stats

echo ""
echo ""
echo "Test 4: Extract vehicle class only, high confidence"
echo "-----------------------------------------------------"
python eval_tools/heading_analysis/extract_bad_heading_cases.py \
    --input "$INPUT_FILE" \
    --threshold 1.5 \
    --classes vehicle \
    --min-confidence 0.5 \
    --top-k 50 \
    --output "$OUTPUT_DIR/vehicle_high_conf.json" \
    --stats

echo ""
echo ""
echo "Test 5: Extract mid-distance cases (30-80m)"
echo "-----------------------------------------------------"
python eval_tools/heading_analysis/extract_bad_heading_cases.py \
    --input "$INPUT_FILE" \
    --threshold 1.5 \
    --min-distance 30 \
    --max-distance 80 \
    --top-k 50 \
    --output "$OUTPUT_DIR/mid_distance.json" \
    --stats

echo ""
echo "========================================="
echo "All tests complete!"
echo "Output directory: $OUTPUT_DIR"
echo "========================================="
