def detect_saccades(orientations, timestep):
    orientations = orientations[~numpy.isnan(orientations)]

    unwrappedOrientations = numpy.unwrap(orientations, 180)
    SMOOTH_TIME = 1
    smoothWindow = int(round(SMOOTH_TIME / timestep))
    smoothedOrientations = tools.smooth(unwrappedOrientations, smoothWindow)
    angSpds = abs(numpy.diff(smoothedOrientations)) / timestep
    MIN_PEAK_SPD = 30
    sacInds = tools.local_minima(-angSpds) & (angSpds > MIN_PEAK_SPD)
    sacSpds = angSpds[sacInds]

    MIN_SAC_SPD = 5
    inSac = angSpds > MIN_SAC_SPD
    startInds = pylab.find(numpy.diff(inSac.view(dtype=int8)) == 1)
    stopInds = pylab.find(numpy.diff(inSac.view(dtype=int8)) == -1)
    if stopInds[-1] < startInds[-1]:
        l = stopInds.tolist()
        l.append(len(inSac) - 1)
        stopInds = numpy.array(l)
    if startInds[0] > stopInds[0]:
        l = startInds.tolist()
        l.insert(0, 0)
        startInds = numpy.array(l)

    angDisp = numpy.zeros(len(angSpds))
    for i, startInd in enumerate(startInds):
        stopInd = stopInds[i]
        angDisp[startInd:stopInd] = smoothedOrientations[stopInd] - smoothedOrientations[startInd]

    sacAmps = angDisp[sacInds]
    return sacAmps
def detect_saccades0(orientations, timestep):
    orientations = orientations[~numpy.isnan(orientations)]

    unwrappedOrientations = numpy.unwrap(orientations, 180)
    SMOOTH_TIME = 0.5
    smoothWindow = int(round(SMOOTH_TIME / timestep))
    smoothedOrientations = tools.smooth(unwrappedOrientations, smoothWindow)
    angSpds = abs(numpy.diff(smoothedOrientations)) / timestep
    sacInds = tools.local_minima(-angSpds) & (angSpds > 10)
    saccades = angSpds[sacInds]
    # sacs = tools.zigzag(smoothedOrientations)
    # sacInds = abs(sacs) > 10
    # saccades = sacs[sacInds]
    return saccades