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 4:46AM

    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

  • edited 4:46AM

    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.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games