code: def within_duration(events, time, limits):
      
      min_dur = max_dur = ones(events.shape[0], dtype=bool)
      
      if limits[0] is not None:
          min_dur = time[events[:, -1] - 1] - time[events[:, 0]] >= limits[0]
      
      if limits[1] is not None:
          max_dur = time[events[:, -1] - 1] - time[events[:, 0]] <= limits[1]
  
      return events[min_dur & max_dur, :]
docs: Check whether event is within time limits.
  
      Parameters
      ----------
      events : ndarray (dtype='int')
          N x M matrix with start sample first and end samples last on M
      time : ndarray (dtype='float')
          vector with time points
      limits : tuple of float
          low and high limit for spindle duration
  
      Returns
      -------
      ndarray (dtype='int')
          N x M matrix with start sample first and end samples last on M