code: def write_seg_nrrd(
      image_np: np.ndarray,
      output_file: str,
      dtype: type,
      affine: np.ndarray,
      labels: List[str],
      color_map: Optional[Dict[str, List[float]]] = None,
      index_order: str = "C",
      space: str = "left-posterior-superior",
  ) -> None:
      """Write multi-channel seg.nrrd file.
  
      Args:
          image_np: Image as numpy ndarray
          output_file: Output file path that the seg.nrrd file should be saved to
          dtype: numpy type e.g. float32
          affine: Affine matrix
          labels: Labels of image segment which will be written to the nrrd header
          color_map: Mapping from segment_name(str) to it's color e.g. {'heart': [255/255, 244/255, 209/255]}
          index_order: Either 'C' or 'F' (see nrrd.write() documentation)
  
      Raises:
          ValueError: In case affine is not provided
          ValueError: In case labels are not provided
      """
      if isinstance(image_np, torch.Tensor):
          image_np = image_np.numpy()
      if isinstance(affine, torch.Tensor):
          affine = affine.numpy()
      image_np = image_np.transpose().copy()
      if dtype:
          image_np = image_np.astype(dtype)
  
      if not isinstance(labels, Iterable):
          raise ValueError("Labels have to be defined, e.g. as a list")
  
      header: Dict[str, Any] = {}
      for i, segment_name in enumerate(labels):
          header.update(
              {
                  f"Segment{i}_ID": segment_name,
                  f"Segment{i}_Name": segment_name,
              }
          )
          if color_map is not None:
              header[f"Segment{i}_Color"] = " ".join(
                  list(map(str, color_map[segment_name]))
              )
  
      if affine is None:
          raise ValueError("Affine matrix has to be defined")
  
      kinds = ["list", "domain", "domain", "domain"]
  
      convert_aff_mat = np.diag([-1, -1, 1, 1])
      affine = convert_aff_mat @ affine
  
      _origin_key = (slice(-1), -1)
      origin = affine[_origin_key]
  
      space_directions = np.array(
          [
              [np.nan, np.nan, np.nan],
              affine[0, :3],
              affine[1, :3],
              affine[2, :3],
          ]
      )
  
      header.update(
          {
              "kinds": kinds,
              "space directions": space_directions,
              "space origin": origin,
              "space": space,
          }
      )
      nrrd.write(
          output_file,
          image_np,
          header=header,
          index_order=index_order,
      )
  
imports: import logging
  import tempfile
  from typing import Any, Dict, Iterable, List, Optional, Tuple
  import itk
  import nrrd
  import numpy
  import torch
  from monai.data import MetaTensor
  from monai.data.image_writer import NibabelWriter
  from monailabel.utils.others.detection import create_slicer_detection_json
  from monailabel.utils.others.generic import file_ext
  from monailabel.utils.others.pathology import create_asap_annotations_xml, create_dsa_annotations_json
label_desc: Use image node_id 3 for calling a global function with example usage: write_seg_nrrd(image_np, output_file, dtype, affine, labels, color_map, index_order, space) without return types