Ë
    ¬!ëj«8  ã                  ó(  — d Z ddlm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mZmZmZ ddlZddlmZ ddlmZmZ dZe G d	„ d
«      «       Zdd„Zdd„Zdd„Zdeef	 	 	 	 	 	 	 	 	 	 	 dd„Z	 	 	 	 	 	 dd„Zdd„Zdd„Z	 	 	 	 	 	 	 	 	 	 dd„Z dd„Z!dd„Z"y)aþ  Read CND-format subject recordings used by the challenge.

A CND subject directory holds ``dataSubN.mat`` (EEG) and ``dataStimN.mat``
(auditory envelopes), both MATLAB v7 files.  Each file carries one struct
(``neural`` / ``stim``) whose ``data`` field stores the recordings as a cell
array with one entry per trial; a trial loads from its own cell.

The optional preprocessing stage mirrors ``matlab/loadSubject.m`` step for step:
channel selection -> re-referencing -> resampling -> per-trial normalisation.
é    )ÚannotationsN)Ú	dataclassÚfield)ÚAnyÚDictÚListÚOptionalÚSequenceÚTupleé   )ÚNEURAL_PREFIXÚSTIM_PREFIX©ÚchannelSelectionÚreReferenceÚtargetFsÚnormEEGÚnormFeaturesc                  óz   — e Zd ZU dZded<   ded<    ee¬«      Zded<    ee¬«      Zded	<    ee¬«      Z	d
ed<   y)ÚSubjectDataaú  One subject's recordings, split per trial.

    Attributes
    ----------
    subject_id : int
        Subject number from the file name.
    fs : float
        Sampling rate of ``neural`` and ``stim`` after preprocessing.
    neural : list of np.ndarray
        Per trial, an ``(n_channels, n_samples)`` EEG matrix.
    stim : list of np.ndarray
        Per trial, an ``(n_streams, n_samples)`` envelope matrix.
    chan_labels : list of str
        Channel labels actually produced, in output order.
    ÚintÚ
subject_idÚfloatÚfs)Údefault_factoryzList[np.ndarray]ÚneuralÚstimú	List[str]Úchan_labelsN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   Úlistr   r   r   © ó    ú˜/root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset/test_phase/example_pipeline/TOPUBLISH-test-phase-inference-pipeline-example/challenge/io_cnd.pyr   r   "   sB   … ñð  ƒOØƒIÙ$°TÔ:€FÐÓ:Ù"°4Ô8€DÐ
Ó8Ù"°4Ô8€K�Ô8r'   r   c                óþ  — | €g S t        j                  | «      }|j                  dk(  rg S |j                  j                  dv r(|j                  d«      D �cg c]  }t        |«      ‘Œ c}S g }|j                  d«      D ]w  }t        |t         j                  «      rA|j                  dk(  rd}n/|j                  d«      d   }t        |t         j                  «      rŒA|j                  t        |«      «       Œy |S c c}w )z=Flatten a MATLAB cellstr / char array as scipy hands it back.r   ÚUSéÿÿÿÿÚ )
ÚnpÚasarrayÚsizeÚdtypeÚkindÚreshapeÚstrÚ
isinstanceÚndarrayÚappend)ÚvalueÚarrayÚitemÚouts       r(   Ú_cellstrr;   ;   sÚ   € à€}Øˆ	Ü�J‰J�uÓ€EØ‡z�z�Q‚Øˆ	Ø‡{�{×Ñ˜4ÑØ&+§m¡m°BÔ&7Ó8Ñ&7˜d”�D•	Ð&7Ñ8Ð8Ø
€CØ—‘˜bÖ!ˆÜ˜œrŸz™zÔ*Ø�y‰y˜AŠ~Ø�ØØ—<‘< Ó# AÑ&ˆDô	 ˜œrŸz™zÕ*ð
 	�
‰
”3�t“9Õð "ð €Jùò 9s   ÁC:c                óÈ   — ddddddœ}| s|S t        t        | «      t        t        «      z
  «      }|rt        d|›dt	        t        «      ›�«      ‚|j                  | «       |S )z�Fill the loadSubject() defaults and reject unknown keys.

    Port of ``loadSubject>resolveOpts``: an unknown option raises.
    NÚnoner   z unknown preprocessing option(s) z; loadSubject knows )ÚsortedÚsetÚ_KNOWN_OPTIONSÚ
