
    if                         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 d dlmZ d d	lmZmZ esd
gZ G d de      Zy)    )Sequence)AnyOptionalUnionN)Tensor)Literal)Metric)procrustes_disparity)_MATPLOTLIB_AVAILABLE)_AX_TYPE_PLOT_OUT_TYPEzProcrustesDisparity.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ded   deddf fdZdej                  dej                  ddfdZdej                  fdZddeeee   df   dee   defdZ xZS )ProcrustesDisparityak  Compute the `Procrustes Disparity`_.

    The Procrustes Disparity is defined as the sum of the squared differences between two datasets after
    applying a Procrustes transformation. The Procrustes Disparity is useful to compare two datasets
    that are similar but not aligned.

    The metric works similar to ``scipy.spatial.procrustes`` but for batches of data points. The disparity is
    aggregated over the batch, thus to get the individual disparities please use the functional version of this
    metric: ``torchmetrics.functional.shape.procrustes.procrustes_disparity``.

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

        - ``point_cloud1`` (torch.Tensor): A tensor of shape ``(N, M, D)`` with ``N`` being the batch size,
          ``M`` the number of data points and ``D`` the dimensionality of the data points.
        - ``point_cloud2`` (torch.Tensor): A tensor of shape ``(N, M, D)`` with ``N`` being the batch size,
          ``M`` the number of data points and ``D`` the dimensionality of the data points.


    As output to ``forward`` and ``compute`` the metric returns the following output:

        - ``gds`` (:class:`~torch.Tensor`): A scalar tensor with the Procrustes Disparity.

    Args:
        reduction: Determines whether to return the mean disparity or the sum of the disparities.
            Can be one of ``"mean"`` or ``"sum"``.
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Raises:
        ValueError: If ``average`` is not one of ``"mean"`` or ``"sum"``.

    Example:
        >>> from torch import randn
        >>> from torchmetrics.shape import ProcrustesDisparity
        >>> metric = ProcrustesDisparity()
        >>> point_cloud1 = randn(10, 50, 2)
        >>> point_cloud2 = randn(10, 50, 2)
        >>> metric(point_cloud1, point_cloud2)
        tensor(0.9770)

    	disparitytotalFfull_state_updateis_differentiablehigher_is_better        plot_lower_boundg      ?plot_upper_bound	reductionmeansumkwargsreturnNc                     t        |   d	i | |dvrt        d|       || _        | j	                  dt        j                  d      d       | j	                  dt        j                  d      d       y )
Nr   z9Argument `reduction` must be one of ['mean', 'sum'], got r   r   r   )defaultdist_reduce_fxr   r    )super__init__
ValueErrorr   	add_statetorchtensor)selfr   r   	__class__s      r/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/shape/procrustes.pyr#   zProcrustesDisparity.__init__P   sl    "6"O+XYbXcdee"{ELL,=eTwQN    point_cloud1point_cloud2c                     t        ||      }| xj                  |j                         z  c_        | xj                  |j	                         z  c_        y)z8Update the Procrustes Disparity with the given datasets.N)r
   r   r   r   numel)r(   r,   r-   r   s       r*   updatezProcrustesDisparity.updateX   s7    0|L	)--/)

ioo''
r+   c                 j    | j                   dk(  r| j                  | j                  z  S | j                  S )z"Computes the Procrustes Disparity.r   )r   r   r   )r(   s    r*   computezProcrustesDisparity.compute^   s+    >>V#>>DJJ..~~r+   valaxc                 &    | j                  ||      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.shape import ProcrustesDisparity
            >>> metric = ProcrustesDisparity()
            >>> metric.update(torch.randn(10, 50, 2), torch.randn(10, 50, 2))
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

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

        )_plot)r(   r3   r4   s      r*   plotzProcrustesDisparity.plotd   s    L zz#r""r+   )r   )NN)__name__
__module____qualname____doc__r   __annotations__r   boolr   r   r   floatr   r   r   r#   r&   r0   r2   r   r   r   r   r   r7   __classcell__)r)   s   @r*   r   r      s    'R M#t##t#"d"!e!!e!O'-"8 OS OUY O(5<< (u|| (PT ( &#fhv&6<= &#(S[J\ &#hv &#r+   r   )collections.abcr   typingr   r   r   r&   r   typing_extensionsr   torchmetricsr	   (torchmetrics.functional.shape.procrustesr
   torchmetrics.utilities.importsr   torchmetrics.utilities.plotr   r   __doctest_skip__r   r!   r+   r*   <module>rH      s?    % ' '   %  I @ @23l#& l#r+   