
    i                         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	 d dl
mZ  G d de      Z G d d	e      Z G d
 de      Zy)    )AnyCallableOptionalUnionN)MetricCollection)Metric)WrapperMetricc                   r    e Zd ZdZdeeef   deee	f   ddf fdZ
dej                  dej                  fdZd	ej                  dej                  fd
Zdej                  deej                  df   fdZdej                  deee	f   ddfdZde	fdZdej                  deee	f   de	fdZd fdZ xZS )MetricInputTransformerzAbstract base class for metric input transformations.

    Input transformations are characterized by them applying a transformation to the input data of a metric, and then
    forwarding all calls to the wrapped metric with modifications applied.

    wrapped_metrickwargsreturnNc                 z    t        |   di | t        |t        t        f      st        d|       || _        y )NzsExpected wrapped metric to be an instance of `torchmetrics.Metric` or `torchmetrics.MetricsCollection`but received  )super__init__
isinstancer   r   	TypeErrorr   )selfr   r   	__class__s      z/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/wrappers/transformations.pyr   zMetricInputTransformer.__init__   sL    "6".63C*DE@@N?OQ  -    predc                     |S )zuDefine transform operations on the prediction data.

        Overridden by subclasses. Identity by default.

        r   )r   r   s     r   transform_predz%MetricInputTransformer.transform_pred(   s	     r   targetc                     |S )zqDefine transform operations on the target data.

        Overridden by subclasses. Identity by default.

        r   r   r   s     r   transform_targetz'MetricInputTransformer.transform_target0   s	     r   args.c                    t        |      dk(  r| j                  |d         fS t        |      dk(  r(| j                  |d         | j                  |d         fS | j                  |d         | j                  |d         g|dd S )zWWrap transformation functions to dispatch args to their individual transform functions.   r      N)lenr   r   )r   r    s     r   _wrap_transformz&MetricInputTransformer._wrap_transform8   s    t9>''Q022t9>&&tAw/1F1FtAw1OOO""47+T-B-B47-KVdSTSUhVVr   c                 Z     | j                   | } | j                  j                  |i | y)z.Wrap the update call of the underlying metric.N)r%   r   updater   r    r   s      r   r'   zMetricInputTransformer.update@   s/    #t##T*"""D3F3r   c                 6    | j                   j                         S )z/Wrap the compute call of the underlying metric.)r   compute)r   s    r   r*   zMetricInputTransformer.computeE   s    ""**,,r   c                 X     | j                   | } | j                  j                  |i |S )z/Wrap the forward call of the underlying metric.)r%   r   forwardr(   s      r   r,   zMetricInputTransformer.forwardI   s2    #t##T**t""**D;F;;r   c                 V    | j                   j                          t        |           y)z-Wrap the reset call of the underlying metric.N)r   resetr   )r   r   s    r   r.   zMetricInputTransformer.resetN   s    !!#r   )r   N)__name__
__module____qualname____doc__r   r   r   dictstrr   r   torchTensorr   r   tupler%   r'   r*   r,   r.   __classcell__r   s   @r   r   r      s    -uV5E-E'F -RVWZ\_W_R` -ei -5<< ELL u||  WU\\ WeELL#<M6N W4ELL 4DcN 4t 4
- -<U\\ <T#s(^ < <
 r   r   c                        e Zd ZdZ	 	 d	dedeeej                  gej                  f      deeej                  gej                  f      de	ddf
 fdZ
 xZS )
LambdaInputTransformera1  Wrapper class for transforming a metrics' inputs given a user-defined lambda function.

    Args:
        wrapped_metric:
            The underlying `Metric` or `MetricCollection`.
        transform_pred:
            The function to apply to the predictions before computing the metric.
        transform_target:
            The function to apply to the target before computing the metric.

    Raises:
        TypeError:
            If `transform_pred` is not a Callable.
        TypeError:
            If `transform_target` is not a Callable.

    Example:
        >>> import torch
        >>> from torchmetrics.classification import BinaryAccuracy
        >>> from torchmetrics.wrappers import LambdaInputTransformer
        >>>
        >>> preds = torch.tensor([0.9, 0.8, 0.7, 0.6, 0.5, 0.6, 0.7, 0.8, 0.5, 0.4])
        >>> targets = torch.tensor([1,0,0,0,0,1,1,0,0,0])
        >>>
        >>> metric = LambdaInputTransformer(BinaryAccuracy(), lambda preds: 1 - preds)
        >>> metric.update(preds, targets)
        >>> metric.compute()
        tensor(0.6000)

    Nr   r   r   r   r   c                     t        |   |fi | |!t        |      st        d| d      || _        |"t        |      st        d| d      || _        y y )NzAExpected `transform_pred` to be of type `Callable` but received ``zCExpected `transform_target` to be of type `Callable` but received `)r   r   callabler   r   r   )r   r   r   r   r   r   s        r   r   zLambdaInputTransformer.__init__t   s     	262%N+"cdrcsst uvv"0D',-YZjYkklm  %5D! (r   )NN)r/   r0   r1   r2   r   r   r   r5   r6   r   r   r8   r9   s   @r   r;   r;   T   s    D LPMQ	55 !5<<.%,,*F!GH5 #8U\\NELL,H#IJ	5
 5 
5 5r   r;   c            	       x     e Zd ZdZd
deeef   dededdf fdZ	de
j                  de
j                  fd	Z xZS )BinaryTargetTransformera  Wrapper class for computing a metric on binarized targets.

    Useful when the given ground-truth targets are continuous, but the metric requires binary targets.

    Args:
        wrapped_metric:
            The underlying `Metric` or `MetricCollection`.
        threshold:
            The binarization threshold for the targets. Targets values `t` are cast to binary with `t > threshold`.

    Raises:
        TypeError:
            If `threshold` is not an `int` or `float`.

    Example:
        >>> import torch
        >>> from torchmetrics.retrieval import RetrievalMRR
        >>> from torchmetrics.wrappers import BinaryTargetTransformer
        >>>
        >>> preds = torch.tensor([0.9, 0.8, 0.7, 0.6, 0.5, 0.6, 0.7, 0.8, 0.5, 0.4])
        >>> targets = torch.tensor([1,0,0,0,0,2,1,0,0,0])
        >>> topics = torch.tensor([0,0,0,0,0,1,1,1,1,1])
        >>>
        >>> metric = BinaryTargetTransformer(RetrievalMRR())
        >>> metric.update(preds, targets, indexes=topics)
        >>> metric.compute()
        tensor(0.7500)

    r   	thresholdr   r   Nc                 ~    t        |   |fi | t        |t        t        f      st        d| d      || _        y )NzBExpected `threshold` to be of type `int` or `float` but received `r=   )r   r   r   intfloatr   rA   )r   r   rA   r   r   s       r   r   z BinaryTargetTransformer.__init__   sB    262)c5\2`aj`kklmnn"r   r   c                 j    |j                  | j                        j                  |j                        S )zyCast the target tensor to binary values according to the threshold.

        Output assumes same type as input.

        )gtrA   todtyper   s     r   r   z(BinaryTargetTransformer.transform_target   s&     yy(++FLL99r   )r   )r/   r0   r1   r2   r   r   r   rD   r   r   r5   r6   r   r8   r9   s   @r   r@   r@      sR    <#uV5E-E'F #SX #hk #pt #:u|| : :r   r@   )typingr   r   r   r   r5   torchmetrics.collectionsr   torchmetrics.metricr   torchmetrics.wrappers.abstractr	   r   r;   r@   r   r   r   <module>rM      sA    2 1  5 & 8:] :z253 25j+:4 +:r   