Ë
    §½Àjä  ã            
       ó   — d Z ddlZddlZddlZddlmZmZ ddlm	Z	m
Z
mZ ddlmZmZ ddlmZmZ  eej&                  «      dkD  rej&                  d   ndZej+                  d	«      r eed
d «      ndZedv Zeserdn ee«      Z ej4                  d«      Zej8                  j;                  edde› �«      Z e	dddgddd¬«      Z ej4                  d«      Z d„ Z!d„ Z"d„ Z#e$dk(  rg ejJ                  ed¬«        e«       jL                  jO                  «       Z( e)  ed
¬«      d„ e(D «       «      «      Z* e+de› de*› d ee(«      › �«       yy) aÕ  Glimpse features (Raghavan et al.: glimpsed vs masked speech are encoded differently; the ignored talker is mostly
encoded in its glimpses, the attended talker also in its masked parts).

Per trial, from the RAW 128-Hz envelopes of both streams (unfiltered gammatone^0.6, supplied):
  level  L_s(t) = (20/0.6) log10(causal 1st-order smoothing (25 ms) of env_s)     (env ~ amplitude^0.6 -> dB)
  mask   m_s(t) = 1 if L_s(t) - L_other(t) > THR dB (talker s clearly louder: a glimpse of s), else 0
  glim_s = causal Butterworth band-pass 1-9 Hz (order 2) of env_s * m_s
  mask_s = causal band-pass of env_s * (1 - m_s)                                    (the masked part of s)
then 128 -> 32 Hz and the same 1-s edge trim as the decoder cache (alignment with cache env checked: lag 0).
Everything is CAUSAL (no exempted method is used a second time): smoothing, mask and band-pass only use the past.
Output cache/glimpse<THR>/<key>.npz: glim (T,2), mask (T,2), m (T,2) at 32 Hz; loaded by common_exp.load_trial.
Graded variant ('softW', e.g. soft20): dominance STRENGTH instead of the binary mask, m_s = clip((L_s - L_other) / W dB,
0, 1) (0 when not louder, 1 when >= W dB louder); glim = BP(env m), mask = BP(env (1 - m)) as before.
Normalised variant ('norm'): g_s = (e_s - e_o) / (e_s + e_o + eps) in [-1, 1] on the smoothed envelopes (independent of
the overall loudness; eps = 5% of the CAUSAL running mean (EW, 10 s, past only) of e_s + e_o); m_s = max(g_s, 0);
additionally the dominance target dom = causal band-pass of [g_0, g_1 = -g_0].
Onset variant ('onorm' = norm + relative onsets): o_s = positive first difference of the smoothed envelope (128 Hz);
  odiff = o_0 - o_1 ;  orel = (o_0 - o_1) / (o_0 + o_1 + eps), exactly 0 where neither talker has an onset
  (eps = 5% of the causal running mean (10 s, past only) of o_0 + o_1); both causally band-passed, target pair [x, -x].
  +1: only talker 0 has an onset, -1: only talker 1, 0: equal onsets or none.
'full' = onorm + the RAW envelope difference ediff = e_0 - e_1 (smoothed envelopes, causal band-pass, pair [x, -x]):
the un-normalised counterpart of dom (dom treats 0.9 vs 0.4 and 0.6 vs 0.1 differently, ediff does not).
usage: python build_glimpse.py [THR_dB=10 | softW | norm | onorm | full]é    N)ÚParallelÚdelayed)ÚbutterÚlfilterÚsosfilt)ÚEXPÚ
load_trial)Úload_envÚtrial_indexé   Ú10Úsofté   )ÚnormÚonormÚfullgš™™™™™I¿ÚcacheÚglimpseé   é	   Úbandpassé€   Úsos)ÚfsÚoutputg      Ô¿c                 óv   — t        dt        z
  gdt         g| «      }t        j                  | d   g|dd g«      S )z6Causal EW running mean (10 s) of the samples BEFORE t.r   r   Néÿÿÿÿ)r   ÚA_EPSÚnpÚconcatenate)ÚxÚruns     úƒ/root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset/experiments/external_data/Fuglsang2020/../../decoder_lodo/build_glimpse.pyÚ	past_meanr$   ,   s<   € ä
�1”u‘9�+ ¤E 6˜{¨AÓ
.€CÜ�>‰>˜A˜a™D˜6 3 s¨ 8Ð,Ó-Ð-ó    c                 ó0  — t        dt        z
  gdt         gt        j                  | d«      d¬«      }d„ }i }t        �rV|j                  d«      }|dd…df   |dd…df   z
  |dt        |«      z  z   z  }t        j                  t        j                  |d«      t        j                  | d«      gd«      } ||«      |d<   t        d	k(  r ||dd…df   |dd…df   z
  «      |d
<   t        dv �rjt        j                  dt        j                  |d|dd ¬«      «      }|j                  d«      } |t        j                  |dkD  |dd…df   |dd…df   z
  |dt        |«      z  z   dz   z  d«      «      |d<    ||dd…df   |dd…df   z
  «      |d<   nÄdt        j                  |dd…df   |dd…df   z  «      z  }	t        rQt        j                  t        j                  |	t        z  dd«      t        j                  |	 t        z  dd«      gd«      nCt        j                  |	t        kD  |	 t        kD  gd«      j                  t        j                   «      }|j#                  t%        t&        | |z  d¬«      t%        t&        | d|z
  z  d¬«      |¬«       |S )z�e: (N, 2) raw 128-Hz envelopes -> dict of (N, 2) arrays at 128 Hz, all causal: glim, mask, m (+ dom for 'norm',
    + orel, odiff for 'onorm').r   g•Ö&è.>r   ©Úaxisc                 óT   — t        t        t        j                  | |  gd«      d¬«      S )Nr   r   r'   )r   ÚBPr   Ústack)r!   s    r#   Ú<lambda>zfeatures.<locals>.<lambda>6   s   € ”WœR¤§¡¨1¨q¨b¨'°1Ó!5¸AÕ>r%   Ngš™™™™™©?Údomr   Úediff)r   r   g        )r(   Úprependgê-�™—q=ÚorelÚodiffg«ªªªªª@@)ÚglimÚmaskÚm)r   ÚAr   ÚmaximumÚNORMÚsumr$   r+   ÚARGÚdiffÚwhereÚlog10ÚSOFTÚclipÚTHRÚastypeÚfloat64Úupdater   r*   )
