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

Refactor/state and projection/unit tests bug fixes #502

Merged
merged 3 commits into from
Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 18 additions & 125 deletions Scripts/Scratch Pad.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,12 +757,10 @@ def __init__(self, error_value):
from psyneulink.globals.keywords import MECHANISM, OUTPUT_STATES, PROJECTIONS, NAME, INPUT_STATES

# # 10/29/17 NOT YET IMPLEMENTED:
# my_mech_1 = DDM()
my_mech_1 = TransferMechanism(output_states=['FIRST', 'SECOND'])

# InputState specification tests:


# NOT YET IMPLEMENTED [10/29/17]:
# MECHANISM/OUTPUT_STATES specification
# my_mech_2 = TransferMechanism(input_states=[{MECHANISM: my_mech_1,
Expand All @@ -774,32 +772,21 @@ def __init__(self, error_value):
# assert projection.sender.owner is my_mech_1


# MATCH OF default_variable and specification of multiple InputStates by value and string
my_mech_2 = TransferMechanism(default_variable=[[0,0],[0]],
input_states=[[32, 24], 'HELLO'])
assert my_mech_2.input_states[1].name == 'HELLO'
# # PROBLEM WITH input FOR RUN:
# my_mech_2.execute()

# TEST: Implement as error test
# # Mismatch between InputState variable specification and corresponding item of owner Mechanism's variable
# my_mech_2 = TransferMechanism(default_variable=[[0],[0]],
# # MATCH OF default_variable and specification of multiple InputStates by value and string
# my_mech_2 = TransferMechanism(default_variable=[[0,0],[0]],
# input_states=[[32, 24], 'HELLO'])
# assert (
# "Value specified for" in str(error_text.value)
# and "with its expected format" in str(error_text.value)
# )


# OVERRIDE OF input_states (mis)specification by params dict INPUT_STATES entry specification
my_mech_2 = TransferMechanism(default_variable=[[0,0],[0]],
input_states=[[32], 'HELLO'],
params = {INPUT_STATES:[[32, 24], 'HELLO']}
)
assert my_mech_2.input_states[1].name == 'HELLO'
# # PROBLEM WITH input FOR RUN:
# my_mech_2.execute()

# assert my_mech_2.input_states[1].name == 'HELLO'
# # # PROBLEM WITH input FOR RUN:
# # my_mech_2.execute()

# # OVERRIDE OF input_states (mis)specification by params dict INPUT_STATES entry specification
# my_mech_2 = TransferMechanism(default_variable=[[0,0],[0]],
# input_states=[[32], 'HELLO'],
# params = {INPUT_STATES:[[32, 24], 'HELLO']}
# )
# assert my_mech_2.input_states[1].name == 'HELLO'
# # # PROBLEM WITH input FOR RUN:
# # my_mech_2.execute()

# # PROBLEM: SHOULD GENERATE TWO INPUT_STATES (
# # ONE WITH [[32],[24]] AND OTHER WITH [[0]] AS VARIABLE INSTANCE DEFAULT
Expand All @@ -820,98 +807,14 @@ def __init__(self, error_value):
# assert len(my_mech_3.variable[0])==2
# assert len(my_mech_3.variable[1])==1

# # ********** ADDED TO TESTS:*********************************

# Mechanism specification
my_mech_2 = TransferMechanism(default_variable=[[0]],
input_states=[my_mech_1])
assert my_mech_2.input_state.path_afferents[0].sender == my_mech_1.output_state
my_mech_2.execute()


# Mechanism outside of list specification
my_mech_2 = TransferMechanism(default_variable=[[0]],
input_states=my_mech_1)
assert my_mech_2.input_state.path_afferents[0].sender == my_mech_1.output_state
my_mech_2.execute()


# OutputState specification
my_mech_2 = TransferMechanism(default_variable=[[0],[0]],
input_states=[my_mech_1.output_states['FIRST'],
my_mech_1.output_states['SECOND']])
assert my_mech_2.input_states.names[0] == 'InputState'
assert my_mech_2.input_states.names[1] == 'InputState-1'
for input_state in my_mech_2.input_states:
for projection in input_state.path_afferents:
assert projection.sender.owner is my_mech_1
my_mech_2.execute()

