code: def simulate_sparse_stc(
      src,
      n_dipoles,
      times,
      data_fun=lambda t: 1e-7 * np.sin(20 * np.pi * t),
      labels=None,
      random_state=None,
      location="random",
      subject=None,
      subjects_dir=None,
      surf="sphere",
  ):
      """Generate sparse (n_dipoles) sources time courses from data_fun.
  
      This function randomly selects ``n_dipoles`` vertices in the whole
      cortex or one single vertex (randomly in or in the center of) each
      label if ``labels is not None``. It uses ``data_fun`` to generate
      waveforms for each vertex.
  
      Parameters
      ----------
      src : instance of SourceSpaces
          The source space.
      n_dipoles : int
          Number of dipoles to simulate.
      times : array
          Time array.
      data_fun : callable
          Function to generate the waveforms. The default is a 100 nAm, 10 Hz
          sinusoid as ``1e-7 * np.sin(20 * pi * t)``. The function should take
          as input the array of time samples in seconds and return an array of
          the same length containing the time courses.
      labels : None | list of Label
          The labels. The default is None, otherwise its size must be n_dipoles.
      %(random_state)s
      location : str
          The label location to choose. Can be ``'random'`` (default) or
          ``'center'`` to use :func:`mne.Label.center_of_mass`. Note that for
          ``'center'`` mode the label values are used as weights.
  
          .. versionadded:: 0.13
      subject : str | None
          The subject the label is defined for.
          Only used with ``location='center'``.
  
          .. versionadded:: 0.13
      %(subjects_dir)s
  
          .. versionadded:: 0.13
      surf : str
          The surface to use for Euclidean distance center of mass
          finding. The default here is "sphere", which finds the center
          of mass on the spherical surface to help avoid potential issues
          with cortical folding.
  
          .. versionadded:: 0.13
  
      Returns
      -------
      stc : SourceEstimate
          The generated source time courses.
  
      See Also
      --------
      simulate_raw
      simulate_evoked
      simulate_stc
  
      Notes
      -----
      .. versionadded:: 0.10.0
      """
      rng = check_random_state(random_state)
      src = _ensure_src(src, verbose=False)
      subject_src = src._subject
      if subject is None:
          subject = subject_src
      elif subject_src is not None and subject != subject_src:
          raise ValueError(
              "subject argument (%s) did not match the source "
              "space subject_his_id (%s)" % (subject, subject_src)
          )
      data = np.zeros((n_dipoles, len(times)))
      for i_dip in range(n_dipoles):
          data[i_dip, :] = data_fun(times)
  
      if labels is None:
          # can be vol or surface source space
          offsets = np.linspace(0, n_dipoles, len(src) + 1).astype(int)
          n_dipoles_ss = np.diff(offsets)
          # don't use .choice b/c not on old numpy
          vs = [
              s["vertno"][
                  np.sort(rng.permutation(np.arange(s["nuse"]))[:n])
              ]
              for n, s in zip(n_dipoles_ss, src)
          ]
          datas = data
      elif n_dipoles > len(labels):
          raise ValueError(
              "Number of labels (%d) smaller than n_dipoles (%d) "
              "is not allowed." % (len(labels), n_dipoles)
          )
      else:
          if n_dipoles != len(labels):
              warn(
                  "The number of labels is different from the number of "
                  "dipoles. %s dipole(s) will be generated."
                  % min(n_dipoles, len(labels))
              )
          labels = (
              labels[:n_dipoles] if n_dipoles < len(labels) else labels
          )
  
          vertno = [[], []]
          lh_data = [np.empty((0, data.shape[1]))]
          rh_data = [np.empty((0, data.shape[1]))]
          for i, label in enumerate(labels):
              lh_vertno, rh_vertno = select_source_in_label(
                  src, label, rng, location, subject, subjects_dir, surf
              )
              vertno[0] += lh_vertno
              vertno[1] += rh_vertno
              if len(lh_vertno) != 0:
                  lh_data.append(data[i][np.newaxis])
              elif len(rh_vertno) != 0:
                  rh_data.append(data[i][np.newaxis])
              else:
                  raise ValueError("No vertno found.")
          vs = [np.array(v) for v in vertno]
          datas = [np.concatenate(d) for d in [lh_data, rh_data]]
          # need to sort each hemi by vertex number
          for ii in range(2):
              order = np.argsort(vs[ii])
              vs[ii] = vs[ii][order]
              if len(order) > 0:  # fix for old numpy
                  datas[ii] = datas[ii][order]
          datas = np.concatenate(datas)
  
      tmin, tstep = times[0], np.diff(times[:2])[0]
      assert datas.shape == data.shape
      cls = SourceEstimate if len(vs) == 2 else VolSourceEstimate
      stc = cls(
          datas, vertices=vs, tmin=tmin, tstep=tstep, subject=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 2 for calling a global function with example usage: simulate_sparse_stc(src, n_dipoles, times, data_fun, labels, random_state, location, subject, subjects_dir, surf) and returns: stc