Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/comp and mech/input variable #2310

Merged
merged 272 commits into from
Feb 7, 2022
Merged

Conversation

jdcpni
Copy link
Collaborator

@jdcpni jdcpni commented Feb 6, 2022

Major changes:

• Mechanism._get_variable_from_input()
- previously, simply assigned input as input_port.value, ignoring variable
- now assigns input as input_port.variable, calls input_port.execute, and assigns result to input_port.value

• Add changes_shape Parameter to Function and *_input_shape attributes to InputPort,
used to determine whether to base shape of inputs to an InputPort based on its variable
(if its input_port.function.changes_shape = True) or is value (as previously, and still useful/convenient in most cases,
such as LinearCombination which can accomodate many inputs but expects them all to be the same)

• Add Composition.get_input_format() that returns formatted inputs dict (with default input values)
appropriate for composition.run()

Specific changes:

• composition.py:

  • add external_input_shape, default_external_input_shape, external_input_variables, default_external_input_variables
  • _create_cim_ports(): use input_port.default_input_shape instead of input_port.defaults.value for shaping
  • _instantiate_input_dict: use node.default_external_input_shape instead of external_input_values
  • _build_predicted_inputs_dict(): use default_input_shape for predicted input rather than input_port.defaults.value
  • _validate_single_input(): use default_external_input_shape instead of defaults.value
  • _validate_input_shapes(): use default_external_input_shape instead of defaults.value
  • _validate_execution_inputs(): use default_external_input_shape instead of default_external_input_values
  • get_input_format(): add template option to return formatted dict for use as input to run()

• mechanism.py:

  • add input_variables, get_input_variables, external_input_variables, default_external_input_variables,
    external_input_shape, default_external_input_shape
    _get_variable_from_input():
  • use default_input_shape to evaluate shape of input, and set input_port.variable
  • call input_port._eecute and set input_port.value to result
  • raise exception for input that fails InputPort._execute

• function.py:

  • add changes_shape Parameter that specifies whether value of function is different shape than variable:
    used to determine whether to base shape of input to an InputPort on its variable or OK to do so based on its value

• inputport.py:

  • add: _input_shape_template, default_input_shape, get_input_shape used to validate shape of inputs to InputPort

• test_composition

  • test_get_input_format()

• test_ddm_mechanism:

  • test_selected_input_array(): modify test to correctly use array as input for pass, and test for error input is incorrect

jdcpni and others added 30 commits November 9, 2021 11:11
  - _add_controller:  modifying to instantiate feature_input_ports if none are specified
  - add_controller:  assign simulation_input_ports
  - feature_input_ports -> state_input_ports
  - _instantiate_input_ports():
      state_features only allowed to specifying state_input_ports if
      agent_rep is a CompositionFunctionApproximator (i.e., model-free optimization)

• composition.py:
  - add_controller: adds state_input_ports to shadow INPUT Nodes of Composition
    if controller.agent_rep is Composition (model-based optimziation)
    or state_features have not been specified (for model-free optimizaton)
  _instantiate_input_ports:
     reinstate allowance of state_features specification if agent_rep is a Composition
     (i.e., model-based optimization) as long as they are all INPUT Nodes of agent_rep
  - _gen_llvm_evaluate_function: num_estimates -> num_estimates_per_trial
  - _gen_llvm_evaluate_function: num_estimates -> num_trial_per_estimate
…rincetonUniversity/PsyNeuLink into refactor/ocm/state_input_ports

� Conflicts:
�	psyneulink/core/components/mechanisms/modulatory/control/optimizationcontrolmechanism.py
  - _gen_llvm_evaluate_function: num_estimates -> num_trials_per_estimate
…ut_ports

# Conflicts:
#	psyneulink/core/components/mechanisms/modulatory/control/optimizationcontrolmechanism.py
  - __init__: moved controller instantiation until after nodes, projections and pathways
  - __init__: restored add_controller position
…teger

Signed-off-by: Jan Vesely <jan.vesely@rutgers.edu>
…ut_ports

# Conflicts:
#	psyneulink/core/llvm/builder_context.py
  - _update_controller: added
  - add_controller and _analyze_graph(): call _update_controller
  _update_controller:  fixed to loop through all input_ports of comp INPUT nodes
jdcpni and others added 26 commits February 3, 2022 22:09
  - add external_input_variables properties
  - add get_input_variables() methods
  replace use of input.value with input.varible for validating/sizing inputs
  Function_Base:
  - add input_shape_template attribute (as experiment for now)
  - add:
    _input_shape_template, default_input_shape, input_shape, get_input_shape,
    default_external_input_shape, external_input_shape
  - _get_variable_from_input(): refactor to use above instead of variable or value
  move from mechanism to inputport: _input_shape_template, default_input_shape, input_shape, get_input_shape
  - get_variable_from_input():
    - refactor to assign each item of input to corresponding input_port.variable
      and each input_port.value to result of executing its function with the variable
• function.py:
  input_shape_template -> changes_shape
  add: test_get_input_format()
@github-actions
Copy link

github-actions bot commented Feb 6, 2022

