o
    zi                     @   s  d dl Z d dlmZ d dlmZ d dlmZ d dlZd dl	Z	d dl
mZ d dl
mZ d dlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ e	jZ	 G dd dZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZ G d d! d!eZ!G d"d# d#eZ"G d$d% d%eZ#G d&d' d'eZ$G d(d) d)eZ%G d*d+ d+eZ&G d,d- d-eZ'G d.d/ d/eZ(G d0d1 d1eZ)G d2d3 d3eZ*G d4d5 d5eZ+G d6d7 d7eZ,G d8d9 d9eZ-G d:d; d;eZ.G d<d= d=e.Z/G d>d? d?eZ0G d@dA dAeZ1G dBdC dCeZ2G dDdE dEeZ3G dFdG dGeZ4G dHdI dIe2Z5G dJdK dKe1Z6G dLdM dMe Z7dS )N    N)contextmanager)	signature)List)flatten)	unflatten)RandomState   )ml)AudioSignal)util   )AudioLoaderc                   @   s   e Zd ZdZg ddfdededefddZd	efd
dZ	dd Z
ddedefddZed	edejfddZdefddZdd Z		ddedefddZ		ddedefddZdS ) BaseTransforma	  This is the base class for all transforms that are implemented
    in this library. Transforms have two main operations: ``transform``
    and ``instantiate``.

    ``instantiate`` sets the parameters randomly
    from distribution tuples for each parameter. For example, for the
    ``BackgroundNoise`` transform, the signal-to-noise ratio (``snr``)
    is chosen randomly by instantiate. By default, it chosen uniformly
    between 10.0 and 30.0 (the tuple is set to ``("uniform", 10.0, 30.0)``).

    ``transform`` applies the transform using the instantiated parameters.
    A simple example is as follows:

    >>> seed = 0
    >>> signal = ...
    >>> transform = transforms.NoiseFloor(db = ("uniform", -50.0, -30.0))
    >>> kwargs = transform.instantiate()
    >>> output = transform(signal.clone(), **kwargs)

    By breaking apart the instantiation of parameters from the actual audio
    processing of the transform, we can make things more reproducible, while
    also applying the transform on batches of data efficiently on GPU,
    rather than on individual audio samples.

    ..  note::
        We call ``signal.clone()`` for the input to the ``transform`` function
        because signals are modified in-place! If you don't clone the signal,
        you will lose the original data.

    Parameters
    ----------
    keys : list, optional
        Keys that the transform looks for when
        calling ``self.transform``, by default []. In general this is
        set automatically, and you won't need to manipulate this argument.
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0

    Examples
    --------

    >>> seed = 0
    >>>
    >>> audio_path = "tests/audio/spk/f10_script4_produced.wav"
    >>> signal = AudioSignal(audio_path, offset=10, duration=2)
    >>> transform = tfm.Compose(
    >>>     [
    >>>         tfm.RoomImpulseResponse(sources=["tests/audio/irs.csv"]),
    >>>         tfm.BackgroundNoise(sources=["tests/audio/noises.csv"]),
    >>>     ],
    >>> )
    >>>
    >>> kwargs = transform.instantiate(seed, signal)
    >>> output = transform(signal, **kwargs)

    N      ?keysnameprobc                    s^   t t| jj }ddg  fdd|D }|| dg | _|| _|d u r*| jj}|| _d S )Nsignalkwargsc                    s   g | ]}| vr|qS  r   ).0kignore_keysr   N/home/ubuntu/.local/lib/python3.10/site-packages/audiotools/data/transforms.py
<listcomp>X   s    z*BaseTransform.__init__.<locals>.<listcomp>mask)	listr   
_transform
parametersr   r   	__class____name__r   )selfr   r   r   tfm_keysr   r   r   __init__R   s   
zBaseTransform.__init__batchc                 C   s4   || j  }| jD ]}|| v sJ | dq|S )Nz not in batch)r   r   )r"   r%   	sub_batchr   r   r   r   _prepared   s   

zBaseTransform._preparec                 C   s   |S Nr   r"   r   r   r   r   r   l      zBaseTransform._transformstater   c                 C   s   i S r(   r   r"   r+   r   r   r   r   _instantiateo   r*   zBaseTransform._instantiater   c                    s"    fddt |  D }t|S )ab  Applies a mask to the batch.

        Parameters
        ----------
        batch : dict
            Batch whose values will be masked in the ``transform`` pass.
        mask : torch.Tensor
            Mask to apply to batch.

        Returns
        -------
        dict
            A dictionary that contains values only where ``mask = True``.
        c                    s   i | ]	\}}||  qS r   r   r   r   vr   r   r   
<dictcomp>   s    z,BaseTransform.apply_mask.<locals>.<dictcomp>)r   itemsr   )r%   r   masked_batchr   r0   r   
apply_maskr   s   zBaseTransform.apply_maskc                 K   sX   |  |}|d }t|r*| ||}dd | D }| j|| fi |||< |S )at  Apply the transform to the audio signal,
        with given keyword arguments.

        Parameters
        ----------
        signal : AudioSignal
            Signal that will be modified by the transforms in-place.
        kwargs: dict
            Keyword arguments to the specific transforms ``self._transform``
            function.

        Returns
        -------
        AudioSignal
            Transformed AudioSignal.

        Examples
        --------

        >>> for seed in range(10):
        >>>     kwargs = transform.instantiate(seed, signal)
        >>>     output = transform(signal.clone(), **kwargs)

        r   c                 S   s   i | ]\}}|d kr||qS r0   r   r.   r   r   r   r1      s    z+BaseTransform.transform.<locals>.<dictcomp>)r'   torchanyr4   r2   r   )r"   r   r   
tfm_kwargsr   r   r   r   	transform   s   

