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

Updated Huston Cohen 1994 model #2222

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
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
180 changes: 126 additions & 54 deletions psyneulink/library/models/Cohen_Huston1994.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@

parser = argparse.ArgumentParser()
parser.add_argument('--no-plot', action='store_false', help='Disable plotting', dest='enable_plot')
parser.add_argument('--threshold', type=float, help='Termination threshold for response output (default: %(default)f)', default=0.55)
parser.add_argument('--settle-trials', type=int, help='Number of trials for composition to initialize and settle (default: %(default)d)', default=50)
parser.add_argument('--threshold', type=float, help='Termination threshold for response output (default: %(default)f)',
default=0.60)
parser.add_argument('--settle-trials', type=int,
help='Number of trials for composition to initialize and settle (default: %(default)d)',
default=500)
args = parser.parse_args()

# Define Variables ----------------------------------------------------------------------------------------------------
rate = 0.1 # modified from the original code from 0.01 to 0.1
inhibition = -2.0 # lateral inhibition
bias = 4.0 # bias is positive since Logistic equation has - sing already implemented
threshold = args.threshold # modified from thr original code from 0.6 to 0.55 because incongruent condition won't reach 0.6
# Define Variables -----------------------------------------------------------------------------------------------------
rate = 0.01 # integration rate
inhibition = -2.0 # lateral inhibition
bias = 4.0 # bias is positive since Logistic equation has - sing already implemented
threshold = args.threshold # response threshold
settle_trials = args.settle_trials # cycles until model settles

# Create mechanisms ---------------------------------------------------------------------------------------------------
slope = 0.98 # original slope value
intercept = 298 # original intercept value

# Create mechanisms ----------------------------------------------------------------------------------------------------
# Linear input units, colors: ('red', 'green'), words: ('RED','GREEN')
colors_input_layer = pnl.TransferMechanism(
size=3,
Expand Down Expand Up @@ -81,27 +87,27 @@
name='RESPONSE'
)

# Log mechanisms ------------------------------------------------------------------------------------------------------
# Log mechanisms -------------------------------------------------------------------------------------------------------
task_layer.set_log_conditions('value')
colors_hidden_layer.set_log_conditions('value')
words_hidden_layer.set_log_conditions('value')
response_layer.set_log_conditions('value')
# Connect mechanisms --------------------------------------------------------------------------------------------------
# Connect mechanisms ---------------------------------------------------------------------------------------------------
# (note that response layer projections are set to all zero first for initialization

color_input_weights = pnl.MappingProjection(
matrix=np.array([
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 0.0]
[0.0, 0.0, 1.0]
])
)

word_input_weights = pnl.MappingProjection(
matrix=np.array([
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 0.0]
[0.0, 0.0, 1.0]
])
)

Expand Down Expand Up @@ -171,7 +177,7 @@
])
)
#
# Create pathways -----------------------------------------------------------------------------------------------------
# Create pathways ------------------------------------------------------------------------------------------------------
color_response_process_1 = pnl.Pathway(
pathway=[
colors_input_layer,
Expand Down Expand Up @@ -239,8 +245,7 @@
word_task_weights,
task_layer])


