o
    yi:                     @   sL   d dl mZmZmZmZ d dlZd dlmZ d dlmZ G dd deZ	dS )    )AnyDictOptionalUnionN)Tensor)Metricc                       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 fddZedeeeef defddZ  ZS )MinMaxMetrica`  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 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   P/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/wrappers/minmax.pyr   ;   s   
zMinMaxMetric.__init__argsc                 O   s   | j j|i | dS )zUpdates the underlying metric.N)r   update)r   r   r   r   r   r   r   I   s   zMinMaxMetric.updatec                 C   s~   | j  }| |std| | j|j|k r|n| j|j| _| j|j|kr/|n| j|j| _|| j| jdS )zComputes 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.
        z\Returned value from base metric should be a scalar (int, float or tensor of size 1, but got )rawmaxmin)r   compute_is_suitable_valRuntimeErrorr   todevicer
   )r   valr   r   r   r"   M   s   

&&zMinMaxMetric.computec                    s   t    | j  dS )zYSets ``max_val`` and ``min_val`` to the initialization bounds and resets the base metric.N)r   resetr   )r   r   r   r   r(   \   s   
zMinMaxMetric.resetr'   c                 C   s,   t | ttfr	dS t | tr|  dkS dS )z3Utility function that checks whether min/max value.T   F)r   intr   r   numel)r'   r   r   r   r#   a   s
   
zMinMaxMetric._is_suitable_val)r   N)__name__
__module____qualname____doc__r	   r   bool__annotations__r   r   r   r   r   r   strr"   r(   staticmethodr   r*   r   r#   __classcell__r   r   r   r   r      s"   
 &r   )
typingr   r   r   r   r   r   torchmetrics.metricr   r   r   r   r   r   <module>   s
   