zBaseTransform.transformc                 O   s   | j |i |S r(   r8   )r"   argsr   r   r   r   __call__      zBaseTransform.__call__c           	      C   s   t |}dtt| jj v }i }|rd|i}| j|fi |}t| D ]}|| }t|t	t
jtfr<|||< q(t|||< q(| | jk}t||d< | j|i}|S )ae  Instantiates parameters for the transform.

        Parameters
        ----------
        state : RandomState, optional
            _description_, by default None
        signal : AudioSignal, optional
            _description_, by default None

        Returns
        -------
        dict
            Dictionary containing instantiated arguments for every keyword
            argument to ``self._transform``.

        Examples
        --------

        >>> for seed in range(10):
        >>>     kwargs = transform.instantiate(seed, signal)
        >>>     output = transform(signal.clone(), **kwargs)

        r   r   )r   random_statesetr   r-   r   r   r   
isinstancer
   r5   Tensordictttrandr   r   )	r"   r+   r   needs_signalr   paramsr   r/   r   r   r   r   instantiate   s   


zBaseTransform.instantiatestatesc                 C   s.   g }|D ]}| | || qt|}|S )a  Instantiates arguments for every item in a batch,
        given a list of states. Each state in the list
        corresponds to one item in the batch.

        Parameters
        ----------
        states : list, optional
            List of states, by default None
        signal : AudioSignal, optional
            AudioSignal to pass to the ``self.instantiate`` section
            if it is needed for this transform, by default None

        Returns
        -------
        dict
            Collated dictionary of arguments.

        Examples
        --------

        >>> batch_size = 4
        >>> signal = AudioSignal(audio_path, offset=10, duration=2)
        >>> signal_batch = AudioSignal.batch([signal.clone() for _ in range(batch_size)])
        >>>
        >>> states = [seed + idx for idx in list(range(batch_size))]
        >>> kwargs = transform.batch_instantiate(states, signal_batch)
        >>> batch_output = transform(signal_batch, **kwargs)
        )appendrF   r   collate)r"   rG   r   r   r+   r   r   r   batch_instantiate   s
   !
zBaseTransform.batch_instantiater(   )NN)r!   
__module____qualname____doc__r   strfloatr$   rA   r'   r   r   r
   r-   staticmethodr5   r@   r4   r8   r;   rF   rJ   r   r   r   r   r      s0    <#
;r   c                   @   s   e Zd ZdZdS )Identityz0This transform just returns the original signal.N)r!   rK   rL   rM   r   r   r   r   rQ     s    rQ   c                       s    e Zd ZdZ fddZ  ZS )SpectralTransforma.  Spectral transforms require STFT data to exist, since manipulations
    of the STFT require the spectrogram. This just calls ``stft`` before
    the transform is called, and calls ``istft`` after the transform is
    called so that the audio data is written to after the spectral
    manipulation.
    c                    s(   |   t j|fi | |  |S r(   )stftsuperr8   istftr"   r   r   r    r   r   r8     s   zSpectralTransform.transform)r!   rK   rL   rM   r8   __classcell__r   r   rW   r   rR     s    rR   c                       s|   e Zd ZdZddddededef fdd	Zed
efddZ	dd Z
ddedefddZdd Zdd Zdd Z  ZS )Composea  Compose applies transforms in sequence, one after the other. The
    transforms are passed in as positional arguments or as a list like so:

    >>> transform = tfm.Compose(
    >>>     [
    >>>         tfm.RoomImpulseResponse(sources=["tests/audio/irs.csv"]),
    >>>         tfm.BackgroundNoise(sources=["tests/audio/noises.csv"]),
    >>>     ],
    >>> )

    This will convolve the signal with a room impulse response, and then
    add background noise to the signal. Instantiate instantiates
    all the parameters for every transform in the transform list so the
    interface for using the Compose transform is the same as everything
    else:

    >>> kwargs = transform.instantiate()
    >>> output = transform(signal.clone(), **kwargs)

    Under the hood, the transform maps each transform to a unique name
    under the hood of the form ``{position}.{name}``, where ``position``
    is the index of the transform in the list. ``Compose`` can nest
    within other ``Compose`` transforms, like so:

    >>> preprocess = transforms.Compose(
    >>>     tfm.GlobalVolumeNorm(),
    >>>     tfm.CrossTalk(),
    >>>     name="preprocess",
    >>> )
    >>> augment = transforms.Compose(
    >>>     tfm.RoomImpulseResponse(),
    >>>     tfm.BackgroundNoise(),
    >>>     name="augment",
    >>> )
    >>> postprocess = transforms.Compose(
    >>>     tfm.VolumeChange(),
    >>>     tfm.RescaleAudio(),
    >>>     tfm.ShiftPhase(),
    >>>     name="postprocess",
    >>> )
    >>> transform = transforms.Compose(preprocess, augment, postprocess),

    This defines 3 composed transforms, and then composes them in sequence
    with one another.

    Parameters
    ----------
    *transforms : list
        List of transforms to apply
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    Nr   r   r   
transformsr   r   c                   sj   t |d tr|d }t|D ]\}}| d|j |_qdd |D }t j|||d || _|| _d S )Nr   .c                 S   s   g | ]}|j qS r   r   )r   tfmr   r   r   r   a  s    z$Compose.__init__.<locals>.<listcomp>)r   r   r   )r?   r   	enumerater   rT   r$   r[   transforms_to_apply)r"   r   r   r[   ir^   r   rW   r   r   r$   Z  s   
zCompose.__init__namesc                 g   s    | j }|| _ dV  || _ dS )ab  This can be used to skip transforms entirely when applying
        the sequence of transforms to a signal. For example, take
        the following transforms with the names ``preprocess, augment, postprocess``.

        >>> preprocess = transforms.Compose(
        >>>     tfm.GlobalVolumeNorm(),
        >>>     tfm.CrossTalk(),
        >>>     name="preprocess",
        >>> )
        >>> augment = transforms.Compose(
        >>>     tfm.RoomImpulseResponse(),
        >>>     tfm.BackgroundNoise(),
        >>>     name="augment",
        >>> )
        >>> postprocess = transforms.Compose(
        >>>     tfm.VolumeChange(),
        >>>     tfm.RescaleAudio(),
        >>>     tfm.ShiftPhase(),
        >>>     name="postprocess",
        >>> )
        >>> transform = transforms.Compose(preprocess, augment, postprocess)

        If we wanted to apply all 3 to a signal, we do:

        >>> kwargs = transform.instantiate()
        >>> output = transform(signal.clone(), **kwargs)

        But if we only wanted to apply the ``preprocess`` and ``postprocess``
        transforms to the signal, we do:

        >>> with transform_fn.filter("preprocess", "postprocess"):
        >>>     output = transform(signal.clone(), **kwargs)

        Parameters
        ----------
        *names : list
            List of transforms, identified by name, to apply to signal.
        N)r`   )r"   rb   old_transformsr   r   r   filterg  s
   (
zCompose.filterc                    s8   | j D ] t fdd| jD r |fi |}q|S )Nc                    s   g | ]}| j v qS r   r]   )r   xr9   r   r   r         z&Compose._transform.<locals>.<listcomp>)r[   r6   r`   rV   r   r9   r   r     s
   
