function layer_sort
Computes a global topological ordering of layers from a HoloMap containing CompositeOverlay objects by analyzing layer dependencies and sorting them.
/tf/active/vicechatdev/patches/util.py
1406 - 1418
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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class HoloMap 57.9% similar
-
function layer_groups 53.1% similar
-
function sort_topologically 52.8% similar
-
function dimension_sort 46.0% similar
-
function get_overlay_spec 42.3% similar