Unknown error assigning new value to variables

Psychopy V. 2024.1.4

I’ve been running into this constant error that I can’t manage to figure out. I’ve used this exact same logic in a different part of my study that worked fine.

I have four regular stimuli and one catch stimuli. In the beginning of my study, I take all of my regular stimuli and pair them together. Here is how I define these stimuli:

fairy = 'stimuli/fairy.png'
mermaid = 'stimuli/mermaid.png'
witch = 'stimuli/witch.png'
elf = 'stimuli/elf.png'
catchStim = 'catch/grem-catch.png'

Now, I introduce a new variable that determines if the trial will be a catch trial dependent on the block we are in. Here is how I redefine those variables indexing through the trials and blocks:

conditions_list = {
   "scenes": conditions["all_scenes"][scene_order[cur_trial]],
   "leftStim": conditions["leftStim"][scene_order[cur_trial]],
    "rightStim": conditions["rightStim"][scene_order[cur_trial]],
    "targetPos": conditions["targetPos"][scene_order[cur_trial]][block_counter],
    "targetStim": conditions["targetStim"][scene_order[cur_trial]][block_counter],
    "catch": conditions["catchTrials"][scene_order[cur_trial]][block_counter],
    "type": conditions["trialType"][scene_order[cur_trial]]
    }

After, I introduce a piece of code that defines two new variables leftStim_new and rightStim_new (which I use in my image components within the actual task). Depending on whether the trial is a catch trial or not, the target stimuli may be replaced by the catch stimuli (0 = catch, 1 = no catch):

leftStim_new = ''
rightStim_new = ''

if conditions_list["catch"] == 0:  # if it's a catch trial
    print("Entered catch trial block")
    if conditions_list["targetStim"].strip() == conditions_list["leftStim"].strip():  # and the target is on the left
        print("Target is on the left")
        leftStim_new = catchStim
        rightStim_new = conditions_list["rightStim"]
    elif conditions_list["targetStim"].strip() == conditions_list["rightStim"].strip():  # and the target is on the right
        print("Target is on the right")
        leftStim_new = conditions_list["leftStim"]
        rightStim_new = catchStim
elif conditions_list["catch"] == 1:  # if it's a no catch trial
    print("Entered no catch trial block")
    leftStim_new = conditions_list["leftStim"]
    rightStim_new = conditions_list["rightStim"]

The problem is, no matter what I do, leftStim_new and rightStim_new end up completely BLANK. Because of this, Psychopy cannot make sense of the requested image simply because there is no requested image!

When I print all of the values within the conditions_list list, everything prints totally normally (including the variables defined as fairy, elf, witch, etc.) EXCEPT those two variables:

print('catch', conditions_list["catch"])
print('targetStim', conditions_list["targetStim"])
print('leftStim', conditions_list["leftStim"])
print('rightStim', conditions_list["rightStim"])
print("leftStim_new:", repr(leftStim_new))
print("rightStim_new:", repr(rightStim_new))
print(conditions_list["scenes"])

image

Also as seen in the code above, I try printing basically every “if” statement and nothing actually prints meaning it never meets those conditions.

Please help, I don’t know what else to try!!

Is it possible, that the value stored in conditions_list["catch"] is of type string?
In this case your if/else statement will not recognize it as 0 or 1. That is, because 1 is not equal to '1'.
You could try converting it to int by using the int() method. Or you may just check if it is equal to the correct character:

if conditions_list["catch"] == '0':
    ...
elif conditions_list["catch"] == '1':
    ...

(Note the single quotes around the numbers)

2 Likes

I’d be quite surprised by that because the list is populated with a series of 0 and 1 integers. I’ll give it a shot and let you know!

Well… that solved it. Not sure how, but I appreciate the suggestion! The variable conditions_list[“catch”] is populated by Ustim[2] and Pstim[3] which you can see are integers. I’ve used this piece of code in a different part of the study and it worked fine so I’m wondering how this happened:

Ustim = np.array([[stim_U1, stim_U1, stim_U1, stim_U1, stim_U2, stim_U2, stim_U2,  stim_U2],
         ['E', 'E', 'L', 'L', 'E', 'E', 'L', 'L'],
         [1, 1, 1, 0, 1, 1, 1, 0]])
Pstim = np.array([[stim_P1, stim_P1, stim_P1, stim_P1, stim_P2, stim_P2, stim_P2,  stim_P2],
         ['E', 'E', 'E', 'E', 'L', 'L', 'L', 'L'],
         ['L', 'L', 'L', 'L', 'E', 'E', 'E', 'E'],
         [1, 1, 1, 0, 1, 1, 1, 0]])

Thanks anyway!!!

Just to explain it:
Numpy arrays require their elements to be of the same data type. If they do not match, numpy will try to convert them to a data type that can hold them all.
If you have strings and ints, it will convert everything to string. If you have floats and ints, it will convert to float. Etc.