Advertisement
smathot

Saccade detection

Mar 29th, 2013
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. import numpy as np
  2. from matplotlib import pyplot as plt
  3.  
  4. # Source files for data
  5. src = '/home/sebastiaan/Data/0045 Pupil cuing/0045E Pre-saccadic pupil/traces/0045E00A.asc-%.5d-%s.npy'
  6.  
  7. vThr = 2 # Velocity threshold for both saccade onset and offset
  8. aThr = .1 # Acceleration threshold for saccade onset
  9. wLen = 5 # Moving window size. Larger values are more accurate but slower.
  10.  
  11. for trialId in range(100):
  12.  
  13.     # Load the data into a numpy array with three columns: x, y, pupil size
  14.     a = np.concatenate([
  15.         np.load(src % (trialId, 'baseline')),
  16.         np.load(src % (trialId, 'cue')),
  17.         np.load(src % (trialId, 'postSacc'))
  18.         ])
  19.  
  20.  
  21.     # We will use this array to maintain the eye position during the moving
  22.     # window average.
  23.     aPos = np.zeros( [wLen, 2] )
  24.  
  25.     # These arrays are to store the outcome so that we can plot it later on.
  26.     # They don't play a role in the detection algorithm.
  27.     _vel = np.zeros(len(a))
  28.     _acc = np.zeros(len(a))
  29.     _sacc = np.zeros(len(a))
  30.  
  31.     inSacc = 0 # 0 or 1 depending on whether we are in a saccade
  32.  
  33.     for i in range(len(a)):
  34.  
  35.         # Get a sample. Here we read the x, y coordinates from the data.
  36.         # Generally you would query the eye tracker.
  37.         x = a[i,0]
  38.         y = a[i,1]
  39.  
  40.         # Shift the array one step forward and insert the new sample as the
  41.         # first entry in the array.
  42.         aPos[1:] = aPos[:-1]
  43.         aPos[0] = [x,y]
  44.  
  45.         # Get the coordinates difference scores for consecutive samples. This is
  46.         # a 2D array (so velocity for x and y separately).
  47.         d = aPos[1:] - aPos[:-1]
  48.         # Calculate the velocity. This is a 1D array, which is the Pythagoras of
  49.         # the previous array.
  50.         aVel = np.sqrt(d[:,0]**2 + d[:,1]**2)
  51.         # Calculate the difference scores for the velocity, i.e. the
  52.         # acceleration.
  53.         aAcc = aVel[:-1] - aVel[1:]
  54.  
  55.         # Calculate the mean velocity and acceleration over the moving window.
  56.         mVel = aVel.mean()
  57.         mAcc = aAcc.mean()
  58.  
  59.         # If either the velocity or the acceleration threshold is exceeded,
  60.         # detect a saccade.
  61.         if mVel > vThr or mAcc > aThr:
  62.             inSacc = 1
  63.         # If the velocity drops back under threshold (you can use a lower
  64.         # threshold here as well, may be) detect and-end of-saccade.
  65.         if mVel < vThr:
  66.             inSacc = 0
  67.  
  68.         # Save for plotting
  69.         _vel[i] = mVel
  70.         _acc[i] = mAcc
  71.         _sacc[i] = inSacc
  72.  
  73.     # Plot to see it worked!
  74.     yPos = a[:,0]
  75.     plt.plot(yPos[wLen:] / 100, color='green')
  76.     plt.plot(_vel[wLen:], color='red')
  77.     plt.plot(_acc[wLen:], color='blue')
  78.     plt.plot(_sacc[wLen:] * 10, color='black')
  79.     plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement