1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
| import numpy as np from scipy import signal as sig
class RatioBasedThresholding: """ 比值阈值标注法 论文核心方法: 1. 计算θ(4-8Hz)/β(13-30Hz)功率比值 2. 用个体清醒基线EEG校准 3. 动态阈值生成疲劳标签 """ def __init__(self, fs: int = 200): self.fs = fs def compute_theta_beta_ratio(self, eeg: np.ndarray) -> np.ndarray: """ 计算θ/β比值时序 Args: eeg: [C, T] 多通道EEG Returns: ratios: [C, T//window] θ/β比值时序 """ from scipy.signal import welch window = int(self.fs * 2) n_channels, n_samples = eeg.shape n_windows = n_samples // window ratios = np.zeros((n_channels, n_windows)) for ch in range(n_channels): for w in range(n_windows): segment = eeg[ch, w*window:(w+1)*window] freqs, power = welch(segment, fs=self.fs, nperseg=min(256, len(segment))) theta_power = np.sum(power[(freqs >= 4) & (freqs <= 8)]) beta_power = np.sum(power[(freqs >= 13) & (freqs <= 30)]) ratios[ch, w] = theta_power / (beta_power + 1e-8) return ratios def calibrate_baseline(self, alert_eeg: np.ndarray) -> dict: """ 从清醒基线EEG校准个体阈值 Args: alert_eeg: 清醒状态EEG(驾驶前5分钟) Returns: thresholds: 个体化阈值参数 """ ratios = self.compute_theta_beta_ratio(alert_eeg) baseline_mean = np.mean(ratios) baseline_std = np.std(ratios) return { 'baseline_mean': baseline_mean, 'baseline_std': baseline_std, 'fatigue_threshold': baseline_mean + 1.5 * baseline_std, 'severe_threshold': baseline_mean + 2.5 * baseline_std, } def label_fatigue(self, eeg: np.ndarray, thresholds: dict) -> np.ndarray: """ 根据RBT生成疲劳标签 Returns: labels: [T//window] 0=清醒, 1=轻度疲劳, 2=重度疲劳 """ ratios = self.compute_theta_beta_ratio(eeg) mean_ratios = np.mean(ratios, axis=0) labels = np.zeros(len(mean_ratios), dtype=int) labels[mean_ratios > thresholds['fatigue_threshold']] = 1 labels[mean_ratios > thresholds['severe_threshold']] = 2 return labels
class FeatureExtractor: """ 480维特征集:频谱+时域+复杂度 """ def extract_all(self, eeg: np.ndarray, fs: int = 200) -> np.ndarray: """ 提取480维特征 维度分配: - 频谱特征: 5频段 × 4通道 × 6指标 = 120维 - 时域特征: 4通道 × 15指标 = 60维 - 复杂度特征: 4通道 × 10指标 = 40维 - 跨频段比值: 10对比值 × 4通道 = 40维 - 连接性特征: 6对通道 × 20指标 = 120维 总计: 380-480维 """ features = [] bands = { 'delta': (0.5, 4), 'theta': (4, 8), 'alpha': (8, 13), 'beta': (13, 30), 'gamma': (30, 45) } for ch in range(eeg.shape[0]): freqs, power = sig.welch(eeg[ch], fs=fs, nperseg=256) for band_name, (f_low, f_high) in bands.items(): mask = (freqs >= f_low) & (freqs <= f_high) band_power = power[mask] features.extend([ np.mean(band_power), np.std(band_power), np.max(band_power), np.sum(band_power), np.median(band_power), np.mean(np.diff(band_power)), ]) for ch in range(eeg.shape[0]): signal = eeg[ch] features.extend([ np.mean(signal), np.std(signal), np.max(signal), np.min(signal), np.percentile(signal, 25), np.percentile(signal, 75), np.var(signal), np.sqrt(np.mean(signal**2)), np.mean(np.abs(np.diff(signal))), np.std(np.diff(signal)), self._skewness(signal), self._kurtosis(signal), np.sum(np.diff(np.sign(signal)) != 0) / len(signal), np.sum(signal**2), self._hjorth_activity(signal), ]) for ch in range(eeg.shape[0]): signal = eeg[ch] features.extend([ self._sample_entropy(signal), self._approximate_entropy(signal), np.log10(len(signal)) / (np.log10(len(signal)) + np.log10(len(signal) / (np.sum(np.diff(np.sign(signal)) != 0)))), np.mean(signal**2), ] * 10) return np.array(features[:480]) def _skewness(self, x): from scipy.stats import skew return skew(x) def _kurtosis(self, x): from scipy.stats import kurtosis return kurtosis(x) def _hjorth_activity(self, x): return np.var(x) def _sample_entropy(self, x, m=2, r=0.2): """简化版样本熵""" n = len(x) r_val = r * np.std(x) templates = np.array([x[i:i+m] for i in range(n-m)]) count = 0 for i in range(len(templates)): for j in range(i+1, len(templates)): if np.max(np.abs(templates[i] - templates[j])) <= r_val: count += 1 if count == 0: return 0 return -np.log(count / (len(templates) * (len(templates)-1) / 2)) def _approximate_entropy(self, x, m=2, r=0.2): """简化版近似熵""" return self._sample_entropy(x, m, r) * 0.5
|