zCompose._transformr+   r   c                 C   s(   i }| j D ]}||j||d q|S )N)r   )r[   updaterF   )r"   r+   r   r   r8   r   r   r   r-     s   
zCompose._instantiatec                 C   s
   | j | S r(   r[   )r"   idxr   r   r   __getitem__     
zCompose.__getitem__c                 C   s
   t | jS r(   )lenr[   )r"   r   r   r   __len__  rk   zCompose.__len__c                 c   s    | j D ]}|V  qd S r(   rh   )r"   r8   r   r   r   __iter__  s   
zCompose.__iter__r(   )r!   rK   rL   rM   r   rN   rO   r$   r   rd   r   r   r
   r-   rj   rm   rn   rX   r   r   rW   r   rY   !  s    "8,rY   c                	       sT   e Zd ZdZdddddedededef fd	d
Zddede	f fddZ
  ZS )Choosea  Choose logic is the same as :py:func:`audiotools.data.transforms.Compose`,
    but instead of applying all the transforms in sequence, it applies just a single transform,
    which is chosen for each item in the batch.

    Parameters
    ----------
    *transforms : list
        List of transforms to apply
    weights : list
        Probability of choosing any specific transform.
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0

    Examples
    --------

    >>> transforms.Choose(tfm.LowPass(), tfm.HighPass())
    Nr   )weightsr   r   r[   rp   r   r   c                   sL   t  j|||d |d u rt| j  fddt D }t|| _d S )NrZ   c                    s   g | ]}d   qS )r   r   r   __lenr   r   r     s    z#Choose.__init__.<locals>.<listcomp>)rT   r$   rl   r[   rangenparrayrp   )r"   rp   r   r   r[   rW   rs   r   r$     s
   
zChoose.__init__r+   r   c           	         s   t  ||}ttt| j}|j|| jd}g }t| jD ]$\}}||j	 d }|
 r9t||k||j	 d< |||j	 d  q||d< |S )N)pr   one_hot)rT   r-   r   ru   rl   r[   choicerp   r_   r   itemrB   rH   )	r"   r+   r   r   tfm_idxry   ra   tr   rW   r   r   r-     s   zChoose._instantiater(   )r!   rK   rL   rM   r   rN   rO   r$   r   r
   r-   rX   r   r   rW   r   ro     s     ro   c                       s6   e Zd ZdZ			d
dededef fdd	Z  ZS )RepeatzRepeatedly applies a given transform ``n_repeat`` times."

    Parameters
    ----------
    transform : BaseTransform
        Transform to repeat.
    n_repeat : int, optional
        Number of times to repeat transform, by default 1
    r   Nr   n_repeatr   r   c                    s2    fddt |D }t j|||d || _d S )Nc                    s   g | ]}t   qS r   )copyrq   r9   r   r   r     rf   z#Repeat.__init__.<locals>.<listcomp>rZ   )ru   rT   r$   r   )r"   r8   r   r   r   r[   rW   r9   r   r$     s   
zRepeat.__init__)r   Nr   )	r!   rK   rL   rM   intrN   rO   r$   rX   r   r   rW   r   r~     s    r~   c                	       s<   e Zd ZdZ				ddedededef fd	d
Z  Z	S )
RepeatUpToaZ  Repeatedly applies a given transform up to ``max_repeat`` times."

    Parameters
    ----------
    transform : BaseTransform
        Transform to repeat.
    max_repeat : int, optional
        Max number of times to repeat transform, by default 1
    weights : list
        Probability of choosing any specific number up to ``max_repeat``.
       Nr   
max_repeatrp   r   r   c                    sD   g }t d|D ]}|t||d qt j||||d || _d S )Nr   )r   )r   r   rp   )ru   rH   r~   rT   r$   r   )r"   r8   r   rp   r   r   r[   nrW   r   r   r$     s
   
zRepeatUpTo.__init__)r   NNr   )
r!   rK   rL   rM   r   r   rN   rO   r$   rX   r   r   rW   r   r     s    r   c                       L   e Zd ZdZ			ddededef fdd	Zd
efddZ	dd Z
  ZS )ClippingDistortiona.  Adds clipping distortion to signal. Corresponds
    to :py:func:`audiotools.core.effects.EffectMixin.clip_distortion`.

    Parameters
    ----------
    perc : tuple, optional
        Clipping percentile. Values are between 0.0 to 1.0.
        Typical values are 0.1 or below, by default ("uniform", 0.0, 0.1)
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    uniform        皙?Nr   percr   r   c                       t  j||d || _d S NrZ   )rT   r$   r   )r"   r   r   r   rW   r   r   r$   #     
zClippingDistortion.__init__r+   c                 C      dt | j|iS )Nr   )r   sample_from_distr   r"   r+   r   r   r   r-   -     zClippingDistortion._instantiatec                 C   
   | |S r(   )clip_distortion)r"   r   r   r   r   r   r   0  rk   zClippingDistortion._transform)r   Nr   r!   rK   rL   rM   tuplerN   rO   r$   r   r-   r   rX   r   r   rW   r   r         
