function layer_groups
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.
/tf/active/vicechatdev/patches/util.py
1421 - 1430
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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function group_select 55.3% similar
-
function layer_sort 53.1% similar
-
function match_spec 49.5% similar
-
function unpack_group 47.8% similar
-
function get_overlay_spec 47.5% similar