Example #1
0
def test_gate_defaults_change_with_nodetype(fixed_nodenet, resourcepath, nodetype_def, nodefunc_def):
    # gate_parameters are a property of the nodetype, and should change with
    # the nodetype definition if not explicitly overwritten for a given node
    with open(nodetype_def, 'w') as fp:
        fp.write('{"Testnode": {\
            "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()
    res, uid = micropsi.add_node(fixed_nodenet, "Testnode", [10, 10], name="Testnode")
    with open(nodetype_def, 'w') as fp:
        fp.write('{"Testnode": {\
            "name": "Testnode",\
            "slottypes": ["gen", "foo", "bar"],\
            "nodefunction_name": "testnodefunc",\
            "gatetypes": ["gen", "foo", "bar"],\
            "symbol": "t",\
            "gate_defaults":{\
              "foo": {\
                "amplification": 5\
              }\
            }}}')
    micropsi.reload_native_modules()
    params = micropsi.nodenets[fixed_nodenet].get_node(uid).get_gate_parameters()
    assert params["foo"]["amplification"] == 5
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'] == {}
Example #3
0
def test_remove_and_reload_native_module(fixed_nodenet, 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"],\
            "nodefunction_name": "testnodefunc",\
            "gatetypes": ["gen", "foo", "bar"],\
            "symbol": "t",\
            "gate_defaults":{\
              "foo": {\
                "amplification": 13\
              }\
            }}}')
    with open(nodefunc_file, '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, 10], name="Testnode")
    os.remove(nodetype_file)
    os.remove(nodefunc_file)
    micropsi.reload_native_modules()
    assert 'Testnode' not in micropsi.get_available_native_module_types(fixed_nodenet)
Example #4
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'] == {}
Example #5
0
def test_generate_netapi_fragment(test_nodenet, resourcepath):
    import os
    netapi = micropsi.nodenets[test_nodenet].netapi
    # create a bunch of nodes and link them
    linktypes = ['subsur', 'porret', 'catexp']
    nodes = []
    for t in linktypes:
        p1 = netapi.create_node('Pipe', None, t)
        p2 = netapi.create_node('Pipe', None, t + '2')
        nodes.extend([p1, p2])
        netapi.link_with_reciprocal(p1, p2, t)
    reg = netapi.create_node('Register', None, 'reg')
    netapi.link(reg, 'gen', nodes[0], 'gen')
    ns = netapi.create_nodespace(None, 'ns1')
    nodes.extend([reg, ns])
    # remember their names
    names = [n.name for n in nodes]
    fragment = micropsi.generate_netapi_fragment(test_nodenet, [n.uid for n in nodes])
    micropsi.nodenets[test_nodenet].clear()
    code = "def foo(netapi):\n    " + "\n    ".join(fragment.split('\n'))
    # save the fragment as recipe & run
    with open(os.path.join(resourcepath, 'recipes.py'), 'w+') as fp:
        fp.write(code)
    micropsi.reload_native_modules()
    micropsi.run_recipe(test_nodenet, 'foo', {})
    # assert that all the nodes are there again
    assert set(names) == set([n.name for n in netapi.get_nodes()] + ['ns1'])
Example #6
0
def test_generate_netapi_fragment(test_nodenet, resourcepath):
    import os
    netapi = micropsi.nodenets[test_nodenet].netapi
    # create a bunch of nodes and link them
    linktypes = ['subsur', 'porret', 'catexp']
    nodes = []
    for t in linktypes:
        p1 = netapi.create_node('Pipe', None, t)
        p2 = netapi.create_node('Pipe', None, t + '2')
        nodes.extend([p1, p2])
        netapi.link_with_reciprocal(p1, p2, t)
    reg = netapi.create_node('Register', None, 'reg')
    netapi.link(reg, 'gen', nodes[0], 'gen')
    ns = netapi.create_nodespace(None, 'ns1')
    nodes.extend([reg, ns])
    # remember their names
    names = [n.name for n in nodes]
    fragment = micropsi.generate_netapi_fragment(test_nodenet,
                                                 [n.uid for n in nodes])
    micropsi.nodenets[test_nodenet].clear()
    code = "def foo(netapi):\n    " + "\n    ".join(fragment.split('\n'))
    # save the fragment as recipe & run
    with open(os.path.join(resourcepath, 'recipes.py'), 'w+') as fp:
        fp.write(code)
    micropsi.reload_native_modules()
    micropsi.run_recipe(test_nodenet, 'foo', {})
    # assert that all the nodes are there again
    assert set(names) == set([n.name for n in netapi.get_nodes()] + ['ns1'])
Example #7
0
def test_node_parameters_from_persistence(fixed_nodenet, 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",\
            "parameters": ["testparam"],\
            "parameter_defaults": {\
                "testparam": 13\
              }\
            }}')
    with open(nodefunc_file, '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, 10], name="Test")
    node = micropsi.nodenets[fixed_nodenet].get_node(uid)
    node.set_parameter("testparam", 42)
    micropsi.save_nodenet(fixed_nodenet)
    micropsi.revert_nodenet(fixed_nodenet)
    node = micropsi.nodenets[fixed_nodenet].get_node(uid)
    assert node.get_parameter("testparam") == 42
Example #8
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"
Example #9
0
def test_user_prompt(fixed_nodenet, 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",\
            "parameters": ["testparam"],\
            "parameter_defaults": {\
                "testparam": 13\
              }\
            }}')
    with open(nodefunc_file, '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)
    result, data = micropsi.get_calculation_state(fixed_nodenet, nodenet={})
    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
Example #10
0
def test_reload_native_modules(fixed_nodenet):
    def hashlink(l):
        return "%s:%s:%s:%s" % (l['source_node_uid'], l['source_gate_name'], l['target_node_uid'], l['target_slot_name'])
    data_before = micropsi.nodenets[fixed_nodenet].export_json()
    links_before = set([hashlink(l) for l in data_before.pop('links')])
    micropsi.reload_native_modules()
    data_after = micropsi.nodenets[fixed_nodenet].export_json()
    links_after = set([hashlink(l) for l in data_after.pop('links')])
    assert data_before == data_after
    assert links_before == links_after
Example #11
0
def test_run_recipe(fixed_nodenet, resourcepath, recipes_def):
    with open(recipes_def, 'w') as fp:
        fp.write("""
def testfoo(netapi, count=23):
    return {'count':count}
""")
    micropsi.reload_native_modules()
    state, result = micropsi.run_recipe(fixed_nodenet, 'testfoo', {'count': 42})
    assert state
    assert result['count'] == 42
Example #12
0
def test_run_recipe(fixed_nodenet, resourcepath):
    import os
    recipe_file = os.path.join(resourcepath, 'Test', 'recipes.py')
    with open(recipe_file, 'w') as fp:
        fp.write("""
def testfoo(netapi, count=23):
    return {'count':count}
""")
    micropsi.reload_native_modules()
    state, result = micropsi.run_recipe(fixed_nodenet, 'testfoo', {'count': 42})
    assert state
    assert result['count'] == 42
Example #13
0
def test_get_recipes(fixed_nodenet, resourcepath, recipes_def):
    with open(recipes_def, 'w') as fp:
        fp.write("""
def testfoo(netapi, count=23):
    return {'count':count}
""")
    micropsi.reload_native_modules()
    recipes = micropsi.get_available_recipes()
    assert 'testfoo' in recipes
    assert len(recipes['testfoo']['parameters']) == 1
    assert recipes['testfoo']['parameters'][0]['name'] == 'count'
    assert recipes['testfoo']['parameters'][0]['default'] == 23
Example #14
0
def test_run_recipe(fixed_nodenet, resourcepath):
    from os import path, remove
    with open(path.join(resourcepath, 'recipes.py'), 'w') as fp:
        fp.write("""
def testfoo(netapi, count=23):
    return count
""")
    micropsi.reload_native_modules(fixed_nodenet)
    state, result = micropsi.run_recipe(fixed_nodenet, 'testfoo', {'count': 42})
    assert state
    assert result == 42
    remove(path.join(resourcepath, 'recipes.py'))
Example #15
0
def test_get_recipes(fixed_nodenet, resourcepath):
    from os import path, remove
    with open(path.join(resourcepath, 'recipes.py'), 'w') as fp:
        fp.write("""
def testfoo(netapi, count=23):
    return count
""")
    micropsi.reload_native_modules(fixed_nodenet)
    recipes = micropsi.get_available_recipes()
    assert 'testfoo' in recipes
    assert len(recipes['testfoo']['parameters']) == 1
    assert recipes['testfoo']['parameters'][0]['name'] == 'count'
    assert recipes['testfoo']['parameters'][0]['default'] == 23
    remove(path.join(resourcepath, 'recipes.py'))
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_user_prompt(fixed_nodenet, 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",\
            "parameters": ["testparam"],\
            "parameter_defaults": {\
                "testparam": 13\
              }\
            }}')
    with open(nodefunc_file, '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
    )
    result, data = micropsi.get_calculation_state(fixed_nodenet, nodenet={})
    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
Example #18
0
def pytest_runtest_teardown(item, nextitem):
    if nextitem is None:
        print("DELETING ALL STUFF")
        shutil.rmtree(config['paths']['resource_path'])
    else:
        uids = list(micropsi.nodenets.keys())
        for uid in uids:
            micropsi.delete_nodenet(uid)
        if os.path.isfile(nodetype_file):
            os.remove(nodetype_file)
        if os.path.isfile(nodefunc_file):
            os.remove(nodefunc_file)
        if os.path.isfile(recipes_file):
            os.remove(recipes_file)
        micropsi.reload_native_modules()
        set_logging_levels()
def test_node_parameters(fixed_nodenet, 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",\
            "parameters": ["linktype", "threshold", "protocol_mode"],\
            "parameter_values": {\
                "linktype": ["catexp", "subsur"],\
                "protocol_mode": ["all_active", "most_active_one"]\
            },\
            "parameter_defaults": {\
                "linktype": "catexp",\
                "protocol_mode": "all_active"\
            }}\
        }')
    with open(nodefunc_file, 'w') as fp:
        fp.write("def testnodefunc(netapi, node=None, **prams):\r\n    return 17")

    assert micropsi.reload_native_modules()
    res, uid = micropsi.add_node(fixed_nodenet, "Testnode", [10, 10], name="Test", parameters={"linktype": "catexp", "threshold": "", "protocol_mode": "all_active"})
    # nativemodule = micropsi.nodenets[fixed_nodenet].get_node(uid)
    assert micropsi.save_nodenet(fixed_nodenet)
Example #20
0
def test_node_parameters(fixed_nodenet, 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",\
            "parameters": ["linktype", "threshold", "protocol_mode"],\
            "parameter_values": {\
                "linktype": ["catexp", "subsur"],\
                "protocol_mode": ["all_active", "most_active_one"]\
            },\
            "parameter_defaults": {\
                "linktype": "catexp",\
                "protocol_mode": "all_active"\
            }}\
        }')
    with open(nodefunc_file, 'w') as fp:
        fp.write(
            "def testnodefunc(netapi, node=None, **prams):\r\n    return 17")

    assert micropsi.reload_native_modules()
    res, uid = micropsi.add_node(fixed_nodenet,
                                 "Testnode", [10, 10],
                                 name="Test",
                                 parameters={
                                     "linktype": "catexp",
                                     "threshold": "",
                                     "protocol_mode": "all_active"
                                 })
    # nativemodule = micropsi.nodenets[fixed_nodenet].get_node(uid)
    assert micropsi.save_nodenet(fixed_nodenet)
Example #21
0
def pytest_runtest_teardown(item, nextitem):
    if nextitem is None:
        print("DELETING ALL STUFF")
        shutil.rmtree(config['paths']['resource_path'])
    else:
        uids = list(micropsi.nodenets.keys())
        for uid in uids:
            micropsi.delete_nodenet(uid)
        if os.path.isfile(nodetype_file):
            os.remove(nodetype_file)
        if os.path.isfile(nodefunc_file):
            os.remove(nodefunc_file)
        if os.path.isfile(recipes_file):
            os.remove(recipes_file)
        micropsi.reload_native_modules()
        set_logging_levels()
Example #22
0
def pytest_runtest_setup(item):
    engine_marker = item.get_marker("engine")
    if engine_marker is not None:
        engine_marker = engine_marker.args[0]
        if engine_marker != item.callspec.params['engine']:
            pytest.skip("test requires engine %s" % engine_marker)
    for item in os.listdir(testpath):
        if item != 'worlds' and item != 'nodenets':
            path = os.path.join(testpath, item)
            if os.path.isdir(path):
                shutil.rmtree(path)
            else:
                os.remove(path)
    os.mkdir(os.path.join(testpath, 'Test'))
    open(os.path.join(testpath, 'Test', '__init__.py'), 'w').close()
    micropsi.reload_native_modules()
    micropsi.logger.clear_logs()
    micropsi.set_runner_properties(1, 1)
    set_logging_levels()
Example #23
0
def test_plot_from_nodefunc(test_nodenet, resourcepath):
    import os
    from random import random
    from time import sleep
    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)
    filepath = os.path.join(resourcepath, "plot.png")
    returnpath = plot.save_to_file(filepath)
    assert os.path.abspath(returnpath) == os.path.abspath(filepath)
    assert os.path.isfile(filepath)
    os.remove(filepath)
    os.mkdir(os.path.join(resourcepath, 'plotter'))
    nodetype_file = os.path.join(resourcepath, "plotter", "nodetypes.json")
    nodefunc_file = os.path.join(resourcepath, "plotter", "nodefunctions.py")
    with open(nodetype_file, 'w') as fp:
        fp.write("""{"Plotter": {
            "name": "Plotter",
            "slottypes": [],
            "nodefunction_name": "plotfunc",
            "gatetypes": [],
            "parameters": ["plotpath"]}}""")
    with open(nodefunc_file, 'w') as fp:
        fp.write("""
def plotfunc(netapi, node=None, **params):
    import os
    from random import random
    filepath = os.path.join(params['plotpath'], 'plot.png')
    activations = [random() for i in range(256)]
    plot = netapi.vizapi.NodenetPlot(plotsize=(2, 2))
    plot.add_activation_plot(activations)
    plot.save_to_file(filepath)
""")
    micropsi.reload_native_modules()
    node = nodenet.netapi.create_node("Plotter", None, name="Plotter")
    node.set_parameter("plotpath", resourcepath)
    micropsi.start_nodenetrunner(test_nodenet)
    sleep(2)
    micropsi.stop_nodenetrunner(test_nodenet)
    assert micropsi.MicropsiRunner.last_nodenet_exception == {}
    assert os.path.isfile(os.path.join(resourcepath, "plot.png"))
Example #24
0
def test_plot_from_nodefunc(test_nodenet, resourcepath):
    import os
    from random import random
    from time import sleep
    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)
    filepath = os.path.join(resourcepath, "plot.png")
    returnpath = plot.save_to_file(filepath)
    assert os.path.abspath(returnpath) == os.path.abspath(filepath)
    assert os.path.isfile(filepath)
    os.remove(filepath)
    os.mkdir(os.path.join(resourcepath, 'plotter'))
    nodetype_file = os.path.join(resourcepath, "plotter", "nodetypes.json")
    nodefunc_file = os.path.join(resourcepath, "plotter", "nodefunctions.py")
    with open(nodetype_file, 'w') as fp:
        fp.write("""{"Plotter": {
            "name": "Plotter",
            "slottypes": [],
            "nodefunction_name": "plotfunc",
            "gatetypes": [],
            "parameters": ["plotpath"]}}""")
    with open(nodefunc_file, 'w') as fp:
        fp.write("""
def plotfunc(netapi, node=None, **params):
    import os
    from random import random
    filepath = os.path.join(params['plotpath'], 'plot.png')
    activations = [random() for i in range(256)]
    plot = netapi.vizapi.NodenetPlot(plotsize=(2, 2))
    plot.add_activation_plot(activations)
    plot.save_to_file(filepath)
""")
    micropsi.reload_native_modules()
    node = nodenet.netapi.create_node("Plotter", None, name="Plotter")
    node.set_parameter("plotpath", resourcepath)
    micropsi.start_nodenetrunner(test_nodenet)
    sleep(2)
    micropsi.stop_nodenetrunner(test_nodenet)
    assert micropsi.MicropsiRunner.last_nodenet_exception == {}
    assert os.path.isfile(os.path.join(resourcepath, "plot.png"))
Example #25
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
Example #26
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']
Example #27
0
def test_user_operation(test_nodenet, resourcepath):
    import os
    os.makedirs(os.path.join(resourcepath, 'foobar'))
    with open(os.path.join(resourcepath, 'foobar', 'operations.py'), 'w+') as fp:
        fp.write("""
def delete_nodes(netapi, selection):
    for uid in selection:
        netapi.delete_node(netapi.get_node(uid))

delete_nodes.selectioninfo = {
    'nodetypes': [],
    'mincount': 1,
    'maxcount': -1
}""")
    runtime.reload_native_modules()
    ops = runtime.get_available_operations()
    assert ops['delete_nodes']['category'] == 'foobar'
    res, uid = runtime.add_node(test_nodenet, "Register", [10, 10], None)
    runtime.run_operation(test_nodenet, "delete_nodes", {}, [uid])
    assert uid not in runtime.nodenets[test_nodenet].get_node_uids()
Example #28
0
def test_user_operation(test_nodenet, resourcepath):
    import os
    os.makedirs(os.path.join(resourcepath, 'foobar'))
    with open(os.path.join(resourcepath, 'foobar', 'operations.py'),
              'w+') as fp:
        fp.write("""
def delete_nodes(netapi, selection):
    for uid in selection:
        netapi.delete_node(netapi.get_node(uid))

delete_nodes.selectioninfo = {
    'nodetypes': [],
    'mincount': 1,
    'maxcount': -1
}""")
    runtime.reload_native_modules()
    ops = runtime.get_available_operations()
    assert ops['delete_nodes']['category'] == 'foobar'
    res, uid = runtime.add_node(test_nodenet, "Register", [10, 10], None)
    runtime.run_operation(test_nodenet, "delete_nodes", {}, [uid])
    assert uid not in runtime.nodenets[test_nodenet].get_node_uids()
def test_delete_partition_unlinks_native_module(test_nodenet, resourcepath):
    nodenet = micropsi.get_nodenet(test_nodenet)
    netapi = nodenet.netapi
    nodespace, source, register = prepare(netapi)
    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', register, 'gen')
    netapi.link(register, 'gen', testnode, 'bar')
    netapi.delete_nodespace(nodespace)
    data = testnode.get_data(include_links=True)
    assert data['links'] == {}
Example #30
0
def test_native_module_and_recipe_categories(fixed_nodenet, resourcepath):
    import os
    os.mkdir(os.path.join(resourcepath, 'Test', 'Test2'))
    nodetype_file = os.path.join(resourcepath, 'Test', 'nodetypes.json')
    nodefunc_file = os.path.join(resourcepath, 'Test', 'nodefunctions.py')
    recipe_file = os.path.join(resourcepath, 'Test', 'Test2', 'recipes.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")
    with open(recipe_file, 'w') as fp:
        fp.write("def testrecipe(netapi):\r\n    pass")
    micropsi.reload_native_modules()
    res = micropsi.get_available_native_module_types(fixed_nodenet)
    assert res['Testnode']['category'] == 'Test'
    res = micropsi.get_available_recipes()
    assert res['testrecipe']['category'] == 'Test/Test2'
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'))
Example #32
0
def test_delete_partition_unlinks_native_module(test_nodenet, resourcepath):
    nodenet = micropsi.get_nodenet(test_nodenet)
    netapi = nodenet.netapi
    nodespace, source, register = prepare(netapi)
    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', register, 'gen')
    netapi.link(register, 'gen', testnode, 'bar')
    netapi.delete_nodespace(nodespace)
    data = testnode.get_data(include_links=True)
    assert data['links'] == {}
Example #33
0
def test_remove_and_reload_native_module(fixed_nodenet, resourcepath, nodetype_def, nodefunc_def):
    from os import remove
    with open(nodetype_def, 'w') as fp:
        fp.write('{"Testnode": {\
            "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()
    res, uid = micropsi.add_node(fixed_nodenet, "Testnode", [10, 10], name="Testnode")
    remove(nodetype_def)
    remove(nodefunc_def)
    micropsi.reload_native_modules()
    assert micropsi.get_available_native_module_types(fixed_nodenet) == {}
Example #34
0
def test_engine_specific_nodetype_theano(fixed_nodenet, 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": {\
            "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_file, 'w') as fp:
        fp.write("def testnodefunc(netapi, node=None, **prams):\r\n    return 17")

    micropsi.reload_native_modules()
    data = micropsi.get_nodenet_metadata(fixed_nodenet)
    assert "Testnode" not in data['native_modules']
Example #35
0
def reload_native_modules():
    return runtime.reload_native_modules()
Example #36
0
def reload_native_modules(nodenet_uid=None):
    return runtime.reload_native_modules(nodenet_uid)
Example #37
0
def test_reload_native_modules(fixed_nodenet):
    data_before = micropsi.nodenets[fixed_nodenet].data
    micropsi.reload_native_modules()
    data_after = micropsi.nodenets[fixed_nodenet].data
    assert data_before == data_after