code: def test_simulate_sparse_stc(_get_fwd_labels):
      """Test generation of sparse source estimate."""
      pytest.importorskip("nibabel")
      fwd, labels = _get_fwd_labels
      n_times = 10
      tmin = 0
      tstep = 1e-3
      times = np.arange(n_times, dtype=np.float64) * tstep + tmin
  
      pytest.raises(
          ValueError,
          simulate_sparse_stc,
          fwd["src"],
          len(labels),
          times,
          labels=labels,
          location="center",
          subject="sample",
          subjects_dir=subjects_dir,
      )  # no non-zero values
  
      mylabels = []
      for label in labels:
          this_label = label.copy()
          this_label.values.fill(1.0)
          mylabels.append(this_label)
  
      for location in ("random", "center"):
          random_state = 0 if location == "random" else None
          stc_1 = simulate_sparse_stc(
              fwd["src"],
              len(mylabels),
              times,
              labels=mylabels,
              random_state=random_state,
              location=location,
              subjects_dir=subjects_dir,
          )
          assert_equal(stc_1.subject, "sample")
  
          assert stc_1.data.shape[0] == len(mylabels)
          assert stc_1.data.shape[1] == n_times
  
          # make sure we get the same result when using the same seed
          stc_2 = simulate_sparse_stc(
              fwd["src"],
              len(mylabels),
              times,
              labels=mylabels,
              random_state=random_state,
              location=location,
              subjects_dir=subjects_dir,
          )
  
          assert_array_equal(stc_1.lh_vertno, stc_2.lh_vertno)
          assert_array_equal(stc_1.rh_vertno, stc_2.rh_vertno)
  
      # Degenerate cases
      pytest.raises(
          ValueError,
          simulate_sparse_stc,
          fwd["src"],
          len(mylabels),
          times,
          labels=mylabels,
          location="center",
          subject="foo",
          subjects_dir=subjects_dir,
      )  # wrong subject
      del fwd["src"][0]["subject_his_id"]  # remove subject
      pytest.raises(
          ValueError,
          simulate_sparse_stc,
          fwd["src"],
          len(mylabels),
          times,
          labels=mylabels,
          location="center",
          subjects_dir=subjects_dir,
      )  # no subject
      fwd["src"][0]["subject_his_id"] = "sample"  # put back subject
      pytest.raises(
          ValueError,
          simulate_sparse_stc,
          fwd["src"],
          len(mylabels),
          times,
          labels=mylabels,
          location="foo",
      )  # bad location
      err_str = "Number of labels"
      with pytest.raises(ValueError, match=err_str):
          simulate_sparse_stc(
              fwd["src"],
              len(mylabels) + 1,
              times,
              labels=mylabels,
              random_state=random_state,
              location=location,
              subjects_dir=subjects_dir,
          )
  
file_path: mne-python/mne/simulation/tests/test_source.py
imports: import numpy
  import pytest
  from numpy.testing import assert_array_almost_equal, assert_array_equal, assert_equal
  from mne import convert_forward_solution, pick_types_forward, read_forward_solution, read_label
  from mne.datasets import testing
  from mne.label import Label
  from mne.simulation import SourceSimulator, simulate_sparse_stc, simulate_stc
label_desc: Use image node_id 4 for calling a global function with example usage: test_simulate_sparse_stc(_get_fwd_labels) without return types