o
    .wi[C                     @   s   d dl mZ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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sEd
gZG dd deZdS )    )IterableSequence)deepcopy)AnyOptionalUnion)Tensornn)MetricCollection)Metric)_MATPLOTLIB_AVAILABLE)_AX_TYPE_PLOT_OUT_TYPE)WrapperMetricMultitaskWrapper.plotc                	       s  e Zd ZU dZdZeed< 		d*deee	e
ef f dee dee ddf fd	d
Zd+dedeeeejf  fddZd+dedee fddZd+dedeej fddZdeeef deeef ddfddZdeeef deeef fddZdeeef fddZdeeef deeef deeef fddZd, fddZed ee d!edee fd"d#Zd*dee dee dd fd$d%Z	d*d&ee	eee f  d'eee   dee! fd(d)Z"  Z#S )-MultitaskWrapperah  Wrapper class for computing different metrics on different tasks in the context of multitask learning.

    In multitask learning the different tasks requires different metrics to be evaluated. This wrapper allows
    for easy evaluation in such cases by supporting multiple predictions and targets through a dictionary.
    Note that only metrics where the signature of `update` follows the standard `preds, target` is supported.

    Args:
        task_metrics:
            Dictionary associating each task to a Metric or a MetricCollection. The keys of the dictionary represent the
            names of the tasks, and the values represent the metrics to use for each task.
        prefix:
            A string to append in front of the metric keys. If not provided, will default to an empty string.
        postfix:
            A string to append after the keys of the output dict. If not provided, will default to an empty string.

    .. tip::
        The use prefix and postfix allows for easily creating task wrappers for training, validation and test.
        The arguments are only changing the output keys of the computed metrics and not the input keys. This means
        that a ``MultitaskWrapper`` initialized as ``MultitaskWrapper({"task": Metric()}, prefix="train_")`` will
        still expect the input to be a dictionary with the key "task", but the output will be a dictionary with the key
        "train_task".

    Raises:
        TypeError:
            If argument `task_metrics` is not an dictionary
        TypeError:
            If not all values in the `task_metrics` dictionary is instances of `Metric` or `MetricCollection`
        ValueError:
            If `prefix` is not a string
        ValueError:
            If `postfix` is not a string

    Example (with a single metric per class):
         >>> import torch
         >>> from torchmetrics.wrappers import MultitaskWrapper
         >>> from torchmetrics.regression import MeanSquaredError
         >>> from torchmetrics.classification import BinaryAccuracy
         >>>
         >>> classification_target = torch.tensor([0, 1, 0])
         >>> regression_target = torch.tensor([2.5, 5.0, 4.0])
         >>> targets = {"Classification": classification_target, "Regression": regression_target}
         >>>
         >>> classification_preds = torch.tensor([0, 0, 1])
         >>> regression_preds = torch.tensor([3.0, 5.0, 2.5])
         >>> preds = {"Classification": classification_preds, "Regression": regression_preds}
         >>>
         >>> metrics = MultitaskWrapper({
         ...     "Classification": BinaryAccuracy(),
         ...     "Regression": MeanSquaredError()
         ... })
         >>> metrics.update(preds, targets)
         >>> metrics.compute()
         {'Classification': tensor(0.3333), 'Regression': tensor(0.8333)}

    Example (with several metrics per task):
         >>> import torch
         >>> from torchmetrics import MetricCollection
         >>> from torchmetrics.wrappers import MultitaskWrapper
         >>> from torchmetrics.regression import MeanSquaredError, MeanAbsoluteError
         >>> from torchmetrics.classification import BinaryAccuracy, BinaryF1Score
         >>>
         >>> classification_target = torch.tensor([0, 1, 0])
         >>> regression_target = torch.tensor([2.5, 5.0, 4.0])
         >>> targets = {"Classification": classification_target, "Regression": regression_target}
         >>>
         >>> classification_preds = torch.tensor([0, 0, 1])
         >>> regression_preds = torch.tensor([3.0, 5.0, 2.5])
         >>> preds = {"Classification": classification_preds, "Regression": regression_preds}
         >>>
         >>> metrics = MultitaskWrapper({
         ...     "Classification": MetricCollection(BinaryAccuracy(), BinaryF1Score()),
         ...     "Regression": MetricCollection(MeanSquaredError(), MeanAbsoluteError())
         ... })
         >>> metrics.update(preds, targets)
         >>> metrics.compute()
         {'Classification': {'BinaryAccuracy': tensor(0.3333), 'BinaryF1Score': tensor(0.)},
          'Regression': {'MeanSquaredError': tensor(0.8333), 'MeanAbsoluteError': tensor(0.6667)}}

    Example (with a prefix and postfix):
        >>> import torch
        >>> from torchmetrics.wrappers import MultitaskWrapper
        >>> from torchmetrics.regression import MeanSquaredError
        >>> from torchmetrics.classification import BinaryAccuracy
        >>>
        >>> classification_target = torch.tensor([0, 1, 0])
        >>> regression_target = torch.tensor([2.5, 5.0, 4.0])
        >>> targets = {"Classification": classification_target, "Regression": regression_target}
        >>> classification_preds = torch.tensor([0, 0, 1])
        >>> regression_preds = torch.tensor([3.0, 5.0, 2.5])
        >>> preds = {"Classification": classification_preds, "Regression": regression_preds}
        >>>
        >>> metrics = MultitaskWrapper({
        ...     "Classification": BinaryAccuracy(),
        ...     "Regression": MeanSquaredError()
        ... }, prefix="train_")
        >>> metrics.update(preds, targets)
        >>> metrics.compute()
        {'train_Classification': tensor(0.3333), 'train_Regression': tensor(0.8333)}

    Fis_differentiableNtask_metricsprefixpostfixreturnc                    s   t    t|tstd| | D ]}t|ttfs'tdt| qt	
