code: def _select_period(detected, true_values):
      
      true_values = invert(true_values)
  
      for one_spindle in detected:
          # get the first time point when it goes above/below selection thres
          start_sel = where(true_values[:one_spindle[0]])[0]
          if start_sel.any():
              one_spindle[0] = start_sel[-1]
  
          # get the last time point when it stays above/below selection thres
          end_sel = where(true_values[one_spindle[2]:])[0] - 1
          if end_sel.any():
              one_spindle[2] += end_sel[0]
  
      return detected
docs: For the detected values, we check when it goes above/below the
      selection.
  
      Parameters
      ----------
      detected : ndarray (dtype='int')
          N x 3 matrix with starting and ending times.
      true_values : ndarray (dtype='bool')
          array with bool values
  
      Returns
      -------
      ndarray (dtype='int')
          N x 2 matrix with starting and ending times, but these periods are
          usually larger than those of the input, because the selection window is
          usually more lenient (lower threshold) than the detection window.
  
      Notes
      -----
      Both start and end time points are inclusive (not python convention, but
      matlab convention) because these values are converted to time points later.