code: def _annotations_from_mask(times, mask, annot_name, orig_time=None):
      """Construct annotations from boolean mask of the data."""
      mask_tf = distance_transform_edt(mask)
      # Overcome the shortcoming of find_peaks
      # in finding a marginal peak, by
      # inserting 0s at the front and the
      # rear, then subtracting in index
      ins_mask_tf = np.concatenate((np.zeros(1), mask_tf, np.zeros(1)))
      left_midpt_index = find_peaks(ins_mask_tf)[0] - 1
      right_midpt_index = (
          np.flip(
              len(ins_mask_tf) - 1 - find_peaks(ins_mask_tf[::-1])[0]
          )
          - 1
      )
      onsets_index = (
          left_midpt_index - mask_tf[left_midpt_index].astype(int) + 1
      )
      ends_index = right_midpt_index + mask_tf[
          right_midpt_index
      ].astype(int)
      # Ensure onsets_index >= 0,
      # otherwise the duration starts from the beginning
      onsets_index[onsets_index < 0] = 0
      # Ensure ends_index < len(times),
      # otherwise the duration is to the end of times
      if len(times) == len(mask):
          ends_index[ends_index >= len(times)] = len(times) - 1
      # To be consistent with the original code,
      # possibly a bug in tests code
      else:
          ends_index[ends_index >= len(mask)] = len(mask)
      onsets = times[onsets_index]
      ends = times[ends_index]
      durations = ends - onsets
      desc = [annot_name] * len(durations)
      return Annotations(onsets, durations, desc, orig_time=orig_time)
  
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 4 for calling a global function with example usage: _annotations_from_mask(times, mask, annot_name, orig_time) and returns: Annotations