def fixed_nodenet(request, test_world, engine): """ A test nodenet filled with some example data (nodenet_data.py) Structure: -> A1 -> A2 / S ACTA \ -> B1 -> B2 S: Sensor, brightness_l A1: Pipe A2: Pipe B1: Pipe B2: Pipe ACTA: Activator, por """ from micropsi_core.tests.nodenet_data import fixed_nodenet_data if engine == "theano_engine": fixed_nodenet_data = fixed_nodenet_data.replace('Root', 's0001') success, uid = micropsi.new_nodenet("Fixednet", engine=engine, worldadapter="Braitenberg", owner="Pytest User", world_uid=test_world, uid='fixed_test_nodenet') micropsi.get_nodenet(uid) micropsi.merge_nodenet(uid, fixed_nodenet_data, keep_uids=True) micropsi.save_nodenet(uid) yield uid try: micropsi.delete_nodenet(uid) except: pass
def fixed_nodenet(request, test_world, engine): from micropsi_core.tests.nodenet_data import fixed_nodenet_data if engine == "theano_engine": fixed_nodenet_data = fixed_nodenet_data.replace('Root', 's0001') success, uid = micropsi.new_nodenet("Fixednet", engine=engine, worldadapter="Braitenberg", owner="Pytest User", world_uid=test_world, uid='fixed_test_nodenet') micropsi.get_nodenet(uid) micropsi.merge_nodenet(uid, fixed_nodenet_data, keep_uids=True) micropsi.save_nodenet(uid) return uid
def fixed_nodenet(request, test_world): from micropsi_core.tests.nodenet_data import fixed_nodenet_data success, uid = micropsi.new_nodenet("Fixednet", "Braitenberg", owner="Pytest User", world_uid=test_world, uid='fixed_test_nodenet') micropsi.get_nodenet(uid) micropsi.merge_nodenet(uid, fixed_nodenet_data) def fin(): if DELETE_TEST_FILES_ON_EXIT: micropsi.delete_nodenet(uid) request.addfinalizer(fin) return uid
def test_clone_nodes_to_new_nodespace(fixed_nodenet): nodenet = micropsi.get_nodenet(fixed_nodenet) res, testspace_uid = micropsi.add_nodespace(fixed_nodenet, [100, 100], nodespace=None, name="testspace") success, result = micropsi.clone_nodes(fixed_nodenet, ['n0001', 'n0002'], 'internal', nodespace=testspace_uid) assert success assert len(result['nodes']) == 2 assert len(result['links']) == 1 if result['nodes'][0]['name'] == 'A1_copy': a1_copy = result['nodes'][0] a2_copy = result['nodes'][1] else: a1_copy = result['nodes'][1] a2_copy = result['nodes'][0] a1_copy = nodenet.get_node(a1_copy['uid']) a2_copy = nodenet.get_node(a2_copy['uid']) assert a1_copy.parent_nodespace == testspace_uid assert a2_copy.parent_nodespace == testspace_uid
def test_clone_nodes_internal_links(fixed_nodenet): nodenet = micropsi.get_nodenet(fixed_nodenet) success, result = micropsi.clone_nodes(fixed_nodenet, ['n0001', 'n0002'], 'internal') assert success assert len(result['nodes']) == 2 assert len(result['links']) == 1 if result['nodes'][0]['name'] == 'A1_copy': a1_copy = result['nodes'][0] a2_copy = result['nodes'][1] else: a1_copy = result['nodes'][1] a2_copy = result['nodes'][0] a1_copy = nodenet.get_node(a1_copy['uid']) a2_copy = nodenet.get_node(a2_copy['uid']) l1_uid = result['links'][0]['uid'] links = a1_copy.get_associated_links() link = None for candidate in links: if candidate.source_node.uid == a1_copy.uid and \ candidate.target_node.uid == a2_copy.uid and \ candidate.source_gate.type == 'por' and \ candidate.target_slot.type == 'gen': link = candidate assert link is not None
def test_clone_nodes_nolinks(fixed_nodenet): nodenet = micropsi.get_nodenet(fixed_nodenet) success, result = micropsi.clone_nodes(fixed_nodenet, ['n0001', 'n0002'], 'none', offset=[10, 20]) assert success if result['nodes'][0]['name'] == 'A1_copy': a1_copy = result['nodes'][0] a2_copy = result['nodes'][1] else: a1_copy = result['nodes'][1] a2_copy = result['nodes'][0] assert nodenet.is_node(a1_copy['uid']) assert a1_copy['uid'] != 'n0001' assert a1_copy['type'] == nodenet.get_node('n0001').type assert a1_copy['parameters'] == nodenet.get_node( 'n0001').clone_parameters() assert a1_copy['position'][0] == nodenet.get_node('n0001').position[0] + 10 assert a1_copy['position'][1] == nodenet.get_node('n0001').position[1] + 20 assert nodenet.is_node(a2_copy['uid']) assert a2_copy['name'] == nodenet.get_node('n0002').name + '_copy' assert a2_copy['uid'] != 'n0002' assert len(result['nodes']) == 2 assert len(result['links']) == 0
def test_clone_nodes_all_links(fixed_nodenet): nodenet = micropsi.get_nodenet(fixed_nodenet) success, result = micropsi.clone_nodes(fixed_nodenet, ['A1', 'A2'], 'all') assert success assert len(result['nodes']) == 2 assert len(result['links']) == 2 if result['nodes'][0]['name'] == 'A1_copy': a1_copy = result['nodes'][0] a2_copy = result['nodes'][1] else: a1_copy = result['nodes'][1] a2_copy = result['nodes'][0] a1_copy = nodenet.nodes[a1_copy['uid']] a2_copy = nodenet.nodes[a2_copy['uid']] l1_uid = list(a1_copy.gates['por'].outgoing.keys())[0] l2_uid = list(a1_copy.slots['gen'].incoming.keys())[0] assert nodenet.links[l1_uid].source_node.uid == a1_copy.uid assert nodenet.links[l1_uid].target_node.uid == a2_copy.uid assert nodenet.links[l1_uid].source_gate.type == 'por' assert nodenet.links[l1_uid].target_slot.type == 'gen' assert l1_uid in [l['uid'] for l in result['links']] assert nodenet.links[l2_uid].source_node.uid == 'S' assert nodenet.links[l2_uid].target_node.uid == a1_copy.uid assert nodenet.links[l2_uid].source_gate.type == 'gen' assert nodenet.links[l2_uid].target_slot.type == 'gen' assert l2_uid in [l['uid'] for l in result['links']]
def test_decay_porret_links(test_nodenet): nodenet = micropsi.get_nodenet(test_nodenet) netapi = nodenet.netapi pipes = [] netapi.set_modulator('base_porret_decay_factor', 0.1) for i in range(10): node = netapi.create_node("Pipe", None, "P%d" % i) pipes.append(node) if i > 0: netapi.link_with_reciprocal(pipes[i - 1], node, 'porret', weight=0.1 * i) netapi.link_with_reciprocal(pipes[0], pipes[1], 'subsur', weight=0.5) reg = netapi.create_node("Register", None, "source") netapi.link(reg, 'gen', pipes[0], 'gen', 0.4) netapi.decay_por_links(None) for i in range(9): assert round(pipes[i].get_gate('por').get_links()[0].weight, 3) == round(0.1 * (i + 1) * 0.9, 3) # sub/sur/ret/gen links unchanged assert round(reg.get_gate('gen').get_links()[0].weight, 3) == 0.4 assert round(pipes[0].get_gate('sub').get_links()[0].weight, 3) == 0.5 assert round(pipes[7].get_gate('ret').get_links()[0].weight, 3) == 0.7
def test_announce_nodes(test_nodenet): nodenet = micropsi.get_nodenet(test_nodenet) netapi = nodenet.netapi nodespace = netapi.create_nodespace( None, name="partition", options={ "new_partition": True, "initial_number_of_nodes": 2, "average_elements_per_node_assumption": 4, "initial_number_of_nodespaces": 1 }) # announce 20 pipe nodes netapi.announce_nodes(nodespace.uid, 20, 8) partition = nodespace.partition # 18 nodes needed assert partition.NoN == 26 # growby: 18 + 18//3 # 152 elements needed assert partition.NoE == 210 # growby: 152 + 152//3 for i in range(20): netapi.create_node("Pipe", nodespace.uid, "N %d" % i) # assert that we did not grow again assert partition.NoN == 26 assert partition.NoE == 210
def test_add_custom_monitor(test_nodenet): nodenet = micropsi.get_nodenet(test_nodenet) netapi = nodenet.netapi function = "return len(netapi.get_nodes())" uid = netapi.add_custom_monitor(function, 'number_of_nodes', color=None) assert nodenet.get_monitor(uid).name == 'number_of_nodes' assert nodenet.get_monitor(uid).function == function
def test_partition_get_node_data(test_nodenet): nodenet = micropsi.get_nodenet(test_nodenet) netapi = nodenet.netapi nodespace, source, register = prepare(netapi) nodes = [] for i in range(10): n = netapi.create_node("Pipe", nodespace.uid if i > 4 else None, "node %d" % i) nodes.append(n) for i in range(4): netapi.link(nodes[i], 'gen', nodes[5], 'gen', weight=((i + 2) / 10)) netapi.link(nodes[9], 'gen', nodes[4], 'gen', 0.375) third_ns = netapi.create_nodespace(None, "third") third = netapi.create_node("Register", third_ns.uid, "third") netapi.link(nodes[4], 'gen', third, 'gen') node_data = nodenet.get_nodes(nodespace_uids=[None])['nodes'] assert set(node_data.keys()) == set([n.uid for n in nodes[:5]] + [source.uid, register.uid, third.uid] + [nodes[9].uid, nodes[5].uid]) node_data = nodenet.get_nodes()['nodes'] n1, n3, n4, n9 = nodes[1], nodes[3], nodes[4], nodes[9] assert round(node_data[n1.uid]['links']['gen'][0]['weight'], 3) == 0.3 assert round(node_data[n3.uid]['links']['gen'][0]['weight'], 3) == 0.5 assert round(node_data[n9.uid]['links']['gen'][0]['weight'], 3) == 0.375 # assert node_data[n4.uid]['links'] == {} node_data = nodenet.get_nodes(nodespace_uids=[nodespace.uid])['nodes'] assert len(node_data.keys()) == 12 assert node_data[n4.uid]['links'] == {} assert third.uid not in node_data
def test_sensor_actuator_indices(test_nodenet): nodenet = micropsi.get_nodenet(test_nodenet) netapi = nodenet.netapi result, world_uid = micropsi.new_world('default', 'World') micropsi.set_nodenet_properties(test_nodenet, worldadapter='Default', world_uid=world_uid) sensor = netapi.create_node("Sensor", None, "static_sensor") sensor.set_parameter("datasource", "static_on") actor = netapi.create_node("Actor", None, "echo_actor") actor.set_parameter("datatarget", "echo") register = netapi.create_node("Register", None, "source") register.activation = 0.8 netapi.link(register, 'gen', register, 'gen', weight=0.5) netapi.link(register, 'gen', actor, 'gen') assert sensor.activation == 0 assert actor.get_gate('gen').activation == 0 micropsi.step_nodenet(test_nodenet) micropsi.step_nodenet(test_nodenet) assert sensor.activation == 1 assert round(actor.get_gate('gen').activation, 3) == 0.8 netapi.delete_node(sensor) netapi.delete_node(actor) assert set(nodenet.rootpartition.actuator_indices) == {0} assert set(nodenet.rootpartition.sensor_indices) == {0}
def test_delete_nodespace_unlinks_native_module(test_nodenet, resourcepath): nodenet = micropsi.get_nodenet(test_nodenet) netapi = nodenet.netapi nodespace = netapi.create_nodespace(None, "foo") foopipe = netapi.create_node("Pipe", nodespace.uid, 'foopipe') import os nodetype_file = os.path.join(resourcepath, 'Test', 'nodetypes.json') nodefunc_file = os.path.join(resourcepath, 'Test', 'nodefunctions.py') with open(nodetype_file, 'w') as fp: fp.write('{"Testnode": {\ "name": "Testnode",\ "slottypes": ["gen", "foo", "bar"],\ "nodefunction_name": "testnodefunc",\ "gatetypes": ["gen", "foo", "bar"]}}') with open(nodefunc_file, 'w') as fp: fp.write( "def testnodefunc(netapi, node=None, **prams):\r\n return 17") micropsi.reload_native_modules() testnode = netapi.create_node("Testnode", None, "test") netapi.link(testnode, 'foo', foopipe, 'sub') netapi.link(foopipe, 'sur', testnode, 'bar') micropsi.save_nodenet(test_nodenet) # I don't understand why, but this is necessary. micropsi.revert_nodenet(test_nodenet) netapi.delete_nodespace(nodespace) data = netapi.get_node(testnode.uid).get_data(include_links=True) assert data['links'] == {}
def test_partition_creation(test_nodenet): nodenet = micropsi.get_nodenet(test_nodenet) netapi = nodenet.netapi netapi.create_nodespace(None, name="partition", options={'new_partition': True}) assert len(nodenet.partitions.keys()) == 2
def test_clone_nodes_nolinks(fixed_nodenet): nodenet = micropsi.get_nodenet(fixed_nodenet) success, result = micropsi.clone_nodes(fixed_nodenet, ['n0001', 'n0002'], 'none', offset=[10, 20, 2]) assert success for n in result.values(): if n['name'] == 'A1_copy': a1_copy = n elif n['name'] == 'A2_copy': a2_copy = n assert nodenet.is_node(a1_copy['uid']) assert a1_copy['uid'] != 'n0001' assert a1_copy['type'] == nodenet.get_node('n0001').type assert a1_copy['parameters'] == nodenet.get_node( 'n0001').clone_parameters() assert a1_copy['position'][0] == nodenet.get_node('n0001').position[0] + 10 assert a1_copy['position'][1] == nodenet.get_node('n0001').position[1] + 20 assert a1_copy['position'][2] == nodenet.get_node('n0001').position[2] + 2 assert nodenet.is_node(a2_copy['uid']) assert a2_copy['name'] == nodenet.get_node('n0002').name + '_copy' assert a2_copy['uid'] != 'n0002' assert len(result.keys()) == 2 assert a1_copy['links'] == {} assert a2_copy['links'] == {}
def test_clone_nodes_copies_gate_params(fixed_nodenet): nodenet = micropsi.get_nodenet(fixed_nodenet) micropsi.set_gate_parameters(fixed_nodenet, 'A1', 'gen', {'decay': 0.1}) success, result = micropsi.clone_nodes(fixed_nodenet, ['A1'], 'internal') assert success copy = nodenet.get_node(result['nodes'][0]['uid']) assert copy.get_gate_parameters()['gen']['decay'] == 0.1
def test_clone_nodes_all_links(fixed_nodenet): nodenet = micropsi.get_nodenet(fixed_nodenet) success, result = micropsi.clone_nodes(fixed_nodenet, ['n0001', 'n0002'], 'all') assert success # expect 3 instead of two results, because the sensor that links to A1 should be delivered # as a followupdnode to A1_copy to render incoming links assert len(result.keys()) == 3 for n in result.values(): if n['name'] == 'A1_copy': a1_copy = n elif n['name'] == 'A2_copy': a2_copy = n # assert the link between a1-copy and a2-copy exists a1link = a1_copy['links']['por'][0] assert a1link['target_node_uid'] == a2_copy['uid'] # assert the link between sensor and the a1-copy exists sensor = nodenet.get_node('n0005').get_data() candidate = None for link in sensor['links']['gen']: if link['target_node_uid'] == a1_copy['uid']: candidate = link assert candidate['target_slot_name'] == 'gen'
def test_grow_partitions(test_nodenet): nodenet = micropsi.get_nodenet(test_nodenet) netapi = nodenet.netapi nodespace = netapi.create_nodespace( None, name="partition", options={ "new_partition": True, "initial_number_of_nodes": 2, "average_elements_per_node_assumption": 4, "initial_number_of_nodespaces": 1 }) for i in range(20): netapi.create_node("Pipe", nodespace.uid, "N %d" % i) partition = nodespace.partition # growby (NoN // 2): 2,3,4,6,9,13,19,28 assert len(partition.allocated_nodes) == 28 assert partition.NoE > 28 * 4 for i in range(2): netapi.create_nodespace(nodespace.uid, name="NS %d" % i) assert len(partition.allocated_nodespaces) == 4 # step, save, and load the net to make sure all data structures have been grown properly micropsi.step_nodenet(test_nodenet) micropsi.save_nodenet(test_nodenet) micropsi.revert_nodenet(test_nodenet) micropsi.step_nodenet(test_nodenet)
def test_delete_linked_nodes(fixed_nodenet): nodenet = micropsi.get_nodenet(fixed_nodenet) netapi = nodenet.netapi # create all evil (there will never be another dawn) root_of_all_evil = netapi.create_node("Pipe", None) evil_one = netapi.create_node("Pipe", None) evil_two = netapi.create_node("Pipe", None) netapi.link_with_reciprocal(root_of_all_evil, evil_one, "subsur") netapi.link_with_reciprocal(root_of_all_evil, evil_two, "subsur") for link in evil_one.get_gate("sub").get_links(): link.source_node.name # touch of evil link.target_node.name # touch of evil for link in evil_two.get_gate("sur").get_links(): link.source_node.name # touch of evil link.target_node.name # touch of evil # and the name of the horse was death netapi.delete_node(root_of_all_evil) netapi.delete_node(evil_one) netapi.delete_node(evil_two)
def test_unlink_slot(test_nodenet): nodenet = micropsi.get_nodenet(test_nodenet) netapi = nodenet.netapi node = netapi.create_node("Pipe", None) pipe1 = netapi.create_node("Pipe", None) pipe2 = netapi.create_node("Pipe", None) netapi.link_with_reciprocal(node, pipe1, 'subsur') netapi.link_with_reciprocal(node, pipe2, 'subsur') netapi.link(pipe1, 'gen', node, 'por') netapi.link(pipe2, 'gen', node, 'por') netapi.link(pipe1, 'sur', node, 'por') micropsi.save_nodenet(test_nodenet) netapi.unlink_slot(node, 'por') assert node.get_slot('por').empty assert not node.get_slot('sur').empty micropsi.revert_nodenet(test_nodenet) netapi = micropsi.nodenets[test_nodenet].netapi node = netapi.get_node(node.uid) netapi.unlink_slot(node, 'por', source_node_uid=pipe1.uid) assert len(node.get_slot('por').get_links()) == 1 assert node.get_slot('por').get_links()[0].source_node.uid == pipe2.uid micropsi.revert_nodenet(test_nodenet) netapi = micropsi.nodenets[test_nodenet].netapi node = netapi.get_node(node.uid) netapi.unlink_slot(node, 'por', source_gate_name='sur') assert len(node.get_slot('por').get_links()) == 2 # pipe1:gen, pipe2:gen assert len(node.get_slot('sur').get_links()) == 2 # only sur->por unlinked
def test_add_modulator_monitor(test_nodenet): nodenet = micropsi.get_nodenet(test_nodenet) netapi = nodenet.netapi nodenet.step() uid = netapi.add_modulator_monitor('base_age', 'age') assert nodenet.get_monitor(uid).modulator == 'base_age' assert nodenet.get_monitor(uid).name == 'age'
def test_add_gate_monitor(test_nodenet, node): nodenet = micropsi.get_nodenet(test_nodenet) netapi = nodenet.netapi uid = netapi.add_gate_monitor(node, 'gen', name='sepp', color='#987654') assert nodenet.get_monitor(uid).name == 'sepp' assert nodenet.get_monitor(uid).type == 'gate' assert nodenet.get_monitor(uid).color == '#987654'
def test_nodespace_properties(test_nodenet): nodenet = micropsi.get_nodenet(test_nodenet) netapi = nodenet.netapi rootns = netapi.get_nodespace(None) netapi.set_nodespace_properties(None, {'foo': 'bar'}) data = netapi.get_nodespace_properties() assert data[rootns.uid] == {'foo': 'bar'}
def test_save_nodenet(test_nodenet): prepare_nodenet(test_nodenet) # save_nodenet micropsi.save_nodenet(test_nodenet) # unload_nodenet micropsi.unload_nodenet(test_nodenet) try: micropsi.get_nodes(test_nodenet) assert False, "could fetch a Nodespace that should not have been in memory" except: pass # load_nodenet micropsi.get_nodenet(test_nodenet) nodespace = micropsi.get_nodes(test_nodenet) assert len(nodespace["nodes"]) == 4 micropsi.delete_nodenet(test_nodenet)
def test_clone_nodes_copies_gate_params(fixed_nodenet): nodenet = micropsi.get_nodenet(fixed_nodenet) micropsi.set_gate_parameters(fixed_nodenet, 'n0001', 'gen', {'maximum': 0.1}) success, result = micropsi.clone_nodes(fixed_nodenet, ['n0001'], 'internal') assert success copy = nodenet.get_node(result['nodes'][0]['uid']) assert round(copy.get_gate_parameters()['gen']['maximum'], 2) == 0.1
def test_announce_nodes(test_nodenet): nodenet = micropsi.get_nodenet(test_nodenet) netapi = nodenet.netapi nodespace = netapi.create_nodespace(None, name="partition", options={ "new_partition": True, "initial_number_of_nodes": 2, "average_elements_per_node_assumption": 4, "initial_number_of_nodespaces": 1 }) # announce 20 pipe nodes netapi.announce_nodes(nodespace.uid, 20, 8) partition = nodespace.partition # 18 nodes needed assert partition.NoN == 26 # growby: 18 + 18//3 # 152 elements needed assert partition.NoE == 210 # growby: 152 + 152//3 for i in range(20): netapi.create_node("Pipe", nodespace.uid, "N %d" % i) # assert that we did not grow again assert partition.NoN == 26 assert partition.NoE == 210
def test_grow_partitions(test_nodenet): nodenet = micropsi.get_nodenet(test_nodenet) netapi = nodenet.netapi nodespace = netapi.create_nodespace(None, name="partition", options={ "new_partition": True, "initial_number_of_nodes": 2, "average_elements_per_node_assumption": 4, "initial_number_of_nodespaces": 1 }) for i in range(20): netapi.create_node("Pipe", nodespace.uid, "N %d" % i) partition = nodespace.partition # growby (NoN // 2): 2,3,4,6,9,13,19,28 assert len(partition.allocated_nodes) == 28 assert partition.NoE > 28 * 4 for i in range(2): netapi.create_nodespace(nodespace.uid, name="NS %d" % i) assert len(partition.allocated_nodespaces) == 4 # step, save, and load the net to make sure all data structures have been grown properly micropsi.step_nodenet(test_nodenet) micropsi.save_nodenet(test_nodenet) micropsi.revert_nodenet(test_nodenet) micropsi.step_nodenet(test_nodenet)
def test_modulators_sensor_actor_connection(test_nodenet, test_world): nodenet = micropsi.get_nodenet(test_nodenet) micropsi.set_nodenet_properties(test_nodenet, worldadapter="Braitenberg", world_uid=test_world) res, s1_id = micropsi.add_node(test_nodenet, "Sensor", [10, 10], None, name="brightness_l", parameters={'datasource': 'brightness_l'}) res, s2_id = micropsi.add_node(test_nodenet, "Sensor", [20, 20], None, name="emo_activation", parameters={'datasource': 'emo_activation'}) res, a1_id = micropsi.add_node(test_nodenet, "Actor", [30, 30], None, name="engine_l", parameters={'datatarget': 'engine_l'}) res, a2_id = micropsi.add_node(test_nodenet, "Actor", [40, 40], None, name="base_importance_of_intention", parameters={'datatarget': 'base_importance_of_intention'}) res, r1_id = micropsi.add_node(test_nodenet, "Register", [10, 30], None, name="r1") res, r2_id = micropsi.add_node(test_nodenet, "Register", [10, 30], None, name="r2") s1 = nodenet.get_node(s1_id) s2 = nodenet.get_node(s2_id) r1 = nodenet.get_node(r1_id) r2 = nodenet.get_node(r2_id) s2.set_gate_parameter('gen', 'maximum', 999) micropsi.add_link(test_nodenet, r1_id, 'gen', a1_id, 'gen') micropsi.add_link(test_nodenet, r2_id, 'gen', a2_id, 'gen') r1.activation = 0.3 r2.activation = 0.7 emo_val = nodenet.get_modulator("emo_activation") # patch reset method, to check if datatarget was written def nothing(): pass nodenet.worldadapter_instance.reset_datatargets = nothing nodenet.step() assert round(nodenet.worldadapter_instance.datatargets['engine_l'], 3) == 0.3 assert round(s1.activation, 3) == round(nodenet.worldadapter_instance.get_datasource_value('brightness_l'), 3) assert round(s2.activation, 3) == round(emo_val, 3) assert round(nodenet.get_modulator('base_importance_of_intention'), 3) == 0.7 assert round(nodenet.worldadapter_instance.datatargets['engine_l'], 3) == 0.3 emo_val = nodenet.get_modulator("emo_activation") nodenet.step() assert round(s2.activation, 3) == round(emo_val, 3)
def test_delete_nodespace_unlinks_native_module(test_nodenet, resourcepath): nodenet = micropsi.get_nodenet(test_nodenet) netapi = nodenet.netapi nodespace = netapi.create_nodespace(None, "foo") foopipe = netapi.create_node("Pipe", nodespace.uid, 'foopipe') import os nodetype_file = os.path.join(resourcepath, 'Test', 'nodetypes.json') nodefunc_file = os.path.join(resourcepath, 'Test', 'nodefunctions.py') with open(nodetype_file, 'w') as fp: fp.write('{"Testnode": {\ "name": "Testnode",\ "slottypes": ["gen", "foo", "bar"],\ "nodefunction_name": "testnodefunc",\ "gatetypes": ["gen", "foo", "bar"]}}') with open(nodefunc_file, 'w') as fp: fp.write("def testnodefunc(netapi, node=None, **prams):\r\n return 17") micropsi.reload_native_modules() testnode = netapi.create_node("Testnode", None, "test") netapi.link(testnode, 'foo', foopipe, 'sub') netapi.link(foopipe, 'sur', testnode, 'bar') micropsi.save_nodenet(test_nodenet) # I don't understand why, but this is necessary. micropsi.revert_nodenet(test_nodenet) netapi.delete_nodespace(nodespace) data = netapi.get_node(testnode.uid).get_data(include_links=True) assert data['links'] == {}
def test_delete_partition(test_nodenet): nodenet = micropsi.get_nodenet(test_nodenet) netapi = nodenet.netapi nodespace, source, register = prepare(netapi) netapi.delete_nodespace(nodespace) links = source.get_gate('gen').get_links() assert len(links) == 1 assert links[0].target_node == source
def test_partition_persistence(test_nodenet): nodenet = micropsi.get_nodenet(test_nodenet) netapi = nodenet.netapi nodespace, source, register = prepare(netapi) micropsi.save_nodenet(test_nodenet) micropsi.revert_nodenet(test_nodenet) nodenet.step() assert register.activation == 1
def test_set_agent_properties(test_world, test_nodenet): world = runtime.worlds[test_world] nodenet = runtime.get_nodenet(test_nodenet) nodenet.world = world runtime.load_nodenet(test_nodenet) runtime.set_worldagent_properties(test_world, test_nodenet, position=(5, 5)) assert world.agents[test_nodenet].position == (5, 5) assert world.data['agents'][test_nodenet]['position'] == (5, 5)
def test_node_states(test_nodenet, node): nodenet = micropsi.get_nodenet(test_nodenet) node = nodenet.get_node(node) assert node.get_state('foobar') is None node.set_state('foobar', 'bazbaz') assert node.get_state('foobar') == 'bazbaz' node.set_state('foobar', 42) assert node.get_state('foobar') == 42
def test_modulators(fixed_nodenet): nodenet = micropsi.get_nodenet(fixed_nodenet) nodenet.netapi.change_modulator("test_modulator", 0.42) assert nodenet.netapi.get_modulator("test_modulator") == 0.42 nodenet.set_modulator("test_modulator", -1) assert nodenet.netapi.get_modulator("test_modulator") == -1
def prepare(fixed_nodenet): nodenet = micropsi.get_nodenet(fixed_nodenet) netapi = nodenet.netapi source = netapi.create_node("Register", None, "Source") netapi.link(source, "gen", source, "gen") source.activation = 1 nodenet.step() return nodenet, netapi, source
def test_worldadapter_update_calls_reset_datatargets(test_world, test_nodenet): world = runtime.worlds[test_world] nodenet = runtime.get_nodenet(test_nodenet) runtime.load_nodenet(test_nodenet) nodenet.world = test_world runtime.set_nodenet_properties(nodenet.uid, worldadapter='Braitenberg', world_uid=world.uid) world.agents[test_nodenet].reset_datatargets = mock.MagicMock(name='reset') runtime.step_nodenet(test_nodenet) world.agents[test_nodenet].reset_datatargets.assert_called_once()
def test_entity_positions_as_tuples(test_nodenet): nodenet = micropsi.get_nodenet(test_nodenet) api = nodenet.netapi node = api.create_node("Pipe", None, "node1") nodespace = api.create_nodespace(None, "nodespace1") node.position = (23, 42) nodespace.position = (13, 23, 42) assert node.position == [23, 42, 0] assert nodespace.position == [13, 23, 42]
def test_clone_nodes_copies_gate_params(fixed_nodenet): nodenet = micropsi.get_nodenet(fixed_nodenet) micropsi.set_gate_parameters(fixed_nodenet, 'n0001', 'gen', {'maximum': 0.1}) success, result = micropsi.clone_nodes(fixed_nodenet, ['n0001'], 'internal') assert success copy = nodenet.get_node(list(result.keys())[0]) assert round(copy.get_gate_parameters()['gen']['maximum'], 2) == 0.1
def test_nodetype_function_definition_overwrites_default_function_name_theano(fixed_nodenet): nodenet = micropsi.get_nodenet(fixed_nodenet) nodetype = nodenet.get_standard_nodetype_definitions()['Register'].copy() foo = Nodetype(nodenet=nodenet, **nodetype) assert foo.nodefunction == register nodetype['nodefunction_definition'] = 'return 17' foo = Nodetype(nodenet=nodenet, **nodetype) assert foo.nodefunction != register assert foo.nodefunction(nodenet, None) == 17
def test_worldadapter_update_calls_reset_datatargets(test_world, test_nodenet): world = runtime.worlds[test_world] nodenet = runtime.get_nodenet(test_nodenet) runtime.load_nodenet(test_nodenet) nodenet.world = world runtime.set_nodenet_properties(nodenet.uid, worldadapter='Braitenberg', world_uid=world.uid) world.agents[test_nodenet].reset_datatargets = mock.MagicMock(name='reset') runtime.step_nodenet(test_nodenet) world.agents[test_nodenet].reset_datatargets.assert_called_once()
def test_nodetype_function_definition_overwrites_default_function_name(fixed_nodenet): nodenet = micropsi.get_nodenet(fixed_nodenet) nodetype = nodenet.get_standard_nodetype_definitions()['Concept'].copy() foo = Nodetype(nodenet=nodenet, **nodetype) assert foo.nodefunction == concept nodetype['nodefunction_definition'] = 'return 17' foo = Nodetype(nodenet=nodenet, **nodetype) assert foo.nodefunction != concept assert foo.nodefunction(nodenet, None) == 17
def test_plot_activations(test_nodenet): from random import random nodenet = micropsi.get_nodenet(test_nodenet) vizapi = nodenet.netapi.vizapi activations = [random() for i in range(256)] plot = vizapi.NodenetPlot(plotsize=(2, 2)) plot.add_activation_plot(activations) res = plot.to_base64(format="png") assert len(res) > 1000 assert res.endswith('\n')
def prepare(fixed_nodenet): nodenet = micropsi.get_nodenet(fixed_nodenet) netapi = nodenet.netapi netapi.delete_node(netapi.get_node("ACTA")) netapi.delete_node(netapi.get_node("ACTB")) source = netapi.create_node("Register", "Root", "Source") netapi.link(source, "gen", source, "gen") source.activation = 1 nodenet.step() return nodenet, netapi, source
def test_reset_datatargets(test_world, test_nodenet): world = runtime.worlds[test_world] nodenet = runtime.get_nodenet(test_nodenet) runtime.load_nodenet(test_nodenet) nodenet.world = world runtime.set_nodenet_properties(nodenet.uid, worldadapter='Braitenberg', world_uid=world.uid) world.agents[test_nodenet].datatargets['engine_r'] = 0.7 world.agents[test_nodenet].datatargets['engine_l'] = 0.2 world.agents[test_nodenet].reset_datatargets() assert world.agents[test_nodenet].datatargets['engine_l'] == 0 assert world.agents[test_nodenet].datatargets['engine_r'] == 0