o
    .wif                     @   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 d dlmZ d d	lmZmZ es?d
gZG dd deZdS )    )Sequence)AnyOptionalUnionN)Tensor)Literal)Metric)procrustes_disparity)_MATPLOTLIB_AVAILABLE)_AX_TYPE_PLOT_OUT_TYPEProcrustesDisparity.plotc                       s   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dZdejdejddfddZdejfddZddeeee df dee defddZ  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mean	reductionr   sumkwargsreturnNc                    s^   t  jd	i | |dvrtd| || _| jdtddd | jdtddd d S )
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__r   Z/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/shape/procrustes.pyr!   P   s   zProcrustesDisparity.__init__point_cloud1point_cloud2c                 C   s2   t ||}|  j| 7  _|  j| 7  _dS )z8Update the Procrustes Disparity with the given datasets.N)r	   r   r   r   numel)r&   r*   r+   r   r   r   r)   updateX   s   
zProcrustesDisparity.updatec                 C   s   | j dkr| j| j S | jS )z"Computes the Procrustes Disparity.r   )r   r   r   )r&   r   r   r)   compute^   s   
zProcrustesDisparity.compute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.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&   r/   r0   r   r   r)   plotd   s   &r   )r   )NN)__name__
__module____qualname____doc__r   __annotations__r   boolr   r   r   floatr   r   r   r!   r$   r-   r.   r   r   r   r   r   r2   __classcell__r   r   r'   r)   r      s   
 ) 2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   r)   <module>   s   