ValueErrorr%   Úupdate)ÚpreprocessingÚoptionsÚunknowns      r(   Ú_resolve_optionsrF   O   so   € ð !ØØØØñ€Gñ ØˆÜ”S˜Ó'¬#¬nÓ*=Ñ=Ó>€GÙÝâœœ^Ô,ð.ó
ð 	
ð ‡N�N�=Ô!Ø€Nr'   c                ó†  — g }g }t        | «      D ]X  \  }}t        j                  d|«      }|sŒ|j                  t	        |j                  d«      «      «       |j                  |«       ŒZ |s't        d| rdj                  | «      z  «      ‚dz  «      ‚t        j                  |d¬«      }|D �cg c]  }||   ‘Œ	 c}S c c}w )a/  Rows of ``stim.data`` carrying ``<feature>_stream<k>``, ordered by ``k``.

    Port of ``loadSubject>buildStreamMap``.  ``k`` is the stream identity (which
    talker).  Rows are ordered by ``k`` numerically (``_stream2`` precedes
    ``_stream10``); that order is what the per-sample labels index.
    z_stream(\d+)$r   zsstim.names contains no '<feature>_stream<k>' entries; there are no candidate streams to decide between (found: %s).ú, r=   Ústable)r1   )
Ú	enumerateÚreÚsearchr6   r   ÚgrouprA   Újoinr-   Úargsort)Úfeature_namesÚrowsÚidentityÚpositionÚnameÚmatchÚorderÚis           r(   Ú_build_stream_maprX   g   sÉ   € ð €DØ€HÜ# MÖ2‰ˆ�$Ü—	‘	Ð*¨DÓ1ˆÚØ�O‰OœC §¡¨A£Ó/Ô0Ø�K‰K˜Õ!ð	 3ñ
 Üð?á+8ˆt�y‰y˜Ó'ñFó
ð 	
ð ?EñFó
ð 	
ô
 �J‰J�x hÔ/€EÙ"Ó#™U˜ˆD�‹G˜UÑ#Ð#ùÒ#s   Â/B>c                óº  — t        |«      }t        j                  j                  | |› |› d�«      }t        j                  j                  | |› |› d�«      }t	        j
                  |«      d   d   }t	        j
                  |«      d   d   }	t        t        j                  |d   «      j                  d«      d   «      }
t        t        j                  |	d   «      j                  d«      d   «      }||
k7  rt        d|
|fz  «      ‚g }d	|j                  j                  v r|d	   }|j                  rt        |d
   «      }t        |d   |«      \  }}|d   }|	d   }|j                   d   }|j                   d   |k7  rt        d||j                   d   fz  «      ‚|d   €|