r   c                	       sR   e Zd ZdZ				ddededed	ef fd
dZde	fddZ
dd Z  ZS )	Equalizeraa  Applies an equalization curve to the audio signal. Corresponds
    to :py:func:`audiotools.core.effects.EffectMixin.equalizer`.

    Parameters
    ----------
    eq_amount : tuple, optional
        The maximum dB cut to apply to the audio in any band,
        by default ("const", 1.0 dB)
    n_bands : int, optional
        Number of bands in EQ, by default 6
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    constr      Nr   	eq_amountn_bandsr   r   c                        t  j||d || _|| _d S r   )rT   r$   r   r   )r"   r   r   r   r   rW   r   r   r$   F     
zEqualizer.__init__r+   c                 C   s(   t | j|}| || j }d|iS )Neq)r   r   r   rC   r   )r"   r+   r   r   r   r   r   r-   R  s   zEqualizer._instantiatec                 C   r   r(   )	equalizer)r"   r   r   r   r   r   r   W  rk   zEqualizer._transform)r   r   Nr   r!   rK   rL   rM   r   r   rN   rO   r$   r   r-   r   rX   r   r   rW   r   r   4  s"    r   c                       T   e Zd ZdZdg dfddfdededef fd	d
ZdefddZ	dd Z
  ZS )Quantizationa.  Applies quantization to the input waveform. Corresponds
    to :py:func:`audiotools.core.effects.EffectMixin.quantization`.

    Parameters
    ----------
    channels : tuple, optional
        Number of evenly spaced quantization channels to quantize
        to, by default ("choice", [8, 32, 128, 256, 1024])
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    rz                i   Nr   channelsr   r   c                    r   r   rT   r$   r   r"   r   r   r   rW   r   r   r$   k  r   zQuantization.__init__r+   c                 C   r   Nr   r   r   r   r   r   r   r   r-   u  r   zQuantization._instantiatec                 C   r   r(   )quantizationr"   r   r   r   r   r   r   x  rk   zQuantization._transformr   r   r   rW   r   r   [      

r   c                       r   )MuLawQuantizationa;  Applies mu-law quantization to the input waveform. Corresponds
    to :py:func:`audiotools.core.effects.EffectMixin.mulaw_quantization`.

    Parameters
    ----------
    channels : tuple, optional
        Number of mu-law spaced quantization channels to quantize
        to, by default ("choice", [8, 32, 128, 256, 1024])
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    rz   r   Nr   r   r   r   c                    r   r   r   r   rW   r   r   r$     r   zMuLawQuantization.__init__r+   c                 C   r   r   r   r   r   r   r   r-     r   zMuLawQuantization._instantiatec                 C   r   r(   )mulaw_quantizationr   r   r   r   r     rk   zMuLawQuantization._transformr   r   r   rW   r   r   |  r   r   c                       P   e Zd ZdZ			ddededef fdd	Zd
ede	fddZ
dd Z  ZS )
NoiseFloora  Adds a noise floor of Gaussian noise to the signal at a specified
    dB.

    Parameters
    ----------
    db : tuple, optional
        Level of noise to add to signal, by default ("const", -50.0)
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    r   g      INr   dbr   r   c                    r   r   rT   r$   r   r"   r   r   r   rW   r   r   r$     r   zNoiseFloor.__init__r+   r   c                 C   s<   t | j|}||j|j}t||j}|| d|iS )N	nz_signal)	r   r   r   randnnum_channelssignal_lengthr
   sample_rate	normalize)r"   r+   r   r   
audio_datar   r   r   r   r-     s
   
zNoiseFloor._instantiatec                 C   s   || S r(   r   )r"   r   r   r   r   r   r     s   zNoiseFloor._transform)r   Nr   r!   rK   rL   rM   r   rN   rO   r$   r   r
   r-   r   rX   r   r   rW   r   r     s    
r   c                       sv   e Zd ZdZ								ddedee d	ee d
ededededef fddZ	de
defddZdd Z  ZS )BackgroundNoiseaC  Adds background noise from audio specified by a set of CSV files.
    A valid CSV file looks like, and is typically generated by
    :py:func:`audiotools.data.preprocess.create_csv`:

    ..  csv-table::
        :header: path

        room_tone/m6_script2_clean.wav
        room_tone/m6_script2_cleanraw.wav
        room_tone/m6_script2_ipad_balcony1.wav
        room_tone/m6_script2_ipad_bedroom1.wav
        room_tone/m6_script2_ipad_confroom1.wav
        room_tone/m6_script2_ipad_confroom2.wav
        room_tone/m6_script2_ipad_livingroom1.wav
        room_tone/m6_script2_ipad_office1.wav

    ..  note::
        All paths are relative to an environment variable called ``PATH_TO_DATA``,
        so that CSV files are portable across machines where data may be
        located in different places.

    This transform calls :py:func:`audiotools.core.effects.EffectMixin.mix`
    and :py:func:`audiotools.core.effects.EffectMixin.equalizer` under the
    hood.

    Parameters
    ----------
    snr : tuple, optional
        Signal-to-noise ratio, by default ("uniform", 10.0, 30.0)
    sources : List[str], optional
        Sources containing folders, or CSVs with paths to audio files,
        by default None
    weights : List[float], optional
        Weights to sample audio files from each source, by default None
    eq_amount : tuple, optional
        Amount of equalization to apply, by default ("const", 1.0)
    n_bands : int, optional
        Number of bands in equalizer, by default 3
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    loudness_cutoff : float, optional
        Loudness cutoff when loading from audio files, by default None
    r         $@      >@Nr      r   snrsourcesrp   r   r   r   r   loudness_cutoffc	           	         s8   t  j||d || _|| _|| _t||| _|| _d S r   )rT   r$   r   r   r   r   loaderr   )	r"   r   r   rp   r   r   r   r   r   rW   r   r   r$     s   
