
    i                         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edede j                  d	e j                  d
ef
dZdededed
efdZ	 	 ddedededed   d
ef
dZy)    N)Tensor)conv2d)Literal)dim_zero_catwin_sizesigmadtypedevicereturnc                    t        j                  | ||      | dz
  dz  z
  }|dz  }t        j                  |j                  d      |j                  d      z    d|dz  z  z        }|t        j                  |      z  }|S )Nr	   r
         r          @)torcharangeexp	unsqueezesum)r   r   r	   r
   coordsgs         v/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/functional/image/vif.py_filterr      sx    
 \\(%?8a<STBTTF	A		AKKNQ[[^34eQhGHA1AH    predstarget
sigma_n_sqc           
         | j                   }| j                  }| j                  d      } |j                  d      }t        j                  d||      }t        j                  |||      }t        j
                  | j                  d      ||      }t        j
                  | j                  d      ||      }t        d      D ]  }dd|z
  z  dz   }	t        |	|	dz  ||      d d d d f   }
|dkD  r:t        ||
      d d d d d d dd d df   }t        | |
      d d d d d d dd d df   } t        ||
      }t        | |
      }|dz  }|dz  }||z  }t        j                  t        |dz  |
      |z
  d	
      }t        j                  t        | dz  |
      |z
  d	
      }t        || z  |
      |z
  }|||z   z  }|||z  z
  }||k  }d||<   ||   ||<   d||<   ||k  }d||<   d||<   |dk  }||   ||<   d||<   t        j                  ||
      }|t        j                  t        j                  d|dz  |z  ||z   z  z         g d      z  }|t        j                  t        j                  d||z  z         g d      z  } ||z  S )Nr   g|=r   r      r      r   g        )ming      ?)r   r      dim)r	   r
   r   r   tensorzerossizeranger   r   clampr   log10)r   r   r   r	   r
   eps	preds_vif
target_vifscalenkernel	mu_targetmu_predsmu_target_sqmu_preds_sqmu_target_predssigma_target_sqsigma_preds_sqsigma_target_predsr   
sigma_v_sqmasks                         r   _vif_per_channelr;   "   s   KKE\\FOOAEa F
,,uE&
9CjfEJEJJqMvFIUZZ]%GJqAI"AEv>tT1}M19FF+Aq#A#ssN;F5&)!Q!SqS.9E66*	%( !|k#h.++fVQY&?,&NTWXVE1Hf%=%KQTU#FUNF;oM/C"78#a*<&<<
$$)$/
4 !#$
41u)$/
4$[[5
UYYu{{3!S&O1Kz\fOf1g+ghnwxx	eiiC/J2N,N OU^__
I L z!!r   	reductionmeannonec                 d   | j                  d      dk  s| j                  d      dk  r0t        d| j                  d       d| j                  d       d      |j                  d      dk  s|j                  d      dk  r0t        d|j                  d       d|j                  d       d      | j                  |j                  k7  r&t        d| j                   d	|j                   d
      |dvrt        d|       t        | j                  d            D cg c])  }t	        | dd|ddddf   |dd|ddddf   |      + }}t        | j                  d      dkD  r&t        j                  |d      j                  d      n|d         }|dk(  r|j                         S |S c c}w )a  Compute Pixel-Based Visual Information Fidelity (VIF-P).

    VIF is a full-reference metric that measures the amount of visual information
    preserved in a distorted image compared to the reference image.

    Args:
        preds: Predicted images of shape (N, C, H, W). Height and width must be at least 41.
        target: Ground truth images of shape (N, C, H, W). Must match preds in shape.
        sigma_n_sq: Variance of the visual noise. Default: 2.0.
        reduction: Method for reducing the metric across the batch.
            - "mean": Return a tensor average over the batch.
            - "none": Return a VIF score for each sample as a 1D tensor of shape (N,).

    Returns:
        torch.Tensor: VIF score(s). The shape depends on the `reduction` argument:
            - If ``reduction="mean"``, returns a scalar tensor.
            - If ``reduction="none"``, returns a tensor of shape ``(N,)``.

    Raises:
        ValueError: If input dimensions are smaller than ``41x41``.
        ValueError: If ``preds`` and ``target`` shapes don't match.
        ValueError: If ``reduction`` is not ``"mean"`` or ``"none"``.

    Example:
        >>> from torchmetrics.functional.image import visual_information_fidelity
        >>> preds = torch.randn(4, 3, 41, 41, generator=torch.Generator().manual_seed(42))
        >>> target = torch.randn(4, 3, 41, 41, generator=torch.Generator().manual_seed(43))
        >>> visual_information_fidelity(preds, target, reduction="none")
        tensor([0.0040, 0.0049, 0.0017, 0.0039])

    )   z8Invalid size of preds. Expected at least 41x41, but got x!z9Invalid size of target. Expected at least 41x41, but got z7`preds` and `target` must have the same shape, but got z vs .r=   z7Argument `reduction` must be 'mean' or 'none', but got r   Nr   r#   r>   )	r'   
ValueErrorshaper(   r;   r   r   stackr>   )r   r   r   r<   iper_channel_scoresvif_per_samples          r   visual_information_fidelityrM   Y   s   R zz"~ejjnr1STYT^T^_aTbSccdejeoeopresdttuvww{{2v{{23GTVGXXYZ`ZeZefhZiYjjkl
 	
 {{fll"RSXS^S^R__cdjdpdpcqqrstt((RS\R]^__ V[[`[e[efg[hUiUiPQq!Qz*F1aA:,>
KUi   ":?**Q-!:K&A.33A6QcdeQfN F""$$s   .F-)r   r>   )r   r   torch.nn.functionalr   typing_extensionsr   torchmetrics.utilities.datar   floatr	   r
   r   r;   rM    r   r   <module>rS      s      & % 4	e 	E 	%++ 	u|| 	X^ 	4"F 4"F 4" 4"& 4"t )/	AAA A ~&	A
 Ar   