code: def optimize_scrub(motion_outliers_index, n_scans, scrub):
      """Remove continuous segments with fewer than a minimal segment length.
  
      Parameters
      ----------
      motion_outliers_index : numpy.ndarray
          Index array of shape (n_motion_outliers) indicating the volumes
          that are motion outliers.
  
      n_scans : int
          Number of volumes in the functional image.
  
      scrub : int, default=5
          Minimal segment length.
  
      Returns
      -------
      motion_outliers_index : numpy.ndarray
          Index of outlier volumes.
      """
      # Start by checking if the beginning continuous segment is fewer than
      # a minimal segment length (default to 5)
      if motion_outliers_index[0] < scrub:
          motion_outliers_index = np.asarray(
              list(range(motion_outliers_index[0]))
              + list(motion_outliers_index)
          )
      # Do the same for the ending segment of scans
      if n_scans - (motion_outliers_index[-1] + 1) < scrub:
          motion_outliers_index = np.asarray(
              list(motion_outliers_index)
              + list(range(motion_outliers_index[-1], n_scans))
          )
      # Now do everything in between
      fd_outlier_ind_diffs = np.diff(motion_outliers_index)
      short_segments_inds = np.where(
          np.logical_and(
              fd_outlier_ind_diffs > 1,
              fd_outlier_ind_diffs < (scrub + 1),
          )
      )[0]
      for ind in short_segments_inds:
          motion_outliers_index = np.asarray(
              list(motion_outliers_index)
              + list(
                  range(
                      motion_outliers_index[ind] + 1,
                      motion_outliers_index[ind + 1],
                  )
              )
          )
      motion_outliers_index = np.sort(np.unique(motion_outliers_index))
      return motion_outliers_index
  
imports: import numpy
  import pandas
label_desc: Use image node_id 1 for calling a global function with example usage: optimize_scrub(motion_outliers_index, n_scans, scrub) and returns: motion_outliers_index