nt        |d   «      }g }t#        |«      D ]ƒ  }t        j                  |d|f   t        j$                  ¬«      }t'        ||«      }t)        ||d   ||«      }t+        ||
|d   «      }t-        ||d   «      }|j/                  |j0                  «       Œ… d|	j                  j                  v rt        |	d   «      }ng }t3        |«      }g }t#        |«      D �]F  }g }|D ]Ä  }t        j                  |||f   t        j$                  ¬«      }|j4                  dkD  r8t7        |j                   «      dkD  r t        d||   |j                   d   fz  «      ‚|j                  dd«      }t+        ||
|d   «      }t-        ||d   «      }|j/                  |j                  d«      «       ŒÆ t        j8                  |d¬«      }|j                   d   ||   j                   d   k7  r.t        d|||   j                   d   |j                   d   fz  «      ‚|j/                  |«       �ŒI t;        |||||¬«      S )aj  Load one subject's neural and stimulus recordings.

    Parameters
    ----------
    data_dir : str
        Directory containing ``<neural_prefix>N.mat`` / ``<stim_prefix>N.mat``.
    subject_id : int
        Subject number ``N``.
    preprocessing : dict, optional
        ``channelSelection``, ``reReference``, ``targetFs``, ``normEEG``,
        ``normFeatures`` -- the same names and meanings as ``loadSubject.m``.
        Default: no channel selection, no re-referencing, native rate, no
        normalisation.
    neural_prefix, stim_prefix : str
        File-name prefixes.  The default reads the raw recordings; pass
        ``pre_dataSub`` / ``pre_dataStim`` for the downsampled copies, which
        are what the shipped decoder was fitted on.

    Returns
    -------
    SubjectData
        Trials are ordered by their column index in the source cell array.
    z.matr   )r   r   r   r   r+   r   z8neural.fs (%g) ~= stim.fs (%g): reconcile upstream firstÚchanlocsÚlabelsr   Údatar   z1neural has %d trials but stim.data has %d columnsr   ©r0   r   r   Únameszxstream feature %r has %d columns; the per-sample target gather assumes one column per stream. Reduce it before decoding.r   ©Úaxisz3trial %d: EEG is %d samples but the features are %d)r   r   r   r   r   )rF   ÚosÚpathrN   ÚsioÚloadmatr   r-   r.   r2   rA   r0   r^   r/   r;   Ú_resolve_channel_selectionÚshapeÚrangeÚfloat64Ú_apply_channel_selectionÚ_apply_rereferenceÚ	_resampleÚ
_normaliser6   ÚTrX   ÚndimÚminÚstackr   )Údata_dirr   rC   Úneural_prefixÚstim_prefixrD   Úneural_pathÚ	stim_pathÚneural_structÚstim_structÚfs_inÚfs_stimÚ
raw_labelsrZ   ÚindicesÚ
out_labelsÚneural_cellÚ	stim_cellÚn_trialsÚfs_outÚneural_trialsÚtrial_indexÚblockrP   Ústream_rowsÚstim_trialsÚstreamsÚstream_indexÚrawÚcolumnÚtrials                                  r(   Úload_subjectr‹      sä  € ô< ˜}Ó-€Gä—'‘'—,‘,˜x¨M¨?¸:¸,ÀdÐ)KÓL€KÜ—‘—‘˜X¨+¨°z°lÀ$Ð'GÓH€Iä—K‘K Ó,¨XÑ6°tÑ<€MÜ—+‘+˜iÓ(¨Ñ0°Ñ6€Kä”"—*‘*˜]¨4Ñ0Ó1×9Ñ9¸"Ó=¸aÑ@ÓA€EÜ”B—J‘J˜{¨4Ñ0Ó1×9Ñ9¸"Ó=¸aÑ@ÓA€GØ�%ÒÜØFÈ%ÐQXÐIYÑYó
ð 	
ð €JØ�]×(Ñ(×.Ñ.Ñ.Ø  Ñ,ˆØ�=Š=Ü! (¨8Ñ"4Ó5ˆJä4ØÐ"Ñ# ZóÑ€GˆZð   Ñ'€KØ˜FÑ#€IØ× Ñ  Ñ#€HØ‡��qÑ˜XÒ%ÜØ?Ø˜Ÿ™¨Ñ+Ð,ñ-ó
ð 	
ð
 ˜jÑ)Ð1‰U´u¸WÀZÑ=PÓ7Q€Fà&(€MÜ˜X–ˆÜ—
‘
˜; q¨+ ~Ñ6¼b¿j¹jÔIˆÜ(¨°Ó8ˆÜ" 5¨'°-Ñ*@À'È:ÓVˆÜ˜% ¨°
Ñ(;Ó<ˆÜ˜5 '¨)Ñ"4Ó5ˆØ×Ñ˜UŸW™WÕ%ð 'ð �+×#Ñ#×)Ñ)Ñ)Ü  ¨WÑ!5Ó6‰àˆÜ# MÓ2€Kà$&€KÜ˜X—ˆØˆÛ'ˆLÜ—*‘*˜Y |°[Ð'@ÑAÌÏÉÔTˆCØ�x‰x˜!Š|¤ C§I¡I£°Ò 2Ü ð à#0°Ñ#>ÀÇ	Á	È!ÁÐ"MñNóð ð
 —[‘[  QÓ'ˆFÜ˜v u¨g°jÑ.AÓBˆFÜ ¨°Ñ(?Ó@ˆFØ�N‰N˜6Ÿ>™>¨"Ó-Õ.ð (ô —‘˜ qÔ)ˆØ�;‰;�q‰>˜]¨;Ñ7×=Ñ=¸aÑ@Ò@ÜØEØ ¨kÑ :× @Ñ @ÀÑ CÀUÇ[Á[ÐQRÁ^ÐTñUóð ð 	×Ñ˜5Ö!ð) 'ô, ØØØØØôð r'   c                ó2  — | €g t        |«      fS t        | t        «      rt        d| ›d�«      ‚t        | t        t
        t        j                  t        j                  f«      sÉt        | «      }|st        d«      ‚|D �cg c]  }|j                  «       ‘Œ }}g }g }|D ]1  }	 |j                  |j                  |j                  «       «      «       Œ3 t        |«      t        |«      k(  rt        d«      ‚|r2t        dt        |«      t        |«      dj!                  |«      fz  «       ||fS t        j"                  | «      j%                  d«      D �cg c]  }t	        |«      ‘Œ }	}|s|	g fS |	D �
cg c]  }
d|
cxk  rt        |«      k  rŒn |
‘Œ }}
|rt        d	|t        |«      fz  «      ‚|	|	D �
cg c]
  }
||
dz
     ‘Œ c}
fS c c}w # t        $ r4 |j                  t        j                  «       |j                  |«       Y �Œhw xY wc c}w c c}
w c c}
w )
zùReturn ``(column indices, output labels)``; ``NaN`` marks an absent channel.

    Port of ``loadSubject>resolveChannelSelection``: a requested channel the
    dataset lacks becomes a zero column, and the output order is the requested
    order.
    zchannelSelection z~ is a set name; pass an explicit list of labels or indices (the MATLAB package resolves set names through resolveChannelSet.m)z2label-based channelSelection needs chanlocs labelsz:none of the requested channels are present in this datasetzLwarning: %d of %d requested channels are missing and become zero columns: %srH   r+   r   zCchannelSelection indices %s are outside 1..%d (indices are 1-based))r%   r4   r3   rA   r   r   r-   ÚintegerÚfloatingr;   Úlowerr6   ÚindexÚnanÚlenÚprintrN   r.   r2   )Ú	selectionr[   Ú	requestedÚlabelÚloweredr{   ÚmissingrT   r9   Úcolumn_indicesrW   Úout_of_ranges               r(   re   re   ó   s  € ð ÐØ”4˜“<ÐÐä�)œSÔ!Ýò '0ð2ó
ð 	
ô �i¤#¤u¬b¯j©j¼"¿+¹+Ð!FÔGÜ˜YÓ'ˆ	ÙÜÐQÓRÐRÙ.4Ó5©f U�5—;‘;•=¨fˆÐ5Ø!ˆØˆÛˆDð%Ø—‘˜wŸ}™}¨T¯Z©Z«\Ó:Õ;ð ô ˆw‹<œ3˜y›>Ò)ÜÐYÓZÐZÙÜðÜ!$ W£¬s°9«~¸t¿y¹yÈÓ?QÐ RñSôð ˜	Ð!Ð!ô
 -/¯J©J°yÓ,A×,IÑ,IÈ"Ô,MÓNÑ,M D”c˜$•iÐ,M€NÐNÙØ˜rÐ!Ð!Ù-ÓK™~˜!°Q¸!Ô5J¼sÀ6»{Õ5J’A˜~€LÐKÙÜØQØœS ›[Ð)ñ*ó
ð 	
ð ±>ÓB±>¨a˜F 1 q¡5›M°>ÑBÐBÐBùò= 6øô ò %Ø—‘œrŸv™vÔ&Ø—‘˜t×$ð%üò Oùò Lùò Cs0   Á?GÂ".G
ÅH
Å0HÆ
HÆ2HÇ
9HÈHc                ó^  — |s| S t        j                  t        j                  |t        ¬«      «       }t        j                  | j
                  d   t        |«      f| j                  ¬«      }t        ||«      D ��cg c]  \  }}|sŒ	t        |«      ‘Œ }}}| d d …|f   |d d …|f<   |S c c}}w )Nr]   r   )
