def binomial_test(
        z: torch.Tensor,
        window_size: int,
        probability_thr: float,
        high_precision: bool = False,
    ) -> torch.Tensor:
        """Apply binomial test to validate/reject normality hypothesis.

        For each pixel, tests the null hypothesis that the pixel and its local
        neighborhood are normal against the alternative that they are anomalous.

        The test:
        1. Counts anomalous pixels in local window using chi-square threshold
        2. Compares observed count to expected count under null hypothesis
        3. Returns log probability of observing such extreme counts

        Args:
            z (torch.Tensor): Latent tensor of shape ``(batch_size, channels,
                height, width)``
            window_size (int): Size of local window for counting
            probability_thr (float): Probability threshold for chi-square test
            high_precision (bool, optional): Whether to use high precision
                computation. Defaults to ``False``.

        Returns:
            torch.Tensor: Log probability tensor of shape ``(batch_size, 1,
                height, width)``
        """
        tau = st.chi2.ppf(probability_thr, 1)
        half_win = np.max([int(window_size // 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(tensor: torch.tensor) -> torch.Tensor:
                return binomial(mpn, to_mp(tensor)) * (1 - mpp) ** tensor * mpp ** (mpn - tensor)

            def integral(tensor: torch.Tensor) -> torch.Tensor:
                return integrate.quad(binomial_density, tensor, n)[0]

            integral_array = np.vectorize(integral)
            prob = integral_array(x)
            log_prob = torch.tensor(np.log10(prob))

        return log_prob
Apply binomial test to validate/reject normality hypothesis.

        For each pixel, tests the null hypothesis that the pixel and its local
        neighborhood are normal against the alternative that they are anomalous.

        The test:
        1. Counts anomalous pixels in local window using chi-square threshold
        2. Compares observed count to expected count under null hypothesis
        3. Returns log probability of observing such extreme counts

        Args:
            z (torch.Tensor): Latent tensor of shape ``(batch_size, channels,
                height, width)``
            window_size (int): Size of local window for counting
            probability_thr (float): Probability threshold for chi-square test
            high_precision (bool, optional): Whether to use high precision
                computation. Defaults to ``False``.

        Returns:
            torch.Tensor: Log probability tensor of shape ``(batch_size, 1,
                height, width)``
        
src/anomalib/models/image/uflow/anomaly_map.py