Spaces:
Sleeping
Sleeping
Upload gating.py
Browse files- app/ml/gating.py +22 -4
app/ml/gating.py
CHANGED
|
@@ -37,8 +37,8 @@ def gate_signal(
|
|
| 37 |
signal: List[float],
|
| 38 |
window_size: int = 128,
|
| 39 |
step: int = 64,
|
| 40 |
-
threshold: float = 0.
|
| 41 |
-
temperature: float = 0.
|
| 42 |
return_windows: bool = False,
|
| 43 |
max_windows: int = 200,
|
| 44 |
) -> Tuple[List[float], Dict[str, Any]]:
|
|
@@ -73,6 +73,7 @@ def gate_signal(
|
|
| 73 |
total_windows = 0
|
| 74 |
selected_windows = 0
|
| 75 |
windows_meta: List[Dict[str, Any]] = []
|
|
|
|
| 76 |
|
| 77 |
for start in range(0, len(signal) - window_size + 1, step):
|
| 78 |
window = signal[start : start + window_size]
|
|
@@ -83,6 +84,7 @@ def gate_signal(
|
|
| 83 |
prob = gate_probability_with_hysteresis(sig, threshold=threshold, temperature=temperature, last_activation=last_activation)
|
| 84 |
|
| 85 |
chosen = prob >= 0.5
|
|
|
|
| 86 |
if chosen:
|
| 87 |
selected.extend(window)
|
| 88 |
selected_windows += 1
|
|
@@ -101,8 +103,24 @@ def gate_signal(
|
|
| 101 |
}
|
| 102 |
)
|
| 103 |
|
| 104 |
-
if not selected:
|
| 105 |
-
selected
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
meta: Dict[str, Any] = {
|
| 108 |
"total_windows": total_windows,
|
|
|
|
| 37 |
signal: List[float],
|
| 38 |
window_size: int = 128,
|
| 39 |
step: int = 64,
|
| 40 |
+
threshold: float = 0.6,
|
| 41 |
+
temperature: float = 0.1,
|
| 42 |
return_windows: bool = False,
|
| 43 |
max_windows: int = 200,
|
| 44 |
) -> Tuple[List[float], Dict[str, Any]]:
|
|
|
|
| 73 |
total_windows = 0
|
| 74 |
selected_windows = 0
|
| 75 |
windows_meta: List[Dict[str, Any]] = []
|
| 76 |
+
scored_windows: List[Tuple[float, List[float]]] = []
|
| 77 |
|
| 78 |
for start in range(0, len(signal) - window_size + 1, step):
|
| 79 |
window = signal[start : start + window_size]
|
|
|
|
| 84 |
prob = gate_probability_with_hysteresis(sig, threshold=threshold, temperature=temperature, last_activation=last_activation)
|
| 85 |
|
| 86 |
chosen = prob >= 0.5
|
| 87 |
+
scored_windows.append((sig, window))
|
| 88 |
if chosen:
|
| 89 |
selected.extend(window)
|
| 90 |
selected_windows += 1
|
|
|
|
| 103 |
}
|
| 104 |
)
|
| 105 |
|
| 106 |
+
if not selected and scored_windows:
|
| 107 |
+
# If nothing selected, pick the highest-significance window to avoid empty output
|
| 108 |
+
scored_windows.sort(key=lambda x: x[0], reverse=True)
|
| 109 |
+
best_sig, best_window = scored_windows[0]
|
| 110 |
+
selected = best_window
|
| 111 |
+
selected_windows = 1
|
| 112 |
+
if return_windows and len(windows_meta) < max_windows:
|
| 113 |
+
windows_meta.insert(
|
| 114 |
+
0,
|
| 115 |
+
{
|
| 116 |
+
"start": 0,
|
| 117 |
+
"end": window_size,
|
| 118 |
+
"significance": best_sig,
|
| 119 |
+
"probability": 1.0,
|
| 120 |
+
"selected": True,
|
| 121 |
+
"forced": True,
|
| 122 |
+
},
|
| 123 |
+
)
|
| 124 |
|
| 125 |
meta: Dict[str, Any] = {
|
| 126 |
"total_windows": total_windows,
|