Ejemplo n.º 1
0
def test_save_nodenet(test_nodenet):
    # save_nodenet
    micropsi.save_nodenet(test_nodenet)
    # unload_nodenet
    micropsi.unload_nodenet(test_nodenet)
    try:
        micropsi.get_nodenet_data(test_nodenet, "Root")
        assert False, "could fetch a Nodespace that should not have been in memory"
    except:
        pass
    # load_nodenet
    micropsi.load_nodenet(test_nodenet)
    nodespace = micropsi.get_nodenet_data(test_nodenet, "Root")
    assert len(nodespace["nodes"]) == 4
Ejemplo n.º 2
0
def test_delete_link(test_nodenet):
    nodes = prepare_nodenet(test_nodenet)
    success, link = micropsi.add_link(test_nodenet, nodes['a'], "por", nodes['b'], "gen", 0.5, 1)
    assert success
    micropsi.delete_link(test_nodenet, nodes['a'], "por", nodes['b'], "gen")
    nodespace = micropsi.get_nodenet_data(test_nodenet, None)
    assert len(nodespace["links"]) == 0
Ejemplo n.º 3
0
def load_nodenet(nodenet_uid, **coordinates):
    result, uid = runtime.load_nodenet(nodenet_uid)
    if not result:
        return dict(Error=uid)
    data = runtime.get_nodenet_data(nodenet_uid, **coordinates)
    data['nodetypes'] = runtime.get_available_node_types(nodenet_uid)
    return data
def test_user_prompt(fixed_nodenet):
    options = [{'key': 'foo_parameter', 'label': 'Please give value for "foo"', 'values':  [23, 42]}]
    micropsi.nodenets[fixed_nodenet].netapi.ask_user_for_parameter(
        micropsi.nodenets[fixed_nodenet].get_node('A1'),
        "foobar",
        options
    )
    data = micropsi.get_nodenet_data(fixed_nodenet, 'Root')
    assert 'user_prompt' in data
    assert data['user_prompt']['msg'] == 'foobar'
    assert data['user_prompt']['node']['uid'] == 'A1'
    assert data['user_prompt']['options'] == options
    # response
    micropsi.user_prompt_response(fixed_nodenet, 'A1', {'foo_parameter': 42}, True)
    assert micropsi.nodenets[fixed_nodenet].get_node('A1').get_parameter('foo_parameter') == 42
    assert micropsi.nodenets[fixed_nodenet].is_active
    from micropsi_core.nodenet import nodefunctions
    nodefunc = mock.Mock()
    nodefunctions.concept = nodefunc
    micropsi.nodenets[fixed_nodenet].step()
    foo = micropsi.nodenets[fixed_nodenet].get_node('A1').clone_parameters()
    foo.update({'foo_parameter': 42})
    assert nodefunc.called_with(micropsi.nodenets[fixed_nodenet].netapi, micropsi.nodenets[fixed_nodenet].get_node('A1'), foo)
    micropsi.nodenets[fixed_nodenet].get_node('A1').clear_parameter('foo_parameter')
    assert micropsi.nodenets[fixed_nodenet].get_node('A1').get_parameter('foo_parameter') is None
Ejemplo n.º 5
0
def test_get_nodespace(test_nodenet):
    nodes = prepare_nodenet(test_nodenet)
    nodespace = micropsi.get_nodenet_data(test_nodenet, None)
    assert len(nodespace["nodes"]) == 4
    node1 = nodespace["nodes"][nodes['a']]
    assert node1["name"] == "A"
    assert node1["position"] == (200, 250)
