o
    si                     @   sL   d dl Z d dlmZ d dlZdd ZdddZd	d
 ZdddZdd Z	dS )    N)MutableMappingc                 C   s8   t | }|j|}|du rdS |jt jjt jjfv S )a%  Checks if a callable accepts a given keyword argument.

    Args:
        fn (callable): Callable to inspect.
        name (str): Check if ``fn`` can be called with ``name`` as a keyword
            argument.

    Returns:
        bool: whether ``fn`` accepts a ``name`` keyword argument.
    NF)inspect	signature
parametersgetkind	ParameterPOSITIONAL_OR_KEYWORDKEYWORD_ONLY)fnnamer   	parameter r   P/home/ubuntu/.local/lib/python3.10/site-packages/asteroid/utils/generic_utils.pyhas_arg   s   
r    _c                 C   sd   g }|   D ]'\}}|r|| | n|}t|tr&|t|||d   q|||f qt|S )a  Flattens a dictionary into a single-level dictionary while preserving
    parent keys. Taken from
    `SO <https://stackoverflow.com/questions/6027558/flatten-nested-dictionaries-compressing-keys>`_

    Args:
        d (MutableMapping): Dictionary to be flattened.
        parent_key (str): String to use as a prefix to all subsequent keys.
        sep (str): String to use as a separator between two key levels.

    Returns:
        dict: Single-level dictionary, flattened.
    )sep)items
isinstancer   extendflatten_dictappenddict)d
parent_keyr   r   kvnew_keyr   r   r   r      s   
r   c                 C   s:   t | } |  D ]\}}t|tjrt| | |< q| S )zTake average of numpy arrays in a dictionary.

    Args:
        dic (dict): Input dictionary to take average from

    Returns:
        dict: New dictionary with array averaged.

    )r   r   r   npndarrayfloatmean)dicr   r   r   r   r   average_arrays_in_dic2   s   r$    }  c                 C   s>   |du rd| fS t jdtd| | }t| || }||fS )zGet indexes for a chunk of signal of a given length.

    Args:
        signal_len (int): length of the signal to trim.
        desired_len (int): the length of [start:stop]

    Returns:
        tuple: random start integer, stop integer.
    Nr      )r   randomrandintmaxmin)
signal_lendesired_len
rand_startstopr   r   r   get_wav_random_start_stopD   s
   
r/   c          	      C   sN   g }t | D ]\}}}}}|r|r|}nd}||| ||||f qt|S )aY  Get list of decoder arguments for upsampling (right) side of a symmetric u-net,
    given the arguments used to construct the encoder.

    Args:
        encoders (tuple of length `N` of tuples of (in_chan, out_chan, kernel_size, stride, padding)):
            List of arguments used to construct the encoders
        skip_connections (bool): Whether to include skip connections in the
            calculation of decoder input channels.

    Return:
        tuple of length `N` of tuples of (in_chan, out_chan, kernel_size, stride, padding):
            Arguments to be used to construct decoders
    r   )reversedr   tuple)	encodersskip_connectionsdecoder_argsenc_in_chanenc_out_chanenc_kernel_size
enc_strideenc_paddingskip_in_chanr   r   r   unet_decoder_argsU   s   r;   )r   r   )r%   )
r   collections.abcr   numpyr   r   r   r$   r/   r;   r   r   r   r   <module>   s    