|| _|d ur>t|ts>td| |pAd| _|d urSt|tsStd| |pVd| _d S )NzDExpected argument `task_metrics` to be a dict. Found task_metrics = zYExpected each task's metric to be a Metric or a MetricCollection. Found a metric of type zCExpected argument `prefix` to either be `None` or a string but got  zDExpected argument `postfix` to either be `None` or a string but got )super__init__
isinstancedict	TypeErrorvaluesr   r
   typer	   
ModuleDictr   str
ValueError_prefix_postfix)selfr   r   r   metric	__class__ \/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/wrappers/multitask.pyr      s$   


zMultitaskWrapper.__init__Tflattenc                 c   sv    | j  D ]2\}}|r+t|tr+| D ]\}}| j | d| | j |fV  qq| j | | j |fV  qdS )zIterate over task and task metrics.

        Args:
            flatten: If True, will iterate over all sub-metrics in the case of a MetricCollection.
                If False, will iterate over the task names and the corresponding metrics.

        _Nr   itemsr   r
   r"   r#   )r$   r*   	task_namer%   sub_metric_name
sub_metricr(   r(   r)   r-      s   "zMultitaskWrapper.itemsc                 c   sf    | j  D ]*\}}|r%t|tr%|D ]}| j | d| | j V  qq| j | | j V  qdS )zIterate over task names.

        Args:
            flatten: If True, will iterate over all sub-metrics in the case of a MetricCollection.
                If False, will iterate over the task names and the corresponding metrics.

        r+   Nr,   )r$   r*   r.   r%   r/   r(   r(   r)   keys   s   zMultitaskWrapper.keysc                 c   s:    | j  D ]}|rt|tr| E dH  q|V  qdS )zIterate over task metrics.

        Args:
            flatten: If True, will iterate over all sub-metrics in the case of a MetricCollection.
                If False, will iterate over the task names and the corresponding metrics.

        N)r   r   r   r
   )r$   r*   r%   r(   r(   r)   r      s   zMultitaskWrapper.values
task_predstask_targetsc                 C   s   | j  |   kr| ks&n td|  d|  d| j   | j  D ]\}}|| }|| }||| q+dS )zUpdate each task's metric with its corresponding pred and target.

        Args:
            task_preds: Dictionary associating each task to a Tensor of pred.
            task_targets: Dictionary associating each task to a Tensor of target.

        zExpected arguments `task_preds` and `task_targets` to have the same keys as the wrapped `task_metrics`. Found task_preds.keys() = z, task_targets.keys() = z  and self.task_metrics.keys() = N)r   r1   r!   r-   update)r$   r2   r3   r.   r%   predtargetr(   r(   r)   r4      s   $zMultitaskWrapper.updateoutputc                    s    fdd|  D S )zYConvert the output of the underlying metrics to a dictionary with the task names as keys.c                    s&   i | ]\}} j  |  j |qS r(   )r"   r#   ).0r.   task_outputr$   r(   r)   
<dictcomp>   s   & z4MultitaskWrapper._convert_output.<locals>.<dictcomp>)r-   )r$   r7   r(   r:   r)   _convert_output   s   z MultitaskWrapper._convert_outputc                 C   s   |  dd | j D S )zCompute metrics for all tasks.c                 S   s   i | ]	\}}||  qS r(   )computer8   r.   r%   r(   r(   r)   r;      s    z,MultitaskWrapper.compute.<locals>.<dictcomp>r<   r   r-   r:   r(   r(   r)   r=      s   zMultitaskWrapper.computec                    s    |   fdd| j D S )zTCall underlying forward methods for all tasks and return the result as a dictionary.c                    s$   i | ]\}}|| | | qS r(   r(   r>   r2   r3   r(   r)   r;      s    z,MultitaskWrapper.forward.<locals>.<dictcomp>r?   )r$   r2   r3   r(   r@   r)   forward   s   zMultitaskWrapper.forwardc                    s&   | j  D ]}|  qt   dS )zReset all underlying metrics.N)r   r   resetr   )r$   r%   r&   r(   r)   rB      s   
zMultitaskWrapper.resetargnamec                 C   s.   | d u s	t | tr| S td| dt|  )NzExpected input `z` to be a string, but got )r   r    r!   r   )rC   rD   r(   r(   r)   
_check_arg   s   zMultitaskWrapper._check_argc                 C   s0   t | }| |dpd|_| |dpd|_|S )zMake a copy of the metric.

        Args:
            prefix: a string to append in front of the metric keys
            postfix: a string to append after the keys of the output dict.

        r   r   )r   rE   r"   r#   )r$   r   r   multitask_copyr(   r(   r)   clone  s   zMultitaskWrapper.clonevalaxesc           	         s*  |dur8t |tstdt| tdd |D stdt|t| jkr8tdt| dt| j d|dur>|n|  }g }t	| j
 D ]G\}\ }|durY|| nd}t |trl|j|  |d	\}}nt |tr|j fd
