code: def test_combine_source_spaces(tmp_path):
      """Test combining source spaces."""
      nib = pytest.importorskip("nibabel")
      rng = np.random.RandomState(2)
      volume_labels = [
          "Brain-Stem",
          "Right-Hippocampus",
      ]  # two fairly large
  
      # create a sparse surface source space to ensure all get mapped
      # when mri_resolution=False
      srf = setup_source_space(
          "sample", "oct3", add_dist=False, subjects_dir=subjects_dir
      )
  
      # setup 2 volume source spaces
      vol = setup_volume_source_space(
          "sample",
          subjects_dir=subjects_dir,
          volume_label=volume_labels[0],
          mri=aseg_fname,
          add_interpolator=False,
      )
  
      # setup a discrete source space
      rr = rng.randint(0, 11, (20, 3)) * 5e-3
      nn = np.zeros(rr.shape)
      nn[:, -1] = 1
      pos = {"rr": rr, "nn": nn}
      disc = setup_volume_source_space(
          "sample", subjects_dir=subjects_dir, pos=pos, verbose="error"
      )
  
      # combine source spaces
      assert srf.kind == "surface"
      assert vol.kind == "volume"
      assert disc.kind == "discrete"
      src = srf + vol + disc
      assert src.kind == "mixed"
      assert srf.kind == "surface"
      assert vol.kind == "volume"
      assert disc.kind == "discrete"
  
      # test addition of source spaces
      assert len(src) == 4
  
      # test reading and writing
      src_out_name = tmp_path / "temp-src.fif"
      src.save(src_out_name)
      src_from_file = read_source_spaces(src_out_name)
      _compare_source_spaces(src, src_from_file, mode="approx")
      assert (
          repr(src).split("~")[0] == repr(src_from_file).split("~")[0]
      )
      assert_equal(src.kind, "mixed")
  
      # test that all source spaces are in MRI coordinates
      coord_frames = np.array([s["coord_frame"] for s in src])
      assert (coord_frames == FIFF.FIFFV_COORD_MRI).all()
  
      # test errors for export_volume
      image_fname = tmp_path / "temp-image.mgz"
  
      # source spaces with no volume
      with pytest.raises(ValueError, match="at least one volume"):
          srf.export_volume(image_fname, verbose="error")
  
      # unrecognized source type
      disc2 = disc.copy()
      disc2[0]["type"] = "kitty"
      with pytest.raises(ValueError, match="Invalid value"):
          src + disc2
      del disc2
  
      # unrecognized file type
      bad_image_fname = tmp_path / "temp-image.png"
      # vertices outside vol space warning
      pytest.raises(
          ValueError,
          src.export_volume,
          bad_image_fname,
          verbose="error",
      )
  
      # mixed coordinate frames
      disc3 = disc.copy()
      disc3[0]["coord_frame"] = 10
      src_mixed_coord = src + disc3
      with pytest.raises(
          ValueError, match="must be in head coordinates"
      ):
          src_mixed_coord.export_volume(image_fname, verbose="error")
  
      # now actually write it
      fname_img = tmp_path / "img.nii"
      for mri_resolution in (False, "sparse", True):
          for src, up in (
              (vol, 705),
              (srf + vol, 27272),
              (disc + vol, 705),
          ):
              src.export_volume(
                  fname_img,
                  use_lut=False,
                  mri_resolution=mri_resolution,
                  overwrite=True,
              )
              img_data = _get_img_fdata(nib.load(str(fname_img)))
              n_src = img_data.astype(bool).sum()
              n_want = sum(s["nuse"] for s in src)
              if mri_resolution is True:
                  n_want += up
              assert n_src == n_want, src
  
      # gh-8004
      temp_aseg = tmp_path / "aseg.mgz"
      aseg_img = nib.load(aseg_fname)
      aseg_affine = aseg_img.affine
      aseg_affine[:3, :3] *= 0.7
      new_aseg = nib.MGHImage(aseg_img.dataobj, aseg_affine)
      nib.save(new_aseg, str(temp_aseg))
      lh_cereb = mne.setup_volume_source_space(
          "sample",
          mri=temp_aseg,
          volume_label="Left-Cerebellum-Cortex",
          add_interpolator=False,
          subjects_dir=subjects_dir,
      )
      src = srf + lh_cereb
      with pytest.warns(
          RuntimeWarning, match="2 surf vertices lay outside"
      ):
          src.export_volume(
              image_fname, mri_resolution="sparse", overwrite=True
          )
  
imports: from pathlib import Path
  from shutil import copytree
  import numpy
  import pytest
  from numpy.testing import assert_allclose, assert_array_equal, assert_array_less, assert_equal
  import mne
  from mne import SourceEstimate, add_source_space_distances, compute_source_morph, get_volume_labels_from_src, make_sphere_model, morph_source_spaces, pick_types, read_bem_solution, read_bem_surfaces, read_freesurfer_lut, read_source_spaces, read_trans, setup_source_space, setup_volume_source_space, write_source_spaces
  from mne._fiff.constants import FIFF
  from mne._fiff.pick import _picks_to_idx
  from mne.datasets import testing
  from mne.fixes import _get_img_fdata
  from mne.source_estimate import _get_src_type
  from mne.source_space import compute_distance_to_sensors, get_decimated_surfaces
  from mne.source_space._source_space import _compare_source_spaces
  from mne.surface import _accumulate_normals, _triangle_neighbors
  from mne.utils import _record_warnings, requires_mne, run_subprocess
label_desc: Use image node_id 19 for calling a global function with example usage: test_combine_source_spaces(tmp_path) without return types