
    ՗i}                         S r SSKrSSKrSSKrSSKrSSKJr  SSKJ	r	J
r
JrJrJrJr  \R                  " \5      r SSKrSr " S S5      rg! \ a    Sr Nf = f)	z'Co-location pattern mining core module.    N)defaultdict)DictListSetTupleAnyOptionalTFc                   f   \ rS rSrSrS)S\S\S\4S jjrS\S\	\
\\4      4S jrS\
\\4   4S	 jrS
 rS\
S\
S\4S jrS\
\\	\	\      4   4S jrS\\   S\
\\	\	\      4   S\4S jrS\S\S\4S jrS\
\\	\	\      4   S\
\\	\	\      4   4S jr   S*S\S\S\S\	\
\\4      4S jjrS\	\	\      S\4S jrS\\   S\\   S\\   S\	\	\      S\4
S  jrS\\   S\
\\	\	\      4   4S! jrS\\   S\\   S\S\	\	\      S\4
S" jr  S+S#\	\
\\4      S$\S%\S\	\
\\4      4S& jjrS'rg(),CoLocationMiner   z4Co-location pattern miner using participation index.	data_pathdistance_thresholduse_gpuc                 "   X l         U R                  U5      U l        U R                  5       U l        0 U l        U R                   H  nUS    US    3nX@R
                  U'   M     U=(       a    [        U l        U R                  (       aa  [        R                  R                  5       (       a>  SU l        [        R                  S[        R                  R                  S5       S35        OKSU l        U(       a!  [        (       d  [        R                  S5        OU(       a  [        R                  S	5        S
U l        U R#                  5         [        R                  S[%        U R                  5       S[%        U R                  5       S35        g
)zInitialize miner with data.

Args:
    data_path: Path to JSON data file
    distance_threshold: Distance threshold for neighbor relationship
    use_gpu: Whether to use GPU for distance calculations (if available)
