[solved] logging image path from image_button
Hi,
I'm building an experiment where are four pictures randomly presented from the file pool. The participant should click on the picture that fits to an earlier presented function. The experiment should take place on a touch-display, so I used forms with image_buttons. I randomized the images with this inline script:
import random
nr_of_pictures = 4
self.experiment.pictures = []
for i in range(nr_of_pictures):
self.experiment.pictures.append("%d.png" % i)
random.shuffle(self.experiment.pictures)
pic1 = self.experiment.pictures.pop()
pic2 = self.experiment.pictures.pop()
pic3 = self.experiment.pictures.pop()
pic4 = self.experiment.pictures.pop()
self.experiment.set("pic1", pic1)
self.experiment.set("pic2", pic2)
self.experiment.set("pic3", pic3)
self.experiment.set("pic4", pic4)
The form looks like this:
set margins "50;100;50;100"
set rows "1;1;1"
set spacing "25"
set cols "1;1;1;1"
set description "A generic form plug-in"
widget 0 1 1 1 image_button path="[pic1]" var="response1"
widget 1 1 1 1 image_button path="[pic2]" var="response2"
widget 2 1 1 1 image_button path="[pic3]" var="response3"
widget 3 1 1 1 image_button path="[pic4]" var="response4"
Now i want to log the file name of the picture that has been clicked with this inline script into the variable "ausgewaehltes_icon":
antworten=["response1","response2","response3","response4"]
bildpfade=[self.get("pic1"),self.get("pic2"),self.get("pic3"),self.get("pic4")]
n=0
for i in antworten:
if self.get(i)=="yes":
break
else:
n=n+1
exp.set("ausgewaehltes_icon",bildpfade[n])
Though this doesnt really give me the desired result. Sometimes the logged file name under "ausgewaehltes_icon"
is rigth, but sometimes not. I can't really track down, where there is an issue with that. (This is probably not the most clever code for my problem)
Thanks for your help!
Comments
Hi Yannick,
As far as I can tell, based on a quick test experiment, there's nothing wrong with your code. It works fine for me, and I actually think it's quite a good solution. Provided, and this might be it, that the last script (the one that processes the response) is in the run phase of your
inline_script
. This is important, because the prepare phase is executed before the response has been collected. See also:Could that be it?
Cheers,
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi sebastiaan,
thank you a lot!!
You gave me the right hint, though I accidentally copied the code from the first inline_script for randomizing the pics into "prepare" and "run" so it ran the code twice and mixed up the variables.
Thanks again, this is solved!
Hi everyone,
I'm trying to apply this solution on my experiment using the latest version of OpenSesame and it doesn't work for me. As a matter of fact, I'm looking for a simpler solution: I just need a variable saving the button chosen in the trial (that is "antworten' in the current example). Can anyone help with that?
Thanks
Hi,
Would you mind providing a little more detail on what it is exactly that is the problem? Just from the info " it doesn't work", it is quite hard to suggest a solution. So, which version are you using, what is the error message, which code do you use, etc.
Thanks,
Eduard
Hi eduard,
Yes of course, this is what I think relevant from the script:
What I want is to create the variable "ChosenButton" that show the button selected in the trial (and of course updating itself in every trial).
I'm using the latest version on OpenSesame (3.1.6b1) and The error i'm getting is:
IndexError: list index out of range
Hi,
Well, the code you provide was not really enough to reproduce the error. I had to add some bits. Anyway, the problem is that you do not execute the
form
before you check the response. Therefore, there is not response yet and yourloop
will never break but reach the value4
on its last iteration. When you then callanswers[n]
, it throws thisIndexError
because the last index of thatlist
is3
.Does this make sense? So, to make your code working, you just need to execute the
form
before theloop
. However, to make it stable, you should also take actions, in case no response was given (not sure how this would be possible, but better save than sorry).Eduard
Oh my, it's working perfectly. thank you so much!