Advertisement
Marsja

PsychoPy data

Dec 20th, 2015
1,400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.33 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sun Dec 20 21:33:01 2015
  4.  
  5. @author: erik  marsja se
  6. Full script for the tutorial on http://www.marsja.se/trialhandler-a-psychopy-tutorial/
  7. """
  8.  
  9. from psychopy import data, visual, core, event
  10.  
  11. visual_targets = [digit for digit in xrange(1,9)]
  12.  
  13. targets_responses = []
  14. for target in visual_targets:
  15.    
  16.    if target %2 == 0:
  17.         correct_response = 'x'
  18.  
  19.    else:
  20.         correct_response = 'z'
  21.    
  22.    targets_responses.append({'Target':target, 'CorrectResponse':correct_response})
  23.    
  24.  
  25. trials = data.TrialHandler(targets_responses,20, method='random')
  26.  
  27. trials.data.addDataType('Response')
  28. trials.data.addDataType('Accuracy')
  29.  
  30. experiment_window = visual.Window(size=(800,600),winType='pyglet',fullscr=False,
  31.                         screen=0, monitor='testMonitor',
  32.                         color="black", colorSpace='rgb')
  33.  
  34.  
  35. screen_text = visual.TextStim(experiment_window,text=None,
  36.                               alignHoriz="center", color = 'white')
  37.                              
  38. trial_timer = core.Clock()
  39.  
  40. for trial in trials:
  41.     current_time = 0
  42.     trial_still_running = True
  43.     trial_timer.reset()
  44.     while trial_still_running:
  45.         current_time = trial_timer.getTime()
  46.        
  47.         if current_time <=.4:
  48.             screen_text.setText('+')
  49.             screen_text.draw()
  50.            
  51.         if current_time >= .4 and current_time <=.8:
  52.             screen_text.setText(trial['Target'])
  53.             screen_text.draw()
  54.            
  55.         if current_time >= .8 and current_time <=1.8:
  56.             screen_text.setText('+')
  57.             screen_text.draw()
  58.            
  59.             responded = event.getKeys()
  60.             if responded:
  61.                 print responded
  62.  
  63.                 if trial['CorrectResponse'] == responded[0]:
  64.                     accuracy = 1
  65.                 else: accuracy = 0
  66.  
  67.         if current_time >= 1.8:
  68.             if not responded:
  69.                 accuracy = 0
  70.                
  71.             trial_still_running = False
  72.         experiment_window.flip()
  73.  
  74.     trials.data.add('Accuracy', accuracy)
  75.     trials.data.add('Response', responded[0])
  76.  
  77. trials.saveAsExcel(fileName='data.csv',
  78.                   sheetName = 'rawData',
  79.                   stimOut=[],
  80.                   dataOut=['all_raw'])
  81.  
  82. experiment_window.close()
  83. core.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement