§
    ’©ºj‹  ã                   ó¤   — d dl mZ d dl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mZ d dlmZmZ dd	d
giZesdgZ G d„ de¦  «        ZdS )é    )ÚSequence)ÚAnyÚOptionalÚUnion)ÚTensorÚtensor)Ú'non_intrusive_speech_quality_assessment)ÚMetric)Ú_LIBROSA_AVAILABLEÚ_MATPLOTLIB_AVAILABLEÚ_REQUESTS_AVAILABLE)Ú_AX_TYPEÚ_PLOT_OUT_TYPEÚ#NonIntrusiveSpeechQualityAssessmentÚlibrosaÚrequestsz(NonIntrusiveSpeechQualityAssessment.plotc                   óè   ‡ — 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deddfˆ fd„Zdeddfd„Zdefd„Zddeeee         df         dee         defd„Zˆ xZS )r   uJ  `Non-Intrusive Speech Quality Assessment`_ (NISQA v2.0) [1], [2].

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

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

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

    - ``nisqa`` (:class:`~torch.Tensor`): float tensor reduced across the batch with shape ``(5,)`` corresponding to
      overall MOS, noisiness, discontinuity, coloration and loudness in that order

    .. hint::
        Using this metric requires you to have ``librosa`` and ``requests`` installed. Install as
        ``pip install librosa requests``.

    .. caution::
        The ``forward`` and ``compute`` methods in this class return values reduced across the batch. To obtain
        values for each sample, you may use the functional counterpart
        :func:`~torchmetrics.functional.audio.nisqa.non_intrusive_speech_quality_assessment`.

    Args:
        fs: sampling frequency of input

    Raises:
        ModuleNotFoundError:
            If ``librosa`` or ``requests`` are not installed

    Example:
        >>> import torch
        >>> from torchmetrics.audio import NonIntrusiveSpeechQualityAssessment
        >>> _ = torch.manual_seed(42)
        >>> preds = torch.randn(16000)
        >>> nisqa = NonIntrusiveSpeechQualityAssessment(16000)
        >>> nisqa(preds)
        tensor([1.0433, 1.9545, 2.6087, 1.3460, 1.7117])

    References:
        - [1] G. Mittag and S. MÃ¶ller, "Non-intrusive speech quality assessment for super-wideband speech communication
          networks", in Proc. ICASSP, 2019.
        - [2] G. Mittag, B. Naderi, A. Chehadi and S. MÃ¶ller, "NISQA: A deep CNN-self-attention model for
          multidimensional speech quality prediction with crowdsourced datasets", in Proc. INTERSPEECH, 2021.

    Ú	sum_nisqaÚtotalFÚfull_state_updateÚis_differentiableTÚhigher_is_betterç        Úplot_lower_boundg      @Úplot_upper_boundÚfsÚkwargsÚreturnNc                 ót  •—  t          ¦   «         j        d	i |¤Ž t          rt          st	          d¦  «        ‚t          |t          ¦  «        r|dk    rt          d|› �¦  «        ‚|| _        |  	                    dt          g d¢¦  «        d¬¦  «         |  	                    dt          d¦  «        d¬¦  «         d S )
