🔍 Code Extractor

function get_ndmapping_label

Maturity: 41

Retrieves the first non-auxiliary object's label attribute from an NdMapping data structure by iterating through its values.

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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class ndmapping_groupby 54.6% similar

    A parameterized function class that performs groupby operations on NdMapping objects, automatically using pandas for improved performance when available, falling back to pure Python implementation otherwise.

    From: /tf/active/vicechatdev/patches/util.py
  • function get_unique_keys 50.1% 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_spec 49.2% similar

    Extracts a specification tuple from a labeled data object, consisting of the class name, group, and label attributes.

    From: /tf/active/vicechatdev/patches/util.py
  • function get_node_labels 47.8% similar

    Retrieves all non-private attributes from the NodeLabels class as a dictionary, filtering out attributes that start with underscore.

    From: /tf/active/vicechatdev/CDocs/db/schema_manager.py
  • function get_param_values 46.6% 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