




from typing import Dict, Optional, Tuple

import torch





def 							lowerCamelCase    (							UpperCAmelCase_	:      torch.Tensor      )->			torch.Tensor:

       """simple docstring"""




       a			   =boundaries[1] - boundaries[0]
       a			   =boundaries + step / 2
       a			   =torch.cat([bin_centers, (bin_centers[-1] + step).unsqueeze(-1      )]   ,       dim=0      )
       return bin_centers





def 							lowerCamelCase    (							UpperCAmelCase_	:      torch.Tensor   ,       UpperCAmelCase_	:      torch.Tensor   ,       )->			Tuple[torch.Tensor, torch.Tensor]:

       """simple docstring"""




       a			   =_calculate_bin_centers(UpperCAmelCase_      )
       return (
           torch.sum(aligned_distance_error_probs * bin_centers   ,       dim=-1      ),
           bin_centers[-1],
       )





def 							lowerCamelCase    (							UpperCAmelCase_	:      torch.Tensor   ,       UpperCAmelCase_	:      int = 31   ,       UpperCAmelCase_	:      int = 64   ,       **UpperCAmelCase_	:      List[Any]   ,       )->			Dict[str, torch.Tensor]:

       """simple docstring"""




       a			   =torch.linspace(0   ,       UpperCAmelCase_   ,       steps=(no_bins - 1)   ,       device=logits.device      )

       a			   =torch.nn.functional.softmax(UpperCAmelCase_   ,       dim=-1      )
       a   ,      a			   =_calculate_expected_aligned_error(
           alignment_confidence_breaks=UpperCAmelCase_   ,       aligned_distance_error_probs=UpperCAmelCase_   ,       )

       return {
           "aligned_confidence_probs": aligned_confidence_probs,
           "predicted_aligned_error": predicted_aligned_error,
           "max_predicted_aligned_error": max_predicted_aligned_error,
       }






def 							lowerCamelCase    (							UpperCAmelCase_	:      torch.Tensor   ,       UpperCAmelCase_	:      Optional[torch.Tensor] = None   ,       UpperCAmelCase_	:      int = 31   ,       UpperCAmelCase_	:      int = 64   ,       UpperCAmelCase_	:      float = 1e-8   ,       **UpperCAmelCase_	:      Any   ,       )->			torch.Tensor:

       """simple docstring"""




       if residue_weights is None:
              a			   =logits.new_ones(logits.shape[-2]      )

       a			   =torch.linspace(0   ,       UpperCAmelCase_   ,       steps=(no_bins - 1)   ,       device=logits.device      )

       a			   =_calculate_bin_centers(UpperCAmelCase_      )
       torch.sum(UpperCAmelCase_      )
       a			   =logits.shape[-2]
       a			   =max(UpperCAmelCase_   ,       19      )

       a			   =1.24 * (clipped_n - 15) ** (1.0 / 3) - 1.8

       a			   =torch.nn.functional.softmax(UpperCAmelCase_   ,       dim=-1      )

       a			   =1.0 / (1 + (bin_centers**2) / (da**2))
       a			   =torch.sum(probs * tm_per_bin   ,       dim=-1      )

       a			   =residue_weights / (eps + residue_weights.sum())
       a			   =torch.sum(predicted_tm_term * normed_residue_mask   ,       dim=-1      )

       a			   =per_alignment * residue_weights

       a			   =(weighted == torch.max(UpperCAmelCase_      )).nonzero()[0]
       return per_alignment[tuple(UpperCAmelCase_      )]






import math
import os

import cva
import numpy as np

_lowerCamelCase							=      255.0





def 							lowerCamelCase    (							UpperCAmelCase_	:      float   ,       UpperCAmelCase_	:      float      )->			float:

       """simple docstring"""




       a			   =np.mean((original - contrast) ** 2      )
       if mse == 0:
              return 100

       return 20 * math.logaa(PIXEL_MAX / math.sqrt(UpperCAmelCase_      )      )





def 							lowerCamelCase    (							)->			None:

       """simple docstring"""




       a			   =os.path.dirname(os.path.realpath(UpperCAmelCase_      )      )
       # Loading images (original image and compressed image)
       a			   =cva.imread(os.path.join(UpperCAmelCase_   ,       """image_data/original_image.png"""      )      )
       a			   =cva.imread(os.path.join(UpperCAmelCase_   ,       """image_data/compressed_image.png"""      )   ,       1      )

       a			   =cva.imread(os.path.join(UpperCAmelCase_   ,       """image_data/PSNR-example-base.png"""      )      )
       a			   =cva.imread(
           os.path.join(UpperCAmelCase_   ,       """image_data/PSNR-example-comp-10.jpg"""      )   ,       1      )

       # Value expected: 29.73dB
       print("""-- First Test --"""      )
       print(F'''PSNR value is {peak_signal_to_noise_ratio(UpperCAmelCase_   ,       UpperCAmelCase_      )} dB'''      )

       # # Value expected: 31.53dB (Wikipedia Example)
       print("""\n-- Second Test --"""      )
       print(F'''PSNR value is {peak_signal_to_noise_ratio(UpperCAmelCase_   ,       UpperCAmelCase_      )} dB'''      )


if __name__ == "__main__":
    main()
