def refine(
      self, selector_fn: "Callable[[Any], Selection[OtherSubtree]]"
  ) -> "Selection[OtherSubtree]":
    """Refines a selection by selecting within each selected subtree.

    Although selectors can already be refined by making additional calls,
    chained calls generally treat all selected subtrees the same way. In
    contrast, this method allows each selected node to be processed
    independently. Additionally, similar to `apply`, the additional logic is
    free to modify the subtree as it goes.

    Args:
      selector_fn: A function that takes a selected subtree from this selection
        and returns a new selection object, usually a selection of some nodes in
        the input subtree.

    Returns:
      A new selection that selects every node selected by ``selector_fn``, but
      in the context of the original tree rather than the individual selected
      subtrees.
    """
    return self.apply(
        selector_fn, keep_selected=True
    ).flatten_selected_selections()
Refines a selection by selecting within each selected subtree.

    Although selectors can already be refined by making additional calls,
    chained calls generally treat all selected subtrees the same way. In
    contrast, this method allows each selected node to be processed
    independently. Additionally, similar to `apply`, the additional logic is
    free to modify the subtree as it goes.

    Args:
      selector_fn: A function that takes a selected subtree from this selection
        and returns a new selection object, usually a selection of some nodes in
        the input subtree.

    Returns:
      A new selection that selects every node selected by ``selector_fn``, but
      in the context of the original tree rather than the individual selected
      subtrees.
    
penzai/core/selectors.py