function iterative_select
Recursively selects subgroups from a hierarchical object structure by iterating through dimensions and applying select operations, avoiding duplication of selections.
/tf/active/vicechatdev/patches/util.py
1451 - 1466
moderate
Purpose
This function is designed to work with hierarchical data structures (likely HoloViews or similar visualization objects) where data can be selected along multiple dimensions. It takes the output of a group_select operation and iteratively applies select operations across specified dimensions, building a list of tuples containing selection paths and the corresponding selected objects. The function optimizes performance by avoiding redundant select operations through recursive traversal.
Source Code
def iterative_select(obj, dimensions, selects, depth=None):
"""
Takes the output of group_select selecting subgroups iteratively,
avoiding duplicating select operations.
"""
ndims = len(dimensions)
depth = depth if depth is not None else ndims
items = []
if isinstance(selects, dict):
for k, v in selects.items():
items += iterative_select(obj.select(**{dimensions[ndims-depth]: k}),
dimensions, v, depth-1)
else:
for s in selects:
items.append((s, obj.select(**{dimensions[-1]: s[-1]})))
return items
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
obj |
- | - | positional_or_keyword |
dimensions |
- | - | positional_or_keyword |
selects |
- | - | positional_or_keyword |
depth |
- | None | positional_or_keyword |
Parameter Details
obj: The object to perform selections on. Expected to have a .select() method that accepts keyword arguments for dimension-based selection. Typically a HoloViews object or similar hierarchical data structure.
dimensions: A list or sequence of dimension names (strings) that define the hierarchy of selections. The order matters as selections are applied from outer to inner dimensions.
selects: Either a dictionary mapping dimension values to nested select structures (for recursive traversal), or a list of selection tuples (for leaf nodes). The structure determines whether to recurse deeper or collect results.
depth: Optional integer specifying the current recursion depth. If None, defaults to the length of dimensions (len(dimensions)). Used internally to track position in the dimension hierarchy during recursion. Decrements with each recursive call.
Return Value
Returns a list of tuples where each tuple contains: (1) a selection path (the 's' value from the selects list), and (2) the selected object resulting from applying the select operation. The list aggregates all leaf-level selections across the entire hierarchy.
Usage Example
# Assuming obj is a HoloViews object with dimensions 'category' and 'subcategory'
# and group_select has produced a nested selection structure
selects_dict = {
'A': {
'X': [('A', 'X'), ('A', 'Y')],
'Y': [('A', 'Y')]
},
'B': [('B', 'Z')]
}
dimensions = ['category', 'subcategory']
# Perform iterative selection
results = iterative_select(obj, dimensions, selects_dict)
# results will be a list of tuples like:
# [((('A', 'X'),), selected_obj1), ((('A', 'Y'),), selected_obj2), ...]
# Access individual results
for selection_path, selected_object in results:
print(f"Selection: {selection_path}")
# Do something with selected_object
Best Practices
- Ensure the 'obj' parameter has a .select() method that accepts keyword arguments matching the dimension names
- The 'dimensions' list order should match the hierarchy of the 'selects' structure (outer to inner)
- The 'selects' parameter structure must be consistent: dictionaries for intermediate levels, lists for leaf levels
- When calling manually, typically leave 'depth' as None to auto-calculate from dimensions length
- This function is designed to work with the output of a 'group_select' function, so ensure compatibility
- The function modifies the items list in place during recursion, which is efficient but means the return value accumulates across all recursive calls
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function group_select 63.2% similar
-
function closest_match 51.6% similar
-
function match_spec 48.6% similar
-
function get_unique_keys 47.3% similar
-
function get_param_values 46.0% similar