r-   Úisnanr.   r   Úzerosrf   r’   r0   Úzipr   )rƒ   r{   Úpresentr:   rW   ÚkeepÚcolumnss          r(   ri   ri   +  s“   € ÙØˆÜ�x‰xœŸ
™
 7´%Ô8Ó9Ð9€GÜ
�(‰(�E—K‘K ‘N¤C¨£LÐ1¸¿¹Ô
E€CÜ%(¨°'Ô%:ÔCÑ%:™'˜!˜TºdŒs�1�vÐ%:€GÑCØšA˜w˜JÑ'€CŠˆ7ˆ
�OØ€Jùó Ds   Á<
B)ÂB)c                óš   — | st        j                  |t        ¬«      S t        j                  t        j                  | t
        ¬«      «       S )Nr]   )r-   ÚonesÚboolrœ   r.   r   )r{   Ún_colss     r(   Ú_present_maskr¦   5  s3   € ÙÜ�w‰w�v¤TÔ*Ð*Ü�H‰H”R—Z‘Z ¬uÔ5Ó6Ð6Ð6r'   c                ó  — |dv r| S |dv rFt        || j                  d   «      }| d d …|f   | d d …|f   j                  dd¬«      z
  | d d …|f<   | S |dk(  rt        d«      ‚t	        |t
        «      rDt        |«      D ��cg c]  \  }}||k(  sŒ|‘Œ }}}|st        d|›d	�«      ‚| | d d …|d
   gf   z
  S t        |«      }d|cxk  r| j                  d   k  sn t        d|| j                  d   fz  «      ‚| | d d …|dz
  gf   z
  S c c}}w )N©Nr=   )ÚCARÚavgr   T©r`   ÚkeepdimsÚmastoidsz)mastoid re-referencing is not implementedzreReference z2 is not a known option or a selected channel labelr   z;reReference index %d is outside 1..%d (indices are 1-based))r¦   rf   ÚmeanrA   r4   r3   rJ   r   )	rƒ   Ú	referencer{   r[   rŸ   rW   r–   rU   r‰   s	            r(   rj   rj   >  sG  € ð �NÑ"ØˆØ�NÑ"Ü ¨¯©°Q©Ó8ˆØ!¢! W *Ñ-°²a¸°jÑ0A×0FÑ0FÈAÐX\Ð0FÓ0]Ñ]ˆŠa�ˆjÑØˆØ�JÒÜÐDÓEÐEÜ�)œSÔ!Ü#,¨VÔ#4ÔKÑ#4‘x�q˜%¸ÀÓ8J’Ð#4ˆÑKÙÝâðóð ð �ušQ  q¡ 
˜]Ñ+Ñ+Ð+ô �‹^€FØ�Ô(˜%Ÿ+™+ a™.Ô(ÜØIØ�u—{‘{ 1‘~Ð&ñ'ó
ð 	
ð �5š˜V a™Z˜L˜Ñ)Ñ)Ð)ùó Ls   Á?DÂDc                ó0  — |�t        |«      |k(  r| S t        |«      }||z  }|t        |«      k(  r| ddt        |«      …   S ddlm} ddlm} t        d||fz  «        |||z  «      j                  d«      } || |j                  |j                  d¬«      S )zØPort of ``loadSubject>resampleSignal``.

    An integer ratio decimates without an anti-alias filter (MATLAB
    ``downsample``); anything else goes through ``resample_poly`` (MATLAB
    ``resample``) and warns.
    Nr   )ÚFraction)Úresample_polyzÓwarning: resampling %g -> %g Hz is not an integer ratio; this trial cannot be reproduced bit-for-bit by runPredictions.m (MATLAB resample uses its own filter design). The shipped preprocessing uses 128 -> 32 Hz.iè  r_   )
r   r   Ú	fractionsr±   Úscipy.signalr²   r“   Úlimit_denominatorÚ	numeratorÚdenominator)rƒ   rx   Ú	target_fsÚratior±   r²   Ústeps          r(   rk   rk   \  s¨   € ð ÐœE )Ó,°Ò5ØˆÜ�iÓ €IØ�IÑ€EØ”�E“
ÒØ‘]œ˜E›
�]Ñ#Ð#Ý"å*ä	ð	Tð �)Ð
ñ	ôñ �I Ñ%Ó&×8Ñ8¸Ó>€DÙ˜ §¡°×0@Ñ0@ÀqÔIÐIr'   c                ór  — |dv r| S |dk(  r5| j                  ddd¬«      }d||dk(  <   | | j                  dd¬«      z
  |z  S |d	k(  r| | j                  dd¬«      z
  S |d
k(  rI| t        j                  j	                  | d«      z  | j
                  d   z  }||j                  dd¬«      z
  S t        d|›d�«      ‚)zIPort of ``loadSubject>applyNormalization`` (zscore uses the N-1 divisor).r¨   Úzscorer   r   T)r`   Úddofr¬   g      ð?r«   ÚcenterÚfroNormÚfrozunknown normalisation z! (use none/zscore/center/froNorm))Ústdr®   r-   ÚlinalgÚnormrf   rA   )rƒ   r1   ÚsigmaÚscaleds       r(   rl   rl   w  sË   € àˆ~ÑØˆØˆxÒØ—	‘	˜q q°4�	Ó8ˆØˆˆe�q‰jÑØ˜Ÿ
™
¨°D˜
Ó9Ñ9¸UÑBÐBØˆxÒØ�u—z‘z q°4�zÓ8Ñ8Ð8ØˆyÒØœŸ™Ÿ™¨¨uÓ5Ñ5¸¿¹ÀA¹ÑFˆØ˜Ÿ™¨°T˜Ó:Ñ:Ð:Ý
ÒTXÐZÓ
[Ð[r'   )r7   r   Úreturnr   )rC   zOptional[Dict[str, Any]]rÆ   zDict[str, Any])rP   r   rÆ   z	List[int])rq   r3   r   r   rC   zOptional[Dict]rr   r3   rs   r3   rÆ   r   )r”   r   r[   úSequence[str]rÆ   z!Tuple[Sequence[float], List[str]])rƒ   ú
np.ndarrayr{   úSequence[float]rÆ   rÈ   )r{   rÉ   r¥   r   rÆ   rÈ   )
rƒ   rÈ   r¯   r   r{   rÉ   r[   rÇ   rÆ   rÈ   )rƒ   rÈ   rx   r   r¸   zOptional[float]rÆ   rÈ   )rƒ   rÈ   r1   zOptional[str]rÆ   rÈ   )#r#   Ú
__future__r   ra   rK   Údataclassesr   r   Útypingr   r   r   r	   r
   r   Únumpyr-   Úscipy.ioÚiorc   Úconfigr   r   r@   r   r;   rF   rX   r‹   re   ri   r¦   rj   rk   rl   r&   r'   r(   Ú<module>rÑ      s  ðñ	õ #ã 	Û 	ß (ß =× =ã Ý ç .ð€ð ÷9ð 9ó ð9ó0ó(ó0$ð6 %)Ø&Ø"ðnØðnàðnð "ðnð ð	nð
 ðnð ónðh5CØð5CØ)ð5Cà&ó5Cópó7ð*Øð*Ø"%ð*Ø0?ð*ØIVð*àó*ó<Jô6\r'   