code: def replace_outermost(
      ntuple,
      return_unused=False,
      nested_lookup_fn=get_nested_objs,
      **kw,
  ):
      """Replace (nested) fields in a `namedtuple`.
  
      For each attribute-value update specified, this function only replaces the
      first matching attribute found. Nested objects are traversed breadth-first,
      with level-wise order determined by the list ordering returned from
      `nested_lookup_fn`.
  
      Args:
        ntuple: A `namedtuple` to replace (nested) fields in.
        return_unused: If `True`, return the `dict` of attribute-value pairs in
         `**kw` that were not found and updated in `ntuple`.
        nested_lookup_fn: A single-argument callable that returns a list of
          (attribute name, nested object) pairs. Defaults to `get_nested_objs`.
        **kw: The attribute-value pairs to update.
  
      Returns:
        updated: A copy of `ntuple` with (nested) fields updated.
        unused: If `return_unused` is `True`, the dictionary of attribute-value
          pairs in `**kw` that were not found and updated in `ntuple`.
  
      Raises:
        ValueError: if `returne_unused=False` and attributes in `**kw` are not found
          in `ntuple`.
      """
      root = ntuple
      root_update = {k: kw[k] for k in root._fields if k in kw}
      kw = {k: v for k, v in kw.items() if k not in root_update}
      # Collect the updates to apply later by traversing breadth-first, but with
      # backlinks to parent updates.
      to_visit = collections.deque(
          [
              (root_update, field, child)
              for field, child in nested_lookup_fn(root)
          ]
      )
      inner_updates = []
      while to_visit and kw:
          parent_update, field, child = to_visit.popleft()
          child_update = {k: kw[k] for k in child._fields if k in kw}
          if child_update:
              kw = {
                  k: v for k, v in kw.items() if k not in child_update
              }
              inner_updates.append(
                  (parent_update, field, child, child_update)
              )
          to_visit.extend(
              [
                  (child_update, child_field, child_child)
                  for child_field, child_child in nested_lookup_fn(
                      child
                  )
              ]
          )
      # Now apply updates in reverse order, propogating up to root.
      for parent_update, field, child, child_update in reversed(
          inner_updates
      ):
          parent_update[field] = child._replace(**child_update)
      root = root._replace(**root_update)
      if not return_unused:
          if kw:
              raise ValueError(
                  "Got unexpected (nested) field names: {}".format(
                      list(kw)
                  )
              )
          return root
      return root, kw
  
file_path: probability/tensorflow_probability/python/internal/unnest.py
imports: import collections
label_desc: Use image node_id 6 for calling a global function with example usage: replace_outermost(ntuple, return_unused, nested_lookup_fn) and returns: root,  kw, root