o
    i*                  	   @   s   d dl mZmZ d dlZd dlZddlmZ ddejdee	 dejfd	d
Z
	d dejdee dee	 dejfddZdedefddZ	d!dejdee dejfddZdddejdeej fddZdejdeejejf fddZdS )"    )OptionalTupleN   )linear_sum_assignmentxdimreturnc                 C   s   | | j |dd S )NTr   keepdim)mean)r   r    r   O/home/ubuntu/.local/lib/python3.10/site-packages/fast_bss_eval/torch/helpers.py_remove_mean   s   r   ư>epsc                 C   s    | t j| j|dd|d } | S )NTr
   min)torchclampnorm)r   r   r   r   r   r   
_normalize"   s   r   db_maxc                 C   s   d|  d  }|d|  }|S )N      $@g      ?r   )r   er   r   r   r   _db_clamp_eps)   s   r         N@cohclamp_dbc                 C   sD   |d ur	t |}nd}tj| |d| d} d|  |  }dt| S )N        r   )r   maxr   )r   r   r   log10)r   r   r   ratior   r   r   _coherence_to_neg_sdr/   s   
r#   F)return_permtarget_loss_matrixc                   sB  | }|j dd  |j dd \}}t||||kr6|dd}t|}t|D ]\}}|dd||< q)| f } fdd|D }	| j f tjd}
t	 D ]-}t
|| \}}||||f  ||< t|D ]\}}||||f  |	| |< qp||
|< qX|r|ft|	 |
f S t|	dkr|S |ft|	 S )z0
    Solve the permutation in numpy for now
    Nr   c                    s   g | ]
}|  f qS r   )	new_zeros).0argb_shape
n_chan_outr   r   
<listcomp>X   s    z&_solve_permutation.<locals>.<listcomp>dtyper   )shaper   	transposelist	enumerater'   r   int64npndindex_linear_sum_assignment_with_inftuplelen)r%   r$   argsloss_mat
n_chan_ref
n_chan_estir)   loss_outargs_outp_optsmdump_optr   r*   r   _solve_permutationB   s.   	

rE   cost_matrixc           
   
   C   s  |   dkrtjdtjdtjdtjdfS t  zt|  }t|  }W n! tyL   t	tj
| dd }t	tj
| dd }Y nw |rY|rYt|  td|s]|r|  } | t	|   }| }| }t| j}||| t| t| d  }|r||d ||   | }|r||d ||   | }|| t	| < t| }	W d   |	S 1 sw   Y  |	S )	a^  
    Solves the permutation problem efficiently via the linear sum
    assignment problem.

    https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.linear_sum_assignment.html
    This implementation was proposed by @louisabraham in
    https://github.com/scipy/scipy/issues/6900
    to handle infinite entries in the cost matrix.
    r   r.   r   )r    r   z!matrix contains both inf and -infr   N)numelr   zerosr4   no_gradisneginfanyisposinfAttributeErrorisinfr   print
ValueErrorcloner   r    r0   absr   )
rF   min_infmax_infvaluesrB   Mnpositiveplace_holderretr   r   r   r7   m   s<    

$

r7   )r   )r   N)r   )typingr   r   numpyr5   r   	hungarianr   Tensorintr   floatr   r   r#   rE   ndarrayr7   r   r   r   r   <module>   sF    


+