class_docstr: Generate Anomaly Heatmap and segmentation.
class_docstr_tok: ["Generate","Anomaly","Heatmap","and","segmentation","."]
docstr: The binomial test applied to validate or reject the null hypothesis that the pixel is normal. The null
  hypothesis is that the pixel is normal, and the alternative hypothesis is that the pixel is anomalous. The
  binomial test is applied to a window around the pixel, and the number of pixels in the window that are
  anomalous is compared to the number of pixels that are expected to be anomalous under the null hypothesis.
  Args:
      z: Latent variable from the UFlow model. Tensor of shape (N, Cl, Hl, Wl), where N is the batch size, Cl is
      the number of channels, and Hl and Wl are the height and width of the latent variables, respectively.
      win: Window size for the binomial test.
      probability_thr: Probability threshold for the binomial test.
      high_precision: Whether to use high precision for the binomial test.
  
  Returns:
      Log of the probability of the null hypothesis.
docstr_tok: ["The","binomial","test","applied","to","validate","or","reject","the","null","hypothesis","that","the","pixel","is","normal",".","The","null","hypothesis","is","that","the","pixel","is","normal",",","and","the","alternative","hypothesis","is","that","the","pixel","is","anomalous",".","The","binomial","test","is","applied","to","a","window","around","the","pixel",",","and","the","number","of","pixels","in","the","window","that","are","anomalous","is","compared","to","the","number","of","pixels","that","are","expected","to","be","anomalous","under","the","null","hypothesis",".","Args",":","z",":","Latent","variable","from","the","UFlow","model",".","Tensor","of","shape","(","N",",","Cl",",","Hl",",","Wl",")",",","where","N","is","the","batch","size",",","Cl","is","the","number","of","channels",",","and","Hl","and","Wl","are","the","height","and","width","of","the","latent","variables",",","respectively",".","win",":","Window","size","for","the","binomial","test",".","probability_thr",":","Probability","threshold","for","the","binomial","test",".","high_precision",":","Whether","to","use","high","precision","for","the","binomial","test",".","Returns",":","Log","of","the","probability","of","the","null","hypothesis","."]
code: def binomial_test(
      z: torch.Tensor,
      win,
      probability_thr: float,
      high_precision: bool = False,
  ) -> torch.Tensor:
      """
      The binomial test applied to validate or reject the null hypothesis that the pixel is normal. The null
      hypothesis is that the pixel is normal, and the alternative hypothesis is that the pixel is anomalous. The
      binomial test is applied to a window around the pixel, and the number of pixels in the window that are
      anomalous is compared to the number of pixels that are expected to be anomalous under the null hypothesis.
      Args:
          z: Latent variable from the UFlow model. Tensor of shape (N, Cl, Hl, Wl), where N is the batch size, Cl is
          the number of channels, and Hl and Wl are the height and width of the latent variables, respectively.
          win: Window size for the binomial test.
          probability_thr: Probability threshold for the binomial test.
          high_precision: Whether to use high precision for the binomial test.
  
      Returns:
          Log of the probability of the null hypothesis.
  
      """
      tau = st.chi2.ppf(probability_thr, 1)
      half_win = np.max([int(win // 2), 1])
  
      n_chann = z.shape[1]
  
      # Candidates
      z2 = (
          F.pad(z**2, tuple(4 * [half_win]), "reflect").detach().cpu()
      )
      z2_unfold_h = z2.unfold(-2, 2 * half_win + 1, 1)
      z2_unfold_hw = z2_unfold_h.unfold(-2, 2 * half_win + 1, 1).numpy()
      observed_candidates_k = np.sum(z2_unfold_hw >= tau, axis=(-2, -1))
  
      # All volume together
      observed_candidates = np.sum(
          observed_candidates_k, axis=1, keepdims=True
      )
      x = observed_candidates / n_chann
      n = int((2 * half_win + 1) ** 2)
  
      # Low precision
      if not high_precision:
          log_prob = torch.tensor(
              st.binom.logsf(x, n, 1 - probability_thr) / np.log(10)
          )
      # High precision - good and slow
      else:
          to_mp = np.frompyfunc(mp.mpf, 1, 1)
          mpn = mp.mpf(n)
          mpp = probability_thr
  
          def binomial_density(k):
              return (
                  binomial(mpn, to_mp(k))
                  * (1 - mpp) ** k
                  * mpp ** (mpn - k)
              )
  
          def integral(xx):
              return integrate.quad(binomial_density, xx, n)[0]
  
          integral_array = np.vectorize(integral)
          prob = integral_array(x)
          log_prob = torch.tensor(np.log10(prob))
  
      return log_prob
  
file_path: anomalib/src/anomalib/models/uflow/anomaly_map.py
imports: from __future__ import annotations
  from typing import List
  import numpy
  import scipy.stats
  import torch
  import torch.nn.functional
  from mpmath import binomial, mp
  from omegaconf import ListConfig
  from scipy import integrate
  from torch import Tensor, nn
label_desc: Use image node_id 5 for calling the AnomalyMapGenerator obj's underlying member method code with example usage: obj.binomial_test(z, win, probability_thr, high_precision) and returns: log_prob, unknown, integrate