
    cCil                        S SK Jr  S SKrS\R                  S\R                  4S jrS\R                  S\R                  S\\R                  \R                  4   4S jr  SS	\R                  S
\S\S\\	\R                  4   4S jjr
    SS	\R                  S\\R                     S
\S\S\S\R                  4S jjrg)    )OptionalN
boundariesreturnc                     U S   U S   -
  nXS-  -   n[         R                  " X"S   U-   R                  S5      /SS9nU$ )N   r      dim)torchcat	unsqueeze)r   stepbin_centerss      e/home/james-whalen/.local/lib/python3.13/site-packages/transformers/models/esm/openfold_utils/loss.py_calculate_bin_centersr      sN    a=:a=(Dax'K))[r?T+A*L*LR*PQWXYK    alignment_confidence_breaksaligned_distance_error_probsc                 P    [        U 5      n[        R                  " X-  SS9US   4$ )Nr	   r
   )r   r   sum)r   r   r   s      r   !_calculate_expected_aligned_errorr      s2     ))DEK		.<"EB r   logitsmax_binno_binsc                     [         R                  " SXS-
  U R                  S9n[         R                  R                  R                  U SS9n[        UUS9u  pgUUUS.$ )a   Computes aligned confidence metrics from logits.

Args:
  logits: [*, num_res, num_res, num_bins] the logits output from
    PredictedAlignedErrorHead.
  max_bin: Maximum bin value
  no_bins: Number of bins
Returns:
  aligned_confidence_probs: [*, num_res, num_res, num_bins] the predicted
    aligned error probabilities over bins for each residue pair.
  predicted_aligned_error: [*, num_res, num_res] the expected aligned distance
    error for each pair of residues.
  max_predicted_aligned_error: [*] the maximum predicted error possible.
r   r   stepsdevicer	   r
   )r   r   )aligned_confidence_probspredicted_aligned_errormax_predicted_aligned_error)r   linspacer   nn
functionalsoftmaxr   )r   r   r   kwargsr   r    r!   r"   s           r   compute_predicted_aligned_errorr(   '   sg    ( 7Q;VJ$xx22::6r:J;\$.%=<8 %=#:'B r   residue_weightsepsc                    Uc  U R                  U R                  S   5      n[        R                  " SX#S-
  U R                  S9n[        U5      n[        R                  " U5        U R                  S   n[        US5      n	SU	S-
  S-  -  S	-
  n
[        R                  R                  R                  U S
S9nSSUS-  U
S-  -  -   -  n[        R                  " X-  S
S9nXUR                  5       -   -  n[        R                  " X-  S
S9nX-  nU[        R                  " U5      :H  R                  5       S   nU[        U5         $ )Nr   r   r      gףp=
?   gUUUUUU?g?r	   r
   g      ?r   )new_onesshaper   r#   r   r   r   maxr$   r%   r&   nonzerotuple)r   r)   r   r   r*   r'   r   r   n	clipped_nd0probs
tm_per_binpredicted_tm_termnormed_residue_maskper_alignmentweightedargmaxs                     r   
compute_tmr>   J   s4     //&,,r*:;7Q;VJ(4K	IIoRAAr
I	RW-	-	3BHH''B'7E[!^A667J		%"4"=)?3F3F3H-HIII/E2NM.H%))H--668;Fv''r   )   @   )Nr?   r@   g:0yE>)typingr   r   Tensorr   r3   r   intdictstrr(   floatr>    r   r   <module>rH      s     u||  !&"',, 5<<%&  LL    
 
#u||
 J /3(LL(ell+( ( 	(
 
( \\(r   