🔍 Code Extractor

function iterative_select

Maturity: 44

Recursively selects subgroups from a hierarchical object structure by iterating through dimensions and applying select operations, avoiding duplication of selections.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1451 - 1466
Complexity:
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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function group_select 63.2% similar

    Recursively groups a list of key tuples into a nested dictionary structure to optimize indexing operations by avoiding duplicate key lookups.

    From: /tf/active/vicechatdev/patches/util.py
  • function closest_match 51.6% similar

    Recursively finds the closest matching specification from a list of specs by comparing hierarchical keys (type, group, label, overlay) with a target match pattern.

    From: /tf/active/vicechatdev/patches/util.py
  • function match_spec 48.6% similar

    Matches an element's type, group, and label against a hierarchical specification dictionary, returning the most specific matching value.

    From: /tf/active/vicechatdev/patches/util.py
  • function get_unique_keys 47.3% similar

    Extracts unique key values from an ndmapping object for specified dimensions, returning an iterator of unique tuples.

    From: /tf/active/vicechatdev/patches/util.py
  • function get_param_values 46.0% similar

    Extracts parameter values from a data object, including key dimensions (kdims), value dimensions (vdims), label, and optionally the group if it differs from the default.

    From: /tf/active/vicechatdev/patches/util.py
← Back to Browse