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
| def extract_entropy_features(self, gaze_data: np.ndarray) -> dict: """ 提取眼动熵特征 眼动熵衡量注视点的空间分布随机性 """ x, y = gaze_data[:, 0], gaze_data[:, 1] features = { 'gaze_entropy_static': self._spatial_entropy(x, y), 'gaze_entropy_transition': self._transition_entropy(x, y), 'gaze_sample_entropy': self._sample_entropy(x, y), 'gaze_approximate_entropy': self._approximate_entropy(x, y), } return features def _spatial_entropy(self, x: np.ndarray, y: np.ndarray, grid_size: int = 8) -> float: """ 计算空间熵 将注视点分到网格中,计算分布的熵 """ x_bins = np.linspace(x.min(), x.max() + 1e-6, grid_size + 1) y_bins = np.linspace(y.min(), y.max() + 1e-6, grid_size + 1) hist, _, _ = np.histogram2d(x, y, bins=[x_bins, y_bins]) prob = hist.flatten() / (hist.sum() + 1e-6) prob = prob[prob > 0] entropy = -np.sum(prob * np.log2(prob)) max_entropy = np.log2(len(prob)) normalized_entropy = entropy / max_entropy if max_entropy > 0 else 0 return normalized_entropy def _transition_entropy(self, x: np.ndarray, y: np.ndarray, grid_size: int = 8) -> float: """ 计算转移熵 衡量注视点在网格间转移的随机性 """ x_bins = np.linspace(x.min(), x.max() + 1e-6, grid_size + 1) y_bins = np.linspace(y.min(), y.max() + 1e-6, grid_size + 1) x_idx = np.digitize(x, x_bins) - 1 y_idx = np.digitize(y, y_bins) - 1 x_idx = np.clip(x_idx, 0, grid_size - 1) y_idx = np.clip(y_idx, 0, grid_size - 1) cell_idx = x_idx * grid_size + y_idx transitions = np.zeros((grid_size * grid_size, grid_size * grid_size)) for i in range(len(cell_idx) - 1): transitions[cell_idx[i], cell_idx[i+1]] += 1 row_sums = transitions.sum(axis=1, keepdims=True) trans_prob = transitions / (row_sums + 1e-6) entropy = 0 for i in range(len(transitions)): for j in range(len(transitions)): if trans_prob[i, j] > 0: entropy -= trans_prob[i, j] * np.log2(trans_prob[i, j]) max_entropy = np.log2(grid_size * grid_size) normalized = entropy / max_entropy if max_entropy > 0 else 0 return normalized def _sample_entropy(self, signal: np.ndarray, m: int = 2, r: float = 0.2) -> float: """ 计算样本熵 衡量信号的不规则性 """ N = len(signal) r *= np.std(signal) def _count_matches(template, data, tol): count = 0 for i in range(len(data) - len(template) + 1): if np.max(np.abs(data[i:i+len(template)] - template)) <= tol: count += 1 return count A = 0 B = 0 for i in range(N - m): template_m = signal[i:i+m] template_m1 = signal[i:i+m+1] for j in range(i+1, N - m): if np.max(np.abs(signal[j:j+m] - template_m)) <= r: B += 1 if j < N - m and np.max(np.abs(signal[j:j+m+1] - template_m1)) <= r: A += 1 if B == 0: return 0 return -np.log(A / B) def _approximate_entropy(self, signal: np.ndarray, m: int = 2, r: float = 0.2) -> float: """计算近似熵""" N = len(signal) r *= np.std(signal) def _phi(m): patterns = [] for i in range(N - m + 1): patterns.append(signal[i:i+m]) counts = [] for p in patterns: count = sum(1 for q in patterns if np.max(np.abs(q - p)) <= r) counts.append(count) return np.mean(np.log(np.array(counts) / (N - m + 1))) return _phi(m) - _phi(m + 1)
if __name__ == "__main__": extractor = GazeFeatureExtractor(fps=30) np.random.seed(42) normal_gaze = np.column_stack([ np.random.normal(0.5, 0.1, 900), np.random.normal(0.5, 0.08, 900), np.random.normal(4.0, 0.3, 900), np.random.normal(4.0, 0.3, 900), ]) distracted_gaze = np.column_stack([ np.random.normal(0.5, 0.2, 900), np.random.normal(0.5, 0.15, 900), np.random.normal(4.2, 0.5, 900), np.random.normal(4.2, 0.5, 900), ]) normal_features = extractor.extract_entropy_features(normal_gaze) distracted_features = extractor.extract_entropy_features(distracted_gaze) print("正常状态:") for k, v in normal_features.items(): print(f" {k}: {v:.4f}") print("\n认知分心状态:") for k, v in distracted_features.items(): print(f" {k}: {v:.4f}")
|