code: def power_ratio(events, dat, s_freq, limits, ratio_thresh):
      
      ratio = empty(events.shape[0])
      for i, one_event in enumerate(events):
  
          x0 = one_event[0]
          x1 = one_event[2]
  
          if x0 < 0 or x1 >= len(dat):
              ratio[i] = 0
  
          else:
              f, Pxx = periodogram(dat[x0:x1], s_freq, scaling='spectrum')
              Pxx = sqrt(Pxx)  # use amplitude
  
              freq_sp = (f >= limits[0]) & (f <= limits[1])
              freq_nonsp = (f <= limits[1])
  
              ratio[i] = mean(Pxx[freq_sp]) / mean(Pxx[freq_nonsp])
  
      events = events[ratio > ratio_thresh, :]
  
      return events
docs: Estimate the ratio in power between spindle band and lower frequencies.
  
      Parameters
      ----------
      events : ndarray (dtype='int')
          N x 3 matrix with start, peak, end samples
      dat : ndarray (dtype='float')
          vector with the original data
      s_freq : float
          sampling frequency
      limits : tuple of float
          high and low frequencies for spindle band
      ratio_thresh : float
          ratio between spindle vs non-spindle amplitude
  
      Returns
      -------
      ndarray (dtype='int')
          N x 3 matrix with start, peak, end samples
  
      Notes
      -----
      In the original matlab script, it uses amplitude, not power.