
    DUf                         d dl mZ d dlZd dlZd Zej        dd            Zej        	 	 	 dd            Z	 	 	 dd	Z		 ddZ
dS )    )IterableNc           
         t          j        d          5  t          j        t          j        t          j        |           t          j        |          t          j        |                              }ddd           n# 1 swxY w Y   |S )a  
    Interpolate a function in the log-log space.
    Equivalent to np.exp(np.interp(np.log(xs), np.log(xp), np.log(fp))).

    Parameters
    ----------
    xs : array-like
        The x-coordinates at which to evaluate the interpolated values.
    xp : 1-D sequence of floats
        The x-coordinates of the data points, must be increasing.
    fp : 1D array
        The y-coordinates of the data points, same length as xp.

    Returns
    -------
    ys : 1D array
        The interpolated values, same shape as x.
    ignore)divideN)nperrstateexpinterplog)xsxpfpyss       a/var/www/html/software/conda/lib/python3.11/site-packages/cooltools/sandbox/expected_smoothing.py_log_interpr      s    & 
H	%	%	% 
 
VIr

r

r

 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Is   ABBB皙?c                 
   | d         g}| d         }d|z  }| dd         D ]"}|||z  k    r|                     |           |}#|d         | d         k    r|                     | d                    t          j        |          S )a  
    Thin out a sorted array, by selecting a subset of elements that are uniformly spaced in log-space.

    Parameters
    ----------
    xs : array-like
        An array of elements to thin out.
    min_log10_step : float, optional
        The minimal log10 ratio between consecutive elements in the output, by default 0.1

    Returns
    -------
    xs_thinned : array-like
        A subset of elements from xs, whose logs are approx. uniformly spaced.
    r   
      N)appendr   array)r   min_log10_step
xs_thinnedprev	min_ratioxs         r   	_log_thinr   &   s    " Q%Ja5DN"IV  tia   D"~B"R&!!!8J       r   c                    | }|rt          | ||z            }|j        }|j        d         }t          j        |           }t          j        |          }	t          j        ||f          }
t          |          D ]}|	|         }t          j        ||||z  z
            }t          j        ||||z  z             }t          j        ||||         z
  dz   dz  |z  |z            }|	                                }|dk    r=||z  }t          |          D ](}t          j	        ||||f         |z            |
||f<   )||
fS )Nr      )
r   sizeshaper   log10zerosrangesearchsortedr	   sum)r   r   sigma_log10window_sigmapoints_per_sigmar   NN_FUNCSlog_xslog_thinned_xsys_smoothedi	cur_log_xlohismooth_weightsnormks                     r   _log_smooth_numbar9   D   sl    J Cr;1A#ABB
AhqkGXb\\FXj))N(GQ<((K1XX J J"1%	_VY|1K%KLL_VY|1K%KLL6"R%=(Q./!3kAKO
 
 !!##!88d"N7^^ J J$&F2aBh<.+H$I$IAqD!!{""r   c                     t          j                    t          j        |          } j        dk    rt          d          |j        dvrt          d           j        d         |j        d         k    rt          d          |j        dk    r|t           j        ddf         n|}t           ||||          \  }|r!t          j         fd	|D                       }|j        d         dk    r|d         n|}|S )
a1  
    Convolve a function or multiple functions with a gaussian kernel in the log space.

    Parameters
    ----------
    xs : 1D array
        The x-coordinates (function arguments) of the data points, must be increasing.
    ys : 1D or 2D array
        The y-coordinates (function values) of the data points.
        If 2D, rows correspond to multiple functions, columns correspond to different points.
    sigma_log10 : float, optional
        The standard deviation of the smoothing Gaussian kernel, applied over log10(xs), by default 0.1
    window_sigma : int, optional
        Width of the smoothing window, expressed in sigmas, by default 5
    points_per_sigma : int, optional
        If provided, smoothing is done only for `points_per_sigma` points per sigma and the
        rest of the values are interpolated (this results in a major speed-up). By default 10

    Returns
    -------
    xs_thinned : 1D array
        The subset of arguments, uniformly spaced in log-space.
    ys_smoothed : 1D or 2D array
        The gaussian-smoothed function values.

    r   zxs must be a 1D vector)r   r"   z3ys must be either a 1D vector or a "tall" 2D matrixr   r   z3xs and ys must have the same number of observationsNc                 2    g | ]}t          |          S  )r   ).0ys_rowr   r   s     r   
<listcomp>zlog_smooth.<locals>.<listcomp>   s%    KKKV[Z00KKKr   )r   asarrayndim
ValueErrorr$   newaxisr9   )r   r   r*   r+   r,   r1   r   s   `     @r   
log_smoothrD   j   s   B 
BB	BB	w!||1222	wfNOOO	x{bhrl""NOOO gllBJM		B/
B\+; J  
jKKKKK{KKK
 
 %'HQK1$4$4+a..+Kr    c                    |                      |d                                       |d         d|d         di                                          }t          ||d                  j                            t          j                  ||d                  j                            t          j                  ||d                  j                            t          j                  g|||          \  }}|||d         |z   <   |||d         |z   <   ||d         |z            ||d         |z            z  ||d         |z   <   |S )Ndistn_pixelsr)   
n_contacts)r*   r+   r,   output_prefix)groupbyaggreset_indexrD   valuesastyper   float64)	cvdr*   r+   r,   colssuffixcvd_smoothedsmoothed_balanced_sumsmoothed_n_valids	            r   _smooth_cvd_grouprW      sJ    	DL!!	Z %\"E

 

 
  /9T&\")00<<l+,3::2:FFj)*188DD	
  !)	/ 	/ 	/++ /?Lj!F*+0ELl#f,-T,'&01
tJ'&0
1	2 o&/0
 r   )r   )r   r    r   )NrE   )collections.abcr   numpyr   numbar   njitr   r9   rD   rW   r<   r   r   <module>r\      s    $ $ $ $ $ $       >        :  "# "# "# "#P 8 8 8 8x IK           r   