+
    }i)                        ^ RI t ^ RIt^ RIHt ^ RIHtHtHtHtH	t	H
t
HtHtHt  ^ RIt]! R4      t]]]3,          t ! R R4      t ! R R4      tR	 R
 ltR R ltR R ltR R ltR R ltR R ltR#   ] d    ^ RIt LZi ; i)    NPath)	AnyDictIterableListOptionalSequenceTupleTypeVarUnionTc                      a  ] tR t^t o RtV 3R lR ltV 3R lR ltV 3R lR ltV 3R lR	 ltV 3R
 lR lt	]
V 3R lR l4       t]
V 3R lR l4       t]
V 3R lR l4       tRtV tR# )BayesianNodezk
Implementation of a single node in a bayesian network allowing sampling from its conditional distribution
c                6   < V ^8  d   QhRS[ S[S[3,          /# )   node_definitionr   strr   )format__classdict__s   "o/Users/ahmad/.openclaw/workspace/my-crawler/.venv/lib/python3.14/site-packages/browserforge/bayesian_network.py__annotate__BayesianNode.__annotate__   s     / /S#X /    c                    Wn         R # Nr   )selfr   s   &&r   __init__BayesianNode.__init__   s    .r   c                R   < V ^8  d   QhRS[ S[S[3,          RS[ S[S[3,          /# r   parent_valuesreturn)r   r   r   float)r   r   s   "r   r   r      s.      !#s(^	c5j	r   c                    V P                   R,          pV P                   FN  pVP                  V4      pWBP                  R/ 4      9   d   VR,          V,          pK<  VP                  R/ 4      pKP  	  V# )zZ
Extracts unconditional probabilities of node values given the values of the parent nodes
conditionalProbabilitiesdeeperskip)r   parent_namesget)r   r$   probabilitiesparent_nameparent_values   &&   r   $get_probabilities_given_known_values1BayesianNode.get_probabilities_given_known_values   sq     ,,-GH,,K(,,[9L002>> -h 7 E - 1 1&" = - r   c                R   < V ^8  d   QhRS[ S[,          RS[S[S[3,          RS[/# )r   possible_valuesr-   r%   )r   r   r   r&   r   )r   r   s   "r   r   r   &   s2     " "#Cy"9=c5j9I"	"r   c                    \         P                   ! 4       pRpV F  pWBV,          ,          pWC8  g   K  Vu # 	  V^ ,          # )zF
Randomly samples from the given values using the given probabilities
g        )random)r   r3   r-   anchorcumulative_probabilitypossible_values   &&&   r   &sample_random_value_from_possibilities3BayesianNode.sample_random_value_from_possibilities&   sF     !$-N"N&CC"%.%% .
 q!!r   c                <   < V ^8  d   QhRS[ S[S[3,          RS[/# r#   r   )r   r   s   "r   r   r   7   s#     
 
DcN 
s 
r   c                v    V P                  V4      pV P                  \        VP                  4       4      V4      # )zY
Randomly samples from the conditional distribution of this node given values of parents
)r0   r9   listkeys)r   r$   r-   s   && r   sampleBayesianNode.sample7   s:     AA-P::##%&
 	
r   c          	      x   < V ^8  d   QhRS[ S[S[3,          RS[S[,          RS[S[,          RS[S[,          /# )r   r$   value_possibilitiesbanned_valuesr%   )r   r   r   r   r   r	   )r   r   s   "r   r   r   @   sG      CH~ &c] Cy	
 
#r   c                    V P                  V4      pV Uu. uF  pWS9  g   K  WT9   g   K  VNK  	  ppV'       d   V P                  Wd4      # R# u upi )z
Randomly samples from the conditional distribution of this node given restrictions on the possible values and the values of the parents.
N)r0   r9   )r   r$   rB   rC   r-   valuevalid_valuess   &&&&   r    sample_according_to_restrictions-BayesianNode.sample_according_to_restrictions@   sg     AA-P -
,) .3.D E, 	 

 >>|[[
s   AAAc                    < V ^8  d   QhRS[ /# r   r%   )r   )r   r   s   "r   r   r   U   s     , ,c ,r   c                (    V P                   R ,          # )namer   r   s   &r   rL   BayesianNode.nameT   s    ##F++r   c                0   < V ^8  d   QhRS[ S[,          /# rJ   r   r   )r   r   s   "r   r   r   Y   s     ; ;d3i ;r   c                :    V P                   P                  R . 4      # )parentNamesr   r,   rM   s   &r   r+   BayesianNode.parent_namesX   s    ##''r::r   c                0   < V ^8  d   QhRS[ S[,          /# rJ   rP   )r   r   s   "r   r   r   ]   s     > >c >r   c                :    V P                   P                  R . 4      # )possibleValuesrS   rM   s   &r   r3   BayesianNode.possible_values\   s    ##''(8"==r   r   N)__name__
__module____qualname____firstlineno____doc__r    r0   r9   r?   rG   propertyrL   r+   r3   __static_attributes____classdictcell__r   s   @r   r   r      sx     / / " ""
 
 ( , , ; ; > >r   r   c                   p   a  ] tR t^at o RtV 3R lR ltRV 3R lR lltV 3R lR ltV 3R	 lR
 ltRt	V t
R# )BayesianNetworkzY
Implementation of a bayesian network capable of randomly sampling from its distribution
c                $   < V ^8  d   QhRS[ RR/# )r   pathr%   Nr   )r   r   s   "r   r   BayesianNetwork.__annotate__f   s     X XT Xd Xr   c                    \        V4      pVR ,           Uu. uF  p\        V4      NK  	  upV n        V P                   Uu/ uF  qDP                  VbK  	  upV n        R# u upi u upi )nodesN)extract_jsonr   nodes_in_sampling_orderrL   nodes_by_name)r   re   network_definitionnode_defnodes   &&   r   r    BayesianNetwork.__init__f   sf    )$/3Eg3N(
3NxL"3N(
$ ;?:V:VW:V$iio:VW(
 Xs   A A%Nc                b   < V ^8  d   QhRS[ S[S[S[3,          ,          RS[S[S[3,          /# )r   input_valuesr%   )r	   r   r   r   )r   r   s   "r   r   rf   m   s5     
 
HT#s(^,D 
PTUXZ]U]P^ 
r   c                    Vf   / pVP                  4       pV P                   F3  pVP                  V9  g   K  VP                  V4      W#P                  &   K5  	  V# )zM
Randomly samples from the distribution represented by the bayesian network.
)copyrj   rL   r?   )r   rq   r?   rn   s   &&  r   generate_sampleBayesianNetwork.generate_samplem   sU     L""$00Dyy&$(KK$7yy! 1 r   c                r   < V ^8  d   QhRS[ S[S[S[,          3,          RS[S[ S[S[3,          ,          /# )r   rB   r%   )r   r   r   r	   r   )r   r   s   "r   r   rf   y   s<     e e#'Xc](:#;e	$sCx.	!er   c                (    V P                  / V^ 4      # )z
Randomly samples values from the distribution represented by the bayesian network,
making sure the sample is consistent with the provided restrictions on value possibilities.
Returns None if no such sample can be generated.
)4recursively_generate_consistent_sample_when_possible)r   rB   s   &&r   (generate_consistent_sample_when_possible8BayesianNetwork.generate_consistent_sample_when_possibley   s     HHM`bcddr   c                   < V ^8  d   QhRS[ S[S[3,          RS[ S[S[S[,          3,          RS[RS[S[ S[S[3,          ,          /# )r   sample_so_farrB   depthr%   )r   r   r   r   intr	   )r   r   s   "r   r   rf      sV      CH~ "#x}"45 	
 
$sCx.	!r   c                ~   V\        V P                  4      8X  d   V# V P                  V,          p. pRp VP                  VVP                  VP                  VP
                  4      V4      pVf    R# WaVP                  &   V P                  WV^,           4      pVe   V# VP                  V4       WP                   K  )zb
Recursively generates a random sample consistent with the given restrictions on possible values.
N)lenrj   rG   r,   rL   r3   rx   append)r   r|   rB   r}   rn   rC   sample_valuenext_samples   &&&&    r   rx   DBayesianNetwork.recursively_generate_consistent_sample_when_possible   s     C4455  ++E2#%@@#''		43G3GHL
 #  (4$))$SSEAIK &""  .ii(r   )rk   rj   r   )rY   rZ   r[   r\   r]   r    rt   ry   rx   r_   r`   ra   s   @r   rc   rc   a   s6     X X
 
e e r   rc   c                ~    V ^8  d   QhR\         \        ,          R\         \        ,          R\        \        ,          /# )r   abr%   )r
   r   r   )r   s   "r   r   r      s-     ( ((1+ ((1+ ($q' (r   c                V    \        V4      pV  Uu. uF  q3V9   g   K  VNK  	  up# u upi )z:
Performs a set "intersection" on the given (flat) arrays
)set)r   r   set_bxs   &&  r   array_intersectionr      s+     FE'q!JAAq'''s   &&c          	          V ^8  d   QhR\         \        \        R3,          ,          R\         \        \        R3,          ,          R\         \        \        R3,          ,          /# )r   r   .r   r%   )r   r   r   )r   s   "r   r   r      sH     	: 	:eAsFm$ 	:eAsFm)< 	:eAsFmAT 	:r   c           	         \        W4       UUu. uF(  w  r#\        \        V4      P                  V4      4      NK*  	  upp# u uppi )z
Combines two arrays into a single array using the set union
Args:
    a: First array to be combined.
    b: Second array to be combined.
Returns:
    Zipped (multi-dimensional) array.
)ziptupler   union)r   r   r   ys   &&  r   	array_zipr      s3     031y9ytqE#a&,,q/"y999s   .Ac                t    V ^8  d   QhR\         \        \        3,          R\         \        \        3,          /# )r   objr%   r   )r   s   "r   r   r      s*      $sCx. T#s(^ r   c                    \        V \        4      '       g   V # / pV P                  4        F>  w  r#VR8X  d   K  VR8X  d   VP                  \	        V4      4       K1  \	        V4      W&   K@  	  V# )zM
Removes the "deeper/skip" structures from the conditional probability table
r*   r)   )
isinstancedictitemsupdateundeeper)r   resultkeyrE   s   &   r   r   r      s`     c4  
Fiik
&=(?MM(5/*"5/FK " Mr   c          	          V ^8  d   QhR\         \        \        3,          R\        R\        \
        \        R3,          ,          /# )r   tree
valid_keysr%   .)r   r   r   Mapr   r   )r   s   "r   r   r      s9      DcN  USVX[S[_H] r   c                4   aa . oR VV3R lloS! W. 4       S# )z
Performs DFS on the Tree and returns values of the nodes on the paths that end with the given keys
(stored by levels - first level is the root)
```
   1
  / \
 2   3
/ \ / \
4 5 6 7
```
filter_by_last_level_keys(tree, ['4', '7']) => [[1], [2,3]]
c                    V ^8  d   QhR\         \        \        3,          R\        \        \        R3,          \
        \        ,          3,          R\
        \        ,          RR/# )r   tvk.accr%   N)r   r   r   r   r   r   )r   s   "r   r   /filter_by_last_level_keys.<locals>.__annotate__   sL     1 14S> 1uU38_d3i-G'H 1tTWy 1]a 1r   c                 P  < V P                  4        F  p\        W,          \        4      '       d   W,          fG   W19   d?   \        S4      ^ 8X  d   V Uu. uF  qD3NK  	  upM\	        SV Uu. uF  qD3NK  	  up4      oKo  S! W,          WV.,           4       K  	  R # u upi u upi r   )r>   r   r   r   r   )r   r   r   r   r   outrecurses   &&&  r   r   *filter_by_last_level_keys.<locals>.recurse   s    668Cafd++qv~9 s8q= (++s!s+&s3,?3aT3,?@ 
 3%K0 
 ,,?s   B-B# )r   r   r   r   s   &&@@r   filter_by_last_level_keysr      s%     "$C1 1 Db!Jr   c          	          V ^8  d   QhRRR\         \        \        \        \        R3,          \        \        ,          3,          3,          R\         \        \
        \        ,          3,          /# )r   networkrc   r3   .r%   )r   r   r   r   r   r
   )r   s   "r   r   r      sU     $ $$15c5sCxRVWZR[A[;\6\1]$	#x}
$r   c           
     N   . pVP                  4        F  w  r4\        V\        \        34      '       g   K#  \	        V4      ^ 8X  d   \        R4      hV P                  V,          p\        VP                  R,          4      p\        Wd4      pVP                  / \        \        VP                  V4      4      CW4/C4       K  	  / pV F[  p	V	P                  4        FD  pW89   d2   \        W,          W,          4      p
V
'       g   \        R4      hWV&   K:  W,          W&   KF  	  K]  	  V# )z
Given a `generative-bayesian-network` instance and a set of user constraints, returns an extended
set of constraints **induced** by the original constraints and network structure
zgThe current constraints are too restrictive. No possible values can be found for the given constraints.r(   )r   r   r=   r   r   
ValueErrorrk   r   r   r   r   r   r   r+   r>   r   )r   r3   setsr   rE   rn   r   zipped_valuesr   set_dictintersected_valuess   &&         r   get_possible_valuesr      s    D%++-
%$//u:?y  $$S),,-GHI1$>OtC 1 1=ABOCOP . (*F==?C}%7v{%S")$ B  1s&m #  Mr   c                0    V ^8  d   QhR\         R\        /# )r   re   r%   )r   r   )r   s   "r   r   r     s     ( (t ( (r   c           	        V P                   R8w  dD   \        V R4      ;_uu_ 4       p\        P                  ! VP	                  4       4      uuRRR4       # \
        P                  ! V R4      ;_uu_ 4       p \        R VP                  4        4       4      pTP                  T4      ;_uu_ 4       p\        P                  ! TP	                  4       4      uuRRR4       uuRRR4       #   + '       g   i     L; i  \         d    / u uuRRR4       # i ; i  + '       g   i     M; i RRR4       R#   + '       g   i     R# ; i)z
Unzips a zip file if the path points to a zip file, otherwise directly loads a JSON file.

Parameters:
    path: The path to the zip file or JSON file.

Returns:
    A dictionary representing the JSON content.
z.ziprbNrc              3   V   "   T F  qP                  R 4      '       g   K  Vx  K!  	  R# 5i)z.jsonN)endswith).0files   & r   	<genexpr>extract_json.<locals>.<genexpr>#  s     U]TmmG>TDD]s   )
))
suffixopenjsonloadsreadzipfileZipFilenextnamelistStopIteration)re   r   zffilenamefs   &    r   ri   ri     s     {{f$::diik*  
s	#	#r	UR[[]UUH WWX!::affh'  
$	#   	I 
$	#	 
$	#	#	#sS   $C(4D66 C;D6/$D	
D6(C8	;DD6DD6D(#D66E	)r5   r   pathlibr   typingr   r   r   r   r	   r
   r   r   r   orjsonr   ImportErrorr   r=   r   r   r   rc   r   r   r   r   r   ri   r   r   r   <module>r      s       W W W CLD%KO> O>d@ @F(	:"B$N(S  s   A7 7BB