"Variable does not exist" / "Name is not defined"
Hi everyone,
I want to create an experiment that goes as such: I have a pool of 6 possible stimuli (pictures of animals). In each trial, I want to pick randomly three of those animals, present each one with the corresponing sound (for e.g. a dog and a bark), and in the end, pick randomly one of the 6 possible stimuli , and ask the participant whether it was presented previously, or not.
I am currently trying to build the presentation phase. However, I keep getting errors like this one
"stimulusAudio1 does not exist"
or
"name stimulusAudio1 is not defined"
I realise that I am doing something wrong in the way i define my variables but I am quite confused as to what that is...I have written all the code in the 'Run Phase'. Could this be my mistake?
[all the files are uploaded in the file pool. The images in png format, and the audios in wav format (48hz, 16bit)].
I attach the code below.
Thank you for your help :)
from random import shuffle from openexp.canvas import canvas from openexp.sampler import sampler #define the pool image that every animal name corresponds to. bird = pool['bird.png'] cat = pool['cat.png'] cow = pool['cow.png'] dog = pool['dog.png'] duck = pool['duck.png'] elephant = pool['elephant.png'] goat = pool['goat.png'] #load list of stimuli and shuffle it my_list_stimuli=[bird, cat, cow, dog, duck, elephant, goat] shuffle(my_list_stimuli) ######################## ###first stimulus###### ######################## #pick the first item to show from the shuffled items var.stimulusImage1 = my_list_stimuli[0] #create rule to match images with audios if var.stimulusImage1 == bird: var.stimulusAudio1=pool['bird.wav'] elif var.stimulusImage1 == cat: var.stimulusAudio1=pool['cat.wav'] elif var.stimulusImage1 == cow: var.stimulusAudio1=pool['cow.wav'] elif var.stimulusImage1 == dog: var.stimulusAudio1=pool['dog.wav'] elif var.stimulusImage1 == goat: var.stimulusAudio1=['goat.wav'] elif var.stimulusImage1 == elephant: var.stimulusAudio1=['elephant.wav'] else: print("Error") #create sampler for the first stimulus #src = pool[var.stimulusAudio1] my_sampler1 = Sampler(StimulusAudio1) #create canvas for the first stimulus my_canvas1=Canvas(exp) my_canvas1.image(stimulusImage1) my_canvas1.prepare() #execute everything my_sampler1.play my_canvas1.show() clock.sleep(1000) ######################## ###second stimulus###### ######################## #pick the second item to show from the shuffled items var.stimulusImage2=my_list_stimuli[1] #create rule to match images with audios if var.stimulusImage2 == bird: var.stimulusAudio2=pool['bird.wav'] elif var.stimulusImage2 == cat: var.stimulusAudio2=pool['bird.cat'] elif var.stimulusImage2 == cow: var.stimulusAudio2=pool['cow.wav'] elif var.stimulusImage2 == dog: var.stimulusAudio2=pool['dog.wav'] elif var.stimulusImage2 == goat: var.stimulusAudio2=pool['goat.wav'] elif var.stimulusImage2 == elephant: var.stimulusAudio2=pool['elephant.wav'] else: print("Error") #create sampler for the second stimulus #src = pool[var.stimulusAudio2] my_sampler2 = Sampler(var.stimulusAudio2) #create canvas for the second stimulus my_canvas2=Canvas(exp) my_canvas2.image(var.stimulusImage2) my_canvas2.prepare() #execute everything my_sampler2.play my_canvas2.show() clock.sleep(1000) ######################## ###third stimulus###### ######################## #pick the third item to show from the shuffled items #var.stimulusImage3=my_list_stimuli[2] #create rule to match images with audios if var.stimulusImage3 == bird: var.stimulusAudio3=pool['bird.wav'] elif var.stimulusImage3 == cat: var.stimulusAudio3=pool['cat.wav'] elif var.stimulusImage3 == cow: var.stimulusAudio3=pool['cow.wav'] elif var.stimulusImage3 == dog: var.stimulusAudio3=pool['dog.wav'] elif var.stimulusImage3 == goat: var.stimulusAudio3=pool['goat.wav'] elif var.stimulusImage3 == elephant: var.stimulusAudio3=pool['elephant.wav'] else: print("Error") #create sampler for third stimulus #src = pool[var.stimulusAudio3] my_sampler3 = Sampler(var.stimulusAudio3) #create canvas for the third stimulus my_canvas3=Canvas(exp) my_canvas3.image(var.stimulusImage3) my_canvas3.prepare() #execute everything my_sampler3.play my_canvas3.show() clock.sleep(1000) #delay my_canvas_delay=Canvas(exp) my_canvas_delay.clear() my_canvas_delay.prepare() my_canvas_delay.show() clock.sleep(1000) #shuffle again the list shuffle(my_list_stimuli) #pick one item randomly for the question part. #var.question=my_list_stimuli[3] #create rule to match images with audios if var.question == bird: var.stimulusAudioQuestion=pool['bird.wav'] elif var.question == cat: var.stimulusAudioQuestion=pool['cat.wav'] elif var.question == cow: var.stimulusAudioQuestion=pool['cow.wav'] elif var.question == dog: var.stimulusAudioQuestion=pool['dog.wav'] elif var.question == goat: var.stimulusAudioQuestion=pool['goat.wav'] elif var.question == elephant: var.stimulusAudioQuestion=pool['elephant.wav'] else: print("Error") #create sampler # src = var.stimulusAudioQ my_sampler5 = Sampler(var.stimulusAudioQuestion) #create canvas my_canvas_question=Canvas(exp) my_canvas_question.text("Το είδατε;", center=True, x=500, y=200) my_canvas_question.image(var.question) my_canvas_question.prepare() #execute my_sampler5.play my_canvas_question.show()
Comments
Hi @amoupsou,
Without seeing the rest of the task, based on your description, I'd say that your code could be simplified to be more efficient, but the key problem seems to be that by the time the object code is being run, some variables are not defined. Have you splitting the code to set the variables in the "Prepare" phase and run the sampler in the "Run·" phase? (you could also use code to set the variables in the "Prepare" phase and then use the actual sampler object instead of code in the trial sequence).
Best,
Fabrice.
Hi , Fab :)
Thank you for taking the time. I tried what you said, i.e., i put all the variable definitions and the "if" rules in the prepare phase, but still nothing.
I have noticed, that the problematic variable is the one which is presented for the first time inside my "if" rules (var.stimulusAudio1). Do you have any idea of how I could introduce it before my "if" rules?
Concerning code simplification, could you point me to a general direction so I can take it from there? Thank you!
Hi @amoupsou,
Regarding the code, though it can be simplified with some higher level programming, if the current code is working for you and you understand how it works, I'd keep it.The alternative is to use a what is called a "dictionnary" in Python, but if you're not experienced with programming, setting it up might take you some time.
Regarding the problematic variable, if the program gets stuck because the variable called does not exist yet, you could try declaring them in some code at the very beginning of the task (e.g., var.stimulusAudio1="") and see if that helps. Just be careful to make sure that if you set the variable in the "run" section, the stimulus you want shown is displayed as it should and not on the next trial. I still think that the setting uop of the variable should take place in the "Prepare" section, but it's a little difficult to be sure without seeing how the taskis actually implemented.
Hope this helps,
Fabrice.
So, I did just that and set it in the "prepare" section but nothing🤦♀️
Would it help to take a look at the whole task?
Thank you!
hi amoupsou,
Like I said earlier, your script ends up in the
else
case. So your logic doesn't work. Looking at the variables that you are comparing it is also clear why. For example:the variable
bird
is"bird.png"
, like you defined between lines 4-10. However, the variablevar.stimlusImage1
is just"bird"
. So naturally they are not the same. I am not quite sure about your intention in this code/experiment, so I can't tell you exactly what you need to do (there are several solutions). You could for example, simply use strings instead of variables:or you could strip the file ending from the variable:
Regardless, I recommend you use the debug window better. If you had looked there, you would have seen the "Error" output that you put into your else case. Furthermore, you can also then print out variables to check whether they have the value that you expect them to have. I also did not see the problem in your code right away, but after I checked what values the var.stimulusImage1, and bird had, it was clear.
Aside of that, based on your experiment that you shared, it looks like your file pool is empty. So provided you fixed the issues that you have at the moment, you're experiment would probably still not run (unless your files are in the same directory as your experiment). It is better to add the missing files to the file pool though.
Eduard
Hi @amoupsou,
In the example you sent me, your variables are empty...
For example:
The variable StimulusAudio1 is empty...
So you need to wirk out why. If you look carefully at console output, you'll see the following:
These come from code like this:
So it all shows that something is not right in your conditions... That's where you should centerv your attention...
One apparent issue is that you did not write the conditions to reflect the nature of the stimulusImage1, stimulusImage2, etc., variables. These contain alphanumerical valkues, so writing "if var.stimulusImage == bird", for example will never be true (that's why you find the "Error" output in the console). You need to use 'bird' etc.:
Note that in order to track what piece of code generates "Error" in the console output, it's best to use "else: print ("Error1")" here, "Error2" elsewhere etc. That way, as you start resolving errors, you can track where the remaining ones come from.
On to the next issue: Your list of animales include "duck"
yet nowhere in your code do you set the program to deal with "duck"... If you look at the picture above, you'll see that plan for 'bird', 'cat', ´cow´, ´dog´, ´goat´, élephant´, but not for the duck. Hence anytime "duck" is selected, your code will lead to none of the conditions being true, except the last, which writes "Error" in the console.
So you need to complete your code.
That's as far a I can go at the moment. I noticed that your pool does not contain any of the pictures or sound files, so I suspect that this will be the next problem to solve.
I attach the few corrections I made so far, but you'll need to take it from there to keep going.
Best,
Fabrice.
Hi @Fab and @eduard ,
Indeed, I focused on the logic and the main mistake was there. An idea I came up with was to introduce the problematic variable (audio1) as "None" in the beggining of the script, before the "if" statements that match var.audio1 with a specific pool file. After the "if" statements, I introduced the functions for the canvas and the sampler but also preceded that with the statement "If var.audio1 = is not None" : etc etc..
It seems to work just fine with those modifications. Thank you both for the additional corrections!
I attach a code snippet in case it helps someone in the future.
Hi @amoupsou,
Glad it helped and that you worked out a way to make it run 👍
Good luck with your study!
Fabrice.