code: def labels_filter(
      a: np.ndarray,
      num_open: int,
      num_clear: int,
      initial_label: int = 0,
      forward_fill: bool = False,
  ):
      if forward_fill:
          a = fill_forward(a)
  
      state = initial_label
      count_high = 0
      count_low = 0
      output = []
      for label in a:
          if label and not np.isnan(label):
              count_low = 0
              count_high += 1
          else:
              count_high = 0
              count_low += 1
          if state and count_low == num_clear:
              state = 0
          if not state and count_high == num_open:
              state = 1
          output.append(state)
      return np.array(output)
  
imports: from typing import Optional
  import numpy
label_desc: Use image node_id 2 for calling a global function with example usage: labels_filter(a, num_open, num_clear, initial_label, forward_fill) and returns: np