function get_ndmapping_label
Retrieves the first non-auxiliary object's label attribute from an NdMapping data structure by iterating through its values.
/tf/active/vicechatdev/patches/util.py
1637 - 1655
simple
Purpose
This function is designed to extract label attributes (like 'group' or 'label') from NdMapping objects in the HoloViews library. It specifically filters out auxiliary components and returns the attribute value from the first valid element. When the attribute is 'group' and matches the element's type name, it returns None to avoid redundancy.
Source Code
def get_ndmapping_label(ndmapping, attr):
"""
Function to get the first non-auxiliary object
label attribute from an NdMapping.
"""
label = None
els = iter(ndmapping.data.values())
while label is None:
try:
el = next(els)
except StopIteration:
return None
if not getattr(el, '_auxiliary_component', True):
label = getattr(el, attr)
if attr == 'group':
tp = type(el).__name__
if tp == label:
return None
return label
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
ndmapping |
- | - | positional_or_keyword |
attr |
- | - | positional_or_keyword |
Parameter Details
ndmapping: An NdMapping object (HoloViews data structure) that contains a 'data' attribute with a dictionary-like structure of values. The function iterates through these values to find non-auxiliary components.
attr: A string representing the attribute name to retrieve from the non-auxiliary element (e.g., 'group', 'label'). This attribute should exist on the elements within the NdMapping.
Return Value
Returns the value of the specified attribute from the first non-auxiliary element found in the NdMapping. Returns None if: (1) no elements exist in the NdMapping, (2) all elements are auxiliary components, or (3) the attribute is 'group' and its value matches the element's type name. The return type depends on the attribute being retrieved but is typically a string or None.
Dependencies
param
Required Imports
No direct imports required for this standalone function
Usage Example
# Assuming you have a HoloViews NdMapping object
# from holoviews import NdMapping, Curve
# import numpy as np
# Create sample NdMapping with curves
# ndmap = NdMapping({i: Curve(np.random.rand(10)) for i in range(3)})
# Get the group label from the mapping
# group_label = get_ndmapping_label(ndmap, 'group')
# print(f"Group label: {group_label}")
# Get another attribute
# label = get_ndmapping_label(ndmap, 'label')
# print(f"Label: {label}")
Best Practices
- Ensure the NdMapping object has a 'data' attribute with dictionary-like values
- Elements in the NdMapping should have the '_auxiliary_component' attribute to properly filter
- The requested attribute should exist on the elements to avoid AttributeError
- Handle the None return value appropriately in calling code
- Be aware that when attr='group' and the value matches the element's type name, None is returned by design
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class ndmapping_groupby 54.6% similar
-
function get_unique_keys 50.1% similar
-
function get_spec 49.2% similar
-
function get_node_labels 47.8% similar
-
function get_param_values 46.6% similar