def run(features, p=p, g=eqn_graph, k=30): global sl sl = Slipnet(g, p) pr(as_dict(p)) print() print('Input:') pr(features) print() t0 = process_time() out_d = sl.dquery(features=features) t1 = process_time() pts(sl.topna(out_d, k=k)) print() pts(sl.topna(out_d, pred=Equation, k=k)) print() pts(sl.topna(out_d, pred=int, k=k)) print() pts(sl.topna(out_d, pred=Before, k=20)) print() pts(sl.topna(out_d, pred=After, k=20)) print() pts(sl.topna(out_d, pred=isclass, k=20)) print() pts(sl.topna(out_d, pred=Operator, k=20)) print() #print(out_d[NumOperands(2)]) #print(out_d[PrefixedNode(1, After(10))]) #print(out_d[PrefixedNode(2, Before(10))]) print(f'{t1 - t0:1.3f} sec')
def test_make_activations_in(self): slipnet = Slipnet.empty() # slipnet contents are ignored in this test self.assertEqual(slipnet.make_activations_in(['a']), {'a': 1.0}) a_in = dict(a=1.5, b=0.5, c=1.2) self.assertEqual( slipnet.make_activations_in(features=['a', 'b', 'd'], activations_in=a_in), dict(a=1.5, b=1.0, c=1.2, d=1.0)) self.assertEqual(a_in, dict(a=1.5, b=0.5, c=1.2))
class ATestEquation(unittest.TestCase): ''' p1 = TyrrellPropagator( max_total=10.0, noise=0.0, positive_feedback_rate=0.1, sigmoid_p=1.5, num_iterations=10, alpha=0.95, tyrrell_alpha=0.02, tyrrell_beta=0.1 ) ''' sl = Slipnet(eqn_graph, propagator=default_tyrrell_propagator) def test_4_10(self): t0 = process_time() out_d = self.sl.dquery(features=[Before(4), After(10), Equation]) elapsed = process_time() - t0 self.assertEqual( self.sl.top1(out_d, pred=Equation), Equation.make([6, 4], plus) ) self.assertCountEqual( self.sl.top(out_d, pred=int, k=3), [6, 4, 10] ) self.assertLessEqual(elapsed, 5.0) def test_backwash(self): # Pulsing an Equation should activate the nodes for its main # features. out_d = self.sl.dquery(features=[Equation.make([6, 4], plus)]) self.assertCountEqual( self.sl.top(out_d, pred=int, k=3), [6, 4, 10] ) self.assertEqual( self.sl.top1(out_d, pred=After), After(10) ) self.assertCountEqual( self.sl.top(out_d, pred=Before, k=2), [Before(6), Before(4)] )
def test_topna_and_top(self): slipnet = Slipnet.empty() # slipnet contents are ignored in this test d = {'a': 0.9, 'b': 0.2, 'c': 0.3, 'd': 0.4, 'e': 0.5, 'f': 0.6} pred = lambda x: x > 'b' self.assertEqual(slipnet.topna(d, pred=pred, k=None), [ NodeA('f', 0.6), NodeA('e', 0.5), NodeA('d', 0.4), NodeA('c', 0.3) ]) self.assertEqual(slipnet.top(d, pred=pred, k=None), ['f', 'e', 'd', 'c']) self.assertEqual(slipnet.top1(d, pred=pred, k=None), 'f') self.assertEqual(slipnet.topna(d, pred=pred, k=2), [NodeA('f', 0.6), NodeA('e', 0.5)]) self.assertEqual( slipnet.topna(d, pred=pred), # default k: pick only the top 1 [NodeA('f', 0.6)]) self.assertEqual(slipnet.topna(d, k=2), [NodeA('a', 0.9), NodeA('f', 0.6)])
def test_filter_by_type(self): slipnet = Slipnet.empty() # slipnet contents are ignored in this test d = {'a': 0.2, 'b': 0.3, 1: 0.2, 2: 0.3} self.assertEqual(slipnet.top(d, pred=str), ['b']) self.assertEqual(slipnet.top(d, pred=int), [2])
def test_dquery(self): slipnet = Slipnet(eqn_graph) d = slipnet.dquery([Before(4), Before(5)]) a = d[Equation.make([5, 4], plus)] self.assertGreater(a, 0.05) self.assertLess(a, 1.0)
# features. out_d = self.sl.dquery(features=[Equation.make([6, 4], plus)]) self.assertCountEqual( self.sl.top(out_d, pred=int, k=3), [6, 4, 10] ) self.assertEqual( self.sl.top1(out_d, pred=After), After(10) ) self.assertCountEqual( self.sl.top(out_d, pred=Before, k=2), [Before(6), Before(4)] ) sl = Slipnet.empty() def run(features, p=p, g=eqn_graph, k=30): global sl sl = Slipnet(g, p) pr(as_dict(p)) print() print('Input:') pr(features) print() t0 = process_time() out_d = sl.dquery(features=features) t1 = process_time() pts(sl.topna(out_d, k=k)) print() pts(sl.topna(out_d, pred=Equation, k=k))