zBackgroundNoise.__init__r+   r   c                 C   sZ   t | j|}| || j }t | j|}| j||j|j| j	|j
dd }|||dS )Ndurationr   r   r   )r   	bg_signalr   )r   r   r   rC   r   r   r   r   signal_durationr   r   )r"   r+   r   r   r   r   r   r   r   r   r-     s   zBackgroundNoise._instantiatec                 C   s   | | ||S r(   )mixclone)r"   r   r   r   r   r   r   r   r     s   zBackgroundNoise._transform)r   NNr   r   Nr   N)r!   rK   rL   rM   r   r   rN   rO   r   r$   r   r
   r-   r   rX   r   r   rW   r   r     s:    1	r   c                       sj   e Zd ZdZ						ddedee dee d	ed
edef fddZde	de
fddZdd Z  ZS )	CrossTalka  Adds crosstalk between speakers, whose audio is drawn from a CSV file
    that was produced via :py:func:`audiotools.data.preprocess.create_csv`.

    This transform calls :py:func:`audiotools.core.effects.EffectMixin.mix`
    under the hood.

    Parameters
    ----------
    snr : tuple, optional
        How loud cross-talk speaker is relative to original signal in dB,
        by default ("uniform", 0.0, 10.0)
    sources : List[str], optional
        Sources containing folders, or CSVs with paths to audio files,
        by default None
    weights : List[float], optional
        Weights to sample audio files from each source, by default None
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    loudness_cutoff : float, optional
        Loudness cutoff when loading from audio files, by default -40
    r   r   r   Nr   r   r   rp   r   r   r   c                    s,   t  j||d || _t||| _|| _d S r   )rT   r$   r   r   r   r   )r"   r   r   rp   r   r   r   rW   r   r   r$   5  s   	
zCrossTalk.__init__r+   r   c                 C   s8   t | j|}| j||j|j| j|jdd }||dS )Nr   r   )crosstalk_signalr   )r   r   r   r   r   r   r   r   )r"   r+   r   r   r   r   r   r   r-   D  s   
zCrossTalk._instantiatec                 C   s&   |  }|| |}|| |S r(   )loudnessr   r   r   )r"   r   r   r   r   r   r   r   r   r   P  s   
zCrossTalk._transform)r   NNNr   r   )r!   rK   rL   rM   r   r   rN   rO   r$   r   r
   r-   r   rX   r   r   rW   r   r     s.    r   c                       s   e Zd ZdZ										dd	ed
ee dee dedededede	dedef fddZ
ddedefddZdd Z  ZS )RoomImpulseResponsea  Convolves signal with a room impulse response, at a specified
    direct-to-reverberant ratio, with equalization applied. Room impulse
    response data is drawn from a CSV file that was produced via
    :py:func:`audiotools.data.preprocess.create_csv`.

    This transform calls :py:func:`audiotools.core.effects.EffectMixin.apply_ir`
    under the hood.

    Parameters
    ----------
    drr : tuple, optional
        _description_, by default ("uniform", 0.0, 30.0)
    sources : List[str], optional
        Sources containing folders, or CSVs with paths to audio files,
        by default None
    weights : List[float], optional
        Weights to sample audio files from each source, by default None
    eq_amount : tuple, optional
        Amount of equalization to apply, by default ("const", 1.0)
    n_bands : int, optional
        Number of bands in equalizer, by default 6
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    use_original_phase : bool, optional
        Whether or not to use the original phase, by default False
    offset : float, optional
        Offset from each impulse response file to use, by default 0.0
    duration : float, optional
        Duration of each impulse response, by default 1.0
    r   r   r   Nr   r   r   Fr   drrr   rp   r   r   r   r   use_original_phaseoffsetr   c                    sD   t  j||d || _|| _|| _|| _t||| _|	| _|
| _	d S r   )
rT   r$   r   r   r   r   r   r   r   r   )r"   r   r   rp   r   r   r   r   r   r   r   rW   r   r   r$   |  s   
zRoomImpulseResponse.__init__r+   r   c                 C   sh   t | j|}| || j }t | j|}| j||j| j| j	d |j
dd }||j |||dS )N)r   r   r   r   r   )r   	ir_signalr   )r   r   r   rC   r   r   r   r   r   r   r   zero_pad_to)r"   r+   r   r   r   r   r   r   r   r   r-     s   z RoomImpulseResponse._instantiatec                 C   s   |j | ||| jdS )N)r   )apply_irr   r   )r"   r   r   r   r   r   r   r   r     s   zRoomImpulseResponse._transform)
r   NNr   r   Nr   Fr   r   r(   )r!   rK   rL   rM   r   r   rN   rO   r   boolr$   r   r
   r-   r   rX   r   r   rW   r   r   Y  sF    $	
