def test_single_agent_mode(): mode = micropsi.cfg['micropsi2'].get('single_agent_mode') micropsi.cfg['micropsi2'].update({'single_agent_mode': '1'}) res, uid1 = micropsi.new_nodenet("test1") res, uid2 = micropsi.new_nodenet("test2") assert uid1 not in micropsi.nodenets micropsi.cfg['micropsi2'].update({'single_agent_mode': mode})
def test_multiple_nodenet_interference(engine, resourcepath): 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"],\ "gatetypes": ["gen", "foo", "bar"],\ "nodefunction_name": "testnodefunc"\ }}') with open(nodefunc_file, 'w') as fp: fp.write( "def testnodefunc(netapi, node=None, **prams):\r\n node.get_gate('gen').gate_function(17)" ) micropsi.reload_native_modules() result, n1_uid = micropsi.new_nodenet('Net1', engine=engine, owner='Pytest User') result, n2_uid = micropsi.new_nodenet('Net2', engine=engine, owner='Pytest User') n1 = micropsi.nodenets[n1_uid] n2 = micropsi.nodenets[n2_uid] nativemodule = n1.netapi.create_node("Testnode", None, "Testnode") register1 = n1.netapi.create_node("Register", None, "Register1") n1.netapi.link(nativemodule, 'gen', register1, 'gen', weight=1.2) source2 = n2.netapi.create_node("Register", None, "Source2") register2 = n2.netapi.create_node("Register", None, "Register2") n2.netapi.link(source2, 'gen', source2, 'gen') n2.netapi.link(source2, 'gen', register2, 'gen', weight=0.9) source2.activation = 0.7 micropsi.step_nodenet(n2.uid) assert n1.current_step == 0 assert register1.activation == 0 assert register1.name == "Register1" assert nativemodule.name == "Testnode" assert round(register1.get_slot('gen').get_links()[0].weight, 2) == 1.2 assert register1.get_slot( 'gen').get_links()[0].source_node.name == 'Testnode' assert n1.get_node(register1.uid).name == "Register1" assert n2.current_step == 1 assert round(source2.activation, 2) == 0.7 assert round(register2.activation, 2) == 0.63 assert register2.name == "Register2" assert source2.name == "Source2" assert round(register2.get_slot('gen').get_links()[0].weight, 2) == 0.9 assert register2.get_slot( 'gen').get_links()[0].source_node.name == 'Source2' assert n2.get_node(register2.uid).name == "Register2"
def test_nodenet_specific_loggers(): res, uid1 = micropsi.new_nodenet("test1") res, uid2 = micropsi.new_nodenet("test2") assert "agent.%s" % uid1 in logging.Logger.manager.loggerDict assert "agent.%s" % uid2 in logging.Logger.manager.loggerDict logging.getLogger("agent.%s" % uid1).warning("hello!") res = micropsi.get_logger_messages("agent.%s" % uid1) item = res['logs'][-1] assert item['msg'] == "hello!" assert item['step'] == 0
def test_multiple_nodenet_interference(engine, resourcepath): 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"],\ "gatetypes": ["gen", "foo", "bar"],\ "nodefunction_name": "testnodefunc"\ }}') with open(nodefunc_file, 'w') as fp: fp.write("def testnodefunc(netapi, node=None, **prams):\r\n node.get_gate('gen').gate_function(17)") micropsi.reload_native_modules() result, n1_uid = micropsi.new_nodenet('Net1', engine=engine, owner='Pytest User') result, n2_uid = micropsi.new_nodenet('Net2', engine=engine, owner='Pytest User') n1 = micropsi.nodenets[n1_uid] n2 = micropsi.nodenets[n2_uid] nativemodule = n1.netapi.create_node("Testnode", None, "Testnode") register1 = n1.netapi.create_node("Register", None, "Register1") n1.netapi.link(nativemodule, 'gen', register1, 'gen', weight=1.2) source2 = n2.netapi.create_node("Register", None, "Source2") register2 = n2.netapi.create_node("Register", None, "Register2") n2.netapi.link(source2, 'gen', source2, 'gen') n2.netapi.link(source2, 'gen', register2, 'gen', weight=0.9) source2.activation = 0.7 micropsi.step_nodenet(n2.uid) assert n1.current_step == 0 assert register1.activation == 0 assert register1.name == "Register1" assert nativemodule.name == "Testnode" assert round(register1.get_slot('gen').get_links()[0].weight, 2) == 1.2 assert register1.get_slot('gen').get_links()[0].source_node.name == 'Testnode' assert n1.get_node(register1.uid).name == "Register1" assert n2.current_step == 1 assert round(source2.activation, 2) == 0.7 assert round(register2.activation, 2) == 0.63 assert register2.name == "Register2" assert source2.name == "Source2" assert round(register2.get_slot('gen').get_links()[0].weight, 2) == 0.9 assert register2.get_slot('gen').get_links()[0].source_node.name == 'Source2' assert n2.get_node(register2.uid).name == "Register2"
def test_nodenet(request, test_world, engine): global nn_uid nodenets = micropsi.get_available_nodenets("Pytest User") or {} if nn_uid not in nodenets: success, nn_uid = micropsi.new_nodenet("Testnet", engine=engine, owner="Pytest User", uid='Testnet') micropsi.save_nodenet(nn_uid) return nn_uid
def test_unregister_logger(): res, uid1 = micropsi.new_nodenet("test1") logging.getLogger("agent.%s" % uid1).warning('hello!') micropsi.delete_nodenet(uid1) assert "agent.%s" % uid1 not in micropsi.logger.loggers assert "agent.%s" % uid1 not in micropsi.logger.record_storage assert "agent.%s" % uid1 not in micropsi.logger.handlers
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 test_new_nodenet(test_nodenet, resourcepath, engine): success, nodenet_uid = micropsi.new_nodenet("Test_Nodenet", engine=engine, worldadapter="Default", owner="tester") assert success assert nodenet_uid != test_nodenet assert micropsi.get_available_nodenets( "tester")[nodenet_uid].name == "Test_Nodenet" n_path = os.path.join(resourcepath, runtime.NODENET_DIRECTORY, nodenet_uid + ".json") assert os.path.exists(n_path) # get_available_nodenets nodenets = micropsi.get_available_nodenets() mynets = micropsi.get_available_nodenets("tester") assert test_nodenet in nodenets assert nodenet_uid in nodenets assert nodenet_uid in mynets assert test_nodenet not in mynets # delete_nodenet micropsi.delete_nodenet(nodenet_uid) assert nodenet_uid not in micropsi.get_available_nodenets() assert not os.path.exists(n_path)
def test_node_logic_sensor_nomodulators(engine, default_world): result, nnuid = micropsi.new_nodenet("adf", engine, "Default", world_uid=default_world, use_modulators=False) net, netapi, source = prepare(nnuid) register = netapi.create_node("Register", None) netapi.link_sensor(register, "static_on", "gen", weight=0.4) micropsi.step_nodenet(nnuid) micropsi.step_nodenet(nnuid) assert round(register.get_gate("gen").activation, 1) == 0.4
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 write_nodenet(): user_id, permissions, token = get_request_data() if "manage nodenets" in permissions: result, nodenet_uid = runtime.new_nodenet(request.params['nn_name'], engine=request.params['nn_engine'], worldadapter=request.params['nn_worldadapter'], template=request.params.get('nn_template'), owner=user_id, world_uid=request.params.get('nn_world')) if result: return dict(status="success", msg="Nodenet created", nodenet_uid=nodenet_uid) else: return dict(status="error", msg="Error saving nodenet: %s" % nodenet_uid) return dict(status="error", msg="Insufficient rights to write nodenet")
def write_nodenet(): user_id, permissions, token = get_request_data() params = dict((key, request.forms.getunicode(key)) for key in request.forms) if "manage nodenets" in permissions: result, nodenet_uid = runtime.new_nodenet(params['nn_name'], engine=params['nn_engine'], worldadapter=params['nn_worldadapter'], template=params.get('nn_template'), owner=user_id, world_uid=params.get('nn_world'), use_modulators=params.get('nn_modulators', False)) if result: return dict(status="success", msg="Nodenet created", nodenet_uid=nodenet_uid) else: return dict(status="error", msg="Error saving nodenet: %s" % nodenet_uid) return dict(status="error", msg="Insufficient rights to write nodenet")
def new_nodenet(name, owner=None, engine='dict_engine', template=None, worldadapter=None, world_uid=None): if owner is None: owner, _, _ = get_request_data() return runtime.new_nodenet( name, engine=engine, worldadapter=worldadapter, template=template, owner=owner, world_uid=world_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_node_logic_actor_nomodulators(engine, default_world): result, nnuid = micropsi.new_nodenet("adf", engine, "Default", world_uid=default_world, use_modulators=False) net, netapi, source = prepare(nnuid) netapi.link_actor(source, "echo", weight=0.7, gate="gen") register = netapi.create_node("Register", None) actor = netapi.get_nodes(node_name_prefix="echo")[0] netapi.link(actor, "gen", register, "gen") micropsi.step_nodenet(nnuid) micropsi.step_nodenet(nnuid) micropsi.step_nodenet(nnuid) assert round(register.get_gate("gen").activation, 1) == 0.7
def new_nodenet(name, owner=None, engine='dict_engine', template=None, worldadapter=None, world_uid=None, use_modulators=None): if owner is None: owner, _, _ = get_request_data() return runtime.new_nodenet( name, engine=engine, worldadapter=worldadapter, template=template, owner=owner, world_uid=world_uid, use_modulators=use_modulators)
def test_nodenet(request, test_world, engine): """ Fixture: A completely empty nodenet without a worldadapter """ global nn_uid success, nn_uid = micropsi.new_nodenet("Testnet", engine=engine, owner="Pytest User", uid='Testnet') micropsi.save_nodenet(nn_uid) yield nn_uid try: micropsi.delete_nodenet(nn_uid) except: pass
def default_nodenet(request): """ A nodenet with the default engine Use this for tests that are engine-agnostic """ success, nn_uid = micropsi_runtime.new_nodenet("Defaultnet", owner="Pytest User") micropsi_runtime.save_nodenet(nn_uid) yield nn_uid try: micropsi_runtime.delete_nodenet(nn_uid) except: pass
def test_nodenet(request): global nn_uid nodenets = micropsi.get_available_nodenets("Pytest User") or {} if nn_uid not in nodenets: success, nn_uid = micropsi.new_nodenet("Testnet", worldadapter="Default", owner="Pytest User", world_uid=world_uid, uid='Testnet') micropsi.add_node(nn_uid, 'Concept', [10, 10], uid='N1', name='N1') micropsi.add_link(nn_uid, 'N1', 'gen', 'N1', 'gen') micropsi.save_nodenet(nn_uid) def fin(): micropsi.revert_nodenet(nn_uid) request.addfinalizer(fin) return nn_uid
def test_nodenet(request, default_world, engine): """ An empty nodenet, with the currently tested engine. Use this for tests that should run in both engines """ global nn_uid success, nn_uid = micropsi_runtime.new_nodenet("Testnet", engine=engine, owner="Pytest User") micropsi_runtime.save_nodenet(nn_uid) yield nn_uid try: micropsi_runtime.delete_nodenet(nn_uid) except: pass
def test_nodenet(request): global nn_uid nodenets = micropsi.get_available_nodenets("Pytest User") or {} for uid, nn in nodenets.items(): if(nn.name == 'Testnet'): nn_uid = list(nodenets.keys())[0] else: success, nn_uid = micropsi.new_nodenet("Testnet", "Default", owner="Pytest User", world_uid=world_uid, uid='Testnet') def fin(): if DELETE_TEST_FILES_ON_EXIT: micropsi.delete_nodenet(nn_uid) request.addfinalizer(fin) return nn_uid
def new_nodenet(name, owner=None, engine='dict_engine', template=None, worldadapter=None, world_uid=None, use_modulators=None, worldadapter_config={}, device_map={}): """ Create a new nodenet with the given configuration """ if owner is None: owner, _, _ = get_request_data() return runtime.new_nodenet( name, engine=engine, worldadapter=worldadapter, template=template, owner=owner, world_uid=world_uid, use_modulators=use_modulators, worldadapter_config=worldadapter_config, device_map=device_map)
def test_create_nodenet_from_template(test_nodenet, node, engine): mode = micropsi.cfg['micropsi2'].get('single_agent_mode') micropsi.cfg['micropsi2'].update({'single_agent_mode': '1'}) api = micropsi.nodenets[test_nodenet].netapi node1 = api.get_node(node) node2 = api.create_node("Register", None, "node2") api.link(node1, 'gen', node2, 'gen') micropsi.save_nodenet(test_nodenet) result, uid = micropsi.new_nodenet('copynet', engine=engine, template=test_nodenet) data = micropsi.get_nodes(uid) for uid, n in data['nodes'].items(): if n['name'] == node1.name: assert len(n['links']['gen']) == 2 else: assert n['name'] == node2.name micropsi.cfg['micropsi2'].update({'single_agent_mode': mode})
def write_nodenet(): user_id, permissions, token = get_request_data() params = dict((key, request.forms.getunicode(key)) for key in request.forms) worldadapter_name = params['nn_worldadapter'] wa_params = {} device_map = {} for key in params: if key.startswith('worldadapter_%s_' % worldadapter_name): strip = len("worldadapter_%s_" % worldadapter_name) wa_params[key[strip:]] = params[key] elif key.startswith('device-map-'): uid = key[11:] device_map[uid] = params['device-name-%s' % uid] if "manage nodenets" in permissions: if not params.get('nodenet_uid'): result, nodenet_uid = runtime.new_nodenet( params['nn_name'], engine=params['nn_engine'], worldadapter=params['nn_worldadapter'], template=params.get('nn_template'), owner=user_id, world_uid=params.get('nn_world'), use_modulators=params.get('nn_modulators', False), worldadapter_config=wa_params, device_map=device_map) if result: return dict(status="success", msg="Agent created", nodenet_uid=nodenet_uid) else: return dict(status="error", msg="Error saving agent: %s" % nodenet_uid) else: result = runtime.set_nodenet_properties( params['nodenet_uid'], nodenet_name=params['nn_name'], worldadapter=params['nn_worldadapter'], world_uid=params['nn_world'], owner=user_id, worldadapter_config=wa_params, device_map=device_map) if result: return dict(status="success", msg="Changes saved") else: return dict(status="error", msg="Error saving changes!") return dict(status="error", msg="Insufficient rights to write agent")
def test_new_nodenet(test_nodenet, resourcepath, engine): success, nodenet_uid = micropsi.new_nodenet("Test_Nodenet", engine=engine, worldadapter="Default", owner="tester") assert success assert nodenet_uid != test_nodenet assert micropsi.get_available_nodenets("tester")[nodenet_uid].name == "Test_Nodenet" n_path = os.path.join(resourcepath, runtime.NODENET_DIRECTORY, nodenet_uid + ".json") assert os.path.exists(n_path) # get_available_nodenets nodenets = micropsi.get_available_nodenets() mynets = micropsi.get_available_nodenets("tester") assert test_nodenet in nodenets assert nodenet_uid in nodenets assert nodenet_uid in mynets assert test_nodenet not in mynets # delete_nodenet micropsi.delete_nodenet(nodenet_uid) assert nodenet_uid not in micropsi.get_available_nodenets() assert not os.path.exists(n_path)
def test_modulators(fixed_nodenet, engine): nodenet = micropsi.get_nodenet(fixed_nodenet) # assert modulators are instantiated from the beginning assert nodenet._modulators != {} assert nodenet.get_modulator('emo_activation') is not None # set a modulator nodenet.set_modulator("test_modulator", -1) assert nodenet.netapi.get_modulator("test_modulator") == -1 # assert change_modulator sets diff. nodenet.netapi.change_modulator("test_modulator", 0.42) assert round(nodenet.netapi.get_modulator("test_modulator"), 4) == -0.58 # no modulators should be set if we disable the emotional_parameter module res, uid = micropsi.new_nodenet('foobar', engine, use_modulators=False) new_nodenet = micropsi.get_nodenet(uid) assert new_nodenet._modulators == {} # and no Emo-stepoperator should be set. for item in new_nodenet.stepoperators: assert 'Emotional' not in item.__class__.__name__
def test_copy_nodes(): success, nodenet_uid1 = micropsi.new_nodenet("Origin_Nodenet", "Default", owner="tester") success, nodenet_uid2 = micropsi.new_nodenet("Target_Nodenet", "Default", owner="tester") # create a few nodes assert nodenet_uid1 not in micropsi.nodenets micropsi.load_nodenet(nodenet_uid1) assert nodenet_uid1 in micropsi.nodenets micropsi.load_nodenet(nodenet_uid2) assert nodenet_uid1 in micropsi.nodenets assert nodenet_uid2 in micropsi.nodenets micropsi.add_node(nodenet_uid1, "Nodespace", (100, 150), "Root", uid="ns1") micropsi.add_node(nodenet_uid1, "Nodespace", (200, 150), "Root", uid="confl") micropsi.add_node(nodenet_uid1, "Nodespace", (400, 150), "Root", uid="ns2") micropsi.add_node(nodenet_uid2, "Nodespace", (200, 150), "Root", uid="confl") micropsi.add_node(nodenet_uid1, "Register", (300, 140), "Root", uid="n1") micropsi.add_node(nodenet_uid1, "Register", (300, 240), "Root", uid="n2") micropsi.add_node(nodenet_uid1, "Register", (300, 340), "Root", uid="associated_node") micropsi.add_node(nodenet_uid1, "Register", (400, 240), "ns1", uid="n3") micropsi.add_node(nodenet_uid1, "Register", (400, 240), "ns2", uid="n4") micropsi.add_node(nodenet_uid1, "Register", (100, 240), "confl", uid="n5") micropsi.add_node(nodenet_uid2, "Register", (100, 140), "Root", uid="n1") micropsi.add_node(nodenet_uid2, "Register", (150, 240), "Root", uid="nt2") micropsi.add_link(nodenet_uid1, "n1", "gen", "n2", "gen", uid="l1") micropsi.add_link(nodenet_uid1, "n2", "gen", "n3", "gen", uid="l2") micropsi.add_link(nodenet_uid1, "n1", "gen", "associated_node", "gen", uid="la") micropsi.add_link(nodenet_uid1, "n3", "gen", "n1", "gen", uid="l3") micropsi.add_link(nodenet_uid1, "n4", "gen", "n1", "gen", uid="l4") micropsi.add_link(nodenet_uid2, "n1", "gen", "nt2", "gen", uid="l1") # now copy stuff between nodespaces micropsi.copy_nodes( ["n1", "n2", "n3", "n5", "ns1", "confl"], nodenet_uid1, nodenet_uid2) micropsi.save_nodenet(nodenet_uid1) micropsi.save_nodenet(nodenet_uid2) target = micropsi.get_nodespace(nodenet_uid2, "Root", -1) assert len(target["nodes"]) == 4 + 2 assert len(target["nodespaces"]) == 2 + 2 assert "n1" in target["nodes"] assert "n2" in target["nodes"] assert "n3" in target["nodes"] assert "associated_node" not in target["nodes"] assert "n4" not in target["nodes"] assert "n5" in target["nodes"] assert "nt2" in target["nodes"] assert "ns1" in target["nodespaces"] assert "ns2" not in target["nodespaces"] assert "confl" in target["nodespaces"] assert len(target["links"]) == 3 + 1 assert "l1" in target["links"] assert "l2" in target["links"] assert "l3" in target["links"] assert "l4" not in target["links"] # we should also test for parentage and link connectivity # TODO now test copying within the same nodenet micropsi.copy_nodes( ["n1", "n2", "n3", "n5", "ns1", "confl"], nodenet_uid1, nodenet_uid1, target_nodespace_uid="ns2" ) micropsi.save_nodenet(nodenet_uid1) # delete_nodenets micropsi.delete_nodenet(nodenet_uid1) micropsi.delete_nodenet(nodenet_uid2)