How to use psyneulink - 10 common examples

To help you get started, we’ve selected a few psyneulink examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github PrincetonUniversity / PsyNeuLink / tests / mechanisms / test_contrastive_hebbian_mechanism.py View on Github external
def test_configure_learning(self):

        o = pnl.TransferMechanism()
        m = pnl.ContrastiveHebbianMechanism(
                input_size=2, hidden_size=0, target_size=2,
                mode=pnl.SIMPLE_HEBBIAN,
                separated=False,
                matrix=[[0,-.5],[-.5,0]]
        )

        with pytest.warns(UserWarning) as record:
            m.learning_enabled = True

        correct_message_found = False
        for warning in record:
            if ("Learning cannot be enabled" in str(warning.message) and
                    "because it has no LearningMechanism" in str(warning.message)):
                correct_message_found = True
                break
github PrincetonUniversity / PsyNeuLink / tests / composition / test_control.py View on Github external
def test_multilevel_control(self, mode, benchmark):
        oA = pnl.TransferMechanism(
            name='OuterA',
        )
        oB = pnl.TransferMechanism(
            name='OuterB',
        )
        iA = pnl.TransferMechanism(
            name='InnerA',
        )
        iB = pnl.TransferMechanism(
            name='InnerB',
        )
        iComp = pnl.Composition(name='InnerComp')
        iComp.add_node(iA)
        iComp.add_node(iB)
        iComp.add_projection(pnl.MappingProjection(), iA, iB)
        oComp = pnl.Composition(name='OuterComp')
        oComp.add_node(oA)
        oComp.add_node(oB)
        oComp.add_node(iComp)
        oComp.add_projection(pnl.MappingProjection(), oA, iComp)
        oComp.add_projection(pnl.MappingProjection(), iB, oB)
        oController = pnl.ControlMechanism(
github PrincetonUniversity / PsyNeuLink / tests / composition / test_control.py View on Github external
def test_multilevel_ocm_gridsearch_conflicting_directions(self, mode, benchmark):
        oa = pnl.TransferMechanism(name='oa')
        ob = pnl.TransferMechanism(name='ob')
        ocomp = pnl.Composition(name='ocomp', controller_mode=pnl.BEFORE)
        ia = pnl.TransferMechanism(name='ia')
        ib = pnl.ProcessingMechanism(name='ib',
                                     function=lambda x: abs(x - 75))
        icomp = pnl.Composition(name='icomp', controller_mode=pnl.BEFORE)
        ocomp.add_node(oa, required_roles=pnl.NodeRole.INPUT)
        ocomp.add_node(ob)
        ocomp.add_node(icomp)
        icomp.add_node(ia, required_roles=pnl.NodeRole.INPUT)
        icomp.add_node(ib)
        ocomp._analyze_graph()
        icomp._analyze_graph()
        ocomp.add_projection(pnl.MappingProjection(), sender=oa, receiver=ia)
        icomp.add_projection(pnl.MappingProjection(), sender=ia, receiver=ib)
        ocomp.add_projection(pnl.MappingProjection(), sender=ib, receiver=ob)
github PrincetonUniversity / PsyNeuLink / tests / system / test_system.py View on Github external
def test_system_run_with_sticky_condition(self):

        # Construction
        T = TransferMechanism()
        P = Process(pathway=[T])
        S = System(processes=[P])
        assert T.noise == 0.0
        assert T.parameter_ports['noise'].value == 0.0

        # Runtime param used for noise
        # ONLY mechanism value should reflect runtime param -- attr should be changed back by the time we inspect it
        S.run(inputs={T: 2.0},
              runtime_params={T: {"noise": (10.0, AfterTrial(1))}},
              num_trials=4)

        # Runtime param NOT used for noise
        S.run(inputs={T: 2.0})

        assert np.allclose(S.results, [[np.array([2.])],      # Trial 0 - condition not satisfied yet
                                       [np.array([2.])],      # Trial 1 - condition not satisfied yet
github PrincetonUniversity / PsyNeuLink / tests / composition / test_interfaces.py View on Github external
def test_parameter_CIM_routing_from_ControlMechanism(self):
        # Inner Composition
        ia = TransferMechanism(name='ia')
        ib = TransferMechanism(name='ib')
        icomp = Composition(name='icomp', pathways=[ia])
        # Outer Composition
        ocomp = Composition(name='ocomp', pathways=[icomp])
        cm = ControlMechanism(
            name='control_mechanism',
            control_signals=
            ControlSignal(projections=[(SLOPE, ib)])
        )
        icomp.add_linear_processing_pathway([ia, ib])
        ocomp.add_linear_processing_pathway([cm, icomp])
        res = ocomp.run([[2], [2], [2]])
        assert np.allclose(res, [[4], [4], [4]])
        assert len(ib.mod_afferents) == 1
        assert ib.mod_afferents[0].sender == icomp.parameter_CIM.output_port
        assert icomp.parameter_CIM_ports[ib.parameter_ports['slope']][0].path_afferents[0].sender == cm.output_port
github PrincetonUniversity / PsyNeuLink / tests / system / test_system.py View on Github external
def test_system_run_with_combined_condition(self):

        # Construction
        T = TransferMechanism()
        P = Process(pathway=[T])
        S = System(processes=[P])

        # Runtime param used for noise
        # ONLY mechanism value should reflect runtime param -- attr should be changed back by the time we inspect it
        S.run(inputs={T: 2.0},
              runtime_params={T: {"noise": (10.0, Any(AtTrial(1), AfterTrial(2)))}},
              num_trials=5)

        # Runtime param NOT used for noise
        S.run(inputs={T: 2.0})

        assert np.allclose(S.results, [[np.array([2.])],      # Trial 0 - NOT condition 0, NOT condition 1
                                       [np.array([12.])],     # Trial 1 - condition 0, NOT condition 1
                                       [np.array([2.])],      # Trial 2 - NOT condition 0, NOT condition 1
                                       [np.array([12.])],     # Trial 3 - NOT condition 0, condition 1
github PrincetonUniversity / PsyNeuLink / tests / mechanisms / test_recurrent_transfer_mechanism.py View on Github external
def test_recurrent_mech_process_matrix_change(self):
        R = RecurrentTransferMechanism(
            size=4,
            auto=1,
            hetero=-1)
        T = TransferMechanism(
            size=4,
            function=Linear)
        p = Process(size=4, pathway=[T, R], prefs=TestRecurrentTransferMechanismInSystem.simple_prefs)
        R.matrix = [[2, 0, 1, 3]] * 4

        p.run(inputs={T: [[1, 2, 3, 4]]})
        np.testing.assert_allclose(T.parameters.value.get(p), [[1, 2, 3, 4]])
        np.testing.assert_allclose(R.parameters.value.get(p), [[1, 2, 3, 4]])
        p.run(inputs={T: [[1, 3, 2, 5]]})
        np.testing.assert_allclose(R.recurrent_projection.matrix, [[2, 0, 1, 3]] * 4)
        np.testing.assert_allclose(T.parameters.value.get(p), [[1, 3, 2, 5]])
        np.testing.assert_allclose(R.parameters.value.get(p), [[21, 3, 12, 35]])
github PrincetonUniversity / PsyNeuLink / tests / composition / test_show_graph.py View on Github external
def test_converging_pathways(self):
        a = TransferMechanism(name="a", default_variable=[0, 0, 0])
        b = TransferMechanism(name="b")
        c = TransferMechanism(name="c", default_variable=[0, 0, 0, 0, 0])
        LC = LCControlMechanism(
            modulated_mechanisms=[a, b],
            objective_mechanism=ObjectiveMechanism(
                function=Linear, monitor=[b], name="lc_om"
            ),
            name="lc",
        )
        comp = Composition()
        comp.add_linear_processing_pathway([a, c])
        comp.add_linear_processing_pathway([b, c])

        a_label = comp._show_graph._get_graph_node_label(comp, a, show_dimensions=ALL)
        b_label = comp._show_graph._get_graph_node_label(comp, b, show_dimensions=ALL)
        c_label = comp._show_graph._get_graph_node_label(comp, c, show_dimensions=ALL)
github PrincetonUniversity / PsyNeuLink / tests / learning / test_multilayer.py View on Github external
def test_multilayer_log():
    Input_Layer = TransferMechanism(
        name='Input Layer',
        function=Logistic,
        default_variable=np.zeros((2,)),
    )

    Hidden_Layer_1 = TransferMechanism(
        name='Hidden Layer_1',
        function=Logistic(),
        # default_variable=np.zeros((5,)),
        size=5
    )

    Hidden_Layer_2 = TransferMechanism(
        name='Hidden Layer_2',
        function=Logistic(),
        default_variable=[0, 0, 0, 0],
github PrincetonUniversity / PsyNeuLink / tests / mechanisms / test_recurrent_transfer_mechanism.py View on Github external
def test_transfer_mech_process_matrix_change(self):
        from psyneulink.core.components.projections.pathway.mappingprojection import MappingProjection
        T1 = TransferMechanism(
            size=4,
            function=Linear)
        proj = MappingProjection(matrix=[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]])
        T2 = TransferMechanism(
            size=4,
            function=Linear)

        p = Process(size=4, pathway=[T1, proj, T2])

        p.run(inputs={T1: [[1, 2, 3, 4]]})
        proj.matrix = [[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]]
        assert np.allclose(proj.matrix, [[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]])
        # p.run(inputs={T1: [[1, 2, 3, 4]]})
        T1.execute([[1, 2, 3, 4]])
        proj.execute()
        # removed this assert, because before the changes of most_recent_execution_id -> most_recent_context