function get_unique_keys
Extracts unique key values from an ndmapping object for specified dimensions, returning an iterator of unique tuples.
/tf/active/vicechatdev/patches/util.py
1795 - 1799
moderate
Purpose
This function is designed to extract and deduplicate key values from a multi-dimensional mapping structure. It retrieves the indices of specified dimensions from the ndmapping object, then uses those indices to extract corresponding values from all keys in the mapping's data dictionary. The result is an iterator of unique tuples containing only the values for the requested dimensions, useful for analyzing or filtering multi-dimensional data structures.
Source Code
def get_unique_keys(ndmapping, dimensions):
inds = [ndmapping.get_dimension_index(dim) for dim in dimensions]
getter = operator.itemgetter(*inds)
return unique_iterator(getter(key) if len(inds) > 1 else (key[inds[0]],)
for key in ndmapping.data.keys())
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
ndmapping |
- | - | positional_or_keyword |
dimensions |
- | - | positional_or_keyword |
Parameter Details
ndmapping: An object with a multi-dimensional mapping structure that must have methods 'get_dimension_index(dim)' to retrieve dimension indices and a 'data' attribute with a 'keys()' method. Typically a HoloViews-like data structure that organizes data by multiple dimensions.
dimensions: A list or iterable of dimension names/identifiers to extract from the ndmapping keys. These dimensions must exist in the ndmapping object, otherwise get_dimension_index will fail. Can be a single dimension or multiple dimensions.
Return Value
Returns an iterator (from unique_iterator function) that yields unique tuples. Each tuple contains the key values for the specified dimensions. If only one dimension is requested, each tuple will have one element. If multiple dimensions are requested, tuples will have multiple elements corresponding to each dimension's value. The iterator ensures no duplicate tuples are returned.
Dependencies
operator
Required Imports
import operator
Usage Example
# Assuming ndmapping is a HoloViews-like object with dimensions 'x', 'y', 'z'
# and unique_iterator is available in scope
import operator
# Example with mock ndmapping object
class MockNDMapping:
def __init__(self):
self.data = {
('a', 1, 'x'): 'value1',
('b', 2, 'y'): 'value2',
('a', 1, 'z'): 'value3',
('c', 3, 'x'): 'value4'
}
self.dimensions = ['dim1', 'dim2', 'dim3']
def get_dimension_index(self, dim):
return self.dimensions.index(dim)
# Mock unique_iterator for demonstration
def unique_iterator(iterable):
seen = set()
for item in iterable:
if item not in seen:
seen.add(item)
yield item
ndmap = MockNDMapping()
dimensions_to_extract = ['dim1', 'dim2']
# Get unique keys for specified dimensions
unique_keys = get_unique_keys(ndmap, dimensions_to_extract)
for key in unique_keys:
print(key) # Outputs: ('a', 1), ('b', 2), ('c', 3)
Best Practices
- Ensure the ndmapping object has the required 'get_dimension_index' method and 'data' attribute before calling this function
- The dimensions parameter should contain valid dimension names that exist in the ndmapping object
- The function returns an iterator, not a list, so consume it appropriately (e.g., convert to list if needed or iterate once)
- The unique_iterator function must be available in the scope where this function is called
- This function assumes keys in ndmapping.data are tuples or indexable sequences
- Performance depends on the size of ndmapping.data and the number of dimensions requested
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function unpack_group 53.1% similar
-
function unique_iterator 50.8% similar
-
function get_param_values 50.2% similar
-
function get_ndmapping_label 50.1% similar
-
function group_select 49.7% similar