ÚeÚsÚpairÚoutÚtotÚgr4   ÚoÚtoÚDs
             r#   ÚfeaturesrL   2   sL  € ô 	�”Q‘�˜!œa˜R˜¤"§*¡*¨Q°Ó"5¸AÔ>€AÙ>€DØ
€CÞØ�e‰e�A‹hˆØŠq�!ˆt‰W�qš˜A˜‘wÑ 3¨´	¸#³Ñ)>Ñ#>Ñ?ˆÜ�H‰H”b—j‘j  AÓ&¬¯
©
°A°2°qÓ(9Ð:¸AÓ>ˆÙ˜!“WˆˆE‰
Ü�&Š=Ù ¢! Q $¡¨!ªA¨q¨D©'Ñ 1Ó2ˆC�‰LÜÐ#Ò#Ü—
‘
˜3¤§¡¨°¸1¸R¸a¸5Ô AÓBˆAØ—‘�q“ˆBÙœrŸx™x¨¨Q©°²1°a°4±¸1ºQÀ¸T¹7Ñ1BÀrÈDÔS\Ð]_ÓS`ÑL`ÑG`ÐchÑGhÑ0iÐknÓoÓpˆC�‰KÙ ¢! Q $¡¨!ªA¨q¨D©'Ñ 1Ó2ˆC�ŠLàœŸ™ !¢A q D¡'¨Aªa°¨d©GÑ"3Ó4Ñ4ˆÝQUŒR�X‰X”r—w‘w˜q¤4™x¨¨AÓ.´·±¸¸¼T¹	À1ÀaÓ0HÐIÈ1ÔMÜ—(‘(˜A¤™G a R¬#¡XÐ.°Ó2×9Ñ9¼"¿*¹*ÓEð 	
à‡J�J”GœB  A¡¨AÔ.´W¼RÀÀaÈ!ÁeÁÐSTÔ5UÐYZ€JÔ[Ø€Jr%   c           	      ó>  — t         j                  j                  t        | dz   «      }t         j                  j	                  |«      ryt        t        d| «      d   «      }t        t        | «      j                  t        j                  «      «      j                  «       D ��ci c]1  \  }}||d d d…   dd|z    j                  t        j                  «      “Œ3 }}}t        |d   «      |k(  sJ | t        |d   «      |f«       ‚t        j                  |fi |¤Ž yc c}}w )	Nz.npzr   Úmat_reacÚenvr   é    r2   r   )ÚosÚpathÚjoinÚOUTÚexistsÚlenr	   rL   r
   r@   r   rA   ÚitemsÚfloat32Úsavez)ÚkeyÚpÚTÚkÚvÚFs         r#   Úbuildr`   L   sì   € Ü
�‰�‰”S˜# ™,Ó'€AÜ	‡w�w‡~�~�aÔØÜŒJ�z 3Ó'¨Ñ.Ó/€AÜ=EÄhÈsÃm×FZÑFZÔ[]×[eÑ[eÓFfÓ=g×=mÑ=mÔ=oÔpÑ=o±T°Q¸ˆˆA‰c�ˆc‰F�2�b˜1‘fÐ×$Ñ$¤R§Z¡ZÓ0Ñ	0Ð=o€AÑpÜˆq�‰y‹>˜QÒÐ8 ¤c¨!¨F©)£n°aÐ 8Ó8ÐÜ‡H�HˆQÑ�!ÒØùó 	qs   Â"6DÚ__main__T)Úexist_ok)Ún_jobsc              #   óF   K  — | ]  } t        t        «      |«      –— Œ y ­w)N)r   r`   )Ú.0r]   s     r#   Ú	<genexpr>rf   Z   s   è ø€ Ð?¹$°Q˜~œw¤u›~¨a×0¹$ùs   ‚!z: built z of ),Ú__doc__rQ   ÚsysÚnumpyr   Újoblibr   r   Úscipy.signalr   r   r   Ú
common_expr   r	   Úcommonr
   r   rV   Úargvr9   Ú
startswithÚfloatr=   r7   r?   Úexpr   rR   rS   rT   r*   r5   r$   rL   r`   Ú__name__ÚmakedirsrZ   ÚtolistÚkeysr8   ÚnÚprint© r%   r#   Ú<module>ry      sM  ðñLó. 
Û 
ã ß $ß 1Ñ 1ç &ß (á˜Ÿ™“] QÒ&€c‡h�hˆq‚k¨D€ØŸ™¨Ô/�uˆS��ˆW„~°T€Ø
Ð'Ð'€Ù‘t�d¡%¨£*€Øˆ�‰Ð Ó!€Ø‡g�g‡l�l�3˜ 7¨3¨% Ó1€ÙˆA��1ˆv�z c°%Ô8€Ø
€B‡F�FÐÓ€ò.òò4ð ˆzÒØ€B‡K�K�˜dÕ#Ù‹=×Ñ×#Ñ#Ó%€DÙÐ‰H˜AÔÑ?¹$Ó?Ó?Ó@€AÙ	ˆG�C�5˜   4©¨D«	 {Ð
3Õ4ð	 r%   