# Create Composition --------------------------------------------------------------------------------------------------
# Create Composition ---------------------------------------------------------------------------------------------------
Bidirectional_Stroop = pnl.Composition(
pathways=[
color_response_process_1,
Expand All @@ -265,12 +270,12 @@
for node in Bidirectional_Stroop.mechanisms:
print(node.name, " Value: ", node.get_output_values(Bidirectional_Stroop))


# # LOGGING:
colors_hidden_layer.set_log_conditions('value')
words_hidden_layer.set_log_conditions('value')

# Create threshold function -------------------------------------------------------------------------------------------

# Create threshold function --------------------------------------------------------------------------------------------


def pass_threshold(response_layer, thresh):
Expand All @@ -285,14 +290,14 @@ def pass_threshold(response_layer, thresh):
pnl.TimeScale.TRIAL: pnl.While(pass_threshold, response_layer, threshold)
}

# Create test trials function -----------------------------------------------------------------------------------------

# Create test trials function ------------------------------------------------------------------------------------------
# a BLUE word input is [1,0] to words_input_layer and GREEN word is [0,1]
# a blue color input is [1,0] to colors_input_layer and green color is [0,1]
# a color-naming trial is [1,0] to task_layer and a word-reading trial is [0,1]


def trial_dict(red_color, green_color, neutral_color, red_word, green_word, neutral_word, CN, WR):

trialdict = {
colors_input_layer: [red_color, green_color, neutral_color],
words_input_layer: [red_word, green_word, neutral_word],
Expand Down Expand Up @@ -327,44 +332,78 @@ def trial_dict(red_color, green_color, neutral_color, red_word, green_word, neut

# Run color naming trials ----------------------------------------------------------------------------------------------
for cond in range(conditions):
response_color_weights = pnl.MappingProjection(
matrix=np.array([
color_response_weights.parameters.matrix.set(
np.array([
[0.0, 0.0],
[0.0, 0.0],
[0.0, 0.0]
]), Bidirectional_Stroop
)

word_response_weights.parameters.matrix.set(
np.array([
[0.0, 0.0],
[0.0, 0.0],
[0.0, 0.0]
]), Bidirectional_Stroop
)

response_word_weights.parameters.matrix.set(
np.array([
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0]
])
]), Bidirectional_Stroop
)
response_word_weights = pnl.MappingProjection(
matrix=np.array([

response_color_weights.parameters.matrix.set(
np.array([
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0]
])
]), Bidirectional_Stroop
)

Bidirectional_Stroop.run(inputs=Stimulus[cond][0], num_trials=settle_trials)
response_color_weights = pnl.MappingProjection(
matrix=np.array([

color_response_weights.parameters.matrix.set(
np.array([
[1.5, 0.0],
[0.0, 1.5],
[0.0, 0.0]
]), Bidirectional_Stroop
)
word_response_weights.parameters.matrix.set(
np.array([
[2.5, 0.0],
[0.0, 2.5],
[0.0, 0.0]
]), Bidirectional_Stroop)

response_word_weights.parameters.matrix.set(
np.array([
[1.5, 0.0, 0.0],
[0.0, 1.5, 0.0]
])
]), Bidirectional_Stroop
)
response_word_weights = pnl.MappingProjection(
matrix=np.array([

response_color_weights.parameters.matrix.set(
np.array([
[2.5, 0.0, 0.0],
[0.0, 2.5, 0.0]
])
]), Bidirectional_Stroop
)

Bidirectional_Stroop.run(inputs=Stimulus[cond][1], termination_processing=terminate_trial)

# Store values from run -----------------------------------------------------------------------------------------------
# Store values from run --------------------------------------------------------------------------------------------
B_S = Bidirectional_Stroop.name
r = response_layer.log.nparray_dictionary('value') # Log response output from special logistic function
r = response_layer.log.nparray_dictionary('value') # Log response output from special logistic function
rr = r[B_S]['value']
n_r = rr.shape[0]
rrr = rr.reshape(n_r, 2)
response_all.append(rrr) # .shape[0])
response_all2.append(rrr.shape[0])

# Clear log & reset ----------------------------------------------------------------------------------------
# Clear log & reset ------------------------------------------------------------------------------------------------
response_layer.log.clear_entries()
colors_hidden_layer.log.clear_entries()
words_hidden_layer.log.clear_entries()
Expand All @@ -375,40 +414,75 @@ def trial_dict(red_color, green_color, neutral_color, red_word, green_word, neut
task_layer.reset([[0, 0]])
print('response_all: ', response_all)

# Run color naming trials ----------------------------------------------------------------------------------------------
# Run word reading trials ----------------------------------------------------------------------------------------------
response_all3 = []
response_all4 = []
for cond in range(conditions):
response_color_weights = pnl.MappingProjection(
matrix=np.array([
color_response_weights.parameters.matrix.set(
np.array([
[0.0, 0.0],
[0.0, 0.0],
[0.0, 0.0]
]), Bidirectional_Stroop
)

word_response_weights.parameters.matrix.set(
np.array([
[0.0, 0.0],
[0.0, 0.0],
[0.0, 0.0]
]), Bidirectional_Stroop
)

response_word_weights.parameters.matrix.set(
np.array([
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0]
])
]), Bidirectional_Stroop
)
response_word_weights = pnl.MappingProjection(
matrix=np.array([

response_color_weights.parameters.matrix.set(
np.array([
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0]
])
]), Bidirectional_Stroop
)

Bidirectional_Stroop.run(inputs=Stimulus2[cond][0], num_trials=settle_trials)
response_color_weights = pnl.MappingProjection(
matrix=np.array([

color_response_weights.parameters.matrix.set(
np.array([
[1.5, 0.0],
[0.0, 1.5],
[0.0, 0.0]
]), Bidirectional_Stroop
)
word_response_weights.parameters.matrix.set(
np.array([
[2.5, 0.0],
[0.0, 2.5],
[0.0, 0.0]
]), Bidirectional_Stroop
)

response_word_weights.parameters.matrix.set(
np.array([
[1.5, 0.0, 0.0],
[0.0, 1.5, 0.0]
])
]), Bidirectional_Stroop
)
response_word_weights = pnl.MappingProjection(
matrix=np.array([

response_color_weights.parameters.matrix.set(
np.array([
[2.5, 0.0, 0.0],
[0.0, 2.5, 0.0]
])
]), Bidirectional_Stroop
)

Bidirectional_Stroop.run(inputs=Stimulus2[cond][1], termination_processing=terminate_trial)

# Store values from run -----------------------------------------------------------------------------------------------
r2 = response_layer.log.nparray_dictionary('value') # Log response output from special logistic function
# Store values from run --------------------------------------------------------------------------------------------
r2 = response_layer.log.nparray_dictionary('value') # Log response output from special logistic function
rr2 = r2[Bidirectional_Stroop.name]['value']
n_r2 = rr2.shape[0]
rrr2 = rr2.reshape(n_r2, 2)
Expand All @@ -426,11 +500,9 @@ def trial_dict(red_color, green_color, neutral_color, red_word, green_word, neut
task_layer.reset([[0, 0]])
print('response_all: ', response_all)


if args.enable_plot:
import matplotlib.pyplot as plt

# Plot results --------------------------------------------------------------------------------------------------------
# Plot results ----------------------------------------------------------------------------------------------------
# First, plot response layer activity for whole run
plt.figure()
# color naming plot
Expand All @@ -444,8 +516,8 @@ def trial_dict(red_color, green_color, neutral_color, red_word, green_word, neut
plt.show(block=not pnl._called_from_pytest)
# Second, plot regression plot
# regression
reg = np.dot(response_all2, 5) + 115
reg2 = np.dot(response_all4, 5) + 115
reg = np.dot(response_all2, slope) + intercept - settle_trials
reg2 = np.dot(response_all4, slope) + intercept - settle_trials
plt.figure()

plt.plot(reg, '-s') # plot color naming
Expand Down