NziNISQA metric requires that librosa and requests are installed. Install as `pip install librosa requests`.r   z9Argument `fs` expected to be a positive integer, but got r   )r   r   r   r   r   Úsum)ÚdefaultÚdist_reduce_fxr   © )ÚsuperÚ__init__r   r   ÚModuleNotFoundErrorÚ
isinstanceÚintÚ
ValueErrorr   Ú	add_stater   )Úselfr   r   Ú	__class__s      €úh/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/audio/nisqa.pyr%   z,NonIntrusiveSpeechQualityAssessment.__init__X   sÏ   ø€ Ø�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"Ý!ð 	Õ)<ð 	Ý%ð=ñô ð õ ˜"�cÑ"Ô"ð 	_ b¨A¢g gÝÐ]ÐY[Ð]Ð]Ñ^Ô^Ð^ØˆŒà�Š�{­FÐ3LÐ3LÐ3LÑ,MÔ,MÐ^cˆÑdÔdÐdØ�Š�w­¨q©	¬	À%ˆÑHÔHÐHÐHÐHó    Úpredsc                 ó  — t          || j        ¦  «                             | j        j        ¦  «        }|                     dd¦  «        }| xj        |                     d¬¦  «        z  c_        | xj        |j        d         z  c_        dS )zUpdate state with predictions.éÿÿÿÿé   r   )ÚdimN)	r	   r   Útor   ÚdeviceÚreshaper    r   Úshape)r+   r/   Únisqa_batchs      r-   Úupdatez*NonIntrusiveSpeechQualityAssessment.updatef   s�   € å=ØØŒGñ
ô 
÷ Š"ˆTŒ^Ô"Ñ
#Ô
#ð 	ð
 "×)Ò)¨"¨aÑ0Ô0ˆØˆŒ˜+Ÿ/š/¨a˜/Ñ0Ô0Ñ0ˆŒØˆ
Œ
�kÔ'¨Ô*Ñ*ˆ
Œ
ˆ
ˆ
r.   c                 ó    — | j         | j        z  S )zCompute metric.)r   r   )r+   s    r-   Úcomputez+NonIntrusiveSpeechQualityAssessment.computeq   s   € àŒ~ ¤
Ñ*Ð*r.   ÚvalÚaxc                 ó.   — |                       ||¦  «        S )aF  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: A 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 NonIntrusiveSpeechQualityAssessment
            >>> metric = NonIntrusiveSpeechQualityAssessment(16000)
            >>> metric.update(torch.randn(16000))
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> import torch
            >>> from torchmetrics.audio import NonIntrusiveSpeechQualityAssessment
            >>> metric = NonIntrusiveSpeechQualityAssessment(16000)
            >>> values = []
            >>> for _ in range(10):
            ...     values.append(metric(torch.randn(16000)))
            >>> fig_, ax_ = metric.plot(values)

        )Ú_plot)r+   r<   r=   s      r-   Úplotz(NonIntrusiveSpeechQualityAssessment.plotu   s   € ðL �zŠz˜#˜rÑ"Ô"Ð"r.   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r   Úboolr   r   r   Úfloatr   r(   r   r%   r9   r;   r   r   r   r   r   r@   Ú__classcell__)r,   s   @r-   r   r   #   sW  ø€ € € € € € ð*ð *ðX ÐÐÑØ€M€M�MØ#Ð�tÐ#Ð#Ñ#Ø#Ð�tÐ#Ð#Ñ#Ø!Ð�dÐ!Ð!Ñ!Ø!Ð�eÐ!Ð!Ñ!Ø!Ð�eÐ!Ð!Ñ!ðI˜3ð I¨#ð I°$ð Ið Ið Ið Ið Ið Ið	+˜Fð 	+ tð 	+ð 	+ð 	+ð 	+ð+˜ð +ð +ð +ð +ð&#ð &#˜˜f h¨vÔ&6¸Ð<Ô=ð &#È(ÐS[ÔJ\ð &#Ðhvð &#ð &#ð &#ð &#ð &#ð &#ð &#ð &#r.   N)Úcollections.abcr   Útypingr   r   r   Útorchr   r   Ú#torchmetrics.functional.audio.nisqar	   Útorchmetrics.metricr
   Útorchmetrics.utilities.importsr   r   r   Útorchmetrics.utilities.plotr   r   Ú__doctest_requires__Ú__doctest_skip__r   r#   r.   r-   ú<module>rR      s   ðð %Ð $Ð $Ð $Ð $Ð $Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'à  Ð  Ð  Ð  Ð  Ð  Ð  Ð  à WÐ WÐ WÐ WÐ WÐ WØ &Ð &Ð &Ð &Ð &Ð &ðð ð ð ð ð ð ð ð ð ð
 AÐ @Ð @Ð @Ð @Ð @Ð @Ð @à=À	È:Ð?VÐWÐ àð DØBÐCÐðx#ð x#ð x#ð x#ð x#¨&ñ x#ô x#ð x#ð x#ð x#r.   