Ë
    Z5ëjT  ã                  óX  — d Z ddl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dlmZmZ e	e
j&                  e
j&                  eeef   Ze	e
j&                  e
j&                  ee   ee   ee   e
j&                  f   Z G d	„ d
ee   «      Z G d„ de«      Z G d„ de«      Zdd„Zy)ao  Per-trial datasets and batch collation used by the prediction pipeline.

Each dataset yields one trial as
``(neural, stim, subject_id, trial_index, fs)``.  The identifiers let the pipeline
map predictions back to the correct row; the hook receives the signals only.

Every trial is loaded through ``load_subject`` with the preprocessing the
decoder was fitted under.
é    )Úannotations)ÚAnyÚDictÚListÚOptionalÚSequenceÚTupleN)ÚDataseté   )ÚNEURAL_PREFIXÚSTIM_PREFIX)ÚSubjectDataÚload_subjectc                  óF   — e Zd ZdZdeef	 	 	 	 	 	 	 	 	 	 	 dd„Zdd„Zdd„Zy)	Ú_ChallengeDatasetzDLoad every trial of the requested subjects once, in recording order.Nc                óÀ   — g | _         |D ]R  }t        |||||¬«      }t        t        |j                  «      «      D ]  }| j                   j                  ||f«       Œ! ŒT y )N)Úneural_prefixÚstim_prefix)Úrecordsr   ÚrangeÚlenÚneuralÚappend)	ÚselfÚdata_dirÚsubject_idsÚpreprocessingr   r   Ú
subject_idÚsubjectÚtrial_indexs	            úš/root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset/test_phase/example_pipeline/TOPUBLISH-test-phase-inference-pipeline-example/challenge/datasets.pyÚ__init__z_ChallengeDataset.__init__   s_   € ð 79ˆŒÛ%ˆJÜ"ØØØØ+Ø'ôˆGô  %¤S¨¯©Ó%8Ö9�Ø—‘×#Ñ# W¨kÐ$:Õ;ñ  :ñ &ó    c                ó,   — t        | j                  «      S )N)r   r   )r   s    r!   Ú__len__z_ChallengeDataset.__len__2   s   € Ü�4—<‘<Ó Ð r#   c                ó$  — | j                   |   \  }}t        j                  |j                  |   t        j                  ¬«      }t        j                  |j
                  |   t        j                  ¬«      }|||j                  ||j                  fS )N©Údtype)r   ÚtorchÚ	as_tensorr   Úfloat64Ústimr   Úfs)r   Úindexr   r    r   r,   s         r!   Ú__getitem__z_ChallengeDataset.__getitem__5   sj   € Ø#Ÿ|™|¨EÑ2Ñˆ�Ü—‘ §¡°Ñ!<ÄEÇMÁMÔRˆÜ�‰˜wŸ|™|¨KÑ8ÄÇÁÔNˆØ�t˜W×/Ñ/°¸g¿j¹jÐHÐHr#   )r   Ústrr   zSequence[int]r   zOptional[Dict[str, Any]]r   r0   r   r0   ÚreturnÚNone)r1   Úint)r.   r3   r1   Ú	TrialItem)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r"   r%   r/   © r#   r!   r   r      sZ   „ ÙNð 37Ø*Ø&ð<àð<ð #ð<ð 0ð	<ð
 ð<ð ð<ð 
ó<ó(!ôIr#   r   c                  ó   — e Zd ZdZy)ÚNJUSUSTechDatasetzNJUSUSTech2026 test set.N©r5   r6   r7   r8   r9   r#   r!   r;   r;   <   s   „ Ú"r#   r;   c                  ó   — e Zd ZdZy)ÚDLAADDatasetzDLAAD test set.Nr<   r9   r#   r!   r>   r>   @   s   „ Úr#   r>   c                ó¾  — t        | Ž \  }}}}}|d   j                  d   }|d   j                  d   }t        j                  |D �cg c]  }|j                  d   ‘Œ c}t        j                  ¬«      }	t        |	j                  «       «      }
|d   j                  }t        j                  t        | «      ||
|¬«      }t        j                  t        | «      ||
|¬«      }t        |«      D ]  \  }}|||dd…d|j                  d   …f<   Œ  t        |«      D ]  \  }}|||dd…d|j                  d   …f<   Œ  ||t        |«      t        |«      t        |«      |	fS c c}w )a’  Stack a batch, padding trials to the longest one.

    Returns
    -------
    neural : torch.Tensor
        ``(batch, n_channels, max_samples)``.
    stim : torch.Tensor
        ``(batch, n_streams, max_samples)``.
    subject_ids : list of int
    trial_indices : list of int
    fs_list : list of float
    lengths : torch.Tensor
        ``(batch,)`` sample length of each trial before padding.
    r   r   r'   N)ÚzipÚshaper)   ÚtensorÚlongr3   Úmaxr(   Úzerosr   Ú	enumerateÚlist)Úbatchr   r,   r   Útrial_indicesÚfs_listÚchannelsÚstreamsÚitemÚlengthsÚmax_samplesr(   Úneural_paddedÚstim_paddedr.   s                  r!   Úcollate_trialsrR   D   sD  € ô 9<¸U¸Ñ5€FˆD�+˜}¨gØ�a‰y�‰˜qÑ!€HØ�1‰g�m‰m˜AÑ€GÜ�l‰l±fÓ=±f¨d˜DŸJ™J q›M°fÑ=ÄUÇZÁZÔP€GÜ�g—k‘k“mÓ$€Kà�1‰I�O‰O€EÜ—K‘K¤ E£
¨H°kÈÔO€MÜ—+‘+œc %›j¨'°;ÀeÔL€KÜ  Ö(‰ˆˆtØ37ˆ�ešQ  $§*¡*¨Q¡- Ð/Ò0ð )ä  –‰ˆˆtØ15ˆ�Eš1˜o §
¡
¨1¡˜oÐ-Ò.ð 'ð 	ØÜˆ[ÓÜˆ]ÓÜˆW‹Øðð ùò >s   ÁE)rH   zList[TrialItem]r1   ÚCollatedBatch)r8   Ú
__future__r   Útypingr   r   r   r   r   r	   r)   Útorch.utils.datar
   Úconfigr   r   Úio_cndr   r   ÚTensorr3   Úfloatr4   rS   r   r;   r>   rR   r9   r#   r!   Ú<module>r[      s£   ðñõ #ç =× =ã Ý $ç .ß -à�%—,‘, §¡¨c°3¸Ð=Ñ>€	ØØ	‡L�L�%—,‘,  S¡	¨4°©9°d¸5±kÀ5Ç<Á<ÐOñ€ô
I˜ 	Ñ*ô IôB#Ð)ô #ôÐ$ô ô$r#   