o
    Zhy                     @   s   d dl mZmZmZ d dlZdejdejfddZdejdejdeejejf fd	d
Z		ddejdededee	ejf fddZ
				ddejdeej dedededejfddZdS )    )DictOptionalTupleN
boundariesreturnc                 C   s@   | d | d  }| |d  }t j||d | dgdd}|S )N   r      dim)torchcatZ	unsqueeze)r   stepbin_centers r   Z/var/www/auris/lib/python3.10/site-packages/transformers/models/esm/openfold_utils/loss.py_calculate_bin_centers   s    r   alignment_confidence_breaksaligned_distance_error_probsc                 C   s"   t | }tj|| dd|d fS )Nr	   r
   )r   r   sum)r   r   r   r   r   r   !_calculate_expected_aligned_error   s   r      @   logitsmax_binno_binsc                 K   sF   t jd||d | jd}t jjj| dd}t||d\}}|||dS )aP  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   Z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    r   r   r   compute_predicted_aligned_error'   s   
r&   :0yE>residue_weightsepsc                 K   s   |d u r|  | jd }tjd||d | jd}t|}t| | jd }t|d}	d|	d d  d	 }
tjj	j
| d
d}dd|d |
d    }tj|| d
d}|||   }tj|| d
d}|| }|t|k d }|t| S )Nr   r   r      gףp=
?   gUUUUUU?g?r	   r
   g      ?r   )Znew_onesshaper   r!   r   r   r   maxr"   r#   r$   Znonzerotuple)r   r(   r   r   r)   r%   r   r   nZ	clipped_nZd0ZprobsZ
tm_per_binZpredicted_tm_termZnormed_residue_maskZper_alignmentZweightedZargmaxr   r   r   
compute_tmJ   s    


r1   )r   r   )Nr   r   r'   )typingr   r   r   r   ZTensorr   r   intstrr&   floatr1   r   r   r   r   <module>   sL   

%