
    >ie                        d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ d Z	d Z
d Zd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdS )    )annotationsN)randomc                     t          d          t          j        d          } t          t	          j        | d                                                    }t          |          dk    sJ t          fd|D                       sJ d S )N      npartitions   kc              3      K   | ]}|v V  	d S N .0iseqs     :lib/python3.11/site-packages/dask/bag/tests/test_random.py	<genexpr>z.test_choices_size_exactly_k.<locals>.<genexpr>   '      $$AqCx$$$$$$    	rangedbfrom_sequencelistr   choicescomputelenallsutlir   s     @r   test_choices_size_exactly_kr$      s    
))C

3A
.
.
.C	fnSA&&&..00	1	1Br77a<<<<$$$$$$$$$$$$$$r   c                     t          d          t          j        d          } t          t	          j        | d                                                    }t          |          dk    sJ t          fd|D                       sJ d S )Nr   r      r   c              3      K   | ]}|v V  	d S r   r   r   s     r   r   z6test_choices_k_bigger_than_bag_size.<locals>.<genexpr>   r   r   r   r!   s     @r   #test_choices_k_bigger_than_bag_sizer(      s    
((C

3A
.
.
.C	fnSA&&&..00	1	1Br77a<<<<$$$$$$$$$$$$$$r   c                    t          d          t          j        d          } |                     d          } t	          t          j        | d                                                    }|                     t                                                    dk    sJ t          |          dk    sJ t          fd|D                       sJ d S )	N
   	   partition_sizer   r
   r   r+   r      c              3      K   | ]}|v V  	d S r   r   r   s     r   r   z/test_choices_empty_partition.<locals>.<genexpr>#   r   r   )r   r   r   repartitionr   r   r   r   map_partitionsr   r    r!   s     @r   test_choices_empty_partitionr3      s    
))C

3q
1
1
1C
//!

C	fnSA&&&..00	1	1Bc""**,,	9999r77a<<<<$$$$$$$$$$$$$$r   c                    t          d          t          j        d          } t          t	          j        | d                                                    }|                     t                                                    dk    sJ t          |          dk    sJ t          fd|D                       sJ d S )Nr*   r+   r,   r
   r   r+   r/   c              3      K   | ]}|v V  	d S r   r   r   s     r   r   zEtest_choices_k_bigger_than_smallest_partition_size.<locals>.<genexpr>,   r   r   
r   r   r   r   r   r   r   r2   r   r    r!   s     @r   2test_choices_k_bigger_than_smallest_partition_sizer8   &   s    
))C

3q
1
1
1C	fnSA&&&..00	1	1Bc""**,,6666r77a<<<<$$$$$$$$$$$$$$r   c                    t          d          t          j        d          } t          t	          j        | d                                                    }|                     t                                                    dk    sJ t          |          dk    sJ t          fd|D                       sJ d S )Nr*   r+   r,   r   r5   c              3      K   | ]}|v V  	d S r   r   r   s     r   r   zKtest_choices_k_equal_bag_size_with_unbalanced_partitions.<locals>.<genexpr>5   r   r   r7   r!   s     @r   8test_choices_k_equal_bag_size_with_unbalanced_partitionsr;   /   s    
))C

