code: def compute_average_dev_head_t(raw, pos):
      """Get new device to head transform based on good segments.
  
      Segments starting with "BAD" annotations are not included for calculating
      the mean head position.
  
      Parameters
      ----------
      raw : instance of Raw
          Data to compute head position.
      pos : array, shape (N, 10)
          The position and quaternion parameters from cHPI fitting.
  
      Returns
      -------
      dev_head_t : instance of Transform
          New ``dev_head_t`` transformation using the averaged good head positions.
      """
      sfreq = raw.info["sfreq"]
      seg_good = np.ones(len(raw.times))
      trans_pos = np.zeros(3)
      hp = pos.copy()
      hp_ts = hp[:, 0] - raw._first_time
  
      # Check rounding issues at 0 time
      if hp_ts[0] < 0:
          hp_ts[0] = 0
          assert hp_ts[1] > 1.0 / sfreq
  
      # Mask out segments if beyond scan time
      mask = hp_ts <= raw.times[-1]
      if not mask.all():
          logger.info(
              "          Removing %d samples > raw.times[-1] (%s)"
              % (np.sum(~mask), raw.times[-1])
          )
          hp = hp[mask]
      del mask, hp_ts
  
      # Get time indices
      ts = np.concatenate((hp[:, 0], [(raw.last_samp + 1) / sfreq]))
      assert (np.diff(ts) > 0).all()
      ts -= raw.first_samp / sfreq
      idx = raw.time_as_index(ts, use_rounding=True)
      del ts
      if idx[0] == -1:  # annoying rounding errors
          idx[0] = 0
          assert idx[1] > 0
      assert (idx >= 0).all()
      assert idx[-1] == len(seg_good)
      assert (np.diff(idx) > 0).all()
  
      # Mark times bad that are bad according to annotations
      onsets, ends = _annotations_starts_stops(raw, "bad")
      for onset, end in zip(onsets, ends):
          seg_good[onset:end] = 0
      dt = np.diff(np.cumsum(np.concatenate([[0], seg_good]))[idx])
      assert (dt >= 0).all()
      dt = dt / sfreq
      del seg_good, idx
  
      # Get weighted head pos trans and rot
      trans_pos += np.dot(dt, hp[:, 4:7])
  
      rot_qs = hp[:, 1:4]
      best_q = _average_quats(rot_qs, weights=dt)
  
      trans = np.eye(4)
      trans[:3, :3] = quat_to_rot(best_q)
      trans[:3, 3] = trans_pos / dt.sum()
      assert (
          np.linalg.norm(trans[:3, 3]) < 1
      )  # less than 1 meter is sane
      dev_head_t = Transform("meg", "head", trans)
      return dev_head_t
  
file_path: mne-python/mne/preprocessing/artifact_detection.py
imports: import numpy
  from scipy.ndimage import distance_transform_edt, label
  from scipy.signal import find_peaks
  from scipy.stats import zscore
  from ..annotations import Annotations, _adjust_onset_meas_date, _annotations_starts_stops, annotations_from_events
  from ..filter import filter_data
  from ..io.base import BaseRaw
  from ..transforms import Transform, _angle_between_quats, _average_quats, _quat_to_affine, apply_trans, quat_to_rot
  from ..utils import _mask_to_onsets_offsets, _pl, _validate_type, logger, verbose
label_desc: Use image node_id 3 for calling a global function with example usage: compute_average_dev_head_t(raw, pos) and returns: dev_head_t