code: def test_brain_traces(
      renderer_interactive_pyvistaqt, hemi, src, tmp_path, brain_gc
  ):
      """Test brain traces."""
      hemi_str = list()
      if src in ("surface", "vector", "mixed"):
          hemi_str.extend(
              [hemi] if hemi in ("lh", "rh") else ["lh", "rh"]
          )
      if src in ("mixed", "volume"):
          hemi_str.extend(["vol"])
  
      # label traces
      brain = _create_testing_brain(
          hemi=hemi,
          surf="white",
          src=src,
          show_traces="label",
          volume_options=None,  # for speed, don't upsample
          n_time=5,
          initial_time=0,
      )
      if src == "surface":
          brain._data["src"] = None  # test src=None
      if src in ("surface", "vector", "mixed"):
          assert brain.show_traces
          assert brain.traces_mode == "label"
          brain.widgets["extract_mode"].set_value("max")
  
          # test picking a cell at random
          rng = np.random.RandomState(0)
          for idx, current_hemi in enumerate(hemi_str):
              if current_hemi == "vol":
                  continue
              current_mesh = brain._layered_meshes[
                  current_hemi
              ]._polydata
              cell_id = rng.randint(0, current_mesh.n_cells)
              test_picker = TstVTKPicker(
                  current_mesh, cell_id, current_hemi, brain
              )
              assert len(brain.picked_patches[current_hemi]) == 0
              brain._on_pick(test_picker, None)
              assert len(brain.picked_patches[current_hemi]) == 1
              for label_id in list(brain.picked_patches[current_hemi]):
                  label = brain._annotation_labels[current_hemi][
                      label_id
                  ]
                  assert isinstance(label._line, Line2D)
              brain.widgets["extract_mode"].set_value("mean")
              brain.clear_glyphs()
              assert len(brain.picked_patches[current_hemi]) == 0
              brain._on_pick(test_picker, None)  # picked and added
              assert len(brain.picked_patches[current_hemi]) == 1
              brain._on_pick(
                  test_picker, None
              )  # picked again so removed
              assert len(brain.picked_patches[current_hemi]) == 0
          # test switching from 'label' to 'vertex'
          brain.widgets["annotation"].set_value("None")
          brain.widgets["extract_mode"].set_value("max")
      else:  # volume
          assert "annotation" not in brain.widgets
          assert "extract_mode" not in brain.widgets
      brain.close()
  
      # test colormap
      if src != "vector":
          brain = _create_testing_brain(
              hemi=hemi,
              surf="white",
              src=src,
              show_traces=0.5,
              initial_time=0,
              volume_options=None,  # for speed, don't upsample
              n_time=1 if src == "mixed" else 5,
              diverging=True,
              add_data_kwargs=dict(colorbar_kwargs=dict(n_labels=3)),
          )
          # mne_analyze should be chosen
          ctab = brain._data["ctable"]
          assert_array_equal(ctab[0], [0, 255, 255, 255])  # opaque cyan
          assert_array_equal(
              ctab[-1], [255, 255, 0, 255]
          )  # opaque yellow
          assert_allclose(
              ctab[len(ctab) // 2], [128, 128, 128, 0], atol=3
          )
          brain.close()
  
      # vertex traces
      brain = _create_testing_brain(
          hemi=hemi,
          surf="white",
          src=src,
          show_traces=0.5,
          initial_time=0,
          volume_options=None,  # for speed, don't upsample
          n_time=1 if src == "mixed" else 5,
          add_data_kwargs=dict(colorbar_kwargs=dict(n_labels=3)),
      )
      assert brain.show_traces
      assert brain.traces_mode == "vertex"
      assert hasattr(brain, "picked_points")
      assert hasattr(brain, "_spheres")
      assert brain._scalar_bar.GetNumberOfLabels() == 3
  
      # add foci should work for 'lh', 'rh' and 'vol'
      for current_hemi in hemi_str:
          brain.add_foci([[0, 0, 0]], hemi=current_hemi)
          assert_array_equal(
              brain._data[current_hemi]["foci"], [[0, 0, 0]]
          )
  
      # test points picked by default
      picked_points = brain.get_picked_points()
      spheres = brain._spheres
      for current_hemi in hemi_str:
          assert len(picked_points[current_hemi]) == 1
      n_spheres = len(hemi_str)
      n_actors = n_spheres
      if hemi == "split" and src in ("mixed", "volume"):
          n_spheres += 1
      assert len(spheres) == n_spheres
  
      # test that there are actually enough actors
      assert len(brain._actors["data"]) == n_actors
  
      # test switching from 'vertex' to 'label'
      if src == "surface":
          brain.widgets["annotation"].set_value("aparc")
          brain.widgets["annotation"].set_value("None")
      # test removing points
      brain.clear_glyphs()
      assert len(spheres) == 0
      for key in ("lh", "rh", "vol"):
          assert len(picked_points[key]) == 0
  
      # test picking a cell at random
      rng = np.random.RandomState(0)
      for idx, current_hemi in enumerate(hemi_str):
          assert len(spheres) == 0
          if current_hemi == "vol":
              current_mesh = brain._data["vol"]["grid"]
              vertices = brain._data["vol"]["vertices"]
              values = current_mesh.cell_data["values"][vertices]
              cell_id = vertices[np.argmax(np.abs(values))]
          else:
              current_mesh = brain._layered_meshes[
                  current_hemi
              ]._polydata
              cell_id = rng.randint(0, current_mesh.n_cells)
          test_picker = TstVTKPicker(None, None, current_hemi, brain)
          assert brain._on_pick(test_picker, None) is None
          test_picker = TstVTKPicker(
              current_mesh, cell_id, current_hemi, brain
          )
          assert cell_id == test_picker.cell_id
          assert test_picker.point_id is None
          brain._on_pick(test_picker, None)
          brain._on_pick(test_picker, None)
          assert test_picker.point_id is not None
          assert len(picked_points[current_hemi]) == 1
          assert picked_points[current_hemi][0] == test_picker.point_id
          assert len(spheres) > 0
          sphere = spheres[-1]
          vertex_id = sphere._vertex_id
          assert vertex_id == test_picker.point_id
          line = sphere._line
  
          hemi_prefix = current_hemi[0].upper()
          if current_hemi == "vol":
              assert hemi_prefix + ":" in line.get_label()
              assert "MNI" in line.get_label()
              continue  # the MNI conversion is more complex
          hemi_int = 0 if current_hemi == "lh" else 1
          mni = vertex_to_mni(
              vertices=vertex_id,
              hemis=hemi_int,
              subject=brain._subject,
              subjects_dir=brain._subjects_dir,
          )
          label = "{}:{} MNI: {}".format(
              hemi_prefix,
              str(vertex_id).ljust(6),
              ", ".join("%5.1f" % m for m in mni),
          )
  
          assert line.get_label() == label
  
          # remove the sphere by clicking in its vicinity
          old_len = len(spheres)
          test_picker._actors = sum((s._actors for s in spheres), [])
          brain._on_pick(test_picker, None)
          assert len(spheres) < old_len
  
      screenshot = brain.screenshot()
      screenshot_all = brain.screenshot(time_viewer=True)
      assert screenshot.shape[0] < screenshot_all.shape[0]
      # and the scraper for it (will close the instance)
      # only test one condition to save time
      if not (
          hemi == "rh"
          and src == "surface"
          and check_version("sphinx_gallery")
      ):
          brain.close()
          return
      fnames = [str(tmp_path / f"temp_{ii}.png") for ii in range(2)]
      block_vars = dict(
          image_path_iterator=iter(fnames),
          example_globals=dict(brain=brain),
      )
      block = (
          "code",
          """
  something
  # brain.save_movie(time_dilation=1, framerate=1,
  #                  interpolation='linear', time_viewer=True)
  #
  """,
          1,
      )
      gallery_conf = dict(
          src_dir=str(tmp_path),
          compress_images=[],
          image_srcset=[],
          matplotlib_animations=False,
      )
      scraper = _BrainScraper()
      rst = scraper(block, block_vars, gallery_conf)
      assert brain.plotter is None  # closed
      gif_0 = fnames[0][:-3] + "gif"
      for fname in (gif_0, fnames[1]):
          fname = Path(fname)
          assert fname.stem in rst
          assert fname.is_file()
          img = image.imread(fname)
          assert_allclose(
              img.shape[1], screenshot.shape[1], atol=1
          )  # width
          assert img.shape[0] > screenshot.shape[0]  # larger height
          assert_allclose(img.shape[1], screenshot_all.shape[1], atol=1)
          assert_allclose(img.shape[0], screenshot_all.shape[0], atol=1)
  
imports: import os
  import platform
  from pathlib import Path
  from shutil import copyfile
  import numpy
  import pytest
  from matplotlib import image
  from matplotlib.lines import Line2D
  from numpy.testing import assert_allclose, assert_array_equal
  from mne import Dipole, MixedSourceEstimate, SourceEstimate, VolSourceEstimate, create_info, pick_types_forward, read_cov, read_evokeds, read_forward_solution, read_source_estimate, vertex_to_mni, write_surface
  from mne.channels import make_dig_montage
  from mne.datasets import testing
  from mne.io import read_info
  from mne.label import read_label
  from mne.minimum_norm import apply_inverse, make_inverse_operator
  from mne.source_estimate import _BaseSourceEstimate
  from mne.source_space import read_source_spaces, setup_volume_source_space
  from mne.utils import check_version
  from mne.viz import ui_events
  from mne.viz._brain import Brain, _BrainScraper, _LayeredMesh, _LinkViewer
  from mne.viz._brain.colormap import calculate_lut
  from mne.viz.utils import _get_cmap
label_desc: Use image node_id 15 for calling a global function with example usage: test_brain_traces(renderer_interactive_pyvistaqt, hemi, src, tmp_path, brain_gc) without return types