code: def test_source_simulator(_get_fwd_labels):
      """Test Source Simulator."""
      fwd, _ = _get_fwd_labels
  
      src = fwd["src"]
      hemi_to_ind = {"lh": 0, "rh": 1}
      tstep = 1.0 / 6.0
  
      label_vertices = [[], [], []]
      label_vertices[0] = np.arange(1000)
      label_vertices[1] = np.arange(500, 1500)
      label_vertices[2] = np.arange(1000)
  
      hemis = ["lh", "lh", "rh"]
  
      mylabels = []
      src_vertices = []
      for i, vert in enumerate(label_vertices):
          new_label = Label(vertices=vert, hemi=hemis[i])
          mylabels.append(new_label)
          src_vertices.append(
              np.intersect1d(
                  src[hemi_to_ind[hemis[i]]]["vertno"],
                  new_label.vertices,
              )
          )
  
      wfs = [[], [], []]
  
      wfs[0] = np.array([0, 1.0, 0])  # 1d array
      wfs[1] = [np.array([0, 1.0, 0]), np.array([0, 1.5, 0])]  # list
      wfs[2] = np.array([[1, 1, 1.0]])  # 2d array
  
      events = [[], [], []]
      events[0] = np.array([[0, 0, 1], [3, 0, 1]])
      events[1] = np.array([[0, 0, 1], [3, 0, 1]])
      events[2] = np.array([[0, 0, 1], [2, 0, 1]])
  
      verts_lh = np.intersect1d(range(1500), src[0]["vertno"])
      verts_rh = np.intersect1d(range(1000), src[1]["vertno"])
      diff_01 = len(np.setdiff1d(src_vertices[0], src_vertices[1]))
      diff_10 = len(np.setdiff1d(src_vertices[1], src_vertices[0]))
      inter_10 = len(np.intersect1d(src_vertices[1], src_vertices[0]))
  
      output_data_lh = np.zeros([len(verts_lh), 6])
      tmp = np.array([0, 1.0, 0, 0, 1, 0])
      output_data_lh[:diff_01, :] = np.tile(tmp, (diff_01, 1))
  
      tmp = np.array([0, 2, 0, 0, 2.5, 0])
      output_data_lh[diff_01 : diff_01 + inter_10, :] = np.tile(
          tmp, (inter_10, 1)
      )
      tmp = np.array([0, 1, 0, 0, 1.5, 0])
      output_data_lh[diff_01 + inter_10 :, :] = np.tile(
          tmp, (diff_10, 1)
      )
  
      data_rh_wf = np.array([1.0, 1, 2, 1, 1, 0])
      output_data_rh = np.tile(data_rh_wf, (len(src_vertices[2]), 1))
      output_data = np.vstack([output_data_lh, output_data_rh])
  
      ss = SourceSimulator(src, tstep)
      for i in range(3):
          ss.add_data(mylabels[i], wfs[i], events[i])
  
      stc = ss.get_stc()
      stim_channel = ss.get_stim_channel()
  
      # Make some size checks.
      assert ss.duration == 1.0
      assert ss.n_times == 6
      assert ss.last_samp == 5
  
      assert len(stim_channel) == stc.data.shape[1]
      assert np.all(stc.vertices[0] == verts_lh)
      assert np.all(stc.vertices[1] == verts_rh)
      assert_array_almost_equal(stc.lh_data, output_data_lh)
      assert_array_almost_equal(stc.rh_data, output_data_rh)
      assert_array_almost_equal(stc.data, output_data)
  
      counter = 0
      for stc, stim in ss:
          assert stc.data.shape[1] == 6
          counter += 1
      assert counter == 1
  
      # Check validity of setting duration and start/stop parameters.
      half_ss = SourceSimulator(src, tstep, duration=0.5)
      for i in range(3):
          half_ss.add_data(mylabels[i], wfs[i], events[i])
      with pytest.raises(TypeError, match="array of integers"):
          half_ss.add_data(mylabels[0], wfs[0], events[0].astype(float))
      half_stc = half_ss.get_stc()
      assert_array_almost_equal(stc.data[:, :3], half_stc.data)
  
      part_stc = ss.get_stc(start_sample=1, stop_sample=4)
      assert part_stc.shape == (24, 4)
      assert part_stc.times[0] == tstep
  
      # Check validity of other arguments.
      with pytest.raises(ValueError, match="start_sample must be"):
          ss.get_stc(2, 0)
      ss = SourceSimulator(src)
      with pytest.raises(ValueError, match="No simulation parameters"):
          ss.get_stc()
      with pytest.raises(ValueError, match="label must be a Label"):
          ss.add_data(1, wfs, events)
      with pytest.raises(
          ValueError,
          match="Number of waveforms and events " "should match",
      ):
          ss.add_data(mylabels[0], wfs[:2], events)
      with pytest.raises(ValueError, match="duration must be None or"):
          ss = SourceSimulator(src, tstep, tstep / 2)
  
      # Verify first_samp functionality.
      ss = SourceSimulator(src, tstep)
      offset = 50
      for i in range(3):  # events are offset, but first_samp = 0
          events[i][:, 0] += offset
          ss.add_data(mylabels[i], wfs[i], events[i])
      offset_stc = ss.get_stc()
      assert ss.n_times == 56
      assert ss.first_samp == 0
      assert offset_stc.data.shape == (
          stc.data.shape[0],
          stc.data.shape[1] + offset,
      )
      ss = SourceSimulator(src, tstep, first_samp=offset)
      for i in range(3):  # events still offset, but first_samp > 0
          ss.add_data(mylabels[i], wfs[i], events[i])
      offset_stc = ss.get_stc()
      assert ss.n_times == 6
      assert ss.first_samp == offset
      assert ss.last_samp == offset + 5
      assert offset_stc.data.shape == stc.data.shape
  
      # Verify that the chunks have the correct length.
      source_simulator = SourceSimulator(
          src, tstep=tstep, duration=10 * tstep
      )
      source_simulator.add_data(
          mylabels[0], np.array([1, 1, 1]), [[0, 0, 0]]
      )
  
      source_simulator._chk_duration = (
          6  # Quick hack to get short chunks.
      )
      stcs = [stc for stc, _ in source_simulator]
      assert len(stcs) == 2
      assert stcs[0].data.shape[1] == 6
      assert stcs[1].data.shape[1] == 4
  
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 8 for calling a global function with example usage: test_source_simulator(_get_fwd_labels) without return types