
    i                         d dl Z d dlmZ d dlZd dlmZmZ ddededefdZded	ed
ededef
dZddededede	eeef   fdZ
	 ddededeee	eef   f   dedef
dZy)    N)Union)Tensortensorx
block_sizereturnc                    | j                   \  }}}}|dkD  rt        d| d      t        j                  |dz
        }t        j                  t        |dz
  |dz
  |            }t        j                  t        t        |j                               j                  |j                                           }t        j                  |dz
        }	t        j                  t        |dz
  |dz
  |            }
t        j                  t        t        |	j                               j                  |
j                                           }| dddddd|f   | dddddd|dz   f   z
  j                  d      j                         }| dddddd|f   | dddddd|dz   f   z
  j                  d      j                         }|| dddd|
ddf   | dddd|
dz   ddf   z
  j                  d      j                         z  }|| dddd|ddf   | dddd|dz   ddf   z
  j                  d      j                         z  }|||z  z  dz
  }||dz
  z  |z
  }|||z  z  dz
  }||dz
  z  |z
  }|||z   z  }|||z   z  }||kD  r5t        j                  |      t        j                  t        ||            z  nd}|||z
  z  S )zCompute block effect.

    Args:
        x: input image
        block_size: integer indication the block size

    Returns:
        Computed block effect

    Raises:
        ValueError:
            If the image is not a grayscale image

       z=`psnrb` metric expects grayscale images, but got images with z
 channels.Ng       @r   )shape
ValueErrortorcharanger   rangelistsettolistsymmetric_differencepowsummathlog2min)r   r   _channelsheightwidthhh_bh_bcvv_bv_bcd_bd_bcn_hbn_hbcn_vbn_vbcts                      x/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/functional/image/psnrb.py_compute_befr+      s   ( 	
	!|XYaXbblmnnUQYA
,,uZ!^UQY
C
DC<<S_AA#**,OPQDVaZ A
,,uZ!^VaZD
EC<<S_AA#**,OPQDQ1c\?Qq!Qa/00
5
5c
:
>
>
@CaAtmqAq$(!23388=AACDAaClOa1cAgq 01166s;??AACQq!T1}!Qq!"3 4499#>BBDDDUZ'(1,Duqy!T)EFZ'(1,Dfqj!T)E4$;CEEMDADt		*		#fe*< ==QRAd
    sum_squared_errorbefnum_obs
data_rangec                 N    | |z  |z   } dt        j                  |dz  | z        z  S )zComputes peak signal-to-noise ratio.

    Args:
        sum_squared_error: Sum of square of errors over all observations
        bef: block effect
        num_obs: Number of predictions or observations
        data_range: the range of the data.

    
      )r   log10)r-   r.   r/   r0   s       r*   _psnrb_computer5   D   s2     *G3c9JM,==>>>r,   predstargetc                     t        j                  t        j                  | |z
  d            }t        |j	                         |j
                        }t        | |      }|||fS )zUpdates and returns variables required to compute peak signal-to-noise ratio.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor
        block_size: Integer indication the block size

    r3   )devicer   )r   r   r   r   numelr9   r+   )r6   r7   r   r-   r/   r.   s         r*   _psnrb_updater<   W   sQ     		%))EFNA">?V\\^FMM:G
u
4Cc7**r,   c                 ,   t        |t              rQt        j                  | |d   |d         } t        j                  ||d   |d         }t	        |d   |d   z
        }nt	        t        |            }t        | ||      \  }}}t        ||||      S )a  Computes `Peak Signal to Noise Ratio With Blocked Effect` (PSNRB) metrics.

    .. math::
        \text{PSNRB}(I, J) = 10 * \log_{10} \left(\frac{\max(I)^2}{\text{MSE}(I, J)-\text{B}(I, J)}\right)

    Where :math:`\text{MSE}` denotes the `mean-squared-error`_ function.

    Args:
        preds: estimated signal
        target: ground truth signal
        data_range: the range of the data. If a tuple is provided then the range is calculated as the difference and
            input is clamped between the values.
        block_size: integer indication the block size

    Return:
        Tensor with PSNRB score

    Example:
        >>> from torch import rand
        >>> from torchmetrics.functional.image import peak_signal_noise_ratio_with_blocked_effect
        >>> preds = rand(1, 1, 28, 28)
        >>> target = rand(1, 1, 28, 28)
        >>> peak_signal_noise_ratio_with_blocked_effect(preds, target, data_range=1.0)
        tensor(7.8402)

    r   r
   )r   maxr:   )
isinstancetupler   clampr   floatr<   r5   )r6   r7   r0   r   data_range_valr-   r.   r/   s           r*   +peak_signal_noise_ratio_with_blocked_effectrD   f   s    @ *e$Ez!}*Q-HVAJqMJ
1
1 =>j 12&3E6j&Y#sG+S'>JJr,   )   )r   typingr   r   r   r   intr+   r5   r@   r<   rB   rD    r,   r*   <module>rI      s        ,F , ,F ,^??	? ? 	?
 ?&+ + +S +vW]_eOeIf +& 	(K(K(K eU5%<001(K 	(K
 (Kr,   