
    PiE                     .   d dl Z d dlZd dlZ	 d dlZn# e$ r e j        j        ZY nw xY wd dlZ		 d dl
mZmZmZmZ n# e$ r dZdZdZdZY nw xY w	 d dlZn# e$ r dZY nw xY wd e	j                    fd e	j                    fd e	j                    fd e	j                    fd e	j                    fd e	j                    fd	 e	j                    fd
 e	j                    fd e	j                    fd e	j                    fd e	j                    fgZd eD             Ze j                            de	j        e	j        e	j         e	j!        g          d             Z"d Z#d Z$d Z%e j                            de	j        e	j        e	j         e	j!        g          d             Z&e j                            de          d             Z'e j                            de          d             Z(e j                            de          d             Z)e j                            de          d             Z*e j                            de          d             Z+e j                            de          d             Z,e j                            de          d             Z-e j                            de	j        e	j        e	j         e	j!        g          e j                            de          d                         Z.e j        /                    e d          e j                            d eef          e j                            de          d!                                     Z0e j        /                    e d          e j                            d eef          e j                            de          d"                                     Z1e j        /                    e d#          e j                            de          d$                         Z2dS )%    N)	csr_array	coo_array
csr_matrix
coo_matrixi1i2i4i8u1u2u4u8f2f4f8c                 *    g | ]}|d          dk    |S )r   r    ).0pairs     t/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/pyarrow/tests/test_sparse_tensor.py
<listcomp>r   9   s(       $q'T//D///    sparse_tensor_typec                    t          j        g dg dg dg dg          }d}|                     ||          }|j        dk    sJ |j        dk    sJ |j        |j        k    sJ |j        sJ |                    d          |d         k    sJ |j        |k    sJ |j	        d	k    sJ t          j        |          } |            J ~ |            J d S )
N   r      r   r   r   r   r   r   r   r         r   r   r   r   r   r   r   r   r         xyr      r   r$   )nparrayfrom_dense_numpyndimsizeshape
is_mutabledim_name	dim_namesnon_zero_lengthweakrefref)r   datar1   sparse_tensorwrs        r   test_sparse_tensor_attrsr8   >   s)    8	  D I&77iHHM""""####$*,,,,####!!!$$	!4444"i////(A----	]	#	#B244244<<<<<r   c                     t          j        g dg          j        } t          j        g dg dg          j        }t          j        g dg dg dg dg          }t          j                            |          }t          j        |          }|                                \  }}|j	        sJ t          j        |          |dz   k    sJ d }t          j
        | |          sJ t          j
        ||          sJ |j        j        sJ d S )	Nr   r   r   r!   r#   r$   r   r      r   r!   r!   r   r   r   r   r#   r   r   r   r    r"   r   )r)   r*   TpaSparseCOOTensorr+   sysgetrefcountto_numpyhas_canonical_formatarray_equalflagsc_contiguous)expected_dataexpected_coordsr*   r6   nresult_dataresult_coordss          r   "test_sparse_coo_tensor_base_objectrM   \   sB   H0001224Mh    	  H	  E &77>>M&&A!.!7!7!9!9K----?=))QU2222M>-55555>/=99999++++++r   c                  4   t          j        g dg          j        } t          j        g d          }t          j        g d          }t          j        g dg dg dg dg          }t          j                            |          }t          j        |          }|                                \  }}}t          j        |          |dz   k    sJ d }t          j	        | |          sJ t          j	        ||          sJ t          j	        ||          sJ d S )	Nr:   r   r   r!   r#   r$   r=   r   r   r    r"   r!   )
r)   r*   r>   r?   SparseCSRMatrixr+   rA   rB   rC   rE   	r5   indptrindicesr*   r6   rJ   rK   result_indptrresult_indicess	            r   "test_sparse_csr_matrix_base_objectrV   t   s2   8'''())+DXooo&&Fh)))**GH	  E &77>>M&&A1>1G1G1I1I.K?=))QU2222M>$,,,,,>&-00000>'>2222222r   c                     t          j        g dg          j        } t          j        g d          g}t          j        g d          t          j        g d          g}t          j        g dg dg dg dg          }t          j                            |          }t          j        |          }|                                \  }}}t          j        |          |d	z   k    sJ d }t          j	        | |          sJ t          j	        |d
         |d
                   sJ t          j	        |d
         |d
                   sJ t          j	        |d         |d                   sJ d S )Nr:   rO   r   r<   r   r!   r=   r   r   r    r"   r#   r   r<   )
r)   r*   r>   r?   SparseCSFTensorr+   rA   rB   rC   rE   rQ   s	            r   "test_sparse_csf_tensor_base_objectrZ      s   8'''())+Dh''(F

###$$G H	  E &77>>M&&A1>1G1G1I1I.K?=))QU2222M>$,,,,,>&)]1%566666>'!*nQ&788888>'!*nQ&78888888r   c                    d }d }t           j                            dd          d d d d df         }|                     |          }|                     t          j        |                    } |||           |                                }d|d<   |                     t          j        |                    } |||           d S )Nc                 T    |                      |          sJ | |k    sJ | |k    rJ d S Nequalsabs     r   eqz%test_sparse_tensor_equals.<locals>.eq   s5    xx{{{AvvvvFFF|FFr   c                 T    |                      |          rJ | |k    rJ | |k    sJ d S r]   r^   r`   s     r   nez%test_sparse_tensor_equals.<locals>.ne   s5    88A;;FFF|Avvvvvvr   
   r$   r   g      ?)	   r   )r)   randomrandnr+   ascontiguousarraycopy)r   rc   re   r5   sparse_tensor1sparse_tensor2s         r   test_sparse_tensor_equalsrn      s      
  
 9??2q!!"""ccc'*D'88>>N'88