# OutputState specification with Names (and single item in PROJECTIONS outside of a list
my_mech_2 = TransferMechanism(default_variable=[[0],[0]],
input_states=[{NAME: 'FROM DECISION',
PROJECTIONS: [my_mech_1.output_states['FIRST']]},
{NAME: 'FROM RESPONSE_TIME',
PROJECTIONS: my_mech_1.output_states['SECOND']}])
assert my_mech_2.input_states.names[0] == 'FROM DECISION'
assert my_mech_2.input_states.names[1] == 'FROM RESPONSE_TIME'
for input_state in my_mech_2.input_states:
for projection in input_state.path_afferents:
assert projection.sender.owner is my_mech_1
my_mech_2.execute()


# OutputState outside of list specification
my_mech_2 = TransferMechanism(default_variable=[0],
input_states=my_mech_1.output_states['FIRST'])
assert my_mech_2.input_states.names[0] == 'InputState'
my_mech_2.input_state.path_afferents[0].sender == my_mech_1.output_state
my_mech_2.execute()


# Projection specification in Tuple
my_mech_3 = TransferMechanism(size=3)
my_proj = MappingProjection(sender=my_mech_3, name='TEST_PROJ')

# default_variable override of OutputState.value
my_mech_2 = TransferMechanism(default_variable=[[0,0]],
input_states=[my_mech_3])
assert len(my_mech_2.input_state.path_afferents[0].sender.variable)==3
assert len(my_mech_2.input_state.variable)==2
assert len(my_mech_2.variable)==1
assert len(my_mech_2.variable[0])==2
my_mech_2.execute()


# 2-item Tuple Specification
# default_variable override of OutputState.value
my_mech_2 = TransferMechanism(size=2, input_states=[(my_mech_3, np.zeros((3,2)))])
assert len(my_mech_2.input_state.path_afferents[0].sender.variable)==3
assert len(my_mech_2.input_state.variable)==2
assert len(my_mech_2.variable)==1
assert len(my_mech_2.variable[0])==2
my_mech_2.execute()


# ConnectionTuple Specification
my_mech_2 = TransferMechanism(size=2, input_states=[(my_mech_3, None, None, np.zeros((3,2)))])
assert len(my_mech_2.input_state.path_afferents[0].sender.variable)==3
assert len(my_mech_2.input_state.variable)==2
assert len(my_mech_2.variable)==1
assert len(my_mech_2.variable[0])==2
my_mech_2.execute()

# my_mech_2 = TransferMechanism(size=2,
# input_states=[my_proj])

my_proj = MappingProjection(sender=my_mech_3)

# Standalone Projection specification
my_mech_2 = TransferMechanism(size=2,
input_states=[my_proj])
assert len(my_mech_2.input_state.path_afferents[0].sender.variable)==3
assert len(my_mech_2.input_state.variable)==2
assert len(my_mech_2.variable)==1
assert len(my_mech_2.variable[0])==2
my_mech_2.execute()

# Projection specification in Tuple
my_mech_2 = TransferMechanism(size=2,
input_states=[(my_mech_3, None, None, my_proj)])
assert len(my_mech_2.input_state.path_afferents[0].sender.variable)==3
Expand All @@ -921,16 +824,6 @@ def __init__(self, error_value):
my_mech_2.execute()


# PROJECTIONS specification
my_mech_2 = TransferMechanism(input_states=[{NAME: 'My InputState with Two Projections',
PROJECTIONS:[my_mech_1.output_states['FIRST'],
my_mech_1.output_states['SECOND']]}])
assert my_mech_2.input_state.name == 'My InputState with Two Projections'
for input_state in my_mech_2.input_states:
for projection in input_state.path_afferents:
assert projection.sender.owner is my_mech_1
my_mech_2.execute()

#endregion

