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_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_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
def test_autoalign_operation(test_nodenet): ops = runtime.get_available_operations() assert ops['autoalign']['selection']['nodetypes'] == [] assert ops['autoalign']['selection']['mincount'] == 1 assert ops['autoalign']['selection']['maxcount'] == -1 assert ops['autoalign']['category'] == 'layout' assert ops['autoalign']['parameters'] == [] api = runtime.nodenets[test_nodenet].netapi ns1 = api.create_nodespace(None, "foo") p1 = api.create_node("Pipe", None, "p1") p2 = api.create_node("Pipe", None, "p2") p3 = api.create_node("Pipe", None, "p3") api.link_with_reciprocal(p1, p2, 'subsur') api.link_with_reciprocal(p1, p3, 'subsur') api.link_with_reciprocal(p2, p3, 'porret') runtime.save_nodenet(test_nodenet) runtime.run_operation(test_nodenet, "autoalign", {}, [p1.uid, p2.uid, p3.uid, ns1]) assert p1.position[0] == p2.position[0] assert p1.position[1] < p2.position[1] assert p2.position[1] == p3.position[1] runtime.revert_nodenet(test_nodenet) runtime.run_operation(test_nodenet, "autoalign", {}, [api.get_nodespace(None).uid]) assert p1.position[0] == p2.position[0] assert p1.position[1] < p2.position[1] assert p2.position[1] == p3.position[1] result, data = runtime.run_operation(test_nodenet, "autoalign", {}, [p1.uid]) assert 'error' in data
def test_autoalign_operation(test_nodenet): ops = runtime.get_available_operations() for selectioninfo in ops['autoalign']['selection']: if selectioninfo['nodetypes'] == ['Nodespace']: assert selectioninfo['mincount'] == 1 assert selectioninfo['maxcount'] == -1 else: assert selectioninfo['mincount'] == 2 assert selectioninfo['maxcount'] == -1 assert selectioninfo['nodetypes'] == [] assert ops['autoalign']['category'] == 'layout' assert ops['autoalign']['parameters'] == [] api = runtime.nodenets[test_nodenet].netapi ns1 = api.create_nodespace(None, "foo") p1 = api.create_node("Pipe", None, "p1") p2 = api.create_node("Pipe", None, "p2") p3 = api.create_node("Pipe", None, "p3") api.link_with_reciprocal(p1, p2, 'subsur') api.link_with_reciprocal(p1, p3, 'subsur') api.link_with_reciprocal(p2, p3, 'porret') runtime.save_nodenet(test_nodenet) runtime.run_operation(test_nodenet, "autoalign", {}, [p1.uid, p2.uid, p3.uid, ns1]) assert p1.position[0] == p2.position[0] assert p1.position[1] < p2.position[1] assert p2.position[1] == p3.position[1] runtime.revert_nodenet(test_nodenet) runtime.run_operation(test_nodenet, "autoalign", {}, [api.get_nodespace(None).uid]) assert p1.position[0] == p2.position[0] assert p1.position[1] < p2.position[1] assert p2.position[1] == p3.position[1] result, data = runtime.run_operation(test_nodenet, "autoalign", {}, [p1.uid]) assert 'error' in data
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_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_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_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_nodespace_properties(test_nodenet): data = {'testvalue': 'foobar'} rootns = micropsi.get_nodenet(test_nodenet).get_nodespace(None) micropsi.set_nodespace_properties(test_nodenet, rootns.uid, data) assert micropsi.nodenets[test_nodenet].metadata['nodespace_ui_properties'][rootns.uid] == data assert micropsi.get_nodespace_properties(test_nodenet, rootns.uid) == data micropsi.save_nodenet(test_nodenet) micropsi.revert_nodenet(test_nodenet) assert micropsi.get_nodespace_properties(test_nodenet, rootns.uid) == data properties = micropsi.get_nodespace_properties(test_nodenet) assert properties[rootns.uid] == data
def test_nodespace_properties(test_nodenet): data = {'testvalue': 'foobar'} rootns = micropsi.get_nodenet(test_nodenet).get_nodespace(None) micropsi.set_nodespace_properties(test_nodenet, rootns.uid, data) assert micropsi.nodenets[test_nodenet].metadata['nodespace_ui_properties'][ rootns.uid] == data assert micropsi.get_nodespace_properties(test_nodenet, rootns.uid) == data micropsi.save_nodenet(test_nodenet) micropsi.revert_nodenet(test_nodenet) assert micropsi.get_nodespace_properties(test_nodenet, rootns.uid) == data properties = micropsi.get_nodespace_properties(test_nodenet) assert properties[rootns.uid] == data
def test_runner_condition_persists(test_nodenet): micropsi.set_runner_condition(test_nodenet, steps=7) micropsi.save_nodenet(test_nodenet) micropsi.revert_nodenet(test_nodenet) assert micropsi.nodenets[test_nodenet].get_runner_condition()['step'] == 7
def revert_calculation(nodenet_uid): """ Revert the state of the nodenet and its world to the persisted one""" return runtime.revert_nodenet(nodenet_uid, True)
def revert_nodenet(nodenet_uid): return runtime.revert_nodenet(nodenet_uid)
def revert_calculation(nodenet_uid): return runtime.revert_nodenet(nodenet_uid, True)
def revert_nodenet(nodenet_uid): """ Revert the state of the nodenet to the persisted one""" return runtime.revert_nodenet(nodenet_uid)
def pytest_runtest_call(item): if 'fixed_test_nodenet' in micropsi.nodenets: micropsi.revert_nodenet("fixed_test_nodenet") micropsi.logger.clear_logs() set_logging_levels()
def fin(): micropsi.revert_nodenet(nn_uid)