code: def simulate_stc(
      src,
      labels,
      stc_data,
      tmin,
      tstep,
      value_fun=None,
      allow_overlap=False,
  ):
      """Simulate sources time courses from waveforms and labels.
  
      This function generates a source estimate with extended sources by
      filling the labels with the waveforms given in stc_data.
  
      Parameters
      ----------
      src : instance of SourceSpaces
          The source space.
      labels : list of Label
          The labels.
      stc_data : array, shape (n_labels, n_times)
          The waveforms.
      tmin : float
          The beginning of the timeseries.
      tstep : float
          The time step (1 / sampling frequency).
      value_fun : callable | None
          Function to apply to the label values to obtain the waveform
          scaling for each vertex in the label. If None (default), uniform
          scaling is used.
      allow_overlap : bool
          Allow overlapping labels or not. Default value is False.
  
          .. versionadded:: 0.18
  
      Returns
      -------
      stc : SourceEstimate
          The generated source time courses.
  
      See Also
      --------
      simulate_raw
      simulate_evoked
      simulate_sparse_stc
      """
      if len(labels) != len(stc_data):
          raise ValueError(
              "labels and stc_data must have the same length"
          )
  
      vertno = [[], []]
      stc_data_extended = [[], []]
      hemi_to_ind = {"lh": 0, "rh": 1}
      for i, label in enumerate(labels):
          hemi_ind = hemi_to_ind[label.hemi]
          src_sel = np.intersect1d(
              src[hemi_ind]["vertno"], label.vertices
          )
          if len(src_sel) == 0:
              idx = src[hemi_ind]["inuse"].astype("bool")
              xhs = src[hemi_ind]["rr"][idx]
              rr = src[hemi_ind]["rr"][label.vertices]
              closest_src = _compute_nearest(xhs, rr)
              src_sel = src[hemi_ind]["vertno"][np.unique(closest_src)]
  
          if value_fun is not None:
              idx_sel = np.searchsorted(label.vertices, src_sel)
              values_sel = np.array(
                  [value_fun(v) for v in label.values[idx_sel]]
              )
  
              data = np.outer(values_sel, stc_data[i])
          else:
              data = np.tile(stc_data[i], (len(src_sel), 1))
          # If overlaps are allowed, deal with them
          if allow_overlap:
              # Search for duplicate vertex indices
              # in the existing vertex matrix vertex.
              duplicates = []
              for src_ind, vertex_ind in enumerate(src_sel):
                  ind = np.where(vertex_ind == vertno[hemi_ind])[0]
                  if len(ind) > 0:
                      assert len(ind) == 1
                      # Add the new data to the existing one
                      stc_data_extended[hemi_ind][ind[0]] += data[
                          src_ind
                      ]
                      duplicates.append(src_ind)
              # Remove the duplicates from both data and selected vertices
              data = np.delete(data, duplicates, axis=0)
              src_sel = list(np.delete(np.array(src_sel), duplicates))
          # Extend the existing list instead of appending it so that we can
          # index its elements
          vertno[hemi_ind].extend(src_sel)
          stc_data_extended[hemi_ind].extend(np.atleast_2d(data))
  
      vertno = [np.array(v) for v in vertno]
      if not allow_overlap:
          for v, hemi in zip(vertno, ("left", "right")):
              d = len(v) - len(np.unique(v))
              if d > 0:
                  raise RuntimeError(
                      "Labels had %s overlaps in the %s "
                      "hemisphere, "
                      "they must be non-overlapping" % (d, hemi)
                  )
      # the data is in the order left, right
      data = list()
      for i in range(2):
          if len(stc_data_extended[i]) != 0:
              stc_data_extended[i] = np.vstack(stc_data_extended[i])
              # Order the indices of each hemisphere
              idx = np.argsort(vertno[i])
              data.append(stc_data_extended[i][idx])
              vertno[i] = vertno[i][idx]
  
      stc = SourceEstimate(
          np.concatenate(data),
          vertices=vertno,
          tmin=tmin,
          tstep=tstep,
          subject=src._subject,
      )
      return stc
  
file_path: mne-python/mne/simulation/source.py
imports: import numpy
  from ..fixes import rng_uniform
  from ..label import Label
  from ..source_estimate import SourceEstimate, VolSourceEstimate
  from ..source_space._source_space import _ensure_src
  from ..surface import _compute_nearest
  from ..utils import _check_option, _ensure_events, _ensure_int, check_random_state, fill_doc, warn
label_desc: Use image node_id 3 for calling a global function with example usage: simulate_stc(src, labels, stc_data, tmin, tstep, value_fun, allow_overlap) and returns: stc