Ejemplo n.º 6
0
def test_add_link(test_nodenet):
    nodes = prepare_nodenet(test_nodenet)
    micropsi.add_link(test_nodenet, nodes['a'], "por", nodes['b'], "gen", 0.5, 1)
    micropsi.add_link(test_nodenet, nodes['a'], "por", nodes['b'], "gen", 1, 0.1)
    micropsi.add_link(test_nodenet, nodes['c'], "ret", nodes['b'], "gen", 1, 1)

    nodespace = micropsi.get_nodenet_data(test_nodenet, None)
    assert len(nodespace["nodes"]) == 4
    assert len(nodespace["links"]) == 2
    link1 = None
    link2 = None
    for uid, data in nodespace["links"].items():
        if data['source_node_uid'] == nodes['a']:
            link1 = data
        else:
            link2 = data

    assert link1["weight"] == 1
    # assert link1["certainty"] == 0.1
    assert link1["source_node_uid"] == nodes['a']
    assert link1["target_node_uid"] == nodes['b']
    assert link1["source_gate_name"] == "por"
    assert link1["target_slot_name"] == "gen"

    assert link2["source_node_uid"] == nodes['c']
    assert link2["target_node_uid"] == nodes['b']
    assert link2["source_gate_name"] == "ret"
    assert link2["target_slot_name"] == "gen"
Ejemplo n.º 7
0
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_nodenet_data(test_nodenet, None)
        assert False, "could fetch a Nodespace that should not have been in memory"
    except:
        pass
    # load_nodenet
    micropsi.load_nodenet(test_nodenet)
    nodespace = micropsi.get_nodenet_data(test_nodenet, None)
    assert len(nodespace["nodes"]) == 4
    micropsi.delete_nodenet(test_nodenet)
Ejemplo n.º 8
0
def load_nodenet(nodenet_uid, nodespace='Root', coordinates={}):
    result, uid = runtime.load_nodenet(nodenet_uid)
    if result:
        data = runtime.get_nodenet_data(nodenet_uid, nodespace, coordinates)
        data['nodetypes'] = runtime.get_available_node_types(nodenet_uid)
        data['recipes'] = runtime.get_available_recipes()
        return True, data
    else:
        return False, uid
Ejemplo n.º 9
0
def load_nodenet(nodenet_uid, nodespace='Root', include_links=True):
    result, uid = runtime.load_nodenet(nodenet_uid)
    if result:
        data = runtime.get_nodenet_data(nodenet_uid, nodespace, -1, include_links)
        data['nodetypes'] = runtime.get_available_node_types(nodenet_uid)
        data['recipes'] = runtime.get_available_recipes()
        return True, data
    else:
        return False, uid
Ejemplo n.º 10
0
def test_add_node(test_nodenet):
    micropsi.load_nodenet(test_nodenet)
    # make sure nodenet is empty
    nodespace = micropsi.get_nodenet_data(test_nodenet, None)
    try:
        for i in nodespace["nodes"]:
            micropsi.delete_node(test_nodenet, i)
    except:
        pass

    nodespace = micropsi.get_nodenet_data(test_nodenet, None)
    assert len(nodespace.get("nodes", [])) == 0
    assert len(nodespace.get("links", [])) == 0
    res, uid = micropsi.add_node(test_nodenet, "Pipe", (200, 250), None, state=None, name="A")
    nodespace = micropsi.get_nodenet_data(test_nodenet, None)
    assert len(nodespace["nodes"]) == 1
    node1 = nodespace["nodes"][uid]
    assert node1["name"] == "A"
    assert node1["position"] == (200, 250)
Ejemplo n.º 11
0
def test_non_standard_gate_defaults(fixed_nodenet):
    nodenet = micropsi.nodenets[fixed_nodenet]
    res, uid = micropsi.add_node(fixed_nodenet, 'Register', [30, 30], name='test')
    node = nodenet.netapi.get_node(uid)
    genparams = {'maximum': 0.5}
    micropsi.set_gate_parameters(nodenet.uid, node.uid, 'gen', genparams)
    assert node.clone_non_default_gate_parameters()['gen']['maximum'] == 0.5
    assert node.data['gate_parameters'] == {'gen': {'maximum': 0.5}}
    assert nodenet.data['nodes'][uid]['gate_parameters'] == {'gen': {'maximum': 0.5}}
    data = micropsi.get_nodenet_data(fixed_nodenet, None, step=-1, include_links=False)
    assert data['nodes'][uid]['gate_parameters'] == {'gen': {'maximum': 0.5}}
Ejemplo n.º 12
0
def test_add_node(test_nodenet):
    micropsi.load_nodenet(test_nodenet)
    # make sure nodenet is empty
    nodespace = micropsi.get_nodenet_data(test_nodenet, "Root")
    try:
        for i in nodespace["nodes"]:
            micropsi.delete_node(test_nodenet, i)
    except:
        pass

    nodespace = micropsi.get_nodenet_data(test_nodenet, "Root")
    assert len(nodespace.get("nodes", [])) == 0
    assert len(nodespace.get("links", [])) == 0
    micropsi.add_node(test_nodenet, "Concept", (200, 250), "Root", state=None, uid="node_a", name="A")
    nodespace = micropsi.get_nodenet_data(test_nodenet, "Root")
    assert len(nodespace["nodes"]) == 1
    node1 = nodespace["nodes"]["node_a"]
    assert node1["name"] == "A"
    assert node1["position"] == (200, 250)

    micropsi.add_node(test_nodenet, "Concept", (500, 350), "Root", state=None, uid="node_b", name="B")
    micropsi.add_node(test_nodenet, "Concept", (300, 150), "Root", state=None, uid="node_c", name="C")
    micropsi.add_node(test_nodenet, "Sensor", (200, 450), "Root", state=None, uid="node_s", name="S")
Ejemplo n.º 13
0
def get_current_state(nodenet_uid, nodenet=None, world=None, monitors=None):
    data = {}
    nodenet_obj = runtime.get_nodenet(nodenet_uid)
    if nodenet_obj is not None:
        data['simulation_running'] = nodenet_obj.is_active
        data['current_nodenet_step'] = nodenet_obj.current_step
        data['current_world_step'] = nodenet_obj.world.current_step if nodenet_obj.world else 0
        if nodenet is not None:
            data['nodenet'] = runtime.get_nodenet_data(nodenet_uid=nodenet_uid, **nodenet)
        if world is not None and nodenet_obj.world:
            data['world'] = runtime.get_world_view(world_uid=nodenet_obj.world.uid, **world)
        if monitors is not None:
            data['monitors'] = runtime.get_monitoring_info(nodenet_uid=nodenet_uid, **monitors)
        return True, data
    else:
        return False, "No such nodenet"
def test_user_prompt(fixed_nodenet, nodetype_def, nodefunc_def):
    with open(nodetype_def, 'w') as fp:
        fp.write('{"Testnode": {\
            "name": "Testnode",\
            "slottypes": ["gen", "foo", "bar"],\
            "gatetypes": ["gen", "foo", "bar"],\
            "nodefunction_name": "testnodefunc",\
            "parameters": ["testparam"],\
            "parameter_defaults": {\
                "testparam": 13\
              }\
            }}')
    with open(nodefunc_def, 'w') as fp:
        fp.write("def testnodefunc(netapi, node=None, **prams):\r\n    return 17")

    micropsi.reload_native_modules()
    res, uid = micropsi.add_node(fixed_nodenet, "Testnode", [10, 10], name="Test")
    nativemodule = micropsi.nodenets[fixed_nodenet].get_node(uid)

    options = [{'key': 'foo_parameter', 'label': 'Please give value for "foo"', 'values': [23, 42]}]
    micropsi.nodenets[fixed_nodenet].netapi.ask_user_for_parameter(
        nativemodule,
        "foobar",
        options
    )
    data = micropsi.get_nodenet_data(fixed_nodenet, 'Root')
    assert 'user_prompt' in data
    assert data['user_prompt']['msg'] == 'foobar'
    assert data['user_prompt']['node']['uid'] == uid
    assert data['user_prompt']['options'] == options
    # response
    micropsi.user_prompt_response(fixed_nodenet, uid, {'foo_parameter': 42}, True)
    assert micropsi.nodenets[fixed_nodenet].get_node(uid).get_parameter('foo_parameter') == 42
    assert micropsi.nodenets[fixed_nodenet].is_active
    from micropsi_core.nodenet import nodefunctions
    tmp = nodefunctions.concept
    nodefunc = mock.Mock()
    nodefunctions.concept = nodefunc
    micropsi.nodenets[fixed_nodenet].step()
    foo = micropsi.nodenets[fixed_nodenet].get_node('n0001').clone_parameters()
    foo.update({'foo_parameter': 42})
    assert nodefunc.called_with(micropsi.nodenets[fixed_nodenet].netapi, micropsi.nodenets[fixed_nodenet].get_node('n0001'), foo)
    micropsi.nodenets[fixed_nodenet].get_node('n0001').clear_parameter('foo_parameter')
    assert micropsi.nodenets[fixed_nodenet].get_node('n0001').get_parameter('foo_parameter') is None
    nodefunctions.concept = tmp
