|
| 1 | +//! Export filtering heuristics for identifying logs worth exporting |
| 2 | +//! |
| 3 | +//! This module provides intelligent filtering functions to help identify flight logs |
| 4 | +//! that should be skipped during export due to being ground tests, arm checks, or other |
| 5 | +//! non-flight data. |
| 6 | +//! |
| 7 | +//! # Usage |
| 8 | +//! |
| 9 | +//! These filters are controlled via `ExportOptions`. CLI users get filtering enabled by |
| 10 | +//! default for convenience, while library consumers can opt in/out as needed. |
| 11 | +
|
| 12 | +use crate::types::BBLLog; |
| 13 | + |
| 14 | +/// Determines if a log should be skipped for export based on duration and frame count |
| 15 | +/// |
| 16 | +/// Uses smart filtering: <5s always skip, 5-15s keep if good data density (>1500fps), >15s always keep |
| 17 | +/// This helps eliminate ground tests, arm checks, and other non-flight activities. |
| 18 | +/// |
| 19 | +/// # Arguments |
| 20 | +/// * `log` - The BBL log to evaluate |
| 21 | +/// * `force_export` - If true, never skips (overrides all heuristics) |
| 22 | +/// |
| 23 | +/// # Returns |
| 24 | +/// Tuple of (should_skip, reason_description) |
| 25 | +pub fn should_skip_export(log: &BBLLog, force_export: bool) -> (bool, String) { |
| 26 | + if force_export { |
| 27 | + return (false, String::new()); // Never skip when forced |
| 28 | + } |
| 29 | + |
| 30 | + const VERY_SHORT_DURATION_MS: u64 = 5_000; // 5 seconds - always skip |
| 31 | + const SHORT_DURATION_MS: u64 = 15_000; // 15 seconds - threshold for normal logs |
| 32 | + const MIN_DATA_DENSITY_FPS: f64 = 1500.0; // Minimum fps for short logs |
| 33 | + const FALLBACK_MIN_FRAMES: u32 = 7_500; // ~5 seconds at 1500 fps (fallback when no duration) |
| 34 | + |
| 35 | + // Check if we have duration information |
| 36 | + if log.stats.start_time_us > 0 && log.stats.end_time_us > log.stats.start_time_us { |
| 37 | + let duration_us = log |
| 38 | + .stats |
| 39 | + .end_time_us |
| 40 | + .saturating_sub(log.stats.start_time_us); |
| 41 | + // Use floating-point duration to avoid precision loss and division by zero |
| 42 | + let duration_s = duration_us as f64 / 1_000_000.0; |
| 43 | + |
| 44 | + // Guard against division by zero or very small durations |
| 45 | + if duration_s <= 0.0 { |
| 46 | + return (true, "duration too small or invalid".to_string()); |
| 47 | + } |
| 48 | + |
| 49 | + let duration_ms = duration_us / 1000; |
| 50 | + let fps = log.stats.total_frames as f64 / duration_s; |
| 51 | + |
| 52 | + // Very short logs: < 5 seconds → Always skip |
| 53 | + if duration_ms < VERY_SHORT_DURATION_MS { |
| 54 | + return (true, format!("too short ({:.1}s < 5.0s)", duration_s)); |
| 55 | + } |
| 56 | + |
| 57 | + // Short logs: 5-15 seconds → Keep if sufficient data density (>1500 fps) |
| 58 | + if duration_ms < SHORT_DURATION_MS { |
| 59 | + if fps < MIN_DATA_DENSITY_FPS { |
| 60 | + return ( |
| 61 | + true, |
| 62 | + format!( |
| 63 | + "insufficient data density ({:.0}fps < {:.0}fps for {:.1}s log)", |
| 64 | + fps, MIN_DATA_DENSITY_FPS, duration_s |
| 65 | + ), |
| 66 | + ); |
| 67 | + } |
| 68 | + // Good data density, keep it |
| 69 | + return (false, String::new()); |
| 70 | + } |
| 71 | + |
| 72 | + // Normal logs: > 15 seconds → Check for minimal gyro activity (ground tests) |
| 73 | + if duration_ms >= SHORT_DURATION_MS { |
| 74 | + let (is_minimal_movement, max_variance) = has_minimal_gyro_activity(log); |
| 75 | + if is_minimal_movement { |
| 76 | + return ( |
| 77 | + true, |
| 78 | + format!( |
| 79 | + "minimal gyro activity ({:.1} variance) - likely ground test", |
| 80 | + max_variance |
| 81 | + ), |
| 82 | + ); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return (false, String::new()); |
| 87 | + } |
| 88 | + |
| 89 | + // No duration information available, fall back to frame count |
| 90 | + // Skip if very low frame count (equivalent to <5s at minimum viable fps) |
| 91 | + if log.stats.total_frames < FALLBACK_MIN_FRAMES { |
| 92 | + return ( |
| 93 | + true, |
| 94 | + format!( |
| 95 | + "too few frames ({} < {}) and no duration info", |
| 96 | + log.stats.total_frames, FALLBACK_MIN_FRAMES |
| 97 | + ), |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + // Sufficient frames without duration info, keep it |
| 102 | + (false, String::new()) |
| 103 | +} |
| 104 | + |
| 105 | +/// Analyzes gyro variance to detect ground tests vs actual flight |
| 106 | +/// |
| 107 | +/// Returns true if the log appears to be a static ground test (minimal movement) |
| 108 | +/// |
| 109 | +/// # Arguments |
| 110 | +/// * `log` - The BBL log to analyze |
| 111 | +/// |
| 112 | +/// # Returns |
| 113 | +/// Tuple of (is_minimal_movement, max_variance_value) |
| 114 | +pub fn has_minimal_gyro_activity(log: &BBLLog) -> (bool, f64) { |
| 115 | + // Conservative thresholds to avoid false-skips |
| 116 | + const MIN_SAMPLES_FOR_ANALYSIS: usize = 15; // Reduced for limited sample data |
| 117 | + const VERY_LOW_GYRO_VARIANCE_THRESHOLD: f64 = 0.3; // More aggressive threshold for ground test detection |
| 118 | + |
| 119 | + let mut gyro_x_values = Vec::new(); |
| 120 | + let mut gyro_y_values = Vec::new(); |
| 121 | + let mut gyro_z_values = Vec::new(); |
| 122 | + |
| 123 | + // First try to use debug_frames if available (contains more comprehensive data) |
| 124 | + if let Some(debug_frames) = &log.debug_frames { |
| 125 | + // Collect gyro data from I and P frames in debug_frames |
| 126 | + for (frame_type, frames) in debug_frames { |
| 127 | + if *frame_type == 'I' || *frame_type == 'P' { |
| 128 | + for frame in frames { |
| 129 | + if let Some(gyro_x) = frame.data.get("gyroADC[0]") { |
| 130 | + if let Some(gyro_y) = frame.data.get("gyroADC[1]") { |
| 131 | + if let Some(gyro_z) = frame.data.get("gyroADC[2]") { |
| 132 | + gyro_x_values.push(*gyro_x as f64); |
| 133 | + gyro_y_values.push(*gyro_y as f64); |
| 134 | + gyro_z_values.push(*gyro_z as f64); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // Fallback to frames if debug_frames not available or insufficient data |
| 144 | + if gyro_x_values.len() < MIN_SAMPLES_FOR_ANALYSIS { |
| 145 | + for frame in &log.frames { |
| 146 | + if let Some(gyro_x) = frame.data.get("gyroADC[0]") { |
| 147 | + if let Some(gyro_y) = frame.data.get("gyroADC[1]") { |
| 148 | + if let Some(gyro_z) = frame.data.get("gyroADC[2]") { |
| 149 | + gyro_x_values.push(*gyro_x as f64); |
| 150 | + gyro_y_values.push(*gyro_y as f64); |
| 151 | + gyro_z_values.push(*gyro_z as f64); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // Need sufficient data points for reliable analysis |
| 159 | + if gyro_x_values.len() < MIN_SAMPLES_FOR_ANALYSIS { |
| 160 | + return (false, 0.0); // Not enough data, don't skip (conservative approach) |
| 161 | + } |
| 162 | + |
| 163 | + // Calculate variance for each axis |
| 164 | + let variance_x = calculate_variance(&gyro_x_values); |
| 165 | + let variance_y = calculate_variance(&gyro_y_values); |
| 166 | + let variance_z = calculate_variance(&gyro_z_values); |
| 167 | + |
| 168 | + // Use the maximum variance across all axes |
| 169 | + let max_variance = variance_x.max(variance_y).max(variance_z); |
| 170 | + |
| 171 | + // Very conservative: only skip if ALL axes show extremely low variance |
| 172 | + let is_minimal = max_variance < VERY_LOW_GYRO_VARIANCE_THRESHOLD; |
| 173 | + |
| 174 | + (is_minimal, max_variance) |
| 175 | +} |
| 176 | + |
| 177 | +/// Calculate variance of a dataset |
| 178 | +/// |
| 179 | +/// # Arguments |
| 180 | +/// * `values` - Slice of f64 values to compute variance for |
| 181 | +/// |
| 182 | +/// # Returns |
| 183 | +/// The variance of the dataset |
| 184 | +pub fn calculate_variance(values: &[f64]) -> f64 { |
| 185 | + if values.len() < 2 { |
| 186 | + return 0.0; |
| 187 | + } |
| 188 | + |
| 189 | + let mean = values.iter().sum::<f64>() / values.len() as f64; |
| 190 | + let variance = values.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / values.len() as f64; |
| 191 | + |
| 192 | + variance |
| 193 | +} |
0 commit comments