🔍 Code Extractor

function drop_streams

Maturity: 43

Filters out dimensioned stream parameters from key dimensions (kdims) and their corresponding keys, returning cleaned dimensions and keys.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1783 - 1792
Complexity:
moderate

Purpose

This function is used in data visualization or streaming contexts to remove stream-related parameters from dimension definitions. It identifies which key dimensions are associated with stream parameters, removes them, and reconstructs the keys tuple/list accordingly. This is useful when processing dynamic data streams where certain dimensions need to be excluded based on stream configuration.

Source Code

def drop_streams(streams, kdims, keys):
    """
    Drop any dimensioned streams from the keys and kdims.
    """
    stream_params = stream_parameters(streams)
    inds, dims = zip(*[(ind, kdim) for ind, kdim in enumerate(kdims)
                       if kdim not in stream_params])
    get = operator.itemgetter(*inds) # itemgetter used for performance
    keys = (get(k) for k in keys)
    return dims, ([wrap_tuple(k) for k in keys] if len(inds) == 1 else list(keys))

Parameters

Name Type Default Kind
streams - - positional_or_keyword
kdims - - positional_or_keyword
keys - - positional_or_keyword

Parameter Details

streams: A collection of stream objects that contain parameters. These streams are analyzed to determine which key dimensions should be dropped. Expected to be an iterable of stream objects that can be processed by the stream_parameters() function.

kdims: Key dimensions - an iterable (likely list or tuple) of dimension names/objects that define the structure of the data. These are filtered to remove any that match stream parameters.

keys: An iterable of key tuples/values corresponding to the kdims. Each key should have the same length as kdims. These are filtered in parallel with kdims to maintain correspondence.

Return Value

Returns a tuple containing two elements: (1) dims - a tuple of filtered key dimensions with stream parameters removed, and (2) a list of filtered keys. If only one dimension remains (len(inds) == 1), each key is wrapped using wrap_tuple() to ensure proper structure; otherwise, returns the keys as a plain list. The keys maintain their order and correspondence with the returned dims.

Dependencies

  • operator

Required Imports

import operator

Conditional/Optional Imports

These imports are only needed under specific conditions:

from unknown_module import stream_parameters

Condition: Required helper function to extract parameters from stream objects - must be defined in the same module or imported

Required (conditional)
from unknown_module import wrap_tuple

Condition: Required helper function to wrap single values in tuples - must be defined in the same module or imported

Required (conditional)

Usage Example

# Note: This example assumes stream_parameters and wrap_tuple are defined
# Example helper functions (simplified):
def stream_parameters(streams):
    return {'stream_param1', 'stream_param2'}

def wrap_tuple(val):
    return (val,) if not isinstance(val, tuple) else val

# Usage:
streams = [...]  # Stream objects
kdims = ['x', 'y', 'stream_param1', 'z']
keys = [('a', 'b', 'c', 'd'), ('e', 'f', 'g', 'h')]

filtered_dims, filtered_keys = drop_streams(streams, kdims, keys)
# filtered_dims: ('x', 'y', 'z')
# filtered_keys: [('a', 'b', 'd'), ('e', 'f', 'h')]

Best Practices

  • Ensure that streams parameter contains valid stream objects that can be processed by stream_parameters()
  • The length of each key in keys should match the length of kdims before filtering
  • Be aware that the function uses operator.itemgetter for performance optimization when extracting filtered indices
  • The function handles the special case of single remaining dimension by wrapping keys with wrap_tuple()
  • This function is likely part of a larger visualization framework (possibly HoloViews based on context) and should be used within that ecosystem
  • The returned keys structure differs based on the number of remaining dimensions (wrapped tuples vs plain list)

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function unbound_dimensions 78.4% similar

    Filters a list of dimensions (kdims) to return only those that have not been associated with any of the provided streams.

    From: /tf/active/vicechatdev/patches/util.py
  • function dimensionless_contents 75.3% similar

    Filters stream parameters to return only those that are not associated with any of the provided key dimensions.

    From: /tf/active/vicechatdev/patches/util.py
  • function dimensioned_streams 75.1% similar

    Filters and returns streams from a DynamicMap that have parameters matching the DynamicMap's key dimensions.

    From: /tf/active/vicechatdev/patches/util.py
  • function wrap_tuple_streams 64.2% similar

    Fills in None values in a tuple with corresponding dimensioned stream values based on matching key dimension names.

    From: /tf/active/vicechatdev/patches/util.py
  • function stream_parameters 54.8% similar

    Extracts and flattens parameter names from a list of stream objects, with optional duplicate detection and parameter exclusion.

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