Skip to content

Commit

Permalink
Fix/mechanism/ parse arg input states (#532)
Browse files Browse the repository at this point in the history
* -

* Merge branch 'devel' of https://github.com/PrincetonUniversity/PsyNeuLink into CURRENT

# Conflicts:
#	tests/mechanisms/test_transfer_mechanism.py

* Merge branch 'devel' of https://github.com/PrincetonUniversity/PsyNeuLink into CURRENT

# Conflicts:
#	tests/mechanisms/test_transfer_mechanism.py

* -

* -

* • State
  - docstring: STATE NAME ENTRY, MECHANISM/STATE and State constructor examples

* -

* • Mechanism
  - bug fix in _parse_arg_input_states
  • Loading branch information
jdcpni authored Nov 14, 2017
1 parent 7da6769 commit 50e094f
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 83 deletions.
54 changes: 51 additions & 3 deletions Scripts/Scratch Pad.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def __init__(self, error_value):
p = pnl.MappingProjection()
T = pnl.TransferMechanism(default_variable=[0, 0], input_states=[p])


# FIX: ADD AS TEST:
m = pnl.TransferMechanism()
i = pnl.InputState(variable=[0,0])
m.add_states([i])
Expand Down Expand Up @@ -484,8 +484,6 @@ def __init__(self, error_value):
pnl.PROJECTIONS:[my_gating_mech]}])