Ejemplo n.º 15
0
def test_node_parameter_defaults(fixed_nodenet, resourcepath, nodetype_def, nodefunc_def):
    with open(nodetype_def, 'w') as fp:
        fp.write('{"Testnode": {\
            "name": "Testnode",\
            "slottypes": ["gen", "foo", "bar"],\
            "gatetypes": ["gen", "foo", "bar"],\
            "nodefunction_name": "testnodefunc",\
            "parameters": ["testparam"],\
            "parameter_defaults": {\
                "testparam": 13\
              }\
            }}')
    with open(nodefunc_def, 'w') as fp:
        fp.write("def testnodefunc(netapi, node=None, **prams):\r\n    return 17")

    micropsi.reload_native_modules()
    data = micropsi.get_nodenet_data(fixed_nodenet, None)
    res, uid = micropsi.add_node(fixed_nodenet, "Testnode", [10, 10], name="Test")
    node = micropsi.nodenets[fixed_nodenet].get_node(uid)
    assert node.get_parameter("testparam") == 13
Ejemplo n.º 16
0
def get_current_state(nodenet_uid, nodenet=None, world=None, monitors=None):
    data = {}
    nodenet_obj = runtime.get_nodenet(nodenet_uid)
    if nodenet_obj is not None:
        if nodenet_uid in runtime.MicropsiRunner.conditions:
            data['simulation_condition'] = runtime.MicropsiRunner.conditions[nodenet_uid]
            if 'monitor' in data['simulation_condition']:
                data['simulation_condition']['monitor']['color'] = nodenet_obj.get_monitor(data['simulation_condition']['monitor']['uid']).color
        data['simulation_running'] = nodenet_obj.is_active
        data['current_nodenet_step'] = nodenet_obj.current_step
        data['current_world_step'] = nodenet_obj.world.current_step if nodenet_obj.world else 0
        if nodenet is not None:
            data['nodenet'] = runtime.get_nodenet_data(nodenet_uid=nodenet_uid, **nodenet)
        if world is not None and nodenet_obj.world:
            data['world'] = runtime.get_world_view(world_uid=nodenet_obj.world.uid, **world)
        if monitors is not None:
            data['monitors'] = runtime.get_monitoring_info(nodenet_uid=nodenet_uid, **monitors)
        return True, data
    else:
        return False, "No such nodenet"
