🔍 Code Extractor

function layer_groups

Maturity: 45

Groups elements from an ordering list into a dictionary based on a slice of each element's specification, using the first 'length' items as the grouping key.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1421 - 1430
Complexity:
simple

Purpose

This function is designed to organize Layer objects (or similar structured elements) into logical groups by extracting a common prefix from their specification. It's useful for hierarchical organization of layers where elements sharing the same prefix in their spec should be grouped together. The grouping granularity can be adjusted via the 'length' parameter to control how many elements of the spec are used for grouping.

Source Code

def layer_groups(ordering, length=2):
   """
   Splits a global ordering of Layers into groups based on a slice of
   the spec.  The grouping behavior can be modified by changing the
   length of spec the entries are grouped by.
   """
   group_orderings = defaultdict(list)
   for el in ordering:
      group_orderings[el[:length]].append(el)
   return group_orderings

Parameters

Name Type Default Kind
ordering - - positional_or_keyword
length - 2 positional_or_keyword

Parameter Details

ordering: An iterable collection of elements (typically Layer objects or tuples/lists) that support slicing. Each element should be subscriptable (support [:length] syntax) to extract a grouping key. Expected to be a list or similar sequence of structured objects with consistent indexable properties.

length: An integer specifying how many elements from the beginning of each entry's spec to use as the grouping key. Default is 2. Higher values create more specific groups (finer granularity), while lower values create broader groups. Must be a positive integer less than or equal to the length of the shortest element in ordering.

Return Value

Returns a defaultdict(list) where keys are tuples representing the first 'length' elements of each unique spec, and values are lists containing all elements from the original ordering that share that key. The dictionary allows grouping of related layers/elements based on their spec prefix.

Dependencies

  • collections

Required Imports

from collections import defaultdict

Usage Example

from collections import defaultdict

def layer_groups(ordering, length=2):
    group_orderings = defaultdict(list)
    for el in ordering:
        group_orderings[el[:length]].append(el)
    return group_orderings

# Example with layer specifications as tuples
layers = [
    ('plot', 'scatter', 'layer1'),
    ('plot', 'scatter', 'layer2'),
    ('plot', 'line', 'layer3'),
    ('overlay', 'curve', 'layer4'),
    ('overlay', 'curve', 'layer5')
]

# Group by first 2 elements (default)
groups = layer_groups(layers)
for key, items in groups.items():
    print(f"{key}: {items}")
# Output:
# ('plot', 'scatter'): [('plot', 'scatter', 'layer1'), ('plot', 'scatter', 'layer2')]
# ('plot', 'line'): [('plot', 'line', 'layer3')]
# ('overlay', 'curve'): [('overlay', 'curve', 'layer4'), ('overlay', 'curve', 'layer5')]

# Group by first element only
groups_broad = layer_groups(layers, length=1)
for key, items in groups_broad.items():
    print(f"{key}: {len(items)} items")
# Output:
# ('plot',): 3 items
# ('overlay',): 2 items

Best Practices

  • Ensure all elements in the 'ordering' parameter are subscriptable and support slicing operations
  • The 'length' parameter should not exceed the minimum length of elements in the ordering to avoid IndexError
  • Elements in ordering should have consistent structure (same type and indexable properties) for predictable grouping
  • The returned defaultdict will create empty lists for new keys, which is useful for incremental population
  • Consider the appropriate 'length' value based on your layer specification hierarchy - too small creates overly broad groups, too large creates too many small groups
  • The function assumes elements are hashable up to the slice point (el[:length]) since they're used as dictionary keys

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function group_select 55.3% 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 layer_sort 53.1% similar

    Computes a global topological ordering of layers from a HoloMap containing CompositeOverlay objects by analyzing layer dependencies and sorting them.

    From: /tf/active/vicechatdev/patches/util.py
  • function match_spec 49.5% 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 unpack_group 47.8% similar

    Unpacks a pandas DataFrame group by iterating over rows and yielding tuples of keys and objects, with special handling for objects with 'kdims' attribute.

    From: /tf/active/vicechatdev/patches/util.py
  • function get_overlay_spec 47.5% similar

    Constructs a specification tuple for an Element within an Overlay by combining the element's type name, group, label, and key dimensions.

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