def test_yaml_dump():
    from pyrates.frontend.template.circuit import CircuitTemplate
    circuit = CircuitTemplate.from_yaml(
        "model_templates.jansen_rit.circuit.JansenRitCircuit").apply()
    from pyrates.frontend.yaml import from_circuit
    from_circuit(circuit, "output/yaml_dump.yaml", "DumpedCircuit")

    # reload saved circuit
    saved_circuit = CircuitTemplate.from_yaml(
        "output/yaml_dump/DumpedCircuit").apply()
    assert saved_circuit
示例#2
0
def test_pickle_ir():
    pass

    path = "model_templates.jansen_rit.circuit.JansenRitCircuit"
    from pyrates.frontend.template.circuit import CircuitTemplate
    from pyrates.ir.circuit import CircuitIR
    from pyrates.frontend.fileio import pickle

    template = CircuitTemplate.from_yaml(path)

    # try to pickle non-vectorized circuit
    circuit = template.apply()  # type: CircuitIR
    filename1 = os.path.join(get_parent_directory(), "output",
                             "jansen_rit_ir.p")
    circuit.to_file(filename=filename1, filetype="pickle")

    # compare to reference pickle
    # compare_files("output/jansen_rit_ir.p", "resources/jansen_rit_ir.p")  # currently does not work
    circuit2 = CircuitIR.from_file(filename1, filetype="pickle")

    # ToDo: compare circuit IR instances at runtime

    # try to pickle vectorized circuit
    circuit.optimize_graph_in_place()
    filename2 = os.path.join(get_parent_directory(), "output",
                             "jansen_rit_ir_vectorized.p")
    circuit.to_file(filename=filename2, filetype="pickle")

    # compare to reference pickle
    # compare_files("output/jansen_rit_ir_vectorized.p", "resources/jansen_rit_ir_vectorized.p")
    # currently does not work

    circuit3 = CircuitIR.from_file(filename2, filetype="pickle")
def test_circuit_instantiation():
    """Test, if apply() functions all work properly"""
    path = "model_templates.jansen_rit.circuit.JansenRitCircuit"
    from pyrates.frontend.template.circuit import CircuitTemplate

    template = CircuitTemplate.from_yaml(path)

    circuit = template.apply()
    # used to be: test if two edges refer to the same coupling operator by comparing ids
    # this is why we referenced by "operator"
    # now: compare operators directly
    edge_to_compare = circuit.edges[('JR_PC', 'JR_EIN', 0)]["edge_ir"]
    for op_key, op in circuit.edges[("JR_PC", "JR_IIN",
                                     0)]["edge_ir"].op_graph.nodes(data=True):
        if op_key in edge_to_compare:
            assert op["operator"].equations == edge_to_compare[
                op_key].equations

    # now test, if JR_EIN and JR_IIN refer to the same operator graph
    assert circuit["JR_EIN"].op_graph is circuit["JR_IIN"].op_graph

    # now test, if the references are collected properly
    for key, data in circuit.nodes(data=True):
        node = data["node"]
        assert node in circuit._reference_map[node.op_graph]

    assert len(circuit._reference_map[circuit["JR_EIN"].op_graph]) == 2
示例#4
0
def test_simple_example():
    """Test of a simple self-connecting one-node backend with a linear operator for the full pipeline from YAML
    to simulation."""

    # Step 1: Load Circuit template
    from pyrates.frontend.template.circuit import CircuitTemplate
    path = "../model_templates/test_resources/linear/ExampleCircuit"
    tmp = CircuitTemplate.from_yaml(path)

    # Step 2: Instantiate template to create frontend IR
    circuit = tmp.apply()

    # Step 3: Reformat frontend IR to backend IR
    # ToDo: adapt this step to new frontend-ir-backend structure
    from pyrates.frontend.nxgraph import from_circuit
    net_def = from_circuit(circuit, revert_node_names=True)

    # Step 4: Create tensorflow graph
    from pyrates.backend import ComputeGraph
    net = ComputeGraph(net_def, dt=5e-4, vectorization='none')

    # Step 5: Run simulation
    results, _ = net.run(simulation_time=1.,
                         outputs={"V": ('Node.0', 'LinearOperator.0', 'y')},
                         sampling_step_size=1e-3)

    results.plot()
def test_multi_circuit_instantiation():
    """Test, if a circuit with subcircuits is also working."""
    path = "model_templates.jansen_rit.circuit.MultiJansenRitCircuit"
    from pyrates.frontend.template.circuit import CircuitTemplate

    template = CircuitTemplate.from_yaml(path)

    circuit = template.apply()
    assert circuit
示例#6
0
def test_yaml_dump():
    """Test the functionality to dump an object to YAML"""
    from pyrates.frontend import fileio

    with pytest.raises(AttributeError):
        fileio.dump("no_to_dict()", "random_art", "yaml")

    from pyrates.frontend.template.circuit import CircuitTemplate
    circuit = CircuitTemplate.from_yaml(
        "model_templates.jansen_rit.circuit.JansenRitCircuit").apply()

    with pytest.raises(ValueError):
        fileio.dump(circuit, "output/yaml_dump.yaml", "yml")

    with pytest.raises(TypeError):
        fileio.dump(circuit, "output/yaml_dump.yaml", "yaml")

    fileio.dump(circuit, "output/yaml_dump.yaml", "yaml", "DumpedCircuit")

    # reload saved circuit
    saved_circuit = CircuitTemplate.from_yaml(
        "output/yaml_dump/DumpedCircuit").apply()
    assert saved_circuit