T""$ $NB~~&&&99;;DDJ'88
T""$ $NB~~&&&&&r   zdtype_str,arrow_typec                 n   t          j        |           }t          j        g dg          j                            |          }t          j        g dg dg          j        }t          j        g dg dg dg dg                              |          }t
          j                            |          }t
          j        	                    |          }t          |           |                                \  }}	|j        |k    sJ t          j        ||          sJ t          j        ||	          sJ t
          j                            |          }t          |           |                                \  }}	|j        |k    sJ t          j        ||          sJ t          j        ||	          sJ d S )Nr:   r;   r=   r   r   r    r"   )r)   dtyper*   r>   astyper?   Tensor
from_numpyr@   r+   reprrC   typerE   from_tensor)
	dtype_str
arrow_typerp   rH   rI   r*   tensorr6   rK   rL   s
             r   !test_sparse_coo_tensor_from_denserz      s   HYEH0001224;;EBBMh    	  H	  
 ve}} 
 Y!!%((F &77>>M!.!7!7!9!9K++++>-55555>/=99999 &226::M!.!7!7!9!9K++++>-55555>/=9999999r   c                    t          j        |           }t          j        g dg          j                            |          }t          j        g d          }t          j        g d          }t          j        g dg dg dg dg                              |          }t
          j                            |          }t
          j        	                    |          }t          |           |                                \  }	}
}|j        |k    sJ t          j        ||	          sJ t          j        ||
          sJ t          j        ||          sJ t
          j                            |          }t          |           |                                \  }	}
}|j        |k    sJ t          j        ||	          sJ t          j        ||
          sJ t          j        ||          sJ d S )Nr:   rO   r=   r   r   r    r"   )r)   rp   r*   r>   rq   r?   rr   rs   rP   r+   rt   rC   ru   rE   rv   rw   rx   rp   r5   rR   rS   r*   ry   r6   rK   rT   rU   s               r   !test_sparse_csr_matrix_from_denser}      s   HYE8'''())+22599DXooo&&Fh)))**GH	  
 ve}} 
 Y!!%((F &77>>M1>1G1G1I1I.K++++>$,,,,,>&-00000>'>22222 &226::M1>1G1G1I1I.K++++>$,,,,,>&-00000>'>2222222r   c                    t          j        |           }t          j        g dg          j                            |          }t          j        g d          g}t          j        g d          t          j        g d          g}t          j        g dg dg dg dg                              |          }t
          j                            |          }t          |           |	                                \  }}	}
|j
        |k    sJ t          j        ||          sJ t          j        |d	         |	d	                   sJ t          j        |d	         |
d	                   sJ t          j        |d
         |
d
                   sJ d S Nr:   rO   rX   r=   r   r   r    r"   r   r<   )r)   rp   r*   r>   rq   r?   rY   r+   rt   rC   ru   rE   )rw   rx   rp   r5   rR   rS   r*   r6   rK   rT   rU   s              r   'test_sparse_csf_tensor_from_dense_numpyr     s   HYE8'''())+22599Dh''(F