This PR causes the following changes to the html docs (ubuntu-latest-3.7-x64):

diff -r docs-base/CombinationFunctions.html docs-head/CombinationFunctions.html
1047c1047
< <dd><p>Insure that all items of list or np.ndarray in variable are of the same length</p>
---
> <dd><p>Insure that all items of list or np.array in variable are of the same length</p>
diff -r docs-base/Composition.html docs-head/Composition.html
684c684
< MappingProjection is used as an <a class="reference internal" href="Mechanism.html#id15" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.efferents"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">efferent</span> <span class="pre">projection</span></code></a> of a ModulatoryMechanism.</p>
---
> MappingProjection is used as an <a class="reference internal" href="Mechanism.html#id17" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.efferents"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">efferent</span> <span class="pre">projection</span></code></a> of a ModulatoryMechanism.</p>
728c728
< the Projection will still be associated with the two Mechanisms (i.e., listed in their <a class="reference internal" href="Mechanism.html#id14" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.afferents"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">afferents</span></code></a> and <a class="reference internal" href="Mechanism.html#id15" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.efferents"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">efferents</span></code></a> attributes, respectively), but it is not
---
> the Projection will still be associated with the two Mechanisms (i.e., listed in their <a class="reference internal" href="Mechanism.html#id16" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.afferents"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">afferents</span></code></a> and <a class="reference internal" href="Mechanism.html#id17" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.efferents"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">efferents</span></code></a> attributes, respectively), but it is not
1216c1216
< <a class="reference internal" href="#id16" title="psyneulink.core.compositions.composition.Composition.external_input_values"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">external_input_values</span></code></a> attribute (<a class="reference internal" href="Mechanism.html#psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.external_input_values" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.external_input_values"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">here</span></code></a> if it is Mechanism, or <a class="reference internal" href="#id16" title="psyneulink.core.compositions.composition.Composition.external_input_values"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">here</span></code></a> if it is a Composition). While these are always 2d arrays, the number and size
---
> <a class="reference internal" href="#psyneulink.core.compositions.composition.Composition.external_input_variables" title="psyneulink.core.compositions.composition.Composition.external_input_variables"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">external_input_variables</span></code></a> attribute (<a class="reference internal" href="Mechanism.html#id10" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.external_input_variables"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">here</span></code></a> if it is Mechanism, or <a class="reference internal" href="#psyneulink.core.compositions.composition.Composition.external_input_variables" title="psyneulink.core.compositions.composition.Composition.external_input_variables"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">here</span></code></a> if it is a Composition). While these are always 2d arrays, the number and size
1383c1383
< specifying the runtime parameters for a Mechanism’s <a class="reference internal" href="Mechanism.html#mechanism-ports"><span class="std std-ref">Ports</span></a>, and/or any of their <a class="reference internal" href="Mechanism.html#id14" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.afferents"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">afferent</span> <span class="pre">Projections</span></code></a> (see <a class="reference internal" href="Mechanism.html#mechanism-runtime-port-and-projection-param-specification"><span class="std std-ref">Runtime specification ditionary: parameters of a Mechanism’s Ports and Projections</span></a>).  The subdictionaries
---
> specifying the runtime parameters for a Mechanism’s <a class="reference internal" href="Mechanism.html#mechanism-ports"><span class="std std-ref">Ports</span></a>, and/or any of their <a class="reference internal" href="Mechanism.html#id16" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.afferents"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">afferent</span> <span class="pre">Projections</span></code></a> (see <a class="reference internal" href="Mechanism.html#mechanism-runtime-port-and-projection-param-specification"><span class="std std-ref">Runtime specification ditionary: parameters of a Mechanism’s Ports and Projections</span></a>).  The subdictionaries
1876,1882c1876,1882
< <p>A <a class="reference internal" href="#composition-nodes"><span class="std std-ref">Node's</span></a> <a class="reference internal" href="#id16" title="psyneulink.core.compositions.composition.Composition.external_input_values"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">external_input_values</span></code></a> attribute is always a 2d list in which the index i
< element is the value of the i’th element of the Node’s <a class="reference internal" href="#id15" title="psyneulink.core.compositions.composition.Composition.external_input_ports"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">external_input_ports</span></code></a> attribute.  For Mechanisms,
< the <a class="reference internal" href="Mechanism.html#psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.external_input_values" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.external_input_values"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">external_input_values</span></code></a> is often the same as its <a class="reference internal" href="Mechanism.html#psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.variable" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.variable"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">variable</span></code></a>.  However, some Mechanisms may have InputPorts marked as <a class="reference internal" href="InputPort.html#psyneulink.core.components.ports.inputport.InputPort.internal_only" title="psyneulink.core.components.ports.inputport.InputPort.internal_only"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">internal_only</span></code></a> which are excluded from its <a class="reference internal" href="Mechanism.html#psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.external_input_ports" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.external_input_ports"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">external_input_ports</span></code></a>
< and therefore its <a class="reference internal" href="Mechanism.html#psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.external_input_values" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.external_input_values"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">external_input_values</span></code></a>, and so should not receive an
< input value.  The same considerations extend to the <a class="reference internal" href="#id15" title="psyneulink.core.compositions.composition.Composition.external_input_ports"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">external_input_ports</span></code></a>
< and <a class="reference internal" href="#id16" title="psyneulink.core.compositions.composition.Composition.external_input_values"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">external_input_values</span></code></a> of a Composition, based on the Mechanisms and/or
< <a class="reference internal" href="#composition-nested"><span class="std std-ref">nested Compositions</span></a> that comprise its <a class="reference internal" href="#psyneulink.core.compositions.composition.NodeRole.INPUT" title="psyneulink.core.compositions.composition.NodeRole.INPUT"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">INPUT</span></code></a> Nodes.</p>
---
> <p>A <a class="reference internal" href="#composition-nodes"><span class="std std-ref">Node's</span></a> <a class="reference internal" href="#psyneulink.core.compositions.composition.Composition.external_input_variables" title="psyneulink.core.compositions.composition.Composition.external_input_variables"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">external_input_variables</span></code></a> attribute is always a 2d list in which the index i
> element is the variable of the i’th element of the Node’s <a class="reference internal" href="#id15" title="psyneulink.core.compositions.composition.Composition.external_input_ports"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">external_input_ports</span></code></a> attribute.  For Mechanisms,
> the <a class="reference internal" href="Mechanism.html#id10" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.external_input_variables"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">external_input_variables</span></code></a> is often the same as its <a class="reference internal" href="Mechanism.html#psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.variable" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.variable"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">variable</span></code></a>.  However, some Mechanisms may have InputPorts marked as <a class="reference internal" href="InputPort.html#psyneulink.core.components.ports.inputport.InputPort.internal_only" title="psyneulink.core.components.ports.inputport.InputPort.internal_only"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">internal_only</span></code></a> which are excluded from its <a class="reference internal" href="Mechanism.html#psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.external_input_ports" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.external_input_ports"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">external_input_ports</span></code></a>
> and therefore its <a class="reference internal" href="Mechanism.html#id10" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.external_input_variables"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">external_input_variables</span></code></a>, and so should not receive
> an input value.  The same considerations extend to the <a class="reference internal" href="#id15" title="psyneulink.core.compositions.composition.Composition.external_input_ports"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">external_input_ports</span></code></a>
> and <a class="reference internal" href="#psyneulink.core.compositions.composition.Composition.external_input_variables" title="psyneulink.core.compositions.composition.Composition.external_input_variables"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">external_input_variabels</span></code></a> of a Composition, based on the Mechanisms
> and/or <a class="reference internal" href="#composition-nested"><span class="std std-ref">nested Compositions</span></a> that comprise its <a class="reference internal" href="#psyneulink.core.compositions.composition.NodeRole.INPUT" title="psyneulink.core.compositions.composition.NodeRole.INPUT"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">INPUT</span></code></a> Nodes.</p>
2614c2614
< <dd><p>list of <a class="reference internal" href="#composition-nodes"><span class="std std-ref">Nodes</span></a> that have one or more <a class="reference internal" href="Mechanism.html#id15" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.efferents"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">efferent</span> <span class="pre">Projections</span></code></a>
---
> <dd><p>list of <a class="reference internal" href="#composition-nodes"><span class="std std-ref">Nodes</span></a> that have one or more <a class="reference internal" href="Mechanism.html#id17" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.efferents"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">efferent</span> <span class="pre">Projections</span></code></a>
2626c2626
< <dd><p>list of <a class="reference internal" href="#composition-nodes"><span class="std std-ref">Nodes</span></a> that have one or more <a class="reference internal" href="Mechanism.html#id14" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.afferents"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">afferents</span></code></a> designated as
---
> <dd><p>list of <a class="reference internal" href="#composition-nodes"><span class="std std-ref">Nodes</span></a> that have one or more <a class="reference internal" href="Mechanism.html#id16" title="psyneulink.core.components.mechanisms.mechanism.Mechanism_Base.afferents"><code class="xref any py py-attr docutils literal notranslate"><span class="pre">afferents</span></code></a> designated as
4606c4606
< <span class="sig-name descname"><span class="pre">get_input_format</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">num_trials</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_labels</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">show_nested_input_nodes</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">alias</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#psyneulink.core.compositions.composition.Composition.get_input_format" title="Permalink to this definition">¶</a></dt>
---
> <span class="sig-name descname"><span class="pre">get_input_format</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">num_trials</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_labels</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">show_nested_input_nodes</span></span><span class="o"><span class="pre">=</span></span><span class="de
...

See CI logs for the full diff.

@lgtm-com
Copy link
Contributor

lgtm-com bot commented Feb 6, 2022

This pull request introduces 3 alerts and fixes 1 when merging 5302106 into 8578bd1 - view on LGTM.com

new alerts:

  • 3 for Unused import

fixed alerts:

  • 1 for Unused import

@jdcpni jdcpni merged commit 05ab651 into devel Feb 7, 2022
@jdcpni jdcpni deleted the fix/comp_and_mech/input_variable branch February 7, 2022 00:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants