o
    Òôjv  ã                   @   s†   d dl mZ d dlmZmZm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mZ d dlmZ es9d	gZG d
d„ deƒZdS )é    )ÚSequence)ÚAnyÚOptionalÚUnionN)ÚTensor)ÚMetric)Ú_MATPLOTLIB_AVAILABLE)Ú_AX_TYPEÚ_PLOT_OUT_TYPE)ÚWrapperMetricúMinMaxMetric.plotc                       sð   e Zd ZU dZdZee ed< eed< eed< de	de
dd	f‡ fd
d„Zde
de
dd	fdd„Zdeeef fdd„Zde
de
de
f‡ fdd„Zd‡ fdd„Zedeeef defdd„ƒZ		ddeeeee f  dee defdd„Z‡  ZS )ÚMinMaxMetricak  Wrapper metric that tracks both the minimum and maximum of a scalar/tensor across an experiment.

    The min/max value will be updated each time ``.compute`` is called.

    Args:
        base_metric:
            The metric of which you want to keep track of its maximum and minimum values.
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Raises:
        ValueError
            If ``base_metric` argument is not a subclasses instance of ``torchmetrics.Metric``

    Example::
        >>> import torch
        >>> from torchmetrics.wrappers import MinMaxMetric
        >>> from torchmetrics.classification import BinaryAccuracy
        >>> from pprint import pprint
        >>> base_metric = BinaryAccuracy()
        >>> minmax_metric = MinMaxMetric(base_metric)
        >>> preds_1 = torch.Tensor([[0.1, 0.9], [0.2, 0.8]])
        >>> preds_2 = torch.Tensor([[0.9, 0.1], [0.2, 0.8]])
        >>> labels = torch.Tensor([[0, 1], [0, 1]]).long()
        >>> pprint(minmax_metric(preds_1, labels))
        {'max': tensor(1.), 'min': tensor(1.), 'raw': tensor(1.)}
        >>> pprint(minmax_metric.compute())
        {'max': tensor(1.), 'min': tensor(1.), 'raw': tensor(1.)}
        >>> minmax_metric.update(preds_2, labels)
        >>> pprint(minmax_metric.compute())
        {'max': tensor(1.), 'min': tensor(0.7500), 'raw': tensor(0.7500)}

    TÚfull_state_updateÚmin_valÚmax_valÚbase_metricÚkwargsÚreturnNc                    sT   t ƒ jdi |¤Ž t|tƒstd|› �ƒ‚|| _t tdƒ¡| _	t tdƒ¡| _
d S )NzMExpected base metric to be an instance of `torchmetrics.Metric` but received Úinfz-inf© )ÚsuperÚ__init__Ú
isinstancer   Ú
ValueErrorÚ_base_metricÚtorchÚtensorÚfloatr   r   )Úselfr   r   ©Ú	__class__r   úo/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/torchmetrics/wrappers/minmax.pyr   D   s   
ÿzMinMaxMetric.__init__Úargsc                 O   s   | j j|i |¤Ž dS )zUpdate the underlying metric.N)r   Úupdate©r   r"   r   r   r   r!   r#   R   ó   zMinMaxMetric.updatec                 C   s€   | j  ¡ }|  |¡std|› d�ƒ‚| j |j¡|k r|n| j |j¡| _| j |j¡|kr0|n| j |j¡| _|| j| jdœS )zêCompute the underlying metric as well as max and min values for this metric.

        Returns a dictionary that consists of the computed value (``raw``), as well as the minimum (``min``) and maximum
        (``max``) values.

        zLReturned value from base metric should be a float or scalar tensor, but got Ú.)ÚrawÚmaxÚmin)r   ÚcomputeÚ_is_suitable_valÚRuntimeErrorr   ÚtoÚdevicer   )r   Úvalr   r   r!   r*   V   s   

&&zMinMaxMetric.computec                    s   t t| ƒj|i |¤ŽS )z9Use the original forward method of the base metric class.)r   r   Úforwardr$   r   r   r!   r0   d   r%   zMinMaxMetric.forwardc                    s   t ƒ  ¡  | j ¡  dS )zXSet ``max_val`` and ``min_val`` to the initialization bounds and resets the base metric.N)r   Úresetr   )r   r   r   r!   r1   h   s   
zMinMaxMetric.resetr/   c                 C   s,   t | ttfƒr	dS t | tƒr|  ¡ dkS dS )z(Check whether min/max is a scalar value.Té   F)r   Úintr   r   Únumel)r/   r   r   r!   r+   m   s
   
zMinMaxMetric._is_suitable_valÚaxc                 C   s   |   ||¡S )aÉ  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.wrappers import MinMaxMetric
            >>> from torchmetrics.classification import BinaryAccuracy
            >>> metric = MinMaxMetric(BinaryAccuracy())
            >>> metric.update(torch.randint(2, (20,)), torch.randint(2, (20,)))
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> import torch
            >>> from torchmetrics.wrappers import MinMaxMetric
            >>> from torchmetrics.classification import BinaryAccuracy
            >>> metric = MinMaxMetric(BinaryAccuracy())
            >>> values = [ ]
            >>> for _ in range(3):
            ...     values.append(metric(torch.randint(2, (20,)), torch.randint(2, (20,))))
            >>> fig_, ax_ = metric.plot(values)

        )Ú_plot)r   r/   r5   r   r   r!   Úplotv   s   *r   )r   N)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ÚboolÚ__annotations__r   r   r   r   r#   ÚdictÚstrr*   r0   r1   Ústaticmethodr   r   r+   r   r	   r
   r7   Ú__classcell__r   r   r   r!   r      s4   
 !þýü	ÿÿÿþr   )Úcollections.abcr   Útypingr   r   r   r   r   Útorchmetrics.metricr   Útorchmetrics.utilities.importsr   Útorchmetrics.utilities.plotr	   r
   Útorchmetrics.wrappers.abstractr   Ú__doctest_skip__r   r   r   r   r!   Ú<module>   s   