o
    ZA)eÞ/  ã                   @   s‚   d Z ddlZddlZddlZddlZejdd„ ƒZejdd„ ƒZ	dd„ Z
dd	d	ejejfd
ddfdd„Zdd„ ZG dd„ dƒZdS )z.Code for the dataset_generator for both tasks.é    Nc               	      sî   ˆ d ‰t ˆ ƒd ‰tjˆgˆ dd�}|g}‡ ‡fdd„tdˆd ƒD ƒ}t|Ž D ]5}tˆƒD ].}tj||dd�}t |t |¡d t |¡d  |d jd |d jd g¡}| |¡ q0q*tj‡‡fd	d„tˆƒD ƒdd�}t	|ƒ|fS )
a  Batch equalizer.
        Prepares the inputs for a model to be trained in
        match-mismatch task. It makes sure that match_env
        and mismatch_env are equally presented as a first
        envelope in match-mismatch task.

        Parameters
        ----------
        args : Sequence[tf.Tensor]
            List of tensors representing feature data

        Returns
        -------
        Tuple[Tuple[tf.Tensor], tf.Tensor]
            Tuple of the EEG/speech features serving as the input to the model and
            the labels for the match/mismatch task

        Notes
        -----
        This function will also double the batch size. E.g. if the batch size of
        the elements in each of the args was 32, the output features will have
        a batch size of 64.
        r   é   ©Úaxisc                    s   g | ]	}ˆ |d ˆ… ‘qS ©N© )Ú.0Úi)ÚargsÚnum_stimulir   úG/Users/qiuzelin/Desktop/project_20/ICASSP2024/util/dataset_generator.pyÚ
<listcomp>/   s    z&batch_equalizer_fn.<locals>.<listcomp>)Úshiftr   éþÿÿÿéÿÿÿÿc              	      s@   g | ]‰ t  t  ‡ fd d„tˆƒD ƒg¡t  ˆ¡d dg¡‘qS )c                    s   g | ]
}|ˆ kr
d nd‘qS )r   r   r   )r   Úii©r   r   r   r   D   s    z1batch_equalizer_fn.<locals>.<listcomp>.<listcomp>r   r   )ÚtfÚtileÚconstantÚrangeÚshape)r   )Úeegr
   r   r   r   C   s    4ÿ)
Úlenr   Úconcatr   ÚzipÚrollÚreshaper   ÚappendÚtuple)r	   Únew_eegÚall_featuresÚargs_to_zipÚstimuli_featuresr   Ústimulus_rolledÚlabelsr   )r	   r   r
   r   Úbatch_equalizer_fn   s$   >÷
ÿýr%   c                  G   sD   | d }| d }| d }| d | d  }||||g}t |ƒ||gfS )Nr   r   é   )r   )r	   r   Z
speech_attZspeech_unattZ
speech_mixZall_feacturesr   r   r   Úbatch_equalizer_fn_NSSEQ   s   r'   c                 C   s4   t | ƒ} t|ƒD ]}|  tj | d ¡¡ qt| ƒS )Nr   )Úlistr   r   r   ÚrandomÚshuffler   )r	   Únumber_mismatchÚ_r   r   r   Ú
shuffle_fnc   s   r-   é@   )r.   r   Fc	           
         s  t  d¡�z t jjj| t‡ fdd„t|ƒD ƒƒd�}	|s+|	j‡‡fdd„t jjd�}	n|	j‡‡fdd„t jjd�}	ˆd	urJ|	j‡fd
d„t jjd�}	|	j	dd„ ddt jjd�}	|d	urb|	j
|dd�}	|d	urw|	j|t jjd�}	W d	  ƒ |	S W d	  ƒ |	S 1 s‚w   Y  |	S )a¡  Creates a tf.data.Dataset.

    This will be used to create a dataset generator that will
    pass windowed data to a model in both tasks.

    Parameters
    ---------
    data_generator: DataGenerator
        A data generator.
    window_length: int
        Length of the decision window in samples.
    batch_equalizer_fn: Callable
        Function that will be applied on the data after batching (using
        the `map` method from tf.data.Dataset). In the match/mismatch task,
        this function creates the imposter segments and labels.
    hop_length: int
        Hop length between two consecutive decision windows.
    batch_size: Optional[int]
        If not None, specifies the batch size. In the match/mismatch task,
        this amount will be doubled by the default_batch_equalizer_fn
    data_types: Union[Sequence[tf.dtype], tf.dtype]
        The data types that the individual features of data_generator should
        be cast to. If you only specify a single datatype, it will be chosen
        for all EEG/speech features.

    Returns
    -------
    tf.data.Dataset
        A Dataset object that generates data to train/evaluate models
        efficiently
    z/CPU:0c                 3   s*   � | ]\}}t jd |fˆ | d�V  qd S )N)r   Údtype)r   Ú
TensorSpec)r   ÚindexÚx)Ú
data_typesr   r   Ú	<genexpr>    s
   € ÿ
ÿz$create_tf_dataset.<locals>.<genexpr>)Úoutput_signaturec                     s   ‡ ‡fdd„| D ƒS )Nc                    ó    g | ]}t jj|ˆˆ d d�‘qS ©r   r   ©r   ÚsignalÚframe©r   Úarg©Ú
hop_lengthÚwindow_lengthr   r   r   «   ó    ÿÿú7create_tf_dataset.<locals>.<lambda>.<locals>.<listcomp>r   ©r	   r=   r   r   Ú<lambda>«   s    þz#create_tf_dataset.<locals>.<lambda>)Únum_parallel_callsc                     s8   ‡ ‡fdd„| dd… D ƒ‡ ‡fdd„| dd … D ƒ S )Nc                    r6   r7   r8   r;   r=   r   r   r   ³   r@   rA   r   r   c                    s(   g | ]}t jj|ˆd  ˆ d  dd�‘qS )é}   r   r   r8   r;   r=   r   r   r   ¶   s   ( r   rB   r=   r   r   rC   ³   s
    