3q
1
1
1C	fnSB'''//11	2	2Bc""**,,6666r77b====$$$$$$$$$$$$$$r   c                    t          d          t          j        d          } t          t	          j        | dd                                                    }|                     t                                                    dk    sJ t          |          dk    sJ t          fd|D                       sJ d S )Nd   r*   r      r   split_every
r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   c              3      K   | ]}|v V  	d S r   r   r   s     r   r   z7test_choices_with_more_bag_partitons.<locals>.<genexpr>?   r   r   r7   r!   s     @r   $test_choices_with_more_bag_partitonsrC   8   s    
**C

3B
/
/
/C	fnSBA666>>@@	A	ABc""**,,0XXXXXr77b====$$$$$$$$$$$$$$r   c                    t          d          t          j        d          } t          t	          j        | dd                                                    }|                     t                                                    dk    sJ t          |          dk    sJ t          fd|D                       sJ t          t          |                    t          |          k    sJ d S )Nr=   r*   r   r>   r?   rA   c              3      K   | ]}|v V  	d S r   r   r   s     r   r   z6test_sample_with_more_bag_partitons.<locals>.<genexpr>I   r   r   r   r   r   r   r   sampler   r2   r   r    setr!   s     @r   #test_sample_with_more_bag_partitonsrI   B   s    
**C

3B
/
/
/C	fmC21555==??	@	@Bc""**,,0XXXXXr77b====$$$$$$$$$$$$s2ww<<3r77""""""r   c                    t          d          t          j        d          } t          t	          j        | d                                                    }|                     t                                                    dk    sJ t          |          dk    sJ t          fd|D                       sJ t          t          |                    t          |          k    sJ d S )Nr   r   r   r
   r   )   rK      c              3      K   | ]}|v V  	d S r   r   r   s     r   r   z-test_sample_size_exactly_k.<locals>.<genexpr>S   r   r   rF   r!   s     @r   test_sample_size_exactly_krN   M   s    
))C

3A
.
.
.C	fmC1%%%--//	0	0Bc""**,,	9999r77a<<<<$$$$$$$$$$$$s2ww<<3r77""""""r   c                    t          d          } t          j        | d          }t          j        t
          d          5  t          j        |d                                           d d d            d S # 1 swxY w Y   d S )Nr   r   zSample larger than population)matchr&   r   )	r   r   r   pytestraises
ValueErrorr   rG   r   r   r"   s     r   "test_sample_k_bigger_than_bag_sizerU   W   s    
((C

3A
.
.
.C	z)H	I	I	I * *cQ'')))* * * * * * * * * * * * * * * * * *s   )A77A;>A;c                    t          d          t          j        d          } |                     d          } t	          t          j        | d                                                    }|                     t                                                    dk    sJ t          |          dk    sJ t          fd|D                       sJ t          t          |                    t          |          k    sJ d S )	Nr*   r+   r,   r   r
   r   r.   c              3      K   | ]}|v V  	d S r   r   r   s     r   r   z.test_sample_empty_partition.<locals>.<genexpr>f   r   r   )r   r   r   r1   r   r   rG   r   r2   r   r    rH   r!   s     @r   test_sample_empty_partitionrX   _   s    
))C

3q
1
1
1C
//!

C	fmC1%%%--//	0	0Bc""**,,	9999r77a<<<<$$$$$$$$$$$$s2ww<<3r77""""""r   c                    t          d          t          j        d          } t          t	          j        | d                                                    }|                     t                                                    dk    sJ t          |          dk    sJ t          fd|D                       sJ t          t          |                    t          |          k    sJ d S )Nr*   r+   r,   r
   r   r5   c              3      K   | ]}|v V  	d S r   r   r   s     r   r   zItest_sample_size_k_bigger_than_smallest_partition_size.<locals>.<genexpr>p   r   r   rF   r!   s     @r   6test_sample_size_k_bigger_than_smallest_partition_sizer[   j   s    
))C

3q
1
1
1C	fmC1%%%--//	0	0Bc""**,,6666r77a<<<<$$$$$$$$$$$$s2ww<<3r77""""""r   c                    t          d          t          j        d          } t          t	          j        | d                                                    }|                     t                                                    dk    sJ t          |          dk    sJ t          fd|D                       sJ t          t          |                    t          |          k    sJ d S )Nr*   r+   r,   r   r5   c              3      K   | ]}|v V  	d S r   r   r   s     r   r   zJtest_sample_k_equal_bag_size_with_unbalanced_partitions.<locals>.<genexpr>z   r   r   rF   r!   s     @r   7test_sample_k_equal_bag_size_with_unbalanced_partitionsr^   t   s    
))C

3q
1
1
1C	fmC2&&&..00	1	1Bc""**,,6666r77b====$$$$$$$$$$$$s2ww<<3r77""""""r   c                     t          j        t          d          d          } t          j        | dd          }|                                }t          |          dk    sJ d S )Nr*   r   r,   r>   r
   r?   )r   r   r   r   rG   r   r   )bagbag2r   s      r   $test_sample_k_larger_than_partitionsrb   ~   sW    

599Q
7
7
7C=q111D
,,..Cs88q======r   c                     t          d          } g d}d}t          j        | ||          }t          t	          |                    |k    sJ d S )Nr&   ){Gz?Q?re   re   r   )
populationweightsr   )r   r   &_weighted_sampling_without_replacementr   rH   )rf   pr   sampleds       r   *test_weighted_sampling_without_replacementrk      sb    qJ   A	A;qA  G s7||!!!!!!r   c                     t          d          } t          j        | d          }t          t	          j        |d          t          j                  sJ d S )Nr   r   r   r
   r   )r   r   r   
isinstancer   rG   BagrT   s     r   test_sample_return_bagro      sO    
))C

3A
.
.
.CfmC1---rv6666666r   c                    t          j        ddgg ddgdgg          } t          j        g d          }t          j        |                                 |                                                                                              d          }t          j        ||                                          }t          |          dk    sJ d S )	Nr/   r
   )r   r&      rL   rK   )abcdr   r&   )
r   r   r   r   flattencountr   r1   zipr   )ABrr   Cs       r   $test_partitions_are_coerced_to_listsr|      s    
1a&)))aS1#677A
---..Aqyy{{aggii&7&7&9&9:::FFqIIA
q!Aq66Q;;;;;;r   c                    d\  } t          t          |                     }d t          |           D             }d t          |           D             }dt                    D ]X}t          j        |          \  }}|D ]}||xx         dz  cc<   t	          j        |          D ]}||xx         dz  cc<   Yfd|D             }fd|D             }t          j        d	t          ||          d
          sJ d S )Nr   r*   c                    g | ]}d S r   r   r   _s     r   
<listcomp>zDtest_reservoir_sample_map_partitions_correctness.<locals>.<listcomp>       (((!A(((r   c                    g | ]}d S r   r   r   s     r   r   zDtest_reservoir_sample_map_partitions_correctness.<locals>.<listcomp>       1111Q111r     r/   r   c                     g | ]
}|z  z  S r   r   r   rt   r   repss     r   r   zDtest_reservoir_sample_map_partitions_correctness.<locals>.<listcomp>   !    999qAN999r   c                     g | ]
}|z  z  S r   r   r   s     r   r   zDtest_reservoir_sample_map_partitions_correctness.<locals>.<listcomp>   !    KKKQ$(^KKKr           rd   abs_tol)	r   r   r   _sample_map_partitionsrndrG   mathisclosebhattacharyya	Nr   distributionexpected_distributionr   pickspickr   r   s	          @@r   0test_reservoir_sample_map_partitions_correctnessr      sn   DAq
uQxx..C((uQxx(((L11a111D4[[ - -0a88q 	$ 	$D!#Jsa((( 	- 	-D!$'''1,''''	- :9999L999LKKKKK5JKKK <]<)>??       r   c                    d\  } t          t          |                     }d t          |           D             }d t          |           D             }dt                    D ]X}t          j        |          \  }}|D ]}||xx         dz  cc<   t	          j        |          D ]}||xx         dz  cc<   Yfd|D             }fd|D             }t          j        d	t          ||          d
          sJ d S )Nr~   c                    g | ]}d S r   r   r   s     r   r   zUtest_reservoir_sample_with_replacement_map_partitions_correctness.<locals>.<listcomp>   r   r   c                    g | ]}d S r   r   r   s     r   r   zUtest_reservoir_sample_with_replacement_map_partitions_correctness.<locals>.<listcomp>   r   r   r   r/   r   c                     g | ]
}|z  z  S r   r   r   s     r   r   zUtest_reservoir_sample_with_replacement_map_partitions_correctness.<locals>.<listcomp>   r   r   c                     g | ]
}|z  z  S r   r   r   s     r   r   zUtest_reservoir_sample_with_replacement_map_partitions_correctness.<locals>.<listcomp>   r   r   r   rd   r   )	r   r   r   '_sample_with_replacement_map_partitionsr   r   r   r   r   r   s	          @@r   Atest_reservoir_sample_with_replacement_map_partitions_correctnessr      sn   DAq
uQxx..C((uQxx(((L11a111D4[[ - -A#qIIq 	$ 	$D!#Kq))) 	- 	-D!$'''1,''''	- :9999L999LKKKKK5JKKK <]<)>??       r   c                V    dt          d t          | |          D                       z
  S )Nr/   c              3  J   K   | ]\  }}t          j        ||z            V  d S r   )r   sqrt)r   hihjs      r   r   z bhattacharyya.<locals>.<genexpr>   s4      @@&"b49R"W%%@@@@@@r   )sumrx   )h1h2s     r   r   r      s,    s@@CBKK@@@@@@@r   )
__future__r   r   r   r   rQ   dask.bagr`   r   r$   r(   r3   r8   r;   rC   rI   rN   rU   rX   r[   r^   rb   rk   ro   r|   r   r   r   r   r   r   <module>r      s   " " " " " "                  % % %% % %% % %% % %% % %% % %# # ## # #* * *# # ## # ## # #  " " "7 7 7    .  .A A A A Ar   