def test_network_def_workaround():
    path = "model_templates.jansen_rit.circuit.JansenRitCircuit"
    from pyrates.frontend.template.circuit import CircuitTemplate

    template = CircuitTemplate.from_yaml(path)

    circuit = template.apply()

    from pyrates.frontend.nxgraph import from_circuit
    nd = from_circuit(circuit, revert_node_names=True)
    operator_order = [
        'LinearCouplingOperator.3', 'LinearCouplingOperator.1',
        'JansenRitExcitatorySynapseRCO', 'JansenRitInhibitorySynapseRCO',
        'JansenRitCPO', 'JansenRitPRO'
    ]
    inputs = {}

    cpo_i = {
        'dtype': 'float32',
        'shape': (),
        'vtype': 'state_var',
        'value': 0.
    }  # 0. is the new default value

    jr_cpo = {
        'equations': ['V = k * I'],
        'inputs': {
            'I': {
                'reduce_dim':
                True,
                'sources': [
                    'JansenRitExcitatorySynapseRCO',
                    'JansenRitInhibitorySynapseRCO'
                ]
            }
        },
        'output': 'V'
    }
    # assert dict(nd.nodes["JR_PC.0"]) == JR_PC
    node = nd.nodes["JR_PC.0"]
    edge = {
        'delay': 0,
        'source_var': 'JansenRitPRO/m_out',
        'target_var': 'LinearCouplingOperator.3/m_out',
        'weight': 1
    }
    # assert node["operator_order"] == operator_order
    assert node["inputs"] == inputs
    assert node["operator_args"]['JansenRitCPO/I'] == cpo_i
示例#8
0
def test_ir_vectorization():
    """Test, if apply() functions all work properly"""

    path = "model_templates.jansen_rit.circuit.JansenRitCircuit"
    from pyrates.frontend.template.circuit import CircuitTemplate
    from pyrates.ir.circuit import CircuitIR

    template = CircuitTemplate.from_yaml(path)

    circuit = template.apply()  # type: CircuitIR
    circuit2 = circuit.optimize_graph_in_place()

    # these should actually be the same
    assert circuit is circuit2

    # ensure only vector nodes exist
    for node in circuit.nodes:
        assert node.startswith("vector_")
示例#9
0
def test_3_coupled_jansen_rit_circuits(vectorize):
    """Test the simple Jansen-Rit example with three coupled circuits."""

    # Step 1: Load Circuit template
    from pyrates.frontend.template.circuit import CircuitTemplate
    path = "model_templates.jansen_rit.circuit.MultiJansenRitCircuit"
    tmp = CircuitTemplate.from_yaml(path)

    # Step 2: Instantiate template to create frontend IR
    circuit = tmp.apply()

    # Step 3: Reformat frontend IR to backend IR
    # ToDo: adapt this step to new frontend-ir-backend structure
    from pyrates.frontend.nxgraph import from_circuit
    net_def = from_circuit(circuit, revert_node_names=True)

    # Step 4: Create tensorflow graph
    from pyrates.backend import ComputeGraph
    net = ComputeGraph(net_def, dt=5e-4, vectorization=vectorize)
示例#10
0
def test_full_jansen_rit_circuit_template_load():
    """Test a simple circuit template, including all nodes and operators to be loaded."""

    path = "model_templates.jansen_rit.circuit.JansenRitCircuit"
    from pyrates.frontend.template.circuit import CircuitTemplate
    from pyrates.frontend.template.edge import EdgeTemplate
    from pyrates.frontend.template.node import NodeTemplate
    from pyrates.frontend.template.operator import OperatorTemplate
    from pyrates.frontend.template import template_cache, clear_cache
    clear_cache()

    template = CircuitTemplate.from_yaml(path)

    # test, whether circuit is in loader cache
    assert template is template_cache[path]

    # test, whether node templates have been loaded successfully
    nodes = {
        "JR_PC": "model_templates.jansen_rit.population.templates.JansenRitPC",
        "JR_IIN":
        "model_templates.jansen_rit.population.templates.JansenRitIN",
        "JR_EIN": "model_templates.jansen_rit.population.templates.JansenRitIN"
    }

    for key, value in nodes.items():
        assert isinstance(template.nodes[key], NodeTemplate)
        assert template.nodes[key] is template_cache[value]
        # test operators in node templates
        for op in template.nodes[key].operators:
            assert op.path in template_cache
            assert isinstance(op, OperatorTemplate)

    # test, whether coupling operator has been loaded correctly
    coupling_path = "model_templates.jansen_rit.edges.LinearCouplingOperator"
    edge_temp = template.edges[0][2]
    assert isinstance(edge_temp, EdgeTemplate)
    assert list(edge_temp.operators)[0] is template_cache[coupling_path]

    assert repr(template) == f"<CircuitTemplate '{path}'>"