
    i'	                     L   d Z ddlZddlmZ ddlmZmZmZmZmZm	Z	 ddl
mZ dedee   fd	Zd
ej                  defdZ	 	 ddeeeef      dedeeegee   f      de	eeef   eeef   f   fdZ	 	 ddeeeeeef      f   deeeef   gef   dedededeeeef      fdZy)zd
Pure helpers for SignalProcessor (date parsing, reorder ratio, temporal aggregation, pair counts).
    N)datetime)AnyListDictCallableOptionalTuple   )DATETIME_FORMATdate_valreturnc                     t        | t              r| S t        | t              r	 t        j                  | t              S y # t
        $ r Y y w xY w)N)
isinstancer   strstrptimer   
ValueError)r   s    H/var/www/html/userprofiledev.eatanceapp.com/signal_processing/helpers.py
parse_dater      sL    (H%(C 	$$X??   		s   ? 	A
Arestaurant_countsc                 x    | syt        d | j                         D              }t        |t        |       z  d      S )Ng        c              3   ,   K   | ]  }|d kD  s	d   yw)r
   N ).0counts     r   	<genexpr>z*calculate_reorder_ratio.<locals>.<genexpr>   s     J"<	1"<s   
   )sumvaluesroundlen)r   reorderss     r   calculate_reorder_ratior"      s:    J"3":":"<JJHC 122A66    recordsdate_keyparse_fnc                 D   |xs t         }t        j                         }t        j                         }| D ]T  } ||j                  |            }|s||j	                  d      xx   dz  cc<   ||j	                  d      xx   dz  cc<   V t        |      t        |      fS )zKReturns (hour_dist, day_dist) as dicts of str -> count (str keys for JSON).z%Hr
   z%A)r   collectionsCountergetstrftimedict)r$   r%   r&   parse	hour_distday_distrdts           r   temporal_distributionr2      s     "
E##%I""$H155?#bkk$'(A-(R[[&'1,'	 
 	?DN**r#   groupskey_fntop_nresult_key_aresult_key_bc                    t        j                         }| j                         D ]y  \  }}t        t	        fd|D                    }t        t        |            D ]@  }	t        |	dz   t        |            D ]#  }
||	   s	||
   s|||	   ||
   fxx   dz  cc<   % B { |j                  |      D cg c]  \  \  }}}||||d|i c}}}S c c}}}w )z
    For each group (e.g. order_id -> items), collect unique keys per item, then count pairs.
    Returns list of {result_key_a, result_key_b, 'times_together': count} for top_n pairs.
    c              3   R   K   | ]  } |      xs d j                            yw) N)strip)r   itemr4   s     r   r   z*pair_counts_from_groups.<locals>.<genexpr><   s$     I546$<-24465s   $'r
   times_together)r(   r)   itemssortedsetranger    most_common)r3   r4   r5   r6   r7   pair_counts	_group_idr>   keysijabr   s    `            r   pair_counts_from_groupsrJ   /   s     %%'K"LLN	5cI5IIJs4y!A1q5#d),7tAwa$q' 23q83 - " + )44U;;MFQE 
q,+;UC;  s   7C)
created_atN)item_aitem_b)__doc__r(   r   typingr   r   r   r   r   r	   	constantsr   r   r)   floatr"   r   r2   intrJ   r   r#   r   <module>rS      s8     = = & (!3 7{/B/B 7u 7 !>B+$sCx.!++ xx'9 9:;+ 4S>4S>)*	+* ! d4S>**+d38n%s*+  	
  
$sCx.r#   