Howdy, Stranger!

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

Supported by

[open] Need help displaying performance after block trial loop

edited February 2015 in OpenSesame

I will paste the experiment section of the code below so that you can get a better idea, but we think that there is an issue with the scope of our variables. At the end of a block trial, we want to show the participant the percentage of correct responses. However, when the percentage function comes up on screen it only shows 1 of 2 different percentages-- it either gives us 100 percent if the trials were all completed correctly, or 0 percent if any referrals (space) or wrong choices were made, even if there were some correct responses. The task is to judge whether the presented field of asterisks is from a high or low distribution. We tried globalizing everything, and it looks a bit messy, but it did not work. Anyway, help is greatly appreciated! Here is the code:

#Initializing Starting Points
import random 

random.shuffle(lo_stim)
random.shuffle(hi_stim)

vv=0
ww=0

global total_correct
total_correct=0
global total
total=0
global total_referrals
total_referrals=0

#Keyboard Import
from openexp.keyboard import keyboard
kb=keyboard(exp, keylist=['z', '/', 'space'], timeout=2000)
kb.get_key()
kb.flush()

#Defining percentage correct and showing participant
def percentage_correct():
    global total_referrals
    global total_correct
    global total
    diagnosis_rate=int((total_correct/total)*100)
    from openexp.canvas import canvas
    my_canvas = canvas(exp)
    my_canvas.text('Correct Diagnosis Rate: '+str(diagnosis_rate)+' %')
    my_canvas.show()
    self.sleep(3000)

#Defining correct/incorrect/referral stimulus function
def correct_kp():
    global total_correct
    total_correct+=1
    global total
    total+=1
    from openexp.canvas import canvas
    my_canvas = canvas(exp)
    my_canvas.text('Patient Diagnosed Correctly!')
    my_canvas.show()
    self.sleep(500)


def incorrect_kp():
    global total
    total+=1
    from openexp.canvas import canvas
    my_canvas = canvas(exp)
    my_canvas.text('Patient Misdiagnosed.')
    my_canvas.show()
    self.sleep(1000)


def referral():
    global total_referrals
    total_referrals+=1
    global total
    total+=1
    from openexp.canvas import canvas
    my_canvas = canvas(exp)
    my_canvas.text('Patient Referred.')
    my_canvas.show()
    self.sleep(500)


#Low vs High Mean Selection
for i in range(num_blocks):
    global total_correct
    global total
    global total_referrals
    for j in range(trials_per_block):
        random.shuffle(general_binary)
        payoff_hi_yn=general_binary[1]
        random.shuffle(general_binary)
        process_hi_yn=general_binary[1]
        kb.flush()
        global total_correct
        global total
        global total_referrals

        #Low Mean Process
        if process_hi_yn==0:
            v=0+vv
            vv=v+1
            stim_num=vv
            global total_correct
            global total
            global total_referrals

            #Path to select .png file 
            from openexp.canvas import canvas
            my_canvas=canvas(exp)
            path=exp.get_file(u'stim_'+str(general_binary[1])+'_'+str(lo_stim[vv])+'.png')
            stim_start=self.time()
            my_canvas.image(path)
            my_canvas.prepare()
            my_canvas.show()

            #key press response time
            key, time = kb.get_key()
            response_time=time-stim_start

            #Correct/Incorrect key press (Low mean)
            if key=='z':
                global total 
                global total_correct
                correct_kp()
            elif key=='/':
                global total 
                incorrect_kp()  
            elif key=='space':
                global total
                global total_referrals 
                referral()  

            #Log info--> trial number, block number, stimulus number, high vs low mean, response, response time
            log_text=str(j+1)+','+str(i+1)+','+str(stim_num)+','+str(process_hi_yn)+','+str(key)+','+str(response_time)
            self.log(log_text)

            #Clears canvas
            my_canvas.clear()
            my_canvas.prepare()
            my_canvas.show()
            self.sleep(2000)

        #High Mean Process          
        elif process_hi_yn==1:
            w=0+ww
            ww=w+1
            stim_num=ww
            global total_correct
            global total
            global total_referrals

            #Path to select .png file
            from openexp.canvas import canvas
            my_canvas=canvas(exp)
            path=exp.get_file(u'stim_'+str(general_binary[1])+'_'+str(hi_stim[ww])+'.png')
            stim_start=self.time()
            my_canvas.image(path)
            my_canvas.prepare()
            my_canvas.show()

            #key press response time
            key, time = kb.get_key()
            response_time=time-stim_start

            #Correct/Incorrect key press (high mean)    
            if key=='z':
                global total
                incorrect_kp()
            elif key=='/':
                global total 
                global total_correct
                correct_kp()
            elif key=='space':
                global total
                global total_referrals
                referral()  

            #Log info--> trial number, block number, stimulus number, high vs low mean, response, response time
            log_text=str(j+1)+','+str(i+1)+','+str(stim_num)+','+str(process_hi_yn)+','+str(key)+','+str(response_time)
            self.log(log_text)

            #Clears canvas
            my_canvas.clear()
            my_canvas.prepare()
            my_canvas.show()
            self.sleep(2000)
#Calling percentage function    
percentage_correct()    

Comments

  • edited 2:11AM

    Hi,

    The problem is that you're using int division to determine the accuracy. int division always rounds down, so if you have some intermediate result (proportion correct in your case) that is less than 1 it will always become 0.

    Let's say that you have 5 out of 10 correct response:

    print(5/10) # Prints 0
    print((5/10)*100) # Prints 0
    

    The trick is to convert at least one number to a float, like so:

    print(float(5)/10) # Prints .5
    print((float(5)/10)*100) # Prints 50.0
    

    Cheers!
    Sebastiaan

Sign In or Register to comment.