#region TEST INPUT FORMATS
Expand Down
2 changes: 2 additions & 0 deletions psyneulink/components/states/inputstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,8 @@ def _parse_state_specific_params(self, owner, state_dict, state_specific_params)
if isinstance(projection, dict):
matrix = projection[MATRIX]
elif isinstance(projection, Projection):
if projection.init_status is InitStatus.DEFERRED_INITIALIZATION:
continue
matrix = projection.matrix
else:
raise InputStateError("Unrecognized Projection specification for {} of {} ({})".
Expand Down
16 changes: 12 additions & 4 deletions psyneulink/components/states/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,10 +1003,18 @@ def _instantiate_projections_to_state(self, projections, context=None):
if isinstance(proj_sender, State) and proj_sender != state:
raise StateError("Projection assigned to {} of {} from {} already has a sender ({})".
format(self.name, self.owner.name, state.name, sender.name))
# If the Projection has a Mechanism specified as its sender,
# and a State type was specified in the connection spec,
# try to get that State type for the Mechanism
if isinstance(proj_sender, Mechanism) and inspect.isclass(state) and issubclass(state, State):
# If the Projection has a Mechanism specified as its sender:
elif isinstance(state, State):
# Connection spec (state) is specified as a State,
# so validate that State belongs to Mechanism and is of the correct type
sender = _get_state_for_socket(owner=self.owner,
mech=proj_sender,
state_spec=state,
state_types=state.__class__,
projection_socket=SENDER)
elif isinstance(proj_sender, Mechanism) and inspect.isclass(state) and issubclass(state, State):
# Connection spec (state) is specified as State type
# so try to get that State type for the Mechanism
sender = _get_state_for_socket(owner=self.owner,
state_spec=proj_sender,
state_types=state)
Expand Down
52 changes: 26 additions & 26 deletions tests/mechanisms/test_transfer_mechanism.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from psyneulink.components.mechanisms.mechanism import MechanismError
from psyneulink.components.mechanisms.processing.transfermechanism import TransferMechanism
from psyneulink.components.projections.pathway.mappingprojection import MappingProjection
from psyneulink.components.states.inputstate import InputStateError
from psyneulink.globals.utilities import UtilitiesError
from psyneulink.globals.keywords import NAME, MECHANISM, INPUT_STATES, OUTPUT_STATES, PROJECTIONS
from psyneulink.scheduling.timescale import TimeScale
Expand Down Expand Up @@ -736,17 +737,16 @@ def test_transfer_mech_input_states_match_with_default_variable(self):
# # PROBLEM WITH input FOR RUN:
# my_mech_2.execute()

# # STILL DEBUGGING:
# # ------------------------------------------------------------------------------------------------
# # TEST 2
# # Mismatch between InputState variable specification and corresponding item of owner Mechanism's variable
#
# def test_transfer_mech_input_states_mismatch_with_default_variable_error(self):
#
# with pytest.raises(ComponentError) as error_text:
# T = TransferMechanism(default_variable=[[0],[0]],
# input_states=[[32, 24], 'HELLO'])
# assert "Value specified for" in str(error_text.value) and "with its expected format" in str(error_text.value)
# ------------------------------------------------------------------------------------------------
# TEST 2
# Mismatch between InputState variable specification and corresponding item of owner Mechanism's variable

def test_transfer_mech_input_states_mismatch_with_default_variable_error(self):

with pytest.raises(InputStateError) as error_text:
T = TransferMechanism(default_variable=[[0],[0]],
input_states=[[32, 24], 'HELLO'])
assert "Value specified for" in str(error_text.value) and "with its expected format" in str(error_text.value)

# ------------------------------------------------------------------------------------------------
# TEST 3
Expand Down Expand Up @@ -921,21 +921,20 @@ def test_transfer_mech_input_states_projection_spec(self):
assert len(T.variable[0])==2
T.execute()

# STILL DEBUGGING:
# # ------------------------------------------------------------------------------------------------
# # TEST 15
# # Projection specification in Tuple
#
# def test_transfer_mech_input_states_projection_in_tuple_spec(self):
#
# P = MappingProjection(sender=R2)
# T = TransferMechanism(size=2,
# input_states=[(R2, None, None, P)])
# assert len(T.input_state.path_afferents[0].sender.variable)==3
# assert len(T.input_state.variable)==2
# assert len(T.variable)==1
# assert len(T.variable[0])==2
# T.execute()
# ------------------------------------------------------------------------------------------------
# TEST 15
# Projection specification in Tuple

def test_transfer_mech_input_states_projection_in_tuple_spec(self):

P = MappingProjection(sender=R2)
T = TransferMechanism(size=2,
input_states=[(R2, None, None, P)])
assert len(T.input_state.path_afferents[0].sender.variable)==3
assert len(T.input_state.variable)==2
assert len(T.variable)==1
assert len(T.variable[0])==2
T.execute()

# ------------------------------------------------------------------------------------------------
# TEST 16
Expand All @@ -954,6 +953,7 @@ def test_transfer_mech_input_states_projection_in_specification_dict_spec(self):

# # ------------------------------------------------------------------------------------------------
# # TEST 17
# METHOD OF SPECIFICATION NOT YET IMPLEMENTED:
# # MECHANISMS/OUTPUT_STATES entries in params specification dict
#
# def test_transfer_mech_input_states_mech_output_state_in_specification_dict_spec(self):
Expand Down