Ë
    þÍ:jf  ã                   ó˜   — 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/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/torchmetrics/shape/procrustes.pyr#   zProcrustesDisparity.__init__P   sl   ø€ Ü‰ÑÑ"˜6Ò"Ø˜OÑ+ÜÐXÐYbÐXcÐdÓeÐeØ"ˆŒØ�‰�{¬E¯L©L¸Ó,=È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ˆ	Ø�Š˜)Ÿ-™-›/Ñ)�Ø�
Š
�i—o‘oÓ'Ñ'Ž
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Ò#Ø—>‘> D§J¡JÑ.Ð.Ø�~‰~Ð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 �z‰z˜#˜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ó (ð˜Ÿ™ó ñ&#˜˜f h¨vÑ&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+   