a
    d                     @   s   d dl mZmZmZmZ d dlmZmZ d dlm	Z	 d dl
mZ d dlmZmZ d dlmZmZ ddgiZestd	gZG d
d deZdS )    )AnyOptionalSequenceUnion)Tensortensor)$perceptual_evaluation_speech_quality)Metric)_MATPLOTLIB_AVAILABLE_PESQ_AVAILABLE)_AX_TYPE_PLOT_OUT_TYPE!PerceptualEvaluationSpeechQualityZpesq&PerceptualEvaluationSpeechQuality.plotc                       s   e Zd ZU dZeed< eed< dZeed< dZeed< dZ	eed< d	Z
eed
< dZeed< deeeedd fddZeeddddZedddZdeeee df ee edddZ  ZS )r   a	  Calculate `Perceptual Evaluation of Speech Quality`_ (PESQ).

    It's a recognized industry standard for audio quality that takes into considerations characteristics such as:
    audio sharpness, call volume, background noise, clipping, audio interference ect. PESQ returns a score between
    -0.5 and 4.5 with the higher scores indicating a better quality.

    This metric is a wrapper for the `pesq package`_. Note that input will be moved to ``cpu`` to perform the metric
    calculation.

    As input to ``forward`` and ``update`` the metric accepts the following input

    - ``preds`` (:class:`~torch.Tensor`): float tensor with shape ``(...,time)``
    - ``target`` (:class:`~torch.Tensor`): float tensor with shape ``(...,time)``

    As output of `forward` and `compute` the metric returns the following output

    - ``pesq`` (:class:`~torch.Tensor`): float tensor with shape ``(...,)`` of PESQ value per sample

    .. note:: using this metrics requires you to have ``pesq`` install. Either install as ``pip install
        torchmetrics[audio]`` or ``pip install pesq``. ``pesq`` will compile with your currently
        installed version of numpy, meaning that if you upgrade numpy at some point in the future you will
        most likely have to reinstall ``pesq``.

    Args:
        fs: sampling frequency, should be 16000 or 8000 (Hz)
        mode: ``'wb'`` (wide-band) or ``'nb'`` (narrow-band)
        keep_same_device: whether to move the pesq value to the device of preds
        n_processes: integer specifiying the number of processes to run in parallel for the metric calculation.
            Only applies to batches of data and if ``multiprocessing`` package is installed.
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Raises:
        ModuleNotFoundError:
            If ``pesq`` package is not installed
        ValueError:
            If ``fs`` is not either  ``8000`` or ``16000``
        ValueError:
            If ``mode`` is not either ``"wb"`` or ``"nb"``

    Example:
        >>> import torch
        >>> from torchmetrics.audio import PerceptualEvaluationSpeechQuality
        >>> g = torch.manual_seed(1)
        >>> preds = torch.randn(8000)
        >>> target = torch.randn(8000)
        >>> nb_pesq = PerceptualEvaluationSpeechQuality(8000, 'nb')
        >>> nb_pesq(preds, target)
        tensor(2.2076)
        >>> wb_pesq = PerceptualEvaluationSpeechQuality(16000, 'wb')
        >>> wb_pesq(preds, target)
        tensor(1.7359)

    sum_pesqtotalFfull_state_updateis_differentiableThigher_is_betterg      plot_lower_boundg      @plot_upper_bound   N)fsmoden_processeskwargsreturnc                    s   t  jf i | tstd|dvr4td| || _|dvrPtd| || _t|tsv|dkrvtd| || _	| j
dtd	d
d | j
dtdd
d d S )NzPerceptualEvaluationSpeechQuality metric requires that `pesq` is installed. Either install as `pip install torchmetrics[audio]` or `pip install pesq`.)i@  i>  z:Expected argument `fs` to either be 8000 or 16000 but got )wbnbz;Expected argument `mode` to either be 'wb' or 'nb' but got r   zCExpected argument `n_processes` to be an int larger than 0 but got r   g        sum)defaultZdist_reduce_fxr   )super__init__r   ModuleNotFoundError
ValueErrorr   r   
isinstanceintr   Z	add_stater   )selfr   r   r   r   	__class__ `/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/audio/pesq.pyr"   \   s     z*PerceptualEvaluationSpeechQuality.__init__)predstargetr   c                 C   sJ   t ||| j| jd| j| jj}|  j| 7  _|  j|	 7  _dS )z*Update state with predictions and targets.FN)
r   r   r   r   tor   Zdevicer   r   Znumel)r'   r,   r-   Z
pesq_batchr*   r*   r+   updatev   s    z(PerceptualEvaluationSpeechQuality.update)r   c                 C   s   | j | j S )zCompute metric.)r   r   )r'   r*   r*   r+   compute   s    z)PerceptualEvaluationSpeechQuality.compute)valaxr   c                 C   s   |  ||S )ab  Plot a single or multiple values from the metric.

        Args:
            val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
                If no value is provided, will automatically call `metric.compute` and plot that result.
            ax: An matplotlib axis object. If provided will add plot to that axis

        Returns:
            Figure and Axes object

        Raises:
            ModuleNotFoundError:
                If `matplotlib` is not installed

        .. plot::
            :scale: 75

            >>> # Example plotting a single value
            >>> import torch
            >>> from torchmetrics.audio import PerceptualEvaluationSpeechQuality
            >>> metric = PerceptualEvaluationSpeechQuality(8000, 'nb')
            >>> metric.update(torch.rand(8000), torch.rand(8000))
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> import torch
            >>> from torchmetrics.audio import PerceptualEvaluationSpeechQuality
            >>> metric = PerceptualEvaluationSpeechQuality(8000, 'nb')
            >>> values = [ ]
            >>> for _ in range(10):
            ...     values.append(metric(torch.rand(8000), torch.rand(8000)))
            >>> fig_, ax_ = metric.plot(values)

        )Z_plot)r'   r1   r2   r*   r*   r+   plot   s    &r   )r   )NN)__name__
__module____qualname____doc__r   __annotations__r   boolr   r   r   floatr   r&   strr   r"   r/   r0   r   r   r   r   r   r3   __classcell__r*   r*   r(   r+   r      s&   
6 	N)typingr   r   r   r   Ztorchr   r   Z"torchmetrics.functional.audio.pesqr   Ztorchmetrics.metricr	   Ztorchmetrics.utilities.importsr
   r   Ztorchmetrics.utilities.plotr   r   Z__doctest_requires__Z__doctest_skip__r   r*   r*   r*   r+   <module>   s   
