This is the code I have so far to generate the design matrix. This code runs smoothly on my computer for approx. 16 trials, but my computer can't handle the computation of a matrix with 144 trials...
# SET-UP
# Design matrix PRACTICE list with 8 circles/squares, 8 blue/orange items,
# 8 color/shape cues, and 8 switch/non-switch trials
prac_colors = 8 * ["blue"] + 8 * ["orange"] # [0]: 0 = blue, 1 = orange
prac_shapes = 4 * ["circle"] + 4 * ["square"] + 4 * ["circle"] + 4 * ["square"] # [1]: 0 = circle, 1 = square
prac_cue = 8 * ["color","shape"] # [2]: 0 = color, 1 = shape
prac_design_matrix = zip(prac_colors, prac_shapes, prac_cues)
# print prac_design_matrix
def chain_check(prac_switch_list):
''' this counts consecutive indices, from the second element to the final one
the longest chain is then selected and its length is computed'''
indices = []
for check_count,x in enumerate(prac_switch_list):
if check_count == 0:
pass
else:
previous = prac_switch_list[check_count -1]
current = prac_switch_list[check_count]
if previous == current:
indices.append(check_count)
else:
pass
longest_chain = len(max(np.split(indices, np.where(np.diff(indices) != 1)[0]+1), key=len).tolist()) + 1
return longest_chain
def draw_permutation(prac_design_matrix):
prac_number_of_switches = 0
while prac_number_of_switches != 8:
# generate a random permutation of the design matrix
prac_mixed_list = np.random.permutation(prac_design_matrix).tolist()
# generate switch information
prac_switch_list = []
for i,x in enumerate(prac_mixed_list):
if True:
if i == 0:
prac_switch_list.append("non-switch")
if i > 0 and i < (len(prac_mixed_list)):
previous = prac_mixed_list[i - 1]
current = prac_mixed_list[i]
if previous[2] == current[2]:
prac_switch_list.append("non-switch")
else:
prac_switch_list.append("switch")
# print longest_chain
prac_number_of_switches = prac_switch_list.count('switch')
# add switch information to permuted matrix
for trial in range(len(prac_mixed_list)):
prac_mixed_list[trial].append(prac_switch_list[trial])
return prac_mixed_list, chain_check(prac_switch_list)
longest_chain = 5
while longest_chain > 4:
final_prac_mixed_list = draw_permutation(prac_design_matrix)
longest_chain = final_prac_mixed_list[1]
print(longest_chain)
# final design matrix is below
for trial in final_prac_mixed_list[0]:
print(trial)
final_prac_mixed_list = final_prac_mixed_list[0]
Is there a more efficient or computationally less complicated way to generate such a matrix? Hope someone can help out! Let me know if you need any other info.
Comments
This is the code I have so far to generate the design matrix. This code runs smoothly on my computer for approx. 16 trials, but my computer can't handle the computation of a matrix with 144 trials...
# SET-UP # Design matrix PRACTICE list with 8 circles/squares, 8 blue/orange items, # 8 color/shape cues, and 8 switch/non-switch trials prac_colors = 8 * ["blue"] + 8 * ["orange"] # [0]: 0 = blue, 1 = orange prac_shapes = 4 * ["circle"] + 4 * ["square"] + 4 * ["circle"] + 4 * ["square"] # [1]: 0 = circle, 1 = square prac_cue = 8 * ["color","shape"] # [2]: 0 = color, 1 = shape prac_design_matrix = zip(prac_colors, prac_shapes, prac_cues) # print prac_design_matrix def chain_check(prac_switch_list): ''' this counts consecutive indices, from the second element to the final one the longest chain is then selected and its length is computed''' indices = [] for check_count,x in enumerate(prac_switch_list): if check_count == 0: pass else: previous = prac_switch_list[check_count -1] current = prac_switch_list[check_count] if previous == current: indices.append(check_count) else: pass longest_chain = len(max(np.split(indices, np.where(np.diff(indices) != 1)[0]+1), key=len).tolist()) + 1 return longest_chain def draw_permutation(prac_design_matrix): prac_number_of_switches = 0 while prac_number_of_switches != 8: # generate a random permutation of the design matrix prac_mixed_list = np.random.permutation(prac_design_matrix).tolist() # generate switch information prac_switch_list = [] for i,x in enumerate(prac_mixed_list): if True: if i == 0: prac_switch_list.append("non-switch") if i > 0 and i < (len(prac_mixed_list)): previous = prac_mixed_list[i - 1] current = prac_mixed_list[i] if previous[2] == current[2]: prac_switch_list.append("non-switch") else: prac_switch_list.append("switch") # print longest_chain prac_number_of_switches = prac_switch_list.count('switch') # add switch information to permuted matrix for trial in range(len(prac_mixed_list)): prac_mixed_list[trial].append(prac_switch_list[trial]) return prac_mixed_list, chain_check(prac_switch_list) longest_chain = 5 while longest_chain > 4: final_prac_mixed_list = draw_permutation(prac_design_matrix) longest_chain = final_prac_mixed_list[1] print(longest_chain) # final design matrix is below for trial in final_prac_mixed_list[0]: print(trial) final_prac_mixed_list = final_prac_mixed_list[0]Is there a more efficient or computationally less complicated way to generate such a matrix? Hope someone can help out! Let me know if you need any other info.
Floor
Hi Floor,
Is this problem solved or not? I am a bit confused with the order of your postings?
Eduard