Spaces:
Sleeping
Sleeping
Download features.py from alfShymon/failure_uav_detection: direct link, hf CLI and curl.
- Browser
- Download file 4.84 kB
-
https://huggingface.co/spaces/alfShymon/failure_uav_detection/resolve/main/features.py
- Command line
-
hf download hf://spaces/alfShymon/failure_uav_detection/features.py
-
curl -L -o features.py https://huggingface.co/spaces/alfShymon/failure_uav_detection/resolve/main/features.py
4.84 kB
| from __future__ import annotations | |
| import numpy as np | |
| from numpy.fft import rfft | |
| def build_drift_fe_names( | |
| imu_cols, | |
| rc_cols, | |
| quantiles=(0.05, 0.25, 0.50, 0.75, 0.95), | |
| ): | |
| fe = [] | |
| for stat in ["mean", "std", "min", "max"]: | |
| for c in list(imu_cols) + list(rc_cols): | |
| fe.append(f"{c}_{stat}") | |
| for c in list(imu_cols) + list(rc_cols): | |
| fe.append(f"{c}_slope") | |
| for c in list(imu_cols) + list(rc_cols): | |
| fe.append(f"{c}_rms") | |
| for c in list(imu_cols) + list(rc_cols): | |
| fe.append(f"{c}_energy") | |
| for c in list(imu_cols) + list(rc_cols): | |
| fe.append(f"{c}_ac_lag1") | |
| for q in quantiles: | |
| for c in list(imu_cols) + list(rc_cols): | |
| fe.append(f"{c}_q{int(q*100):02d}") | |
| for c in list(imu_cols) + list(rc_cols): | |
| fe.append(f"{c}_skew") | |
| for c in list(imu_cols) + list(rc_cols): | |
| fe.append(f"{c}_kurt") | |
| for c in list(imu_cols) + list(rc_cols): | |
| fe.append(f"{c}_fft_lowband") | |
| for c in list(imu_cols) + list(rc_cols): | |
| fe.append(f"{c}_fft_centroid") | |
| for c in list(imu_cols) + list(rc_cols): | |
| fe.append(f"{c}_fft_entropy") | |
| for c in list(imu_cols) + list(rc_cols): | |
| fe.append(f"{c}_fft_low_high_ratio") | |
| for ic in list(imu_cols): | |
| for rc in list(rc_cols): | |
| fe.append(f"{ic}__x__{rc}_xcorr") | |
| return fe | |
| def extract_window_features_drift(X, imu_idx, rc_idx, use_lowband=True, lowband_frac=0.2): | |
| X = np.asarray(X, float) | |
| N, T, F = X.shape | |
| eps = 1e-12 | |
| mean = X.mean(axis=1) | |
| std = X.std(axis=1) | |
| minv = X.min(axis=1) | |
| maxv = X.max(axis=1) | |
| slope = (X[:, -1, :] - X[:, 0, :]) / max(T - 1, 1) | |
| rms = np.sqrt((X ** 2).mean(axis=1)) | |
| energy = (X ** 2).sum(axis=1) / max(T, 1) | |
| x1 = X[:, :-1, :] | |
| x2 = X[:, 1:, :] | |
| m1 = x1.mean(axis=1) | |
| m2 = x2.mean(axis=1) | |
| v1 = ((x1 - m1[:, None, :]) ** 2).mean(axis=1) | |
| v2 = ((x2 - m2[:, None, :]) ** 2).mean(axis=1) | |
| cov = ((x1 - m1[:, None, :]) * (x2 - m2[:, None, :])).mean(axis=1) | |
| ac = np.zeros_like(cov) | |
| mask = (v1 > 0) & (v2 > 0) | |
| ac[mask] = cov[mask] / (np.sqrt(v1[mask] * v2[mask]) + eps) | |
| quantiles = [0.05, 0.25, 0.5, 0.75, 0.95] | |
| q = np.quantile(X, q=quantiles, axis=1) | |
| q = np.transpose(q, (1, 0, 2)).reshape(N, -1) | |
| std_safe = std.copy() | |
| std_safe[std_safe == 0] = 1 | |
| Zs = (X - mean[:, None, :]) / std_safe[:, None, :] | |
| skew = (Zs ** 3).mean(axis=1) | |
| kurt = (Zs ** 4).mean(axis=1) | |
| fft_vals = rfft(X, axis=1) | |
| power = (np.abs(fft_vals) ** 2) # (N, T_fft, F) | |
| T_fft = power.shape[1] | |
| if T_fft > 2: | |
| half = T_fft // 2 | |
| if use_lowband: | |
| k = max(2, int(lowband_frac * T_fft)) | |
| low = power[:, 1:k, :].mean(axis=1) | |
| else: | |
| low = power[:, 1:half, :].mean(axis=1) | |
| high = power[:, int(0.5*T_fft):, :].mean(axis=1) | |
| freqs = np.arange(T_fft).reshape(1, T_fft, 1) | |
| total_power = power.sum(axis=1) + eps | |
| centroid = (freqs * power).sum(axis=1) / total_power | |
| P = power + eps | |
| Pn = P / (P.sum(axis=1, keepdims=True) + eps) | |
| spectral_entropy = -(Pn * np.log(Pn)).sum(axis=1) | |
| lh_ratio = low / (high + eps) | |
| else: | |
| low = high = centroid = spectral_entropy = lh_ratio = np.zeros((N, F)) | |
| if len(imu_idx) > 0 and len(rc_idx) > 0: | |
| imu_w = X[:, :, imu_idx] | |
| rc_w = X[:, :, rc_idx] | |
| imu_c = imu_w - imu_w.mean(axis=1, keepdims=True) | |
| rc_c = rc_w - rc_w.mean(axis=1, keepdims=True) | |
| cov_imu_rc = np.einsum('ntp,ntq->npq', imu_c, rc_c) / max(T - 1, 1) | |
| var_imu = (imu_c ** 2).mean(axis=1) | |
| var_rc = (rc_c ** 2).mean(axis=1) | |
| denom = np.sqrt(var_imu[:, :, None] * var_rc[:, None, :]) + eps | |
| xcorr = (cov_imu_rc / denom).reshape(N, -1) | |
| else: | |
| xcorr = np.zeros((N, 0)) | |
| feats = np.concatenate([ | |
| mean, std, minv, maxv, | |
| slope, rms, energy, | |
| ac, q, skew, kurt, | |
| low, centroid, spectral_entropy, lh_ratio, | |
| xcorr | |
| ], axis=1) | |
| return np.nan_to_num(feats) | |
| def build_lgb_features( | |
| X, | |
| encoder, | |
| imu_cols, | |
| use_lowband=True, | |
| lowband_frac=0.2, | |
| return_parts=False | |
| ): | |
| imu_idx = list(range(len(imu_cols))) | |
| X_imu = X[:, :, imu_idx] | |
| Z = encoder.predict(X_imu, verbose=0) | |
| Xfe = extract_window_features_drift( | |
| X_imu, | |
| imu_idx=imu_idx, | |
| rc_idx=[], | |
| use_lowband=use_lowband, | |
| lowband_frac=lowband_frac | |
| ) | |
| X_lgb = np.hstack([Z, Xfe]) | |
| if return_parts: | |
| return X_lgb, Z, Xfe | |
| return X_lgb | |
| def build_lgb_feature_names(latent_dim, imu_cols): | |
| return [f"Z{i}" for i in range(int(latent_dim))] + build_drift_fe_names( | |
| imu_cols, rc_cols=[] | |
| ) |