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
| """ 酒精损伤多分类器 """
import numpy as np from sklearn.linear_model import LogisticRegression from sklearn.model_selection import LeaveOneOut from sklearn.metrics import classification_report, roc_auc_score from sklearn.preprocessing import StandardScaler
class AlcoholImpairmentDetector: """ 基于运动学特征的酒精损伤检测器 特征:每个信号的排列熵 + 标准差 分类:清醒 / 轻度损伤 / 重度损伤 """ def __init__(self, window_sec: float = 30.0, fps: int = 100): self.window_sec = window_sec self.fps = fps self.window_samples = int(window_sec * fps) self.signals = [ 'imu_ax', 'imu_ay', 'imu_az', 'imu_gx', 'imu_gy', 'imu_gz', 'throttle' ] self.classifier = LogisticRegression( max_iter=1000, multi_class='multinomial', solver='lbfgs', C=1.0 ) self.scaler = StandardScaler() def extract_features(self, imu_data: np.ndarray, throttle: np.ndarray) -> np.ndarray: """ 从窗口数据中提取特征 Args: imu_data: (window, 6) IMU数据 [ax, ay, az, gx, gy, gz] throttle: (window,) 油门位置 Returns: features: (14,) [PE×7, Std×7] """ features = [] signals = [ imu_data[:, 0], imu_data[:, 1], imu_data[:, 2], imu_data[:, 3], imu_data[:, 4], imu_data[:, 5], throttle ] for sig in signals: pe = permutation_entropy(sig, order=3, delay=1) std = np.std(sig) features.extend([pe, std]) return np.array(features) def fit(self, X: np.ndarray, y: np.ndarray): """训练分类器""" X_scaled = self.scaler.fit_transform(X) self.classifier.fit(X_scaled, y) def predict(self, X: np.ndarray) -> np.ndarray: """预测""" X_scaled = self.scaler.transform(X) return self.classifier.predict(X_scaled) def predict_proba(self, X: np.ndarray) -> np.ndarray: """预测概率""" X_scaled = self.scaler.transform(X) return self.classifier.predict_proba(X_scaled) def evaluate_loocv(self, X: np.ndarray, y: np.ndarray) -> dict: """ Leave-One-Participant-Out 评估 """ from sklearn.model_selection import LeaveOneOut loo = LeaveOneOut() predictions = [] probabilities = [] for train_idx, test_idx in loo.split(X): X_train, X_test = X[train_idx], X[test_idx] y_train, y_test = y[train_idx], y[test_idx] scaler = StandardScaler() X_train_s = scaler.fit_transform(X_train) X_test_s = scaler.transform(X_test) clf = LogisticRegression(max_iter=1000, multi_class='multinomial') clf.fit(X_train_s, y_train) predictions.extend(clf.predict(X_test_s)) probabilities.extend(clf.predict_proba(X_test_s)) predictions = np.array(predictions) probabilities = np.array(probabilities) accuracy = np.mean(predictions == y) from sklearn.preprocessing import label_binarize y_bin = label_binarize(y, classes=[0, 1, 2]) auroc = roc_auc_score(y_bin, probabilities, multi_class='ovr', average='weighted') return { 'accuracy': accuracy, 'auroc': auroc, 'predictions': predictions, 'report': classification_report(y, predictions, output_dict=True) }
class DrivingScenarioMigration: """ 将e-scooter方法迁移到驾驶场景 关键映射: e-scooter IMU → 车辆 IMU(或 CAN 总线加速度数据) e-scooter 油门 → 车辆油门踏板位置 e-scooter 刹车杆 → 车辆刹车踏板位置 新增:方向盘转角(e-scooter没有) """ @staticmethod def migrate_features(): """e-scooter → 驾驶场景特征映射""" mapping = { 'IMU_X 加速度': '车辆纵向加速度(CAN总线)', 'IMU_Y 加速度': '车辆横向加速度(CAN总线)', 'IMU_Z 加速度': '车辆垂直加速度(IMU)', '油门位置': '油门踏板位置(CAN总线)', '方向盘转角': '方向盘转角传感器(CAN总线)', '方向盘转角速度': '方向盘转角变化率', '刹车踏板': '刹车踏板位置(CAN总线)', '车速': '车速(CAN总线)', 'IMU 陀螺仪': '车辆陀螺仪(可选,多数车无)' } return mapping @staticmethod def driving_feature_extraction(can_data: dict, window_sec: float = 30.0, fps: int = 100) -> np.ndarray: """ 从CAN总线数据提取运动学特征 Args: can_data: { 'steering_angle': (n,) 方向盘转角 (度), 'throttle': (n,) 油门踏板位置 (0-100%), 'brake': (n,) 刹车踏板位置 (0-100%), 'speed': (n,) 车速 (km/h), 'lateral_accel': (n,) 横向加速度 (m/s²), 'longitudinal_accel': (n,) 纵向加速度 (m/s²) } """ window = int(window_sec * fps) features = [] signals = [ can_data['steering_angle'], can_data['throttle'], can_data['brake'], can_data['speed'], can_data['lateral_accel'], can_data['longitudinal_accel'], ] for sig in signals: pe = permutation_entropy(sig, order=3, delay=1) std = np.std(sig) features.extend([pe, std]) steering_rate = np.diff(can_data['steering_angle']) * fps pe_rate = permutation_entropy(steering_rate, order=3, delay=1) std_rate = np.std(steering_rate) features.extend([pe_rate, std_rate]) return np.array(features)
|