###$$G H	  
 ve}} 
 &77>>M1>1G1G1I1I.K++++>$,,,,,>&)]1%566666>'!*nQ&788888>'!*nQ&78888888r   c                 \   t          j        |           }t          j        g dg          j                            |          }t          j        g d          g}t          j        g d          t          j        g d          g}t          j        g dg dg dg dg                              |          }t
          j                            |          }t
          j        	                    |          }t          |           |                                \  }	}
}|j        |k    sJ t          j        ||	          sJ t          j        |d	         |
d	                   sJ t          j        |d	         |d	                   sJ t          j        |d
         |d
                   sJ d S r   )r)   rp   r*   r>   rq   r?   rr   rs   rY   rv   rt   rC   ru   rE   r|   s               r   (test_sparse_csf_tensor_from_dense_tensorr     s   HYE8'''())+22599Dh''(F

###$$G H	  
 ve}} 
 Y!!%((F &226::M1>1G1G1I1I.K++++>$,,,,,>&)]1%566666>'!*nQ&788888>'!*nQ&78888888r   c                    t          j        |           }t          j        g dg          j                            |          }t          j        g dg dg          j        }d}d}t
          j                            ||||          }t          |           |	                                \  }}	|j
        |k    sJ t          j        ||          sJ t          j        ||	          sJ |j        |k    sJ d S )Nr<   r   r!   r#   r   r$   r   r   r   r!   r<   r!   r   r   r   r#   r   r   r#   r$   r%   )r)   rp   r*   r>   rq   r?   r@   rs   rt   rC   ru   rE   r1   )
rw   rx   rp   r5   coordsr.   r1   r6   rK   rL   s
             r   &test_sparse_coo_tensor_numpy_roundtripr   8  s   HYE8'''())+22599DX   	  EI&11$2;= =M!.!7!7!9!9K++++>$,,,,,>&-00000"i//////r   c                 >   t          j        |           }t          j        g dg          j                            |          }t          j        g d          }t          j        g d          }d}d}t
          j                            |||||          }t          |           |	                                \  }	}
}|j
        |k    sJ t          j        ||	          sJ t          j        ||
          sJ t          j        ||          sJ |j        |k    sJ d S )Nr:   rO   r=   r   r%   )r)   rp   r*   r>   rq   r?   rP   rs   rt   rC   ru   rE   r1   )rw   rx   rp   r5   rR   rS   r.   r1   r6   rK   rT   rU   s               r   &test_sparse_csr_matrix_numpy_roundtripr   M  s/   HYE8'''())+22599DXooo&&Fh)))**GEI&11$27D DM1>1G1G1I1I.K++++>$,,,,,>&-00000>'>22222"i//////r   c                    t          j        |           }t          j        g dg          j                            |          }t          j        g d          g}t          j        g d          t          j        g d          g}d}d}d}t
          j                            ||||||          }	t          |	           |		                                \  }
}}|	j
        |k    sJ t          j        ||
          sJ t          j        |d         |d                   sJ t          j        |d         |d                   sJ t          j        |d	         |d	                   sJ |	j        |k    sJ d S )
Nr:   rO   rX   r=   )r   r<   r   r%   r   r<   )r)   rp   r*   r>   rq   r?   rY   rs   rt   rC   ru   rE   r1   )rw   rx   rp   r5   rR   rS   
axis_orderr.   r1   r6   rK   rT   rU   s                r   &test_sparse_csf_tensor_numpy_roundtripr   a  s   HYE8'''())+22599Dh''(F

###$$G JEI&11$272;= =M 	1>1G1G1I1I.K++++>$,,,,,>&)]1%566666>'!*nQ&788888>'!*nQ&788888"i//////r   c                    t          j        |           }t          j        g dg dg dg dg                              |          }d}|                    ||          }|                                }|                                }|j        |k    sJ |j        |k    sJ |j        |k    sJ t          j	        ||          sJ d S )N)r#   r   rg   r   )r      r   r   )r   r   r   r   )r   r   r   r   r%   )
r)   rp   r*   rq   r+   	to_tensorrC   ru   r1   rE   )	rw   rx   r   rp   r*   r1   r6   ry   result_arrays	            r   test_dense_to_sparse_tensorr   {  s     HYEHlll"ll"ll"ll$ % % &,VE]] 
 I&77yIIM$$&&F??$$L++++;*$$$$"i////>%.......r   zrequires scipy)reasonsparse_objectc                 &   d}d}t          j        |           }t          j        g d                              |          }t          j        g d          }t          j        g d          } ||||ff|          }	t          j                            |	|          }
|
                                }|	j        rJ |
