Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[solved] logging image path from image_button

edited June 2014 in OpenSesame

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

  • edited 6:10AM

    Hi Yannick,

    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

    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

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • edited 6:10AM

    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

  • S_HS_H
    edited February 2017

    Hi eduard,

    Yes of course, this is what I think relevant from the script:

    Button1 = image_button = widgets.image_button(form, path=pool['1.png'],var='Button1')
    Button2 = image_button = widgets.image_button(form, path=pool['2.png'], var='Button2')
    Button3 = image_button = widgets.image_button(form, path=pool['3.png'], var='Button3')
    Button4 = image_button = widgets.image_button(form, path=pool['4.png'], var='Button4')
    form.set_widget(labelTitle, (0,0), colspan=2)
    form.set_widget(HeadLine, (0,2), colspan=2)
    form.set_widget(BottomLine, (0,1), colspan=2)   
    form.set_widget(Button1, (0,3))
    form.set_widget(Button2, (1,3))
    form.set_widget(Button3, (1,4))
    form.set_widget(Button4, (0,4))
    
    answers=["Button1","Button2","Button3","Button4"]
    
    
    n=0
    for i in answers:
        if self.get(i)=="yes":
                break
        else:
            n=n+1
    
    var.set("ChosenButton",answers[n])
    

    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

  • edited February 2017

    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 your loop will never break but reach the value 4 on its last iteration. When you then call answers[n], it throws this IndexError because the last index of that list is 3.

    Does this make sense? So, to make your code working, you just need to execute the form before the loop. 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).

    from libopensesame import widgets
    # I HAD TO ADD THESE TWO LINES TO MAKE IT WORK
    form = widgets.form(exp, cols=2,rows=5)
    label = widgets.label(form, text='My text')
    
    Button1 = image_button = widgets.image_button(form, path=pool['1.png'],var='Button1')
    Button2 = image_button = widgets.image_button(form, path=pool['2.png'], var='Button2')
    Button3 = image_button = widgets.image_button(form, path=pool['3.png'], var='Button3')
    Button4 = image_button = widgets.image_button(form, path=pool['4.png'], var='Button4')
    form.set_widget(label, (0,0), colspan=2)# note the label change as I have not had the original labels
    form.set_widget(label, (0,2), colspan=2)
    form.set_widget(label, (0,1), colspan=2)   
    form.set_widget(Button1, (0,3))
    form.set_widget(Button2, (1,3))
    form.set_widget(Button3, (1,4))
    form.set_widget(Button4, (0,4))
    
    answers=["Button1","Button2","Button3","Button4"]
    
    # EXECUTE THE LOOP
    form._exec()
    
    n=0
    for i in answers:
        if self.get(i)=="yes":
                break
        else:
            n=n+1
    print n
    var.set("ChosenButton",answers[n])
    

    Eduard

  • Oh my, it's working perfectly. thank you so much!

Sign In or Register to comment.