a
    d                     @   s   d dl Z d dlmZ d dlZd dlmZmZ deeedddZeeeeedd	d
Zdeeeeeeef dddZ	deeeedddZ
dS )    N)Tuple)Tensortensor   )x
block_sizereturnc                 C   sb  | j \}}}}|dkr&td| dt|d }tt|d |d |}ttt| 	| }t|d }	tt|d |d |}
ttt|	 	|
 }| dddddd|f | dddddd|d f  
d }| dddddd|f | dddddd|d f  
d }|| dddd|
ddf | dddd|
d ddf  
d 7 }|| dddd|ddf | dddd|d ddf  
d 7 }|||  d }||d  | }|||  d }||d  | }|||  }|||  }||krRt|tt|| nd}|||  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torchZaranger   rangelistsettolistsymmetric_differencepowsummathlog2min)r   r   _ZchannelsheightwidthhZh_bZh_bcvZv_bZv_bcZd_bZd_bcZn_hbZn_hbcZn_vbZn_vbct r   l/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/functional/image/psnrb.py_compute_bef   s4      FFJJ(r   )sum_squared_errorbefn_obs
data_ranger   c                 C   s<   | | | } |dkr*dt |d |   S dt d|   S )a-  Computes peak signal-to-noise ratio.

    Args:
        sum_squared_error: Sum of square of errors over all observations
        bef: block effect
        n_obs: Number of predictions or observations
        data_range: the range of the data. If None, it is determined from the data (max - min).

       
   g      ?)r   log10)r    r!   r"   r#   r   r   r   _psnrb_computeD   s    r'   )predstargetr   r   c                 C   s>   t t | | d}t| |jd}t| |d}|||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

    r$   )devicer   )r   r   r   r   Znumelr*   r   )r(   r)   r   r    r"   r!   r   r   r   _psnrb_updateY   s    	r,   c                 C   s2   |  |  }t| ||d\}}}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: groun truth signal
        block_size: integer indication the block size

    Return:
        Tensor with PSNRB score

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

    r+   )maxr   r,   r'   )r(   r)   r   r#   r    r!   r"   r   r   r   +peak_signal_noise_ratio_with_blocked_effecth   s    r.   )r   )r   )r   )r   typingr   r   r   r   intr   r'   r,   r.   r   r   r   r   <module>   s$   0  