typeidcudaz5GPU acceleration enabled for pattern mining (device: r   )cpuz0PyTorch not available, GPU acceleration disabledzCUDA not available, using CPUNzLoaded z instances with z	 features)r   
_load_datadata_count_featuresfeature_counts_instance_mapTORCH_AVAILABLEr   torchr   is_availabledeviceloggerinfoget_device_namewarning_coords_build_coords_arraylen)selfr   r   r   iteminst_strs         =   /home/ubuntu/codebase/yexijia/保研/iCoLoc/src/core/miner.py__init__CoLocationMiner.__init__   s0    #5OOI.	"224IIDv,T
|4H+/x( 
 2?<<EJJ3355 DKKKOPUPZPZPjPjklPmOnnopqDKQR>?   "gc$))n--=c$BUBU>V=WW`ab    returnc                     [        USSS9 n[        R                  " U5      nSSS5        W H#  nSU;  d  SU;  d  SU;  d  M  [        SU 35      e   U$ ! , (       d  f       N9= f)	zkLoad data from JSON file.

Args:
    data_path: Path to data file
    
Returns:
    List of data instances
rzutf-8)encodingNr   xyzInvalid data format: )openjsonload
ValueError)r&   r   fr   r'   s        r)   r   CoLocationMiner._load_data:   sg     )S73q99Q<D 4 DT!S_4 #8!?@@   43s   A
A$c                 z    [        [        5      nU R                   H  nXS   ==   S-  ss'   M     [        U5      $ )z^Count instances for each feature type.

Returns:
    Dictionary mapping feature type to count
r      )r   intr   dict)r&   countsr'   s      r)   r   CoLocationMiner._count_featuresM   s8     S!IID< A%  F|r,   c                    [        U R                  5      n[        R                  " US4[        R                  S9U l        [        U R                  5       H-  u  p#US   U R
                  US4'   US   U R
                  US4'   M/     U R                  (       aJ  [        (       a>  [        R                  " U R                  [        R                  U R                  S9U l        ggg)	z7Build coordinate arrays for batch distance calculation.   dtyper1   r   r2   r:   )rB   r   N)r%   r   npzerosfloat32r#   	enumerater   r   r   tensorr   r   _distance_threshold_gpu)r&   nir'   s       r)   r$   #CoLocationMiner._build_coords_arrayX   s    		NxxAbjj9 +GA!%cDLLA!%cDLLA ,
 <<OO+0<<8O8OW\WdWdmqmxmx+yD( ,<r,   	instance1	instance2c                     [         R                  " US   US   -
  S-  US   US   -
  S-  -   5      nX0R                  :*  $ )zCheck if two instances are neighbors.

Args:
    instance1: First instance
    instance2: Second instance
    
Returns:
    True if instances are neighbors
r1   r@   r2   )mathsqrtr   )r&   rL   rM   distances       r)   _is_neighborCoLocationMiner._is_neighbord   sR     99s^in,2s^in,23
 2222r,   c           
         SSK nUR                  5       n[        R                  S5        [        [        5      n[        U R                  5      n[        [        5      n[        U R                  5       H  u  pgXWS      R                  U5        M     [        R                  S[        U5       S35        [        UR                  5       5      n/ n	[        [        U5      5       H9  n[        US-   [        U5      5       H  n
U	R                  X   X   45        M     M;     [        R                  S[        U	5       S	35        SnU	 GH  u  pX\   nX]   nU(       a  U(       d  M  U R                  U   nU R                  U   nU R                  (       a  [        (       a  [        R                   " U5      R#                  U R$                  5      n[        R                   " U5      R#                  U R$                  5      nUR'                  S5      UR'                  S5      -
  n[        R(                  " [        R*                  " US
-  S
S95      nUU R,                  :*  n[        R.                  " USS9R1                  5       R3                  5       nOUSS2[4        R6                  SS24   U[4        R6                  SS2SS24   -
  n[4        R(                  " [4        R*                  " US
-  S
S95      nUU R8                  :*  n[4        R:                  " [4        R<                  " U5      5      n[        X/5      nUS    SUS    3nU H  u  nnUU   nUU   nU R                  U   nU R                  U   nUS   US   :X  a  US    US    3US    US    3/n OUS    US    3US    US    3/n UU   R                  U 5        M     US-  nUS-  S:X  d  GM  UR                  5       U-
  n![        R                  SU S[        U	5       SU!S S35        GM     UR                  5       U-
  n![+        S UR?                  5        5       5      n"[        R                  S[        U5       SU" SU!S S35        [A        U5      $ )a8  Create 2-order table instances using optimized batch computation.

Uses spatial indexing and batch distance calculation for performance.
Merges patterns with same features but different order (e.g., "a,b" and "b,a").

Returns:
    Dictionary mapping pattern to table instances (pattern is sorted alphabetically)
r   NzCStarting 2-order table instance creation with batch optimization...r   zGrouped instances by type: z typesr:   zProcessing z type pairs...r@   dimF)as_tupleaxis,r   
   
Processed /z type pairs (.1f
s elapsed)c              3   8   #    U  H  n[        U5      v   M     g 7fNr%   .0vs     r)   	<genexpr><CoLocationMiner._create_table_instances_2.<locals>.<genexpr>   s     G.Fc!ff.F   Created ! candidate 2-order patterns with z total instances in .2fs)!timer   r    r   listr%   r   rF   appendsortedkeysranger#   r   r   r   
from_numpytor   	unsqueezerP   sumrH   nonzeror   numpyrC   newaxisr   column_stackwherevaluesr<   )#r&   rm   
start_timetable_instancesrI   type_indicesrJ   r'   types
type_pairsjprocessed_pairstype1type2indices1indices2coords1coords2coords1_gpucoords2_gpudiff	distancesneighbor_maskneighbor_indicespattern_typespatternidx1_posidx2_posi1i2inst1inst2instanceelapsedtotal_instancess#                                      r)   _create_table_instances_2)CoLocationMiner._create_table_instances_2t   s@    	YY[
YZ%d+		N #4( +GAf&--a0 , 	1#l2C1DFKL |((*+
s5z"A1q5#e*-!!58UX"67 . # 	k#j/!2.AB &LE#*H#*H8 ll8,Gll8,G||#..w7::4;;G#..w7::4;;G #,,Q/+2G2G2JJ!JJuyy'BC	 !*T-I-I I#(==#O#S#S#U#[#[#]  q"**a/072::q!;K3LLGGBFF4191$=>	 !*T-D-D D#%??288M3J#K  #E>2M&q)*!M!,<+=>G '7"(h'h'		"		" =M!$44#(=/%+ ?E&M?SXY]S^R_A`aH#(=/%+ ?E&M?SXY]S^R_A`aH(//9 '7 q O#q())+
2j(93z?:K=Y`adXeeopqq 't ))+
*Go.D.D.FGGhs?344UVeUffz  |C  DG  {H  HI  J  	KO$$r,   r   r~   c                    U(       d  g0 nU H  n[        5       nUR                  5        HV  u  pgXFR                  S5      ;   d  M  U H5  nU H,  n	U	R                  U5      (       d  M  UR	                  U	5        M.     M7     MX     U R
                  R                  US5      n
U
S:  a  [        U5      U
-  OSX4'   M     U(       a  [        UR                  5       5      $ S$ )zCalculate participation index for a pattern.

Args:
    pattern: Set of feature types in the pattern
    table_instances: Table instances for the pattern (key is pattern_str, value is list of instances)
    
Returns:
    Participation index value
        rZ   r:   r   )
setitemssplit
startswithaddr   getr%   minr|   )r&   r   r~   participation_ratesfeatureparticipating_instancespattern_str	instancesr   r(   r   s              r)   _calculate_participation_index.CoLocationMiner._calculate_participation_index   s      !G '*e# +:*?*?*A&//44$-(0H'227;; 7 ; ;H E )1 %.	 +B #1155gqAO]lop]p3/F+G/+Yvy(' , 5Hs&--/0PSPr,   	inst1_str	inst2_strc                     U R                   R                  U5      nU R                   R                  U5      nUb  Uc  gU R                  X45      $ )a  Check if two instances are neighbors by directly calculating distance.

Args:
    inst1_str: First instance string (e.g., "Restaurant1")
    inst2_str: Second instance string (e.g., "Cafe2")
    
Returns:
    True if the two instances are neighbors (distance <= threshold)
F)r   r   rR   )r&   r   r   r   r   s        r)   _are_instances_neighbors(CoLocationMiner._are_instances_neighbors   sO     ""&&y1""&&y1=EM   ..r,   k_minus_1_patternsc                   ^ ^$^% SSK nUR                  5       n[        R                  S[        U5       S35        [	        [
        5      m$[        UR                  5       5      n[	        [
        5      m%U HN  nUR                  S5      n[        U5      S:  d  M%  SR                  USS 5      nT%U   R                  XV45        MP     [        R                  S[        T%5       S	35        Sn[        U%4S
 jT% 5       5      n	[        R                  SU	 35        Sn
/ nSnSnU$U 4S jnT%R                  5        GHq  u  pv[        U5      S:  a  M  [        [        U5      S-
  5       GH>  nXo   u  nnUU   n[        US-   [        U5      5       GH  nUU   u  nnUS   US   :w  d  M  SR                  UUS   /-   5      nUU   nSn[        U5      [        U5      -  U:  a  SSKn[        [         R"                  " U5      5      n[        U5      U:  a  UR%                  UU5      n[        U5      U:  a  UR%                  UU5      n[        R'                  SU SU 35        [	        [
        5      nU H6  n[        U5      S:  d  M  [)        USS 5      nUU   R                  U5        M8     U H  n[        U5      S:  d  M  [)        USS 5      nUU;   d  M*  UU    Hr  nUS   nUS   n UR                  UUUUU S.5        [        U5      U
:  d  M5  UR                  5       n!U" U5        XR                  5       U!-
  -  nU[        U5      -  n/ nMt     M     US-  nUS-  S:X  d  GM  UR                  5       U-
  n"[        R                  SU SU	 SU SU"S S3	5        GM     GMA     GMt     U(       a;  UR                  5       n!U" U5        XR                  5       U!-
  -  nU[        U5      -  nUR                  5       U-
  n"[        S T$R+                  5        5       5      n#[        R                  SU SUS S35        [        R                  S[        T$5       S U# S!U"S S"US S#3	5        [-        T$5      $ )$zGenerate k-order patterns from (k-1)-order patterns using optimized batch computation.

Args:
    k_minus_1_patterns: (k-1)-order table instances
    
Returns:
    k-order table instances
r   Nz!Generating k-order patterns from z (k-1)-order patterns...rZ   r:   zBuilt prefix index with z	 prefixesc              3   j   >#    U  H(  n[        TU   5      [        TU   5      S -
  -  S-  v   M*     g7f)r:   r@   Nrb   )rd   pprefix_indexs     r)   rf   =CoLocationMiner._generate_k_order_patterns.<locals>.<genexpr>-  s5     gZfUV#l1o.#l1o2F2JKqPZfs   03z Total pattern pairs to process: i c                 l  > U (       d  gU  Vs/ s H  oS   PM	     nnU  Vs/ s H  oS   PM	     nnU Vs/ s H  nTR                   R                  U5      PM      nnU Vs/ s H  nTR                   R                  U5      PM      nnTR                  (       Gad  [        (       GaX  [	        U 5      S:  GaH  [
        R                  " U Vs/ s H  ow(       a
  US   US   /OSS/PM     sn[
        R                  S9n[
        R                  " U Vs/ s H  ow(       a
  US   US   /OSS/PM     sn[
        R                  S9n	[        R                  " U5      R                  TR                  5      n
[        R                  " U	5      R                  TR                  5      nX-
  n[        R                  " [        R                  " US	-  S
S95      nUTR                  :*  nUR                  5       R!                  5       nO[
        R                  " U Vs/ s H  ow(       a
  US   US   /OSS/PM     sn[
        R                  S9n[
        R                  " U Vs/ s H  ow(       a
  US   US   /OSS/PM     sn[
        R                  S9n	X-
  n[
        R                  " [
        R                  " US	-  S
S95      nUTR"                  :*  n[%        X5       H5  u  nnU(       d  M  US   US   S   /-   nTUS      R'                  U5        M7     gs  snf s  snf s  snf s  snf s  snf s  snf s  snf s  snf )u   处理一批邻近关系检查Nlast1last2i  r1   r2   r   rA   r@   r:   rU   rX   r   r   r   r   )r   r   r   r   r%   rC   arrayrE   r   rs   rt   r   rP   rv   rH   r   rx   r   zipro   )batch_checkscheck
last1_strs
last2_strsrl   
inst1_objs
inst2_objsobjr   r   r   r   r   r   r   neighbor_resultsis_neighbornew_instance
k_patternsr&   s                     r)   process_batchACoLocationMiner._generate_k_order_patterns.<locals>.process_batch6  s    7CClU.lJC6BClU.lJC >HHZ$,,003ZJH=GHZ$,,003ZJH |||C4E4L((Wa#bWaPSCSXs3x$8aV$KWa#bjljtjtu((Wa#bWaPSCSXs3x$8aV$KWa#bjljtjtu#..w7::4;;G#..w7::4;;G"0!JJuyy'BC	 )T-I-I I#0#4#4#6#<#<#>  ((Wa#bWaPSCSXs3x$8aV$KWa#bjljtjtu((Wa#bWaPSCSXs3x$8aV$KWa#bjljtjtu(GGBFF4191$=>	#,0G0G#G  '*,&I"{;#(>U7^B5G4H#HLuY/077E 'J= DC IH
 $c#b $c#bs-   LL%L!%L
L"L'L,L1r@   iP  z%Sampling instances for pattern pair: z x )r   r   r   r   r   2   r\   r]   z pattern pairs, checked z neighbors (r^   r_   c              3   8   #    U  H  n[        U5      v   M     g 7fra   rb   rc   s     r)   rf   r     s     B.Ac!ff.Arh   zTotal neighbor checks: z, batch check time: rk   rl   
Generated z k-order patterns with z instances in zs (batch check: zs))rm   r   r    r%   r   rn   rp   rq   r   joinro   rv   r   rr   randomr;   rC   rP   sampledebugtupler|   r<   )&r&   r   rm   r}   pattern_keyspattern_keypattern_listprefixr   total_pairs
batch_sizecurrent_batchtotal_checksbatch_check_timer   rJ   pattern1_keypattern1_list
inst1_listr   pattern2_keypattern2_listnew_pattern
inst2_listmax_instances_per_pairr   sample_sizeinst1_prefix_mapr   inst_prefixr   last_inst1_strlast_inst2_strbatch_startr   r   r   r   s&   `                                   @@r)   _generate_k_order_patterns*CoLocationMiner._generate_k_order_patterns  s    	YY[
7<N8O7PPhij &
05578 #4('K&,,S1L< 1$,s"34V$++[,GH	 ( 	.s</@.AKLgZfgg6{mDE 
'	FT %1$6$6$8 F< 1$ 3|,q01.:o+m/=
q1uc,&78A2>q//L- %R(M",==&)hh}b@Q?R/R&S%7%E
 27.z?S_<?UU)*-bgg6L.M*NK":<-3]]:{-S
":<-3]]:{-S
"LL+PQ]P^^abnao)pq ,7t+<(%/E"5zA~.3E#2J.? 0 = D DU K &0 &0E"5zA~.3E#2J.?#.2B#B1A+1N9>r9>r(5(<(<7B5:5:5C5C>* )+ ,/}+=+K:>))+K,9-,H,<		k@Y,Y,<,8C<N,NL<>M% 2O &02 (1,*R/14&*iikJ&>G"KK*_4EQ{mSklxky  zF  GN  OR  FS  S]  )^  _u 9	 2 %9L ))+K-(		k 99C..L))+
*Bj.?.?.ABB-l^;OP`adOeefghjZ 11HHYYghopsgt  uE  FV  WZ  E[  []  ^  	_Jr,   
confidencemin_participationmax_pattern_sizepriorityc                    [         R                  SU SU 35        / nU R                  5       n[         R                  S[        U5       S[	        S UR                  5        5       5       S35        SnXb::  Gaf  U(       Ga^  [         R                  SU S	[        U5       S
35        0 n/ nUR                  5        H  u  p[        U	R                  S5      5      nU R                  XU
05      nUR                  X[        U
5      45        X:  d  MU  XU	'   UnUR                  [        U5      U	UUU[        U
5      U
U
SS S.5        M     U(       ad  UR                  S SS9  [         R                  SU S35        USS  H1  u  pnX:  a  SOSn[         R                  SU SU	 SUS SU 35        M3     Xb:  a  U R                  U5      nUS-  nOOXb::  a
  U(       a  GM^  US:X  a  UR                  S SS9  O/US:X  a  UR                  S SS9  OUS :X  a  UR                  S! SS9  [         R                  S"[        U5       S#35        U$ )$a  Mine co-location patterns.

Args:
    min_participation: Minimum participation index threshold
    max_pattern_size: Maximum pattern size
    priority: Priority for sorting ("confidence", "participation", "size")
    
Returns:
    List of discovered patterns with metadata
z'Mining patterns with min_participation=z, max_pattern_size=ri   rj   c              3   8   #    U  H  n[        U5      v   M     g 7fra   rb   rc   s     r)   rf   0CoLocationMiner.mine_patterns.<locals>.<genexpr>  s)       [F  lEfg[^_`[a[a  lErh   z total instancesr@   zMining z-order patterns, found z candidatesrZ   N   )r   r   sizeparticipation_indexr   instance_countr~   table_instances_samplec                     U S   $ )Nr:    r1   s    r)   <lambda>/CoLocationMiner.mine_patterns.<locals>.<lambda>  s    qtr,   Tkeyreversez+Top 5 patterns by participation (threshold=z):u   ✓u   ✗z   z: participation=z.4fz, instances=r:   r   c                     U S   $ Nr   r   r   s    r)   r   r     s    AlOr,   participationc                     U S   $ )Nr   r   r   s    r)   r   r     s
    A.C,Dr,   r   c                     U S   $ )Nr   r   r   s    r)   r   r     s    AfIr,   zFound z valid patterns)r   r    r   r%   rv   r|   r   r   r   r   ro   rp   sortr   )r&   r   r   r   all_patternscurrent_patternskvalid_patternsparticipation_statsr   r   pattern_setr  r   countstatuss                   r)   mine_patternsCoLocationMiner.mine_patterns  s    	=>O=P Q''7&8: 	; 99;hs#3455VWZ  [Fk{  lC  lC  lE  [F  XF  WG  GW  X  	Y #(8KK'!$;C@P<Q;RR]^_  N"$*:*@*@*B&!+"3"3C"89 $ C CK_hQi j#**KI+WX 52;;/ "/J ''#)+#6'2 !/<&0*-i.+42;BQ-	) 	 +C. ##((^T(JIJ[I\\^_`9LRa9P5K&3&HUeFKK"VHAk]:J=Y\J]]ijoip qr :Q
 ##'#B#B>#R QS #(8(8X |#";TJ("DdS"5tDfS./?@r,   pattern_table_instancesc                     U(       d  g[        U5      n[        U R                  5      [        U R                  5      S-
  -  S-  nUS:  a  X#-  $ S$ )zCalculate support for a pattern.

Support = number of table instances / total possible neighbor pairs

Args:
    pattern_table_instances: Table instances for the pattern
    
Returns:
    Support value (0.0 to 1.0)
r   r:   r@   r   )r%   r   )r&   r  r   r   s       r)   _calculate_support"CoLocationMiner._calculate_support  sQ     ' 56 $))nDII(:;a?0;a,HSHr,   
antecedent
consequentc                   ^  U(       d  g[        5       nU H>  nU H5  nU H,  nUR                  U5      (       d  M  UR                  U5        M.     M7     M@     [        U 4S jU 5       5      n	U	S:X  a  g[	        U5      U	-  n
[        U
S5      $ )u  Calculate confidence for a rule: antecedent → consequent.

For co-location rules, confidence = P(consequent near antecedent | antecedent exists)
= |instances where antecedent and consequent co-occur| / |instances of antecedent|

Args:
    antecedent: Left side of the rule (条件)
    consequent: Right side of the rule (结果)
    pattern: Full pattern containing both antecedent and consequent
    pattern_table_instances: Table instances for the full pattern
    
Returns:
    Confidence value (0.0 to 1.0)
r   c              3   \   >#    U  H!  nTR                   R                  US 5      v   M#     g7fr   Nr   r   rd   r7   r&   s     r)   rf   =CoLocationMiner._calculate_rule_confidence.<locals>.<genexpr>3  '     Qjt2266q!<<j   ),r         ?)r   r   r   rv   r%   r   )r&   r  r  r   r  antecedent_instances_in_patternr   r(   r   antecedent_totalr   s   `          r)   _calculate_rule_confidence*CoLocationMiner._calculate_rule_confidence  s      ' +.%'/H$)G**7337;;HE  * % 0 QjQQq  89<LL
:s##r,   c                     [        U5      S:X  a  0 $ [        U5      S:X  a7  SR                  [        U5      5      nU R                  5       nX#;   a  X#U   0$ 0 $ 0 $ )zGet table instances for a given pattern.

Args:
    pattern: Set of feature types
    
Returns:
    Dictionary mapping pattern string to table instances
r:   r@   rZ   )r%   r   rp   r   )r&   r   r   all_2_orders       r)   _get_pattern_table_instances,CoLocationMiner._get_pattern_table_instances=  sc     w<1I w<1((6'?3K88:K)#%=>>I 	r,   c                    ^  [        U 4S jU 5       5      n[        T R                  5      nUS:X  d  US:X  a  gXV-  nUS:X  a  gX7-  nU$ )a  Calculate lift for a rule.

Lift = confidence / P(consequent) = P(consequent | antecedent) / P(consequent)

For co-location, P(consequent) = |consequent instances| / |total instances|

Args:
    antecedent: Left side of the rule
    consequent: Right side of the rule
    confidence: Confidence of the rule
    pattern_table_instances: Table instances for the full pattern
    
Returns:
    Lift value
c              3   \   >#    U  H!  nTR                   R                  US 5      v   M#     g7fr  r  r  s     r)   rf   2CoLocationMiner._calculate_lift.<locals>.<genexpr>i  r  r  r   r   )rv   r%   r   )	r&   r  r  r   r  consequent_totalr   consequent_problifts	   `        r)   _calculate_liftCoLocationMiner._calculate_liftV  sX    & QjQQdii.a#3q#8*<a +r,   patternsmin_confidencemin_liftc                    [         R                  S[        U5       SU SU 35        / nU GHy  n[        US   5      nUS   nUR	                  S/ 5      nU(       d  [         R                  SU S35        MK  U R                  U5      n	[        [        U5      5      n
S	S
K	J
n  [        S[        U5      5       H  nU" X5       H  n[        U5      nXn-
  nU(       d  M  U R                  XXh5      nUU:  a  M5  U R                  XUU5      nUU:  a  MP  [        [        U5      5      [        [        U5      5      [        [        U5      5       S[        [        U5      5       3UU	U[        [        U5      5      UR	                  SS5      S.nUR                  U5        M     M     GM|     UR                  S SS9  [         R                  S[        U5       S35        U$ )zGenerate association rules from mined patterns.

Args:
    patterns: List of mined patterns
    min_confidence: Minimum confidence threshold
    min_lift: Minimum lift threshold
    
Returns:
    List of association rules with metrics
zGenerating rules from z patterns with min_confidence=z, min_lift=r   r   r~   zSkipping pattern z: no table instancesr   )combinationsr:   u    → r   r   )r  r  rule_strr   supportr-  r   pattern_participationc                     U S   $ r  r   r   s    r)   r   0CoLocationMiner.generate_rules.<locals>.<lambda>  s    Q|_r,   Tr   r   z rules)r   r    r%   r   r   r   r  rp   rn   	itertoolsr4  rr   r"  r.  ro   r  )r&   r0  r1  r2  	all_rulespattern_datar   r   r~   pattern_supportr   r4  r/   antecedent_tupler  r  r   r-  rules                      r)   generate_rulesCoLocationMiner.generate_rulesy  s    	,S]O;YZhYiitu}t~  	A	 %L,y12G&}5K*../@"EO #0=QRS #55oFO "$w-0L /1c'l+(4\(E$!$%5!6J!(!5J%  "&!@!@""J "N2   //

TcdDh  '-T*-=&>&,T*-=&>'-d:.>'?&@fTR\M]F^E_$`&0#2 $#)$w-#81=1A1ABWY\1]	D $$T*C )F ,/ %z 	4dCjY 078r,   )r#   rH   r   r   r   r   r   r   N)g      0@F)g333333?r   r   )g      ?r  )__name__
__module____qualname____firstlineno____doc__strfloatboolr*   r   r   r   r   r;   r   r$   rR   r   r   r   r   r   r  r  r"  r&  r.  r@  __static_attributes__r   r,   r)   r   r      s   >!c# !c5 !cRV !cFC Dc3h,@ &	c3h 	
z3d 3t 3 3 `%4T$s)_0D+E `%D&Qc#h &Q8<S$tCy/=Q8R&QW\&QP/# /# /$ /(_ T#tDQTIBV=W _ \`adfjkopsktfuau\v _ B 8;-.%1Ju J'*J"J6:4S>6JJXI$tCy/ Ie I.%$SX %$3s8 %$,/H%$OSTXY\T]%$ch%$NCH c4PTUXPY?FZA[ 2!#c( !C !#(!CGS	?!W\!H /2(+RtDcN'; R&+R %R04T#s(^0DR Rr,   r   )rF  r4   loggingrO   rx   rC   collectionsr   typingr   r   r   r   r   r	   	getLoggerrB  r   r   r   ImportErrorr   r   r,   r)   <module>rP     s\    -     # 8 8			8	$O
w
 w
	  Os   A AA