smathot

MOT

Feb 2nd, 2013
1,162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.14 KB | None | 0 0
  1. # Import the necessary modules
  2. import random
  3. import math
  4. from openexp.canvas import canvas
  5. from openexp.mouse import mouse
  6.  
  7. w, h = exp.resolution()
  8.  
  9. # Define some general properties of the experiment
  10. NStim = 4
  11. NTarget = 2
  12. NFrames = 50
  13. maxRotSpeed = .1
  14. firstFrameDur = 1000
  15. targetColor = 'red'
  16. normalColor = 'white'
  17. winSpeed = 8
  18. winSize = 400, 400 # The size of the window
  19. winPos = w/2, h/2 # The position of the center of the window
  20. winMargin = 20  # The margin around the window used to draw the edge (so the window
  21.                 # appears a bit larger than the winSize)
  22. clickMargin = 100 # The margin near the edge that will trigger a move when clicked
  23.  
  24. # Replay should be a variable that is set to 0 or 1. If replay is not enabled, the
  25. # trajectory will be remembered to be played back later on. If replay is enabled,
  26. # a previous trajectory will be used to play back.
  27. replay = self.get('replay')
  28. if not replay:
  29.     trajectory = []
  30. else:
  31.     random.shuffle(trajectories)
  32.     trajectory = trajectories.pop()
  33.                
  34. # Initiate a mouse object
  35. # (See documentation: http://osdoc.cogsci.nl/python-inline-code/mouse-functions)
  36. my_mouse = mouse(exp, visible=True)
  37.  
  38. # Create a list of stimuli, with various properties
  39. stimList = []
  40. for i in range(NStim):
  41.     x = random.randint(-winSize[0]/2, winSize[0]/2)
  42.     y = random.randint(-winSize[1]/2, winSize[1]/2)
  43.     a = random.random() * 2*math.pi
  44.     v = 4.
  45.     r = 20
  46.     stimList.append( (x, y, a, v, r) )
  47.        
  48. # Create a canvas
  49. # (See documentation: http://osdoc.cogsci.nl/python-inline-code/canvas-functions)
  50. myCanvas = canvas(exp)
  51.    
  52. # Initially the targetPosition of the window is the same as the winPos, so that the
  53. # window doesn't move.
  54. targetPos = winPos
  55.    
  56. # Loop through all frames
  57. for j in range(NFrames):
  58.  
  59.     # Add the current position to the trajectory in we are not in replay mode,
  60.     # otherwise retrieve the first winPos from the to-be-played-back trajectory.
  61.     if not replay:
  62.         trajectory.append(winPos)
  63.     else:
  64.         winPos = trajectory.pop(0)
  65.  
  66.     # For each frame, clear the canvas and then loop through all stimuli
  67.     myCanvas.clear()
  68.     # Draw a rectangle to indicate the position of the window. Make it a bit bigger
  69.     # than the actual window size, so that the stimuli do not overlap the edge
  70.     myCanvas.rect(winPos[0]-winSize[0]/2-winMargin, winPos[1]-winSize[1]/2-winMargin, winSize[0]+2*winMargin, winSize[1]+2*winMargin)
  71.     for i in range(NStim):
  72.         # Get the stimulus properties
  73.         x, y, a, v, r = stimList[i]
  74.         # Update the position of the stimulus based on the angle and the speed
  75.         x += v * math.cos(a)
  76.         y += v * math.sin(a)   
  77.         # If the stimulus leaves the screen, reverse direction by 180 deg (= 1pi radial)
  78.         if x <= -winSize[0]/2 or x >= winSize[0]/2 or y <= -winSize[1]/2 or y >= winSize[1]/2:
  79.             a = a + math.pi
  80.         else:
  81.             # else randomly rotate the stimulus a bit
  82.             a += (random.random()-.5) * maxRotSpeed        
  83.         # Highlight the targets on the first frame
  84.         if i < NTarget and j == 0:
  85.             color = targetColor
  86.         else:
  87.             color = normalColor        
  88.         # Draw the stimulus
  89.         myCanvas.circle(x+winPos[0], y+winPos[1], r, fill=True, color=color)
  90.         # Store the new stimulus properties back in the stimulus list
  91.         stimList[i] = x, y, a, v, r
  92.     # Show the canvas  
  93.     myCanvas.show()
  94.    
  95.     if j == 0:
  96.         self.sleep(firstFrameDur)  
  97.  
  98.     # Get a mouse click
  99.     button, position, timestamp = my_mouse.get_click(timeout=1)
  100.     if position != None:
  101.         x, y = position            
  102.         # The target position is not that of the mouse cursor, but one of the edges
  103.         if x < clickMargin:
  104.             targetPos = winSize[0]/2, h/2
  105.         elif x > w-clickMargin:    
  106.             targetPos = w-winSize[0]/2, h/2
  107.         elif y < clickMargin:
  108.             targetPos = w/2, winSize[1]/2
  109.         elif y > h-clickMargin:    
  110.             targetPos = w/2, h-winSize[1]/2
  111.                
  112.     # Move the window nearer the target position
  113.     if targetPos[0] < winPos[0]:
  114.         winPos = winPos[0]-winSpeed, winPos[1]
  115.     elif targetPos[0] > winPos[0]:
  116.         winPos = winPos[0]+winSpeed, winPos[1]
  117.     if targetPos[1] < winPos[1]:
  118.         winPos = winPos[0], winPos[1]-winSpeed
  119.     elif targetPos[1] > winPos[1]:
  120.         winPos = winPos[0], winPos[1]+winSpeed     
  121.    
  122. # Remember this trajectory if we are not in replay mode
  123. if not replay:
  124.     trajectories.append(trajectory)
  125.    
  126. # Store the coordinates of the stimuli in the last frame, in order to compare
  127. # the mouse click responses with the actual positions
  128.  
  129. stimID = 0
  130. # Loop through the stimulus list
  131. for stimProperties in stimList:
  132.     stimID +=1
  133.     # By setting variables with the exp.set() function you make sure the variables
  134.     # are also available in the GUI (notably, the logger item)
  135.     exp.set("xStim"+str(stimID), stimProperties[0])
  136.     exp.set("yStim"+str(stimID), stimProperties[1])
  137.  
  138. # Collect response
  139. responseID = 0
  140. for i in range(NTarget):
  141.     responseID +=1
  142.     print responseID
  143.     # Collect the response
  144.     button, position, timestamp = my_mouse.get_click()
  145.    
  146.     # Make the cursor position relative to the window
  147.     position = position[0] - winPos[0], position[1] - winPos[1]
  148.    
  149.     # Set the x and y coordinates of the response so that they will be logged by the
  150.     # logger item. Position is an (x,y) tuple in screen coordinates
  151.     exp.set("xMouseClick"+str(responseID), position[0])
  152.     exp.set("yMouseClick"+str(responseID), position[1])
Advertisement
Add Comment
Please, Sign In to add comment