r   c                       r   )VolumeChangea  Changes the volume of the input signal.

    Uses :py:func:`audiotools.core.effects.EffectMixin.volume_change`.

    Parameters
    ----------
    db : tuple, optional
        Change in volume in decibels, by default ("uniform", -12.0, 0.0)
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    r   g      (r   Nr   r   r   r   c                    r   r   r   r   rW   r   r   r$        
zVolumeChange.__init__r+   c                 C   r   Nr   r   r   r   r   r   r   r   r-     r   zVolumeChange._instantiatec                 C   r   r(   volume_changer"   r   r   r   r   r   r     rk   zVolumeChange._transform)r   Nr   r   r   r   rW   r   r     s    	r   c                       r   )
VolumeNorma  Normalizes the volume of the excerpt to a specified decibel.

    Uses :py:func:`audiotools.core.effects.EffectMixin.normalize`.

    Parameters
    ----------
    db : tuple, optional
        dB to normalize signal to, by default ("const", -24)
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    r   iNr   r   r   r   c                    r   r   r   r   rW   r   r   r$     r   zVolumeNorm.__init__r+   c                 C   r   r   r   r   r   r   r   r-     r   zVolumeNorm._instantiatec                 C   r   r(   )r   r   r   r   r   r     rk   zVolumeNorm._transformr   Nr   r   r   r   rW   r   r     r   r   c                       r   )GlobalVolumeNorma  Similar to :py:func:`audiotools.data.transforms.VolumeNorm`, this
    transform also normalizes the volume of a signal, but it uses
    the volume of the entire audio file the loaded excerpt comes from,
    rather than the volume of just the excerpt. The volume of the
    entire audio file is expected in ``signal.metadata["loudness"]``.
    If loading audio from a CSV generated by :py:func:`audiotools.data.preprocess.create_csv`
    with ``loudness = True``, like the following:

    ..  csv-table::
        :header: path,loudness

        daps/produced/f1_script1_produced.wav,-16.299999237060547
        daps/produced/f1_script2_produced.wav,-16.600000381469727
        daps/produced/f1_script3_produced.wav,-17.299999237060547
        daps/produced/f1_script4_produced.wav,-16.100000381469727
        daps/produced/f1_script5_produced.wav,-16.700000762939453
        daps/produced/f3_script1_produced.wav,-16.5

    The ``AudioLoader`` will automatically load the loudness column into
    the metadata of the signal.

    Uses :py:func:`audiotools.core.effects.EffectMixin.volume_change`.

    Parameters
    ----------
    db : tuple, optional
        dB to normalize signal to, by default ("const", -24)
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    r   Nr   r   r   r   c                    r   r   r   r   rW   r   r   r$     r   zGlobalVolumeNorm.__init__r+   r   c                 C   s`   d|j vrd}d|iS t|j d tdkrd}d|iS t| j|}|t|j d  }d|iS )Nr   r   z-infr   )metadatarO   r   r   r   )r"   r+   r   	db_changer   r   r   r   r-     s   
zGlobalVolumeNorm._instantiatec                 C   r   r(   r   r   r   r   r   r   &  rk   zGlobalVolumeNorm._transformr   r   r   r   rW   r   r     s    $
r   c                       s4   e Zd ZdZd
dedef fddZdd	 Z  ZS )SilenceaB  Zeros out the signal with some probability.

    Parameters
    ----------
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 0.1
    Nr   r   r   c                    s   t  j||d d S r   rT   r$   r"   r   r   rW   r   r   r$   6  s   zSilence.__init__c                 C   s*   |j }tt|j|j|jd}||_ |S )N)r   stft_params)	_loudnessr
   r5   
zeros_liker   r   r   )r"   r   r   r   r   r   r   9  s   
zSilence._transform)Nr   )	r!   rK   rL   rM   rN   rO   r$   r   rX   r   r   rW   r   r   *  s    r   c                	       Z   e Zd ZdZdg dfdddfdeded	ed
ef fddZde	fddZ
dd Z  ZS )LowPassaj  Applies a LowPass filter.

    Uses :py:func:`audiotools.core.dsp.DSPMixin.low_pass`.

    Parameters
    ----------
    cutoff : tuple, optional
        Cutoff frequency distribution,
        by default ``("choice", [4000, 8000, 16000])``
    zeros : int, optional
        Number of zero-crossings in filter, argument to
        ``julius.LowPassFilters``, by default 51
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    rz   )i  i@  i>  3   Nr   cutoffzerosr   r   c                    r   r   rT   r$   r   r   r"   r   r   r   r   rW   r   r   r$   [  r   zLowPass.__init__r+   c                 C   r   Nr   r   r   r   r   r   r   r   r-   g  r   zLowPass._instantiatec                 C      |j || jdS N)r   )low_passr   r"   r   r   r   r   r   r   j  r<   zLowPass._transformr   r   r   rW   r   r   G  "    
r   c                	       r   )HighPassar  Applies a HighPass filter.

    Uses :py:func:`audiotools.core.dsp.DSPMixin.high_pass`.

    Parameters
    ----------
    cutoff : tuple, optional
        Cutoff frequency distribution,
        by default ``("choice", [50, 100, 250, 500, 1000])``
    zeros : int, optional
        Number of zero-crossings in filter, argument to
        ``julius.LowPassFilters``, by default 51
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    rz   )2   d      i  i  r   Nr   r   r   r   r   c                    r   r   r   r   rW   r   r   r$     r   zHighPass.__init__r+   c                 C   r   r   r   r   r   r   r   r-     r   zHighPass._instantiatec                 C   r   r   )	high_passr   r   r   r   r   r     r<   zHighPass._transformr   r   r   rW   r   r  n  r  r  c                       s8   e Zd ZdZddededef fdd	Zd
d Z  ZS )RescaleAudioa\  Rescales the audio so it is in between ``-val`` and ``val``
    only if the original audio exceeds those bounds. Useful if
    transforms have caused the audio to clip.

    Uses :py:func:`audiotools.core.effects.EffectMixin.ensure_max_of_audio`.

    Parameters
    ----------
    val : float, optional
        Max absolute value of signal, by default 1.0
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    r   Nr   valr   r   c                    r   r   )rT   r$   r  )r"   r  r   r   rW   r   r   r$     s   
zRescaleAudio.__init__c                 C   s   | | jS r(   )ensure_max_of_audior  r)   r   r   r   r        zRescaleAudio._transform)r   Nr   )	r!   rK   rL   rM   rO   rN   r$   r   rX   r   r   rW   r   r    s    r  c                       sX   e Zd ZdZdej ejfddfdededef fdd	Z	d
e
fddZdd Z  ZS )
ShiftPhasea  Shifts the phase of the audio.

    Uses :py:func:`audiotools.core.dsp.DSPMixin.shift)phase`.

    Parameters
    ----------
    shift : tuple, optional
        How much to shift phase by, by default ("uniform", -np.pi, np.pi)
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    r   Nr   shiftr   r   c                    r   r   )rT   r$   r  )r"   r  r   r   rW   r   r   r$     r   zShiftPhase.__init__r+   c                 C   r   )Nr  )r   r   r  r   r   r   r   r-     r   zShiftPhase._instantiatec                 C   r   r(   shift_phase)r"   r   r  r   r   r   r     rk   zShiftPhase._transform)r!   rK   rL   rM   rv   pir   rN   rO   r$   r   r-   r   rX   r   r   rW   r   r    s    	r  c                       s,   e Zd ZdZddedef fddZ  ZS )	InvertPhaseau  Inverts the phase of the audio.

    Uses :py:func:`audiotools.core.dsp.DSPMixin.shift_phase`.

    Parameters
    ----------
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    Nr   r   r   c                    s   t  jdtjf||d d S )Nr   )r  r   r   )rT   r$   rv   r  r   rW   r   r   r$     s   zInvertPhase.__init__)Nr   )r!   rK   rL   rM   rN   rO   r$   rX   r   r   rW   r   r    s     r  c                       sZ   e Zd ZdZddejfddfdededef fd	d
Z	dde
defddZdd Z  ZS )CorruptPhasea  Corrupts the phase of the audio.

    Uses :py:func:`audiotools.core.dsp.DSPMixin.corrupt_phase`.

    Parameters
    ----------
    scale : tuple, optional
        How much to corrupt phase by, by default ("uniform", 0, np.pi)
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    r   r   Nr   scaler   r   c                    r   r   )rT   r$   r  )r"   r  r   r   rW   r   r   r$     s   
zCorruptPhase.__init__r+   r   c                 C   s6   t | j|}|j||jjdd  d}d|diS )Nr   )r  size
corruptionfloat32)r   r   r  normalphaseshapeastype)r"   r+   r   r  r  r   r   r   r-     s   zCorruptPhase._instantiatec                 C   s   |j |dS )N)r  r  )r"   r   r  r   r   r   r     r
  zCorruptPhase._transformr(   )r!   rK   rL   rM   rv   r  r   rN   rO   r$   r   r
   r-   r   rX   r   r   rW   r   r    s    r  c                	       `   e Zd ZdZ				ddededed	ef fd