þýNc                     s
   t | ˆ ƒS r   )r-   rB   )r+   r   r   rC   Â   s   
 c                  W   s   t jj | ¡S r   )r   ÚdataÚDatasetÚfrom_tensor_slicesrB   r   r   r   rC   Í   s    é   r   )Úcycle_lengthÚblock_lengthrD   T)Údrop_remainder)r   ÚdevicerF   rG   Úfrom_generatorr   Ú	enumerateÚmapÚAUTOTUNEÚ
interleaveÚbatch)
Údata_generatorr?   r%   r>   Ú
batch_sizer3   Zfeature_dimsr+   ZspeechÚdatasetr   )r3   r>   r+   r?   r   Úcreate_tf_datasetp   sN   -þþûûþ
üÿ
¿Gø
ÁG¹GrW   c                 C   s@   g }t  t| ƒdd„ ¡}|D ]\}}|t|dd„ d�g7 }q|S )aX  Group recordings and corresponding stimuli.

    Parameters
    ----------
    files : Sequence[Union[str, pathlib.Path]]
        List of filepaths to preprocessed and split EEG and speech features

    Returns
    -------
    list
        Files grouped by the self.group_key_fn and subsequently sorted
        by the self.feature_sort_fn.
    c                 S   ó    d  tj | ¡ d¡d d… ¡S ©Nz_-_é   ©ÚjoinÚosÚpathÚbasenameÚsplit©r2   r   r   r   rC   ö   ó     z"group_recordings.<locals>.<lambda>c                 S   ó   | dkrdS | S ©Nr   Ú0r   ra   r   r   r   rC   ø   ó    ©Úkey©Ú	itertoolsÚgroupbyÚsorted)ÚfilesÚ	new_filesÚgroupedÚrecording_nameÚfeature_pathsr   r   r   Úgroup_recordingsç   ó
   rr   c                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚDataGeneratorz*Generate data for the Match/Mismatch task.c                 C   s   || _ |  |¡| _dS )a0  Initialize the DataGenerator.

        Parameters
        ----------
        files: Sequence[Union[str, pathlib.Path]]
            Files to load.
        window_length: int
            Length of the decision window.
        spacing: int
            Spacing between matched and mismatched samples
        N)r?   rr   rm   )Úselfrm   r?   r   r   r   Ú__init__   s   zDataGenerator.__init__c                 C   s@   g }t  t|ƒdd„ ¡}|D ]\}}|t|dd„ d�g7 }q|S )a€  Group recordings and corresponding stimuli.

        Parameters
        ----------
        files : Sequence[Union[str, pathlib.Path]]
            List of filepaths to preprocessed and split EEG and speech features

        Returns
        -------
        list
            Files grouped by the self.group_key_fn and subsequently sorted
            by the self.feature_sort_fn.
        c                 S   rX   rY   r[   ra   r   r   r   rC   #  rb   z0DataGenerator.group_recordings.<locals>.<lambda>c                 S   rc   rd   r   ra   r   r   r   rC   %  rf   rg   ri   )ru   rm   rn   ro   rp   rq   r   r   r   rr     rs   zDataGenerator.group_recordingsc                 C   s
   t | jƒS r   )r   rm   ©ru   r   r   r   Ú__len__(  s   
zDataGenerator.__len__c                 C   sf   g }| j | D ]}t |¡ tj¡}|jdkr|dd…df }||g7 }q|  |¡}tdd„ |D ƒƒS )aE  Get data for a certain recording.

        Parameters
        ----------
        recording_index: int
            Index of the recording in this dataset

        Returns
        -------
        Union[Tuple[tf.Tensor,...], Tuple[np.ndarray,...]]
            The features corresponding to the recording_index recording
        r   Nc                 s   s   � | ]}t  |¡V  qd S r   )r   r   )r   r2   r   r   r   r4   @  s   € z,DataGenerator.__getitem__.<locals>.<genexpr>)rm   ÚnpÚloadÚastypeÚfloat32ÚndimÚprepare_datar   )ru   Úrecording_indexrF   ÚfeatureÚfr   r   r   Ú__getitem__+  s   

zDataGenerator.__getitem__c                 c   s<   � t |  ¡ ƒD ]}|  |¡V  ||  ¡ d kr|  ¡  qdS )zÍLoad data for the next recording.

        Yields
        -------
        Union[Tuple[tf.Tensor,...], Tuple[np.ndarray,...]]
            The features corresponding to the recording_index recording
        r   N)r   rx   r‚   Úon_epoch_end)ru   Úidxr   r   r   Ú__call__C  s   €€üzDataGenerator.__call__c                 C   s   t j | j¡ dS )z$Change state at the end of an epoch.N)ry   r)   r*   rm   rw   r   r   r   rƒ   Q  s   zDataGenerator.on_epoch_endc                 C   s   |S r   r   )ru   rF   r   r   r   r~   U  s   zDataGenerator.prepare_dataN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rv   rr   rx   r‚   r…   rƒ   r~   r   r   r   r   rt   ý   s    rt   )r‰   rj   r]   Únumpyry   Ú
tensorflowr   Úfunctionr%   r'   r-   r|   rW   rr   rt   r   r   r   r   Ú<module>   s(    
H


÷w