
    ni*                        S SK rS SK Jr   S SKrS SKJr  SrS SKr	S SK
r
S SKrS SKrS SKJr  S r\
R"                  S 5       r\
R"                  S 5       r\R(                  R+                  S	/ S
Q5      S 5       r\R(                  R.                  \R(                  R+                  S	/ S
Q5      S 5       5       r\R(                  R.                  \R(                  R+                  S	/ S
Q5      S 5       5       r\R(                  R.                  \R(                  R+                  S	/ S
Q5      S 5       5       r\R(                  R7                  \(       + SS9\R(                  R+                  S	/ S
Q5      S 5       5       r\R(                  R7                  SSS9\R(                  R+                  S	S/5      S 5       5       r\R(                  R+                  S	/ S
Q5      S 5       rg! \ a    Sr GNf = f)    N)	ThresholdTF)closedc                    [         R                  " 5         [         R                  " 5         [         R                  " 5         S[         R                  l        U S:X  a  S[         R                  l        gU S:X  a  S[         R                  l        gg)z3Configure PyReason settings for the specified mode.TfpparallelN)prresetreset_rulesreset_settingssettingsverbose
fp_versionparallel_computing)modes    a/home/james-whalen/.local/lib/python3.13/site-packages/tests/functional/test_advanced_features.py
setup_moder      sY    HHJNNBKKt|!%		)-& 
    c                     U S   S   R                   nU S   S   R                   nX#-   n[        R                  " US5      nUS4$ )Nr         )lowernpround)annotationsweightsprob_Aprob_B
union_probs        r   probability_funcr      sK    ^A$$F^A$$FJ*a(Jq=r   c                 R    [         R                  R                  U S   S   /5      nU$ )z6Head function that returns the input node lists as-is.r   )numbatypedList)r   results     r   identity_funcr%   '   s)     [[{1~a012FMr   r   )regularr   r   c                    [        U 5        [        R                  R                  5       nUR	                  [        R                  R                  [        SS5      /5      5        UR	                  [        R                  R                  [        SS5      /5      5        [        R                  R                  SS/5      n[        X5      n[        R                  X5      nX4:X  d   eg)zAEnsure annotation function behaves the same with and without JIT.g{Gz?g      ?g?N)r   r!   r"   r#   appendr   r   py_func)r   r   r   jit_respy_ress        r   !test_probability_func_consistencyr,   .   s     t++""$Ku{{''c):(;<=u{{''S)9(:;<kkSz*G{4G%%k;Fr   c                    [        U 5        [        R                  " [        5        [        R
                  " 5       nUR                  SSS9  UR                  SSS9  UR                  SSSS9  [        R                  " U5        [        R                  " [        R                  " SS5      5        [        R                  " [        R                  " SS	5      5        [        R                  " [        R                  " S
S5      5        [        R                  " [        R                  " SS5      5        [        R                  " SS9nUR                  [        R                  " S5      SS9(       d   eUR                  [        R                  " S5      SS9(       d   eUR                  [        R                  " S5      SS9(       d   eUR                  [        R                  " S5      SS9(       d   eg)z:Test head function usage in rules for node and edge rules.Ar   )propertyB)	connectedzHProcessed(identity_func(X)) <- property(X), property(Y), connected(X, Y)node_rule_with_funczGRoute(identity_func(A), B) <- property(X), property(Y), connected(X, Y)edge_rule_func_firstzFPath(A, identity_func(B)) <- property(X), property(Y), connected(X, Y)edge_rule_func_secondzULink(identity_func(A), identity_func(B)) <- property(X), property(Y), connected(X, Y)edge_rule_func_both	timestepszProcessed(A)T)return_boolzRoute(A, B)z
Path(A, B)z
Link(A, B)N)r   r   add_head_functionr%   nxDiGraphadd_nodeadd_edge
load_graphadd_ruleRulereasonqueryQuery)r   graphinterpretations      r   test_head_functionsrF   ;   sg    t'JJLE	NN3N#	NN3N#	NN3qN)MM%KKbdyz{KKacyz{KK`byz{KKo  rG  H  IYY+N 8dKKK 7TJJJ 6DIII 6DIIIr   c                    [        U 5        S[        R                  l        [        R                  " [        R
                  " S5      5        [        R                  " [        R
                  " S5      5        [        R                  " [        5        [        R                  " [        R                  " SSS95        [        R                  " SS9n[        R                  " US/5      n[        U5       H(  u  p4[        S	U 35        [        U5        [        5         M*     UR                  [        R                  " S
5      5      (       d   S5       eg)z,Test annotation function usage in reasoning.TzP(A) : [0.01, 1]zP(B) : [0.2, 1]zDunion_probability(A, B):probability_func <- P(A):[0, 1], P(B):[0, 1])infer_edgesr   r6   union_probabilityTIMESTEP - z#union_probability(A, B) : [0.21, 1]z Union probability should be 0.21N)r   r   r   allow_ground_rulesadd_factFactadd_annotation_functionr   r?   r@   rA   filter_and_sort_edges	enumerateprintrB   rC   )r   rE   
dataframestdfs        r   test_annotation_functionrU   V   s     t%)BKK"KK*+,KK)*+/0KK^lpqrYY+N)).;N:OPJ:&A3 b	 '
 )N OPPtRttPr   c                    [        U 5        SnS[        R                  l        [        R                  " U5        [        SSS5      [        SSS5      /n[        R                  " [        R                  " SS	US
95        [        R                  " [        R                  " SSSS5      5        [        R                  " [        R                  " SSSS5      5        [        R                  " [        R                  " SSSS5      5        [        R                  " [        R                  " SSSS5      5        [        R                  " SS9n[        R                  " US/5      n[        U5       H(  u  pV[        SU 35        [        U5        [        5         M*     [        US   5      S:X  d   S5       e[        US   5      S:X  d   S5       eSUS   S   R                  ;   a"  US   R                   S   R"                  SS/:X  d   S5       eg)z$Test custom threshold functionality.z+./tests/functional/group_chat_graph.graphmlTgreater_equal)numbertotalr   )percentrY   d   z,ViewedByAll(y) <- HaveAccess(x,y), Viewed(x)viewed_by_all_rule)custom_thresholdszViewed(Zach)zseen-fact-zachr   r   zViewed(Justin)zseen-fact-justinzViewed(Michelle)zseen-fact-michellezViewed(Amy)zseen-fact-amy   r6   ViewedByAllrJ   z7At t=0 the TextMessage should not have been ViewedByAllz3At t=2 the TextMessage should have been ViewedByAllTextMessage	componentzBTextMessage should have ViewedByAll bounds [1,1] for t=2 timestepsN)r   r   r   
atom_traceload_graphmlr   r?   r@   rL   rM   rA   filter_and_sort_nodesrP   rQ   lenvaluesilocr_   )r   
graph_pathuser_defined_thresholdsrE   rR   rS   rT   s          r   test_custom_thresholdsrj   n   s    t ?J "BKK OOJ 	/#6:/#7=
 KK
: 5	
 KK(8!Q?@KK(*<aCDKK*,@!QGHKK1=> YY+N )).=/JJ:&A3 b	 ' 	JqMaA@A 	JqMa=<= JqM+6===*Q-BTBT	Ck		C L
 LL  Cr   ztorch not installed)rA   c                    [        U 5        [        R                  " SS5      n/ SQn[        R                  " SSSSS9n[        R
                  " XS	US
9n[        R                  " SS5      nSnSnU" XVU5      u  pn
[        U[        R                  5      (       d   S5       e[        U
5      [        U5      :X  d   S5       e[        SU5        [        SU	5        [        S5        U
 H  n[        U5        M     g)z*Test classifier integration with PyReason.
   r   )catdograbbitg?TFgffffff?)	thresholdset_lower_boundset_upper_bound
snap_value
classifier)
identifierinterface_optionsr   r   z)The model output should be a torch.TensorzExpected one fact per classLogitsProbabilitiesz
Generated PyReason Facts:N)r   nnLinearr   ModelInterfaceOptionsLogicIntegratedClassifiertorchrand
isinstanceTensorre   rQ   )r   modelclass_namesrv   logic_classifierinput_tensort1t2outputprobabilitiesfactsfacts               r   test_classifier_integrationr      s     t IIb!E +K
 00	 33ES_FWY ::a$L 
B	
B $4Lb#I F5 fell++X-XX+u:[))H+HH) 
(F	/=)	
'(d r   z9Reason again functionality not implemented for FP versionr&   c                    [        U 5        Sn[        R                  " U5        [        R                  " [        R                  " SS5      5        [        R
                  " [        R                  " SSSS5      5        [        R                  5         [        R                  " SS9n[        R                  " SS	S
S5      n[        R
                  " U5        [        R                  " SSSS9n[        R                  " US/5      n[        U5       H(  u  pV[        SU 35        [        U5        [        5         M*     [        US
   5      S:X  d   S5       e[        US   5      S
:X  d   S5       e[        US   5      S:X  d   S5       eSUS
   S   R                  ;   a"  US
   R                  S   R                   SS/:X  d   S5       eSUS   S   R                  ;   a"  US   R                  S   R                   SS/:X  d   S5       eSUS   S   R                  ;   a"  US   R                  S   R                   SS/:X  d   S5       eSUS   S   R                  ;   a"  US   R                  S   R                   SS/:X  d   S5       eSUS   S   R                  ;   a"  US   R                  S
   R                   SS/:X  d   S5       eSUS   S   R                  ;   a"  US   R                  S   R                   SS/:X  d   S5       eg) z*Test reasoning continuation functionality.z(./tests/functional/friends_graph.graphmlz=popular(x) <-1 popular(y), Friends(x,y), owns(y,z), owns(x,z)popular_rulezpopular(Mary)popular_factr   r   r6   popular_fact2r^      r   TF)r7   againrestartpopularrJ   z)At t=0 there should be one popular personz)At t=1 there should be two popular peoplez+At t=2 there should be three popular peopleMaryra   z7Mary should have popular bounds [1,1] for t=0 timestepsz7Mary should have popular bounds [1,1] for t=1 timestepsz7Mary should have popular bounds [1,1] for t=2 timestepsJustinz9Justin should have popular bounds [1,1] for t=1 timestepsz9Justin should have popular bounds [1,1] for t=2 timestepsJohnz7John should have popular bounds [1,1] for t=2 timestepsN)r   r   rc   r?   r@   rL   rM   faulthandlerenablerA   rd   rP   rQ   re   rf   rg   r   )r   rh   rE   new_factrR   rS   rT   s          r   test_reason_againr      s+    t <J OOJKKWYghiKKA>? YY+N wwA>HKKYY$FN )).9+FJ:&A3 b	 '
 z!}"O$OO"z!}"O$OO"z!}"Q$QQ" Z];/666:a=;M;Ma;P;X;X]^`a\b;b  ^  e^  ^bZ];/666:a=;M;Ma;P;X;X]^`a\b;b  ^  e^  ^bZ];/666:a=;M;Ma;P;X;X]^`a\b;b  ^  e^  ^b z!}[1888Z]=O=OPQ=R=Z=Z_`bc^d=d  b  gb  bdz!}[1888Z]=O=OPQ=R=Z=Z_`bc^d=d  b  gb  bd Z];/666:a=;M;Ma;P;X;X]^`a\b;b  ^  e^  ^b;br   c                    [        U 5        [        R                  " 5       nUR                  SS/5        [        R
                  " U5        [        R                  " [        R                  " SS5      5        [        R                  " [        R                  " SS5      5        [        R                  " [        R                  " SS5      5        [        R                  " S	5      nS
[        R                  l        [        R                  " SU/S9ng)z.Test reasoning with query-based rule filtering)r.   r0   )r0   Czpopular(x) <-1 friend(x, y)rule1zfriend(x, y) <-1 knows(x, y)rule2zknows(A, B)fact1z
popular(A)Fr   )r7   queriesN)r   r:   r;   add_edges_fromr   r>   r?   r@   rL   rM   rC   r   r   rA   )r   rD   rB   rE   s       r   test_reason_with_queriesr   
  s     tJJLE	*j12MM%KK5w?@KK6@AKKw/0 HH\"EBKKYYUG<Nr   )pyreasonr   r   r}   torch.nnry   torch_availableImportErrornetworkxr:   r!   numpyr   pytest8pyreason.scripts.numba_wrapper.numba_types.interval_typer   r   njitr   r%   markparametrizer,   slowrF   rU   rj   skipifr   r   r    r   r   <module>r      s	    O     K
.     !>?	 @	 !>?J @ J2 !>?u @ u, !>?7L @ 7Lt '0EF!>?, @ G,^ D!\])-*^ . ^*^Z !>?= @=G  Os   G G'&G'