dZdede	fddZ
dedefddZ  ZS )FrequencyMaskar  Masks a band of frequencies at a center frequency
    from the audio.

    Uses :py:func:`audiotools.core.dsp.DSPMixin.mask_frequencies`.

    Parameters
    ----------
    f_center : tuple, optional
        Center frequency between 0.0 and 1.0 (Nyquist), by default ("uniform", 0.0, 1.0)
    f_width : tuple, optional
        Width of zero'd out band, by default ("const", 0.1)
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    r   r   r   r   r   Nr   f_centerf_widthr   r   c                    r   r   )rT   r$   r  r  r"   r  r  r   r   rW   r   r   r$        
zFrequencyMask.__init__r+   r   c           	      C   sf   t | j|}t | j|}t||d  d}t||d  d}|jd | }|jd | }||dS )Nr   r   r   fmin_hzfmax_hz)r   r   r  r  maxminr   )	r"   r+   r   r  r  fminfmaxr#  r$  r   r   r   r-     s   
zFrequencyMask._instantiater#  r$  c                 C      |j ||dS )Nr"  )mask_frequencies)r"   r   r#  r$  r   r   r   r   +     zFrequencyMask._transformr  r  Nr   r   r   r   rW   r   r    s"    r  c                	       r  )TimeMaskao  Masks out contiguous time-steps from signal.

    Uses :py:func:`audiotools.core.dsp.DSPMixin.mask_timesteps`.

    Parameters
    ----------
    t_center : tuple, optional
        Center time in terms of 0.0 and 1.0 (duration of signal),
        by default ("uniform", 0.0, 1.0)
    t_width : tuple, optional
        Width of dropped out portion, by default ("const", 0.025)
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    r  r   g?Nr   t_centert_widthr   r   c                    r   r   )rT   r$   r/  r0  r"   r/  r0  r   r   rW   r   r   r$   B  r!  zTimeMask.__init__r+   r   c           	      C   s^   t | j|}t | j|}t||d  d}t||d  d}|j| }|j| }||dS )Nr   r   r   tmin_stmax_s)r   r   r/  r0  r%  r&  r   )	r"   r+   r   r/  r0  tmintmaxr3  r4  r   r   r   r-   M  s   


