def compute_score(self, distance: torch.Tensor, scale: tuple[int, int]) -> torch.Tensor:
        """Compute anomaly scores from distance features.

        Args:
            distance (torch.Tensor): Distance tensor of shape
                ``(batch_size, num_pixels, 1)``.
            scale (tuple[int, int]): Height and width of the feature map used
                to reshape the scores.

        Returns:
            torch.Tensor: Anomaly scores of shape
                ``(batch_size, 1, height, width)``.
        """
        distance = torch.sqrt(distance)
        distance = distance.topk(self.num_nearest_neighbors, largest=False).values  # noqa: PD011
        distance = (F.softmin(distance, dim=-1)[:, :, 0]) * distance[:, :, 0]
        distance = distance.unsqueeze(-1)

        score = rearrange(distance, "b (h w) c -> b c h w", h=scale[0], w=scale[1])
        return score.detach()
Compute anomaly scores from distance features.

        Args:
            distance (torch.Tensor): Distance tensor of shape
                ``(batch_size, num_pixels, 1)``.
            scale (tuple[int, int]): Height and width of the feature map used
                to reshape the scores.

        Returns:
            torch.Tensor: Anomaly scores of shape
                ``(batch_size, 1, height, width)``.
        
src/anomalib/models/image/cfa/anomaly_map.py