class_docstr: Generate Anomaly Heatmap and segmentation.
class_docstr_tok: ["Generate","Anomaly","Heatmap","and","segmentation","."]
docstr: This method is not used in the basic functionality of training and testing. It is a bit slow, so we decided to
  leave it as an option for the user. It is included as it is part of the U-Flow paper, and can be called
  separately if an unsupervised anomaly segmentation is needed.
  
  Generate an anomaly mask, from latent variables. It is based on the NFA (Number of False Alarms) method, which
  is a statistical method to detect anomalies. The NFA is computed as the log of the probability of the null
  hypothesis, which is that all pixels are normal. First, we compute a list of  candidate pixels, with
  suspiciously high values of z^2, by applying a binomial test to each pixel, looking at a window  around it.
  Then, to compute the NFA values (actually the log-NFA), we evaluate how probable is that a pixel  belongs to the
  normal distribution. The null-hypothesis is that under normality assumptions, all candidate pixels are uniformly
  distributed. Then, the detection is based on the concentration of candidate pixels.
  
  Args:
      z: List of latent variables from the UFlow model. Each element is a 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, for each scale l.
      win_size: Window size for the binomial test.
      binomial_probability_thr: Probability threshold for the binomial test.
      high_precision: Whether to use high precision for the binomial test.
  
  Returns:
      Anomaly mask. Tensor of shape (N, 1, H, W), where N is the batch size, and H and W are the height and
      width of the input image, respectively.
docstr_tok: ["This","method","is","not","used","in","the","basic","functionality","of","training","and","testing",".","It","is","a","bit","slow",",","so","we","decided","to","leave","it","as","an","option","for","the","user",".","It","is","included","as","it","is","part","of","the","U-Flow","paper",",","and","can","be","called","separately","if","an","unsupervised","anomaly","segmentation","is","needed",".","Generate","an","anomaly","mask",",","from","latent","variables",".","It","is","based","on","the","NFA","(","Number","of","False","Alarms",")","method",",","which","is","a","statistical","method","to","detect","anomalies",".","The","NFA","is","computed","as","the","log","of","the","probability","of","the","null","hypothesis",",","which","is","that","all","pixels","are","normal",".","First",",","we","compute","a","list","of","candidate","pixels",",","with","suspiciously","high","values","of","z^2",",","by","applying","a","binomial","test","to","each","pixel",",","looking","at","a","window","around","it",".","Then",",","to","compute","the","NFA","values","(","actually","the","log-NFA",")",",","we","evaluate","how","probable","is","that","a","pixel","belongs","to","the","normal","distribution",".","The","null-hypothesis","is","that","under","normality","assumptions",",","all","candidate","pixels","are","uniformly","distributed",".","Then",",","the","detection","is","based","on","the","concentration","of","candidate","pixels",".","Args",":","z",":","List","of","latent","variables","from","the","UFlow","model",".","Each","element","is","a","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",",","for","each","scale","l.","win_size",":","Window","size","for","the","binomial","test",".","binomial_probability_thr",":","Probability","threshold","for","the","binomial","test",".","high_precision",":","Whether","to","use","high","precision","for","the","binomial","test",".","Returns",":","Anomaly","mask",".","Tensor","of","shape","(","N",",","1",",","H",",","W",")",",","where","N","is","the","batch","size",",","and","H","and","W","are","the","height","and","width","of","the","input","image",",","respectively","."]
code: def compute_anomaly_mask(
      self,
      z: List[torch.Tensor],
      win_size: int = 7,
      binomial_probability_thr: float = 0.5,
      high_precision: bool = False,
  ):
      """
      This method is not used in the basic functionality of training and testing. It is a bit slow, so we decided to
      leave it as an option for the user. It is included as it is part of the U-Flow paper, and can be called
      separately if an unsupervised anomaly segmentation is needed.
  
      Generate an anomaly mask, from latent variables. It is based on the NFA (Number of False Alarms) method, which
      is a statistical method to detect anomalies. The NFA is computed as the log of the probability of the null
      hypothesis, which is that all pixels are normal. First, we compute a list of  candidate pixels, with
      suspiciously high values of z^2, by applying a binomial test to each pixel, looking at a window  around it.
      Then, to compute the NFA values (actually the log-NFA), we evaluate how probable is that a pixel  belongs to the
      normal distribution. The null-hypothesis is that under normality assumptions, all candidate pixels are uniformly
      distributed. Then, the detection is based on the concentration of candidate pixels.
  
      Args:
          z: List of latent variables from the UFlow model. Each element is a 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, for each scale l.
          win_size: Window size for the binomial test.
          binomial_probability_thr: Probability threshold for the binomial test.
          high_precision: Whether to use high precision for the binomial test.
  
      Returns:
          Anomaly mask. Tensor of shape (N, 1, H, W), where N is the batch size, and H and W are the height and
          width of the input image, respectively.
      """
      log_prob_l = [
          self.binomial_test(
              zi,
              win_size / (2**scale),
              binomial_probability_thr,
              high_precision,
          )
          for scale, zi in enumerate(z)
      ]
  
      log_prob_l_up = torch.cat(
          [
              F.interpolate(
                  lpl,
                  size=self.input_size,
                  mode="bicubic",
                  align_corners=True,
              )
              for lpl in log_prob_l
          ],
          dim=1,
      )
  
      log_prob = torch.sum(log_prob_l_up, dim=1, keepdim=True)
  
      log_number_of_tests = torch.log10(
          torch.sum(
              torch.tensor([zi.shape[-2] * zi.shape[-1] for zi in z])
          )
      )
      log_nfa = log_number_of_tests + log_prob
  
      anomaly_score = -log_nfa
      anomaly_mask = anomaly_score < 0
  
      return anomaly_mask
  
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 4 for calling the AnomalyMapGenerator obj's underlying member method code with example usage: obj.compute_anomaly_mask(z, win_size, binomial_probability_thr, high_precision) and returns: anomaly_mask