zTimeMask._instantiater3  r4  c                 C   r)  )Nr2  )mask_timesteps)r"   r   r3  r4  r   r   r   r   X  r+  zTimeMask._transformr  r.  Nr   r   r   r   rW   r   r-  /  s"    r-  c                       sX   e Zd ZdZ			ddededef fdd	Zdd
ede	fddZ
defddZ  ZS )MaskLowMagnitudesa  Masks low magnitude regions out of signal.

    Uses :py:func:`audiotools.core.dsp.DSPMixin.mask_low_magnitudes`.

    Parameters
    ----------
    db_cutoff : tuple, optional
        Decibel value for which things below it will be masked away,
        by default ("uniform", -10, 10)
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    r   i
   Nr   	db_cutoffr   r   c                    r   r   )rT   r$   r<  )r"   r<  r   r   rW   r   r   r$   m  r   zMaskLowMagnitudes.__init__r+   r   c                 C   r   )Nr<  )r   r   r<  r,   r   r   r   r-   v  r   zMaskLowMagnitudes._instantiatec                 C   r   r(   )mask_low_magnitudes)r"   r   r<  r   r   r   r   y  rk   zMaskLowMagnitudes._transform)r:  Nr   r(   r   r   r   rW   r   r9  \  s    	r9  c                	       s`   e Zd ZdZddg dfddfdeded	ed
ef fddZddede	fddZ
dd Z  ZS )	Smoothingan  Convolves the signal with a smoothing window.

    Uses :py:func:`audiotools.core.effects.EffectMixin.convolve`.

    Parameters
    ----------
    window_type : tuple, optional
        Type of window to use, by default ("const", "average")
    window_length : tuple, optional
        Length of smoothing window, by
        default ("choice", [8, 16, 32, 64, 128, 256, 512])
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    )r   averagerz   )r      r   @   r   r   i   Nr   window_typewindow_lengthr   r   c                    r   r   )rT   r$   rB  rC  )r"   rB  rC  r   r   rW   r   r   r$     r!  zSmoothing.__init__r+   r   c                 C   s<   t | j|}t | j|}|j||dd}dt||jiS )Ncpu)rB  rC  devicewindow)r   r   rB  rC  
get_windowr
   r   )r"   r+   r   rB  rC  rF  r   r   r   r-     s   zSmoothing._instantiatec                 C   s^   |j  jdddj}d||dk< ||}|j  jdddj}d||dk< |||  }|S )NT)dimkeepdimr   r   )r   absr%  valuesconvolve)r"   r   rF  sscaleoutoscaler   r   r   r     s   
zSmoothing._transformr(   r   r   r   rW   r   r>  }  s"    
r>  c                	       N   e Zd ZdZ				ddededed	ef fd
dZdedefddZ  Z	S )	TimeNoiseai  Similar to :py:func:`audiotools.data.transforms.TimeMask`, but
    replaces with noise instead of zeros.

    Parameters
    ----------
    t_center : tuple, optional
        Center time in terms of 0.0 and 1.0 (duration of signal),
        by default ("uniform", 0.0, 1.0)
    t_width : tuple, optional
        Width of dropped out portion, by default ("const", 0.025)
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    r  r.  Nr   r/  r0  r   r   c                       t  j||||d d S )N)r/  r0  r   r   r   r1  rW   r   r   r$        zTimeNoise.__init__r3  r4  c           	      C   sl   |j ||dd}|j|j}}t|t|}}|dk|dk }|| ||< || ||< ||_||_|S )Nr   )r3  r4  r  )r7  	magnituder  r5   
randn_like)	r"   r   r3  r4  magr  mag_rphase_rr   r   r   r   r     s   zTimeNoise._transformr8  
r!   rK   rL   rM   r   rN   rO   r$   r   rX   r   r   rW   r   rR    s     	rR  c                	       rQ  )FrequencyNoiseaV  Similar to :py:func:`audiotools.data.transforms.FrequencyMask`, but
    replaces with noise instead of zeros.

    Parameters
    ----------
    f_center : tuple, optional
        Center frequency between 0.0 and 1.0 (Nyquist), by default ("uniform", 0.0, 1.0)
    f_width : tuple, optional
        Width of zero'd out band, by default ("const", 0.1)
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    r  r  Nr   r  r  r   r   c                    rS  )N)r  r  r   r   r   r   rW   r   r   r$     rT  zFrequencyNoise.__init__r#  r$  c           	      C   sj   |j ||d}|j|j}}t|t|}}|dk|dk }|| ||< || ||< ||_||_|S )Nr"  r   )r*  rU  r  r5   rV  )	r"   r   r#  r$  rW  r  rX  rY  r   r   r   r   r     s   zFrequencyNoise._transformr,  rZ  r   r   rW   r   r[    s     	r[  c                       sn   e Zd ZdZ									dd
edededededededef fddZdd Z	de
f fddZ  ZS )SpectralDenoisinga  Applies denoising algorithm detailed in
    :py:func:`audiotools.ml.layers.spectral_gate.SpectralGate`,
    using a randomly generated noise signal for denoising.

    Parameters
    ----------
    eq_amount : tuple, optional
        Amount of eq to apply to noise signal, by default ("const", 1.0)
    denoise_amount : tuple, optional
        Amount to denoise by, by default ("uniform", 0.8, 1.0)
    nz_volume : float, optional
        Volume of noise to denoise with, by default -40
    n_bands : int, optional
        Number of bands in equalizer, by default 6
    n_freq : int, optional
        Number of frequency bins to smooth by, by default 3
    n_time : int, optional
        Number of time bins to smooth by, by default 5
    name : str, optional
        Name of this transform, used to identify it in the dictionary
        produced by ``self.instantiate``, by default None
    prob : float, optional
        Probability of applying this transform, by default 1.0
    r   r   g?r   r   r   r   r   Nr   r   denoise_amount	nz_volumer   n_freqn_timer   r   c	           	         s4   t  j||||d || _|| _tj||| _d S )N)r   r   r   r   )rT   r$   r_  r^  r	   layersSpectralGatespectral_gate)	r"   r   r^  r_  r   r`  ra  r   r   rW   r   r   r$     s   zSpectralDenoising.__init__c                 C   s4   | | j|}| j|j| _| |||}|S r(   )r   r_  r   rd  torE  )r"   r   nzr   r^  r   r   r   r   .  s   zSpectralDenoising._transformr+   c                    s6   t  |}t| j||d< t|dd|d< |S )Nr^  i"V  iD  rf  )rT   r-   r   r   r^  r
   r   )r"   r+   r   rW   r   r   r-   4  s   zSpectralDenoising._instantiate)r   r]  r   r   r   r   Nr   )r!   rK   rL   rM   r   rO   r   rN   r$   r   r   r-   rX   r   r   rW   r   r\    s:    	r\  )8r   
contextlibr   inspectr   typingr   numpyrv   r5   flatten_dictr   r   numpy.randomr    r	   corer
   r   datasetsr   tensorrB   r   rQ   rR   rY   ro   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r-  r9  r>  rR  r[  r\  r   r   r   r   <module>   sb     x 3!'!!&X>T !<'' .-!3*)