j        rJ |j        rJ |
j	        |k    sJ |
j
        |k    sJ |	j        |j        k    sJ t          j        |	j        |j                  sJ t          j        |	j        |j                  sJ t          j        |	j        |j                  sJ |	                                }t          j        ||
                                                                          sJ |	                                 t          j                            |	|          }
|
                                }|	j        sJ |
j        sJ |j        sJ d S )Nr   r%   r   r   r   r.   r1   )r)   rp   r*   rq   r?   r@   
from_scipyto_scipyrD   ru   r1   rE   r5   rowcoltoarrayr   rC   sum_duplicates)rw   rx   r   r.   r1   rp   r5   r   r   scipy_matrixr6   out_scipy_matrixdense_arrays                r   &test_sparse_coo_tensor_scipy_roundtripr     sJ   
 EIHYE8&&&''..u55D
(%%%
&
&C
(%%%
&
&C !=$c
!35AAAL&11,<E 2 G GM$--//000011114444++++"i////!1!77777>,+-=-BCCCCC>,*,<,@AAAAA>,*,<,@AAAAA&&((K>+}'>'>'@'@'I'I'K'KLLLLL !!!&11,<E 2 G GM$--//,,,,----000000r   c                 &   d}d}t          j        |           }t          j        g d                              |          }t          j        g d          }t          j        g d          } ||||f|          }	t          j                            |	|          }
|
                                }|
j        |k    sJ |
j	        |k    sJ |	j        |j        k    sJ t          j
        |	j        |j                  sJ t          j
        |	j        |j                  sJ t          j
        |	j        |j                  sJ |	                                }t          j
        ||
                                                                          sJ d S )Nr   r%   r:   rO   r=   r   r   )r)   rp   r*   rq   r?   rP   r   r   ru   r1   rE   r5   rR   rS   r   r   rC   )rw   rx   r   r.   r1   rp   r5   rR   rS   sparse_arrayr6   out_sparse_arrayr   s                r   &test_sparse_csr_matrix_scipy_roundtripr     s   
 EIHYE8&&&''..u55DXooo&&Fh)))**G =$!8FFFL&11,<E 2 G GM$--//++++"i////!1!77777>,+-=-BCCCCC>,-/?/FGGGGG>,.0@0HIIIII&&((K>+}'>'>'@'@'I'I'K'KLLLLLLLr   zrequires pydata/sparsec                    t          j        |           }t          j        g d                              |          }t          j        g dg dg          }d}d}t	          j        |||          }t          j                            ||          }|	                                }	|j
        |k    sJ |j        |k    sJ |j        |	j        k    sJ t          j        |j        |	j                  sJ t          j        |j        |	j                  sJ t          j        |                                |                                                                          sJ d S )Nr   r   r   r   r%   )r5   r   r.   r   )r)   rp   r*   rq   sparseCOOr?   r@   from_pydata_sparseto_pydata_sparseru   r1   rE   r5   r   todenser   rC   )
rw   rx   rp   r5   r   r.   r1   r   r6   r   s
             r   .test_pydata_sparse_sparse_coo_tensor_roundtripr     s    HYE8&&&''..u55DX  F EI:4eDDDL&99,DM : O OM$5577++++"i////!1!77777>,+-=-BCCCCC>,-/?/FGGGGG>,..00'1133<<>>@ @ @ @ @ @ @r   )3pytestrA   r3   numpyr)   ImportErrormark
pytestmarkpyarrowr?   scipy.sparser   r   r   r   r   int8int16int32int64uint8uint16uint32uint64float16float32float64tensor_type_pairsscipy_type_pairsparametrizerP   SparseCSCMatrixr@   rY   r8   rM   rV   rZ   rn   rz   r}   r   r   r   r   r   r   skipifr   r   r   r   r   r   <module>r      s  $  



 # # # #"JJJ#    IIIIIIIIIIIII   JJIIII	MMMM   FFF
 
72799	828::	828::	828::	828::	929;;	929;;	929;;	:2:<<	:2:<<	:2:<<  &   
 -	0    0, , ,03 3 3*9 9 92 -	0  ' ' '. /1BCC: : DC:@ /1BCC3 3 DC3@ /1BCC9 9 DC94 /1BCC9 9 DC96 /1BCC0 0 DC0( /1BCC0 0 DC0& /1BCC0 0 DC02 -	0   /1BCC/ / DC /$ 
N+;<<9j*ABB/1ABB$1 $1 CB CB =<$1N 
N+;<<9j*ABB/1ABBM M CB CB =<M2 J'?@@/1BCC@ @ DC A@@ @ @s-    ''< AAA AA