my_mech = pnl.DDM(name='MY DDM')
my_ctl_mech = pnl.ControlMechanism(control_signals=[{pnl.NAME: 'MY DDM DRIFT RATE AND THREHOLD CONTROL SIGNAL',
pnl.PROJECTIONS: [my_mech.parameter_states[pnl.DRIFT_RATE],
Expand Down Expand Up @@ -529,6 +527,56 @@ def __init__(self, error_value):
# my_mech = pnl.DDM(name='MY DDM')
# my_control_mech = pnl.ControlMechanism(control_signals=[{pnl.MECHANISM: my_mech,
# pnl.PARAMETER_STATES: [pnl.DRIFT_RATE, pnl.THRESHOLD]}])
# m = pnl.TransferMechanism(default_variable=[0, 0, 0])
# i = pnl.InputState(owner=m, variable=[0, 0, 0])
# T = pnl.TransferMechanism(input_states=[i])

# my_mech_A = pnl.TransferMechanism()
# my_mech_B = pnl.TransferMechanism()
# FIX: THROWING ERRORS - CAN'T HANDLE DICT IN PLACE OF LIST
# my_mech_C = pnl.TransferMechanism(input_states={'MY STATE':[my_mech_A]})
# FIX: PROPERLY THROWING ERRORS (SETS INSTEAD OF LISTS) -- MAKE ERROR MESSAGE CLEARER
# my_mech_C = pnl.TransferMechanism(input_states=[{'MY STATE':{my_mech_A, my_mech_B}}])
# my_mech_C = pnl.TransferMechanism(input_states=[{'MY STATE':[my_mech_A, my_mech_B]}])
# my_mech_C = pnl.TransferMechanism(input_states=[{'MY STATE A':{my_mech_A},
# 'MY STATE B':{my_mech_B}}])


source_mech_1 = pnl.TransferMechanism()
source_mech_2 = pnl.TransferMechanism()
destination_mech = pnl.TransferMechanism()
my_mech_C = pnl.TransferMechanism(input_states=[{'MY INPUT':[source_mech_1, source_mech_2]}],
output_states=[{'RESULT':[destination_mech]}])

# FIX: CORRECTLY GENERATES ERROR - ADD AS TEST
# source_mech_1 = pnl.TransferMechanism()
# source_mech_2 = pnl.TransferMechanism()
# destination_mech = pnl.TransferMechanism()
# my_mech_C = pnl.TransferMechanism(input_states=[{'MY INPUT 1':[source_mech_1],
# 'MY INPUT 2':[source_mech_2]}])
# Error message:
# 'There is more than one entry of the InputState specification dictionary for TransferMechanism-17 (MY INPUT 2, MY INPUT 1) that is not a keyword; there should be only one (used to name the State, with a list of Projection specifications'

# my_mech = pnl.DDM(name='MY DDM')
# my_ctl_mech = pnl.ControlMechanism(control_signals=[{pnl.MECHANISM: my_mech,
# pnl.PARAMETER_STATES: [pnl.DRIFT_RATE, pnl.THRESHOLD]}])

# # FIX: ADD THIS AS TEST
# mech_A = pnl.TransferMechanism()
# my_input_state = pnl.InputState(projections=[mech_A])
# mech_B = pnl.TransferMechanism(input_states=[my_input_state])

# # FIX: ADD THIS AS TEST INCLUDING ASSERTION OF PROJECTION
# mech_A = pnl.TransferMechanism()
# my_input_state = pnl.InputState(projections=[mech_A])
# mech_B = pnl.TransferMechanism()
# mech_B.add_states([my_input_state])

# # FIX: ADD THIS AS TEST INCLUDING ASSERTION OF PROJECTION
mech_A = pnl.TransferMechanism()
mech_B = pnl.TransferMechanism()
my_input_state = pnl.InputState(owner=mech_B,
projections=[mech_A])

m = pnl.TransferMechanism(default_variable=[0, 0, 0])
i = pnl.InputState(owner=m, variable=[0, 0, 0])
Expand Down
23 changes: 13 additions & 10 deletions psyneulink/components/mechanisms/mechanism.py
Original file line number Diff line number Diff line change
Expand Up @@ -1345,10 +1345,13 @@ def _parse_arg_input_states(self, input_states):
else:
variable = parsed_spec.instance_defaults.variable

if variable is None:
variable = InputState.ClassDefaults.variable
else:
variable_was_specified = True
try:
if variable is None:
variable = InputState.ClassDefaults.variable
else:
variable_was_specified = True
except UnboundLocalError:
variable = InputState.ClassDefaults.variable

default_variable_from_input_states.append(variable)

Expand Down Expand Up @@ -2294,12 +2297,12 @@ def add_states(self, states, context=ADD_STATES):
old_variable.extend(added_variable)
self.instance_defaults.variable = np.array(old_variable)
self._update_variable(self.instance_defaults.variable)
instantiated_input_states = _instantiate_input_states(self,
input_states,
added_variable,
context=context)
for state in instantiated_input_states:
if state.name is state.componentName or state.componentName + '-' in state.name:
instantiated_input_states = _instantiate_input_states(self,
input_states,
added_variable,
context=context)
for state in instantiated_input_states:
if state.name is state.componentName or state.componentName + '-' in state.name:
state._assign_default_name(context=context)
if output_states:
instantiated_output_states = _instantiate_output_states(self, output_states, context=context)
Expand Down
5 changes: 3 additions & 2 deletions psyneulink/components/projections/projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1281,8 +1281,9 @@ def _parse_connection_specs(connectee_state_type,
# Check that dict has at least one entry with a Mechanism as the key
if (not any(isinstance(spec, Mechanism) for spec in connection) and
not any(spec == STATES for spec in connection)):
raise ProjectionError("There are no {} or {} entries in the connection specification dictionary for {}".
format(MECHANISM, STATES, owner.name))
raise ProjectionError("There are no {}s or {}s in the list ({}) specifying {}s for an {} of {}".
format(Mechanism.__name__, State.__name__, connection, Projection.__name__,
connectee_state_type.__name__, owner.name))

# Add default WEIGHT, EXPONENT, and/or PROJECTION specification for any that are not aleady in the dict
# (used as the default values for all the States of all Mechanisms specified for this dict;
Expand Down
2 changes: 1 addition & 1 deletion psyneulink/components/states/inputstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ def __init__(self,
params=params)

# If owner or reference_value has not been assigned, defer init to State._instantiate_projection()
if owner is None or (variable is None and reference_value is None):
if owner is None or (variable is None and reference_value is None and projections is None):
# Store args for deferred initialization
self.init_args = locals().copy()
self.init_args['context'] = context
Expand Down
Loading

0 comments on commit 50e094f

Please sign in to comment.