o
    i                  	   @   s   d dl mZmZ d dlZd dlmZ d dd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_assignmentc                 C   s,   |d ur
t | |} |d urt | |} | S )N)npmaximumminimum)xminmax r   O/home/ubuntu/.local/lib/python3.10/site-packages/fast_bss_eval/numpy/helpers.pyclamp   s
   r   r   axisreturnc                 C   s   | | j |dd S )NTr   keepdims)mean)r   r   r   r   r   _remove_mean&   s   r   ư>epsc                 C   s"   | t tjj| |dd|d } | S )NTr   )r	   )r   r   linalg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_eps1   s   r         N@cohclamp_dbc                 C   sB   |d ur	t |}nd}t| |d| d} d|  |  }dt| S )Ng           )r	   r
   r   )r   r   r   log10)r   r    r   ratior   r   r   _coherence_to_neg_sdr7   s   
r$   F)return_permtarget_loss_matrixc                   sF  | }|j dd  |j dd \}}t||||kr6|dd}t|}t|D ]\}}|dd||< q)tj| f d} fdd|D }	tj f tjd}
t	 D ]-}t
|| \}}||||f  ||< t|D ]\}}||||f  |	| |< qr||
|< qZ|r|ft|	 |
f S t|	dkr|S |ft|	 S )	z0
    Solve the permutation in numpy for now
    Nr   shapec                    s    g | ]}t j| f d qS )r(   )r   
zeros_like).0argb_shape
n_chan_outr   r   
<listcomp>^   s     z&_solve_permutation.<locals>.<listcomp>)dtyper   )r)   r	   swapaxeslist	enumerater   r*   zerosint64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_permutationH   s.   	

rF   cost_matrixc           	      C   s   t | } t |  }t |  }|r|rtd|s|rm|  } | t |   }| }|	 }t| j
}||| t | t | d  }|rX||d ||   | }|rf||d ||   | }|| t | < t| 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.
    z!matrix contains both inf and -infr!   )r   asarrayisneginfanyisposinf
ValueErrorcopyisinfr	   r
   r)   absr   )	rG   min_infmax_infvaluesrC   Mnpositiveplace_holderr   r   r   r8   s   s$   

$r8   )NN)r   )r   N)r   )typingr   r   numpyr   scipy.optimizer   r   ndarrayintr   floatr   r   r$   rF   r8   r   r   r   r   <module>   sF   
 


+