code: def _make_filter_stages(
      view,
      filters,
  ):
      stages = []
      queries = []
      for path, label_path, field, args in _iter_paths(view, filters):
          is_matching = args.get("isMatching", True)
          path_field = view.get_field(path)
          is_label_field = _is_label(field)
          if (
              is_label_field
              and issubclass(
                  field.document_type, (fol.Keypoint, fol.Keypoints)
              )
              and isinstance(
                  path_field, (fof.KeypointsField, fof.ListField)
              )
          ):
              continue
  
          if args.get("exclude") and not is_matching:
              continue
  
          queries.append(_make_query(path, path_field, args))
  
      if queries:
          stages.append(fosg.Match({"$and": queries}))
  
      for path, label_path, label_field, args in _iter_paths(
          view, filters, labels=True
      ):
          is_matching = args.get("isMatching", True)
          field = view.get_field(path)
          if issubclass(
              label_field.document_type, (fol.Keypoint, fol.Keypoints)
          ) and isinstance(field, fof.ListField):
              expr = _make_keypoint_list_filter(args, view, path, field)
              if expr is not None:
                  stages.append(
                      fosg.FilterKeypoints(
                          label_path,
                          only_matches=True,
                          **expr,
                      )
                  )
  
          elif not is_matching:
              key = field.db_field if field.db_field else field.name
              expr = _make_scalar_expression(
                  F(key), args, field, is_label=True
              )
              if expr is not None:
                  stages.append(
                      fosg.FilterLabels(
                          label_path,
                          expr,
                          only_matches=not args.get("exclude", False),
                      )
                  )
  
      return stages
  
imports: from typing import List, Optional
  from bson import ObjectId
  import strawberry
  import fiftyone.core.collections
  import fiftyone.core.dataset
  from fiftyone.core.expressions import ViewField, VALUE
  import fiftyone.core.fields
  import fiftyone.core.labels
  import fiftyone.core.media
  import fiftyone.core.stages
  import fiftyone.core.utils
  import fiftyone.core.view
  from fiftyone.server.aggregations import GroupElementFilter, SampleFilter
  from fiftyone.server.scalars import BSONArray, JSON
label_desc: Use image node_id 6 for calling a global function with example usage: _make_filter_stages(view, filters) and returns: stages