d|D |d	\}}n	tdt| |||f qK|S )az  Plot a single or multiple values from the metric.

        All tasks' results are plotted on individual axes.

        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.
            axes: Sequence of matplotlib axis objects. If provided, will add the plots to the provided axis objects.
                If not provided, will create them.

        Returns:
            Sequence of tuples with Figure and Axes object for each task.

        .. plot::
            :scale: 75

            >>> # Example plotting a single value
            >>> import torch
            >>> from torchmetrics.wrappers import MultitaskWrapper
            >>> from torchmetrics.regression import MeanSquaredError
            >>> from torchmetrics.classification import BinaryAccuracy
            >>>
            >>> classification_target = torch.tensor([0, 1, 0])
            >>> regression_target = torch.tensor([2.5, 5.0, 4.0])
            >>> targets = {"Classification": classification_target, "Regression": regression_target}
            >>>
            >>> classification_preds = torch.tensor([0, 0, 1])
            >>> regression_preds = torch.tensor([3.0, 5.0, 2.5])
            >>> preds = {"Classification": classification_preds, "Regression": regression_preds}
            >>>
            >>> metrics = MultitaskWrapper({
            ...     "Classification": BinaryAccuracy(),
            ...     "Regression": MeanSquaredError()
            ... })
            >>> metrics.update(preds, targets)
            >>> value = metrics.compute()
            >>> fig_, ax_ = metrics.plot(value)

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> import torch
            >>> from torchmetrics.wrappers import MultitaskWrapper
            >>> from torchmetrics.regression import MeanSquaredError
            >>> from torchmetrics.classification import BinaryAccuracy
            >>>
            >>> classification_target = torch.tensor([0, 1, 0])
            >>> regression_target = torch.tensor([2.5, 5.0, 4.0])
            >>> targets = {"Classification": classification_target, "Regression": regression_target}
            >>>
            >>> classification_preds = torch.tensor([0, 0, 1])
            >>> regression_preds = torch.tensor([3.0, 5.0, 2.5])
            >>> preds = {"Classification": classification_preds, "Regression": regression_preds}
            >>>
            >>> metrics = MultitaskWrapper({
            ...     "Classification": BinaryAccuracy(),
            ...     "Regression": MeanSquaredError()
            ... })
            >>> values = []
            >>> for _ in range(10):
            ...     values.append(metrics(preds, targets))
            >>> fig_, ax_ = metrics.plot(values)

        Nz>Expected argument `axes` to be a Sequence. Found type(axes) = c                 s   s    | ]}t |tV  qd S )N)r   r   )r8   axr(   r(   r)   	<genexpr>W  s    z(MultitaskWrapper.plot.<locals>.<genexpr>zBExpected each ax in argument `axes` to be a matplotlib axis objectzfExpected argument `axes` to be a Sequence of the same length as the number of tasks.Found len(axes) = z and z tasks)rJ   c                    s   g | ]}|  qS r(   r(   )r8   vr.   r(   r)   
<listcomp>g  s    z)MultitaskWrapper.plot.<locals>.<listcomp>zWExpected argument `val` to be None or of type Dict or Sequence[Dict]. Found type(val)= )r   r   r   r   alllenr   r!   r=   	enumerater-   r   plotappend)	r$   rH   rI   fig_axsitask_metricrJ   far(   rM   r)   rR     s8   D


"r   )NN)T)r   N)$__name__
__module____qualname____doc__r   bool__annotations__r   r    r   r   r
   r   r   r   tupler	   Moduler-   r1   r   r   r4   r<   r=   r   rA   rB   staticmethodrE   rG   r   r   r   rR   __classcell__r(   r(   r&   r)   r      sB   
 e"&".  
r   N)collections.abcr   r   copyr   typingr   r   r   torchr   r	   torchmetrics.collectionsr
   torchmetrics.metricr   torchmetrics.utilities.importsr   torchmetrics.utilities.plotr   r   torchmetrics.wrappers.abstractr   __doctest_skip__r   r(   r(   r(   r)   <module>   s   