o
    Uݢg                     @  s   d Z ddlmZ ddlmZ ddlZedddZedddZdddZe	dd ddZe	dd!ddZ	dd"ddZdS )#zJFunctions for fast, memory-efficient operations on masked sparse matrices.    )annotations)overloadNrow_mask#np.ndarray[int, np.dtype[np.bool_]]col_maskaxisNonereturn
np.genericc                 C     d S N matrixr   r   r   r   r   `/oak/stanford/groups/akundaje/marinovg/programs/cellranger-9.0.1/lib/python/cellranger/sparse.py
sum_masked      r   int%np.ndarray[int, np.dtype[np.generic]]c                 C  r   r   r   r   r   r   r   r      r   
int | Nonec                 C  s   |   dksJ t|| jd ksJ t|| jd ksJ |jdks%J |jdks,J |dkr;| jdd|| S |dksC|du r[| || }|dkrP|S |du sVJ t|S td)	a"  Sum a masked sparse matrix along an axis.

    Args:
      matrix (scipy.sparse.csc_matrix): Matrix
      axis (int): Axis to sum away; None for total sum.
      row_mask (np.array): Boolean mask for rows
      col_mask (np.array): Boolean mask for columns

    Returns:
      np.array
    cscr      boolFcopyNzaxis must be 0, 1, or None)		getformatlenshapedtype	transposedotnpsum
ValueError)r   r   r   r   row_sumsr   r   r   r   "   s   
@   	thresholdchunk_size_mb	np.uint64c                 C  r   r   r   mr   r   r&   r   r'   r   r   r   count_ge_maskedH      r+   $np.ndarray[int, np.dtype[np.uint64]]c                 C  r   r   r   r)   r   r   r   r+   S   r,   c                 C  s  |dksJ |   dksJ t|| jd ksJ t|| jd ks$J |jdks+J |jdks2J | jjj}| jjj}|| }t| j	}	|	| }
|d }dg}d}t
t|
D ]}||
| 7 }||krl|| d}qYg }|dvrwtdt|D ]D\}}|t|d k r|d|  n| jd }t||}| dd|f |ktj}|dkr|jd	d
|}n||| }|| q{|dkrt|| S |dkrt|| S |du rtt|| S td)a  Count values greater than or equal to a threshold in a masked sparse matrix.

    Args:
      m (scipy.sparse.csc_matrix): Matrix
      axis (int): Axis to sum away; None for total count.
      threshold (int): Only count elements exceeding this threshold
      row_mask (np.array): Boolean mask for rows
      col_mask (np.array): Boolean mask for columns
      chunk_size_mb (int): Operate on chunks of this size; don't copy the entire matrix at once.

    Returns:
      np.array
    r   r   r   r   i   )r   r   NzAxis must be 0, 1, or NoneNFr   )r   r   r   r   indicesitemsizedatar!   diffindptrrangeappendr#   	enumeratesliceastypeuint64r   r    concatenater"   )r*   r   r   r&   r   r'   Z	ind_bytesZ
data_bytesZbytes_per_nz_elemZnz_elem_per_colZbytes_per_colZchunk_size_bytesZchunk_starts
chunk_sizeiresultsZ	chunk_idxZchunk_start	chunk_endchunkZ	submatrixsr   r   r   r+   ^   sN   


$
)r   r   r   r   r   r   r	   r
   )r   r   r   r   r   r   r	   r   )r   r   r   r   r   r   )r%   )r   r   r   r   r&   r   r   r   r'   r   r	   r(   )r   r   r   r   r&   r   r   r   r'   r   r	   r-   )
r   r   r   r   r&   r   r   r   r'   r   )	__doc__
__future__r   typingr   numpyr!   r   r+   r   r   r   r   <module>   s"   
&
