def show_break(hrl,trial, total_trials): # Function from Thorsten hrl.graphics.flip(clr=True) if LANG=='de': lines = [u'Du kannst jetzt eine Pause machen.', u' ', u'Du hast %d von %d Durchgängen geschafft.' % (trial, total_trials), u' ', u'Wenn du bereit bist, drücke die mittlere Taste.' ] elif LANG=='en': lines = [u'You can take a break now.', u' ', u'You have completed %d out of %d trials.' % (trial, total_trials), u' ', u'When you are ready, press the middle button.' ] else: raise('LANG not available') for line_nr, line in enumerate(lines): textline = hrl.graphics.newTexture(draw_text(line, fontsize=36)) textline.draw(((1024 - textline.wdth) / 2, (768 / 2 - (4 - line_nr) * (textline.hght + 10)))) hrl.graphics.flip(clr=True) btn = None while btn != 'Space': (btn,t1) = hrl.inputs.readButton() # clean text graphics.deleteTextureDL(textline._dlid)
def show_continue(hrl, b, nb): # Function from Thorsten hrl.graphics.flip(clr=True) if LANG=='de': lines = [u'Du kannst jetzt eine Pause machen.', u' ', u'Du hast %d von %d blocks geschafft.' % (b, nb), u' ', u'Zum Weitermachen, druecke die rechte Taste,', u'zum Beenden druecke die linke Taste.' ] elif LANG=='en': lines = [u'You can take a break now.', u' ', u'You have completed %d out of %d blocks.' % (b, nb), u' ', u'To continue, press the right button,', u'to finish, press the left button.' ] else: raise('LANG not available') for line_nr, line in enumerate(lines): textline = hrl.graphics.newTexture(draw_text(line, fontsize=36)) textline.draw(((1024 - textline.wdth) / 2, (768 / 2 - (4 - line_nr) * (textline.hght + 10)))) hrl.graphics.flip(clr=True) btn = None while not (btn == 'Left' or btn == 'Right'): (btn,t1) = hrl.inputs.readButton() # clean text graphics.deleteTextureDL(textline._dlid) return btn
def run_trial(hrl, trl, start_trl, end_trl): # function written by Torsten and edited by Christiane # read out variable values for each trial from the designmatrix print "TRIAL =", trl print trl print start_trl print end_trl #show break automatically, define after how many trials if (trl - start_trl) % 50 == 0: show_break(hrl, (trl - start_trl), (end_trl - start_trl)) # get values from design matrix for current trial trl_id = float(design['Trial'][trl]) ref_side = float(design['Pos'][trl]) rep_id = int(float(design['Rep'][trl])) if trl_id == 0: t_ref = 2 a_test = 0.05 rep = 0 if trl_id == 1: t_ref = 4 a_test = 0.05 rep = 0 if trl_id == 2: t_ref = 6 a_test = 0.05 rep = 0 if trl_id == 3: t_ref = 2 a_test = 0.10 rep = 1 if trl_id == 4: t_ref = 4.0 a_test = 0.10 rep = 1 if trl_id == 5: t_ref = 6 a_test = 0.10 rep = 1 if trl_id == 6: t_ref = 2 a_test = 0.20 rep = 2 if trl_id == 7: t_ref = 4 a_test = 0.20 rep = 2 if trl_id == 8: t_ref = 6 a_test = 0.20 rep = 2 if trl_id == 9: t_ref = 2 a_test = 0.40 rep = 3 if trl_id == 10: t_ref = 4 a_test = 0.40 rep = 3 if trl_id == 11: t_ref = 6 a_test = 0.40 rep = 3 print trl_id print rep_id print rep print t_ref print a_test #a_ref = float(design['a_ref'][trl]) #t_ref = float(design['t_ref'][trl]) #t_test = float(design['t_test'][trl]) # use these variable values to define test stimulus (name corresponds to design matrix and name of saved image) #trialname = 'rep_%d_aref_%.2f_tref_%.1f_atest_%.2f' % (rep, a_ref, t_ref, t_test) # reference file stim_name = 'stimuli_matching/stimuli/rep_%d_tref_%d_atest_%.2f.png' % ( rep, t_ref, a_test) #'%s/%s_ref_cropped.png' % (trialname) # texture creation in buffer : referece stimulus #reference = hrl.graphics.newTexture(curr_image) image = Image.open(stim_name).convert("L").resize((576, 576)) im = np.asarray(image) / 255. # texture creation in buffer reference = hrl.graphics.newTexture(im) ### loading adjustment files textures = [] for alpha in alpha_vec: teststimname = 'rep_%d_tref_%d_atest_%.2f' % (rep, t_ref, alpha) fname = 'stimuli_matching/stimuli/%s_cropped.png' % (teststimname) #print "loading... %s " % fname image = Image.open(fname).convert("L").resize((576, 576)) im = np.asarray(image) / 255. # texture creation in buffer tex = hrl.graphics.newTexture(im) textures.append(tex) # random assignment of start match intensity between 0 and 1 start_idx = random.randint(0, len(textures) - 1) no_match = True match_alpha = start_idx while no_match: # as long as no_match TRUE # Show stimuli: reference and test show_stimulus(hrl, reference, textures, match_alpha, ref_side) match_alpha, btn = adjust_loop(hrl, reference, textures, match_alpha, ref_side) print "btn =", btn print "match alpha index", match_alpha print "match alpha", alpha_vec[match_alpha] if btn == 'Space': no_match = False rfl.write('%d\t%d\t%f\t%f\t%d\t%d\t%d\t%d\t%f\n' % (trl, ref_side, a_test, t_ref, rep, rep_id, start_idx, match_alpha, alpha_vec[match_alpha])) rfl.flush() hrl.graphics.flip(clr=True) # clean checkerboard texture graphics.deleteTextureDL(reference._dlid) graphics.deleteTexture(reference._txid) for tex in textures: graphics.deleteTextureDL(tex._dlid) graphics.deleteTexture(tex._txid) return match_alpha
def run_trial(hrl,trl, block,start_trl, end_trl): whlf = WIDTH/2.0 hhlf = HEIGHT/2.0 hrl.graphics.flip() frm1 = hrl.graphics.newTexture(np.ones((2, 2))*0.0) frm1.draw(( whlf, hhlf)) hrl.graphics.flip() time.sleep(0.25) # function written by Torsten and edited by me # read out variable values for each trial from the designmatrix print("TRIAL =", trl) #show break automatically, define after how many trials if (trl-start_trl)%120==0 and (trl-start_trl)!=0: show_break(hrl,trl, (start_trl + (end_trl-start_trl))) thistrial = int(design['Trial'][trl]) tau1 = float(design['tau1'][trl]) tau2 = float(design['tau2'][trl]) tau3 = float(design['tau3'][trl]) alpha = float(design['alpha'][trl]) trialname1 = '%d_a_%.2f_tau_%.2f' % (thistrial, alpha, tau1) stim_name1 = 'stimuli/%s/mlds/block_%d_%s_cropped' % (vp_id, block, trialname1) trialname2 = '%d_a_%.2f_tau_%.2f' % (thistrial, alpha, tau2) stim_name2 = 'stimuli/%s/mlds/block_%d_%s_cropped' % (vp_id, block, trialname2) trialname3 = '%d_a_%.2f_tau_%.2f' % (thistrial, alpha, tau3) stim_name3 = 'stimuli/%s/mlds/block_%d_%s_cropped' % (vp_id, block, trialname3) #print stim_name # load stimlus image and convert from png to numpy array curr_image1 = image_to_array(stim_name1) curr_image2 = image_to_array(stim_name2) curr_image3 = image_to_array(stim_name3) # texture creation in buffer : stimulus checkerboard1 = hrl.graphics.newTexture(curr_image1) checkerboard2 = hrl.graphics.newTexture(curr_image2) checkerboard3 = hrl.graphics.newTexture(curr_image3) # Show stimlus # draw the checkerboard s checkerboard2.draw((whlf - checkerboard1.wdth/2, 0)) checkerboard1.draw((0, hhlf)) checkerboard3.draw((WIDTH - checkerboard3.wdth , hhlf)) # flip everything hrl.graphics.flip(clr=False) # clr= True to clear buffer no_resp = True while no_resp: # as long as no_resp TRUE response, btn, t1 = read_response(hrl) print("btn =", btn) if btn != None: no_resp = False rfl.write('%d\t%d\t%f\t%f\t%f\t%f\t%d\t%f\t%f\n' % (block, trl, alpha, tau1, tau2, tau3, response, t1, time.time())) rfl.flush() # clean checkerboard texture graphics.deleteTextureDL(checkerboard1._dlid) graphics.deleteTextureDL(checkerboard2._dlid) graphics.deleteTextureDL(checkerboard3._dlid) return response
def run_trial(hrl,trl, block,start_trl, end_trl, imgs): whlf = WIDTH/2.0 hhlf = HEIGHT/2.0 # clears screen hrl.graphics.flip() # draws fixation dot in the middle frm1 = hrl.graphics.newTexture(np.ones((2, 2))*0.0) frm1.draw(( whlf, hhlf)) hrl.graphics.flip() # sleeps for 250 ms time.sleep(0.25) # print "TRIAL =", trl #show a break screen automatically after so many trials if (trl-start_trl)%80==0 and (trl-start_trl)!=0: show_break(hrl,trl, (start_trl + (end_trl-start_trl))) # current trial design variables thistrial = int(design['Trial'][trl]) illum = float(design['illum'][trl]) ann = float(design['ann'][trl]) ped = float(design['ped'][trl]) incr = float(design['incr'][trl]) transp = design['transp'][trl] pos = design['pos'][trl] ann_r = ann/illum ped_r = ped/illum print illum print ann print ped print incr print transp print pos # stimuli if transp == '1': env = imgs['transp'] trp = np.mean(imgs['transp'][imgs['disk'] > 0]) ann_r = ann_r - trp ped_r = ped_r - trp else: env = imgs['env'] pedestal = illum * (env + ann_r*imgs['annulus'] + ped_r * imgs['disk']) increment = illum * (env + ann_r*imgs['annulus'] + (1 + incr) * ped_r * imgs['disk']) # texture creation in buffer : stimulus ped_tex = hrl.graphics.newTexture(pedestal) incr_tex = hrl.graphics.newTexture(increment) # Show stimulus # draw the checkerboard s if pos == '1': ped_tex.draw((0,0)) hrl.graphics.flip() # clr= True to clear buffer time.sleep(0.5) hrl.graphics.flip() time.sleep(0.5) incr_tex.draw((0,0)) hrl.graphics.flip() time.sleep(0.5) else: ped_tex.draw((0,0)) hrl.graphics.flip() time.sleep(0.5) hrl.graphics.flip() time.sleep(0.5) incr_tex.draw((0,0)) hrl.graphics.flip() time.sleep(0.5) # flip everything hrl.graphics.flip() # loop for waiting a response no_resp = True while no_resp: # as long as no_resp TRUE response, btn, t1 = read_response(hrl) print "btn =", btn if btn != None: no_resp = False rfl.write('%d\t%d\t%f\t%f\t%f\t%f\t%f\t%f\t%s\t%s\t%d\t%f\t%f\n' % (block, trl, illum, ann, ped, ann_r, ped_r, incr, transp, pos, response, t1, time.time())) rfl.flush() # clean checkerboard texture from buffer # (needed! Specially if hundreds of trials are presented, if not # cleared they accummulate in buffer) graphics.deleteTextureDL(incr_tex._dlid) graphics.deleteTextureDL(ped_tex._dlid) return response
def run_trial(hrl,trl, block,start_trl, end_trl): whlf = WIDTH/2.0 hhlf = HEIGHT/2.0 # clears screen hrl.graphics.flip() # draws fixation dot in the middle frm1 = hrl.graphics.newTexture(np.ones((2, 2))*0.0) frm1.draw(( whlf, hhlf)) hrl.graphics.flip() # sleeps for 250 ms time.sleep(0.25) # print("TRIAL =", trl) #show a break screen automatically after so many trials if (trl-start_trl)%80==0 and (trl-start_trl)!=0: show_break(hrl,trl, (start_trl + (end_trl-start_trl))) # current trial design variables thistrial = int(design['Trial'][trl]) tau1 = float(design['tau1'][trl]) tau2 = float(design['tau2'][trl]) alpha1 = float(design['alpha1'][trl]) alpha2 = float(design['alpha2'][trl]) print(tau1) print(alpha1) print(tau2) print(alpha2) # stimuli trialname1 = '%d_a_%.2f_tau_%.2f' % (thistrial, alpha1, tau1) stim_name1 = 'stimuli/%s/mlcm/block_%d_%s_cropped' % (vp_id, block, trialname1) trialname2 = '%d_a_%.2f_tau_%.2f' % (thistrial, alpha2, tau2) stim_name2 = 'stimuli/%s/mlcm/block_%d_%s_cropped' % (vp_id, block, trialname2) #print stim_name # load stimlus image and convert from png to numpy array curr_image1 = image_to_array(stim_name1) curr_image2 = image_to_array(stim_name2) # texture creation in buffer : stimulus checkerboard1 = hrl.graphics.newTexture(curr_image1) checkerboard2 = hrl.graphics.newTexture(curr_image2) # Show stimlus # draw the checkerboard s checkerboard1.draw((0, hhlf - checkerboard1.hght/2)) checkerboard2.draw((whlf, hhlf - checkerboard1.hght/2)) # flip everything hrl.graphics.flip(clr=False) # clr= True to clear buffer # loop for waiting a response no_resp = True while no_resp: # as long as no_resp TRUE response, btn, t1 = read_response(hrl) print("btn =", btn) if btn != None: no_resp = False rfl.write('%d\t%d\t%f\t%f\t%f\t%f\t%d\t%f\t%f\n' % (block, trl, alpha1, alpha2, tau1, tau2, response, t1, time.time())) rfl.flush() # clean checkerboard texture from buffer # (needed! Specially if hundreds of trials are presented, if not # cleared they accummulate in buffer) graphics.deleteTextureDL(checkerboard1._dlid) graphics.deleteTextureDL(checkerboard2._dlid) return response
def run_trial(hrl, trl, start_trl, end_trl): # function written by Torsten and edited by Christiane, reused by GA # read out variable values for each trial from the design matrix print("TRIAL =", trl) #show break automatically, define after how many trials if (trl - start_trl) % 50 == 0: show_break(hrl, (trl - start_trl), (end_trl - start_trl)) # get values from design matrix for current trial context, r, Trial = design['context'][trl], float(design['r'][trl]), int( design['Trial'][trl]) # use these variable values to define test stimulus (name corresponds to design matrix and name of saved image) trialname = '%d_%s_%.2f' % (Trial, context, r) stim_name = 'stimuli/%s/matching/%s' % (vp_id, trialname) #print "r =", r # load stimlus image and convert from png to numpy array curr_image = image_to_array(stim_name) # texture creation in buffer : stimulus checkerboard = hrl.graphics.newTexture(curr_image) # random assignment of start match intensity between 0 and 1 start_idx = random.random() no_match = True match_lum = start_idx # generate match stimulus trial_match, all_surround = match_stimulus(trl) t1 = time.time() while no_match: # as long as no_match TRUE #curr_match = np.array(trial_match.shape, dtype=np.float64) curr_match = trial_match / 255. #print curr_match[60] # Show stimulus and match show_stimulus(hrl, checkerboard, curr_match, match_lum) # adjust the lumiance match_lum, btn = adjust_loop(hrl, match_lum, checkerboard, curr_match) print("btn =", btn) if btn == 'Space': no_match = False t2 = time.time() resptime = t2 - t1 timestamp = time.time() rfl.write('%d\t%s\t%s\t%f\t%f\t%f\t%f\t%f\n' % (Trial, context, stim_name, r, start_idx, match_lum, resptime, timestamp)) rfl.flush() # surround information of matching patch should be written together with matched value fid_all_match.write( '%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n' % (all_surround[0, 0], all_surround[0, 1], all_surround[0, 2], all_surround[0, 3], all_surround[0, 4], all_surround[1, 0], all_surround[1, 1], all_surround[1, 2], all_surround[1, 3], all_surround[1, 4], all_surround[2, 0], all_surround[2, 1], all_surround[2, 3], all_surround[2, 4], all_surround[3, 0], all_surround[3, 1], all_surround[3, 2], all_surround[3, 3], all_surround[3, 4], all_surround[4, 0], all_surround[4, 1], all_surround[4, 2], all_surround[4, 3], all_surround[4, 4])) fid_all_match.flush() # screenshooting #gl.glReadBuffer(gl.GL_FRONT) #pixels = gl.glReadPixels(0,0, WIDTH, HEIGHT, gl.GL_RGB, gl.GL_UNSIGNED_BYTE) #image = Image.fromstring("RGB", (WIDTH, HEIGHT), pixels) #image = image.transpose( Image.FLIP_TOP_BOTTOM) #image.save('screenshot_%d.png' % trl) # clean checkerboard texture graphics.deleteTextureDL(checkerboard._dlid) return match_lum