function analyze_hierarchy
Analyzes a hierarchical database structure to extract statistics about nodes, their relationships, depths, and types.
/tf/active/vicechatdev/e-ink-llm/cloudtest/analyze_replica.py
35 - 67
simple
Purpose
This function processes a database dictionary containing nodes and hierarchy information to provide comprehensive statistics including root nodes identification, depth distribution analysis, node type categorization, and parent-child relationship counts. It's useful for understanding the structure and composition of hierarchical data systems, tree structures, or organizational charts stored in dictionary format.
Source Code
def analyze_hierarchy(database: Dict[str, Any]) -> Dict[str, Any]:
"""Analyze the hierarchy structure"""
nodes = database.get('nodes', {})
hierarchy = database.get('hierarchy', {})
# Find root nodes (nodes with no parent)
root_nodes = []
for uuid, node in nodes.items():
if not node.get('parent_uuid'):
root_nodes.append(uuid)
# Calculate depth statistics
depth_counts = {}
max_depth = 0
for uuid, node in nodes.items():
depth = node.get('depth', 0)
depth_counts[depth] = depth_counts.get(depth, 0) + 1
max_depth = max(max_depth, depth)
# Count node types
type_counts = {}
for uuid, node in nodes.items():
node_type = node.get('node_type', 'unknown')
type_counts[node_type] = type_counts.get(node_type, 0) + 1
return {
'root_nodes': root_nodes,
'depth_counts': depth_counts,
'max_depth': max_depth,
'type_counts': type_counts,
'total_parent_relationships': len(hierarchy)
}
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
database |
Dict[str, Any] | - | positional_or_keyword |
Parameter Details
database: A dictionary containing the hierarchical database structure. Expected to have two keys: 'nodes' (dict mapping UUIDs to node dictionaries with properties like 'parent_uuid', 'depth', and 'node_type') and 'hierarchy' (dict representing parent-child relationships). The 'nodes' dict should contain node objects with optional fields: 'parent_uuid' (string or None), 'depth' (integer), and 'node_type' (string).
Return Value
Type: Dict[str, Any]
Returns a dictionary with five keys: 'root_nodes' (list of UUIDs for nodes without parents), 'depth_counts' (dict mapping depth levels to count of nodes at that depth), 'max_depth' (integer representing the maximum depth in the hierarchy), 'type_counts' (dict mapping node types to their counts), and 'total_parent_relationships' (integer count of entries in the hierarchy dict).
Required Imports
from typing import Dict, Any
Usage Example
from typing import Dict, Any
def analyze_hierarchy(database: Dict[str, Any]) -> Dict[str, Any]:
nodes = database.get('nodes', {})
hierarchy = database.get('hierarchy', {})
root_nodes = []
for uuid, node in nodes.items():
if not node.get('parent_uuid'):
root_nodes.append(uuid)
depth_counts = {}
max_depth = 0
for uuid, node in nodes.items():
depth = node.get('depth', 0)
depth_counts[depth] = depth_counts.get(depth, 0) + 1
max_depth = max(max_depth, depth)
type_counts = {}
for uuid, node in nodes.items():
node_type = node.get('node_type', 'unknown')
type_counts[node_type] = type_counts.get(node_type, 0) + 1
return {
'root_nodes': root_nodes,
'depth_counts': depth_counts,
'max_depth': max_depth,
'type_counts': type_counts,
'total_parent_relationships': len(hierarchy)
}
# Example usage
database = {
'nodes': {
'uuid1': {'parent_uuid': None, 'depth': 0, 'node_type': 'root'},
'uuid2': {'parent_uuid': 'uuid1', 'depth': 1, 'node_type': 'branch'},
'uuid3': {'parent_uuid': 'uuid1', 'depth': 1, 'node_type': 'branch'},
'uuid4': {'parent_uuid': 'uuid2', 'depth': 2, 'node_type': 'leaf'}
},
'hierarchy': {
'uuid1': ['uuid2', 'uuid3'],
'uuid2': ['uuid4']
}
}
result = analyze_hierarchy(database)
print(result)
# Output: {'root_nodes': ['uuid1'], 'depth_counts': {0: 1, 1: 2, 2: 1}, 'max_depth': 2, 'type_counts': {'root': 1, 'branch': 2, 'leaf': 1}, 'total_parent_relationships': 2}
Best Practices
- Ensure the database dictionary contains both 'nodes' and 'hierarchy' keys for complete analysis
- Node dictionaries should include 'parent_uuid', 'depth', and 'node_type' fields for accurate statistics
- The function handles missing keys gracefully using .get() with defaults, but providing complete data yields better results
- Root nodes are identified by the absence of 'parent_uuid' or a falsy value for that field
- The function assumes depth values are pre-calculated in the node data; it does not compute depth from hierarchy
- For large hierarchies, consider the memory implications of storing all statistics in the returned dictionary
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_folder_hierarchy 58.1% similar
-
function print_database_analysis 56.6% similar
-
function get_database_statistics 56.6% similar
-
function create_folder_hierarchy_v2 56.6% similar
-
function create_folder_hierarchy_v1 56.1% similar