code: def scan_can_remove_outs(op, out_idxs):
      """Look at all outputs defined by indices ``out_idxs`` and determines which can be removed.
  
      Returns
      -------
      two lists, the first one with the indices of outs that can be removed, the
      second with the outputs that can not be removed.
  
      """
      non_removable = [
          o for i, o in enumerate(op.inner_outputs) if i not in out_idxs
      ]
      required_inputs = list(graph_inputs(non_removable))
  
      out_ins = []
      offset = op.info.n_seqs
      for idx, tap in enumerate(
          chain(
              op.info.mit_mot_in_slices,
              op.info.mit_sot_in_slices,
              op.info.sit_sot_in_slices,
          )
      ):
          n_ins = len(tap)
          out_ins += [op.inner_inputs[offset : offset + n_ins]]
          offset += n_ins
      out_ins += [[] for k in range(op.info.n_nit_sot)]
      out_ins += [
          [op.inner_inputs[offset + k]]
          for k in range(op.info.n_shared_outs)
      ]
  
      added = True
      out_idxs_mask = [1 for idx in out_idxs]
      while added:
          added = False
          for pos, idx in enumerate(out_idxs):
              if out_idxs_mask[pos] and any(
                  x in required_inputs for x in out_ins[idx]
              ):
                  # This output is required ..
                  out_idxs_mask[pos] = 0
                  required_inputs += list(
                      graph_inputs([op.inner_outputs[idx]])
                  )
                  added = True
  
      required_outs = [
          x for i, x in enumerate(out_idxs) if out_idxs_mask[i] == 0
      ]
      not_required = [
          x for i, x in enumerate(out_idxs) if out_idxs_mask[i] == 1
      ]
      return (required_outs, not_required)
  
imports: import copy
  import dataclasses
  import logging
  from collections import OrderedDict, namedtuple
  from collections.abc import Sequence
  from itertools import chain
  from typing import TYPE_CHECKING, Callable, Optional, Union
  from typing import cast
  import numpy
  from pytensor import scalar
  from pytensor import tensor
  from pytensor.compile.profiling import ProfileStats
  from pytensor.configdefaults import config
  from pytensor.graph.basic import Constant, Variable, equal_computations, graph_inputs
  from pytensor.graph.op import get_test_value
  from pytensor.graph.replace import clone_replace
  from pytensor.graph.type import HasDataType
  from pytensor.graph.utils import TestValueError
  from pytensor.tensor.basic import AllocEmpty, cast
  from pytensor.tensor.subtensor import set_subtensor
  from pytensor.tensor.variable import TensorConstant
label_desc: Use image node_id 4 for calling a global function with example usage: scan_can_remove_outs(op, out_idxs) and returns: required_outs,  not_required