newthought-quantum-coherence / bi_coupled_coherence.py
9x25dillon's picture
Upload 10 files
3ea65b2 verified
# ==== PATCH: Corrected significant-position selector for DualStreamProcessor ====
# This monkey-patches the class method with an adaptive, band-aware threshold.
# It is robust to the existing file layout (no need to find/replace inside the class).
def _dsp_get_significant_positions(self, capsule: np.ndarray, b_idx: int, threshold: float = 1e-3) -> List[SpatialPosition]:
"""Select positions with significant amplitude using an adaptive, band-specific cutoff.
Steps:
1) Collect amplitudes for the requested band's slice.
2) Drop values <= `threshold` (absolute noise floor).
3) Set adaptive cutoff tau = max(threshold, percentile(nz, 85)).
4) Return positions with amplitude >= tau.
"""
amps: List[float] = []
for pos in self.encoder.positions:
amps.append(
float(
np.abs(
capsule[pos.m + self.encoder.M, pos.n + self.encoder.N, b_idx]
)
)
)
if not amps:
return []
a = np.asarray(amps, dtype=float)
nz = a[a > threshold]
if nz.size == 0:
tau = threshold
else:
tau = max(threshold, float(np.percentile(nz, 85)))
positions: List[SpatialPosition] = []
for i, pos in enumerate(self.encoder.positions):
if a[i] >= tau:
positions.append(pos)
return positions
# Bind the patched method onto the class
try:
DualStreamProcessor._get_significant_positions = _dsp_get_significant_positions # type: ignore[attr-defined]
except NameError:
# If the class isn't defined yet (e.g., reordering), this patch will be applied at import time after class definition
pass