🔍 Code Extractor

function layer_sort

Maturity: 41

Computes a global topological ordering of layers from a HoloMap containing CompositeOverlay objects by analyzing layer dependencies and sorting them.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1406 - 1418
Complexity:
moderate

Purpose

This function is used in visualization contexts (likely HoloViews/HoloMap) to determine the correct rendering order of overlaid layers. It extracts layer specifications from each overlay in the HoloMap, builds a dependency graph, performs topological sorting to resolve layer ordering constraints, and returns a flattened list representing the global layer order. This ensures that layers are rendered in a consistent and correct order across all frames of the HoloMap.

Source Code

def layer_sort(hmap):
   """
   Find a global ordering for layers in a HoloMap of CompositeOverlay
   types.
   """
   orderings = {}
   for o in hmap:
      okeys = [get_overlay_spec(o, k, v) for k, v in o.data.items()]
      if len(okeys) == 1 and not okeys[0] in orderings:
         orderings[okeys[0]] = []
      else:
         orderings.update({k: [] if k == v else [v] for k, v in zip(okeys[1:], okeys)})
   return [i for g in sort_topologically(orderings) for i in sorted(g)]

Parameters

Name Type Default Kind
hmap - - positional_or_keyword

Parameter Details

hmap: A HoloMap object containing CompositeOverlay types. Each element in the HoloMap should have a 'data' attribute that is a dictionary-like structure with key-value pairs representing layers. The function iterates over this HoloMap to extract layer specifications and build ordering relationships.

Return Value

Returns a list of layer specifications in topologically sorted order. The list is flattened from groups of layers that can be rendered in any order relative to each other (within each group, layers are alphabetically sorted). This ordering ensures that layer dependencies are respected globally across all overlays in the HoloMap.

Dependencies

  • holoviews

Required Imports

from holoviews.core.util import get_overlay_spec
from holoviews.core.util import sort_topologically

Usage Example

# Assuming HoloViews is properly set up and you have a HoloMap with CompositeOverlays
import holoviews as hv
from holoviews.core.util import layer_sort, get_overlay_spec, sort_topologically

# Create sample overlays
curve1 = hv.Curve([1, 2, 3])
curve2 = hv.Curve([2, 3, 4])
overlay1 = curve1 * curve2

curve3 = hv.Curve([3, 4, 5])
overlay2 = curve1 * curve3

# Create HoloMap with overlays
hmap = hv.HoloMap({0: overlay1, 1: overlay2})

# Get global layer ordering
ordered_layers = layer_sort(hmap)
print(ordered_layers)

Best Practices

  • Ensure the input hmap is a valid HoloMap object with CompositeOverlay elements that have a 'data' attribute
  • The function assumes get_overlay_spec and sort_topologically are available in the same module
  • This function is typically used internally by HoloViews for rendering purposes rather than being called directly by end users
  • The ordering is deterministic within groups (alphabetically sorted) but groups themselves are topologically ordered
  • Empty overlays or single-layer overlays are handled specially to avoid circular dependencies

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class HoloMap 57.9% similar

    HoloMap is an n-dimensional mapping container that stores viewable elements or overlays indexed by tuple keys along declared key dimensions, enabling interactive exploration through widgets.

    From: /tf/active/vicechatdev/patches/spaces.py
  • function layer_groups 53.1% similar

    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.

    From: /tf/active/vicechatdev/patches/util.py
  • function sort_topologically 52.8% similar

    Performs stackless topological sorting on a directed acyclic graph (DAG), organizing nodes into levels based on their dependencies.

    From: /tf/active/vicechatdev/patches/util.py
  • function dimension_sort 46.0% similar

    Sorts an ordered dictionary by specified dimension keys, supporting both standard Python tuple sorting and categorical ordering for dimensions with predefined values.

    From: /tf/active/vicechatdev/patches/util.py
  • function get_overlay_spec 42.3% similar

    Constructs a specification tuple for an Element within an Overlay by combining the element's type name, group, label, and key dimensions.

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