
    z-Ph\D                     z   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 n# e$ r 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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          d                         Z-e j        ,                    e d          e j                            de          d                          Z.e j        ,                    e d!          e j                            de          d"                         Z/dS )#    N)
csr_matrix
coo_matrixi1i2i4i8u1u2u4u8f2f4f8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   datar(   sparse_tensorwrs        `/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyarrow/tests/test_sparse_tensor.pytest_sparse_tensor_attrsr0   7   s)    8	  D I&77iHHM""""####$*,,,,####!!!$$	!4444"i////(A----	]	#	#B244244<<<<<    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!   r-   nresult_dataresult_coordss          r/   "test_sparse_coo_tensor_base_objectrF   U   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++++++r1   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 )	Nr3   r   r   r   r   r   r6   r   r   r   r   r   )
r    r!   r7   r8   SparseCSRMatrixr"   r:   r;   r<   r>   	r,   indptrindicesr!   r-   rC   rD   result_indptrresult_indicess	            r/   "test_sparse_csr_matrix_base_objectrO   m   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2r1   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 )Nr3   rH   r   r5   r   r   r6   r   r   r   r   r   r   r5   )
r    r!   r7   r8   SparseCSFTensorr"   r:   r;   r<   r>   rJ   s	            r/   "test_sparse_csf_tensor_base_objectrS      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8r1   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   s6    xx{{AvvvvFFFFFFr1   c                 T    |                      |          rJ | |k    rJ | |k    sJ d S rV   rW   rY   s     r/   nez%test_sparse_tensor_equals.<locals>.ne   s6    88A;;FFFFAvvvvvvr1   
   r   r   g      ?)	   r   )r    randomrandnr"   ascontiguousarraycopy)r   r\   r^   r,   sparse_tensor1sparse_tensor2s         r/   test_sparse_tensor_equalsrg      s      
  
 9??2q!!"""ccc'*D'88>>N'88
T""$ $NB~~&&&99;;DDJ'88
T""$ $NB~~&&&&&r1   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 )Nr3   r4   r6   r   r   r   r   )r    dtyper!   r7   astyper8   Tensor
from_numpyr9   r"   reprr<   typer>   from_tensor)
	dtype_str
arrow_typeri   rA   rB   r!   tensorr-   rD   rE   s
             r/   !test_sparse_coo_tensor_from_densers      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9r1   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 )Nr3   rH   r6   r   r   r   r   )r    ri   r!   r7   rj   r8   rk   rl   rI   r"   rm   r<   rn   r>   ro   rp   rq   ri   r,   rK   rL   r!   rr   r-   rD   rM   rN   s               r/   !test_sparse_csr_matrix_from_denserv      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2r1   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 Nr3   rH   rQ   r6   r   r   r   r   r   r5   )r    ri   r!   r7   rj   r8   rR   r"   rm   r<   rn   r>   )rp   rq   ri   r,   rK   rL   r!   r-   rD   rM   rN   s              r/   'test_sparse_csf_tensor_from_dense_numpyry      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8r1   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 rx   )r    ri   r!   r7   rj   r8   rk   rl   rR   ro   rm   r<   rn   r>   ru   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8r1   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r5   r   r   r   r   r   r   r   r   r   r5   r   r   r   r   r   r   r   r   r   r   )r    ri   r!   r7   rj   r8   r9   rl   rm   r<   rn   r>   r(   )
rp   rq   ri   r,   coordsr%   r(   r-   rD   rE   s
             r/   &test_sparse_coo_tensor_numpy_roundtripr   1  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//////r1   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 )Nr3   rH   r6   r   r   )r    ri   r!   r7   rj   r8   rI   rl   rm   r<   rn   r>   r(   )rp   rq   ri   r,   rK   rL   r%   r(   r-   rD   rM   rN   s               r/   &test_sparse_csr_matrix_numpy_roundtripr   F  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//////r1   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 )
Nr3   rH   rQ   r6   )r   r5   r   r   r   r5   )r    ri   r!   r7   rj   r8   rR   rl   rm   r<   rn   r>   r(   )rp   rq   ri   r,   rK   rL   
axis_orderr%   r(   r-   rD   rM   rN   s                r/   &test_sparse_csf_tensor_numpy_roundtripr   Z  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//////r1   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   r`   r   )r      r   r   )r   r   r   r   )r   r   r   r   r   )
r    ri   r!   rj   r"   	to_tensorr<   rn   r(   r>   )	rp   rq   r   ri   r!   r(   r-   rr   result_arrays	            r/   test_dense_to_sparse_tensorr   t  s     HYEHlll"ll"ll"ll$ % % &,VE]] 
 I&77yIIM$$&&F??$$L++++;*$$$$"i////>%.......r1   zrequires scipy)reasonc                    t          j        |           }t          j        g d                              |          }t          j        g d          }t          j        g d          }d}d}t	          |||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 | dk    rO|                    t           j                                                                      t           j                  }n|                                }t          j        ||	                                                                          sJ |                                 t
          j                            ||          }	|	                                }
|j	        sJ |	j	        sJ |
j	        sJ d S )	Nr}   r~   r   r   r   r%   r(   r   )r    ri   r!   rj   r   r8   r9   
from_scipyto_scipyr=   rn   r(   r>   r,   rowcolfloat32toarrayfloat16r   r<   sum_duplicates)rp   rq   ri   r,   r   r   r%   r(   scipy_matrixr-   out_scipy_matrixdense_arrays               r/   &test_sparse_coo_tensor_scipy_roundtripr     s    HYE8&&&''..u55D
(%%%
&
&C
(%%%
&
&CEI tc3Z0>>>L&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D
++3355<<RZHH 	 #**,,>+}'>'>'@'@'I'I'K'KLLLLL !!!&11,<E 2 G GM$--//,,,,----000000r1   c                    t          j        |           }t          j        g d                              |          }t          j        g d          }t          j        g d          }d}d}t	          |||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 | dk    rO|                    t           j                                                                      t           j                  }n|                                }t          j        ||	                                                                          sJ d S )	Nr3   rH   r6   r   r   r   r   r   )r    ri   r!   rj   r   r8   rI   r   r   rn   r(   r>   r,   rK   rL   r   r   r   r   r<   )rp   rq   ri   r,   rK   rL   r%   r(   sparse_arrayr-   out_sparse_arrayr   s               r/   &test_sparse_csr_matrix_scipy_roundtripr     s    HYE8&&&''..u55DXooo&&Fh)))**GEItWf5UCCCL&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D
++3355<<RZHH 	 #**,,>+}'>'>'@'@'I'I'K'KLLLLLLLr1   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   )r,   r   r%   r   )r    ri   r!   rj   sparseCOOr8   r9   from_pydata_sparseto_pydata_sparsern   r(   r>   r,   r   todenser   r<   )
rp   rq   ri   r,   r   r%   r(   r   r-   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<<>>@ @ @ @ @ @ @r1   )0pytestr:   r*   numpyr    ImportErrormark
pytestmarkpyarrowr8   scipy.sparser   r   r   int8int16int32int64uint8uint16uint32uint64r   r   float64tensor_type_pairsparametrizerI   SparseCSCMatrixr9   rR   r0   rF   rO   rS   rg   rs   rv   ry   r{   r   r   r   r   skipifr   r   r    r1   r/   <module>r      sJ  $  



 # # # #"JJJ#    333333333   JJJJ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+;<</1BCC'1 '1 DC =<'1T 
N+;<</1BCCM M DC =<M8 J'?@@/1BCC@ @ DC A@@ @ @s-    ''8 	AAA AA