Ejemplo n.º 17
0
def test_engine_specific_nodetype_theano(fixed_nodenet, resourcepath, nodetype_def, nodefunc_def):
    with open(nodetype_def, 'w') as fp:
        fp.write('{"Testnode": {\
            "engine": "dict_engine",\
            "name": "Testnode",\
            "slottypes": ["gen", "foo", "bar"],\
            "nodefunction_name": "testnodefunc",\
            "gatetypes": ["gen", "foo", "bar"],\
            "symbol": "t",\
            "gate_defaults":{\
              "foo": {\
                "amplification": 13\
              }\
            }}}')
    with open(nodefunc_def, 'w') as fp:
        fp.write("def testnodefunc(netapi, node=None, **prams):\r\n    return 17")

    micropsi.reload_native_modules()
    data = micropsi.get_nodenet_data(fixed_nodenet, nodespace='Root')
    assert "Testnode" not in data['native_modules']
Ejemplo n.º 18
0
def test_add_link(test_nodenet):
    micropsi.add_link(test_nodenet, "node_a", "por", "node_b", "gen", 0.5, 1)
    micropsi.add_link(test_nodenet, "node_a", "por", "node_b", "gen", 1, 0.1)
    micropsi.add_link(test_nodenet, "node_c", "ret", "node_b", "gen", 1, 1)

    nodespace = micropsi.get_nodenet_data(test_nodenet, "Root")
    assert len(nodespace["nodes"]) == 4
    assert len(nodespace["links"]) == 2
    link1 = nodespace["links"]["node_a:por:gen:node_b"]
    assert link1["weight"] == 1
    assert link1["certainty"] == 0.1
    assert link1["source_node_uid"] == "node_a"
    assert link1["target_node_uid"] == "node_b"
    assert link1["source_gate_name"] == "por"
    assert link1["target_slot_name"] == "gen"

    link2 = nodespace["links"]["node_c:ret:gen:node_b"]
    assert link2["source_node_uid"] == "node_c"
    assert link2["target_node_uid"] == "node_b"
    assert link2["source_gate_name"] == "ret"
    assert link2["target_slot_name"] == "gen"
Ejemplo n.º 19
0
def test_engine_specific_nodetype(fixed_nodenet, resourcepath):
    from os import path, remove
    with open(path.join(resourcepath, 'nodetypes.json'), 'w') as fp:
        fp.write('{"Testnode": {\
            "engine": "theano_engine",\
            "name": "Testnode",\
            "slottypes": ["gen", "foo", "bar"],\
            "nodefunction_name": "testnodefunc",\
            "gatetypes": ["gen", "foo", "bar"],\
            "symbol": "t",\
            "gate_defaults":{\
              "foo": {\
                "amplification": 13\
              }\
            }}}')
    with open(path.join(resourcepath, 'nodefunctions.py'), 'w') as fp:
        fp.write("def testnodefunc(netapi, node=None, **prams):\r\n    return 17")

    micropsi.reload_native_modules(fixed_nodenet)
    data = micropsi.get_nodenet_data(fixed_nodenet, nodespace='Root')
    assert "Testnode" not in data['native_modules']
    remove(path.join(resourcepath, 'nodetypes.json'))
    remove(path.join(resourcepath, 'nodefunctions.py'))
Ejemplo n.º 20
0
def test_get_nodespace(test_nodenet):
    nodespace = micropsi.get_nodenet_data(test_nodenet, "Root")
    assert len(nodespace["nodes"]) == 4
    node1 = nodespace["nodes"]["node_a"]
    assert node1["name"] == "A"
    assert node1["position"] == (200, 250)
Ejemplo n.º 21
0
def get_nodespace(nodenet_uid, nodespace, step, include_links=True):
    return True, runtime.get_nodenet_data(nodenet_uid, nodespace, step, include_links)
Ejemplo n.º 22
0
def test_delete_link(test_nodenet):
    success, link = micropsi.add_link(test_nodenet, "node_a", "por", "node_b", "gen", 0.5, 1)

    micropsi.delete_link(test_nodenet, "node_a", "por", "node_b", "gen")
    nodespace = micropsi.get_nodenet_data(test_nodenet, "Root")
    assert len(nodespace["links"]) == 1